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