View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link KeyboardEvent}.
25   *
26   * Note that special key seems to have '0' {@code .which} with FF.
27   * And no keypress event in IE.
28   *
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   */
32  public class KeyboardEvent2Test extends SimpleWebTestCase {
33  
34      /**
35       * Test .type('a').
36       *
37       * @throws Exception if the test fails
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       * Test .type(KeyboardEvent.DOM_VK_A).
77       * @throws Exception if the test fails
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       * Test .type(KeyboardEvent.DOM_VK_F2).
88       * @throws Exception if the test fails
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      * Test .type(KeyboardEvent.DOM_VK_RIGHT).
103      * @throws Exception if the test fails
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 }