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  import org.openqa.selenium.By;
23  import org.openqa.selenium.WebDriver;
24  
25  /**
26   * Tests for {@link HashChangeEvent}.
27   *
28   * @author Frank Danek
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class HashChangeEventTest extends WebDriverTestCase {
33  
34      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
35          + "    if (event) {\n"
36          + "      log(event);\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.oldURL);\n"
43          + "      log(event.newURL);\n"
44          + "    } else {\n"
45          + "      log('no event');\n"
46          + "    }\n"
47          + "  }\n";
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      @Alerts({"[object HashChangeEvent]", "hashchange", "false", "false", "false", "", ""})
54      public void create_ctor() throws Exception {
55          final String html = DOCTYPE_HTML
56              + "<html><head><script>\n"
57              + LOG_TITLE_FUNCTION
58              + "  function test() {\n"
59              + "    try {\n"
60              + "      var event = new HashChangeEvent('hashchange');\n"
61              + "      dump(event);\n"
62              + "    } catch(e) { logEx(e) }\n"
63              + "  }\n"
64              + DUMP_EVENT_FUNCTION
65              + "</script></head><body onload='test()'>\n"
66              + "</body></html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      @Alerts({"[object HashChangeEvent]", "hashchange", "true", "false", "false", "null", "§§URL§§#1"})
76      public void create_ctorWithDetails() throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html><head><script>\n"
79              + LOG_TITLE_FUNCTION
80              + "  function test() {\n"
81              + "    try {\n"
82              + "      var event = new HashChangeEvent('hashchange', {\n"
83              + "        'bubbles': true,\n"
84              + "        'oldURL': null,\n"
85              + "        'newURL': '" + URL_FIRST + "#1'\n"
86              + "      });\n"
87              + "      dump(event);\n"
88              + "    } catch(e) { logEx(e) }\n"
89              + "  }\n"
90              + DUMP_EVENT_FUNCTION
91              + "</script></head><body onload='test()'>\n"
92              + "</body></html>";
93  
94          expandExpectedAlertsVariables(URL_FIRST);
95          loadPageVerifyTitle2(html);
96      }
97  
98      /**
99       * @throws Exception if the test fails
100      */
101     @Test
102     @Alerts({"[object HashChangeEvent]", "", "false", "false", "false", "", ""})
103     public void create_createEvent() throws Exception {
104         final String html = DOCTYPE_HTML
105             + "<html><head><script>\n"
106             + LOG_TITLE_FUNCTION
107             + "  function test() {\n"
108             + "    try {\n"
109             + "      var event = document.createEvent('HashChangeEvent');\n"
110             + "      dump(event);\n"
111             + "    } catch(e) { logEx(e) }\n"
112             + "  }\n"
113             + DUMP_EVENT_FUNCTION
114             + "</script></head><body onload='test()'>\n"
115             + "</body></html>";
116 
117         loadPageVerifyTitle2(html);
118     }
119 
120     /**
121      * @throws Exception if the test fails
122      */
123     @Test
124     @Alerts({"[object HashChangeEvent]", "missing initHashChangeEvent"})
125     public void initHashChangeEvent() throws Exception {
126         final String html = DOCTYPE_HTML
127             + "<html><head><script>\n"
128             + LOG_TITLE_FUNCTION
129             + "  function test() {\n"
130             + "    try {\n"
131             + "      var event = document.createEvent('HashChangeEvent');\n"
132             + "      log(event);\n"
133             + "    } catch(e) { log('exception createEvent'); logEx(e); return; }\n"
134 
135             + "    if (!event.initHashChangeEvent) {log('missing initHashChangeEvent'); return;}\n"
136 
137             + "    try {\n"
138             + "      event.initHashChangeEvent('hashchange', true, false, '" + URL_FIRST + "', '"
139             + URL_FIRST + "#1');\n"
140             + "      dump(event);\n"
141             + "    } catch(e) { log('exception initHashChangeEvent'); logEx(e); }\n"
142             + "  }\n"
143             + DUMP_EVENT_FUNCTION
144             + "</script></head><body onload='test()'>\n"
145             + "</body></html>";
146 
147         expandExpectedAlertsVariables(URL_FIRST);
148         loadPageVerifyTitle2(html);
149     }
150 
151     /**
152      * @throws Exception if the test fails
153      */
154     @Test
155     @Alerts("TypeError")
156     public void dispatchEvent() throws Exception {
157         final String html = DOCTYPE_HTML
158             + "<html><head><script>\n"
159             + LOG_TITLE_FUNCTION
160             + "  function test() {\n"
161             + "    try {\n"
162             + "      var event = document.createEvent('HashChangeEvent');\n"
163             + "      event.initHashChangeEvent('hashchange', true, false, '" + URL_FIRST + "', '"
164             + URL_FIRST + "#1');\n"
165             + "      dispatchEvent(event);\n"
166             + "    } catch(e) { logEx(e) }\n"
167             + "  }\n"
168             + DUMP_EVENT_FUNCTION
169             + "  window.onhashchange = dump;\n"
170             + "</script></head><body onload='test()'>\n"
171             + "</body></html>";
172 
173         expandExpectedAlertsVariables(URL_FIRST);
174         loadPageVerifyTitle2(html);
175     }
176 
177     /**
178      * @throws Exception if the test fails
179      */
180     @Test
181     @Alerts({"[object Event]", "hashchange", "true", "false", "false", "undefined", "undefined"})
182     public void dispatchEvent_event() 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('Event');\n"
189             + "      event.initEvent('hashchange', true, false);\n"
190             + "      dispatchEvent(event);\n"
191             + "    } catch(e) { logEx(e) }\n"
192             + "  }\n"
193             + DUMP_EVENT_FUNCTION
194             + "  window.onhashchange = dump;\n"
195             + "</script></head><body onload='test()'>\n"
196             + "</body></html>";
197 
198         loadPageVerifyTitle2(html);
199     }
200 
201     /**
202      * @throws Exception if the test fails
203      */
204     @Test
205     @Alerts("supported")
206     public void onHashChange_supported() throws Exception {
207         final String html = DOCTYPE_HTML
208             + "<html><head><script>\n"
209             + LOG_TITLE_FUNCTION
210             + "  function test() {\n"
211             + "    if ('onhashchange' in window) { log('supported') }\n"
212             + "  }\n"
213             + "</script></head><body onload='test()'>\n"
214             + "</body></html>";
215 
216         loadPageVerifyTitle2(html);
217     }
218 
219     /**
220      * @throws Exception if the test fails
221      */
222     @Test
223     @Alerts({"[object HashChangeEvent]", "hashchange", "false", "false", "false", "§§URL§§", "§§URL§§#1"})
224     public void onHashChange() throws Exception {
225         final String html = DOCTYPE_HTML
226             + "<html><head><script>\n"
227             + LOG_TITLE_FUNCTION
228             + DUMP_EVENT_FUNCTION
229             + "  window.onhashchange = dump;\n"
230             + "</script></head><body>\n"
231             + "  <a id='click' href='#1'>change hash</a>\n"
232             + "</body></html>";
233 
234         expandExpectedAlertsVariables(URL_FIRST);
235         final WebDriver driver = loadPage2(html);
236         driver.findElement(By.id("click")).click();
237 
238         verifyTitle2(driver, getExpectedAlerts());
239     }
240 }