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