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