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.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Unit tests for {@link HTMLDivElement}.
25   *
26   * @author Daniel Gredler
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   * @author Frank Danek
31   */
32  @RunWith(BrowserRunner.class)
33  public class HTMLDivElementTest extends WebDriverTestCase {
34  
35      /**
36       * @throws Exception if an error occurs
37       */
38      @Test
39      @Alerts("no")
40      public void doScroll() throws Exception {
41          final String html = DOCTYPE_HTML
42              + "<html>\n"
43              + "  <head>\n"
44              + "    <script>\n"
45              + LOG_TITLE_FUNCTION
46              + "      function test() {\n"
47              + "        var div = document.getElementById('d');\n"
48              + "        if(div.doScroll) {\n"
49              + "          log('yes');\n"
50              + "          div.doScroll();\n"
51              + "          div.doScroll('down');\n"
52              + "        } else {\n"
53              + "          log('no');\n"
54              + "        }\n"
55              + "      }\n"
56              + "    </script>\n"
57              + "  </head>\n"
58              + "  <body onload='test()'><div id='d'>abc</div></body>\n"
59              + "</html>";
60  
61          loadPageVerifyTitle2(html);
62      }
63  
64      /**
65       * @throws Exception if an error occurs
66       */
67      @Test
68      @Alerts({"left", "right", "justify", "center", "wrong", ""})
69      public void getAlign() throws Exception {
70          final String html = DOCTYPE_HTML
71              + "<html><body>\n"
72              + "  <table>\n"
73              + "    <div id='d1' align='left' ></div>\n"
74              + "    <div id='d2' align='right' ></div>\n"
75              + "    <div id='d3' align='justify' ></div>\n"
76              + "    <div id='d4' align='center' ></div>\n"
77              + "    <div id='d5' align='wrong' ></div>\n"
78              + "    <div id='d6' ></div>\n"
79              + "  </table>\n"
80  
81              + "<script>\n"
82              + LOG_TITLE_FUNCTION
83              + "  for (var i = 1; i <= 6; i++) {\n"
84              + "    log(document.getElementById('d' + i).align);\n"
85              + "  }\n"
86              + "</script>\n"
87              + "</body></html>";
88  
89          loadPageVerifyTitle2(html);
90      }
91  
92      /**
93       * @throws Exception if an error occurs
94       */
95      @Test
96      @Alerts({"CenTer", "8", "foo", "left", "right", "justify", "center"})
97      public void setAlign() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html><body>\n"
100             + "  <table>\n"
101             + "    <div id='d1' align='left' ></div>\n"
102             + "  </table>\n"
103 
104             + "<script>\n"
105             + LOG_TITLE_FUNCTION
106             + "  function setAlign(elem, value) {\n"
107             + "    try {\n"
108             + "      elem.align = value;\n"
109             + "    } catch(e) { logEx(e); }\n"
110             + "    log(elem.align);\n"
111             + "  }\n"
112 
113             + "  var elem = document.getElementById('d1');\n"
114             + "  setAlign(elem, 'CenTer');\n"
115 
116             + "  setAlign(elem, '8');\n"
117             + "  setAlign(elem, 'foo');\n"
118 
119             + "  setAlign(elem, 'left');\n"
120             + "  setAlign(elem, 'right');\n"
121             + "  setAlign(elem, 'justify');\n"
122             + "  setAlign(elem, 'center');\n"
123             + "</script>\n"
124             + "</body></html>";
125 
126         loadPageVerifyTitle2(html);
127     }
128 
129     /**
130      * A similar test is used by jQuery-1.4.1 to detect browser capacities.
131      * @throws Exception if an error occurs
132      */
133     @Test
134     @Alerts({"null", "true", "null", "true"})
135     public void handlers() throws Exception {
136         final String html = DOCTYPE_HTML
137             + "<html><body>\n"
138             + "<div id='d1'></div>\n"
139             + "<script>\n"
140             + LOG_TITLE_FUNCTION
141             + "  var d = document.getElementById('d1');\n"
142             + "  log(d.onchange);\n"
143             + "  log('onchange' in d);\n"
144             + "  log(d.onsubmit);\n"
145             + "  log('onsubmit' in d);\n"
146             + "</script>\n"
147             + "</body></html>";
148 
149         loadPageVerifyTitle2(html);
150     }
151 
152     /**
153      * @throws Exception if an error occurs
154      */
155     @Test
156     @Alerts({"true", "true", "true", "true", "true", "true"})
157     public void clientHeight() throws Exception {
158         final String html = DOCTYPE_HTML
159             + "<html>\n"
160             + "<head>\n"
161             + "  <script>\n"
162             + LOG_TITLE_FUNCTION
163             + "    function test() {\n"
164             + "      var elt = document.getElementById('emptyDiv');\n"
165             + "      log(elt.clientHeight == 0);\n"
166 
167             + "      elt = document.getElementById('textDiv');\n"
168             + "      log(elt.clientHeight > 15);\n"
169 
170             + "      elt = document.getElementById('styleDiv0');\n"
171             + "      log(elt.clientHeight == 0);\n"
172 
173             + "      elt = document.getElementById('styleDiv10');\n"
174             + "      log(elt.clientHeight > 5);\n"
175 
176             + "      elt = document.getElementById('styleDivAuto');\n"
177             + "      log(elt.clientHeight > 15);\n"
178 
179             + "      elt = document.getElementById('styleDivAutoEmpty');\n"
180             + "      log(elt.clientHeight == 0);\n"
181             + "    }\n"
182             + "  </script>\n"
183             + "</head>\n"
184             + "<body onload='test()'>\n"
185             + "  <div id='emptyDiv'></div>\n"
186             + "  <div id='textDiv'>HtmlUnit</div>\n"
187             + "  <div id='styleDiv0' style='height: 0px'>HtmlUnit</div>\n"
188             + "  <div id='styleDiv10' style='height: 10px'>HtmlUnit</div>\n"
189             + "  <div id='styleDivAuto' style='height: auto'>HtmlUnit</div>\n"
190             + "  <div id='styleDivAutoEmpty' style='height: auto'></div>\n"
191             + "</body></html>";
192 
193         loadPageVerifyTitle2(html);
194     }
195 
196     /**
197      * @throws Exception if an error occurs
198      */
199     @Test
200     @Alerts({"true", "true", "true", "true", "true", "true"})
201     public void clientWidth() throws Exception {
202         final String html = DOCTYPE_HTML
203             + "<html>\n"
204             + "<head>\n"
205             + "  <script>\n"
206             + LOG_TITLE_FUNCTION
207             + "    function test() {\n"
208             + "      var elt = document.getElementById('emptyDiv');\n"
209             + "      log(elt.clientWidth > 500);\n"
210 
211             + "      elt = document.getElementById('textDiv');\n"
212             + "      log(elt.clientWidth > 500);\n"
213 
214             + "      elt = document.getElementById('styleDiv0');\n"
215             + "      log(elt.clientWidth == 0);\n"
216 
217             + "      elt = document.getElementById('styleDiv10');\n"
218             + "      log(elt.clientWidth > 8);\n"
219 
220             + "      elt = document.getElementById('styleDivAuto');\n"
221             + "      log(elt.clientWidth > 500);\n"
222 
223             + "      elt = document.getElementById('styleDivAutoEmpty');\n"
224             + "      log(elt.clientWidth > 500);\n"
225             + "    }\n"
226             + "  </script>\n"
227             + "</head>\n"
228             + "<body onload='test()'>\n"
229             + "  <div id='emptyDiv'></div>\n"
230             + "  <div id='textDiv'>HtmlUnit</div>\n"
231             + "  <div id='styleDiv0' style='width: 0px'>HtmlUnit</div>\n"
232             + "  <div id='styleDiv10' style='width: 10px'>HtmlUnit</div>\n"
233             + "  <div id='styleDivAuto' style='width: auto'>HtmlUnit</div>\n"
234             + "  <div id='styleDivAutoEmpty' style='width: auto'></div>\n"
235             + "</body></html>";
236 
237         loadPageVerifyTitle2(html);
238     }
239 
240     /**
241      * @throws Exception if an error occurs
242      */
243     @Test
244     @Alerts({"true", "true", "true", "true", "true", "true"})
245     public void clientWidthNested() 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 elt = document.getElementById('emptyDiv');\n"
253             + "      log(elt.clientWidth > 500);\n"
254 
255             + "      elt = document.getElementById('textDiv');\n"
256             + "      log(elt.clientWidth > 500);\n"
257 
258             + "      elt = document.getElementById('styleDiv0');\n"
259             + "      log(elt.clientWidth == 0);\n"
260 
261             + "      elt = document.getElementById('styleDiv10');\n"
262             + "      log(elt.clientWidth > 8);\n"
263 
264             + "      elt = document.getElementById('styleDivAuto');\n"
265             + "      log(elt.clientWidth > 500);\n"
266 
267             + "      elt = document.getElementById('styleDivAutoEmpty');\n"
268             + "      log(elt.clientWidth > 500);\n"
269             + "    }\n"
270             + "  </script>\n"
271             + "</head>\n"
272             + "<body onload='test()'>\n"
273             + "  <div id='emptyDiv'><div></div></div>\n"
274             + "  <div id='textDiv'><div>HtmlUnit</div></div>\n"
275             + "  <div id='styleDiv0' style='width: 0px'><div>HtmlUnit</div></div>\n"
276             + "  <div id='styleDiv10' style='width: 10px'><div>HtmlUnit</div></div>\n"
277             + "  <div id='styleDivAuto' style='width: auto'><div>HtmlUnit</div></div>\n"
278             + "  <div id='styleDivAutoEmpty' style='width: auto'><div></div></div>\n"
279             + "</body></html>";
280 
281         loadPageVerifyTitle2(html);
282     }
283 
284     /**
285      * @throws Exception if an error occurs
286      */
287     @Test
288     @Alerts({"undefined", "null", "nowrap", "null", "x", "null", "x", "blah", "", "blah"})
289     public void noWrap() throws Exception {
290         final String html = DOCTYPE_HTML
291             + "<html>\n"
292             + "  <head>\n"
293             + "    <script>\n"
294             + LOG_TITLE_FUNCTION
295             + "      function test() {\n"
296             + "        var div = document.getElementById('test');\n"
297             + "        log(div.noWrap);\n"
298             + "        log(div.getAttribute('noWrap'));\n"
299             + "        div.noWrap = 'nowrap';\n"
300             + "        log(div.noWrap);\n"
301             + "        log(div.getAttribute('noWrap'));\n"
302             + "        div.noWrap = 'x';\n"
303             + "        log(div.noWrap);\n"
304             + "        log(div.getAttribute('noWrap'));\n"
305             + "        div.setAttribute('noWrap', 'blah');\n"
306             + "        log(div.noWrap);\n"
307             + "        log(div.getAttribute('noWrap'));\n"
308             + "        div.noWrap = '';\n"
309             + "        log(div.noWrap);\n"
310             + "        log(div.getAttribute('noWrap'));\n"
311             + "      }\n"
312             + "    </script>\n"
313             + "  </head>\n"
314             + "  <body onload='test()'>\n"
315             + "  <div id='test'>div</div>\n"
316             + "  </body>\n"
317             + "</html>";
318 
319         loadPageVerifyTitle2(html);
320     }
321 }