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