test-window-observer.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 { Loader } = require("sdk/test/loader");
  6. const { open, close } = require("sdk/window/helpers");
  7. const { browserWindows: windows } = require("sdk/windows");
  8. const { isBrowser } = require('sdk/window/utils');
  9. const app = require("sdk/system/xul-app");
  10. exports["test unload window observer"] = function(assert, done) {
  11. // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
  12. let loader = Loader(module);
  13. let observer = loader.require("sdk/windows/observer").observer;
  14. let opened = 0;
  15. let closed = 0;
  16. let windowsOpen = windows.length;
  17. observer.on("open", onOpen);
  18. observer.on("close", onClose);
  19. // On Fennec, only test that the module does not throw an error
  20. if (app.is("Fennec")) {
  21. assert.pass("Windows observer did not throw on Fennec");
  22. return cleanUp();
  23. }
  24. // Open window and close it to trigger observers.
  25. open().
  26. then(close).
  27. then(loader.unload).
  28. then(open).
  29. then(close).
  30. then(function() {
  31. // Enqueuing asserts to make sure that assertion is not performed early.
  32. assert.equal(1, opened, "observer open was called before unload only");
  33. assert.equal(windowsOpen + 1, closed, "observer close was called before unload only");
  34. }).
  35. then(cleanUp, assert.fail);
  36. function cleanUp () {
  37. observer.removeListener("open", onOpen);
  38. observer.removeListener("close", onClose);
  39. done();
  40. }
  41. function onOpen(window) {
  42. // Ignoring non-browser windows
  43. if (isBrowser(window))
  44. opened++;
  45. }
  46. function onClose(window) {
  47. // Ignore non-browser windows & already opened `activeWindow` (unload will
  48. // emit close on it even though it is not actually closed).
  49. if (isBrowser(window))
  50. closed++;
  51. }
  52. };
  53. require("sdk/test").run(exports);