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.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link PointerEvent}.
23   *
24   * @author Frank Danek
25   * @author Ronald Brill
26   */
27  public class PointerEventTest extends WebDriverTestCase {
28  
29      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
30          + "    if (event) {\n"
31          + "      log(event);\n"
32          + "      log(event.type);\n"
33          + "      log(event.bubbles);\n"
34          + "      log(event.cancelable);\n"
35          + "      log(event.composed);\n"
36  
37          + "      log(event.pointerId);\n"
38          + "      log(event.width);\n"
39          + "      log(event.height);\n"
40          + "      log(event.pressure);\n"
41          + "      log(event.tiltX);\n"
42          + "      log(event.tiltY);\n"
43          + "      log(event.pointerType);\n"
44          + "      log(event.isPrimary);\n"
45          + "      log(event.altitudeAngle);\n"
46          + "      log(event.azimuthAngle);\n"
47          + "    } else {\n"
48          + "      log('no event');\n"
49          + "    }\n"
50          + "  }\n";
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts({"[object PointerEvent]", "click", "false", "false", "false",
57               "0", "1", "1", "0", "0", "0", "", "false", "1.5707963267948966", "0"})
58      public void create_ctor() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><head><script>\n"
61              + LOG_TITLE_FUNCTION
62              + "  function test() {\n"
63              + "    try {\n"
64              + "      var event = new PointerEvent('click');\n"
65              + "      dump(event);\n"
66              + "    } catch(e) { logEx(e) }\n"
67              + "  }\n"
68              + DUMP_EVENT_FUNCTION
69              + "</script></head><body onload='test()'>\n"
70              + "</body></html>";
71  
72          loadPageVerifyTitle2(html);
73      }
74  
75      /**
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts({"[object PointerEvent]", "click", "true", "false", "false",
80               "2", "1", "1", "0", "0", "0", "mouse", "false", "1.5707963267948966", "0"})
81      public void create_ctorWithDetails() throws Exception {
82          final String html = DOCTYPE_HTML
83              + "<html><head><script>\n"
84              + LOG_TITLE_FUNCTION
85              + "  function test() {\n"
86              + "    try {\n"
87              + "      var event = new PointerEvent('click', {\n"
88              + "        'bubbles': true,\n"
89              + "        'pointerId': 2,\n"
90              + "        'pointerType': 'mouse'\n"
91              + "      });\n"
92              + "      dump(event);\n"
93              + "    } catch(e) { logEx(e) }\n"
94              + "  }\n"
95              + DUMP_EVENT_FUNCTION
96              + "</script></head><body onload='test()'>\n"
97              + "</body></html>";
98  
99          loadPageVerifyTitle2(html);
100     }
101 
102     /**
103      * @throws Exception if the test fails
104      */
105     @Test
106     @Alerts("NotSupportedError/DOMException")
107     public void create_createEvent() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html><head><script>\n"
110             + LOG_TITLE_FUNCTION
111             + "  function test() {\n"
112             + "    try {\n"
113             + "      var event = document.createEvent('PointerEvent');\n"
114             + "      dump(event);\n"
115             + "    } catch(e) { logEx(e) }\n"
116             + "  }\n"
117             + DUMP_EVENT_FUNCTION
118             + "</script></head><body onload='test()'>\n"
119             + "</body></html>";
120 
121         loadPageVerifyTitle2(html);
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     @Alerts("NotSupportedError/DOMException")
129     public void initPointerEvent() throws Exception {
130         final String html = DOCTYPE_HTML
131             + "<html><head><script>\n"
132             + LOG_TITLE_FUNCTION
133             + "  function test() {\n"
134             + "    try {\n"
135             + "      var event = document.createEvent('PointerEvent');\n"
136             + "      event.initPointerEvent('click', true, false, window, 3, 10, 11, 12, 13, true, true, true, false, "
137             + "0, null, 14, 15, 4, 5, 6, 16, 17, 18, 123, 'mouse', 987, false);\n"
138             + "      dump(event);\n"
139             + "    } catch(e) { logEx(e) }\n"
140             + "  }\n"
141             + DUMP_EVENT_FUNCTION
142             + "</script></head><body onload='test()'>\n"
143             + "</body></html>";
144 
145         loadPageVerifyTitle2(html);
146     }
147 
148     /**
149      * @throws Exception if the test fails
150      */
151     @Test
152     @Alerts("NotSupportedError/DOMException")
153     public void dispatchEvent() throws Exception {
154         final String html = DOCTYPE_HTML
155             + "<html><head><script>\n"
156             + LOG_TITLE_FUNCTION
157             + "  function test() {\n"
158             + "    try {\n"
159             + "      var event = document.createEvent('PointerEvent');\n"
160             + "      event.initPointerEvent('click', true, false, window, 3, 10, 11, 12, 13, true, true, true, false, "
161             + "0, null, 14, 15, 4, 5, 6, 16, 17, 18, 123, 'mouse', 987, false);\n"
162             + "      dispatchEvent(event);\n"
163             + "    } catch(e) { logEx(e) }\n"
164             + "  }\n"
165             + DUMP_EVENT_FUNCTION
166             + "  try {\n"
167             + "    window.addEventListener('click',dump);\n"
168             + "  } catch(e) { }\n"
169             + "</script></head><body onload='test()'>\n"
170             + "</body></html>";
171 
172         loadPageVerifyTitle2(html);
173     }
174 }