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.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link CloseEvent}.
23   *
24   * @author Frank Danek
25   * @author Ronald Brill
26   */
27  public class CloseEventTest extends WebDriverTestCase {
28  
29      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
30          + "    if (event) {\n"
31          + "      log(event);\n"
32          + "      log(event.type);\n"
33          + "      log(event.bubbles);\n"
34          + "      log(event.cancelable);\n"
35          + "      log(event.composed);\n"
36          + "      log(event.code);\n"
37          + "      log(event.reason);\n"
38          + "      log(event.wasClean);\n"
39          + "    } else {\n"
40          + "      log('no event');\n"
41          + "    }\n"
42          + "  }\n";
43  
44      /**
45       * @throws Exception if the test fails
46       */
47      @Test
48      @Alerts({"[object CloseEvent]", "type-close", "false", "false", "false", "0", "", "false"})
49      public void create_ctor() throws Exception {
50          final String html = DOCTYPE_HTML
51              + "<html><head><script>\n"
52              + LOG_TITLE_FUNCTION
53              + "  function test() {\n"
54              + "    try {\n"
55              + "      var event = new CloseEvent('type-close');\n"
56              + "      dump(event);\n"
57              + "    } catch(e) { logEx(e) }\n"
58              + "  }\n"
59              + DUMP_EVENT_FUNCTION
60              + "</script></head><body onload='test()'>\n"
61              + "</body></html>";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts({"[object CloseEvent]", "type-close", "true", "false", "false", "42", "test-reason", "true"})
71      public void create_ctorWithDetails() 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 CloseEvent('type-close', {\n"
78              + "        'bubbles': true,\n"
79              + "        'reason': 'test-reason',\n"
80              + "        'code': 42,\n"
81              + "        'wasClean': true\n"
82              + "      });\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(DEFAULT = {"[object CloseEvent]", "", "false", "false", "false", "0", "", "false"},
98              FF = "NotSupportedError/DOMException",
99              FF_ESR = "NotSupportedError/DOMException")
100     public void create_createEvent() 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 = document.createEvent('CloseEvent');\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(DEFAULT = "no initCloseEvent",
122             FF = "NotSupportedError/DOMException",
123             FF_ESR = "NotSupportedError/DOMException")
124     public void initCloseEvent() throws Exception {
125         final String html = DOCTYPE_HTML
126             + "<html><head><script>\n"
127             + LOG_TITLE_FUNCTION
128             + "  function test() {\n"
129             + "    try {\n"
130             + "      var event = document.createEvent('CloseEvent');\n"
131             + "      if (event.initCloseEvent) {\n"
132             + "        event.initCloseEvent('close', true, false, true, 42, 'time to close');\n"
133             + "        dump(event);\n"
134             + "      } else { log('no initCloseEvent'); }\n"
135             + "    } catch(e) { logEx(e) }\n"
136             + "  }\n"
137             + DUMP_EVENT_FUNCTION
138             + "</script></head><body onload='test()'>\n"
139             + "</body></html>";
140 
141         loadPageVerifyTitle2(html);
142     }
143 }