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.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link TouchEvent}.
24   *
25   * @author Ronald Brill
26   */
27  public class TouchEventTest extends WebDriverTestCase {
28  
29      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
30              + "    log(event);\n"
31              + "    log(event.type);\n"
32              + "    log(event.bubbles);\n"
33              + "    log(event.cancelable);\n"
34              + "    log(event.composed);\n"
35              + "  }\n";
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts(DEFAULT = {"[object TouchEvent]", "touch", "false", "false", "false"},
42              FF = "ReferenceError",
43              FF_ESR = "ReferenceError")
44      public void create_ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    try {\n"
50              + "      var event = new TouchEvent('touch');\n"
51              + "      dump(event);\n"
52              + "    } catch(e) { logEx(e) }\n"
53              + "  }\n"
54              + DUMP_EVENT_FUNCTION
55              + "</script></head><body onload='test()'>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts(DEFAULT = "TypeError",
66              FF = "ReferenceError",
67              FF_ESR = "ReferenceError")
68      @HtmlUnitNYI(CHROME = {"[object TouchEvent]", "undefined", "false", "false", "false"},
69                  EDGE = {"[object TouchEvent]", "undefined", "false", "false", "false"})
70      public void create_ctorWithoutType() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><head><script>\n"
73              + LOG_TITLE_FUNCTION
74              + "  function test() {\n"
75              + "    try {\n"
76              + "      var event = new TouchEvent();\n"
77              + "      dump(event);\n"
78              + "    } catch(e) { logEx(e) }\n"
79              + "  }\n"
80              + DUMP_EVENT_FUNCTION
81              + "</script></head><body onload='test()'>\n"
82              + "</body></html>";
83  
84          loadPageVerifyTitle2(html);
85      }
86  
87      /**
88       * @throws Exception if the test fails
89       */
90      @Test
91      @Alerts(DEFAULT = {"[object TouchEvent]", "42", "false", "false", "false"},
92              FF = "ReferenceError",
93              FF_ESR = "ReferenceError")
94      public void create_ctorNumericType() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html><head><script>\n"
97              + LOG_TITLE_FUNCTION
98              + "  function test() {\n"
99              + "    try {\n"
100             + "      var event = new TouchEvent(42);\n"
101             + "      dump(event);\n"
102             + "    } catch(e) { logEx(e) }\n"
103             + "  }\n"
104             + DUMP_EVENT_FUNCTION
105             + "</script></head><body onload='test()'>\n"
106             + "</body></html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception if the test fails
113      */
114     @Test
115     @Alerts(DEFAULT = {"[object TouchEvent]", "null", "false", "false", "false"},
116             FF = "ReferenceError",
117             FF_ESR = "ReferenceError")
118     public void create_ctorNullType() throws Exception {
119         final String html = DOCTYPE_HTML
120             + "<html><head><script>\n"
121             + LOG_TITLE_FUNCTION
122             + "  function test() {\n"
123             + "    try {\n"
124             + "      var event = new TouchEvent(null);\n"
125             + "      dump(event);\n"
126             + "    } catch(e) { logEx(e) }\n"
127             + "  }\n"
128             + DUMP_EVENT_FUNCTION
129             + "</script></head><body onload='test()'>\n"
130             + "</body></html>";
131 
132         loadPageVerifyTitle2(html);
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts("ReferenceError")
140     public void create_ctorUnknownType() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html><head><script>\n"
143             + LOG_TITLE_FUNCTION
144             + "  function test() {\n"
145             + "    try {\n"
146             + "      var event = new TouchEvent(unknown);\n"
147             + "      dump(event);\n"
148             + "    } catch(e) { logEx(e) }\n"
149             + "  }\n"
150             + DUMP_EVENT_FUNCTION
151             + "</script></head><body onload='test()'>\n"
152             + "</body></html>";
153 
154         loadPageVerifyTitle2(html);
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts(DEFAULT = {"[object TouchEvent]", "HtmlUnitEvent", "false", "false", "false"},
162             FF = "ReferenceError",
163             FF_ESR = "ReferenceError")
164     public void create_ctorArbitraryType() throws Exception {
165         final String html = DOCTYPE_HTML
166             + "<html><head><script>\n"
167             + LOG_TITLE_FUNCTION
168             + "  function test() {\n"
169             + "    try {\n"
170             + "      var event = new TouchEvent('HtmlUnitEvent');\n"
171             + "      dump(event);\n"
172             + "    } catch(e) { logEx(e) }\n"
173             + "  }\n"
174             + DUMP_EVENT_FUNCTION
175             + "</script></head><body onload='test()'>\n"
176             + "</body></html>";
177 
178         loadPageVerifyTitle2(html);
179     }
180 
181     /**
182      * @throws Exception if the test fails
183      */
184     @Test
185     @Alerts(DEFAULT = {"[object TouchEvent]", "touch", "false", "false", "false"},
186             FF = "ReferenceError",
187             FF_ESR = "ReferenceError")
188     public void create_ctorAllDetails() throws Exception {
189         final String html = DOCTYPE_HTML
190             + "<html><head><script>\n"
191             + LOG_TITLE_FUNCTION
192             + "  function test() {\n"
193             + "    try {\n"
194             + "      var debug = {hello: 'world'};\n"
195             + "      var event = new TouchEvent('touch', {\n"
196             + "        'touches': [],\n"
197             + "        'targetTouches': [],\n"
198             + "        'changedTouches': [],\n"
199             + "        'ctrlKey': true,\n"
200             + "        'shiftKey': true,\n"
201             + "        'altKey': true,\n"
202             + "        'metaKey': true\n"
203             + "      });\n"
204             + "      dump(event);\n"
205             + "    } catch(e) { logEx(e) }\n"
206             + "  }\n"
207             + DUMP_EVENT_FUNCTION
208             + "</script></head><body onload='test()'>\n"
209             + "</body></html>";
210 
211         loadPageVerifyTitle2(html);
212     }
213 
214     /**
215      * @throws Exception if the test fails
216      */
217     @Test
218     @Alerts(DEFAULT = {"[object TouchEvent]", "touch", "false", "false", "false"},
219             FF = "ReferenceError",
220             FF_ESR = "ReferenceError")
221     public void create_ctorAllDetailsMissingData() throws Exception {
222         final String html = DOCTYPE_HTML
223             + "<html><head><script>\n"
224             + LOG_TITLE_FUNCTION
225             + "  function test() {\n"
226             + "    try {\n"
227             + "      var event = new TouchEvent('touch', {\n"
228             + "      });\n"
229             + "      dump(event);\n"
230             + "    } catch(e) { logEx(e) }\n"
231             + "  }\n"
232             + DUMP_EVENT_FUNCTION
233             + "</script></head><body onload='test()'>\n"
234             + "</body></html>";
235 
236         loadPageVerifyTitle2(html);
237     }
238 
239     /**
240      * @throws Exception if the test fails
241      */
242     @Test
243     @Alerts(DEFAULT = "TypeError",
244             FF = "ReferenceError",
245             FF_ESR = "ReferenceError")
246     @HtmlUnitNYI(CHROME = {"[object TouchEvent]", "touch", "false", "false", "false"},
247                 EDGE = {"[object TouchEvent]", "touch", "false", "false", "false"})
248     public void create_ctorAllDetailsWrongData() throws Exception {
249         final String html = DOCTYPE_HTML
250             + "<html><head><script>\n"
251             + LOG_TITLE_FUNCTION
252             + "  function test() {\n"
253             + "    try {\n"
254             + "      var event = new TouchEvent('touch', {\n"
255             + "        'touches': 'abc',\n"
256             + "      });\n"
257             + "      dump(event);\n"
258             + "    } catch(e) { logEx(e) }\n"
259             + "  }\n"
260             + DUMP_EVENT_FUNCTION
261             + "</script></head><body onload='test()'>\n"
262             + "</body></html>";
263 
264         loadPageVerifyTitle2(html);
265     }
266 
267     /**
268      * @throws Exception if the test fails
269      */
270     @Test
271     @Alerts(DEFAULT = "true",
272             FF = "false",
273             FF_ESR = "false")
274     public void inWindow() throws Exception {
275         final String html = DOCTYPE_HTML
276             + "<html>\n"
277             + "<head>\n"
278             + "  <script>\n"
279             + LOG_TITLE_FUNCTION
280             + "    function test() {\n"
281             + "      log('TouchEvent' in window);\n"
282             + "    }\n"
283             + "  </script>\n"
284             + "</head>\n"
285             + "<body onload='test()'>\n"
286             + "</body>\n"
287             + "</html>";
288 
289         loadPageVerifyTitle2(html);
290     }
291 }