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 DOMRectList}.
23   *
24   * @author Ronald Brill
25   */
26  public class DOMRectListTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"[object DOMRectList]", "1", "[object DOMRect]", "[object DOMRect]"})
33      public void getClientRects() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html><head>\n"
36              + "<script>\n"
37              + LOG_TITLE_FUNCTION
38              + "  function test() {\n"
39              + "    var d1 = document.getElementById('div1');\n"
40              + "    var rects = d1.getClientRects();\n"
41              + "    log(rects);\n"
42              + "    log(rects.length);\n"
43              + "    log(rects[0]);\n"
44              + "    log(rects.item(0));\n"
45              + "  }\n"
46              + "</script></head>\n"
47              + "<body onload='test()'>\n"
48              + "  <div id='div1'/>\n"
49              + "</body></html>";
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts({"[object DOMRectList]", "1", "null", "null"})
58      public void itemOutside() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><head>\n"
61              + "<script>\n"
62              + LOG_TITLE_FUNCTION
63              + "  function test() {\n"
64              + "    var d1 = document.getElementById('div1');\n"
65              + "    var rects = d1.getClientRects();\n"
66  
67              + "    log(rects);\n"
68              + "    log(rects.length);\n"
69  
70              + "    try {\n"
71              + "      log(rects.item(1));\n"
72              + "    } catch(e) { logEx(e); }\n"
73  
74              + "    try {\n"
75              + "      log(rects.item(-1));\n"
76              + "    } catch(e) { logEx(e); }\n"
77              + "  }\n"
78              + "</script></head>\n"
79              + "<body onload='test()'>\n"
80              + "  <div id='div1'/>\n"
81              + "</body></html>";
82          loadPageVerifyTitle2(html);
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      @Alerts({"[object DOMRectList]", "1", "undefined", "undefined"})
90      public void indexOutside() throws Exception {
91          final String html = DOCTYPE_HTML
92              + "<html><head>\n"
93              + "<script>\n"
94              + LOG_TITLE_FUNCTION
95              + "  function test() {\n"
96              + "    var d1 = document.getElementById('div1');\n"
97              + "    var rects = d1.getClientRects();\n"
98  
99              + "    log(rects);\n"
100             + "    log(rects.length);\n"
101 
102             + "    try {\n"
103             + "      log(rects[1]);\n"
104             + "    } catch(e) { logEx(e); }\n"
105 
106             + "    try {\n"
107             + "      log(rects[-1]);\n"
108             + "    } catch(e) { logEx(e); }\n"
109             + "  }\n"
110             + "</script></head>\n"
111             + "<body onload='test()'>\n"
112             + "  <div id='div1'/>\n"
113             + "</body></html>";
114         loadPageVerifyTitle2(html);
115     }
116 
117     /**
118      * @throws Exception if the test fails
119      */
120     @Test
121     @Alerts({"[object DOMRectList]", "0", "undefined", "undefined"})
122     public void empty() throws Exception {
123         final String html = DOCTYPE_HTML
124             + "<html><head>\n"
125             + "<script>\n"
126             + LOG_TITLE_FUNCTION
127             + "  function test() {\n"
128             + "    var d1 = document.createElement('div');\n"
129             + "    var rects = d1.getClientRects();\n"
130 
131             + "    log(rects);\n"
132             + "    log(rects.length);\n"
133 
134             + "    try {\n"
135             + "      log(rects[1]);\n"
136             + "    } catch(e) { logEx(e); }\n"
137 
138             + "    try {\n"
139             + "      log(rects[-1]);\n"
140             + "    } catch(e) { logEx(e); }\n"
141             + "  }\n"
142             + "</script></head>\n"
143             + "<body onload='test()'>\n"
144             + "  <div id='div1'/>\n"
145             + "</body></html>";
146         loadPageVerifyTitle2(html);
147     }
148 }