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  
21  /**
22   * Tests for changing the type attribute for {@link HtmlInput}.
23   *
24   * @author Ronald Brill
25   */
26  public final class HtmlInput3Test extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
33      public void none_button() throws Exception {
34          changeType("value='abcd'", "1234", "button");
35      }
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts({"1234--null", "1234-1234-1234"})
42      public void noneNoValueAttr_button() throws Exception {
43          changeType("", "1234", "button");
44      }
45  
46      /**
47       * @throws Exception if the test fails
48       */
49      @Test
50      @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
51      public void none_checkbox() throws Exception {
52          changeType("value='abcd'", "1234", "checkbox");
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts({"1234--null", "1234-1234-1234"})
60      public void noneNoValueAttr_checkbox() throws Exception {
61          changeType("", "1234", "checkbox");
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      @Alerts({"#aaaaaa-#ffffff-#ffffff", "#aaaaaa-#ffffff-#ffffff"})
69      public void none_color() throws Exception {
70          changeType("value='#ffffff'", "#aaaaaa", "color");
71      }
72  
73      /**
74       * @throws Exception if the test fails
75       */
76      @Test
77      @Alerts({"#aaaaaa--null", "#aaaaaa--null"})
78      public void noneNoValueAttr_color() throws Exception {
79          changeType("", "#aaaaaa", "color");
80      }
81  
82      /**
83       * @throws Exception if the test fails
84       */
85      @Test
86      @Alerts({"2019-07-11-2018-06-12-2018-06-12", "2019-07-11-2018-06-12-2018-06-12"})
87      public void none_date() throws Exception {
88          changeType("value='2018-06-12'", "2019-07-11", "date");
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts({"2019-07-11--null", "2019-07-11--null"})
96      public void noneNoValueAttr_date() throws Exception {
97          changeType("", "2019-07-11", "date");
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts({"2019-07-11T4:16-2018-06-12T19:30-2018-06-12T19:30",
105              "-2018-06-12T19:30-2018-06-12T19:30"})
106     public void none_datetimelocal() throws Exception {
107         changeType("value='2018-06-12T19:30'", "2019-07-11T4:16", "datetime-local");
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts({"2019-07-11T4:16--null", "--null"})
115     public void noneNoValueAttr_datetimelocal() throws Exception {
116         changeType("", "2019-07-11T4:16", "datetime-local");
117     }
118 
119     /**
120      * @throws Exception if the test fails
121      */
122     @Test
123     @Alerts({"1234-htmlunit.txt-htmlunit.txt", "-htmlunit.txt-htmlunit.txt"})
124     public void none_file() throws Exception {
125         changeType("value='htmlunit.txt'", "1234", "file");
126     }
127 
128     /**
129      * @throws Exception if the test fails
130      */
131     @Test
132     @Alerts({"1234--null", "--null"})
133     public void noneNoValueAttr_file() throws Exception {
134         changeType("", "1234", "file");
135     }
136 
137     /**
138      * @throws Exception if the test fails
139      */
140     @Test
141     @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
142     public void none_hidden() throws Exception {
143         changeType("value='abcd'", "1234", "hidden");
144     }
145 
146     /**
147      * @throws Exception if the test fails
148      */
149     @Test
150     @Alerts({"1234--null", "1234-1234-1234"})
151     public void noneNoValueAttr_hidden() throws Exception {
152         changeType("", "1234", "hidden");
153     }
154 
155     /**
156      * @throws Exception if the test fails
157      */
158     @Test
159     @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
160     public void none_image() throws Exception {
161         changeType("src='test.png' value='abcd'", "1234", "image");
162     }
163 
164     /**
165      * @throws Exception if the test fails
166      */
167     @Test
168     @Alerts({"1234--null", "1234-1234-1234"})
169     public void noneNoValueAttr_image() throws Exception {
170         changeType("src='test.png'", "1234", "image");
171     }
172 
173     /**
174      * @throws Exception if the test fails
175      */
176     @Test
177     @Alerts(DEFAULT = {"12-7-7", "12-7-7"},
178             CHROME = {"12-7-7", "-7-7"},
179             EDGE = {"12-7-7", "-7-7"})
180     public void none_month() throws Exception {
181         changeType("value='7'", "12", "month");
182     }
183 
184     /**
185      * @throws Exception if the test fails
186      */
187     @Test
188     @Alerts(DEFAULT = {"12--null", "12--null"},
189             CHROME = {"12--null", "--null"},
190             EDGE = {"12--null", "--null"})
191     public void noneNoValueAttr_month() throws Exception {
192         changeType("", "12", "month");
193     }
194 
195     /**
196      * @throws Exception if the test fails
197      */
198     @Test
199     @Alerts({"1234-3.14-3.14", "1234-3.14-3.14"})
200     public void none_number() throws Exception {
201         changeType("value='3.14'", "1234", "number");
202     }
203 
204     /**
205      * @throws Exception if the test fails
206      */
207     @Test
208     @Alerts({"1234--null", "1234--null"})
209     public void noneNoValueAttr_number() throws Exception {
210         changeType("", "1234", "number");
211     }
212 
213     /**
214      * @throws Exception if the test fails
215      */
216     @Test
217     @Alerts({"1234-abcd-abcd", "1234-abcd-abcd"})
218     public void none_password() throws Exception {
219         changeType("value='abcd'", "1234", "password");
220     }
221 
222     /**
223      * @throws Exception if the test fails
224      */
225     @Test
226     @Alerts({"1234--null", "1234--null"})
227     public void noneNoValueAttr_password() throws Exception {
228         changeType("", "1234", "password");
229     }
230 
231     /**
232      * @throws Exception if the test fails
233      */
234     @Test
235     @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
236     public void none_radio() throws Exception {
237         changeType("value='abcd'", "1234", "radio");
238     }
239 
240     /**
241      * @throws Exception if the test fails
242      */
243     @Test
244     @Alerts({"1234--null", "1234-1234-1234"})
245     public void noneNoValueAttr_radio() throws Exception {
246         changeType("", "1234", "radio");
247     }
248 
249     /**
250      * @throws Exception if the test fails
251      */
252     @Test
253     @Alerts({"7-4-4", "7-4-4"})
254     public void none_range() throws Exception {
255         changeType("min='0' max='11' value='4'", "7", "range");
256     }
257 
258     /**
259      * @throws Exception if the test fails
260      */
261     @Test
262     @Alerts({"7--null", "7--null"})
263     public void noneNoValueAttr_range() throws Exception {
264         changeType("min='0' max='11'", "7", "range");
265     }
266 
267     /**
268      * @throws Exception if the test fails
269      */
270     @Test
271     @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
272     public void none_reset() throws Exception {
273         changeType("value='abcd'", "1234", "reset");
274     }
275 
276     /**
277      * @throws Exception if the test fails
278      */
279     @Test
280     @Alerts({"1234--null", "1234-1234-1234"})
281     public void noneNoValueAttr_reset() throws Exception {
282         changeType("", "1234", "reset");
283     }
284 
285     /**
286      * @throws Exception if the test fails
287      */
288     @Test
289     @Alerts({"1234-abcd-abcd", "1234-abcd-abcd"})
290     public void none_search() throws Exception {
291         changeType("value='abcd'", "1234", "search");
292     }
293 
294     /**
295      * @throws Exception if the test fails
296      */
297     @Test
298     @Alerts({"1234--null", "1234--null"})
299     public void noneNoValueAttr_search() throws Exception {
300         changeType("", "1234", "search");
301     }
302 
303     /**
304      * @throws Exception if the test fails
305      */
306     @Test
307     @Alerts({"1234-abcd-abcd", "1234-1234-1234"})
308     public void none_submit() throws Exception {
309         changeType("value='abcd'", "1234", "submit");
310     }
311 
312     /**
313      * @throws Exception if the test fails
314      */
315     @Test
316     @Alerts({"1234--null", "1234-1234-1234"})
317     public void noneNoValueAttr_submit() throws Exception {
318         changeType("", "1234", "submit");
319     }
320 
321     /**
322      * @throws Exception if the test fails
323      */
324     @Test
325     @Alerts({"1234-0177 6012345-0177 6012345", "1234-0177 6012345-0177 6012345"})
326     public void none_tel() throws Exception {
327         changeType("value='0177 6012345'", "1234", "tel");
328     }
329 
330     /**
331      * @throws Exception if the test fails
332      */
333     @Test
334     @Alerts({"1234--null", "1234--null"})
335     public void noneNoValueAttr_tel() throws Exception {
336         changeType("", "1234", "tel");
337     }
338 
339     /**
340      * @throws Exception if the test fails
341      */
342     @Test
343     @Alerts({"1234-abcd-abcd", "1234-abcd-abcd"})
344     public void none_text() throws Exception {
345         changeType("value='abcd'", "1234", "text");
346     }
347 
348     /**
349      * @throws Exception if the test fails
350      */
351     @Test
352     @Alerts({"1234--null", "1234--null"})
353     public void noneNoValueAttr_text() throws Exception {
354         changeType("", "1234", "text");
355     }
356 
357     /**
358      * @throws Exception if the test fails
359      */
360     @Test
361     @Alerts({"4:16-19:30-19:30", "-19:30-19:30"})
362     public void none_time() throws Exception {
363         changeType("value='19:30'", "4:16", "time");
364     }
365 
366     /**
367      * @throws Exception if the test fails
368      */
369     @Test
370     @Alerts({"4:16--null", "--null"})
371     public void noneNoValueAttr_time() throws Exception {
372         changeType("", "4:16", "time");
373     }
374 
375     /**
376      * @throws Exception if the test fails
377      */
378     @Test
379     @Alerts({"https://www.wetator.org-https://www.htmlunit.org-https://www.htmlunit.org",
380              "https://www.wetator.org-https://www.htmlunit.org-https://www.htmlunit.org"})
381     public void none_url() throws Exception {
382         changeType("value='https://www.htmlunit.org'", "https://www.wetator.org", "url");
383     }
384 
385     /**
386      * @throws Exception if the test fails
387      */
388     @Test
389     @Alerts({"https://www.wetator.org--null",
390              "https://www.wetator.org--null"})
391     public void noneNoValueAttr_url() throws Exception {
392         changeType("", "https://www.wetator.org", "url");
393     }
394 
395     /**
396      * @throws Exception if the test fails
397      */
398     @Test
399     @Alerts(DEFAULT = {"24-42-42", "24-42-42"},
400             CHROME = {"24-42-42", "-42-42"},
401             EDGE = {"24-42-42", "-42-42"})
402     public void none_week() throws Exception {
403         changeType("value='42'", "24", "week");
404     }
405 
406     /**
407      * @throws Exception if the test fails
408      */
409     @Test
410     @Alerts(DEFAULT = {"24--null", "24--null"},
411             CHROME = {"24--null", "--null"},
412             EDGE = {"24--null", "--null"})
413     public void noneNoValueAttr_week() throws Exception {
414         changeType("", "24", "week");
415     }
416 
417     /**
418      * @throws Exception if the test fails
419      */
420     @Test
421     @Alerts({"2020-04-7T1:13-2018-06-12T19:30-2018-06-12T19:30",
422              "2020-04-7T1:13-2018-06-12T19:30-2018-06-12T19:30"})
423     public void none_datetime() throws Exception {
424         changeType("value='2018-06-12T19:30'", "2020-04-7T1:13", "datetime");
425     }
426 
427     /**
428      * @throws Exception if the test fails
429      */
430     @Test
431     @Alerts({"2020-04-7T1:13--null", "2020-04-7T1:13--null"})
432     public void noneNoValueAttr_datetime() throws Exception {
433         changeType("", "2020-04-7T1:13", "datetime");
434     }
435 
436     private void changeType(final String inputAttribs, final String value, final String targetType) throws Exception {
437         final String html = DOCTYPE_HTML
438                 + "<html><head>\n"
439                 + "<script>\n"
440                 + LOG_TITLE_FUNCTION
441                 + "  function test() {\n"
442                 + "    var input = document.getElementById('tester');\n"
443                 + "    input.value = '" + value + "';\n"
444                 + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
445 
446                 + "    try {\n"
447                 + "      input.type = '" + targetType + "';\n"
448                 + "      log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
449                 + "    } catch(e) { logEx(e); }\n"
450                 + "  }\n"
451                 + "</script>\n"
452                 + "</head><body onload='test()'>\n"
453                 + "<form>\n"
454                 + "  <input id='tester' " + inputAttribs + ">\n"
455                 + "</form>\n"
456                 + "</body></html>";
457 
458         loadPageVerifyTitle2(html);
459     }
460 
461     /**
462      * @throws Exception if the test fails
463      */
464     @Test
465     @Alerts({"1234--null", "1234-1234-1234"})
466     public void detached_button() throws Exception {
467         changeTypeDetached("1234", "button");
468     }
469 
470     /**
471      * @throws Exception if the test fails
472      */
473     @Test
474     @Alerts({"1234--null", "1234-1234-1234"})
475     public void detached_checkbox() throws Exception {
476         changeTypeDetached("1234", "checkbox");
477     }
478 
479     /**
480      * @throws Exception if the test fails
481      */
482     @Test
483     @Alerts({"#aaaaaa--null", "#aaaaaa--null"})
484     public void detached_color() throws Exception {
485         changeTypeDetached("#aaaaaa", "color");
486     }
487 
488     /**
489      * @throws Exception if the test fails
490      */
491     @Test
492     @Alerts({"2019-07-11--null", "2019-07-11--null"})
493     public void detached_date() throws Exception {
494         changeTypeDetached("2019-07-11", "date");
495     }
496 
497     /**
498      * @throws Exception if the test fails
499      */
500     @Test
501     @Alerts({"2019-07-11T4:16--null", "--null"})
502     public void detached_datetimelocal() throws Exception {
503         changeTypeDetached("2019-07-11T4:16", "datetime-local");
504     }
505 
506     /**
507      * @throws Exception if the test fails
508      */
509     @Test
510     @Alerts({"1234--null", "--null"})
511     public void detached_file() throws Exception {
512         changeTypeDetached("1234", "file");
513     }
514 
515     /**
516      * @throws Exception if the test fails
517      */
518     @Test
519     @Alerts({"1234--null", "1234-1234-1234"})
520     public void detached_hidden() throws Exception {
521         changeTypeDetached("1234", "hidden");
522     }
523 
524     /**
525      * @throws Exception if the test fails
526      */
527     @Test
528     @Alerts({"1234--null", "1234-1234-1234"})
529     public void detached_image() throws Exception {
530         changeTypeDetached("1234", "image");
531     }
532 
533     /**
534      * @throws Exception if the test fails
535      */
536     @Test
537     @Alerts(DEFAULT = {"12--null", "12--null"},
538             CHROME = {"12--null", "--null"},
539             EDGE = {"12--null", "--null"})
540     public void detached_month() throws Exception {
541         changeTypeDetached("12", "month");
542     }
543 
544     /**
545      * @throws Exception if the test fails
546      */
547     @Test
548     @Alerts({"1234--null", "1234--null"})
549     public void detached_number() throws Exception {
550         changeTypeDetached("1234", "number");
551     }
552 
553     /**
554      * @throws Exception if the test fails
555      */
556     @Test
557     @Alerts({"1234--null", "1234--null"})
558     public void detached_password() throws Exception {
559         changeTypeDetached("1234", "password");
560     }
561 
562     /**
563      * @throws Exception if the test fails
564      */
565     @Test
566     @Alerts({"1234--null", "1234-1234-1234"})
567     public void detached_radio() throws Exception {
568         changeTypeDetached("1234", "radio");
569     }
570 
571     /**
572      * @throws Exception if the test fails
573      */
574     @Test
575     @Alerts({"7--null", "7--null"})
576     public void detached_range() throws Exception {
577         changeTypeDetached("7", "range");
578     }
579 
580     /**
581      * @throws Exception if the test fails
582      */
583     @Test
584     @Alerts({"1234--null", "1234-1234-1234"})
585     public void detached_reset() throws Exception {
586         changeTypeDetached("1234", "reset");
587     }
588 
589     /**
590      * @throws Exception if the test fails
591      */
592     @Test
593     @Alerts({"1234--null", "1234--null"})
594     public void detached_search() throws Exception {
595         changeTypeDetached("1234", "search");
596     }
597 
598     /**
599      * @throws Exception if the test fails
600      */
601     @Test
602     @Alerts({"1234--null", "1234-1234-1234"})
603     public void detached_submit() throws Exception {
604         changeTypeDetached("1234", "submit");
605     }
606 
607     /**
608      * @throws Exception if the test fails
609      */
610     @Test
611     @Alerts({"1234--null", "1234--null"})
612     public void detached_tel() throws Exception {
613         changeTypeDetached("1234", "tel");
614     }
615 
616     /**
617      * @throws Exception if the test fails
618      */
619     @Test
620     @Alerts({"1234--null", "1234--null"})
621     public void detached_text() throws Exception {
622         changeTypeDetached("1234", "text");
623     }
624 
625     /**
626      * @throws Exception if the test fails
627      */
628     @Test
629     @Alerts({"4:16--null", "--null"})
630     public void detached_time() throws Exception {
631         changeTypeDetached("4:16", "time");
632     }
633 
634     /**
635      * @throws Exception if the test fails
636      */
637     @Test
638     @Alerts({"https://www.wetator.org--null",
639              "https://www.wetator.org--null"})
640     public void detached_url() throws Exception {
641         changeTypeDetached("https://www.wetator.org", "url");
642     }
643 
644     /**
645      * @throws Exception if the test fails
646      */
647     @Test
648     @Alerts(DEFAULT = {"24--null", "24--null"},
649             CHROME = {"24--null", "--null"},
650             EDGE = {"24--null", "--null"})
651     public void detached_week() throws Exception {
652         changeTypeDetached("24", "week");
653     }
654 
655     /**
656      * @throws Exception if the test fails
657      */
658     @Test
659     @Alerts({"2020-04-7T1:13--null", "2020-04-7T1:13--null"})
660     public void detached_datetime() throws Exception {
661         changeTypeDetached("2020-04-7T1:13", "datetime");
662     }
663 
664     private void changeTypeDetached(final String value, final String targetType) throws Exception {
665         final String html = DOCTYPE_HTML
666                 + "<html><head>\n"
667                 + "<script>\n"
668                 + LOG_TITLE_FUNCTION
669                 + "  function test() {\n"
670                 + "    var input = document.createElement('input');\n"
671                 + "    input.value = '" + value + "';\n"
672                 + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
673 
674                 + "    try {\n"
675                 + "      input.type = '" + targetType + "';\n"
676                 + "      log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
677                 + "    } catch(e) { logEx(e); }\n"
678                 + "  }\n"
679                 + "</script>\n"
680                 + "</head><body onload='test()'>\n"
681                 + "</body></html>";
682 
683         loadPageVerifyTitle2(html);
684     }
685 }