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