1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/pagemod-test-helpers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 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 + 1.8 +"use strict"; 1.9 + 1.10 +const {Cc,Ci} = require("chrome"); 1.11 +const timer = require("sdk/timers"); 1.12 +const xulApp = require("sdk/system/xul-app"); 1.13 +const { Loader } = require("sdk/test/loader"); 1.14 +const { openTab, getBrowserForTab, closeTab } = require("sdk/tabs/utils"); 1.15 + 1.16 +/** 1.17 + * A helper function that creates a PageMod, then opens the specified URL 1.18 + * and checks the effect of the page mod on 'onload' event via testCallback. 1.19 + */ 1.20 +exports.testPageMod = function testPageMod(assert, done, testURL, pageModOptions, 1.21 + testCallback, timeout) { 1.22 + if (!xulApp.versionInRange(xulApp.platformVersion, "1.9.3a3", "*") && 1.23 + !xulApp.versionInRange(xulApp.platformVersion, "1.9.2.7", "1.9.2.*")) { 1.24 + assert.pass("Note: not testing PageMod, as it doesn't work on this platform version"); 1.25 + return null; 1.26 + } 1.27 + 1.28 + var wm = Cc['@mozilla.org/appshell/window-mediator;1'] 1.29 + .getService(Ci.nsIWindowMediator); 1.30 + var browserWindow = wm.getMostRecentWindow("navigator:browser"); 1.31 + if (!browserWindow) { 1.32 + assert.pass("page-mod tests: could not find the browser window, so " + 1.33 + "will not run. Use -a firefox to run the pagemod tests.") 1.34 + return null; 1.35 + } 1.36 + 1.37 + let loader = Loader(module); 1.38 + let pageMod = loader.require("sdk/page-mod"); 1.39 + 1.40 + var pageMods = [new pageMod.PageMod(opts) for each(opts in pageModOptions)]; 1.41 + 1.42 + let newTab = openTab(browserWindow, testURL, { 1.43 + inBackground: false 1.44 + }); 1.45 + var b = getBrowserForTab(newTab); 1.46 + 1.47 + function onPageLoad() { 1.48 + b.removeEventListener("load", onPageLoad, true); 1.49 + // Delay callback execute as page-mod content scripts may be executed on 1.50 + // load event. So page-mod actions may not be already done. 1.51 + // If we delay even more contentScriptWhen:'end', we may want to modify 1.52 + // this code again. 1.53 + timer.setTimeout(testCallback, 0, 1.54 + b.contentWindow.wrappedJSObject, 1.55 + function () { 1.56 + pageMods.forEach(function(mod) mod.destroy()); 1.57 + // XXX leaks reported if we don't close the tab? 1.58 + closeTab(newTab); 1.59 + loader.unload(); 1.60 + done(); 1.61 + } 1.62 + ); 1.63 + } 1.64 + b.addEventListener("load", onPageLoad, true); 1.65 + 1.66 + return pageMods; 1.67 +} 1.68 + 1.69 +/** 1.70 + * helper function that creates a PageMod and calls back the appropriate handler 1.71 + * based on the value of document.readyState at the time contentScript is attached 1.72 + */ 1.73 +exports.handleReadyState = function(url, contentScriptWhen, callbacks) { 1.74 + const { PageMod } = Loader(module).require('sdk/page-mod'); 1.75 + 1.76 + let pagemod = PageMod({ 1.77 + include: url, 1.78 + attachTo: ['existing', 'top'], 1.79 + contentScriptWhen: contentScriptWhen, 1.80 + contentScript: "self.postMessage(document.readyState)", 1.81 + onAttach: worker => { 1.82 + let { tab } = worker; 1.83 + worker.on('message', readyState => { 1.84 + pagemod.destroy(); 1.85 + // generate event name from `readyState`, e.g. `"loading"` becomes `onLoading`. 1.86 + let type = 'on' + readyState[0].toUpperCase() + readyState.substr(1); 1.87 + 1.88 + if (type in callbacks) 1.89 + callbacks[type](tab); 1.90 + }) 1.91 + } 1.92 + }); 1.93 +}