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   * Tests for {@link HTMLHeadingElement}.
23   * @author Ronald Brill
24   */
25  public class HTMLHeadingElementTest extends WebDriverTestCase {
26  
27      /**
28       * @throws Exception if an error occurs
29       */
30      @Test
31      @Alerts({"left", "right", "center", "justify", "wrong", ""})
32      public void getAlign() throws Exception {
33          final String html = DOCTYPE_HTML
34              + "<html><body>\n"
35              + "  <h1 id='h1' align='left' ></h1>\n"
36              + "  <h2 id='h2' align='right' ></h2>\n"
37              + "  <h3 id='h3' align='center' ></h3>\n"
38              + "  <h2 id='h4' align='justify' ></h2>\n"
39              + "  <h2 id='h5' align='wrong' ></h2>\n"
40              + "  <h2 id='h6' ></h2>\n"
41  
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "  for (var i = 1; i <= 6; i++) {\n"
45              + "    log(document.getElementById('h' + i).align);\n"
46              + "  }\n"
47              + "</script>\n"
48              + "</body></html>";
49  
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if an error occurs
55       */
56      @Test
57      @Alerts({"CenTer", "8", "foo", "left", "right", "center", "justify"})
58      public void setAlign() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><body>\n"
61              + "  <h2 id='e1' align='left' ></h2>\n"
62  
63              + "<script>\n"
64              + LOG_TITLE_FUNCTION
65              + "  function setAlign(elem, value) {\n"
66              + "    try {\n"
67              + "      elem.align = value;\n"
68              + "    } catch(e) { logEx(e); }\n"
69              + "    log(elem.align);\n"
70              + "  }\n"
71  
72              + "  var elem = document.getElementById('e1');\n"
73              + "  setAlign(elem, 'CenTer');\n"
74  
75              + "  setAlign(elem, '8');\n"
76              + "  setAlign(elem, 'foo');\n"
77  
78              + "  setAlign(elem, 'left');\n"
79              + "  setAlign(elem, 'right');\n"
80              + "  setAlign(elem, 'center');\n"
81              + "  setAlign(elem, 'justify');\n"
82              + "</script>\n"
83              + "</body></html>";
84  
85          loadPageVerifyTitle2(html);
86      }
87  
88      /**
89       * @throws Exception if an error occurs
90       */
91      @Test
92      @Alerts(DEFAULT = {"37", "27", "22", "18", "16", "12"},
93              FF = {"38", "28", "22", "18", "16", "13"},
94              FF_ESR = {"38", "28", "22", "18", "16", "13"})
95      public void clientHeight() throws Exception {
96          final String html = DOCTYPE_HTML
97              + "<html>\n"
98              + "<head>\n"
99              + "  <script>\n"
100             + LOG_TITLE_FUNCTION
101             + "    function test() {\n"
102             + "      var elt = document.getElementById('h1');\n"
103             + "      log(elt.clientHeight);\n"
104             + "      var elt = document.getElementById('h2');\n"
105             + "      log(elt.clientHeight);\n"
106             + "      var elt = document.getElementById('h3');\n"
107             + "      log(elt.clientHeight);\n"
108             + "      var elt = document.getElementById('h4');\n"
109             + "      log(elt.clientHeight);\n"
110             + "      var elt = document.getElementById('h5');\n"
111             + "      log(elt.clientHeight);\n"
112             + "      var elt = document.getElementById('h6');\n"
113             + "      log(elt.clientHeight);\n"
114             + "    }\n"
115             + "  </script>\n"
116             + "</head>\n"
117             + "<body onload='test()'>\n"
118             + "  <h1 id='h1'>HtmlUnit</h1>\n"
119             + "  <h2 id='h2'>HtmlUnit</h2>\n"
120             + "  <h3 id='h3'>HtmlUnit</h3>\n"
121             + "  <h4 id='h4'>HtmlUnit</h4>\n"
122             + "  <h5 id='h5'>HtmlUnit</h5>\n"
123             + "  <h6 id='h6'>HtmlUnit</h6>\n"
124             + "</body></html>";
125 
126         loadPageVerifyTitle2(html);
127     }
128 
129     /**
130      * @throws Exception if an error occurs
131      */
132     @Test
133     @Alerts({"0", "0", "0", "0", "0", "0"})
134     public void clientHeightEmpty() throws Exception {
135         final String html = DOCTYPE_HTML
136             + "<html>\n"
137             + "<head>\n"
138             + "  <script>\n"
139             + LOG_TITLE_FUNCTION
140             + "    function test() {\n"
141             + "      var elt = document.getElementById('h1');\n"
142             + "      log(elt.clientHeight);\n"
143             + "      var elt = document.getElementById('h2');\n"
144             + "      log(elt.clientHeight);\n"
145             + "      var elt = document.getElementById('h3');\n"
146             + "      log(elt.clientHeight);\n"
147             + "      var elt = document.getElementById('h4');\n"
148             + "      log(elt.clientHeight);\n"
149             + "      var elt = document.getElementById('h5');\n"
150             + "      log(elt.clientHeight);\n"
151             + "      var elt = document.getElementById('h6');\n"
152             + "      log(elt.clientHeight);\n"
153             + "    }\n"
154             + "  </script>\n"
155             + "</head>\n"
156             + "<body onload='test()'>\n"
157             + "  <h1 id='h1'></h1>\n"
158             + "  <h2 id='h2'></h2>\n"
159             + "  <h3 id='h3'></h3>\n"
160             + "  <h4 id='h4'></h4>\n"
161             + "  <h5 id='h5'></h5>\n"
162             + "  <h6 id='h6'></h6>\n"
163             + "</body></html>";
164 
165         loadPageVerifyTitle2(html);
166     }
167 }