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