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 TextEvent}.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class TextEventTest 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              + "  }\n";
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts(DEFAULT = "TypeError",
44              FF_ESR = "ReferenceError")
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 TextEvent('text');\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(DEFAULT = "TypeError",
67              FF_ESR = "ReferenceError")
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 TextEvent();\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(DEFAULT = "TypeError",
90              FF_ESR = "ReferenceError")
91      public void create_ctorNumericType() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html><head><script>\n"
94              + LOG_TITLE_FUNCTION
95              + "  function test() {\n"
96              + "    try {\n"
97              + "      var event = new TextEvent(42);\n"
98              + "      dump(event);\n"
99              + "    } catch(e) { logEx(e) }\n"
100             + "  }\n"
101             + DUMP_EVENT_FUNCTION
102             + "</script></head><body onload='test()'>\n"
103             + "</body></html>";
104 
105         loadPageVerifyTitle2(html);
106     }
107 
108     /**
109      * @throws Exception if the test fails
110      */
111     @Test
112     @Alerts(DEFAULT = "TypeError",
113             FF_ESR = "ReferenceError")
114     public void create_ctorNullType() throws Exception {
115         final String html = DOCTYPE_HTML
116             + "<html><head><script>\n"
117             + LOG_TITLE_FUNCTION
118             + "  function test() {\n"
119             + "    try {\n"
120             + "      var event = new TextEvent(null);\n"
121             + "      dump(event);\n"
122             + "    } catch(e) { logEx(e) }\n"
123             + "  }\n"
124             + DUMP_EVENT_FUNCTION
125             + "</script></head><body onload='test()'>\n"
126             + "</body></html>";
127 
128         loadPageVerifyTitle2(html);
129     }
130 
131     /**
132      * @throws Exception if the test fails
133      */
134     @Test
135     @Alerts("ReferenceError")
136     public void create_ctorUnknownType() throws Exception {
137         final String html = DOCTYPE_HTML
138             + "<html><head><script>\n"
139             + LOG_TITLE_FUNCTION
140             + "  function test() {\n"
141             + "    try {\n"
142             + "      var event = new TextEvent(unknown);\n"
143             + "      dump(event);\n"
144             + "    } catch(e) { logEx(e) }\n"
145             + "  }\n"
146             + DUMP_EVENT_FUNCTION
147             + "</script></head><body onload='test()'>\n"
148             + "</body></html>";
149 
150         loadPageVerifyTitle2(html);
151     }
152 
153     /**
154      * @throws Exception if the test fails
155      */
156     @Test
157     @Alerts(DEFAULT = "TypeError",
158             FF_ESR = "ReferenceError")
159     public void create_ctorArbitraryType() throws Exception {
160         final String html = DOCTYPE_HTML
161             + "<html><head><script>\n"
162             + LOG_TITLE_FUNCTION
163             + "  function test() {\n"
164             + "    try {\n"
165             + "      var event = new TextEvent('HtmlUnitEvent');\n"
166             + "      dump(event);\n"
167             + "    } catch(e) { logEx(e) }\n"
168             + "  }\n"
169             + DUMP_EVENT_FUNCTION
170             + "</script></head><body onload='test()'>\n"
171             + "</body></html>";
172 
173         loadPageVerifyTitle2(html);
174     }
175 
176     /**
177      * @throws Exception if the test fails
178      */
179     @Test
180     @Alerts(DEFAULT = "TypeError",
181             FF_ESR = "ReferenceError")
182     public void create_ctorAllDetails() throws Exception {
183         final String html = DOCTYPE_HTML
184             + "<html><head><script>\n"
185             + LOG_TITLE_FUNCTION
186             + "  function test() {\n"
187             + "    try {\n"
188             + "      var debug = {hello: 'world'};\n"
189             + "      var event = new TextEvent('text', {\n"
190             + "        'detail': 7\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(DEFAULT = "TypeError",
207             FF_ESR = "ReferenceError")
208     public void create_ctorAllDetailsMissingData() throws Exception {
209         final String html = DOCTYPE_HTML
210             + "<html><head><script>\n"
211             + LOG_TITLE_FUNCTION
212             + "  function test() {\n"
213             + "    try {\n"
214             + "      var event = new TextEvent('text', {\n"
215             + "      });\n"
216             + "      dump(event);\n"
217             + "    } catch(e) { logEx(e) }\n"
218             + "  }\n"
219             + DUMP_EVENT_FUNCTION
220             + "</script></head><body onload='test()'>\n"
221             + "</body></html>";
222 
223         loadPageVerifyTitle2(html);
224     }
225 
226     /**
227      * @throws Exception if the test fails
228      */
229     @Test
230     @Alerts(DEFAULT = "TypeError",
231             FF_ESR = "ReferenceError")
232     public void create_ctorAllDetailsWrongData() throws Exception {
233         final String html = DOCTYPE_HTML
234             + "<html><head><script>\n"
235             + LOG_TITLE_FUNCTION
236             + "  function test() {\n"
237             + "    try {\n"
238             + "      var event = new TextEvent('text', {\n"
239             + "        'detail': 'ten'\n"
240             + "      });\n"
241             + "      dump(event);\n"
242             + "    } catch(e) { logEx(e) }\n"
243             + "  }\n"
244             + DUMP_EVENT_FUNCTION
245             + "</script></head><body onload='test()'>\n"
246             + "</body></html>";
247 
248         loadPageVerifyTitle2(html);
249     }
250 
251     /**
252      * @throws Exception if the test fails
253      */
254     @Test
255     @Alerts(DEFAULT = "true",
256             FF_ESR = "false")
257     public void inWindow() throws Exception {
258         final String html = DOCTYPE_HTML
259             + "<html>\n"
260             + "<head>\n"
261             + "  <script>\n"
262             + LOG_TITLE_FUNCTION
263             + "    function test() {\n"
264             + "      log('TextEvent' in window);\n"
265             + "    }\n"
266             + "  </script>\n"
267             + "</head>\n"
268             + "<body onload='test()'>\n"
269             + "</body>\n"
270             + "</html>";
271 
272         loadPageVerifyTitle2(html);
273     }
274 }