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 HtmlUrlInput2Test extends SimpleWebTestCase {
31
32
33
34
35
36 @Test
37 @Alerts("http://htmlunit.sourceforge.net")
38 public void asNormalizedText() throws Exception {
39 final String html = DOCTYPE_HTML
40 + "<html>\n"
41 + "<head></head>\n"
42 + "<body>\n"
43 + "<form id='form1'>\n"
44 + " <input type='url' id='tester' value='http://htmlunit.sourceforge.net'>\n"
45 + "</form>\n"
46 + "</body></html>";
47
48 final HtmlPage page = loadPage(html);
49 assertEquals(getExpectedAlerts()[0], page.getBody().asNormalizedText());
50 }
51
52
53
54
55 @Test
56 public void typingAndClone() throws Exception {
57 final String htmlContent = DOCTYPE_HTML
58 + "<html>\n"
59 + "<head></head>\n"
60 + "<body>\n"
61 + "<form id='form1'>\n"
62 + " <input type='url' id='foo'>\n"
63 + "</form>\n"
64 + "</body></html>";
65
66 final HtmlPage page = loadPage(htmlContent);
67
68 HtmlUrlInput input = (HtmlUrlInput) page.getElementById("foo");
69 input = (HtmlUrlInput) input.cloneNode(true);
70 input.type("4711");
71 assertEquals("", input.getValueAttribute());
72 assertEquals("4711", input.getValue());
73 }
74
75
76
77
78 @Test
79 public void typingAndReset() throws Exception {
80 final String htmlContent = DOCTYPE_HTML
81 + "<html>\n"
82 + "<head></head>\n"
83 + "<body>\n"
84 + "<form id='form1'>\n"
85 + " <input type='url' id='foo'>\n"
86 + "</form>\n"
87 + "</body></html>";
88
89 final HtmlPage page = loadPage(htmlContent);
90
91 final HtmlUrlInput input = (HtmlUrlInput) page.getElementById("foo");
92
93 input.type("4711");
94 input.reset();
95 input.type("0815");
96
97 assertEquals("", input.getValueAttribute());
98 assertEquals("0815", input.getValue());
99 }
100
101
102
103
104 @Test
105 public void typingAndSetValueAttribute() throws Exception {
106 final String htmlContent = DOCTYPE_HTML
107 + "<html>\n"
108 + "<head></head>\n"
109 + "<body>\n"
110 + "<form id='form1'>\n"
111 + " <input type='url' id='foo'>\n"
112 + "</form>\n"
113 + "</body></html>";
114
115 final HtmlPage page = loadPage(htmlContent);
116
117 final HtmlUrlInput input = (HtmlUrlInput) page.getElementById("foo");
118
119 input.type("4711");
120 input.setValueAttribute("");
121 input.type("0815");
122
123 assertEquals("", input.getValueAttribute());
124 assertEquals("47110815", input.getValue());
125 }
126
127
128
129
130 @Test
131 public void typingAndSetValue() throws Exception {
132 final String htmlContent = DOCTYPE_HTML
133 + "<html>\n"
134 + "<head></head>\n"
135 + "<body>\n"
136 + "<form id='form1'>\n"
137 + " <input type='url' id='foo'>\n"
138 + "</form>\n"
139 + "</body></html>";
140
141 final HtmlPage page = loadPage(htmlContent);
142
143 final HtmlUrlInput input = (HtmlUrlInput) page.getElementById("foo");
144
145 input.type("4711");
146 input.setValue("");
147 input.type("0815");
148
149 assertEquals("", input.getValueAttribute());
150 assertEquals("0815", input.getValue());
151 }
152
153
154
155
156 @Test
157 public void patternValidation() throws Exception {
158 final String htmlContent = DOCTYPE_HTML
159 + "<html>\n"
160 + "<head></head>\n"
161 + "<body>\n"
162 + "<form id='form1'>\n"
163 + " <input type='url' pattern='.*github.*' id='foo'>\n"
164 + "</form>\n"
165 + "</body></html>";
166
167 final HtmlPage page = loadPage(htmlContent);
168
169 final HtmlUrlInput input = (HtmlUrlInput) page.getElementById("foo");
170
171
172 assertTrue(input.isValid());
173
174 input.setValue("https://sourceforge.net/projects/htmlunit/");
175 assertFalse(input.isValid());
176
177 input.setValue("https://github.com/HtmlUnit/htmlunit");
178 assertTrue(input.isValid());
179 }
180
181
182
183
184
185 @Test
186 @Alerts({"true", "true", "true", "", "https://github.com"})
187 public void maxLengthValidation() throws Exception {
188 final String htmlContent = DOCTYPE_HTML
189 + "<html>\n"
190 + "<head></head>\n"
191 + "<body>\n"
192 + "<form id='form1'>\n"
193 + " <input type='url' id='foo' maxLength='18'>\n"
194 + "</form>\n"
195 + "</body></html>";
196
197 final HtmlPage page = loadPage(htmlContent);
198
199 final HtmlInput input = (HtmlInput) page.getElementById("foo");
200 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
201 input.type("https://github.com");
202 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
203 input.type("/HtmlUnit/htmlunit");
204 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
205 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
206 assertEquals(getExpectedAlerts()[4], input.getValue());
207 }
208
209
210
211
212
213 @Test
214 @Alerts({"true", "false", "true", "", "https://github.com/HtmlUnit/htmlunit"})
215 public void minLengthValidation() throws Exception {
216 final String htmlContent = DOCTYPE_HTML
217 + "<html>\n"
218 + "<head></head>\n"
219 + "<body>\n"
220 + "<form id='form1'>\n"
221 + " <input type='url' id='foo' minLength='20'>\n"
222 + "</form>\n"
223 + "</body></html>";
224
225 final HtmlPage page = loadPage(htmlContent);
226
227 final HtmlInput input = (HtmlInput) page.getElementById("foo");
228 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
229 input.type("https://github.com");
230 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
231 input.type("/HtmlUnit/htmlunit");
232 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
233 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
234 assertEquals(getExpectedAlerts()[4], input.getValue());
235 }
236 }