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