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