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