test-window-events.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // Opening new windows in Fennec causes issues
  6. module.metadata = {
  7. engines: {
  8. 'Firefox': '*'
  9. }
  10. };
  11. const { Loader } = require("sdk/test/loader");
  12. const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils");
  13. exports["test browser events"] = function(assert, done) {
  14. let loader = Loader(module);
  15. let { events } = loader.require("sdk/window/events");
  16. let { on, off } = loader.require("sdk/event/core");
  17. let actual = [];
  18. on(events, "data", function handler(e) {
  19. actual.push(e);
  20. if (e.type === "open") {
  21. assert.pass("window open has occured");
  22. }
  23. else if (e.type === "DOMContentLoaded") {
  24. assert.pass("window DOMContentLoaded has occured");
  25. }
  26. else if (e.type === "load") {
  27. assert.pass("window load has occured");
  28. window.close();
  29. }
  30. else if (e.type === "close") {
  31. // confirm the ordering of events
  32. let [ open, ready, load, close ] = actual;
  33. assert.equal(open.type, "open")
  34. assert.equal(open.target, window, "window is open")
  35. assert.equal(ready.type, "DOMContentLoaded")
  36. assert.equal(ready.target, window, "window ready")
  37. assert.equal(load.type, "load")
  38. assert.equal(load.target, window, "window load")
  39. assert.equal(close.type, "close")
  40. assert.equal(close.target, window, "window load")
  41. // Note: If window is closed right after this GC won't have time
  42. // to claim loader and there for this listener. It's better to remove
  43. // remove listener here to avoid race conditions.
  44. off(events, "data", handler);
  45. loader.unload();
  46. done();
  47. }
  48. });
  49. // Open window and close it to trigger observers.
  50. let window = open();
  51. };
  52. require("sdk/test").run(exports);