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 TimeEvent}.
23   *
24   * @author Ronald Brill
25   */
26  public class TimeEventTest extends WebDriverTestCase {
27  
28      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
29              + "    log(event);\n"
30              + "    log(event.type);\n"
31              + "    log(event.bubbles);\n"
32              + "    log(event.cancelable);\n"
33              + "    log(event.composed);\n"
34              + "  }\n";
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts(DEFAULT = "ReferenceError",
41              FF = "TypeError",
42              FF_ESR = "TypeError")
43      public void create_ctor() throws Exception {
44          final String html = DOCTYPE_HTML
45              + "<html><head><script>\n"
46              + LOG_TITLE_FUNCTION
47              + "  function test() {\n"
48              + "    try {\n"
49              + "      var event = new TimeEvent('time');\n"
50              + "      dump(event);\n"
51              + "    } catch(e) { logEx(e) }\n"
52              + "  }\n"
53              + DUMP_EVENT_FUNCTION
54              + "</script></head><body onload='test()'>\n"
55              + "</body></html>";
56  
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts(DEFAULT = "ReferenceError",
65              FF = "TypeError",
66              FF_ESR = "TypeError")
67      public void create_ctorWithoutType() throws Exception {
68          final String html = DOCTYPE_HTML
69              + "<html><head><script>\n"
70              + LOG_TITLE_FUNCTION
71              + "  function test() {\n"
72              + "    try {\n"
73              + "      var event = new TimeEvent();\n"
74              + "      dump(event);\n"
75              + "    } catch(e) { logEx(e) }\n"
76              + "  }\n"
77              + DUMP_EVENT_FUNCTION
78              + "</script></head><body onload='test()'>\n"
79              + "</body></html>";
80  
81          loadPageVerifyTitle2(html);
82      }
83  
84      /**
85       * @throws Exception if the test fails
86       */
87      @Test
88      @Alerts(DEFAULT = "ReferenceError",
89              FF = "TypeError",
90              FF_ESR = "TypeError")
91      public void create_ctorNumericType() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html><head><script>\n"
94              + LOG_TITLE_FUNCTION
95              + "  function test() {\n"
96              + "    try {\n"
97              + "      var event = new TimeEvent(42);\n"
98              + "      dump(event);\n"
99              + "    } catch(e) { logEx(e) }\n"
100             + "  }\n"
101             + DUMP_EVENT_FUNCTION
102             + "</script></head><body onload='test()'>\n"
103             + "</body></html>";
104 
105         loadPageVerifyTitle2(html);
106     }
107 
108     /**
109      * @throws Exception if the test fails
110      */
111     @Test
112     @Alerts(DEFAULT = "ReferenceError",
113             FF = "TypeError",
114             FF_ESR = "TypeError")
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 TimeEvent(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 TimeEvent(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(DEFAULT = "ReferenceError",
159             FF = "TypeError",
160             FF_ESR = "TypeError")
161     public void create_ctorArbitraryType() throws Exception {
162         final String html = DOCTYPE_HTML
163             + "<html><head><script>\n"
164             + LOG_TITLE_FUNCTION
165             + "  function test() {\n"
166             + "    try {\n"
167             + "      var event = new TimeEvent('HtmlUnitEvent');\n"
168             + "      dump(event);\n"
169             + "    } catch(e) { logEx(e) }\n"
170             + "  }\n"
171             + DUMP_EVENT_FUNCTION
172             + "</script></head><body onload='test()'>\n"
173             + "</body></html>";
174 
175         loadPageVerifyTitle2(html);
176     }
177 
178     /**
179      * @throws Exception if the test fails
180      */
181     @Test
182     @Alerts(DEFAULT = "ReferenceError",
183             FF = "TypeError",
184             FF_ESR = "TypeError")
185     public void create_ctorAllDetails() throws Exception {
186         final String html = DOCTYPE_HTML
187             + "<html><head><script>\n"
188             + LOG_TITLE_FUNCTION
189             + "  function test() {\n"
190             + "    try {\n"
191             + "      var debug = {hello: 'world'};\n"
192             + "      var event = new TimeEvent('time', {\n"
193             + "        'detail': 7\n"
194             + "      });\n"
195             + "      dump(event);\n"
196             + "    } catch(e) { logEx(e) }\n"
197             + "  }\n"
198             + DUMP_EVENT_FUNCTION
199             + "</script></head><body onload='test()'>\n"
200             + "</body></html>";
201 
202         loadPageVerifyTitle2(html);
203     }
204 
205     /**
206      * @throws Exception if the test fails
207      */
208     @Test
209     @Alerts(DEFAULT = "ReferenceError",
210             FF = "TypeError",
211             FF_ESR = "TypeError")
212     public void create_ctorAllDetailsMissingData() throws Exception {
213         final String html = DOCTYPE_HTML
214             + "<html><head><script>\n"
215             + LOG_TITLE_FUNCTION
216             + "  function test() {\n"
217             + "    try {\n"
218             + "      var event = new TimeEvent('time', {\n"
219             + "      });\n"
220             + "      dump(event);\n"
221             + "    } catch(e) { logEx(e) }\n"
222             + "  }\n"
223             + DUMP_EVENT_FUNCTION
224             + "</script></head><body onload='test()'>\n"
225             + "</body></html>";
226 
227         loadPageVerifyTitle2(html);
228     }
229 
230     /**
231      * @throws Exception if the test fails
232      */
233     @Test
234     @Alerts(DEFAULT = "ReferenceError",
235             FF = "TypeError",
236             FF_ESR = "TypeError")
237     public void create_ctorAllDetailsWrongData() throws Exception {
238         final String html = DOCTYPE_HTML
239             + "<html><head><script>\n"
240             + LOG_TITLE_FUNCTION
241             + "  function test() {\n"
242             + "    try {\n"
243             + "      var event = new TimeEvent('time', {\n"
244             + "        'detail': 'ten'\n"
245             + "      });\n"
246             + "      dump(event);\n"
247             + "    } catch(e) { logEx(e) }\n"
248             + "  }\n"
249             + DUMP_EVENT_FUNCTION
250             + "</script></head><body onload='test()'>\n"
251             + "</body></html>";
252 
253         loadPageVerifyTitle2(html);
254     }
255 
256     /**
257      * @throws Exception if the test fails
258      */
259     @Test
260     @Alerts(DEFAULT = "true",
261             CHROME = "false",
262             EDGE = "false")
263     public void inWindow() throws Exception {
264         final String html = DOCTYPE_HTML
265             + "<html>\n"
266             + "<head>\n"
267             + "  <script>\n"
268             + LOG_TITLE_FUNCTION
269             + "    function test() {\n"
270             + "      log('TimeEvent' in window);\n"
271             + "    }\n"
272             + "  </script>\n"
273             + "</head>\n"
274             + "<body onload='test()'>\n"
275             + "</body>\n"
276             + "</html>";
277 
278         loadPageVerifyTitle2(html);
279     }
280 }