pagemod-test-helpers.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {Cc,Ci} = require("chrome");
  6. const timer = require("sdk/timers");
  7. const xulApp = require("sdk/system/xul-app");
  8. const { Loader } = require("sdk/test/loader");
  9. const { openTab, getBrowserForTab, closeTab } = require("sdk/tabs/utils");
  10. /**
  11. * A helper function that creates a PageMod, then opens the specified URL
  12. * and checks the effect of the page mod on 'onload' event via testCallback.
  13. */
  14. exports.testPageMod = function testPageMod(assert, done, testURL, pageModOptions,
  15. testCallback, timeout) {
  16. if (!xulApp.versionInRange(xulApp.platformVersion, "1.9.3a3", "*") &&
  17. !xulApp.versionInRange(xulApp.platformVersion, "1.9.2.7", "1.9.2.*")) {
  18. assert.pass("Note: not testing PageMod, as it doesn't work on this platform version");
  19. return null;
  20. }
  21. var wm = Cc['@mozilla.org/appshell/window-mediator;1']
  22. .getService(Ci.nsIWindowMediator);
  23. var browserWindow = wm.getMostRecentWindow("navigator:browser");
  24. if (!browserWindow) {
  25. assert.pass("page-mod tests: could not find the browser window, so " +
  26. "will not run. Use -a firefox to run the pagemod tests.")
  27. return null;
  28. }
  29. let loader = Loader(module);
  30. let pageMod = loader.require("sdk/page-mod");
  31. var pageMods = [new pageMod.PageMod(opts) for each(opts in pageModOptions)];
  32. let newTab = openTab(browserWindow, testURL, {
  33. inBackground: false
  34. });
  35. var b = getBrowserForTab(newTab);
  36. function onPageLoad() {
  37. b.removeEventListener("load", onPageLoad, true);
  38. // Delay callback execute as page-mod content scripts may be executed on
  39. // load event. So page-mod actions may not be already done.
  40. // If we delay even more contentScriptWhen:'end', we may want to modify
  41. // this code again.
  42. timer.setTimeout(testCallback, 0,
  43. b.contentWindow.wrappedJSObject,
  44. function () {
  45. pageMods.forEach(function(mod) mod.destroy());
  46. // XXX leaks reported if we don't close the tab?
  47. closeTab(newTab);
  48. loader.unload();
  49. done();
  50. }
  51. );
  52. }
  53. b.addEventListener("load", onPageLoad, true);
  54. return pageMods;
  55. }