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 MessageEvent}.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class MessageEventTest extends WebDriverTestCase {
29  
30      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
31              + "    if (event) {\n"
32              + "      log(event);\n"
33              + "      log(event.type);\n"
34              + "      log(event.bubbles);\n"
35              + "      log(event.cancelable);\n"
36              + "      log(event.composed);\n"
37              + "      log(event.data);\n"
38              + "      log(event.origin);\n"
39              + "      log(event.lastEventId);\n"
40              + "      log(event.source);\n"
41              + "    } else {\n"
42              + "      log('no event');\n"
43              + "    }\n"
44              + "  }\n";
45  
46      /**
47       * @throws Exception if the test fails
48       */
49      @Test
50      @Alerts({"[object MessageEvent]", "type-message", "false", "false", "false",
51               "null", "", "", "null"})
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 MessageEvent('type-message');\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 MessageEvent]", "type-message", "false", "false", "false",
74               "test-data", "test-origin", "42", "[object Window]"})
75      public void create_ctorWithDetails() throws Exception {
76          final String html = DOCTYPE_HTML
77              + "<html><head><script>\n"
78              + LOG_TITLE_FUNCTION
79              + "  function test() {\n"
80              + "    try {\n"
81              + "      var event = new MessageEvent('type-message', {\n"
82              + "        'data': 'test-data',\n"
83              + "        'origin': 'test-origin',\n"
84              + "        'lastEventId': 42,\n"
85              + "        'source': window\n"
86              + "      });\n"
87              + "      dump(event);\n"
88              + "    } catch(e) { logEx(e); }\n"
89              + "  }\n"
90              + DUMP_EVENT_FUNCTION
91              + "</script></head><body onload='test()'>\n"
92              + "</body></html>";
93  
94          loadPageVerifyTitle2(html);
95      }
96  
97      /**
98       * @throws Exception if the test fails
99       */
100     @Test
101     @Alerts({"DOM2: exception", "DOM3: [object MessageEvent]"})
102     public void createEvent() throws Exception {
103         final String html = DOCTYPE_HTML
104             + "<html><head><script>\n"
105             + LOG_TITLE_FUNCTION
106             + "  function test() {\n"
107             + "    try {\n"
108             + "      log('DOM2: ' + document.createEvent('MessageEvents'));\n"
109             + "    } catch(e) {log('DOM2: exception')}\n"
110             + "    try {\n"
111             + "      log('DOM3: ' + document.createEvent('MessageEvent'));\n"
112             + "    } catch(e) {log('DOM3: exception')}\n"
113             + "  }\n"
114             + "</script></head><body onload='test()'>\n"
115             + "</body></html>";
116 
117         loadPageVerifyTitle2(html);
118     }
119 
120     /**
121      * @throws Exception if an error occurs
122      */
123     @Test
124     @Alerts("TypeError")
125     public void initMessageEventPortsNull() throws Exception {
126         final String[] expectedAlerts = getExpectedAlerts();
127         if (expectedAlerts.length > 4) {
128             expectedAlerts[6] += PORT;
129             setExpectedAlerts(expectedAlerts);
130         }
131 
132         final String origin = "http://localhost:" + PORT;
133         final String html = DOCTYPE_HTML
134             + "<html><body><script>\n"
135             + LOG_TITLE_FUNCTION
136             + "var e = document.createEvent('MessageEvent');\n"
137             + "if (e.initMessageEvent) {\n"
138             + "  try {\n"
139             + "    e.initMessageEvent('message', true, true, 'hello', '" + origin + "', 2, window, null);\n"
140             + "    dump(e);\n"
141             + "  } catch(e) { logEx(e); }\n"
142             + "} else {\n"
143             + "  log('no initMessageEvent');\n"
144             + "}\n"
145             + DUMP_EVENT_FUNCTION
146             + "</script></body></html>";
147 
148         loadPageVerifyTitle2(html);
149     }
150 
151     /**
152      * @throws Exception if an error occurs
153      */
154     @Test
155     @Alerts({"[object MessageEvent]", "message", "true", "true", "false", "hello",
156              "http://localhost:", "2", "[object Window]"})
157     public void initMessageEventPortsUndefined() throws Exception {
158         final String[] expectedAlerts = getExpectedAlerts();
159         if (expectedAlerts.length > 4) {
160             expectedAlerts[6] += PORT;
161             setExpectedAlerts(expectedAlerts);
162         }
163 
164         final String origin = "http://localhost:" + PORT;
165         final String html = DOCTYPE_HTML
166             + "<html><body><script>\n"
167             + LOG_TITLE_FUNCTION
168             + "var e = document.createEvent('MessageEvent');\n"
169             + "if (e.initMessageEvent) {\n"
170             + "  try {\n"
171             + "    e.initMessageEvent('message', true, true, 'hello', '" + origin + "', 2, window, undefined);\n"
172             + "    dump(e);\n"
173             + "  } catch(e) { logEx(e); }\n"
174             + "} else {\n"
175             + "  log('no initMessageEvent');\n"
176             + "}\n"
177             + DUMP_EVENT_FUNCTION
178             + "</script></body></html>";
179 
180         loadPageVerifyTitle2(html);
181     }
182 
183     /**
184      * @throws Exception if an error occurs
185      */
186     @Test
187     @Alerts({"[object MessageEvent]", "message", "true", "true", "false", "hello",
188              "http://localhost:", "2", "[object Window]"})
189     public void initMessageEvent() throws Exception {
190         final String[] expectedAlerts = getExpectedAlerts();
191         expectedAlerts[6] += PORT;
192         setExpectedAlerts(expectedAlerts);
193 
194         final String origin = "http://localhost:" + PORT;
195         final String html = DOCTYPE_HTML
196             + "<html><body><script>\n"
197             + LOG_TITLE_FUNCTION
198             + "var e = document.createEvent('MessageEvent');\n"
199             + "if (e.initMessageEvent) {\n"
200             + "  try {\n"
201             + "    e.initMessageEvent('message', true, true, 'hello', '" + origin + "', 2, window, []);\n"
202             + "    dump(e);\n"
203             + "  } catch(e) { logEx(e); }\n"
204             + "} else {\n"
205             + "  log('no initMessageEvent');\n"
206             + "}\n"
207             + DUMP_EVENT_FUNCTION
208             + "</script></body></html>";
209 
210         loadPageVerifyTitle2(html);
211     }
212 }