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