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 TransitionEvent}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class TransitionEventTest 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 TransitionEvent]", "transition", "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 TransitionEvent('transition');\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 TransitionEvent]", "undefined", "false", "false", "false"},
68                  EDGE = {"[object TransitionEvent]", "undefined", "false", "false", "false"},
69                  FF = {"[object TransitionEvent]", "undefined", "false", "false", "false"},
70                  FF_ESR = {"[object TransitionEvent]", "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 TransitionEvent();\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 TransitionEvent]", "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 TransitionEvent(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 TransitionEvent]", "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 TransitionEvent(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 TransitionEvent(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 TransitionEvent]", "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 TransitionEvent('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 TransitionEvent]", "transition", "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 TransitionEvent('transition', {\n"
189             + "        'propertyName': 'aPropertyName',\n"
190             + "        'elapsedTime': 0.123,\n"
191             + "        'pseudoElement': 'aPseudoElementName'\n"
192             + "      });\n"
193             + "      dump(event);\n"
194             + "    } catch(e) { logEx(e) }\n"
195             + "  }\n"
196             + DUMP_EVENT_FUNCTION
197             + "</script></head><body onload='test()'>\n"
198             + "</body></html>";
199 
200         loadPageVerifyTitle2(html);
201     }
202 
203     /**
204      * @throws Exception if the test fails
205      */
206     @Test
207     @Alerts({"[object TransitionEvent]", "transition", "false", "false", "false"})
208     public void create_ctorAllDetailsMissingData() throws Exception {
209         final String html = DOCTYPE_HTML
210             + "<html><head><script>\n"
211             + LOG_TITLE_FUNCTION
212             + "  function test() {\n"
213             + "    try {\n"
214             + "      var event = new TransitionEvent('transition', {\n"
215             + "      });\n"
216             + "      dump(event);\n"
217             + "    } catch(e) { logEx(e) }\n"
218             + "  }\n"
219             + DUMP_EVENT_FUNCTION
220             + "</script></head><body onload='test()'>\n"
221             + "</body></html>";
222 
223         loadPageVerifyTitle2(html);
224     }
225 
226     /**
227      * @throws Exception if the test fails
228      */
229     @Test
230     @Alerts("TypeError")
231     @HtmlUnitNYI(CHROME = {"[object TransitionEvent]", "transition", "false", "false", "false"},
232                 EDGE = {"[object TransitionEvent]", "transition", "false", "false", "false"},
233                 FF = {"[object TransitionEvent]", "transition", "false", "false", "false"},
234                 FF_ESR = {"[object TransitionEvent]", "transition", "false", "false", "false"})
235     public void create_ctorAllDetailsWrongData() throws Exception {
236         final String html = DOCTYPE_HTML
237             + "<html><head><script>\n"
238             + LOG_TITLE_FUNCTION
239             + "  function test() {\n"
240             + "    try {\n"
241             + "      var event = new TransitionEvent('transition', {\n"
242             + "        'elapsedTime': 'ten'\n"
243             + "      });\n"
244             + "      dump(event);\n"
245             + "    } catch(e) { logEx(e) }\n"
246             + "  }\n"
247             + DUMP_EVENT_FUNCTION
248             + "</script></head><body onload='test()'>\n"
249             + "</body></html>";
250 
251         loadPageVerifyTitle2(html);
252     }
253 
254     /**
255      * @throws Exception if the test fails
256      */
257     @Test
258     @Alerts("true")
259     public void inWindow() throws Exception {
260         final String html = DOCTYPE_HTML
261             + "<html>\n"
262             + "<head>\n"
263             + "  <script>\n"
264             + LOG_TITLE_FUNCTION
265             + "    function test() {\n"
266             + "      log('TransitionEvent' in window);\n"
267             + "    }\n"
268             + "  </script>\n"
269             + "</head>\n"
270             + "<body onload='test()'>\n"
271             + "</body>\n"
272             + "</html>";
273 
274         loadPageVerifyTitle2(html);
275     }
276 }