dom/base/test/test_messagemanager_targetchain.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/test/test_messagemanager_targetchain.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for EventTarget chain of MessageManagers</title>
     1.8 +  <script type="application/javascript"
     1.9 +          src="/tests/SimpleTest/SimpleTest.js">
    1.10 +  </script>
    1.11 +  <script type="application/javascript"
    1.12 +          src="/tests/SimpleTest/EventUtils.js">
    1.13 +  </script>
    1.14 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.15 +</head>
    1.16 +<body>
    1.17 +
    1.18 +  <script type="application/javascript;version=1.7">
    1.19 +    "use strict";
    1.20 +
    1.21 +    SimpleTest.waitForExplicitFinish();
    1.22 +
    1.23 +    const browserFrameURL = "file_empty.html";
    1.24 +    const contentFrameURL =
    1.25 +      "data:text/html,<!DOCTYPE HTML><html><body><button id=\"target\">target</button></body></html>";
    1.26 +
    1.27 +    function frameScript() {
    1.28 +      "use strict";
    1.29 +      addEventListener("test-event", function (e) {
    1.30 +        sendSyncMessage("test-event");
    1.31 +      }, true);
    1.32 +    }
    1.33 +
    1.34 +    function runTests() {
    1.35 +      // messageIndex is incremented for each message/event received
    1.36 +      let messageIndex = 0;
    1.37 +
    1.38 +      let iframe = document.createElement("iframe");
    1.39 +      iframe.setAttribute("mozbrowser", true);
    1.40 +      iframe.setAttribute("src", browserFrameURL);
    1.41 +
    1.42 +      iframe.addEventListener("mozbrowserloadend", function () {
    1.43 +        info("First iframe loaded");
    1.44 +        // First message manager
    1.45 +        let mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
    1.46 +        mm.addMessageListener("test-event", function onEvent(message) {
    1.47 +          is(messageIndex, 0,
    1.48 +             "first mm should be the first one to receive the test event");
    1.49 +          messageIndex++;
    1.50 +        });
    1.51 +        mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
    1.52 +
    1.53 +        // Document in the middle
    1.54 +        let doc1 = SpecialPowers.wrap(iframe).contentDocument;
    1.55 +        doc1.addEventListener("test-event", function (e) {
    1.56 +          ok(false, "content document shouldn't receive test event from child");
    1.57 +        }, true);
    1.58 +
    1.59 +        let iframe2 = doc1.createElement("iframe");
    1.60 +        iframe2.setAttribute("mozbrowser", true);
    1.61 +        iframe2.setAttribute("src", browserFrameURL);
    1.62 +
    1.63 +        iframe2.addEventListener("mozbrowserloadend", function () {
    1.64 +          info("Second iframe loaded");
    1.65 +          // Second message manager
    1.66 +          let mm2 = SpecialPowers.getBrowserFrameMessageManager(iframe2);
    1.67 +          mm2.addMessageListener("test-event", function onEvent(message) {
    1.68 +            is(messageIndex, 1,
    1.69 +               "second mm should be the second one to receive the test event");
    1.70 +            messageIndex++;
    1.71 +          });
    1.72 +          mm2.loadFrameScript("data:,(" + frameScript.toString() +")();", false);
    1.73 +
    1.74 +          // Third is the regular iframe
    1.75 +          let doc2 = SpecialPowers.wrap(iframe2).contentDocument;
    1.76 +          let iframe3 = doc2.createElement("iframe");
    1.77 +          iframe3.setAttribute("src", contentFrameURL);
    1.78 +
    1.79 +          iframe3.addEventListener("load", function (e) {
    1.80 +            info("Third iframe loaded");
    1.81 +            let doc3 = SpecialPowers.wrap(iframe3).contentDocument;
    1.82 +            let target = doc3.getElementById("target");
    1.83 +            target.addEventListener("test-event", function onEvent(e) {
    1.84 +              is(messageIndex, 2,
    1.85 +                 "target should be the last one to receive the test event");
    1.86 +              messageIndex++;
    1.87 +              SimpleTest.finish();
    1.88 +            });
    1.89 +
    1.90 +            // Fire test event after load
    1.91 +            SimpleTest.executeSoon(function () {
    1.92 +              var event = new Event("test-event");
    1.93 +              SpecialPowers.dispatchEvent(iframe3.contentWindow, target, event);
    1.94 +            });
    1.95 +          });
    1.96 +          doc2.body.appendChild(iframe3);
    1.97 +        });
    1.98 +        doc1.body.appendChild(iframe2);
    1.99 +      });
   1.100 +      document.addEventListener("test-event", function (e) {
   1.101 +        ok(false, "top document shouldn't receive test event from child");
   1.102 +      }, true);
   1.103 +      document.body.appendChild(iframe);
   1.104 +    }
   1.105 +
   1.106 +    addEventListener("load", function() {
   1.107 +      var principal = SpecialPowers.wrap(document).nodePrincipal;
   1.108 +      SpecialPowers.addPermission("browser", true, { url: SpecialPowers.wrap(principal.URI).spec,
   1.109 +                                                     appId: principal.appId,
   1.110 +                                                     isInBrowserElement: false });
   1.111 +      SpecialPowers.addPermission("browser", true, { url: SpecialPowers.wrap(principal.URI).spec,
   1.112 +                                                     appId: principal.appId,
   1.113 +                                                     isInBrowserElement: true });
   1.114 +      SpecialPowers.pushPrefEnv({
   1.115 +        "set": [
   1.116 +          ["dom.mozBrowserFramesEnabled", true],
   1.117 +          ["dom.ipc.browser_frames.oop_by_default", false],
   1.118 +        ]
   1.119 +      }, runTests);
   1.120 +    });
   1.121 +    SimpleTest.registerCleanupFunction(function () {
   1.122 +      var principal = SpecialPowers.wrap(document).nodePrincipal;
   1.123 +      SpecialPowers.removePermission("browser", { url: SpecialPowers.wrap(principal.URI).spec,
   1.124 +                                                  appId: principal.appId,
   1.125 +                                                  isInBrowserElement: false });
   1.126 +      SpecialPowers.removePermission("browser", { url: SpecialPowers.wrap(principal.URI).spec,
   1.127 +                                                  appId: principal.appId,
   1.128 +                                                  isInBrowserElement: true });
   1.129 +    });
   1.130 +  </script>
   1.131 +</body>
   1.132 +</html>

mercurial