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 PopStateEvent}.
24   *
25   * @author Ronald Brill
26   */
27  public class PopStateEventTest extends WebDriverTestCase {
28  
29      private static final String DUMP_EVENT_FUNCTION =
30            "  function dump(event) {\n"
31          + "    if (event) {\n"
32          + "      log(event);\n"
33          + "      log(event.target);\n"
34          + "      log(event.type);\n"
35          + "      log(event.bubbles);\n"
36          + "      log(event.cancelable);\n"
37          + "      log(event.composed);\n"
38  
39          + "      log(event.state);\n"
40          + "    } else {\n"
41          + "      log('no event');\n"
42          + "    }\n"
43          + "  }\n";
44  
45      /**
46       * @throws Exception if the test fails
47       */
48      @Test
49      @Alerts({"[object PopStateEvent]", "null", "popstate", "false", "false", "false", "null"})
50      public void create_ctor() throws Exception {
51          final String html = DOCTYPE_HTML
52              + "<html><head><script>\n"
53              + LOG_TITLE_FUNCTION
54              + "  function test() {\n"
55              + "    try {\n"
56              + "      var event = new PopStateEvent('popstate');\n"
57              + "      dump(event);\n"
58              + "    } catch(e) { logEx(e) }\n"
59              + "  }\n"
60              + DUMP_EVENT_FUNCTION
61              + "</script></head><body onload='test()'>\n"
62              + "</body></html>";
63  
64          loadPageVerifyTitle2(html);
65      }
66  
67      /**
68       * @throws Exception if the test fails
69       */
70      @Test
71      @Alerts({"[object PopStateEvent]", "null", "popstate", "true", "false", "false", "2"})
72      public void create_ctorWithDetails() throws Exception {
73          final String html = DOCTYPE_HTML
74              + "<html><head><script>\n"
75              + LOG_TITLE_FUNCTION
76              + "  function test() {\n"
77              + "    try {\n"
78              + "      var event = new PopStateEvent('popstate', {\n"
79              + "        'bubbles': true,\n"
80              + "        'state': 2,\n"
81              + "      });\n"
82              + "      dump(event);\n"
83              + "    } catch(e) { logEx(e) }\n"
84              + "  }\n"
85              + DUMP_EVENT_FUNCTION
86              + "</script></head><body onload='test()'>\n"
87              + "</body></html>";
88  
89          loadPageVerifyTitle2(html);
90      }
91  
92      /**
93       * @throws Exception if the test fails
94       */
95      @Test
96      @Alerts(DEFAULT = {"[object PopStateEvent]", "null", "", "false", "false", "false", "null"},
97              FF = "NotSupportedError/DOMException",
98              FF_ESR = "NotSupportedError/DOMException")
99      public void create_createEvent() throws Exception {
100         final String html = DOCTYPE_HTML
101             + "<html><head><script>\n"
102             + LOG_TITLE_FUNCTION
103             + "  function test() {\n"
104             + "    try {\n"
105             + "      var event = document.createEvent('PopStateEvent');\n"
106             + "      dump(event);\n"
107             + "    } catch(e) { logEx(e) }\n"
108             + "  }\n"
109             + DUMP_EVENT_FUNCTION
110             + "</script></head><body onload='test()'>\n"
111             + "</body></html>";
112 
113         loadPageVerifyTitle2(html);
114     }
115 
116     /**
117      * @throws Exception if the test fails
118      */
119     @Test
120     @Alerts(DEFAULT = {"[object PopStateEvent]", "null", "", "false", "false", "false", "null"},
121             FF = "NotSupportedError/DOMException",
122             FF_ESR = "NotSupportedError/DOMException")
123     public void setState() throws Exception {
124         final String html = DOCTYPE_HTML
125             + "<html><head><script>\n"
126             + LOG_TITLE_FUNCTION
127             + "  function test() {\n"
128             + "    try {\n"
129             + "      var event = document.createEvent('PopStateEvent');\n"
130             + "      event.state = 'test';\n"
131             + "      dump(event);\n"
132             + "    } catch(e) { logEx(e) }\n"
133             + "  }\n"
134             + DUMP_EVENT_FUNCTION
135             + "</script></head><body onload='test()'>\n"
136             + "</body></html>";
137 
138         loadPageVerifyTitle2(html);
139     }
140 
141     /**
142      * @throws Exception if the test fails
143      */
144     @Test
145     @Alerts(DEFAULT = "dispatched",
146             FF = "exception ctor",
147             FF_ESR = "exception ctor")
148     public void dispatchEvent() throws Exception {
149         final String html = DOCTYPE_HTML
150             + "<html><head><script>\n"
151             + LOG_TITLE_FUNCTION
152             + "  function test() {\n"
153             + "    try {\n"
154             + "      var event = document.createEvent('PopStateEvent');\n"
155             + "      event.initEvent('', true, true);\n"
156             + "    } catch(e) { log('exception ctor'); return; }\n"
157 
158             + "    try {\n"
159             + "      dispatchEvent(event);\n"
160             + "      log('dispatched');\n"
161             + "    } catch(e) { log('exception' + e) }\n"
162             + "  }\n"
163             + DUMP_EVENT_FUNCTION
164             + "  try {\n"
165             + "    window.addEventListener('popstate',dump);\n"
166             + "  } catch(e) { }\n"
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(DEFAULT = "InvalidStateError/DOMException",
178             FF = "ctor NotSupportedError",
179             FF_ESR = "ctor NotSupportedError")
180     @HtmlUnitNYI(CHROME = "dispatched",
181             EDGE = "dispatched")
182     public void dispatchEventWithoutInit() throws Exception {
183         final String html = DOCTYPE_HTML
184             + "<html><head><script>\n"
185             + LOG_TITLE_FUNCTION
186             + "  function test() {\n"
187             + "    try {\n"
188             + "      var event = document.createEvent('PopStateEvent');\n"
189             + "    } catch(e) { log('ctor ' + e.name); return; }\n"
190 
191             + "    try {\n"
192             + "      dispatchEvent(event);\n"
193             + "      log('dispatched');\n"
194             + "    } catch(e) { logEx(e) }\n"
195             + "  }\n"
196             + DUMP_EVENT_FUNCTION
197             + "  try {\n"
198             + "    window.addEventListener('popstate',dump);\n"
199             + "  } catch(e) { }\n"
200             + "</script></head><body onload='test()'>\n"
201             + "</body></html>";
202 
203         loadPageVerifyTitle2(html);
204     }
205 
206     /**
207      * @throws Exception if the test fails
208      */
209     @Test
210     @Alerts(DEFAULT = "no initPopStateEvent",
211             FF = "exception ctor",
212             FF_ESR = "exception ctor")
213     public void initPopStateEvent() throws Exception {
214         final String html = DOCTYPE_HTML
215             + "<html><head><script>\n"
216             + LOG_TITLE_FUNCTION
217             + "  function test() {\n"
218             + "    try {\n"
219             + "      var event = document.createEvent('PopStateEvent');\n"
220             + "    } catch(e) { log('exception ctor'); return }\n"
221 
222             + "    if (event.initPopStateEvent) {\n"
223             + "      event.initPopStateEvent('PopState', true, false, 'html');\n"
224             + "      dump(event);\n"
225             + "    } else { log('no initPopStateEvent'); }\n"
226             + "  }\n"
227             + DUMP_EVENT_FUNCTION
228             + "</script></head><body onload='test()'>\n"
229             + "</body></html>";
230 
231         loadPageVerifyTitle2(html);
232     }
233 }