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.WebElement;
23  
24  /**
25   * Tests for {@link HtmlResetInput}.
26   *
27   * @author Ronald Brill
28   * @author Frank Danek
29   */
30  public class HtmlResetInput2Test extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts({"-", "-", "-"})
37      public void defaultValues() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html><head>\n"
40              + "<script>\n"
41              + LOG_TITLE_FUNCTION
42              + "  function test() {\n"
43              + "    var input = document.getElementById('reset1');\n"
44              + "    log(input.value + '-' + input.defaultValue);\n"
45  
46              + "    input = document.createElement('input');\n"
47              + "    input.type = 'reset';\n"
48              + "    log(input.value + '-' + input.defaultValue);\n"
49  
50              + "    var builder = document.createElement('div');\n"
51              + "    builder.innerHTML = '<input type=\"reset\">';\n"
52              + "    input = builder.firstChild;\n"
53              + "    log(input.value + '-' + input.defaultValue);\n"
54              + "  }\n"
55              + "</script>\n"
56              + "</head><body onload='test()'>\n"
57              + "<form>\n"
58              + "  <input type='reset' id='reset1'>\n"
59              + "</form>\n"
60              + "</body></html>";
61  
62          loadPageVerifyTitle2(html);
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      @Alerts({"-", "-", "-"})
70      public void defaultValuesAfterClone() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><head>\n"
73              + "<script>\n"
74              + LOG_TITLE_FUNCTION
75              + "  function test() {\n"
76              + "    var input = document.getElementById('reset1');\n"
77              + "    input = input.cloneNode(false);\n"
78              + "    log(input.value + '-' + input.defaultValue);\n"
79  
80              + "    input = document.createElement('input');\n"
81              + "    input.type = 'reset';\n"
82              + "    input = input.cloneNode(false);\n"
83              + "    log(input.value + '-' + input.defaultValue);\n"
84  
85              + "    var builder = document.createElement('div');\n"
86              + "    builder.innerHTML = '<input type=\"reset\">';\n"
87              + "    input = builder.firstChild;\n"
88              + "    input = input.cloneNode(false);\n"
89              + "    log(input.value + '-' + input.defaultValue);\n"
90              + "  }\n"
91              + "</script>\n"
92              + "</head><body onload='test()'>\n"
93              + "<form>\n"
94              + "  <input type='reset' id='reset1'>\n"
95              + "</form>\n"
96              + "</body></html>";
97  
98          loadPageVerifyTitle2(html);
99      }
100 
101     /**
102      * @throws Exception if the test fails
103      */
104     @Test
105     @Alerts({"initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
106                 "newDefault-newDefault", "newDefault-newDefault"})
107     public void resetByClick() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html><head>\n"
110             + "<script>\n"
111             + LOG_TITLE_FUNCTION
112             + "  function test() {\n"
113             + "    var reset = document.getElementById('testId');\n"
114             + "    log(reset.value + '-' + reset.defaultValue);\n"
115 
116             + "    document.getElementById('testReset').click;\n"
117             + "    log(reset.value + '-' + reset.defaultValue);\n"
118 
119             + "    reset.value = 'newValue';\n"
120             + "    log(reset.value + '-' + reset.defaultValue);\n"
121 
122             + "    document.getElementById('testReset').click;\n"
123             + "    log(reset.value + '-' + reset.defaultValue);\n"
124 
125             + "    reset.defaultValue = 'newDefault';\n"
126             + "    log(reset.value + '-' + reset.defaultValue);\n"
127 
128             + "    document.forms[0].reset;\n"
129             + "    log(reset.value + '-' + reset.defaultValue);\n"
130             + "  }\n"
131             + "</script>\n"
132             + "</head><body onload='test()'>\n"
133             + "<form>\n"
134             + "  <input type='reset' id='testId' value='initial'>\n"
135             + "  <input type='reset' id='testReset'>\n"
136             + "</form>\n"
137             + "</body></html>";
138 
139         loadPageVerifyTitle2(html);
140     }
141 
142     /**
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts({"initial-initial", "initial-initial", "newValue-newValue", "newValue-newValue",
147                 "newDefault-newDefault", "newDefault-newDefault"})
148     public void resetByJS() throws Exception {
149         final String html = DOCTYPE_HTML
150             + "<html><head>\n"
151             + "<script>\n"
152             + LOG_TITLE_FUNCTION
153             + "  function test() {\n"
154             + "    var reset = document.getElementById('testId');\n"
155             + "    log(reset.value + '-' + reset.defaultValue);\n"
156 
157             + "    document.forms[0].reset;\n"
158             + "    log(reset.value + '-' + reset.defaultValue);\n"
159 
160             + "    reset.value = 'newValue';\n"
161             + "    log(reset.value + '-' + reset.defaultValue);\n"
162 
163             + "    document.forms[0].reset;\n"
164             + "    log(reset.value + '-' + reset.defaultValue);\n"
165 
166             + "    reset.defaultValue = 'newDefault';\n"
167             + "    log(reset.value + '-' + reset.defaultValue);\n"
168 
169             + "    document.forms[0].reset;\n"
170             + "    log(reset.value + '-' + reset.defaultValue);\n"
171             + "  }\n"
172             + "</script>\n"
173             + "</head><body onload='test()'>\n"
174             + "<form>\n"
175             + "  <input type='reset' id='testId' value='initial'>\n"
176             + "</form>\n"
177             + "</body></html>";
178 
179         loadPageVerifyTitle2(html);
180     }
181 
182     /**
183      * @throws Exception if the test fails
184      */
185     @Test
186     @Alerts({"initial-initial", "default-default", "newValue-newValue", "newdefault-newdefault"})
187     public void defaultValue() throws Exception {
188         final String html = DOCTYPE_HTML
189             + "<html><head>\n"
190             + "<script>\n"
191             + LOG_TITLE_FUNCTION
192             + "  function test() {\n"
193             + "    var reset = document.getElementById('testId');\n"
194             + "    log(reset.value + '-' + reset.defaultValue);\n"
195 
196             + "    reset.defaultValue = 'default';\n"
197             + "    log(reset.value + '-' + reset.defaultValue);\n"
198 
199             + "    reset.value = 'newValue';\n"
200             + "    log(reset.value + '-' + reset.defaultValue);\n"
201             + "    reset.defaultValue = 'newdefault';\n"
202             + "    log(reset.value + '-' + reset.defaultValue);\n"
203             + "  }\n"
204             + "</script>\n"
205             + "</head><body onload='test()'>\n"
206             + "<form>\n"
207             + "  <input type='reset' id='testId' value='initial'>\n"
208             + "</form>\n"
209             + "</body></html>";
210 
211         loadPageVerifyTitle2(html);
212     }
213 
214     /**
215      * @throws Exception if the test fails
216      */
217     @Test
218     @Alerts("--")
219     public void minMaxStep() throws Exception {
220         final String html = DOCTYPE_HTML
221             + "<html>\n"
222             + "<head>\n"
223             + "<script>\n"
224             + LOG_TITLE_FUNCTION
225             + "  function test() {\n"
226             + "    var input = document.getElementById('tester');\n"
227             + "    log(input.min + '-' + input.max + '-' + input.step);\n"
228             + "  }\n"
229             + "</script>\n"
230             + "</head>\n"
231             + "<body onload='test()'>\n"
232             + "<form>\n"
233             + "  <input type='reset' id='tester'>\n"
234             + "</form>\n"
235             + "</body>\n"
236             + "</html>";
237 
238         loadPageVerifyTitle2(html);
239     }
240 
241     /**
242      * @throws Exception if the test fails
243      */
244     @Test
245     @Alerts({"foo", "foonewValue", "foo"})
246     public void reset() throws Exception {
247         final String html = DOCTYPE_HTML
248             + "<html><head>\n"
249             + "  <script type='text/javascript'>\n"
250             + "    function submitForm() {\n"
251             + "    }\n"
252             + "  </script>\n"
253             + "</head>\n"
254             + "<body>\n"
255             + "  <form action='test' name='deliveryChannelForm'>\n"
256             + "    <input type='text' id='textfield' value='foo'/>\n"
257             + "    <input name='resetBtn' type='reset' value='Save' title='Save' onclick='submitForm();'>\n"
258             + "  </form>"
259             + "</script>\n"
260             + "</body></html>";
261 
262         final WebDriver webDriver = loadPage2(html);
263 
264         final WebElement textfield = webDriver.findElement(By.id("textfield"));
265 
266         assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
267         assertEquals(getExpectedAlerts()[0], textfield.getDomProperty("value"));
268 
269         textfield.sendKeys("newValue");
270         assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
271         assertEquals(getExpectedAlerts()[1], textfield.getDomProperty("value"));
272 
273         final WebElement reset = webDriver.findElement(By.name("resetBtn"));
274         reset.click();
275 
276         assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
277         assertEquals(getExpectedAlerts()[2], textfield.getDomProperty("value"));
278     }
279 
280     /**
281      * @throws Exception if the test fails
282      */
283     @Test
284     @Alerts({"foo", "foonewValue", "foonewValue"})
285     public void onclickDisables() throws Exception {
286         final String html = DOCTYPE_HTML
287             + "<html><head>\n"
288             + "  <script type='text/javascript'>\n"
289             + "    function submitForm() {\n"
290             + "      document.deliveryChannelForm.resetBtn.disabled = true;\n"
291             + "    }\n"
292             + "  </script>\n"
293             + "</head>\n"
294             + "<body>\n"
295             + "  <form action='test' name='deliveryChannelForm'>\n"
296             + "    <input type='text' id='textfield' value='foo'/>\n"
297             + "    <input name='resetBtn' type='reset' value='Save' title='Save' onclick='submitForm();'>\n"
298             + "  </form>"
299             + "</script>\n"
300             + "</body></html>";
301 
302         final WebDriver webDriver = loadPage2(html);
303 
304         final WebElement textfield = webDriver.findElement(By.id("textfield"));
305         assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
306         assertEquals(getExpectedAlerts()[0], textfield.getDomProperty("value"));
307 
308         textfield.sendKeys("newValue");
309         assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
310         assertEquals(getExpectedAlerts()[1], textfield.getDomProperty("value"));
311 
312         final WebElement reset = webDriver.findElement(By.name("resetBtn"));
313         reset.click();
314         assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
315         assertEquals(getExpectedAlerts()[2], textfield.getDomProperty("value"));
316     }
317 
318     /**
319      * @throws Exception if an error occurs
320      */
321     @Test
322     @Alerts({"false", "false", "false", "false", "false"})
323     public void willValidate() throws Exception {
324         final String html = DOCTYPE_HTML
325                 + "<html><head>\n"
326                 + "  <script>\n"
327                 + LOG_TITLE_FUNCTION
328                 + "    function test() {\n"
329                 + "      log(document.getElementById('o1').willValidate);\n"
330                 + "      log(document.getElementById('o2').willValidate);\n"
331                 + "      log(document.getElementById('o3').willValidate);\n"
332                 + "      log(document.getElementById('o4').willValidate);\n"
333                 + "      log(document.getElementById('o5').willValidate);\n"
334                 + "    }\n"
335                 + "  </script>\n"
336                 + "</head>\n"
337                 + "<body onload='test()'>\n"
338                 + "  <form>\n"
339                 + "    <input type='reset' id='o1'>\n"
340                 + "    <input type='reset' id='o2' disabled>\n"
341                 + "    <input type='reset' id='o3' hidden>\n"
342                 + "    <input type='reset' id='o4' readonly>\n"
343                 + "    <input type='reset' id='o5' style='display: none'>\n"
344                 + "  </form>\n"
345                 + "</body></html>";
346 
347         loadPageVerifyTitle2(html);
348     }
349 
350     /**
351      * @throws Exception if an error occurs
352      */
353     @Test
354     @Alerts({"true",
355              "false-false-false-false-false-false-false-false-false-true-false",
356              "false"})
357     public void validationEmpty() throws Exception {
358         validation("<input type='reset' id='e1'>\n", "");
359     }
360 
361     /**
362      * @throws Exception if an error occurs
363      */
364     @Test
365     @Alerts({"true",
366              "false-true-false-false-false-false-false-false-false-false-false",
367              "false"})
368     public void validationCustomValidity() throws Exception {
369         validation("<input type='reset' id='e1'>\n", "elem.setCustomValidity('Invalid');");
370     }
371 
372     /**
373      * @throws Exception if an error occurs
374      */
375     @Test
376     @Alerts({"true",
377              "false-true-false-false-false-false-false-false-false-false-false",
378              "false"})
379     public void validationBlankCustomValidity() throws Exception {
380         validation("<input type='reset' id='e1'>\n", "elem.setCustomValidity(' ');\n");
381     }
382 
383     /**
384      * @throws Exception if an error occurs
385      */
386     @Test
387     @Alerts({"true",
388              "false-false-false-false-false-false-false-false-false-true-false",
389              "false"})
390     public void validationResetCustomValidity() throws Exception {
391         validation("<input type='reset' id='e1'>\n",
392                 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
393     }
394 
395     /**
396      * @throws Exception if an error occurs
397      */
398     @Test
399     @Alerts({"true",
400              "false-false-false-false-false-false-false-false-false-true-false",
401              "false"})
402     public void validationRequired() throws Exception {
403         validation("<input type='reset' id='e1' required>\n", "");
404     }
405 
406     private void validation(final String htmlPart, final String jsPart) throws Exception {
407         final String html = DOCTYPE_HTML
408                 + "<html><head>\n"
409                 + "  <script>\n"
410                 + LOG_TITLE_FUNCTION
411                 + "    function logValidityState(s) {\n"
412                 + "      log(s.badInput"
413                         + "+ '-' + s.customError"
414                         + "+ '-' + s.patternMismatch"
415                         + "+ '-' + s.rangeOverflow"
416                         + "+ '-' + s.rangeUnderflow"
417                         + "+ '-' + s.stepMismatch"
418                         + "+ '-' + s.tooLong"
419                         + "+ '-' + s.tooShort"
420                         + " + '-' + s.typeMismatch"
421                         + " + '-' + s.valid"
422                         + " + '-' + s.valueMissing);\n"
423                 + "    }\n"
424                 + "    function test() {\n"
425                 + "      var elem = document.getElementById('e1');\n"
426                 + jsPart
427                 + "      log(elem.checkValidity());\n"
428                 + "      logValidityState(elem.validity);\n"
429                 + "      log(elem.willValidate);\n"
430                 + "    }\n"
431                 + "  </script>\n"
432                 + "</head>\n"
433                 + "<body onload='test()'>\n"
434                 + "  <form>\n"
435                 + htmlPart
436                 + "  </form>\n"
437                 + "</body></html>";
438 
439         loadPageVerifyTitle2(html);
440     }
441 }