1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.SimpleWebTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26
27 public class HtmlSearchInput2Test extends SimpleWebTestCase {
28
29
30
31
32 @Test
33 public void patternValidation() throws Exception {
34 final String htmlContent = DOCTYPE_HTML
35 + "<html>\n"
36 + "<head></head>\n"
37 + "<body>\n"
38 + "<form id='form1'>\n"
39 + " <input type='search' pattern='[A-z]{2}[0-9]{4}' id='foo'>\n"
40 + "</form>\n"
41 + "</body></html>";
42
43 final HtmlPage page = loadPage(htmlContent);
44
45 final HtmlSearchInput input = (HtmlSearchInput) page.getElementById("foo");
46
47
48 assertTrue(input.isValid());
49
50 input.setValue("qwerty");
51 assertFalse(input.isValid());
52
53 input.setValue("AB1234");
54 assertTrue(input.isValid());
55 }
56
57
58
59
60
61 @Test
62 @Alerts({"true", "true", "true", "", "foo"})
63 public void maxLengthValidation() throws Exception {
64 final String htmlContent = DOCTYPE_HTML
65 + "<html>\n"
66 + "<head></head>\n"
67 + "<body>\n"
68 + "<form id='form1'>\n"
69 + " <input type='search' id='foo' maxLength='3'>\n"
70 + "</form>\n"
71 + "</body></html>";
72
73 final HtmlPage page = loadPage(htmlContent);
74
75 final HtmlInput input = (HtmlInput) page.getElementById("foo");
76 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
77 input.type("foo");
78 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
79 input.type("bar");
80 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
81 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
82 assertEquals(getExpectedAlerts()[4], input.getValue());
83 }
84
85
86
87
88
89 @Test
90 @Alerts({"true", "false", "true", "", "foobar"})
91 public void minLengthValidation() throws Exception {
92 final String htmlContent = DOCTYPE_HTML
93 + "<html>\n"
94 + "<head></head>\n"
95 + "<body>\n"
96 + "<form id='form1'>\n"
97 + " <input type='search' id='foo' minLength='4'>\n"
98 + "</form>\n"
99 + "</body></html>";
100
101 final HtmlPage page = loadPage(htmlContent);
102
103 final HtmlInput input = (HtmlInput) page.getElementById("foo");
104 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
105 input.type("foo");
106 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
107 input.type("bar");
108 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
109 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
110 assertEquals(getExpectedAlerts()[4], input.getValue());
111 }
112 }