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.javascript.host.event.KeyboardEvent;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.jupiter.api.Test;
21
22
23
24
25
26
27
28
29 public class HtmlEmailInput2Test extends SimpleWebTestCase {
30
31
32
33
34 @Test
35 public void typingAndClone() throws Exception {
36 final String htmlContent = DOCTYPE_HTML
37 + "<html>\n"
38 + "<head></head>\n"
39 + "<body>\n"
40 + "<form id='form1'>\n"
41 + " <input type='email' id='foo'>\n"
42 + "</form>\n"
43 + "</body></html>";
44
45 final HtmlPage page = loadPage(htmlContent);
46
47 HtmlEmailInput input = (HtmlEmailInput) page.getElementById("foo");
48 input = (HtmlEmailInput) input.cloneNode(true);
49 input.type("abc@email.com");
50 assertEquals("", input.getValueAttribute());
51 assertEquals("abc@email.com", input.getValue());
52 }
53
54
55
56
57 @Test
58 public void typingAndReset() throws Exception {
59 final String htmlContent = DOCTYPE_HTML
60 + "<html>\n"
61 + "<head></head>\n"
62 + "<body>\n"
63 + "<form id='form1'>\n"
64 + " <input type='email' id='foo'>\n"
65 + "</form>\n"
66 + "</body></html>";
67
68 final HtmlPage page = loadPage(htmlContent);
69
70 final HtmlEmailInput input = (HtmlEmailInput) page.getElementById("foo");
71
72 input.type("abc@email.com");
73 input.reset();
74 input.type("xyz@email.com");
75
76 assertEquals("", input.getValueAttribute());
77 assertEquals("xyz@email.com", input.getValue());
78 }
79
80
81
82
83 @Test
84 public void typingAndSetValueAttribute() throws Exception {
85 final String htmlContent = DOCTYPE_HTML
86 + "<html>\n"
87 + "<head></head>\n"
88 + "<body>\n"
89 + "<form id='form1'>\n"
90 + " <input type='email' id='foo'>\n"
91 + "</form>\n"
92 + "</body></html>";
93
94 final HtmlPage page = loadPage(htmlContent);
95
96 final HtmlEmailInput input = (HtmlEmailInput) page.getElementById("foo");
97
98 input.type("abc@email.com");
99 input.setValueAttribute("");
100 input.type("xyz@email.com");
101
102 assertEquals("", input.getValueAttribute());
103 assertEquals("abc@email.comxyz@email.com", input.getValue());
104 }
105
106
107
108
109 @Test
110 public void typingAndSetValue() throws Exception {
111 final String htmlContent = DOCTYPE_HTML
112 + "<html>\n"
113 + "<head></head>\n"
114 + "<body>\n"
115 + "<form id='form1'>\n"
116 + " <input type='email' id='foo'>\n"
117 + "</form>\n"
118 + "</body></html>";
119
120 final HtmlPage page = loadPage(htmlContent);
121
122 final HtmlEmailInput input = (HtmlEmailInput) page.getElementById("foo");
123
124 input.type("abc@email.com");
125 input.setValue("");
126 input.type("xyz@email.com");
127
128 assertEquals("", input.getValueAttribute());
129 assertEquals("xyz@email.com", input.getValue());
130 }
131
132
133
134
135 @Test
136 public void patternValidation() throws Exception {
137 final String htmlContent = DOCTYPE_HTML
138 + "<html>\n"
139 + "<head></head>\n"
140 + "<body>\n"
141 + "<form id='form1'>\n"
142 + " <input type='email' pattern='.+@email.com' id='foo'>\n"
143 + "</form>\n"
144 + "</body></html>";
145
146 final HtmlPage page = loadPage(htmlContent);
147
148 final HtmlEmailInput input = (HtmlEmailInput) page.getElementById("foo");
149
150
151 assertTrue(input.isValid());
152
153 input.setValue("abc@eemail.com");
154 assertFalse(input.isValid());
155
156 input.setValue("abc@email.com");
157 assertTrue(input.isValid());
158 }
159
160
161
162
163
164
165 @Test
166 public void basicValidation() throws Exception {
167 final String htmlContent = DOCTYPE_HTML
168 + "<html>\n"
169 + "<head></head>\n"
170 + "<body>\n"
171 + "<form id='form1'>\n"
172 + " <input type='email' id='foo'>\n"
173 + "</form>\n"
174 + "</body></html>";
175
176 final HtmlPage page = loadPage(htmlContent);
177
178 final HtmlEmailInput input = (HtmlEmailInput) page.getElementById("foo");
179
180
181 assertTrue(input.isValid());
182
183 input.setValue("abc");
184 assertFalse(input.isValid());
185
186 input.setValue("abc@email.com");
187 assertTrue(input.isValid());
188 }
189
190
191
192
193
194 @Test
195 @Alerts("bla")
196 public void asNormalizedText() throws Exception {
197 final String html = DOCTYPE_HTML
198 + "<html>\n"
199 + "<head></head>\n"
200 + "<body>\n"
201 + "<form id='form1'>\n"
202 + " <input type='email' name='tester' id='tester' value='bla'>\n"
203 + "</form>\n"
204 + "</body></html>";
205
206 final HtmlPage page = loadPage(html);
207 assertEquals(getExpectedAlerts()[0], page.getBody().asNormalizedText());
208 }
209
210
211
212
213
214 @Test
215 public void typeWhileSelected() throws Exception {
216 final String html = DOCTYPE_HTML
217 + "<html><head></head><body>\n"
218 + "<input type='email' id='myInput' value='Hello@world.com'><br>\n"
219 + "</body></html>";
220 final HtmlPage page = loadPage(html);
221 final HtmlEmailInput input = page.getHtmlElementById("myInput");
222 input.select();
223 input.type("bye@world.com");
224 assertEquals("Hello@world.com", input.getValueAttribute());
225 assertEquals("bye@world.com", input.getValue());
226 }
227
228
229
230
231 @Test
232 public void typeLeftArrow() throws Exception {
233 final String html = DOCTYPE_HTML + "<html><head></head><body><input type='email' id='t'/></body></html>";
234 final HtmlPage page = loadPage(html);
235 final HtmlEmailInput t = page.getHtmlElementById("t");
236 t.type('t');
237 t.type('e');
238 t.type('t');
239 assertEquals("", t.getValueAttribute());
240 assertEquals("tet", t.getValue());
241 t.type(KeyboardEvent.DOM_VK_LEFT);
242 assertEquals("", t.getValueAttribute());
243 assertEquals("tet", t.getValue());
244 t.type('s');
245 assertEquals("", t.getValueAttribute());
246 assertEquals("test", t.getValue());
247 t.type(KeyboardEvent.DOM_VK_SPACE);
248 assertEquals("", t.getValueAttribute());
249 assertEquals("tes t", t.getValue());
250 }
251
252
253
254
255 @Test
256 public void typeDelKey() throws Exception {
257 final String html = DOCTYPE_HTML + "<html><head></head><body><input type='email' id='t'/></body></html>";
258 final HtmlPage page = loadPage(html);
259 final HtmlEmailInput t = page.getHtmlElementById("t");
260 t.type('t');
261 t.type('e');
262 t.type('t');
263 assertEquals("", t.getValueAttribute());
264 assertEquals("tet", t.getValue());
265 t.type(KeyboardEvent.DOM_VK_LEFT);
266 t.type(KeyboardEvent.DOM_VK_LEFT);
267 assertEquals("", t.getValueAttribute());
268 assertEquals("tet", t.getValue());
269 t.type(KeyboardEvent.DOM_VK_DELETE);
270 assertEquals("", t.getValueAttribute());
271 assertEquals("tt", t.getValue());
272 }
273
274
275
276
277
278 @Test
279 @Alerts({"true", "true", "true", "", "a@b.co"})
280 public void maxLengthValidation() throws Exception {
281 final String htmlContent = DOCTYPE_HTML
282 + "<html>\n"
283 + "<head></head>\n"
284 + "<body>\n"
285 + "<form id='form1'>\n"
286 + " <input type='email' id='foo' maxLength='6'>\n"
287 + "</form>\n"
288 + "</body></html>";
289
290 final HtmlPage page = loadPage(htmlContent);
291
292 final HtmlInput input = (HtmlInput) page.getElementById("foo");
293 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
294 input.type("a@b.c");
295 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
296 input.type("om");
297 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
298 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
299 assertEquals(getExpectedAlerts()[4], input.getValue());
300 }
301
302
303
304
305
306 @Test
307 @Alerts({"true", "false", "true", "", "a@b.com"})
308 public void minLengthValidation() throws Exception {
309 final String htmlContent = DOCTYPE_HTML
310 + "<html>\n"
311 + "<head></head>\n"
312 + "<body>\n"
313 + "<form id='form1'>\n"
314 + " <input type='email' id='foo' minLength='6'>\n"
315 + "</form>\n"
316 + "</body></html>";
317
318 final HtmlPage page = loadPage(htmlContent);
319
320 final HtmlInput input = (HtmlInput) page.getElementById("foo");
321 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
322 input.type("a@b.c");
323 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
324 input.type("om");
325 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
326 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
327 assertEquals(getExpectedAlerts()[4], input.getValue());
328 }
329 }