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.BrowserRunner;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link KeyboardEvent}.
26   *
27   * Note that special key seems to have '0' {@code .which} with FF.
28   * And no keypress event in IE.
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   */
33  @RunWith(BrowserRunner.class)
34  public class KeyboardEvent2Test extends SimpleWebTestCase {
35  
36      /**
37       * Test .type('a').
38       *
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts("keydown:65,0,65 keypress:97,97,97 keyup:65,0,65")
43      public void a() throws Exception {
44          final HtmlPage page = getHtmlPage();
45          page.getDocumentElement().type('a');
46          final String log = page.getHtmlElementById("log").asNormalizedText();
47          assertTrue(log, log.contains(getExpectedAlerts()[0]));
48      }
49  
50      private HtmlPage getHtmlPage() throws Exception {
51          final String html = DOCTYPE_HTML
52                  + "<html><head><title>foo</title><script>\n"
53                  + "  function test() {\n"
54                  + "    document.onkeydown = checkEvent;\n"
55                  + "    document.onkeyup = checkEvent;\n"
56                  + "    document.onkeypress = checkEvent;\n"
57                  + "  }\n"
58                  + "\n"
59                  + "  function checkEvent(e) {\n"
60                  + "    if (!e) var e = window.event;\n"
61                  + "    var string = e.keyCode + ',' + e.charCode + ',' + e.which;\n"
62                  + "    log(e.type + ':' + string + ' ');\n"
63                  + "    if (e.type == 'keyup') {\n"
64                  + "      log('\\n');\n"
65                  + "    }\n"
66                  + "  }\n"
67                  + "\n"
68                  + "  function log(msg) {\n"
69                  + "    document.getElementById('log').value += msg;\n"
70                  + "  }\n"
71                  + "</script></head><body onload='test()'>\n"
72                  + "  <textarea id='log' rows=40 cols=80></textarea>\n"
73                  + "</body></html>";
74          return loadPage(html);
75      }
76  
77      /**
78       * Test .type(KeyboardEvent.DOM_VK_A).
79       * @throws Exception if the test fails
80       */
81      @Test(expected = IllegalArgumentException.class)
82      public void dom_vk_a() throws Exception {
83          final HtmlPage page = getHtmlPage();
84          page.getDocumentElement().type(KeyboardEvent.DOM_VK_A);
85      }
86  
87      /**
88       * Test .type(KeyboardEvent.DOM_VK_F2).
89       * @throws Exception if the test fails
90       */
91      @Test
92      @Alerts(DEFAULT = "keydown:113,0,113 keyup:113,0,113",
93              FF = "keydown:113,0,113 keypress:113,0,0 keyup:113,0,113",
94              FF_ESR = "keydown:113,0,113 keypress:113,0,0 keyup:113,0,113")
95      public void dom_vk_f2() throws Exception {
96          final HtmlPage page = getHtmlPage();
97          page.getDocumentElement().type(KeyboardEvent.DOM_VK_F2);
98          final String log = page.getHtmlElementById("log").asNormalizedText();
99          assertTrue(log, log.contains(getExpectedAlerts()[0]));
100     }
101 
102     /**
103      * Test .type(KeyboardEvent.DOM_VK_RIGHT).
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts(DEFAULT = "keydown:39,0,39 keyup:39,0,39",
108             FF = "keydown:39,0,39 keypress:39,0,0 keyup:39,0,39",
109             FF_ESR = "keydown:39,0,39 keypress:39,0,0 keyup:39,0,39")
110     public void dom_vk_right() throws Exception {
111         final HtmlPage page = getHtmlPage();
112         page.getDocumentElement().type(KeyboardEvent.DOM_VK_RIGHT);
113         final String log = page.getHtmlElementById("log").asNormalizedText();
114         assertTrue(log, log.contains(getExpectedAlerts()[0]));
115     }
116 }