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 WheelEvent}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class WheelEventTest 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              + "  }\n";
39  
40      /**
41       * @throws Exception if the test fails
42       */
43      @Test
44      @Alerts({"[object WheelEvent]", "wheel", "false", "false", "false"})
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 WheelEvent('wheel');\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("TypeError")
67      @HtmlUnitNYI(CHROME = {"[object WheelEvent]", "undefined", "false", "false", "false"},
68                  EDGE = {"[object WheelEvent]", "undefined", "false", "false", "false"},
69                  FF = {"[object WheelEvent]", "undefined", "false", "false", "false"},
70                  FF_ESR = {"[object WheelEvent]", "undefined", "false", "false", "false"})
71      public void create_ctorWithoutType() throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html><head><script>\n"
74              + LOG_TITLE_FUNCTION
75              + "  function test() {\n"
76              + "    try {\n"
77              + "      var event = new WheelEvent();\n"
78              + "      dump(event);\n"
79              + "    } catch(e) { logEx(e) }\n"
80              + "  }\n"
81              + DUMP_EVENT_FUNCTION
82              + "</script></head><body onload='test()'>\n"
83              + "</body></html>";
84  
85          loadPageVerifyTitle2(html);
86      }
87  
88      /**
89       * @throws Exception if the test fails
90       */
91      @Test
92      @Alerts({"[object WheelEvent]", "42", "false", "false", "false"})
93      public void create_ctorNumericType() throws Exception {
94          final String html = DOCTYPE_HTML
95              + "<html><head><script>\n"
96              + LOG_TITLE_FUNCTION
97              + "  function test() {\n"
98              + "    try {\n"
99              + "      var event = new WheelEvent(42);\n"
100             + "      dump(event);\n"
101             + "    } catch(e) { logEx(e) }\n"
102             + "  }\n"
103             + DUMP_EVENT_FUNCTION
104             + "</script></head><body onload='test()'>\n"
105             + "</body></html>";
106 
107         loadPageVerifyTitle2(html);
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts({"[object WheelEvent]", "null", "false", "false", "false"})
115     public void create_ctorNullType() throws Exception {
116         final String html = DOCTYPE_HTML
117             + "<html><head><script>\n"
118             + LOG_TITLE_FUNCTION
119             + "  function test() {\n"
120             + "    try {\n"
121             + "      var event = new WheelEvent(null);\n"
122             + "      dump(event);\n"
123             + "    } catch(e) { logEx(e) }\n"
124             + "  }\n"
125             + DUMP_EVENT_FUNCTION
126             + "</script></head><body onload='test()'>\n"
127             + "</body></html>";
128 
129         loadPageVerifyTitle2(html);
130     }
131 
132     /**
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts("ReferenceError")
137     public void create_ctorUnknownType() throws Exception {
138         final String html = DOCTYPE_HTML
139             + "<html><head><script>\n"
140             + LOG_TITLE_FUNCTION
141             + "  function test() {\n"
142             + "    try {\n"
143             + "      var event = new WheelEvent(unknown);\n"
144             + "      dump(event);\n"
145             + "    } catch(e) { logEx(e) }\n"
146             + "  }\n"
147             + DUMP_EVENT_FUNCTION
148             + "</script></head><body onload='test()'>\n"
149             + "</body></html>";
150 
151         loadPageVerifyTitle2(html);
152     }
153 
154     /**
155      * @throws Exception if the test fails
156      */
157     @Test
158     @Alerts({"[object WheelEvent]", "HtmlUnitEvent", "false", "false", "false"})
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 WheelEvent('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({"[object WheelEvent]", "wheel", "false", "false", "false"})
181     public void create_ctorAllDetails() throws Exception {
182         final String html = DOCTYPE_HTML
183             + "<html><head><script>\n"
184             + LOG_TITLE_FUNCTION
185             + "  function test() {\n"
186             + "    try {\n"
187             + "      var debug = {hello: 'world'};\n"
188             + "      var event = new WheelEvent('wheel', {\n"
189             + "        'deltaX': 2.3,\n"
190             + "        'deltaY': 2.4,\n"
191             + "        'deltaZ': 2.5,\n"
192             + "        'deltaMode': 1\n"
193             + "      });\n"
194             + "      dump(event);\n"
195             + "    } catch(e) { logEx(e) }\n"
196             + "  }\n"
197             + DUMP_EVENT_FUNCTION
198             + "</script></head><body onload='test()'>\n"
199             + "</body></html>";
200 
201         loadPageVerifyTitle2(html);
202     }
203 
204     /**
205      * @throws Exception if the test fails
206      */
207     @Test
208     @Alerts({"[object WheelEvent]", "wheel", "false", "false", "false"})
209     public void create_ctorAllDetailsMissingData() throws Exception {
210         final String html = DOCTYPE_HTML
211             + "<html><head><script>\n"
212             + LOG_TITLE_FUNCTION
213             + "  function test() {\n"
214             + "    try {\n"
215             + "      var event = new WheelEvent('wheel', {\n"
216             + "      });\n"
217             + "      dump(event);\n"
218             + "    } catch(e) { logEx(e) }\n"
219             + "  }\n"
220             + DUMP_EVENT_FUNCTION
221             + "</script></head><body onload='test()'>\n"
222             + "</body></html>";
223 
224         loadPageVerifyTitle2(html);
225     }
226 
227     /**
228      * @throws Exception if the test fails
229      */
230     @Test
231     @Alerts("TypeError")
232     @HtmlUnitNYI(CHROME = {"[object WheelEvent]", "wheel", "false", "false", "false"},
233                 EDGE = {"[object WheelEvent]", "wheel", "false", "false", "false"},
234                 FF = {"[object WheelEvent]", "wheel", "false", "false", "false"},
235                 FF_ESR = {"[object WheelEvent]", "wheel", "false", "false", "false"})
236     public void create_ctorAllDetailsWrongData() throws Exception {
237         final String html = DOCTYPE_HTML
238             + "<html><head><script>\n"
239             + LOG_TITLE_FUNCTION
240             + "  function test() {\n"
241             + "    try {\n"
242             + "      var event = new WheelEvent('wheel', {\n"
243             + "        'deltaX': 'ten'\n"
244             + "      });\n"
245             + "      dump(event);\n"
246             + "    } catch(e) { logEx(e) }\n"
247             + "  }\n"
248             + DUMP_EVENT_FUNCTION
249             + "</script></head><body onload='test()'>\n"
250             + "</body></html>";
251 
252         loadPageVerifyTitle2(html);
253     }
254 
255     /**
256      * @throws Exception if the test fails
257      */
258     @Test
259     @Alerts("true")
260     public void inWindow() throws Exception {
261         final String html = DOCTYPE_HTML
262             + "<html>\n"
263             + "<head>\n"
264             + "  <script>\n"
265             + LOG_TITLE_FUNCTION
266             + "    function test() {\n"
267             + "      log('WheelEvent' in window);\n"
268             + "    }\n"
269             + "  </script>\n"
270             + "</head>\n"
271             + "<body onload='test()'>\n"
272             + "</body>\n"
273             + "</html>";
274 
275         loadPageVerifyTitle2(html);
276     }
277 }