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 TextEvent}.
23   *
24   * @author Ronald Brill
25   */
26  public class TextEventTest 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              + "  }\n";
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts(DEFAULT = "TypeError",
41              FF_ESR = "ReferenceError")
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 TextEvent('text');\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(DEFAULT = "TypeError",
64              FF_ESR = "ReferenceError")
65      public void create_ctorWithoutType() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html><head><script>\n"
68              + LOG_TITLE_FUNCTION
69              + "  function test() {\n"
70              + "    try {\n"
71              + "      var event = new TextEvent();\n"
72              + "      dump(event);\n"
73              + "    } catch(e) { logEx(e) }\n"
74              + "  }\n"
75              + DUMP_EVENT_FUNCTION
76              + "</script></head><body onload='test()'>\n"
77              + "</body></html>";
78  
79          loadPageVerifyTitle2(html);
80      }
81  
82      /**
83       * @throws Exception if the test fails
84       */
85      @Test
86      @Alerts(DEFAULT = "TypeError",
87              FF_ESR = "ReferenceError")
88      public void create_ctorNumericType() throws Exception {
89          final String html = DOCTYPE_HTML
90              + "<html><head><script>\n"
91              + LOG_TITLE_FUNCTION
92              + "  function test() {\n"
93              + "    try {\n"
94              + "      var event = new TextEvent(42);\n"
95              + "      dump(event);\n"
96              + "    } catch(e) { logEx(e) }\n"
97              + "  }\n"
98              + DUMP_EVENT_FUNCTION
99              + "</script></head><body onload='test()'>\n"
100             + "</body></html>";
101 
102         loadPageVerifyTitle2(html);
103     }
104 
105     /**
106      * @throws Exception if the test fails
107      */
108     @Test
109     @Alerts(DEFAULT = "TypeError",
110             FF_ESR = "ReferenceError")
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 TextEvent(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 TextEvent(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(DEFAULT = "TypeError",
155             FF_ESR = "ReferenceError")
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 TextEvent('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(DEFAULT = "TypeError",
178             FF_ESR = "ReferenceError")
179     public void create_ctorAllDetails() throws Exception {
180         final String html = DOCTYPE_HTML
181             + "<html><head><script>\n"
182             + LOG_TITLE_FUNCTION
183             + "  function test() {\n"
184             + "    try {\n"
185             + "      var debug = {hello: 'world'};\n"
186             + "      var event = new TextEvent('text', {\n"
187             + "        'detail': 7\n"
188             + "      });\n"
189             + "      dump(event);\n"
190             + "    } catch(e) { logEx(e) }\n"
191             + "  }\n"
192             + DUMP_EVENT_FUNCTION
193             + "</script></head><body onload='test()'>\n"
194             + "</body></html>";
195 
196         loadPageVerifyTitle2(html);
197     }
198 
199     /**
200      * @throws Exception if the test fails
201      */
202     @Test
203     @Alerts(DEFAULT = "TypeError",
204             FF_ESR = "ReferenceError")
205     public void create_ctorAllDetailsMissingData() throws Exception {
206         final String html = DOCTYPE_HTML
207             + "<html><head><script>\n"
208             + LOG_TITLE_FUNCTION
209             + "  function test() {\n"
210             + "    try {\n"
211             + "      var event = new TextEvent('text', {\n"
212             + "      });\n"
213             + "      dump(event);\n"
214             + "    } catch(e) { logEx(e) }\n"
215             + "  }\n"
216             + DUMP_EVENT_FUNCTION
217             + "</script></head><body onload='test()'>\n"
218             + "</body></html>";
219 
220         loadPageVerifyTitle2(html);
221     }
222 
223     /**
224      * @throws Exception if the test fails
225      */
226     @Test
227     @Alerts(DEFAULT = "TypeError",
228             FF_ESR = "ReferenceError")
229     public void create_ctorAllDetailsWrongData() throws Exception {
230         final String html = DOCTYPE_HTML
231             + "<html><head><script>\n"
232             + LOG_TITLE_FUNCTION
233             + "  function test() {\n"
234             + "    try {\n"
235             + "      var event = new TextEvent('text', {\n"
236             + "        'detail': 'ten'\n"
237             + "      });\n"
238             + "      dump(event);\n"
239             + "    } catch(e) { logEx(e) }\n"
240             + "  }\n"
241             + DUMP_EVENT_FUNCTION
242             + "</script></head><body onload='test()'>\n"
243             + "</body></html>";
244 
245         loadPageVerifyTitle2(html);
246     }
247 
248     /**
249      * @throws Exception if the test fails
250      */
251     @Test
252     @Alerts(DEFAULT = "true",
253             FF_ESR = "false")
254     public void inWindow() throws Exception {
255         final String html = DOCTYPE_HTML
256             + "<html>\n"
257             + "<head>\n"
258             + "  <script>\n"
259             + LOG_TITLE_FUNCTION
260             + "    function test() {\n"
261             + "      log('TextEvent' in window);\n"
262             + "    }\n"
263             + "  </script>\n"
264             + "</head>\n"
265             + "<body onload='test()'>\n"
266             + "</body>\n"
267             + "</html>";
268 
269         loadPageVerifyTitle2(html);
270     }
271 }