1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.event;
16
17 import org.htmlunit.SimpleWebTestCase;
18 import org.htmlunit.html.HtmlPage;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.Test;
22
23
24
25
26
27
28
29
30
31
32 public class KeyboardEvent2Test extends SimpleWebTestCase {
33
34
35
36
37
38
39 @Test
40 @Alerts("keydown:65,0,65 keypress:97,97,97 keyup:65,0,65")
41 public void a() throws Exception {
42 final HtmlPage page = getHtmlPage();
43 page.getDocumentElement().type('a');
44 final String log = page.getHtmlElementById("log").asNormalizedText();
45 assertTrue(log, log.contains(getExpectedAlerts()[0]));
46 }
47
48 private HtmlPage getHtmlPage() throws Exception {
49 final String html = DOCTYPE_HTML
50 + "<html><head><title>foo</title><script>\n"
51 + " function test() {\n"
52 + " document.onkeydown = checkEvent;\n"
53 + " document.onkeyup = checkEvent;\n"
54 + " document.onkeypress = checkEvent;\n"
55 + " }\n"
56 + "\n"
57 + " function checkEvent(e) {\n"
58 + " if (!e) var e = window.event;\n"
59 + " var string = e.keyCode + ',' + e.charCode + ',' + e.which;\n"
60 + " log(e.type + ':' + string + ' ');\n"
61 + " if (e.type == 'keyup') {\n"
62 + " log('\\n');\n"
63 + " }\n"
64 + " }\n"
65 + "\n"
66 + " function log(msg) {\n"
67 + " document.getElementById('log').value += msg;\n"
68 + " }\n"
69 + "</script></head><body onload='test()'>\n"
70 + " <textarea id='log' rows=40 cols=80></textarea>\n"
71 + "</body></html>";
72 return loadPage(html);
73 }
74
75
76
77
78
79 @Test
80 public void dom_vk_a() throws Exception {
81 final HtmlPage page = getHtmlPage();
82 Assertions.assertThrows(IllegalArgumentException.class,
83 () -> page.getDocumentElement().type(KeyboardEvent.DOM_VK_A));
84 }
85
86
87
88
89
90 @Test
91 @Alerts(DEFAULT = "keydown:113,0,113 keyup:113,0,113",
92 FF = "keydown:113,0,113 keypress:113,0,0 keyup:113,0,113",
93 FF_ESR = "keydown:113,0,113 keypress:113,0,0 keyup:113,0,113")
94 public void dom_vk_f2() throws Exception {
95 final HtmlPage page = getHtmlPage();
96 page.getDocumentElement().type(KeyboardEvent.DOM_VK_F2);
97 final String log = page.getHtmlElementById("log").asNormalizedText();
98 assertTrue(log, log.contains(getExpectedAlerts()[0]));
99 }
100
101
102
103
104
105 @Test
106 @Alerts(DEFAULT = "keydown:39,0,39 keyup:39,0,39",
107 FF = "keydown:39,0,39 keypress:39,0,0 keyup:39,0,39",
108 FF_ESR = "keydown:39,0,39 keypress:39,0,0 keyup:39,0,39")
109 public void dom_vk_right() throws Exception {
110 final HtmlPage page = getHtmlPage();
111 page.getDocumentElement().type(KeyboardEvent.DOM_VK_RIGHT);
112 final String log = page.getHtmlElementById("log").asNormalizedText();
113 assertTrue(log, log.contains(getExpectedAlerts()[0]));
114 }
115 }