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