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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 const { PageMod } = require("sdk/page-mod");
michael@0 7 const tabs = require("sdk/tabs");
michael@0 8 const { startServerAsync } = require("sdk/test/httpd");
michael@0 9
michael@0 10 const serverPort = 8099;
michael@0 11 const TEST_TAB_URL = "about:mozilla";
michael@0 12
michael@0 13 exports.testCrossDomainIframe = function(assert, done) {
michael@0 14 let server = startServerAsync(serverPort);
michael@0 15 server.registerPathHandler("/iframe", function handle(request, response) {
michael@0 16 response.write("<html><body>foo</body></html>");
michael@0 17 });
michael@0 18
michael@0 19 let pageMod = PageMod({
michael@0 20 include: TEST_TAB_URL,
michael@0 21 contentScript: "new " + function ContentScriptScope() {
michael@0 22 self.on("message", function (url) {
michael@0 23 let iframe = document.createElement("iframe");
michael@0 24 iframe.addEventListener("load", function onload() {
michael@0 25 iframe.removeEventListener("load", onload, false);
michael@0 26 self.postMessage(iframe.contentWindow.document.body.innerHTML);
michael@0 27 }, false);
michael@0 28 iframe.setAttribute("src", url);
michael@0 29 document.documentElement.appendChild(iframe);
michael@0 30 });
michael@0 31 },
michael@0 32 onAttach: function(w) {
michael@0 33 w.on("message", function (body) {
michael@0 34 assert.equal(body, "foo", "received iframe html content");
michael@0 35 pageMod.destroy();
michael@0 36 w.tab.close(function() {
michael@0 37 server.stop(done);
michael@0 38 });
michael@0 39 });
michael@0 40
michael@0 41 w.postMessage("http://localhost:" + serverPort + "/iframe");
michael@0 42 }
michael@0 43 });
michael@0 44
michael@0 45 tabs.open({
michael@0 46 url: TEST_TAB_URL,
michael@0 47 inBackground: true
michael@0 48 });
michael@0 49 };
michael@0 50
michael@0 51 exports.testCrossDomainXHR = function(assert, done) {
michael@0 52 let server = startServerAsync(serverPort);
michael@0 53 server.registerPathHandler("/xhr", function handle(request, response) {
michael@0 54 response.write("foo");
michael@0 55 });
michael@0 56
michael@0 57 let pageMod = PageMod({
michael@0 58 include: TEST_TAB_URL,
michael@0 59 contentScript: "new " + function ContentScriptScope() {
michael@0 60 self.on("message", function (url) {
michael@0 61 let request = new XMLHttpRequest();
michael@0 62 request.overrideMimeType("text/plain");
michael@0 63 request.open("GET", url, true);
michael@0 64 request.onload = function () {
michael@0 65 self.postMessage(request.responseText);
michael@0 66 };
michael@0 67 request.send(null);
michael@0 68 });
michael@0 69 },
michael@0 70 onAttach: function(w) {
michael@0 71 w.on("message", function (body) {
michael@0 72 assert.equal(body, "foo", "received XHR content");
michael@0 73 pageMod.destroy();
michael@0 74 w.tab.close(function() {
michael@0 75 server.stop(done);
michael@0 76 });
michael@0 77 });
michael@0 78
michael@0 79 w.postMessage("http://localhost:" + serverPort + "/xhr");
michael@0 80 }
michael@0 81 });
michael@0 82
michael@0 83 tabs.open({
michael@0 84 url: TEST_TAB_URL,
michael@0 85 inBackground: true
michael@0 86 });
michael@0 87 };
michael@0 88
michael@0 89 require("sdk/test/runner").runTestsFromModule(module);

mercurial