addon-sdk/source/test/pagemod-test-helpers.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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
michael@0 5 "use strict";
michael@0 6
michael@0 7 const {Cc,Ci} = require("chrome");
michael@0 8 const timer = require("sdk/timers");
michael@0 9 const xulApp = require("sdk/system/xul-app");
michael@0 10 const { Loader } = require("sdk/test/loader");
michael@0 11 const { openTab, getBrowserForTab, closeTab } = require("sdk/tabs/utils");
michael@0 12
michael@0 13 /**
michael@0 14 * A helper function that creates a PageMod, then opens the specified URL
michael@0 15 * and checks the effect of the page mod on 'onload' event via testCallback.
michael@0 16 */
michael@0 17 exports.testPageMod = function testPageMod(assert, done, testURL, pageModOptions,
michael@0 18 testCallback, timeout) {
michael@0 19 if (!xulApp.versionInRange(xulApp.platformVersion, "1.9.3a3", "*") &&
michael@0 20 !xulApp.versionInRange(xulApp.platformVersion, "1.9.2.7", "1.9.2.*")) {
michael@0 21 assert.pass("Note: not testing PageMod, as it doesn't work on this platform version");
michael@0 22 return null;
michael@0 23 }
michael@0 24
michael@0 25 var wm = Cc['@mozilla.org/appshell/window-mediator;1']
michael@0 26 .getService(Ci.nsIWindowMediator);
michael@0 27 var browserWindow = wm.getMostRecentWindow("navigator:browser");
michael@0 28 if (!browserWindow) {
michael@0 29 assert.pass("page-mod tests: could not find the browser window, so " +
michael@0 30 "will not run. Use -a firefox to run the pagemod tests.")
michael@0 31 return null;
michael@0 32 }
michael@0 33
michael@0 34 let loader = Loader(module);
michael@0 35 let pageMod = loader.require("sdk/page-mod");
michael@0 36
michael@0 37 var pageMods = [new pageMod.PageMod(opts) for each(opts in pageModOptions)];
michael@0 38
michael@0 39 let newTab = openTab(browserWindow, testURL, {
michael@0 40 inBackground: false
michael@0 41 });
michael@0 42 var b = getBrowserForTab(newTab);
michael@0 43
michael@0 44 function onPageLoad() {
michael@0 45 b.removeEventListener("load", onPageLoad, true);
michael@0 46 // Delay callback execute as page-mod content scripts may be executed on
michael@0 47 // load event. So page-mod actions may not be already done.
michael@0 48 // If we delay even more contentScriptWhen:'end', we may want to modify
michael@0 49 // this code again.
michael@0 50 timer.setTimeout(testCallback, 0,
michael@0 51 b.contentWindow.wrappedJSObject,
michael@0 52 function () {
michael@0 53 pageMods.forEach(function(mod) mod.destroy());
michael@0 54 // XXX leaks reported if we don't close the tab?
michael@0 55 closeTab(newTab);
michael@0 56 loader.unload();
michael@0 57 done();
michael@0 58 }
michael@0 59 );
michael@0 60 }
michael@0 61 b.addEventListener("load", onPageLoad, true);
michael@0 62
michael@0 63 return pageMods;
michael@0 64 }
michael@0 65
michael@0 66 /**
michael@0 67 * helper function that creates a PageMod and calls back the appropriate handler
michael@0 68 * based on the value of document.readyState at the time contentScript is attached
michael@0 69 */
michael@0 70 exports.handleReadyState = function(url, contentScriptWhen, callbacks) {
michael@0 71 const { PageMod } = Loader(module).require('sdk/page-mod');
michael@0 72
michael@0 73 let pagemod = PageMod({
michael@0 74 include: url,
michael@0 75 attachTo: ['existing', 'top'],
michael@0 76 contentScriptWhen: contentScriptWhen,
michael@0 77 contentScript: "self.postMessage(document.readyState)",
michael@0 78 onAttach: worker => {
michael@0 79 let { tab } = worker;
michael@0 80 worker.on('message', readyState => {
michael@0 81 pagemod.destroy();
michael@0 82 // generate event name from `readyState`, e.g. `"loading"` becomes `onLoading`.
michael@0 83 let type = 'on' + readyState[0].toUpperCase() + readyState.substr(1);
michael@0 84
michael@0 85 if (type in callbacks)
michael@0 86 callbacks[type](tab);
michael@0 87 })
michael@0 88 }
michael@0 89 });
michael@0 90 }

mercurial