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.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link DOMRect}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  public class DOMRectTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts({"100", "400", "100", "450", "50", "0"})
34      public void properties() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><head>\n"
37              + "<script>\n"
38              + LOG_TITLE_FUNCTION
39              + "  function test() {\n"
40              + "    try {\n"
41              + "    var d1 = document.getElementById('div1');\n"
42              + "    var pos = d1.getBoundingClientRect();\n"
43              + "    log(pos.top);\n"
44              + "    log(pos.left);\n"
45              + "    log(pos.bottom);\n"
46              + "    log(pos.right);\n"
47              + "    log(pos.width);\n"
48              + "    log(pos.height);\n"
49              + "    } catch(e) { logEx(e);}\n"
50              + "  }\n"
51              + "</script></head><body onload='test()'>\n"
52              + "<div id='outer' style='position: absolute; left: 400px; top: 100px; width: 50px; height: 80px;'>\n"
53              + "<div id='div1'></div></div>\n"
54              + "</body></html>";
55          loadPageVerifyTitle2(html);
56      }
57  
58      /**
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts({"true", "true"})
63      public void getBoundingClientRect_WidthPercent() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html>\n"
66              + "<head>\n"
67              + "  <script>\n"
68              + LOG_TITLE_FUNCTION
69              + "    function test() {\n"
70              + "      var input = document.getElementById('myInput');\n"
71              + "      log(input.getBoundingClientRect().height > 10);\n"
72              + "      log(input.getBoundingClientRect().width > 100);\n"
73              + "    }\n"
74              + "  </script>\n"
75              + "</head>\n"
76              + "<body onload='test()'>\n"
77              + "<style>.full { width:100%; }</style>\n"
78              + "<div class='foo bar'>\n"
79              + "  <form action='javascript:void(0)' method='post'>\n"
80              + "    <div class='full'>\n"
81              + "      <input class='full' type='text' id='myInput'>\n"
82              + "    </div>\n"
83              + "  </form>\n"
84              + "</div>\n"
85              + "</body></html>";
86          loadPageVerifyTitle2(html);
87      }
88  }