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.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  import org.openqa.selenium.By;
23  import org.openqa.selenium.WebDriver;
24  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
25  
26  /**
27   * Tests for {@link HtmlFieldSet}.
28   *
29   * @author Ahmed Ashour
30   * @author Daniel Gredler
31   * @author Frank Danek
32   * @author Ronald Brill
33   */
34  @RunWith(BrowserRunner.class)
35  public class HtmlFieldSetTest extends WebDriverTestCase {
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts({"[object HTMLFieldSetElement]", "[object HTMLFormElement]"})
42      public void simpleScriptable() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html><head>\n"
45              + "<script>\n"
46              + LOG_TITLE_FUNCTION
47              + "  function test() {\n"
48              + "    var fs = document.getElementById('fs');\n"
49              + "    log(fs);\n"
50              + "    log(fs.form);\n"
51              + "  }\n"
52              + "</script>\n"
53              + "</head><body onload='test()'>\n"
54              + "  <form>\n"
55              + "    <fieldset id='fs'>\n"
56              + "      <legend>Legend</legend>\n"
57              + "    </fieldset>\n"
58              + "  </form>\n"
59              + "</body></html>";
60          final WebDriver driver = loadPageVerifyTitle2(html);
61          if (driver instanceof HtmlUnitDriver) {
62              final HtmlElement element = toHtmlElement(driver.findElement(By.id("fs")));
63              assertTrue(element instanceof HtmlFieldSet);
64          }
65      }
66  
67      /**
68       * @throws Exception if an error occurs
69       */
70      @Test
71      @Alerts({"undefined", "undefined", "undefined", "center", "8", "foo"})
72      public void align() throws Exception {
73          final String html = DOCTYPE_HTML
74              + "<html><body>\n"
75              + "<form>\n"
76              + "  <fieldset id='fs1' align='left'>\n"
77              + "    <legend>Legend</legend>\n"
78              + "  </fieldset>\n"
79              + "  <fieldset id='fs2' align='right'>\n"
80              + "    <legend>Legend</legend>\n"
81              + "  </fieldset>\n"
82              + "  <fieldset id='fs3' align='3'>\n"
83              + "    <legend>Legend</legend>\n"
84              + "  </fieldset>\n"
85              + "</form>\n"
86              + "<script>\n"
87              + LOG_TITLE_FUNCTION
88              + "  function set(fs, value) {\n"
89              + "    try {\n"
90              + "      fs.align = value;\n"
91              + "    } catch(e) { logEx(e) }\n"
92              + "  }\n"
93              + "  var fs1 = document.getElementById('fs1');\n"
94              + "  var fs2 = document.getElementById('fs2');\n"
95              + "  var fs3 = document.getElementById('fs3');\n"
96              + "  log(fs1.align);\n"
97              + "  log(fs2.align);\n"
98              + "  log(fs3.align);\n"
99              + "  set(fs1, 'center');\n"
100             + "  set(fs2, '8');\n"
101             + "  set(fs3, 'foo');\n"
102             + "  log(fs1.align);\n"
103             + "  log(fs2.align);\n"
104             + "  log(fs3.align);\n"
105             + "</script>\n"
106             + "</body></html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception if an error occurs
113      */
114     @Test
115     @Alerts({"true", "false"})
116     public void disabled() throws Exception {
117         final String html = DOCTYPE_HTML
118             + "<html><body>\n"
119             + "<form>\n"
120             + "  <fieldset id='fs1' disabled>\n"
121             + "    <input type'text' id='txt1' />\n"
122             + "  </fieldset>\n"
123             + "</form>\n"
124             + "<script>\n"
125             + LOG_TITLE_FUNCTION
126             + "  var fs1 = document.getElementById('fs1');\n"
127             + "  var txt1 = document.getElementById('txt1');\n"
128             + "  log(fs1.disabled);\n"
129             + "  log(txt1.disabled);\n"
130             + "</script>\n"
131             + "</body></html>";
132 
133         loadPageVerifyTitle2(html);
134     }
135 
136     /**
137      * @throws Exception if an error occurs
138      */
139     @Test
140     @Alerts({"false", "false", "false", "false", "false"})
141     public void willValidate() throws Exception {
142         final String html = DOCTYPE_HTML
143                 + "<html><head>\n"
144                 + "  <script>\n"
145                 + LOG_TITLE_FUNCTION
146                 + "    function test() {\n"
147                 + "      log(document.getElementById('f1').willValidate);\n"
148                 + "      log(document.getElementById('f2').willValidate);\n"
149                 + "      log(document.getElementById('f3').willValidate);\n"
150                 + "      log(document.getElementById('f4').willValidate);\n"
151                 + "      log(document.getElementById('f5').willValidate);\n"
152                 + "    }\n"
153                 + "  </script>\n"
154                 + "</head>\n"
155                 + "<body onload='test()'>\n"
156                 + "  <form>\n"
157                 + "    <fieldset id='f1' />\n"
158                 + "    <fieldset id='f2' disabled />\n"
159                 + "    <fieldset id='f3' />\n"
160                 + "    <fieldset id='f4' readonly />\n"
161                 + "    <fieldset id='f5' style='display: none' />\n"
162                 + "  </form>\n"
163                 + "</body></html>";
164 
165         loadPageVerifyTitle2(html);
166     }
167 
168     /**
169      * @throws Exception if an error occurs
170      */
171     @Test
172     @Alerts({"true",
173              "false-false-false-false-false-false-false-false-false-true-false",
174              "false"})
175     public void validationEmpty() throws Exception {
176         validation("<fieldset id='e1' />\n", "");
177     }
178 
179     /**
180      * @throws Exception if an error occurs
181      */
182     @Test
183     @Alerts({"true",
184              "false-true-false-false-false-false-false-false-false-false-false",
185              "false"})
186     public void validationCustomValidity() throws Exception {
187         validation("<fieldset id='e1' />\n", "elem.setCustomValidity('Invalid');");
188     }
189 
190     /**
191      * @throws Exception if an error occurs
192      */
193     @Test
194     @Alerts({"true",
195              "false-true-false-false-false-false-false-false-false-false-false",
196              "false"})
197     public void validationBlankCustomValidity() throws Exception {
198         validation("<fieldset id='e1' />\n", "elem.setCustomValidity(' ');\n");
199     }
200 
201     /**
202      * @throws Exception if an error occurs
203      */
204     @Test
205     @Alerts({"true",
206              "false-false-false-false-false-false-false-false-false-true-false",
207              "false"})
208     public void validationResetCustomValidity() throws Exception {
209         validation("<fieldset id='e1' />\n",
210                 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
211     }
212 
213     /**
214      * @throws Exception if an error occurs
215      */
216     @Test
217     @Alerts({"true",
218              "false-false-false-false-false-false-false-false-false-true-false",
219              "false"})
220     public void validationRequired() throws Exception {
221         validation("<fieldset id='e1' required/>\n", "");
222     }
223 
224     /**
225      * @throws Exception if an error occurs
226      */
227     @Test
228     @Alerts({"true",
229              "false-false-false-false-false-false-false-false-false-true-false",
230              "false"})
231     public void validationDisabled() throws Exception {
232         validation("<fieldset id='e1' disabled/>\n", "");
233     }
234 
235     private void validation(final String htmlPart, final String jsPart) throws Exception {
236         final String html = DOCTYPE_HTML
237                 + "<html><head>\n"
238                 + "  <script>\n"
239                 + LOG_TITLE_FUNCTION
240                 + "    function logValidityState(s) {\n"
241                 + "      log(s.badInput"
242                         + "+ '-' + s.customError"
243                         + "+ '-' + s.patternMismatch"
244                         + "+ '-' + s.rangeOverflow"
245                         + "+ '-' + s.rangeUnderflow"
246                         + "+ '-' + s.stepMismatch"
247                         + "+ '-' + s.tooLong"
248                         + "+ '-' + s.tooShort"
249                         + " + '-' + s.typeMismatch"
250                         + " + '-' + s.valid"
251                         + " + '-' + s.valueMissing);\n"
252                 + "    }\n"
253                 + "    function test() {\n"
254                 + "      var elem = document.getElementById('e1');\n"
255                 + jsPart
256                 + "      log(elem.checkValidity());\n"
257                 + "      logValidityState(elem.validity);\n"
258                 + "      log(elem.willValidate);\n"
259                 + "    }\n"
260                 + "  </script>\n"
261                 + "</head>\n"
262                 + "<body onload='test()'>\n"
263                 + "  <form>\n"
264                 + htmlPart
265                 + "  </form>\n"
266                 + "</body></html>";
267 
268         loadPageVerifyTitle2(html);
269     }
270 }