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