addon-sdk/source/test/addons/content-permissions/main.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/addons/content-permissions/main.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +const { PageMod } = require("sdk/page-mod");
    1.10 +const tabs = require("sdk/tabs");
    1.11 +const { startServerAsync } = require("sdk/test/httpd");
    1.12 +
    1.13 +const serverPort = 8099;
    1.14 +const TEST_TAB_URL = "about:mozilla";
    1.15 +
    1.16 +exports.testCrossDomainIframe = function(assert, done) {
    1.17 +  let server = startServerAsync(serverPort);
    1.18 +  server.registerPathHandler("/iframe", function handle(request, response) {
    1.19 +    response.write("<html><body>foo</body></html>");
    1.20 +  });
    1.21 +
    1.22 +  let pageMod = PageMod({
    1.23 +    include: TEST_TAB_URL,
    1.24 +    contentScript: "new " + function ContentScriptScope() {
    1.25 +      self.on("message", function (url) {
    1.26 +        let iframe = document.createElement("iframe");
    1.27 +        iframe.addEventListener("load", function onload() {
    1.28 +          iframe.removeEventListener("load", onload, false);
    1.29 +          self.postMessage(iframe.contentWindow.document.body.innerHTML);
    1.30 +        }, false);
    1.31 +        iframe.setAttribute("src", url);
    1.32 +        document.documentElement.appendChild(iframe);
    1.33 +      });
    1.34 +    },
    1.35 +    onAttach: function(w) {
    1.36 +      w.on("message", function (body) {
    1.37 +        assert.equal(body, "foo", "received iframe html content");
    1.38 +        pageMod.destroy();
    1.39 +        w.tab.close(function() {
    1.40 +          server.stop(done);
    1.41 +        });
    1.42 +      });
    1.43 +
    1.44 +      w.postMessage("http://localhost:" + serverPort + "/iframe");
    1.45 +    }
    1.46 +  });
    1.47 +
    1.48 +  tabs.open({
    1.49 +    url: TEST_TAB_URL,
    1.50 +    inBackground: true
    1.51 +  });
    1.52 +};
    1.53 +
    1.54 +exports.testCrossDomainXHR = function(assert, done) {
    1.55 +  let server = startServerAsync(serverPort);
    1.56 +  server.registerPathHandler("/xhr", function handle(request, response) {
    1.57 +    response.write("foo");
    1.58 +  });
    1.59 +
    1.60 +  let pageMod = PageMod({
    1.61 +    include: TEST_TAB_URL,
    1.62 +    contentScript: "new " + function ContentScriptScope() {
    1.63 +      self.on("message", function (url) {
    1.64 +        let request = new XMLHttpRequest();
    1.65 +        request.overrideMimeType("text/plain");
    1.66 +        request.open("GET", url, true);
    1.67 +        request.onload = function () {
    1.68 +          self.postMessage(request.responseText);
    1.69 +        };
    1.70 +        request.send(null);
    1.71 +      });
    1.72 +    },
    1.73 +    onAttach: function(w) {
    1.74 +      w.on("message", function (body) {
    1.75 +        assert.equal(body, "foo", "received XHR content");
    1.76 +        pageMod.destroy();
    1.77 +        w.tab.close(function() {
    1.78 +          server.stop(done);
    1.79 +        });
    1.80 +      });
    1.81 +
    1.82 +      w.postMessage("http://localhost:" + serverPort + "/xhr");
    1.83 +    }
    1.84 +  });
    1.85 +
    1.86 +  tabs.open({
    1.87 +    url: TEST_TAB_URL,
    1.88 +    inBackground: true
    1.89 +  });
    1.90 +};
    1.91 +
    1.92 +require("sdk/test/runner").runTestsFromModule(module);

mercurial