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