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.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link TimeEvent}.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class TimeEventTest extends WebDriverTestCase {
30  
31      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
32              + "    log(event);\n"
33              + "    log(event.type);\n"
34              + "    log(event.bubbles);\n"
35              + "    log(event.cancelable);\n"
36              + "    log(event.composed);\n"
37              + "  }\n";
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts(DEFAULT = "ReferenceError",
44              FF = "TypeError",
45              FF_ESR = "TypeError")
46      public void create_ctor() throws Exception {
47          final String html = DOCTYPE_HTML
48              + "<html><head><script>\n"
49              + LOG_TITLE_FUNCTION
50              + "  function test() {\n"
51              + "    try {\n"
52              + "      var event = new TimeEvent('time');\n"
53              + "      dump(event);\n"
54              + "    } catch(e) { logEx(e) }\n"
55              + "  }\n"
56              + DUMP_EVENT_FUNCTION
57              + "</script></head><body onload='test()'>\n"
58              + "</body></html>";
59  
60          loadPageVerifyTitle2(html);
61      }
62  
63      /**
64       * @throws Exception if the test fails
65       */
66      @Test
67      @Alerts(DEFAULT = "ReferenceError",
68              FF = "TypeError",
69              FF_ESR = "TypeError")
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 TimeEvent();\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 = "ReferenceError",
92              FF = "TypeError",
93              FF_ESR = "TypeError")
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 TimeEvent(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 = "ReferenceError",
116             FF = "TypeError",
117             FF_ESR = "TypeError")
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 TimeEvent(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 TimeEvent(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 = "ReferenceError",
162             FF = "TypeError",
163             FF_ESR = "TypeError")
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 TimeEvent('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 = "ReferenceError",
186             FF = "TypeError",
187             FF_ESR = "TypeError")
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 TimeEvent('time', {\n"
196             + "        'detail': 7\n"
197             + "      });\n"
198             + "      dump(event);\n"
199             + "    } catch(e) { logEx(e) }\n"
200             + "  }\n"
201             + DUMP_EVENT_FUNCTION
202             + "</script></head><body onload='test()'>\n"
203             + "</body></html>";
204 
205         loadPageVerifyTitle2(html);
206     }
207 
208     /**
209      * @throws Exception if the test fails
210      */
211     @Test
212     @Alerts(DEFAULT = "ReferenceError",
213             FF = "TypeError",
214             FF_ESR = "TypeError")
215     public void create_ctorAllDetailsMissingData() throws Exception {
216         final String html = DOCTYPE_HTML
217             + "<html><head><script>\n"
218             + LOG_TITLE_FUNCTION
219             + "  function test() {\n"
220             + "    try {\n"
221             + "      var event = new TimeEvent('time', {\n"
222             + "      });\n"
223             + "      dump(event);\n"
224             + "    } catch(e) { logEx(e) }\n"
225             + "  }\n"
226             + DUMP_EVENT_FUNCTION
227             + "</script></head><body onload='test()'>\n"
228             + "</body></html>";
229 
230         loadPageVerifyTitle2(html);
231     }
232 
233     /**
234      * @throws Exception if the test fails
235      */
236     @Test
237     @Alerts(DEFAULT = "ReferenceError",
238             FF = "TypeError",
239             FF_ESR = "TypeError")
240     public void create_ctorAllDetailsWrongData() throws Exception {
241         final String html = DOCTYPE_HTML
242             + "<html><head><script>\n"
243             + LOG_TITLE_FUNCTION
244             + "  function test() {\n"
245             + "    try {\n"
246             + "      var event = new TimeEvent('time', {\n"
247             + "        'detail': 'ten'\n"
248             + "      });\n"
249             + "      dump(event);\n"
250             + "    } catch(e) { logEx(e) }\n"
251             + "  }\n"
252             + DUMP_EVENT_FUNCTION
253             + "</script></head><body onload='test()'>\n"
254             + "</body></html>";
255 
256         loadPageVerifyTitle2(html);
257     }
258 
259     /**
260      * @throws Exception if the test fails
261      */
262     @Test
263     @Alerts(DEFAULT = "true",
264             CHROME = "false",
265             EDGE = "false")
266     public void inWindow() throws Exception {
267         final String html = DOCTYPE_HTML
268             + "<html>\n"
269             + "<head>\n"
270             + "  <script>\n"
271             + LOG_TITLE_FUNCTION
272             + "    function test() {\n"
273             + "      log('TimeEvent' in window);\n"
274             + "    }\n"
275             + "  </script>\n"
276             + "</head>\n"
277             + "<body onload='test()'>\n"
278             + "</body>\n"
279             + "</html>";
280 
281         loadPageVerifyTitle2(html);
282     }
283 }