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