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