michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0: "use strict";
michael@0:
michael@0: const { PageMod } = require("sdk/page-mod");
michael@0: const { testPageMod, handleReadyState } = require("./pagemod-test-helpers");
michael@0: const { Loader } = require('sdk/test/loader');
michael@0: const tabs = require("sdk/tabs");
michael@0: const { setTimeout } = require("sdk/timers");
michael@0: const { Cc, Ci, Cu } = require("chrome");
michael@0: const {
michael@0: open,
michael@0: getFrames,
michael@0: getMostRecentBrowserWindow,
michael@0: getInnerId
michael@0: } = require('sdk/window/utils');
michael@0: const { getTabContentWindow, getActiveTab, setTabURL, openTab, closeTab } = require('sdk/tabs/utils');
michael@0: const xulApp = require("sdk/system/xul-app");
michael@0: const { isPrivateBrowsingSupported } = require('sdk/self');
michael@0: const { isPrivate } = require('sdk/private-browsing');
michael@0: const { openWebpage } = require('./private-browsing/helper');
michael@0: const { isTabPBSupported, isWindowPBSupported, isGlobalPBSupported } = require('sdk/private-browsing/utils');
michael@0: const promise = require("sdk/core/promise");
michael@0: const { pb } = require('./private-browsing/helper');
michael@0: const { URL } = require("sdk/url");
michael@0: const { LoaderWithHookedConsole } = require('sdk/test/loader');
michael@0:
michael@0: const { waitUntil } = require("sdk/test/utils");
michael@0: const data = require("./fixtures");
michael@0:
michael@0: const { gDevToolsExtensions } = Cu.import("resource://gre/modules/devtools/DevToolsExtensions.jsm", {});
michael@0:
michael@0: const testPageURI = data.url("test.html");
michael@0:
michael@0: // The following adds Debugger constructor to the global namespace.
michael@0: const { addDebuggerToGlobal } =
michael@0: Cu.import('resource://gre/modules/jsdebugger.jsm', {});
michael@0: addDebuggerToGlobal(this);
michael@0:
michael@0: function Isolate(worker) {
michael@0: return "(" + worker + ")()";
michael@0: }
michael@0:
michael@0: /* Tests for the PageMod APIs */
michael@0:
michael@0: exports.testPageMod1 = function(assert, done) {
michael@0: let mods = testPageMod(assert, done, "about:", [{
michael@0: include: /about:/,
michael@0: contentScriptWhen: 'end',
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: window.document.body.setAttribute("JEP-107", "worked");
michael@0: },
michael@0: onAttach: function() {
michael@0: assert.equal(this, mods[0], "The 'this' object is the page mod.");
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: assert.equal(
michael@0: win.document.body.getAttribute("JEP-107"),
michael@0: "worked",
michael@0: "PageMod.onReady test"
michael@0: );
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testPageMod2 = function(assert, done) {
michael@0: testPageMod(assert, done, "about:", [{
michael@0: include: "about:*",
michael@0: contentScript: [
michael@0: 'new ' + function contentScript() {
michael@0: window.AUQLUE = function() { return 42; }
michael@0: try {
michael@0: window.AUQLUE()
michael@0: }
michael@0: catch(e) {
michael@0: throw new Error("PageMod scripts executed in order");
michael@0: }
michael@0: document.documentElement.setAttribute("first", "true");
michael@0: },
michael@0: 'new ' + function contentScript() {
michael@0: document.documentElement.setAttribute("second", "true");
michael@0: }
michael@0: ]
michael@0: }], function(win, done) {
michael@0: assert.equal(win.document.documentElement.getAttribute("first"),
michael@0: "true",
michael@0: "PageMod test #2: first script has run");
michael@0: assert.equal(win.document.documentElement.getAttribute("second"),
michael@0: "true",
michael@0: "PageMod test #2: second script has run");
michael@0: assert.equal("AUQLUE" in win, false,
michael@0: "PageMod test #2: scripts get a wrapped window");
michael@0: done();
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testPageModIncludes = function(assert, done) {
michael@0: var asserts = [];
michael@0: function createPageModTest(include, expectedMatch) {
michael@0: // Create an 'onload' test function...
michael@0: asserts.push(function(test, win) {
michael@0: var matches = include in win.localStorage;
michael@0: assert.ok(expectedMatch ? matches : !matches,
michael@0: "'" + include + "' match test, expected: " + expectedMatch);
michael@0: });
michael@0: // ...and corresponding PageMod options
michael@0: return {
michael@0: include: include,
michael@0: contentScript: 'new ' + function() {
michael@0: self.on("message", function(msg) {
michael@0: window.localStorage[msg] = true;
michael@0: });
michael@0: },
michael@0: // The testPageMod callback with test assertions is called on 'end',
michael@0: // and we want this page mod to be attached before it gets called,
michael@0: // so we attach it on 'start'.
michael@0: contentScriptWhen: 'start',
michael@0: onAttach: function(worker) {
michael@0: worker.postMessage(this.include[0]);
michael@0: }
michael@0: };
michael@0: }
michael@0:
michael@0: testPageMod(assert, done, testPageURI, [
michael@0: createPageModTest("*", false),
michael@0: createPageModTest("*.google.com", false),
michael@0: createPageModTest("resource:*", true),
michael@0: createPageModTest("resource:", false),
michael@0: createPageModTest(testPageURI, true)
michael@0: ],
michael@0: function (win, done) {
michael@0: waitUntil(function () win.localStorage[testPageURI],
michael@0: testPageURI + " page-mod to be executed")
michael@0: .then(function () {
michael@0: asserts.forEach(function(fn) {
michael@0: fn(assert, win);
michael@0: });
michael@0: done();
michael@0: });
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testPageModValidationAttachTo = function(assert) {
michael@0: [{ val: 'top', type: 'string "top"' },
michael@0: { val: 'frame', type: 'string "frame"' },
michael@0: { val: ['top', 'existing'], type: 'array with "top" and "existing"' },
michael@0: { val: ['frame', 'existing'], type: 'array with "frame" and "existing"' },
michael@0: { val: ['top'], type: 'array with "top"' },
michael@0: { val: ['frame'], type: 'array with "frame"' },
michael@0: { val: undefined, type: 'undefined' }].forEach((attachTo) => {
michael@0: new PageMod({ attachTo: attachTo.val, include: '*.validation111' });
michael@0: assert.pass("PageMod() does not throw when attachTo is " + attachTo.type);
michael@0: });
michael@0:
michael@0: [{ val: 'existing', type: 'string "existing"' },
michael@0: { val: ['existing'], type: 'array with "existing"' },
michael@0: { val: 'not-legit', type: 'string with "not-legit"' },
michael@0: { val: ['not-legit'], type: 'array with "not-legit"' },
michael@0: { val: {}, type: 'object' }].forEach((attachTo) => {
michael@0: assert.throws(() =>
michael@0: new PageMod({ attachTo: attachTo.val, include: '*.validation111' }),
michael@0: /The `attachTo` option/,
michael@0: "PageMod() throws when 'attachTo' option is " + attachTo.type + ".");
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testPageModValidationInclude = function(assert) {
michael@0: [{ val: undefined, type: 'undefined' },
michael@0: { val: {}, type: 'object' },
michael@0: { val: [], type: 'empty array'},
michael@0: { val: [/regexp/, 1], type: 'array with non string/regexp' },
michael@0: { val: 1, type: 'number' }].forEach((include) => {
michael@0: assert.throws(() => new PageMod({ include: include.val }),
michael@0: /The `include` option must always contain atleast one rule/,
michael@0: "PageMod() throws when 'include' option is " + include.type + ".");
michael@0: });
michael@0:
michael@0: [{ val: '*.validation111', type: 'string' },
michael@0: { val: /validation111/, type: 'regexp' },
michael@0: { val: ['*.validation111'], type: 'array with length > 0'}].forEach((include) => {
michael@0: new PageMod({ include: include.val });
michael@0: assert.pass("PageMod() does not throw when include option is " + include.type);
michael@0: });
michael@0: };
michael@0:
michael@0: /* Tests for internal functions. */
michael@0: exports.testCommunication1 = function(assert, done) {
michael@0: let workerDone = false,
michael@0: callbackDone = null;
michael@0:
michael@0: testPageMod(assert, done, "about:", [{
michael@0: include: "about:*",
michael@0: contentScriptWhen: 'end',
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: self.on('message', function(msg) {
michael@0: document.body.setAttribute('JEP-107', 'worked');
michael@0: self.postMessage(document.body.getAttribute('JEP-107'));
michael@0: })
michael@0: },
michael@0: onAttach: function(worker) {
michael@0: worker.on('error', function(e) {
michael@0: assert.fail('Errors where reported');
michael@0: });
michael@0: worker.on('message', function(value) {
michael@0: assert.equal(
michael@0: "worked",
michael@0: value,
michael@0: "test comunication"
michael@0: );
michael@0: workerDone = true;
michael@0: if (callbackDone)
michael@0: callbackDone();
michael@0: });
michael@0: worker.postMessage('do it!')
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: (callbackDone = function() {
michael@0: if (workerDone) {
michael@0: assert.equal(
michael@0: 'worked',
michael@0: win.document.body.getAttribute('JEP-107'),
michael@0: 'attribute should be modified'
michael@0: );
michael@0: done();
michael@0: }
michael@0: })();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testCommunication2 = function(assert, done) {
michael@0: let callbackDone = null,
michael@0: window;
michael@0:
michael@0: testPageMod(assert, done, "about:license", [{
michael@0: include: "about:*",
michael@0: contentScriptWhen: 'start',
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: document.documentElement.setAttribute('AUQLUE', 42);
michael@0: window.addEventListener('load', function listener() {
michael@0: self.postMessage('onload');
michael@0: }, false);
michael@0: self.on("message", function() {
michael@0: self.postMessage(document.documentElement.getAttribute("test"))
michael@0: });
michael@0: },
michael@0: onAttach: function(worker) {
michael@0: worker.on('error', function(e) {
michael@0: assert.fail('Errors where reported');
michael@0: });
michael@0: worker.on('message', function(msg) {
michael@0: if ('onload' == msg) {
michael@0: assert.equal(
michael@0: '42',
michael@0: window.document.documentElement.getAttribute('AUQLUE'),
michael@0: 'PageMod scripts executed in order'
michael@0: );
michael@0: window.document.documentElement.setAttribute('test', 'changes in window');
michael@0: worker.postMessage('get window.test')
michael@0: } else {
michael@0: assert.equal(
michael@0: 'changes in window',
michael@0: msg,
michael@0: 'PageMod test #2: second script has run'
michael@0: )
michael@0: callbackDone();
michael@0: }
michael@0: });
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: window = win;
michael@0: callbackDone = done;
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testEventEmitter = function(assert, done) {
michael@0: let workerDone = false,
michael@0: callbackDone = null;
michael@0:
michael@0: testPageMod(assert, done, "about:", [{
michael@0: include: "about:*",
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: self.port.on('addon-to-content', function(data) {
michael@0: self.port.emit('content-to-addon', data);
michael@0: });
michael@0: },
michael@0: onAttach: function(worker) {
michael@0: worker.on('error', function(e) {
michael@0: assert.fail('Errors were reported : '+e);
michael@0: });
michael@0: worker.port.on('content-to-addon', function(value) {
michael@0: assert.equal(
michael@0: "worked",
michael@0: value,
michael@0: "EventEmitter API works!"
michael@0: );
michael@0: if (callbackDone)
michael@0: callbackDone();
michael@0: else
michael@0: workerDone = true;
michael@0: });
michael@0: worker.port.emit('addon-to-content', 'worked');
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: if (workerDone)
michael@0: done();
michael@0: else
michael@0: callbackDone = done;
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: // Execute two concurrent page mods on same document to ensure that their
michael@0: // JS contexts are different
michael@0: exports.testMixedContext = function(assert, done) {
michael@0: let doneCallback = null;
michael@0: let messages = 0;
michael@0: let modObject = {
michael@0: include: "data:text/html;charset=utf-8,",
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: // Both scripts will execute this,
michael@0: // context is shared if one script see the other one modification.
michael@0: let isContextShared = "sharedAttribute" in document;
michael@0: self.postMessage(isContextShared);
michael@0: document.sharedAttribute = true;
michael@0: },
michael@0: onAttach: function(w) {
michael@0: w.on("message", function (isContextShared) {
michael@0: if (isContextShared) {
michael@0: assert.fail("Page mod contexts are mixed.");
michael@0: doneCallback();
michael@0: }
michael@0: else if (++messages == 2) {
michael@0: assert.pass("Page mod contexts are different.");
michael@0: doneCallback();
michael@0: }
michael@0: });
michael@0: }
michael@0: };
michael@0: testPageMod(assert, done, "data:text/html;charset=utf-8,", [modObject, modObject],
michael@0: function(win, done) {
michael@0: doneCallback = done;
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testHistory = function(assert, done) {
michael@0: // We need a valid url in order to have a working History API.
michael@0: // (i.e do not work on data: or about: pages)
michael@0: // Test bug 679054.
michael@0: let url = data.url("test-page-mod.html");
michael@0: let callbackDone = null;
michael@0: testPageMod(assert, done, url, [{
michael@0: include: url,
michael@0: contentScriptWhen: 'end',
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: history.pushState({}, "", "#");
michael@0: history.replaceState({foo: "bar"}, "", "#");
michael@0: self.postMessage(history.state);
michael@0: },
michael@0: onAttach: function(worker) {
michael@0: worker.on('message', function (data) {
michael@0: assert.equal(JSON.stringify(data), JSON.stringify({foo: "bar"}),
michael@0: "History API works!");
michael@0: callbackDone();
michael@0: });
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: callbackDone = done;
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testRelatedTab = function(assert, done) {
michael@0: let tab;
michael@0: let pageMod = new PageMod({
michael@0: include: "about:*",
michael@0: onAttach: function(worker) {
michael@0: assert.ok(!!worker.tab, "Worker.tab exists");
michael@0: assert.equal(tab, worker.tab, "Worker.tab is valid");
michael@0: pageMod.destroy();
michael@0: tab.close(done);
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: "about:",
michael@0: onOpen: function onOpen(t) {
michael@0: tab = t;
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testRelatedTabNoRequireTab = function(assert, done) {
michael@0: let loader = Loader(module);
michael@0: let tab;
michael@0: let url = "data:text/html;charset=utf-8," + encodeURI("Test related worker tab 2");
michael@0: let { PageMod } = loader.require("sdk/page-mod");
michael@0: let pageMod = new PageMod({
michael@0: include: url,
michael@0: onAttach: function(worker) {
michael@0: assert.equal(worker.tab.url, url, "Worker.tab.url is valid");
michael@0: worker.tab.close(function() {
michael@0: pageMod.destroy();
michael@0: loader.unload();
michael@0: done();
michael@0: });
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open(url);
michael@0: };
michael@0:
michael@0: exports.testRelatedTabNoOtherReqs = function(assert, done) {
michael@0: let loader = Loader(module);
michael@0: let { PageMod } = loader.require("sdk/page-mod");
michael@0: let pageMod = new PageMod({
michael@0: include: "about:blank?testRelatedTabNoOtherReqs",
michael@0: onAttach: function(worker) {
michael@0: assert.ok(!!worker.tab, "Worker.tab exists");
michael@0: pageMod.destroy();
michael@0: worker.tab.close(function() {
michael@0: worker.destroy();
michael@0: loader.unload();
michael@0: done();
michael@0: });
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: "about:blank?testRelatedTabNoOtherReqs"
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testWorksWithExistingTabs = function(assert, done) {
michael@0: let url = "data:text/html;charset=utf-8," + encodeURI("Test unique document");
michael@0: let { PageMod } = require("sdk/page-mod");
michael@0: tabs.open({
michael@0: url: url,
michael@0: onReady: function onReady(tab) {
michael@0: let pageModOnExisting = new PageMod({
michael@0: include: url,
michael@0: attachTo: ["existing", "top", "frame"],
michael@0: onAttach: function(worker) {
michael@0: assert.ok(!!worker.tab, "Worker.tab exists");
michael@0: assert.equal(tab, worker.tab, "A worker has been created on this existing tab");
michael@0:
michael@0: setTimeout(function() {
michael@0: pageModOnExisting.destroy();
michael@0: pageModOffExisting.destroy();
michael@0: tab.close(done);
michael@0: }, 0);
michael@0: }
michael@0: });
michael@0:
michael@0: let pageModOffExisting = new PageMod({
michael@0: include: url,
michael@0: onAttach: function(worker) {
michael@0: assert.fail("pageModOffExisting page-mod should not have attached to anything");
michael@0: }
michael@0: });
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testExistingFrameDoesntMatchInclude = function(assert, done) {
michael@0: let iframeURL = 'data:text/html;charset=utf-8,UNIQUE-TEST-STRING-42';
michael@0: let iframe = '';
michael@0: let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iframe);
michael@0: tabs.open({
michael@0: url: url,
michael@0: onReady: function onReady(tab) {
michael@0: let pagemod = new PageMod({
michael@0: include: url,
michael@0: attachTo: ['existing', 'frame'],
michael@0: onAttach: function() {
michael@0: assert.fail("Existing iframe URL doesn't match include, must not attach to anything");
michael@0: }
michael@0: });
michael@0: setTimeout(function() {
michael@0: assert.pass("PageMod didn't attach to anything")
michael@0: pagemod.destroy();
michael@0: tab.close(done);
michael@0: }, 250);
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testExistingOnlyFrameMatchesInclude = function(assert, done) {
michael@0: let iframeURL = 'data:text/html;charset=utf-8,UNIQUE-TEST-STRING-43';
michael@0: let iframe = '';
michael@0: let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iframe);
michael@0: tabs.open({
michael@0: url: url,
michael@0: onReady: function onReady(tab) {
michael@0: let pagemod = new PageMod({
michael@0: include: iframeURL,
michael@0: attachTo: ['existing', 'frame'],
michael@0: onAttach: function(worker) {
michael@0: assert.equal(iframeURL, worker.url,
michael@0: "PageMod attached to existing iframe when only it matches include rules");
michael@0: pagemod.destroy();
michael@0: tab.close(done);
michael@0: }
michael@0: });
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testContentScriptWhenDefault = function(assert) {
michael@0: let pagemod = PageMod({include: '*'});
michael@0:
michael@0: assert.equal(pagemod.contentScriptWhen, 'end', "Default contentScriptWhen is 'end'");
michael@0: pagemod.destroy();
michael@0: }
michael@0:
michael@0: // test timing for all 3 contentScriptWhen options (start, ready, end)
michael@0: // for new pages, or tabs opened after PageMod is created
michael@0: exports.testContentScriptWhenForNewTabs = function(assert, done) {
michael@0: const url = "data:text/html;charset=utf-8,testContentScriptWhenForNewTabs";
michael@0:
michael@0: let count = 0;
michael@0:
michael@0: handleReadyState(url, 'start', {
michael@0: onLoading: (tab) => {
michael@0: assert.pass("PageMod is attached while document is loading");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
michael@0: onComplete: () => assert.fail("onComplete should not be called with 'start'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'ready', {
michael@0: onInteractive: (tab) => {
michael@0: assert.pass("PageMod is attached while document is interactive");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
michael@0: onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'end', {
michael@0: onComplete: (tab) => {
michael@0: assert.pass("PageMod is attached when document is complete");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'end'."),
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
michael@0: });
michael@0:
michael@0: tabs.open(url);
michael@0: }
michael@0:
michael@0: // test timing for all 3 contentScriptWhen options (start, ready, end)
michael@0: // for PageMods created right as the tab is created (in tab.onOpen)
michael@0: exports.testContentScriptWhenOnTabOpen = function(assert, done) {
michael@0: const url = "data:text/html;charset=utf-8,testContentScriptWhenOnTabOpen";
michael@0:
michael@0: tabs.open({
michael@0: url: url,
michael@0: onOpen: function(tab) {
michael@0: let count = 0;
michael@0:
michael@0: handleReadyState(url, 'start', {
michael@0: onLoading: () => {
michael@0: assert.pass("PageMod is attached while document is loading");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
michael@0: onComplete: () => assert.fail("onComplete should not be called with 'start'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'ready', {
michael@0: onInteractive: () => {
michael@0: assert.pass("PageMod is attached while document is interactive");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
michael@0: onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'end', {
michael@0: onComplete: () => {
michael@0: assert.pass("PageMod is attached when document is complete");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'end'."),
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
michael@0: });
michael@0:
michael@0: }
michael@0: });
michael@0: }
michael@0:
michael@0: // test timing for all 3 contentScriptWhen options (start, ready, end)
michael@0: // for PageMods created while the tab is interactive (in tab.onReady)
michael@0: exports.testContentScriptWhenOnTabReady = function(assert, done) {
michael@0: const url = "data:text/html;charset=utf-8,testContentScriptWhenOnTabReady";
michael@0:
michael@0: tabs.open({
michael@0: url: url,
michael@0: onReady: function(tab) {
michael@0: let count = 0;
michael@0:
michael@0: handleReadyState(url, 'start', {
michael@0: onInteractive: () => {
michael@0: assert.pass("PageMod is attached while document is interactive");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'start'."),
michael@0: onComplete: () => assert.fail("onComplete should not be called with 'start'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'ready', {
michael@0: onInteractive: () => {
michael@0: assert.pass("PageMod is attached while document is interactive");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
michael@0: onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'end', {
michael@0: onComplete: () => {
michael@0: assert.pass("PageMod is attached when document is complete");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'end'."),
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
michael@0: });
michael@0:
michael@0: }
michael@0: });
michael@0: }
michael@0:
michael@0: // test timing for all 3 contentScriptWhen options (start, ready, end)
michael@0: // for PageMods created after a tab has completed loading (in tab.onLoad)
michael@0: exports.testContentScriptWhenOnTabLoad = function(assert, done) {
michael@0: const url = "data:text/html;charset=utf-8,testContentScriptWhenOnTabLoad";
michael@0:
michael@0: tabs.open({
michael@0: url: url,
michael@0: onLoad: function(tab) {
michael@0: let count = 0;
michael@0:
michael@0: handleReadyState(url, 'start', {
michael@0: onComplete: () => {
michael@0: assert.pass("PageMod is attached when document is complete");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'start'."),
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'ready', {
michael@0: onComplete: () => {
michael@0: assert.pass("PageMod is attached when document is complete");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'ready'."),
michael@0: });
michael@0:
michael@0: handleReadyState(url, 'end', {
michael@0: onComplete: () => {
michael@0: assert.pass("PageMod is attached when document is complete");
michael@0: if (++count === 3)
michael@0: tab.close(done);
michael@0: },
michael@0: onLoading: () => assert.fail("onLoading should not be called with 'end'."),
michael@0: onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
michael@0: });
michael@0:
michael@0: }
michael@0: });
michael@0: }
michael@0:
michael@0: exports.testTabWorkerOnMessage = function(assert, done) {
michael@0: let { browserWindows } = require("sdk/windows");
michael@0: let tabs = require("sdk/tabs");
michael@0: let { PageMod } = require("sdk/page-mod");
michael@0:
michael@0: let url1 = "data:text/html;charset=utf-8,
tab1worker1.tab
";
michael@0: let url2 = "data:text/html;charset=utf-8,tab2worker2.tab
";
michael@0: let worker1 = null;
michael@0:
michael@0: let mod = PageMod({
michael@0: include: "data:text/html*",
michael@0: contentScriptWhen: "ready",
michael@0: contentScript: "self.postMessage('#1');",
michael@0: onAttach: function onAttach(worker) {
michael@0: worker.on("message", function onMessage() {
michael@0: this.tab.attach({
michael@0: contentScriptWhen: "ready",
michael@0: contentScript: "self.postMessage({ url: window.location.href, title: document.title });",
michael@0: onMessage: function onMessage(data) {
michael@0: assert.equal(this.tab.url, data.url, "location is correct");
michael@0: assert.equal(this.tab.title, data.title, "title is correct");
michael@0: if (this.tab.url === url1) {
michael@0: worker1 = this;
michael@0: tabs.open({ url: url2, inBackground: true });
michael@0: }
michael@0: else if (this.tab.url === url2) {
michael@0: mod.destroy();
michael@0: worker1.tab.close(function() {
michael@0: worker1.destroy();
michael@0: worker.tab.close(function() {
michael@0: worker.destroy();
michael@0: done();
michael@0: });
michael@0: });
michael@0: }
michael@0: }
michael@0: });
michael@0: });
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open(url1);
michael@0: };
michael@0:
michael@0: exports.testAutomaticDestroy = function(assert, done) {
michael@0: let loader = Loader(module);
michael@0:
michael@0: let pageMod = loader.require("sdk/page-mod").PageMod({
michael@0: include: "about:*",
michael@0: contentScriptWhen: "start",
michael@0: onAttach: function(w) {
michael@0: assert.fail("Page-mod should have been detroyed during module unload");
michael@0: }
michael@0: });
michael@0:
michael@0: // Unload the page-mod module so that our page mod is destroyed
michael@0: loader.unload();
michael@0:
michael@0: // Then create a second tab to ensure that it is correctly destroyed
michael@0: let tabs = require("sdk/tabs");
michael@0: tabs.open({
michael@0: url: "about:",
michael@0: onReady: function onReady(tab) {
michael@0: assert.pass("check automatic destroy");
michael@0: tab.close(done);
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testAttachToTabsOnly = function(assert, done) {
michael@0: let { PageMod } = require('sdk/page-mod');
michael@0: let openedTab = null; // Tab opened in openTabWithIframe()
michael@0: let workerCount = 0;
michael@0:
michael@0: let mod = PageMod({
michael@0: include: 'data:text/html*',
michael@0: contentScriptWhen: 'start',
michael@0: contentScript: '',
michael@0: onAttach: function onAttach(worker) {
michael@0: if (worker.tab === openedTab) {
michael@0: if (++workerCount == 3) {
michael@0: assert.pass('Succesfully applied to tab documents and its iframe');
michael@0: worker.destroy();
michael@0: mod.destroy();
michael@0: openedTab.close(done);
michael@0: }
michael@0: }
michael@0: else {
michael@0: assert.fail('page-mod attached to a non-tab document');
michael@0: }
michael@0: }
michael@0: });
michael@0:
michael@0: function openHiddenFrame() {
michael@0: assert.pass('Open iframe in hidden window');
michael@0: let hiddenFrames = require('sdk/frame/hidden-frame');
michael@0: let hiddenFrame = hiddenFrames.add(hiddenFrames.HiddenFrame({
michael@0: onReady: function () {
michael@0: let element = this.element;
michael@0: element.addEventListener('DOMContentLoaded', function onload() {
michael@0: element.removeEventListener('DOMContentLoaded', onload, false);
michael@0: hiddenFrames.remove(hiddenFrame);
michael@0:
michael@0: if (!xulApp.is("Fennec")) {
michael@0: openToplevelWindow();
michael@0: }
michael@0: else {
michael@0: openBrowserIframe();
michael@0: }
michael@0: }, false);
michael@0: element.setAttribute('src', 'data:text/html;charset=utf-8,foo');
michael@0: }
michael@0: }));
michael@0: }
michael@0:
michael@0: function openToplevelWindow() {
michael@0: assert.pass('Open toplevel window');
michael@0: let win = open('data:text/html;charset=utf-8,bar');
michael@0: win.addEventListener('DOMContentLoaded', function onload() {
michael@0: win.removeEventListener('DOMContentLoaded', onload, false);
michael@0: win.close();
michael@0: openBrowserIframe();
michael@0: }, false);
michael@0: }
michael@0:
michael@0: function openBrowserIframe() {
michael@0: assert.pass('Open iframe in browser window');
michael@0: let window = require('sdk/deprecated/window-utils').activeBrowserWindow;
michael@0: let document = window.document;
michael@0: let iframe = document.createElement('iframe');
michael@0: iframe.setAttribute('type', 'content');
michael@0: iframe.setAttribute('src', 'data:text/html;charset=utf-8,foobar');
michael@0: iframe.addEventListener('DOMContentLoaded', function onload() {
michael@0: iframe.removeEventListener('DOMContentLoaded', onload, false);
michael@0: iframe.parentNode.removeChild(iframe);
michael@0: openTabWithIframes();
michael@0: }, false);
michael@0: document.documentElement.appendChild(iframe);
michael@0: }
michael@0:
michael@0: // Only these three documents will be accepted by the page-mod
michael@0: function openTabWithIframes() {
michael@0: assert.pass('Open iframes in a tab');
michael@0: let subContent = ''
michael@0: let content = '';
michael@0: require('sdk/tabs').open({
michael@0: url: 'data:text/html;charset=utf-8,' + encodeURIComponent(content),
michael@0: onOpen: function onOpen(tab) {
michael@0: openedTab = tab;
michael@0: }
michael@0: });
michael@0: }
michael@0:
michael@0: openHiddenFrame();
michael@0: };
michael@0:
michael@0: exports['test111 attachTo [top]'] = function(assert, done) {
michael@0: let { PageMod } = require('sdk/page-mod');
michael@0:
michael@0: let subContent = ''
michael@0: let content = '';
michael@0: let topDocumentURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(content)
michael@0:
michael@0: let workerCount = 0;
michael@0:
michael@0: let mod = PageMod({
michael@0: include: 'data:text/html*',
michael@0: contentScriptWhen: 'start',
michael@0: contentScript: 'self.postMessage(document.location.href);',
michael@0: attachTo: ['top'],
michael@0: onAttach: function onAttach(worker) {
michael@0: if (++workerCount == 1) {
michael@0: worker.on('message', function (href) {
michael@0: assert.equal(href, topDocumentURL,
michael@0: "worker on top level document only");
michael@0: let tab = worker.tab;
michael@0: worker.destroy();
michael@0: mod.destroy();
michael@0: tab.close(done);
michael@0: });
michael@0: }
michael@0: else {
michael@0: assert.fail('page-mod attached to a non-top document');
michael@0: }
michael@0: }
michael@0: });
michael@0:
michael@0: require('sdk/tabs').open(topDocumentURL);
michael@0: };
michael@0:
michael@0: exports['test111 attachTo [frame]'] = function(assert, done) {
michael@0: let { PageMod } = require('sdk/page-mod');
michael@0:
michael@0: let subFrameURL = 'data:text/html;charset=utf-8,subframe';
michael@0: let subContent = '';
michael@0: let frameURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(subContent);
michael@0: let content = '';
michael@0: let topDocumentURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(content)
michael@0:
michael@0: let workerCount = 0, messageCount = 0;
michael@0:
michael@0: function onMessage(href) {
michael@0: if (href == frameURL)
michael@0: assert.pass("worker on first frame");
michael@0: else if (href == subFrameURL)
michael@0: assert.pass("worker on second frame");
michael@0: else
michael@0: assert.fail("worker on unexpected document: " + href);
michael@0: this.destroy();
michael@0: if (++messageCount == 2) {
michael@0: mod.destroy();
michael@0: require('sdk/tabs').activeTab.close(done);
michael@0: }
michael@0: }
michael@0: let mod = PageMod({
michael@0: include: 'data:text/html*',
michael@0: contentScriptWhen: 'start',
michael@0: contentScript: 'self.postMessage(document.location.href);',
michael@0: attachTo: ['frame'],
michael@0: onAttach: function onAttach(worker) {
michael@0: if (++workerCount <= 2) {
michael@0: worker.on('message', onMessage);
michael@0: }
michael@0: else {
michael@0: assert.fail('page-mod attached to a non-frame document');
michael@0: }
michael@0: }
michael@0: });
michael@0:
michael@0: require('sdk/tabs').open(topDocumentURL);
michael@0: };
michael@0:
michael@0: exports.testContentScriptOptionsOption = function(assert, done) {
michael@0: let callbackDone = null;
michael@0: testPageMod(assert, done, "about:", [{
michael@0: include: "about:*",
michael@0: contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
michael@0: contentScriptWhen: "end",
michael@0: contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
michael@0: onAttach: function(worker) {
michael@0: worker.on('message', function(msg) {
michael@0: assert.equal( msg[0], 'undefined', 'functions are stripped from contentScriptOptions' );
michael@0: assert.equal( typeof msg[1], 'object', 'object as contentScriptOptions' );
michael@0: assert.equal( msg[1].a, true, 'boolean in contentScriptOptions' );
michael@0: assert.equal( msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions' );
michael@0: assert.equal( msg[1].c, 'string', 'string in contentScriptOptions' );
michael@0: callbackDone();
michael@0: });
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: callbackDone = done;
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testPageModCss = function(assert, done) {
michael@0: let [pageMod] = testPageMod(assert, done,
michael@0: 'data:text/html;charset=utf-8,css test
', [{
michael@0: include: ["*", "data:*"],
michael@0: contentStyle: "div { height: 100px; }",
michael@0: contentStyleFile: data.url("css-include-file.css")
michael@0: }],
michael@0: function(win, done) {
michael@0: let div = win.document.querySelector("div");
michael@0: assert.equal(
michael@0: div.clientHeight,
michael@0: 100,
michael@0: "PageMod contentStyle worked"
michael@0: );
michael@0: assert.equal(
michael@0: div.offsetHeight,
michael@0: 120,
michael@0: "PageMod contentStyleFile worked"
michael@0: );
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testPageModCssList = function(assert, done) {
michael@0: let [pageMod] = testPageMod(assert, done,
michael@0: 'data:text/html;charset=utf-8,css test
', [{
michael@0: include: "data:*",
michael@0: contentStyleFile: [
michael@0: // Highlight evaluation order in this list
michael@0: "data:text/css;charset=utf-8,div { border: 1px solid black; }",
michael@0: "data:text/css;charset=utf-8,div { border: 10px solid black; }",
michael@0: // Highlight evaluation order between contentStylesheet & contentStylesheetFile
michael@0: "data:text/css;charset=utf-8s,div { height: 1000px; }",
michael@0: // Highlight precedence between the author and user style sheet
michael@0: "data:text/css;charset=utf-8,div { width: 200px; max-width: 640px!important}",
michael@0: ],
michael@0: contentStyle: [
michael@0: "div { height: 10px; }",
michael@0: "div { height: 100px; }"
michael@0: ]
michael@0: }],
michael@0: function(win, done) {
michael@0: let div = win.document.querySelector("div"),
michael@0: style = win.getComputedStyle(div);
michael@0:
michael@0: assert.equal(
michael@0: div.clientHeight,
michael@0: 100,
michael@0: "PageMod contentStyle list works and is evaluated after contentStyleFile"
michael@0: );
michael@0:
michael@0: assert.equal(
michael@0: div.offsetHeight,
michael@0: 120,
michael@0: "PageMod contentStyleFile list works"
michael@0: );
michael@0:
michael@0: assert.equal(
michael@0: style.width,
michael@0: "320px",
michael@0: "PageMod add-on author/page author style sheet precedence works"
michael@0: );
michael@0:
michael@0: assert.equal(
michael@0: style.maxWidth,
michael@0: "480px",
michael@0: "PageMod add-on author/page author style sheet precedence with !important works"
michael@0: );
michael@0:
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testPageModCssDestroy = function(assert, done) {
michael@0: let [pageMod] = testPageMod(assert, done,
michael@0: 'data:text/html;charset=utf-8,css test
', [{
michael@0: include: "data:*",
michael@0: contentStyle: "div { width: 100px!important; }"
michael@0: }],
michael@0:
michael@0: function(win, done) {
michael@0: let div = win.document.querySelector("div"),
michael@0: style = win.getComputedStyle(div);
michael@0:
michael@0: assert.equal(
michael@0: style.width,
michael@0: "100px",
michael@0: "PageMod contentStyle worked"
michael@0: );
michael@0:
michael@0: pageMod.destroy();
michael@0: assert.equal(
michael@0: style.width,
michael@0: "200px",
michael@0: "PageMod contentStyle is removed after destroy"
michael@0: );
michael@0:
michael@0: done();
michael@0:
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testPageModCssAutomaticDestroy = function(assert, done) {
michael@0: let loader = Loader(module);
michael@0:
michael@0: let pageMod = loader.require("sdk/page-mod").PageMod({
michael@0: include: "data:*",
michael@0: contentStyle: "div { width: 100px!important; }"
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: "data:text/html;charset=utf-8,css test
",
michael@0:
michael@0: onReady: function onReady(tab) {
michael@0: let browserWindow = getMostRecentBrowserWindow();
michael@0: let win = getTabContentWindow(getActiveTab(browserWindow));
michael@0:
michael@0: let div = win.document.querySelector("div");
michael@0: let style = win.getComputedStyle(div);
michael@0:
michael@0: assert.equal(
michael@0: style.width,
michael@0: "100px",
michael@0: "PageMod contentStyle worked"
michael@0: );
michael@0:
michael@0: loader.unload();
michael@0:
michael@0: assert.equal(
michael@0: style.width,
michael@0: "200px",
michael@0: "PageMod contentStyle is removed after loader's unload"
michael@0: );
michael@0:
michael@0: tab.close(done);
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0:
michael@0: exports.testPageModTimeout = function(assert, done) {
michael@0: let tab = null
michael@0: let loader = Loader(module);
michael@0: let { PageMod } = loader.require("sdk/page-mod");
michael@0:
michael@0: let mod = PageMod({
michael@0: include: "data:*",
michael@0: contentScript: Isolate(function() {
michael@0: var id = setTimeout(function() {
michael@0: self.port.emit("fired", id)
michael@0: }, 10)
michael@0: self.port.emit("scheduled", id);
michael@0: }),
michael@0: onAttach: function(worker) {
michael@0: worker.port.on("scheduled", function(id) {
michael@0: assert.pass("timer was scheduled")
michael@0: worker.port.on("fired", function(data) {
michael@0: assert.equal(id, data, "timer was fired")
michael@0: tab.close(function() {
michael@0: worker.destroy()
michael@0: loader.unload()
michael@0: done()
michael@0: });
michael@0: })
michael@0: })
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: "data:text/html;charset=utf-8,timeout",
michael@0: onReady: function($) { tab = $ }
michael@0: })
michael@0: }
michael@0:
michael@0:
michael@0: exports.testPageModcancelTimeout = function(assert, done) {
michael@0: let tab = null
michael@0: let loader = Loader(module);
michael@0: let { PageMod } = loader.require("sdk/page-mod");
michael@0:
michael@0: let mod = PageMod({
michael@0: include: "data:*",
michael@0: contentScript: Isolate(function() {
michael@0: var id1 = setTimeout(function() {
michael@0: self.port.emit("failed")
michael@0: }, 10)
michael@0: var id2 = setTimeout(function() {
michael@0: self.port.emit("timeout")
michael@0: }, 100)
michael@0: clearTimeout(id1)
michael@0: }),
michael@0: onAttach: function(worker) {
michael@0: worker.port.on("failed", function() {
michael@0: assert.fail("cancelled timeout fired")
michael@0: })
michael@0: worker.port.on("timeout", function(id) {
michael@0: assert.pass("timer was scheduled")
michael@0: tab.close(function() {
michael@0: worker.destroy();
michael@0: mod.destroy();
michael@0: loader.unload();
michael@0: done();
michael@0: });
michael@0: })
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: "data:text/html;charset=utf-8,cancell timeout",
michael@0: onReady: function($) { tab = $ }
michael@0: })
michael@0: }
michael@0:
michael@0: exports.testExistingOnFrames = function(assert, done) {
michael@0: let subFrameURL = 'data:text/html;charset=utf-8,testExistingOnFrames-sub-frame';
michael@0: let subIFrame = ''
michael@0: let iFrameURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(subIFrame)
michael@0: let iFrame = '';
michael@0: let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iFrame);
michael@0:
michael@0: // we want all urls related to the test here, and not just the iframe urls
michael@0: // because we need to fail if the test is applied to the top window url.
michael@0: let urls = [url, iFrameURL, subFrameURL];
michael@0:
michael@0: let counter = 0;
michael@0: let tab = openTab(getMostRecentBrowserWindow(), url);
michael@0: let window = getTabContentWindow(tab);
michael@0:
michael@0: function wait4Iframes() {
michael@0: if (window.document.readyState != "complete" ||
michael@0: getFrames(window).length != 2) {
michael@0: return;
michael@0: }
michael@0:
michael@0: let pagemodOnExisting = PageMod({
michael@0: include: ["*", "data:*"],
michael@0: attachTo: ["existing", "frame"],
michael@0: contentScriptWhen: 'ready',
michael@0: onAttach: function(worker) {
michael@0: // need to ignore urls that are not part of the test, because other
michael@0: // tests are not closing their tabs when they complete..
michael@0: if (urls.indexOf(worker.url) == -1)
michael@0: return;
michael@0:
michael@0: assert.notEqual(url,
michael@0: worker.url,
michael@0: 'worker should not be attached to the top window');
michael@0:
michael@0: if (++counter < 2) {
michael@0: // we can rely on this order in this case because we are sure that
michael@0: // the frames being tested have completely loaded
michael@0: assert.equal(iFrameURL, worker.url, '1st attach is for top frame');
michael@0: }
michael@0: else if (counter > 2) {
michael@0: assert.fail('applied page mod too many times');
michael@0: }
michael@0: else {
michael@0: assert.equal(subFrameURL, worker.url, '2nd attach is for sub frame');
michael@0: // need timeout because onAttach is called before the constructor returns
michael@0: setTimeout(function() {
michael@0: pagemodOnExisting.destroy();
michael@0: pagemodOffExisting.destroy();
michael@0: closeTab(tab);
michael@0: done();
michael@0: }, 0);
michael@0: }
michael@0: }
michael@0: });
michael@0:
michael@0: let pagemodOffExisting = PageMod({
michael@0: include: ["*", "data:*"],
michael@0: attachTo: ["frame"],
michael@0: contentScriptWhen: 'ready',
michael@0: onAttach: function(mod) {
michael@0: assert.fail('pagemodOffExisting page-mod should not have been attached');
michael@0: }
michael@0: });
michael@0: }
michael@0:
michael@0: window.addEventListener("load", wait4Iframes, false);
michael@0: };
michael@0:
michael@0: exports.testIFramePostMessage = function(assert, done) {
michael@0: let count = 0;
michael@0:
michael@0: tabs.open({
michael@0: url: data.url("test-iframe.html"),
michael@0: onReady: function(tab) {
michael@0: var worker = tab.attach({
michael@0: contentScriptFile: data.url('test-iframe.js'),
michael@0: contentScript: 'var iframePath = \'' + data.url('test-iframe-postmessage.html') + '\'',
michael@0: onMessage: function(msg) {
michael@0: assert.equal(++count, 1);
michael@0: assert.equal(msg.first, 'a string');
michael@0: assert.ok(msg.second[1], "array");
michael@0: assert.equal(typeof msg.third, 'object');
michael@0:
michael@0: worker.destroy();
michael@0: tab.close(done);
michael@0: }
michael@0: });
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports.testEvents = function(assert, done) {
michael@0: let content = "";
michael@0: let url = "data:text/html;charset=utf-8," + encodeURIComponent(content);
michael@0: testPageMod(assert, done, url, [{
michael@0: include: "data:*",
michael@0: contentScript: 'new ' + function WorkerScope() {
michael@0: let evt = document.createEvent("Event");
michael@0: evt.initEvent("ContentScriptEvent", true, true);
michael@0: document.body.dispatchEvent(evt);
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: assert.ok(
michael@0: win.receivedEvent,
michael@0: "Content script sent an event and document received it"
michael@0: );
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports["test page-mod on private tab"] = function (assert, done) {
michael@0: let fail = assert.fail.bind(assert);
michael@0:
michael@0: let privateUri = "data:text/html;charset=utf-8," +
michael@0: "";
michael@0: let nonPrivateUri = "data:text/html;charset=utf-8,non-private";
michael@0:
michael@0: let pageMod = new PageMod({
michael@0: include: "data:*",
michael@0: onAttach: function(worker) {
michael@0: if (isTabPBSupported || isWindowPBSupported) {
michael@0: // When PB isn't supported, the page-mod will apply to all document
michael@0: // as all of them will be non-private
michael@0: assert.equal(worker.tab.url,
michael@0: nonPrivateUri,
michael@0: "page-mod should only attach to the non-private tab");
michael@0: }
michael@0:
michael@0: assert.ok(!isPrivate(worker),
michael@0: "The worker is really non-private");
michael@0: assert.ok(!isPrivate(worker.tab),
michael@0: "The document is really non-private");
michael@0: pageMod.destroy();
michael@0:
michael@0: page1.close().
michael@0: then(page2.close).
michael@0: then(done, fail);
michael@0: }
michael@0: });
michael@0:
michael@0: let page1, page2;
michael@0: page1 = openWebpage(privateUri, true);
michael@0: page1.ready.then(function() {
michael@0: page2 = openWebpage(nonPrivateUri, false);
michael@0: }, fail);
michael@0: }
michael@0:
michael@0: exports["test page-mod on private tab in global pb"] = function (assert, done) {
michael@0: if (!isGlobalPBSupported) {
michael@0: assert.pass();
michael@0: return done();
michael@0: }
michael@0:
michael@0: let privateUri = "data:text/html;charset=utf-8," +
michael@0: "";
michael@0:
michael@0: let pageMod = new PageMod({
michael@0: include: privateUri,
michael@0: onAttach: function(worker) {
michael@0: assert.equal(worker.tab.url,
michael@0: privateUri,
michael@0: "page-mod should attach");
michael@0: assert.equal(isPrivateBrowsingSupported,
michael@0: false,
michael@0: "private browsing is not supported");
michael@0: assert.ok(isPrivate(worker),
michael@0: "The worker is really non-private");
michael@0: assert.ok(isPrivate(worker.tab),
michael@0: "The document is really non-private");
michael@0: pageMod.destroy();
michael@0:
michael@0: worker.tab.close(function() {
michael@0: pb.once('stop', function() {
michael@0: assert.pass('global pb stop');
michael@0: done();
michael@0: });
michael@0: pb.deactivate();
michael@0: });
michael@0: }
michael@0: });
michael@0:
michael@0: let page1;
michael@0: pb.once('start', function() {
michael@0: assert.pass('global pb start');
michael@0: tabs.open({ url: privateUri });
michael@0: });
michael@0: pb.activate();
michael@0: }
michael@0:
michael@0: // Bug 699450: Calling worker.tab.close() should not lead to exception
michael@0: exports.testWorkerTabClose = function(assert, done) {
michael@0: let callbackDone;
michael@0: testPageMod(assert, done, "about:", [{
michael@0: include: "about:",
michael@0: contentScript: '',
michael@0: onAttach: function(worker) {
michael@0: assert.pass("The page-mod was attached");
michael@0:
michael@0: worker.tab.close(function () {
michael@0: // On Fennec, tab is completely destroyed right after close event is
michael@0: // dispatch, so we need to wait for the next event loop cycle to
michael@0: // check for tab nulliness.
michael@0: setTimeout(function () {
michael@0: assert.ok(!worker.tab,
michael@0: "worker.tab should be null right after tab.close()");
michael@0: callbackDone();
michael@0: }, 0);
michael@0: });
michael@0: }
michael@0: }],
michael@0: function(win, done) {
michael@0: callbackDone = done;
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testDebugMetadata = function(assert, done) {
michael@0: let dbg = new Debugger;
michael@0: let globalDebuggees = [];
michael@0: dbg.onNewGlobalObject = function(global) {
michael@0: globalDebuggees.push(global);
michael@0: }
michael@0:
michael@0: let mods = testPageMod(assert, done, "about:", [{
michael@0: include: "about:",
michael@0: contentScriptWhen: "start",
michael@0: contentScript: "null;",
michael@0: }], function(win, done) {
michael@0: assert.ok(globalDebuggees.some(function(global) {
michael@0: try {
michael@0: let metadata = Cu.getSandboxMetadata(global.unsafeDereference());
michael@0: return metadata && metadata.addonID && metadata.SDKContentScript &&
michael@0: metadata['inner-window-id'] == getInnerId(win);
michael@0: } catch(e) {
michael@0: // Some of the globals might not be Sandbox instances and thus
michael@0: // will cause getSandboxMetadata to fail.
michael@0: return false;
michael@0: }
michael@0: }), "one of the globals is a content script");
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testDevToolsExtensionsGetContentGlobals = function(assert, done) {
michael@0: let mods = testPageMod(assert, done, "about:", [{
michael@0: include: "about:",
michael@0: contentScriptWhen: "start",
michael@0: contentScript: "null;",
michael@0: }], function(win, done) {
michael@0: assert.equal(gDevToolsExtensions.getContentGlobals({ 'inner-window-id': getInnerId(win) }).length, 1);
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: exports.testDetachOnDestroy = function(assert, done) {
michael@0: let tab;
michael@0: const TEST_URL = 'data:text/html;charset=utf-8,detach';
michael@0: const loader = Loader(module);
michael@0: const { PageMod } = loader.require('sdk/page-mod');
michael@0:
michael@0: let mod1 = PageMod({
michael@0: include: TEST_URL,
michael@0: contentScript: Isolate(function() {
michael@0: self.port.on('detach', function(reason) {
michael@0: window.document.body.innerHTML += '!' + reason;
michael@0: });
michael@0: }),
michael@0: onAttach: worker => {
michael@0: assert.pass('attach[1] happened');
michael@0:
michael@0: worker.on('detach', _ => setTimeout(_ => {
michael@0: assert.pass('detach happened');
michael@0:
michael@0: let mod2 = PageMod({
michael@0: attachTo: [ 'existing', 'top' ],
michael@0: include: TEST_URL,
michael@0: contentScript: Isolate(function() {
michael@0: self.port.on('test', _ => {
michael@0: self.port.emit('result', { result: window.document.body.innerHTML});
michael@0: });
michael@0: }),
michael@0: onAttach: worker => {
michael@0: assert.pass('attach[2] happened');
michael@0: worker.port.once('result', ({ result }) => {
michael@0: assert.equal(result, 'detach!', 'the body.innerHTML is as expected');
michael@0: mod1.destroy();
michael@0: mod2.destroy();
michael@0: loader.unload();
michael@0: tab.close(done);
michael@0: });
michael@0: worker.port.emit('test');
michael@0: }
michael@0: });
michael@0: }));
michael@0:
michael@0: worker.destroy();
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: TEST_URL,
michael@0: onOpen: t => tab = t
michael@0: })
michael@0: }
michael@0:
michael@0: exports.testDetachOnUnload = function(assert, done) {
michael@0: let tab;
michael@0: const TEST_URL = 'data:text/html;charset=utf-8,detach';
michael@0: const loader = Loader(module);
michael@0: const { PageMod } = loader.require('sdk/page-mod');
michael@0:
michael@0: let mod1 = PageMod({
michael@0: include: TEST_URL,
michael@0: contentScript: Isolate(function() {
michael@0: self.port.on('detach', function(reason) {
michael@0: window.document.body.innerHTML += '!' + reason;
michael@0: });
michael@0: }),
michael@0: onAttach: worker => {
michael@0: assert.pass('attach[1] happened');
michael@0:
michael@0: worker.on('detach', _ => setTimeout(_ => {
michael@0: assert.pass('detach happened');
michael@0:
michael@0: let mod2 = require('sdk/page-mod').PageMod({
michael@0: attachTo: [ 'existing', 'top' ],
michael@0: include: TEST_URL,
michael@0: contentScript: Isolate(function() {
michael@0: self.port.on('test', _ => {
michael@0: self.port.emit('result', { result: window.document.body.innerHTML});
michael@0: });
michael@0: }),
michael@0: onAttach: worker => {
michael@0: assert.pass('attach[2] happened');
michael@0: worker.port.once('result', ({ result }) => {
michael@0: assert.equal(result, 'detach!shutdown', 'the body.innerHTML is as expected');
michael@0: mod2.destroy();
michael@0: tab.close(done);
michael@0: });
michael@0: worker.port.emit('test');
michael@0: }
michael@0: });
michael@0: }));
michael@0:
michael@0: loader.unload('shutdown');
michael@0: }
michael@0: });
michael@0:
michael@0: tabs.open({
michael@0: url: TEST_URL,
michael@0: onOpen: t => tab = t
michael@0: })
michael@0: }
michael@0:
michael@0: exports.testConsole = function(assert, done) {
michael@0: let innerID;
michael@0: const TEST_URL = 'data:text/html;charset=utf-8,console';
michael@0: const { loader } = LoaderWithHookedConsole(module, onMessage);
michael@0: const { PageMod } = loader.require('sdk/page-mod');
michael@0: const system = require("sdk/system/events");
michael@0:
michael@0: let seenMessage = false;
michael@0: function onMessage(type, msg, msgID) {
michael@0: seenMessage = true;
michael@0: innerID = msgID;
michael@0: }
michael@0:
michael@0: let mod = PageMod({
michael@0: include: TEST_URL,
michael@0: contentScriptWhen: "ready",
michael@0: contentScript: Isolate(function() {
michael@0: console.log("Hello from the page mod");
michael@0: self.port.emit("done");
michael@0: }),
michael@0: onAttach: function(worker) {
michael@0: worker.port.on("done", function() {
michael@0: let window = getTabContentWindow(tab);
michael@0: let id = getInnerId(window);
michael@0: assert.ok(seenMessage, "Should have seen the console message");
michael@0: assert.equal(innerID, id, "Should have seen the right inner ID");
michael@0: closeTab(tab);
michael@0: done();
michael@0: });
michael@0: },
michael@0: });
michael@0:
michael@0: let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
michael@0: }
michael@0:
michael@0: exports.testSyntaxErrorInContentScript = function(assert, done) {
michael@0: const url = "data:text/html;charset=utf-8,testSyntaxErrorInContentScript";
michael@0: let hitError = null;
michael@0: let attached = false;
michael@0:
michael@0: testPageMod(assert, done, url, [{
michael@0: include: url,
michael@0: contentScript: 'console.log(23',
michael@0:
michael@0: onAttach: function() {
michael@0: attached = true;
michael@0: },
michael@0:
michael@0: onError: function(e) {
michael@0: hitError = e;
michael@0: }
michael@0: }],
michael@0:
michael@0: function(win, done) {
michael@0: assert.ok(attached, "The worker was attached.");
michael@0: assert.notStrictEqual(hitError, null, "The syntax error was reported.");
michael@0: if (hitError)
michael@0: assert.equal(hitError.name, "SyntaxError", "The error thrown should be a SyntaxError");
michael@0: done();
michael@0: }
michael@0: );
michael@0: };
michael@0:
michael@0: require('sdk/test').run(exports);