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