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.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link BeforeInstallPromptEvent}.
24   *
25   * @author Ronald Brill
26   */
27  public class BeforeInstallPromptEventTest extends WebDriverTestCase {
28  
29      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
30              + "    log(event);\n"
31              + "    log(event.type);\n"
32              + "    log(event.bubbles);\n"
33              + "    log(event.cancelable);\n"
34              + "    log(event.composed);\n"
35              + "  }\n";
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "before", "false", "false", "false"},
42              FF = "ReferenceError",
43              FF_ESR = "ReferenceError")
44      public void create_ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    try {\n"
50              + "      var event = new BeforeInstallPromptEvent('before');\n"
51              + "      dump(event);\n"
52              + "    } catch(e) { logEx(e) }\n"
53              + "  }\n"
54              + DUMP_EVENT_FUNCTION
55              + "</script></head><body onload='test()'>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts(DEFAULT = "TypeError",
66              FF = "ReferenceError",
67              FF_ESR = "ReferenceError")
68      @HtmlUnitNYI(CHROME = {"[object BeforeInstallPromptEvent]", "undefined", "false", "false", "false"},
69                  EDGE = {"[object BeforeInstallPromptEvent]", "undefined", "false", "false", "false"})
70      public void create_ctorWithoutType() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><head><script>\n"
73              + LOG_TITLE_FUNCTION
74              + "  function test() {\n"
75              + "    try {\n"
76              + "      var event = new BeforeInstallPromptEvent();\n"
77              + "      dump(event);\n"
78              + "    } catch(e) { logEx(e) }\n"
79              + "  }\n"
80              + DUMP_EVENT_FUNCTION
81              + "</script></head><body onload='test()'>\n"
82              + "</body></html>";
83  
84          loadPageVerifyTitle2(html);
85      }
86  
87      /**
88       * @throws Exception if the test fails
89       */
90      @Test
91      @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "42", "false", "false", "false"},
92              FF = "ReferenceError",
93              FF_ESR = "ReferenceError")
94      public void create_ctorNumericType() throws Exception {
95          final String html = DOCTYPE_HTML
96              + "<html><head><script>\n"
97              + LOG_TITLE_FUNCTION
98              + "  function test() {\n"
99              + "    try {\n"
100             + "      var event = new BeforeInstallPromptEvent(42);\n"
101             + "      dump(event);\n"
102             + "    } catch(e) { logEx(e) }\n"
103             + "  }\n"
104             + DUMP_EVENT_FUNCTION
105             + "</script></head><body onload='test()'>\n"
106             + "</body></html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception if the test fails
113      */
114     @Test
115     @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "null", "false", "false", "false"},
116             FF = "ReferenceError",
117             FF_ESR = "ReferenceError")
118     public void create_ctorNullType() throws Exception {
119         final String html = DOCTYPE_HTML
120             + "<html><head><script>\n"
121             + LOG_TITLE_FUNCTION
122             + "  function test() {\n"
123             + "    try {\n"
124             + "      var event = new BeforeInstallPromptEvent(null);\n"
125             + "      dump(event);\n"
126             + "    } catch(e) { logEx(e) }\n"
127             + "  }\n"
128             + DUMP_EVENT_FUNCTION
129             + "</script></head><body onload='test()'>\n"
130             + "</body></html>";
131 
132         loadPageVerifyTitle2(html);
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts("ReferenceError")
140     public void create_ctorUnknownType() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html><head><script>\n"
143             + LOG_TITLE_FUNCTION
144             + "  function test() {\n"
145             + "    try {\n"
146             + "      var event = new BeforeInstallPromptEvent(unknown);\n"
147             + "      dump(event);\n"
148             + "    } catch(e) { logEx(e) }\n"
149             + "  }\n"
150             + DUMP_EVENT_FUNCTION
151             + "</script></head><body onload='test()'>\n"
152             + "</body></html>";
153 
154         loadPageVerifyTitle2(html);
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts(DEFAULT = {"[object BeforeInstallPromptEvent]", "HtmlUnitEvent", "false", "false", "false"},
162             FF = "ReferenceError",
163             FF_ESR = "ReferenceError")
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 BeforeInstallPromptEvent('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(DEFAULT = {"[object BeforeInstallPromptEvent]", "click", "false", "false", "false"},
186             FF = "ReferenceError",
187             FF_ESR = "ReferenceError")
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 BeforeInstallPromptEvent('click', {\n"
195             + "      });\n"
196             + "      dump(event);\n"
197             + "    } catch(e) { logEx(e) }\n"
198             + "  }\n"
199             + DUMP_EVENT_FUNCTION
200             + "</script></head><body onload='test()'>\n"
201             + "</body></html>";
202 
203         loadPageVerifyTitle2(html);
204     }
205 
206     /**
207      * @throws Exception if the test fails
208      */
209     @Test
210     @Alerts(DEFAULT = "true",
211             FF = "false",
212             FF_ESR = "false")
213     public void inWindow() throws Exception {
214         final String html = DOCTYPE_HTML
215             + "<html>\n"
216             + "<head>\n"
217             + "  <script>\n"
218             + LOG_TITLE_FUNCTION
219             + "    function test() {\n"
220             + "      log('BeforeInstallPromptEvent' in window);\n"
221             + "    }\n"
222             + "  </script>\n"
223             + "</head>\n"
224             + "<body onload='test()'>\n"
225             + "</body>\n"
226             + "</html>";
227 
228         loadPageVerifyTitle2(html);
229     }
230 }