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