View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link DOMRect}.
25   *
26   * @author Ahmed Ashour
27   */
28  @RunWith(BrowserRunner.class)
29  public class DOMRectTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"100", "400", "100", "450", "50", "0"})
36      public void properties() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><head>\n"
39              + "<script>\n"
40              + LOG_TITLE_FUNCTION
41              + "  function test() {\n"
42              + "    try {\n"
43              + "    var d1 = document.getElementById('div1');\n"
44              + "    var pos = d1.getBoundingClientRect();\n"
45              + "    log(pos.top);\n"
46              + "    log(pos.left);\n"
47              + "    log(pos.bottom);\n"
48              + "    log(pos.right);\n"
49              + "    log(pos.width);\n"
50              + "    log(pos.height);\n"
51              + "    } catch(e) { logEx(e);}\n"
52              + "  }\n"
53              + "</script></head><body onload='test()'>\n"
54              + "<div id='outer' style='position: absolute; left: 400px; top: 100px; width: 50px; height: 80px;'>\n"
55              + "<div id='div1'></div></div>\n"
56              + "</body></html>";
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts({"true", "true"})
65      public void getBoundingClientRect_WidthPercent() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html>\n"
68              + "<head>\n"
69              + "  <script>\n"
70              + LOG_TITLE_FUNCTION
71              + "    function test() {\n"
72              + "      var input = document.getElementById('myInput');\n"
73              + "      log(input.getBoundingClientRect().height > 10);\n"
74              + "      log(input.getBoundingClientRect().width > 100);\n"
75              + "    }\n"
76              + "  </script>\n"
77              + "</head>\n"
78              + "<body onload='test()'>\n"
79              + "<style>.full { width:100%; }</style>\n"
80              + "<div class='foo bar'>\n"
81              + "  <form action='javascript:void(0)' method='post'>\n"
82              + "    <div class='full'>\n"
83              + "      <input class='full' type='text' id='myInput'>\n"
84              + "    </div>\n"
85              + "  </form>\n"
86              + "</div>\n"
87              + "</body></html>";
88          loadPageVerifyTitle2(html);
89      }
90  }