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.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.htmlunit.util.MimeType;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  import org.openqa.selenium.By;
25  import org.openqa.selenium.WebDriver;
26  
27  /**
28   * Unit tests for {@link HTMLBodyElement}.
29   *
30   * @author Daniel Gredler
31   * @author Ahmed Ashour
32   * @author Marc Guillemot
33   * @author Ronald Brill
34   * @author Frank Danek
35   */
36  @RunWith(BrowserRunner.class)
37  public class HTMLBodyElementTest extends WebDriverTestCase {
38  
39      /**
40       * Tests the default body padding and margins.
41       * @throws Exception if an error occurs
42       */
43      @Test
44      @Alerts({"0px,0px,0px,0px,0px", ",,,,", "8px,8px,8px,8px,8px", ",,,,"})
45      @HtmlUnitNYI(CHROME = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"},
46              EDGE = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"},
47              FF = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"},
48              FF_ESR = {"0px,0px,0px,0px,0px", ",,,,", "0px,8px,8px,8px,8px", ",,,,"})
49      public void defaultPaddingAndMargins() throws Exception {
50          final String html = DOCTYPE_HTML
51              + "<html>\n"
52              + "  <head>\n"
53              + "    <script>\n"
54              + LOG_TITLE_FUNCTION
55              + "      function test() {\n"
56              + "        var b = document.getElementById('body');\n"
57              + "        var s = b.currentStyle ? b.currentStyle : getComputedStyle(b, null);\n"
58              + "        log(s.padding + ',' + s.paddingLeft + ',' + s.paddingRight + ',' + s.paddingTop + ',' + s.paddingBottom);\n"
59              + "        log(b.style.padding + ',' + b.style.paddingLeft + ',' + b.style.paddingRight + ',' + b.style.paddingTop + ',' + b.style.paddingBottom);\n"
60              + "        log(s.margin + ',' + s.marginLeft + ',' + s.marginRight + ',' + s.marginTop + ',' + s.marginBottom);\n"
61              + "        log(b.style.margin + ',' + b.style.marginLeft + ',' + b.style.marginRight + ',' + b.style.marginTop + ',' + b.style.marginBottom);\n"
62              + "      }\n"
63              + "    </script>\n"
64              + "  </head>\n"
65              + "  <body id='body' onload='test()'>blah</body>\n"
66              + "</html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if an error occurs
73       */
74      @Test
75      @Alerts("TypeError")
76      public void attachEvent() throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html>\n"
79              + "  <head>\n"
80              + "    <script>\n"
81              + LOG_TITLE_FUNCTION
82              + "      function handler() {\n"
83              + "        log(event);\n"
84              + "      }\n"
85              + "      function test() {\n"
86              + "        try {\n"
87              + "          document.body.attachEvent('onclick', handler);\n"
88              + "        } catch(e) { logEx(e); }\n"
89              + "      }\n"
90              + "    </script>\n"
91              + "  </head>\n"
92              + "  <body onload='test()'>\n"
93              + "    <input type='button' id='myInput' value='Test me'>\n"
94              + "  </body>\n"
95              + "</html>";
96  
97          final WebDriver driver = loadPageVerifyTitle2(html);
98  
99          driver.findElement(By.id("myInput")).click();
100     }
101 
102     /**
103      * @throws Exception if an error occurs
104      */
105     @Test
106     @Alerts("no")
107     public void doScroll() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html>\n"
110             + "  <head>\n"
111             + "    <script>\n"
112             + LOG_TITLE_FUNCTION
113             + "      function test() {\n"
114             + "        if(document.body.doScroll) {\n"
115             + "          log('yes');\n"
116             + "          document.body.doScroll();\n"
117             + "          document.body.doScroll('down');\n"
118             + "        } else {\n"
119             + "          log('no');\n"
120             + "        }\n"
121             + "      }\n"
122             + "    </script>\n"
123             + "  </head>\n"
124             + "  <body onload='test()'>\n"
125             + "  </body>\n"
126             + "</html>";
127 
128         loadPageVerifyTitle2(html);
129     }
130 
131     /**
132      * @throws Exception if an error occurs
133      */
134     @Test
135     @Alerts({"", "#0000aa", "x"})
136     public void aLink() throws Exception {
137         final String html = DOCTYPE_HTML
138             + "<html>\n"
139             + "  <head>\n"
140             + "    <script>\n"
141             + LOG_TITLE_FUNCTION
142             + "      function test() {\n"
143             + "        var b = document.getElementById('body');\n"
144             + "        log(b.aLink);\n"
145             + "        b.aLink = '#0000aa';\n"
146             + "        log(b.aLink);\n"
147             + "        b.aLink = 'x';\n"
148             + "        log(b.aLink);\n"
149             + "      }\n"
150             + "    </script>\n"
151             + "  </head>\n"
152             + "  <body id='body' onload='test()'>blah</body>\n"
153             + "</html>";
154 
155         loadPageVerifyTitle2(html);
156     }
157 
158     /**
159      * @throws Exception if an error occurs
160      */
161     @Test
162     @Alerts({"", "http://www.foo.com/blah.gif", "blah.gif"})
163     public void background() throws Exception {
164         final String html = DOCTYPE_HTML
165             + "<html>\n"
166             + "  <head>\n"
167             + "    <script>\n"
168             + LOG_TITLE_FUNCTION
169             + "      function test() {\n"
170             + "        var b = document.getElementById('body');\n"
171             + "        log(b.background);\n"
172             + "        b.background = 'http://www.foo.com/blah.gif';\n"
173             + "        log(b.background);\n"
174             + "        b.background = 'blah.gif';\n"
175             + "        log(b.background);\n"
176             + "      }\n"
177             + "    </script>\n"
178             + "  </head>\n"
179             + "  <body id='body' onload='test()'>blah</body>\n"
180             + "</html>";
181         getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
182 
183         loadPageVerifyTitle2(html);
184     }
185 
186     /**
187      * @throws Exception if an error occurs
188      */
189     @Test
190     @Alerts({"", "#0000aa", "x"})
191     public void bgColor() throws Exception {
192         final String html = DOCTYPE_HTML
193             + "<html>\n"
194             + "  <head>\n"
195             + "    <script>\n"
196             + LOG_TITLE_FUNCTION
197             + "      function test() {\n"
198             + "        var b = document.getElementById('body');\n"
199             + "        log(b.bgColor);\n"
200             + "        b.bgColor = '#0000aa';\n"
201             + "        log(b.bgColor);\n"
202             + "        b.bgColor = 'x';\n"
203             + "        log(b.bgColor);\n"
204             + "      }\n"
205             + "    </script>\n"
206             + "  </head>\n"
207             + "  <body id='body' onload='test()'>blah</body>\n"
208             + "</html>";
209 
210         loadPageVerifyTitle2(html);
211     }
212 
213     /**
214      * @throws Exception if an error occurs
215      */
216     @Test
217     @Alerts({"", "#0000aa", "x"})
218     public void link() throws Exception {
219         final String html = DOCTYPE_HTML
220             + "<html>\n"
221             + "  <head>\n"
222             + "    <script>\n"
223             + LOG_TITLE_FUNCTION
224             + "      function test() {\n"
225             + "        var b = document.getElementById('body');\n"
226             + "        log(b.link);\n"
227             + "        b.link = '#0000aa';\n"
228             + "        log(b.link);\n"
229             + "        b.link = 'x';\n"
230             + "        log(b.link);\n"
231             + "      }\n"
232             + "    </script>\n"
233             + "  </head>\n"
234             + "  <body id='body' onload='test()'>blah</body>\n"
235             + "</html>";
236 
237         loadPageVerifyTitle2(html);
238     }
239 
240     /**
241      * @throws Exception if an error occurs
242      */
243     @Test
244     @Alerts({"", "#0000aa", "x"})
245     public void text() throws Exception {
246         final String html = DOCTYPE_HTML
247             + "<html>\n"
248             + "  <head>\n"
249             + "    <script>\n"
250             + LOG_TITLE_FUNCTION
251             + "      function test() {\n"
252             + "        var b = document.getElementById('body');\n"
253             + "        log(b.text);\n"
254             + "        b.text = '#0000aa';\n"
255             + "        log(b.text);\n"
256             + "        b.text = 'x';\n"
257             + "        log(b.text);\n"
258             + "      }\n"
259             + "    </script>\n"
260             + "  </head>\n"
261             + "  <body id='body' onload='test()'>blah</body>\n"
262             + "</html>";
263 
264         loadPageVerifyTitle2(html);
265     }
266 
267     /**
268      * @throws Exception if an error occurs
269      */
270     @Test
271     @Alerts({"", "#0000aa", "x"})
272     public void vLink() throws Exception {
273         final String html = DOCTYPE_HTML
274             + "<html>\n"
275             + "  <head>\n"
276             + "    <script>\n"
277             + LOG_TITLE_FUNCTION
278             + "      function test() {\n"
279             + "        var b = document.getElementById('body');\n"
280             + "        log(b.vLink);\n"
281             + "        b.vLink = '#0000aa';\n"
282             + "        log(b.vLink);\n"
283             + "        b.vLink = 'x';\n"
284             + "        log(b.vLink);\n"
285             + "      }\n"
286             + "    </script>\n"
287             + "  </head>\n"
288             + "  <body id='body' onload='test()'>blah</body>\n"
289             + "</html>";
290 
291         loadPageVerifyTitle2(html);
292     }
293 
294     /**
295      * @throws Exception if an error occurs
296      */
297     @Test
298     @Alerts({"function HTMLBodyElement() { [native code] }", ""
299                     + "ELEMENT_NODE, ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, ENTITY_REFERENCE_NODE, "
300                     + "ENTITY_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, "
301                     + "DOCUMENT_FRAGMENT_NODE, NOTATION_NODE, DOCUMENT_POSITION_DISCONNECTED, "
302                     + "DOCUMENT_POSITION_PRECEDING, "
303                     + "DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_CONTAINED_BY, "
304                     + "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, "})
305     public void enumeratedProperties() throws Exception {
306         final String html = DOCTYPE_HTML
307             + "<html><head>\n"
308             + "<script>\n"
309             + LOG_TITLE_FUNCTION
310             + "  function test() {\n"
311             + "    var str = '';\n"
312             + "    try {\n"
313             + "      log(HTMLBodyElement);\n"
314             + "      var str = '';\n"
315             + "      for (var i in HTMLBodyElement)\n"
316             + "        str += i + ', ';\n"
317             + "      log(str);\n"
318             + "    } catch(e) { logEx(e)}\n"
319             + "  }\n"
320             + "</script>\n"
321             + "</head>\n"
322             + "<body onload='test()'>\n"
323             + "</body></html>";
324 
325         loadPageVerifyTitle2(html);
326     }
327 
328     /**
329      * @throws Exception if an error occurs
330      */
331     @Test
332     @Alerts({"keydown (target)", "keydown (body)"})
333     public void eventHandler() throws Exception {
334         final String html = DOCTYPE_HTML
335             + "<html>\n"
336             + "  <head>\n"
337             + "    <script>\n"
338             + LOG_TITLE_FUNCTION
339             + "      function test() {\n"
340             + "        var target = document.getElementById('target');\n"
341             + "        target.onkeydown = function() {\n"
342             + "          log('keydown (target)');\n"
343             + "        };\n"
344             + "        document.body.onkeydown = function() {\n"
345             + "          log('keydown (body)');\n"
346             + "        };\n"
347             + "      }\n"
348             + "    </script>\n"
349             + "  </head>\n"
350             + "  <body onload='test()'>\n"
351             + "    <input id='target' type='checkbox'>\n"
352             + "  </body>\n"
353             + "</html>";
354 
355         final WebDriver driver = loadPage2(html);
356         driver.findElement(By.id("target")).sendKeys("a");
357 
358         verifyTitle2(DEFAULT_WAIT_TIME, driver, getExpectedAlerts());
359     }
360 
361     /**
362      * @throws Exception if an error occurs
363      */
364     @Test
365     @Alerts({"0", "8"})
366     @HtmlUnitNYI(CHROME = {"0", "0"},
367             EDGE = {"0", "0"},
368             FF = {"0", "0"},
369             FF_ESR = {"0", "0"})
370     public void top() throws Exception {
371         final String html = DOCTYPE_HTML
372             + "<html>\n"
373             + "  <head>\n"
374             + "    <script>\n"
375             + LOG_TITLE_FUNCTION
376             + "      function test() {\n"
377             + "        var b = document.getElementById('body');\n"
378             + "        log(b.offsetTop);\n"
379             + "        log(b.getBoundingClientRect().top);\n"
380             + "      }\n"
381             + "    </script>\n"
382             + "  </head>\n"
383             + "  <body id='body' onload='test()'>blah</body>\n"
384             + "</html>";
385 
386         loadPageVerifyTitle2(html);
387     }
388 
389 }