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 BeforeInstallPromptEvent}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class BeforeInstallPromptEventTest 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(DEFAULT = {"[object BeforeInstallPromptEvent]", "before", "false", "false", "false"},
45              FF = "ReferenceError",
46              FF_ESR = "ReferenceError")
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 BeforeInstallPromptEvent('before');\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(DEFAULT = "TypeError",
69              FF = "ReferenceError",
70              FF_ESR = "ReferenceError")
71      @HtmlUnitNYI(CHROME = {"[object BeforeInstallPromptEvent]", "undefined", "false", "false", "false"},
72                  EDGE = {"[object BeforeInstallPromptEvent]", "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 BeforeInstallPromptEvent();\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(DEFAULT = {"[object BeforeInstallPromptEvent]", "42", "false", "false", "false"},
95              FF = "ReferenceError",
96              FF_ESR = "ReferenceError")
97      public void create_ctorNumericType() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html><head><script>\n"
100             + LOG_TITLE_FUNCTION
101             + "  function test() {\n"
102             + "    try {\n"
103             + "      var event = new BeforeInstallPromptEvent(42);\n"
104             + "      dump(event);\n"
105             + "    } catch(e) { logEx(e) }\n"
106             + "  }\n"
107             + DUMP_EVENT_FUNCTION
108             + "</script></head><body onload='test()'>\n"
109             + "</body></html>";
110 
111         loadPageVerifyTitle2(html);
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "null", "false", "false", "false"},
119             FF = "ReferenceError",
120             FF_ESR = "ReferenceError")
121     public void create_ctorNullType() throws Exception {
122         final String html = DOCTYPE_HTML
123             + "<html><head><script>\n"
124             + LOG_TITLE_FUNCTION
125             + "  function test() {\n"
126             + "    try {\n"
127             + "      var event = new BeforeInstallPromptEvent(null);\n"
128             + "      dump(event);\n"
129             + "    } catch(e) { logEx(e) }\n"
130             + "  }\n"
131             + DUMP_EVENT_FUNCTION
132             + "</script></head><body onload='test()'>\n"
133             + "</body></html>";
134 
135         loadPageVerifyTitle2(html);
136     }
137 
138     /**
139      * @throws Exception if the test fails
140      */
141     @Test
142     @Alerts("ReferenceError")
143     public void create_ctorUnknownType() throws Exception {
144         final String html = DOCTYPE_HTML
145             + "<html><head><script>\n"
146             + LOG_TITLE_FUNCTION
147             + "  function test() {\n"
148             + "    try {\n"
149             + "      var event = new BeforeInstallPromptEvent(unknown);\n"
150             + "      dump(event);\n"
151             + "    } catch(e) { logEx(e) }\n"
152             + "  }\n"
153             + DUMP_EVENT_FUNCTION
154             + "</script></head><body onload='test()'>\n"
155             + "</body></html>";
156 
157         loadPageVerifyTitle2(html);
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "HtmlUnitEvent", "false", "false", "false"},
165             FF = "ReferenceError",
166             FF_ESR = "ReferenceError")
167     public void create_ctorArbitraryType() throws Exception {
168         final String html = DOCTYPE_HTML
169             + "<html><head><script>\n"
170             + LOG_TITLE_FUNCTION
171             + "  function test() {\n"
172             + "    try {\n"
173             + "      var event = new BeforeInstallPromptEvent('HtmlUnitEvent');\n"
174             + "      dump(event);\n"
175             + "    } catch(e) { logEx(e) }\n"
176             + "  }\n"
177             + DUMP_EVENT_FUNCTION
178             + "</script></head><body onload='test()'>\n"
179             + "</body></html>";
180 
181         loadPageVerifyTitle2(html);
182     }
183 
184     /**
185      * @throws Exception if the test fails
186      */
187     @Test
188     @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "click", "false", "false", "false"},
189             FF = "ReferenceError",
190             FF_ESR = "ReferenceError")
191     public void create_ctorAllDetails() throws Exception {
192         final String html = DOCTYPE_HTML
193             + "<html><head><script>\n"
194             + LOG_TITLE_FUNCTION
195             + "  function test() {\n"
196             + "    try {\n"
197             + "      var event = new BeforeInstallPromptEvent('click', {\n"
198             + "      });\n"
199             + "      dump(event);\n"
200             + "    } catch(e) { logEx(e) }\n"
201             + "  }\n"
202             + DUMP_EVENT_FUNCTION
203             + "</script></head><body onload='test()'>\n"
204             + "</body></html>";
205 
206         loadPageVerifyTitle2(html);
207     }
208 
209     /**
210      * @throws Exception if the test fails
211      */
212     @Test
213     @Alerts(DEFAULT = "true",
214             FF = "false",
215             FF_ESR = "false")
216     public void inWindow() throws Exception {
217         final String html = DOCTYPE_HTML
218             + "<html>\n"
219             + "<head>\n"
220             + "  <script>\n"
221             + LOG_TITLE_FUNCTION
222             + "    function test() {\n"
223             + "      log('BeforeInstallPromptEvent' in window);\n"
224             + "    }\n"
225             + "  </script>\n"
226             + "</head>\n"
227             + "<body onload='test()'>\n"
228             + "</body>\n"
229             + "</html>";
230 
231         loadPageVerifyTitle2(html);
232     }
233 }