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