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