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.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link CloseEvent}.
25   *
26   * @author Frank Danek
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class CloseEventTest extends WebDriverTestCase {
31  
32      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
33          + "    if (event) {\n"
34          + "      log(event);\n"
35          + "      log(event.type);\n"
36          + "      log(event.bubbles);\n"
37          + "      log(event.cancelable);\n"
38          + "      log(event.composed);\n"
39          + "      log(event.code);\n"
40          + "      log(event.reason);\n"
41          + "      log(event.wasClean);\n"
42          + "    } else {\n"
43          + "      log('no event');\n"
44          + "    }\n"
45          + "  }\n";
46  
47      /**
48       * @throws Exception if the test fails
49       */
50      @Test
51      @Alerts({"[object CloseEvent]", "type-close", "false", "false", "false", "0", "", "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 CloseEvent('type-close');\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({"[object CloseEvent]", "type-close", "true", "false", "false", "42", "test-reason", "true"})
74      public void create_ctorWithDetails() throws Exception {
75          final String html = DOCTYPE_HTML
76              + "<html><head><script>\n"
77              + LOG_TITLE_FUNCTION
78              + "  function test() {\n"
79              + "    try {\n"
80              + "      var event = new CloseEvent('type-close', {\n"
81              + "        'bubbles': true,\n"
82              + "        'reason': 'test-reason',\n"
83              + "        'code': 42,\n"
84              + "        'wasClean': true\n"
85              + "      });\n"
86              + "      dump(event);\n"
87              + "    } catch(e) { logEx(e) }\n"
88              + "  }\n"
89              + DUMP_EVENT_FUNCTION
90              + "</script></head><body onload='test()'>\n"
91              + "</body></html>";
92  
93          loadPageVerifyTitle2(html);
94      }
95  
96      /**
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts(DEFAULT = {"[object CloseEvent]", "", "false", "false", "false", "0", "", "false"},
101             FF = "NotSupportedError/DOMException",
102             FF_ESR = "NotSupportedError/DOMException")
103     public void create_createEvent() throws Exception {
104         final String html = DOCTYPE_HTML
105             + "<html><head><script>\n"
106             + LOG_TITLE_FUNCTION
107             + "  function test() {\n"
108             + "    try {\n"
109             + "      var event = document.createEvent('CloseEvent');\n"
110             + "      dump(event);\n"
111             + "    } catch(e) { logEx(e) }\n"
112             + "  }\n"
113             + DUMP_EVENT_FUNCTION
114             + "</script></head><body onload='test()'>\n"
115             + "</body></html>";
116 
117         loadPageVerifyTitle2(html);
118     }
119 
120     /**
121      * @throws Exception if the test fails
122      */
123     @Test
124     @Alerts(DEFAULT = "no initCloseEvent",
125             FF = "NotSupportedError/DOMException",
126             FF_ESR = "NotSupportedError/DOMException")
127     public void initCloseEvent() throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html><head><script>\n"
130             + LOG_TITLE_FUNCTION
131             + "  function test() {\n"
132             + "    try {\n"
133             + "      var event = document.createEvent('CloseEvent');\n"
134             + "      if (event.initCloseEvent) {\n"
135             + "        event.initCloseEvent('close', true, false, true, 42, 'time to close');\n"
136             + "        dump(event);\n"
137             + "      } else { log('no initCloseEvent'); }\n"
138             + "    } catch(e) { logEx(e) }\n"
139             + "  }\n"
140             + DUMP_EVENT_FUNCTION
141             + "</script></head><body onload='test()'>\n"
142             + "</body></html>";
143 
144         loadPageVerifyTitle2(html);
145     }
146 }