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 StorageEvent}.
24   *
25   * @author Ronald Brill
26   */
27  public class StorageEventTest 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              + "  }\n";
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts({"[object StorageEvent]", "storage", "false", "false", "false"})
42      public void create_ctor() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html><head><script>\n"
45              + LOG_TITLE_FUNCTION
46              + "  function test() {\n"
47              + "    try {\n"
48              + "      var event = new StorageEvent('storage');\n"
49              + "      dump(event);\n"
50              + "    } catch(e) { logEx(e) }\n"
51              + "  }\n"
52              + DUMP_EVENT_FUNCTION
53              + "</script></head><body onload='test()'>\n"
54              + "</body></html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts("TypeError")
64      @HtmlUnitNYI(CHROME = {"[object StorageEvent]", "undefined", "false", "false", "false"},
65                  EDGE = {"[object StorageEvent]", "undefined", "false", "false", "false"},
66                  FF = {"[object StorageEvent]", "undefined", "false", "false", "false"},
67                  FF_ESR = {"[object StorageEvent]", "undefined", "false", "false", "false"})
68      public void create_ctorWithoutType() throws Exception {
69          final String html = DOCTYPE_HTML
70              + "<html><head><script>\n"
71              + LOG_TITLE_FUNCTION
72              + "  function test() {\n"
73              + "    try {\n"
74              + "      var event = new StorageEvent();\n"
75              + "      dump(event);\n"
76              + "    } catch(e) { logEx(e) }\n"
77              + "  }\n"
78              + DUMP_EVENT_FUNCTION
79              + "</script></head><body onload='test()'>\n"
80              + "</body></html>";
81  
82          loadPageVerifyTitle2(html);
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      @Alerts({"[object StorageEvent]", "42", "false", "false", "false"})
90      public void create_ctorNumericType() throws Exception {
91          final String html = DOCTYPE_HTML
92              + "<html><head><script>\n"
93              + LOG_TITLE_FUNCTION
94              + "  function test() {\n"
95              + "    try {\n"
96              + "      var event = new StorageEvent(42);\n"
97              + "      dump(event);\n"
98              + "    } catch(e) { logEx(e) }\n"
99              + "  }\n"
100             + DUMP_EVENT_FUNCTION
101             + "</script></head><body onload='test()'>\n"
102             + "</body></html>";
103 
104         loadPageVerifyTitle2(html);
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts({"[object StorageEvent]", "null", "false", "false", "false"})
112     public void create_ctorNullType() throws Exception {
113         final String html = DOCTYPE_HTML
114             + "<html><head><script>\n"
115             + LOG_TITLE_FUNCTION
116             + "  function test() {\n"
117             + "    try {\n"
118             + "      var event = new StorageEvent(null);\n"
119             + "      dump(event);\n"
120             + "    } catch(e) { logEx(e) }\n"
121             + "  }\n"
122             + DUMP_EVENT_FUNCTION
123             + "</script></head><body onload='test()'>\n"
124             + "</body></html>";
125 
126         loadPageVerifyTitle2(html);
127     }
128 
129     /**
130      * @throws Exception if the test fails
131      */
132     @Test
133     @Alerts("ReferenceError")
134     public void create_ctorUnknownType() throws Exception {
135         final String html = DOCTYPE_HTML
136             + "<html><head><script>\n"
137             + LOG_TITLE_FUNCTION
138             + "  function test() {\n"
139             + "    try {\n"
140             + "      var event = new StorageEvent(unknown);\n"
141             + "      dump(event);\n"
142             + "    } catch(e) { logEx(e) }\n"
143             + "  }\n"
144             + DUMP_EVENT_FUNCTION
145             + "</script></head><body onload='test()'>\n"
146             + "</body></html>";
147 
148         loadPageVerifyTitle2(html);
149     }
150 
151     /**
152      * @throws Exception if the test fails
153      */
154     @Test
155     @Alerts({"[object StorageEvent]", "HtmlUnitEvent", "false", "false", "false"})
156     public void create_ctorArbitraryType() 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 = new StorageEvent('HtmlUnitEvent');\n"
163             + "      dump(event);\n"
164             + "    } catch(e) { logEx(e) }\n"
165             + "  }\n"
166             + DUMP_EVENT_FUNCTION
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({"[object StorageEvent]", "storage", "false", "false", "false"})
178     public void create_ctorAllDetails() throws Exception {
179         final String html = DOCTYPE_HTML
180             + "<html><head><script>\n"
181             + LOG_TITLE_FUNCTION
182             + "  function test() {\n"
183             + "    try {\n"
184             + "      var debug = {hello: 'world'};\n"
185             + "      var event = new StorageEvent('storage', {\n"
186             + "        'key': 'aKey',\n"
187             + "        'newValue': 'aNewValue',\n"
188             + "        'oldValue': 'anOldValue',\n"
189             + "        'storageArea': null,\n"
190             + "        'url': 'aUrl'\n"
191             + "      });\n"
192             + "      dump(event);\n"
193             + "    } catch(e) { logEx(e) }\n"
194             + "  }\n"
195             + DUMP_EVENT_FUNCTION
196             + "</script></head><body onload='test()'>\n"
197             + "</body></html>";
198 
199         loadPageVerifyTitle2(html);
200     }
201 
202     /**
203      * @throws Exception if the test fails
204      */
205     @Test
206     @Alerts({"[object StorageEvent]", "storage", "false", "false", "false"})
207     public void create_ctorAllDetailsMissingData() throws Exception {
208         final String html = DOCTYPE_HTML
209             + "<html><head><script>\n"
210             + LOG_TITLE_FUNCTION
211             + "  function test() {\n"
212             + "    try {\n"
213             + "      var event = new StorageEvent('storage', {\n"
214             + "      });\n"
215             + "      dump(event);\n"
216             + "    } catch(e) { logEx(e) }\n"
217             + "  }\n"
218             + DUMP_EVENT_FUNCTION
219             + "</script></head><body onload='test()'>\n"
220             + "</body></html>";
221 
222         loadPageVerifyTitle2(html);
223     }
224 
225     /**
226      * @throws Exception if the test fails
227      */
228     @Test
229     @Alerts("TypeError")
230     @HtmlUnitNYI(CHROME = {"[object StorageEvent]", "storage", "false", "false", "false"},
231                 EDGE = {"[object StorageEvent]", "storage", "false", "false", "false"},
232                 FF = {"[object StorageEvent]", "storage", "false", "false", "false"},
233                 FF_ESR = {"[object StorageEvent]", "storage", "false", "false", "false"})
234     public void create_ctorAllDetailsWrongData() throws Exception {
235         final String html = DOCTYPE_HTML
236             + "<html><head><script>\n"
237             + LOG_TITLE_FUNCTION
238             + "  function test() {\n"
239             + "    try {\n"
240             + "      var event = new StorageEvent('storage', {\n"
241             + "        'storageArea': 'wrong'\n"
242             + "      });\n"
243             + "      dump(event);\n"
244             + "    } catch(e) { logEx(e) }\n"
245             + "  }\n"
246             + DUMP_EVENT_FUNCTION
247             + "</script></head><body onload='test()'>\n"
248             + "</body></html>";
249 
250         loadPageVerifyTitle2(html);
251     }
252 
253     /**
254      * @throws Exception if the test fails
255      */
256     @Test
257     @Alerts("true")
258     public void inWindow() throws Exception {
259         final String html = DOCTYPE_HTML
260             + "<html>\n"
261             + "<head>\n"
262             + "  <script>\n"
263             + LOG_TITLE_FUNCTION
264             + "    function test() {\n"
265             + "      log('StorageEvent' in window);\n"
266             + "    }\n"
267             + "  </script>\n"
268             + "</head>\n"
269             + "<body onload='test()'>\n"
270             + "</body>\n"
271             + "</html>";
272 
273         loadPageVerifyTitle2(html);
274     }
275 }