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(DEFAULT = {"[object PointerEvent]", "click", "false", "false", "false",
57                         "0", "1", "1", "0", "0", "0", "", "false", "1.5707963267948966", "0"},
58              FF_ESR = {"[object PointerEvent]", "click", "false", "false", "false",
59                        "0", "1", "1", "0", "0", "0", "", "false", "undefined", "undefined"})
60      public void create_ctor() throws Exception {
61          final String html = DOCTYPE_HTML
62              + "<html><head><script>\n"
63              + LOG_TITLE_FUNCTION
64              + "  function test() {\n"
65              + "    try {\n"
66              + "      var event = new PointerEvent('click');\n"
67              + "      dump(event);\n"
68              + "    } catch(e) { logEx(e) }\n"
69              + "  }\n"
70              + DUMP_EVENT_FUNCTION
71              + "</script></head><body onload='test()'>\n"
72              + "</body></html>";
73  
74          loadPageVerifyTitle2(html);
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts(DEFAULT = {"[object PointerEvent]", "click", "true", "false", "false",
82                         "2", "1", "1", "0", "0", "0", "mouse", "false", "1.5707963267948966", "0"},
83              FF_ESR = {"[object PointerEvent]", "click", "true", "false", "false",
84                        "2", "1", "1", "0", "0", "0", "mouse", "false", "undefined", "undefined"})
85      public void create_ctorWithDetails() throws Exception {
86          final String html = DOCTYPE_HTML
87              + "<html><head><script>\n"
88              + LOG_TITLE_FUNCTION
89              + "  function test() {\n"
90              + "    try {\n"
91              + "      var event = new PointerEvent('click', {\n"
92              + "        'bubbles': true,\n"
93              + "        'pointerId': 2,\n"
94              + "        'pointerType': 'mouse'\n"
95              + "      });\n"
96              + "      dump(event);\n"
97              + "    } catch(e) { logEx(e) }\n"
98              + "  }\n"
99              + DUMP_EVENT_FUNCTION
100             + "</script></head><body onload='test()'>\n"
101             + "</body></html>";
102 
103         loadPageVerifyTitle2(html);
104     }
105 
106     /**
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts("NotSupportedError/DOMException")
111     public void create_createEvent() throws Exception {
112         final String html = DOCTYPE_HTML
113             + "<html><head><script>\n"
114             + LOG_TITLE_FUNCTION
115             + "  function test() {\n"
116             + "    try {\n"
117             + "      var event = document.createEvent('PointerEvent');\n"
118             + "      dump(event);\n"
119             + "    } catch(e) { logEx(e) }\n"
120             + "  }\n"
121             + DUMP_EVENT_FUNCTION
122             + "</script></head><body onload='test()'>\n"
123             + "</body></html>";
124 
125         loadPageVerifyTitle2(html);
126     }
127 
128     /**
129      * @throws Exception if the test fails
130      */
131     @Test
132     @Alerts("NotSupportedError/DOMException")
133     public void initPointerEvent() throws Exception {
134         final String html = DOCTYPE_HTML
135             + "<html><head><script>\n"
136             + LOG_TITLE_FUNCTION
137             + "  function test() {\n"
138             + "    try {\n"
139             + "      var event = document.createEvent('PointerEvent');\n"
140             + "      event.initPointerEvent('click', true, false, window, 3, 10, 11, 12, 13, true, true, true, false, "
141             + "0, null, 14, 15, 4, 5, 6, 16, 17, 18, 123, 'mouse', 987, false);\n"
142             + "      dump(event);\n"
143             + "    } catch(e) { logEx(e) }\n"
144             + "  }\n"
145             + DUMP_EVENT_FUNCTION
146             + "</script></head><body onload='test()'>\n"
147             + "</body></html>";
148 
149         loadPageVerifyTitle2(html);
150     }
151 
152     /**
153      * @throws Exception if the test fails
154      */
155     @Test
156     @Alerts("NotSupportedError/DOMException")
157     public void dispatchEvent() throws Exception {
158         final String html = DOCTYPE_HTML
159             + "<html><head><script>\n"
160             + LOG_TITLE_FUNCTION
161             + "  function test() {\n"
162             + "    try {\n"
163             + "      var event = document.createEvent('PointerEvent');\n"
164             + "      event.initPointerEvent('click', true, false, window, 3, 10, 11, 12, 13, true, true, true, false, "
165             + "0, null, 14, 15, 4, 5, 6, 16, 17, 18, 123, 'mouse', 987, false);\n"
166             + "      dispatchEvent(event);\n"
167             + "    } catch(e) { logEx(e) }\n"
168             + "  }\n"
169             + DUMP_EVENT_FUNCTION
170             + "  try {\n"
171             + "    window.addEventListener('click',dump);\n"
172             + "  } catch(e) { }\n"
173             + "</script></head><body onload='test()'>\n"
174             + "</body></html>";
175 
176         loadPageVerifyTitle2(html);
177     }
178 }