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   * Unit tests for BoxObject.
23   *
24   * @author Daniel Gredler
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class BoxObjectTest extends WebDriverTestCase {
29  
30      /**
31       * Tests box object attributes relating to HTML elements: firstChild, lastChild, previousSibling, etc.
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts("TypeError")
36      public void elementAttributes() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html>\n"
39              + "  <body onload='test()'>\n"
40              + "    <span id='foo'>foo</span><div id='d'><span id='a'>a</span><span id='b'>b</span></div><span id='bar'>bar</span>\n"
41              + "    <script>\n"
42              + LOG_TITLE_FUNCTION
43              + "      function test() {\n"
44              + "        try {\n"
45              + "          var div = document.getElementById('d');\n"
46              + "          var spanFoo = document.getElementById('foo');\n"
47              + "          var spanA = document.getElementById('a');\n"
48              + "          var spanB = document.getElementById('b');\n"
49              + "          var spanBar = document.getElementById('bar');\n"
50              + "          var box = document.getBoxObjectFor(div);\n"
51              + "          log(box.element == div);\n"
52              + "          log(box.firstChild == spanA);\n"
53              + "          log(box.lastChild == spanB);\n"
54              + "          log(box.previousSibling == spanFoo);\n"
55              + "          log(box.nextSibling == spanBar);\n"
56              + "        } catch(e) { logEx(e) }\n"
57              + "      }\n"
58              + "    </script>\n"
59              + "  </body>\n"
60              + "</html>";
61  
62          loadPageVerifyTitle2(html);
63      }
64  
65      /**
66       * Tests box object attributes relating to position and size: x, y, screenX, screenY, etc.
67       * @throws Exception if an error occurs
68       */
69      @Test
70      @Alerts("TypeError")
71      public void positionAndSizeAttributes() throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html>\n"
74              + "  <body onload='test()'>\n"
75              + "    <style>#d { position:absolute; left:50px; top:100px; width:500px; height:400px; border:3px; padding: 5px; margin: 23px; }</style>\n"
76              + "    <div id='d'>daniel</div>\n"
77              + "    <script>\n"
78              + LOG_TITLE_FUNCTION
79              + "      function test() {\n"
80              + "        try {\n"
81              + "          var div = document.getElementById('d');\n"
82              + "          var box = document.getBoxObjectFor(div);\n"
83              + "          log(box.x + '-' + box.y);\n"
84              + "          log(box.screenX + '-' + box.screenY);\n"
85              + "          log(box.width + '-' + box.height);\n"
86              + "        } catch(e) { logEx(e) }\n"
87              + "      }\n"
88              + "    </script>\n"
89              + "  </body>\n"
90              + "</html>";
91  
92          loadPageVerifyTitle2(html);
93      }
94  
95  }