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;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.WebDriverTestCase;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.junit.jupiter.api.Test;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  
26  /**
27   * Tests for {@link MessageChannel}.
28   *
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   */
32  public class MessageChannelTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if an error occurs
36       */
37      @Test
38      @Alerts("Message back from the IFrame, Hello from the main page!")
39      public void test() throws Exception {
40          final String html = DOCTYPE_HTML
41                  + "<html><body>\n"
42                  + "<p>My body</p>\n"
43                  + "<iframe src='" + URL_SECOND + "' width='480' height='320'></iframe>\n"
44                  + "</body>\n"
45                  + "<script>\n"
46                  + "  if (window.MessageChannel) {\n"
47                  + "    var channel = new MessageChannel();\n"
48                  + "    var para = document.querySelector('p');\n"
49                  + "    var ifr = document.querySelector('iframe');\n"
50                  + "    var otherWindow = ifr.contentWindow;\n"
51                  + "    function iframeLoaded() {\n"
52                  + "      otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]);\n"
53                  + "    }\n"
54                  + "    ifr.addEventListener('load', iframeLoaded, false);\n"
55                  + "    function handleMessage(e) {\n"
56                  + "      para.innerHTML = e.data;\n"
57                  + "    }\n"
58                  + "    channel.port1.onmessage = handleMessage;\n"
59                  + "  }\n"
60                  + "</script></html>";
61  
62          final String html2 = DOCTYPE_HTML
63                  + "<html><body>\n"
64                  + "  <p>iFrame body</p>\n"
65                  + "</body>\n"
66                  + "<script>\n"
67                  + "  if (window.MessageChannel) {\n"
68                  + "    var para = document.querySelector('p');\n"
69                  + "    onmessage = function(e) {\n"
70                  + "      para.innerHTML = e.data;\n"
71                  + "      e.ports[0].postMessage('Message back from the IFrame');\n"
72                  + "    }\n"
73                  + "  }\n"
74                  + "</script></html>";
75  
76          getMockWebConnection().setResponse(URL_SECOND, html2);
77          final WebDriver driver = loadPage2(html);
78          final List<String> actual = new ArrayList<>();
79          actual.add(driver.findElement(By.tagName("p")).getText());
80          driver.switchTo().frame(0);
81          actual.add(driver.findElement(By.tagName("p")).getText());
82          assertEquals(getExpectedAlerts(), actual);
83      }
84  
85  }