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