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