1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.awt.GraphicsEnvironment;
18 import java.util.LinkedList;
19 import java.util.List;
20
21 import org.htmlunit.ClipboardHandler;
22 import org.htmlunit.MockWebConnection;
23 import org.htmlunit.SimpleWebTestCase;
24 import org.htmlunit.WebClient;
25 import org.htmlunit.javascript.host.event.KeyboardEvent;
26 import org.htmlunit.junit.BrowserRunner;
27 import org.htmlunit.junit.annotation.Alerts;
28 import org.htmlunit.platform.AwtClipboardHandler;
29 import org.junit.Assume;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32
33
34
35
36
37
38
39
40
41
42 @RunWith(BrowserRunner.class)
43 public class HtmlTextInput2Test extends SimpleWebTestCase {
44
45 private static boolean SKIP_ = false;
46
47 static {
48 if (GraphicsEnvironment.isHeadless()) {
49
50 SKIP_ = true;
51 }
52 }
53
54
55
56
57
58 @Test
59 @Alerts("bla")
60 public void asNormalizedText() throws Exception {
61 final String html = DOCTYPE_HTML
62 + "<html>\n"
63 + "<head></head>\n"
64 + "<body>\n"
65 + "<form id='form1'>\n"
66 + " <input type='text' name='tester' id='tester' value='bla'>\n"
67 + "</form>\n"
68 + "</body></html>";
69
70 final HtmlPage page = loadPage(html);
71 assertEquals(getExpectedAlerts()[0], page.getBody().asNormalizedText());
72 }
73
74
75
76
77 @Test
78 public void type() throws Exception {
79 final String html = DOCTYPE_HTML + "<html><head></head><body><input id='t'/></body></html>";
80 final HtmlPage page = loadPage(html);
81 final HtmlTextInput t = page.getHtmlElementById("t");
82 t.type("abc");
83 assertEquals("", t.getValueAttribute());
84 assertEquals("abc", t.getValue());
85 t.type('\b');
86 assertEquals("", t.getValueAttribute());
87 assertEquals("ab", t.getValue());
88 t.type('\b');
89 assertEquals("", t.getValueAttribute());
90 assertEquals("a", t.getValue());
91 t.type('\b');
92 assertEquals("", t.getValueAttribute());
93 assertEquals("", t.getValue());
94 t.type('\b');
95 assertEquals("", t.getValueAttribute());
96 assertEquals("", t.getValue());
97 }
98
99
100
101
102
103
104
105 @Test
106 public void type_StringIndexOutOfBoundsException() throws Exception {
107 type_StringIndexOutOfBoundsException("<input type='text' id='t'>");
108 type_StringIndexOutOfBoundsException("<input type='password' id='t'>");
109 type_StringIndexOutOfBoundsException("<textarea id='t'></textarea>");
110 }
111
112 void type_StringIndexOutOfBoundsException(final String tag) throws Exception {
113 final String html = DOCTYPE_HTML
114 + "<html><head></head><body>\n"
115 + tag + "\n"
116 + "<script>\n"
117 + "function copy(node) {\n"
118 + " e.value = '231';\n"
119 + "}\n"
120 + "var e = document.getElementById('t');\n"
121 + "e.onkeyup = copy;\n"
122 + "var c = e.cloneNode();\n"
123 + "c.id = 't2';\n"
124 + "document.body.appendChild(c);\n"
125 + "</script>\n"
126 + "</body></html>";
127 final HtmlPage page = loadPage(html);
128 final HtmlElement t = page.getHtmlElementById("t2");
129 t.type("abc");
130 assertEquals("abc", t.asNormalizedText());
131 }
132
133
134
135
136 @Test
137 public void typeWhileDisabled() throws Exception {
138 final String html = DOCTYPE_HTML + "<html><body><input id='t' disabled='disabled'/></body></html>";
139 final HtmlPage page = loadPage(html);
140 final HtmlTextInput t = page.getHtmlElementById("t");
141 t.type("abc");
142 assertEquals("", t.getValueAttribute());
143 assertEquals("", t.getValue());
144 }
145
146
147
148
149 @Test
150 public void preventDefault() throws Exception {
151 final String html = DOCTYPE_HTML
152 + "<html><head><script>\n"
153 + " function handler(e) {\n"
154 + " if (e && e.target.value.length > 2)\n"
155 + " e.preventDefault();\n"
156 + " else if (!e && window.event.srcElement.value.length > 2)\n"
157 + " return false;\n"
158 + " }\n"
159 + " function init() {\n"
160 + " document.getElementById('text1').onkeydown = handler;\n"
161 + " }\n"
162 + "</script></head>\n"
163 + "<body onload='init()'>\n"
164 + "<input id='text1'/>\n"
165 + "</body></html>";
166
167 final HtmlPage page = loadPage(html);
168 final HtmlTextInput text1 = page.getHtmlElementById("text1");
169 text1.type("abcd");
170 assertEquals("", text1.getValueAttribute());
171 assertEquals("abc", text1.getValue());
172 }
173
174
175
176
177 @Test
178 public void typeNewLine() throws Exception {
179 final String firstContent = DOCTYPE_HTML
180 + "<html><head><title>First</title></head><body>\n"
181 + "<form action='" + URL_SECOND + "'>\n"
182 + "<input name='myText' id='myText'>\n"
183 + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n"
184 + "</body></html>";
185 final String secondContent = DOCTYPE_HTML + "<html><head><title>Second</title></head><body></body></html>";
186
187 final WebClient client = getWebClient();
188
189 final MockWebConnection webConnection = new MockWebConnection();
190 webConnection.setResponse(URL_FIRST, firstContent);
191 webConnection.setDefaultResponse(secondContent);
192
193 client.setWebConnection(webConnection);
194
195 final HtmlPage firstPage = client.getPage(URL_FIRST);
196
197 final HtmlTextInput textInput = firstPage.getHtmlElementById("myText");
198
199 final HtmlPage secondPage = (HtmlPage) textInput.type('\n');
200 assertEquals("Second", secondPage.getTitleText());
201 }
202
203
204
205
206 @Test
207 @Alerts({"exception", "My old value", "My old value"})
208 public void setSelectionText() throws Exception {
209 final String html = DOCTYPE_HTML
210 + "<html><head><script>\n"
211 + " function test() {\n"
212 + " try {\n"
213 + " document.selection.createRange().text = 'new';\n"
214 + " } catch(e) { alert('exception'); }\n"
215 + " }\n"
216 + "</script></head>\n"
217 + "<body>\n"
218 + "<input id='myInput' value='My old value'><br>\n"
219 + "<input id='myButton' type='button' value='Test' onclick='test()'>\n"
220 + "</body></html>";
221
222 final List<String> alerts = new LinkedList<>();
223
224 final HtmlPage page = loadPage(html, alerts);
225 final HtmlTextInput input = page.getHtmlElementById("myInput");
226 final HtmlButtonInput button = page.getHtmlElementById("myButton");
227 page.setFocusedElement(input);
228 input.setSelectionStart(3);
229 input.setSelectionEnd(6);
230 button.click();
231
232 alerts.add(input.getValueAttribute());
233 alerts.add(input.getValue());
234 assertEquals(getExpectedAlerts(), alerts);
235 }
236
237
238
239
240 @Test
241 public void typeWhenSelected() throws Exception {
242 final String html = DOCTYPE_HTML
243 + "<html><head></head>\n"
244 + "<body>\n"
245 + "<input id='myInput' value='Hello world'><br>\n"
246 + "</body></html>";
247
248 final HtmlPage page = loadPage(html);
249 final HtmlTextInput input = page.getHtmlElementById("myInput");
250 input.select();
251 input.type("Bye World");
252 assertEquals("Hello world", input.getValueAttribute());
253 assertEquals("Bye World", input.getValue());
254 }
255
256
257
258
259 @Test
260 public void typeWhen_selectPositionChanged() throws Exception {
261 final String html = DOCTYPE_HTML
262 + "<html><head></head>\n"
263 + "<body>\n"
264 + "<input id='myInput' value='Hello world'><br>\n"
265 + "</body></html>";
266
267 final HtmlPage page = loadPage(html);
268 final HtmlTextInput input = page.getHtmlElementById("myInput");
269 input.select();
270 input.type("Bye World!");
271 assertEquals("Hello world", input.getValueAttribute());
272 assertEquals("Bye World!", input.getValue());
273
274 input.type("\b");
275 assertEquals("Hello world", input.getValueAttribute());
276 assertEquals("Bye World", input.getValue());
277
278 input.setSelectionStart(4);
279 input.setSelectionEnd(4);
280 input.type("Bye ");
281 assertEquals("Hello world", input.getValueAttribute());
282 assertEquals("Bye Bye World", input.getValue());
283
284 input.type("\b\b\b\b");
285 assertEquals("Hello world", input.getValueAttribute());
286 assertEquals("Bye World", input.getValue());
287
288 input.setSelectionStart(0);
289 input.setSelectionEnd(3);
290 input.type("Hello");
291 assertEquals("Hello world", input.getValueAttribute());
292 assertEquals("Hello World", input.getValue());
293 }
294
295
296
297
298 @Test
299 public void type_specialCharacters() throws Exception {
300 final String html = DOCTYPE_HTML
301 + "<html><head></head><body>\n"
302 + "<form>\n"
303 + "<input id='t' onkeyup='document.forms[0].lastKey.value = event.keyCode'>\n"
304 + "<input id='lastKey'>\n"
305 + "</form>\n"
306 + "</body></html>";
307 final HtmlPage page = loadPage(html);
308 final HtmlTextInput t = page.getHtmlElementById("t");
309 final HtmlTextInput lastKey = page.getHtmlElementById("lastKey");
310 t.type("abc");
311 assertEquals("", t.getValueAttribute());
312 assertEquals("abc", t.getValue());
313 assertEquals("", lastKey.getValueAttribute());
314 assertEquals("67", lastKey.getValue());
315
316
317 t.type("\uE014");
318 assertEquals("", t.getValueAttribute());
319 assertEquals("abc", t.getValue());
320
321
322 }
323
324
325
326
327 @Test
328 public void serialization() throws Exception {
329 final String html = DOCTYPE_HTML
330 + "<html><head></head><body onload=''><input type='text' onkeydown='' /></body></html>";
331 final HtmlPage page = loadPage(html);
332 final HtmlPage page2 = clone(page);
333 assertNotNull(page2);
334 }
335
336
337
338
339 @Test
340 public void typeLeftArrow() throws Exception {
341 final String html = DOCTYPE_HTML + "<html><head></head><body><input id='t'/></body></html>";
342 final HtmlPage page = loadPage(html);
343 final HtmlTextInput t = page.getHtmlElementById("t");
344 t.type('t');
345 t.type('e');
346 t.type('t');
347 assertEquals("", t.getValueAttribute());
348 assertEquals("tet", t.getValue());
349 t.type(KeyboardEvent.DOM_VK_LEFT);
350 assertEquals("", t.getValueAttribute());
351 assertEquals("tet", t.getValue());
352 t.type('s');
353 assertEquals("", t.getValueAttribute());
354 assertEquals("test", t.getValue());
355 t.type(KeyboardEvent.DOM_VK_SPACE);
356 assertEquals("", t.getValueAttribute());
357 assertEquals("tes t", t.getValue());
358 }
359
360
361
362
363 @Test
364 public void typeDelKey() throws Exception {
365 final String html = DOCTYPE_HTML + "<html><head></head><body><input id='t'/></body></html>";
366 final HtmlPage page = loadPage(html);
367 final HtmlTextInput t = page.getHtmlElementById("t");
368 t.type('t');
369 t.type('e');
370 t.type('t');
371 assertEquals("", t.getValueAttribute());
372 assertEquals("tet", t.getValue());
373 t.type(KeyboardEvent.DOM_VK_LEFT);
374 t.type(KeyboardEvent.DOM_VK_LEFT);
375 assertEquals("", t.getValueAttribute());
376 assertEquals("tet", t.getValue());
377 t.type(KeyboardEvent.DOM_VK_DELETE);
378 assertEquals("", t.getValueAttribute());
379 assertEquals("tt", t.getValue());
380 }
381
382
383
384
385 @Test
386 public void submitOnEnter() throws Exception {
387 final String html = DOCTYPE_HTML
388 + "<html>\n"
389 + "<body>\n"
390 + " <form action='result.html'>\n"
391 + " <input id='t' value='hello'/>\n"
392 + " </form>\n"
393 + "</body>\n"
394 + "</html>";
395
396 final HtmlPage page = loadPage(html);
397 final HtmlTextInput t = page.getHtmlElementById("t");
398
399 t.type("\n");
400
401 assertEquals(2, getMockWebConnection().getRequestCount());
402 }
403
404
405
406
407 @Test
408 public void submitOnEnterWithoutForm() throws Exception {
409 final String html = DOCTYPE_HTML
410 + "<html>\n"
411 + "<body>\n"
412 + " <input id='t' value='hello'/>\n"
413 + "</body>\n"
414 + "</html>";
415
416 final HtmlPage page = loadPage(html);
417 final HtmlTextInput t = page.getHtmlElementById("t");
418
419 t.type("\n");
420
421 assertEquals(1, getMockWebConnection().getRequestCount());
422 }
423
424
425
426
427 @Test
428 public void typingAndClone() throws Exception {
429 final String htmlContent = DOCTYPE_HTML
430 + "<html>\n"
431 + "<head></head>\n"
432 + "<body>\n"
433 + "<form id='form1'>\n"
434 + " <input id='foo'>\n"
435 + "</form>\n"
436 + "</body></html>";
437
438 final HtmlPage page = loadPage(htmlContent);
439
440 HtmlTextInput input = (HtmlTextInput) page.getElementById("foo");
441 input = (HtmlTextInput) input.cloneNode(true);
442 input.type("4711");
443 assertEquals("", input.getValueAttribute());
444 assertEquals("4711", input.getValue());
445 }
446
447
448
449
450 @Test
451 public void typingAndReset() throws Exception {
452 final String htmlContent = DOCTYPE_HTML
453 + "<html>\n"
454 + "<head></head>\n"
455 + "<body>\n"
456 + "<form id='form1'>\n"
457 + " <input id='foo'>\n"
458 + "</form>\n"
459 + "</body></html>";
460
461 final HtmlPage page = loadPage(htmlContent);
462
463 final HtmlTextInput input = (HtmlTextInput) page.getElementById("foo");
464
465 input.type("4711");
466 input.reset();
467 input.type("0815");
468
469 assertEquals("", input.getValueAttribute());
470 assertEquals("0815", input.getValue());
471 }
472
473
474
475
476 @Test
477 public void typingAndSetValueAttribute() throws Exception {
478 final String htmlContent = DOCTYPE_HTML
479 + "<html>\n"
480 + "<head></head>\n"
481 + "<body>\n"
482 + "<form id='form1'>\n"
483 + " <input id='foo'>\n"
484 + "</form>\n"
485 + "</body></html>";
486
487 final HtmlPage page = loadPage(htmlContent);
488
489 final HtmlTextInput input = (HtmlTextInput) page.getElementById("foo");
490
491 input.type("4711");
492 input.setValueAttribute("");
493 input.type("0815");
494
495 assertEquals("", input.getValueAttribute());
496 assertEquals("47110815", input.getValue());
497 }
498
499
500
501
502 @Test
503 public void typingAndSetValue() throws Exception {
504 final String htmlContent = DOCTYPE_HTML
505 + "<html>\n"
506 + "<head></head>\n"
507 + "<body>\n"
508 + "<form id='form1'>\n"
509 + " <input id='foo'>\n"
510 + "</form>\n"
511 + "</body></html>";
512
513 final HtmlPage page = loadPage(htmlContent);
514
515 final HtmlTextInput input = (HtmlTextInput) page.getElementById("foo");
516
517 input.type("4711");
518 input.setValue("");
519 input.type("0815");
520
521 assertEquals("", input.getValueAttribute());
522 assertEquals("0815", input.getValue());
523 }
524
525
526
527
528
529 @Test
530 @Alerts({"text", "x", "x", "hidden", "x", "x"})
531 public void setType() throws Exception {
532 final String htmlContent = DOCTYPE_HTML
533 + "<html>\n"
534 + "<body>\n"
535 + "<form id='form1'>\n"
536 + " <input type='text' id='foo' value='x'>\n"
537 + "</form>\n"
538 + "</body></html>";
539
540 final HtmlPage page = loadPage(htmlContent);
541
542 final HtmlInput input = (HtmlInput) page.getElementById("foo");
543 assertEquals(getExpectedAlerts()[0], input.getType());
544 assertEquals(getExpectedAlerts()[1], input.getValueAttribute());
545 assertEquals(getExpectedAlerts()[2], input.getValue());
546
547 input.changeType("hidden", true);
548 assertEquals(getExpectedAlerts()[0], input.getType());
549 assertEquals(getExpectedAlerts()[1], input.getValueAttribute());
550 assertEquals(getExpectedAlerts()[2], input.getValue());
551
552 final HtmlInput newInput = (HtmlInput) page.getElementById("foo");
553 assertEquals(getExpectedAlerts()[3], newInput.getType());
554 assertEquals(getExpectedAlerts()[4], newInput.getValueAttribute());
555 assertEquals(getExpectedAlerts()[5], newInput.getValue());
556
557 assertNotSame(input, newInput);
558 }
559
560
561
562
563 @Test
564 public void patternValidation() throws Exception {
565 final String htmlContent = DOCTYPE_HTML
566 + "<html>\n"
567 + "<head></head>\n"
568 + "<body>\n"
569 + "<form id='form1'>\n"
570 + " <input type='text' pattern='[a-z]{4,8}' id='foo'>\n"
571 + "</form>\n"
572 + "</body></html>";
573
574 final HtmlPage page = loadPage(htmlContent);
575
576 final HtmlTextInput input = (HtmlTextInput) page.getElementById("foo");
577
578
579 assertTrue(input.isValid());
580
581 input.setValue("foo");
582 assertFalse(input.isValid());
583
584 input.setValue("foobar");
585 assertTrue(input.isValid());
586 }
587
588
589
590
591
592 @Test
593 @Alerts({"true", "true", "true", "", "foo"})
594 public void maxLengthValidation() throws Exception {
595 final String htmlContent = DOCTYPE_HTML
596 + "<html>\n"
597 + "<head></head>\n"
598 + "<body>\n"
599 + "<form id='form1'>\n"
600 + " <input type='text' id='foo' maxLength='3'>\n"
601 + "</form>\n"
602 + "</body></html>";
603
604 final HtmlPage page = loadPage(htmlContent);
605
606 final HtmlInput input = (HtmlInput) page.getElementById("foo");
607 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
608 input.type("foo");
609 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
610 input.type("bar");
611 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
612 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
613 assertEquals(getExpectedAlerts()[4], input.getValue());
614 }
615
616
617
618
619
620 @Test
621 @Alerts({"true", "false", "true", "", "foobar"})
622 public void minLengthValidation() throws Exception {
623 final String htmlContent = DOCTYPE_HTML
624 + "<html>\n"
625 + "<head></head>\n"
626 + "<body>\n"
627 + "<form id='form1'>\n"
628 + " <input type='text' id='foo' minLength='4'>\n"
629 + "</form>\n"
630 + "</body></html>";
631
632 final HtmlPage page = loadPage(htmlContent);
633
634 final HtmlInput input = (HtmlInput) page.getElementById("foo");
635 assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
636 input.type("foo");
637 assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
638 input.type("bar");
639 assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
640 assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
641 assertEquals(getExpectedAlerts()[4], input.getValue());
642 }
643
644
645
646
647 @Test
648 public void clipboardCopy() throws Exception {
649 Assume.assumeFalse(SKIP_);
650
651 final String html = DOCTYPE_HTML
652 + "<html><head>\n"
653 + "</head>\n"
654 + "<body>\n"
655 + " <form>\n"
656 + " <input type='text' id='i1' value='aswjdfue'>\n"
657 + " </form>\n"
658 + " <button id='check' onclick='test()'>Test</button>\n"
659 + "</body></html>";
660
661 final ClipboardHandler clipboardHandler = new AwtClipboardHandler();
662 getWebClient().setClipboardHandler(clipboardHandler);
663
664 clipboardHandler.setClipboardContent("");
665 final HtmlPage page = loadPage(html);
666
667 final HtmlInput input = (HtmlInput) page.getElementById("i1");
668
669 final Keyboard kb = new Keyboard();
670 kb.press(KeyboardEvent.DOM_VK_CONTROL);
671 kb.type('a');
672 kb.type('c');
673 kb.release(KeyboardEvent.DOM_VK_CONTROL);
674 input.type(kb);
675
676 assertEquals("aswjdfue", clipboardHandler.getClipboardContent());
677 }
678
679
680
681
682 @Test
683 public void clipboardPaste() throws Exception {
684 Assume.assumeFalse(SKIP_);
685
686 final String html = DOCTYPE_HTML
687 + "<html><head>\n"
688 + "</head>\n"
689 + "<body>\n"
690 + " <form>\n"
691 + " <input type='text' id='i1'>\n"
692 + " </form>\n"
693 + " <button id='check' onclick='test()'>Test</button>\n"
694 + "</body></html>";
695
696 final ClipboardHandler clipboardHandler = new AwtClipboardHandler();
697 getWebClient().setClipboardHandler(clipboardHandler);
698
699 clipboardHandler.setClipboardContent("xyz");
700 final HtmlPage page = loadPage(html);
701
702 final HtmlInput input = (HtmlInput) page.getElementById("i1");
703
704 final Keyboard kb = new Keyboard();
705 kb.press(KeyboardEvent.DOM_VK_CONTROL);
706 kb.type('v');
707 kb.release(KeyboardEvent.DOM_VK_CONTROL);
708 input.type(kb);
709
710 assertEquals("xyz", input.getValue());
711 }
712
713
714
715
716 @Test
717 public void clipboardPasteFakeClipboard() throws Exception {
718 final String html = DOCTYPE_HTML
719 + "<html><head>\n"
720 + "</head>\n"
721 + "<body>\n"
722 + " <form>\n"
723 + " <input type='text' id='i1'>\n"
724 + " </form>\n"
725 + " <button id='check' onclick='test()'>Test</button>\n"
726 + "</body></html>";
727
728 final ClipboardHandler clipboardHandler = new ClipboardHandler() {
729 @Override
730 public String getClipboardContent() {
731 return "#dbbb";
732 }
733
734 @Override
735 public void setClipboardContent(final String string) {
736 }
737
738 };
739 getWebClient().setClipboardHandler(clipboardHandler);
740
741 clipboardHandler.setClipboardContent("xyz");
742 final HtmlPage page = loadPage(html);
743
744 final HtmlInput input = (HtmlInput) page.getElementById("i1");
745
746 final Keyboard kb = new Keyboard();
747 kb.press(KeyboardEvent.DOM_VK_CONTROL);
748 kb.type('v');
749 kb.release(KeyboardEvent.DOM_VK_CONTROL);
750 input.type(kb);
751
752 assertEquals("#dbbb", input.getValue());
753 }
754 }