test-browser-events.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. module.metadata = {
  6. engines: {
  7. "Firefox": "*"
  8. }
  9. };
  10. const { Loader } = require("sdk/test/loader");
  11. const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils");
  12. const { setTimeout } = require("sdk/timers");
  13. exports["test browser events"] = function(assert, done) {
  14. let loader = Loader(module);
  15. let { events } = loader.require("sdk/browser/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 === "load") window.close();
  21. if (e.type === "close") {
  22. // Unload the module so that all listeners set by observer are removed.
  23. let [ ready, load, close ] = actual;
  24. assert.equal(ready.type, "DOMContentLoaded");
  25. assert.equal(ready.target, window, "window ready");
  26. assert.equal(load.type, "load");
  27. assert.equal(load.target, window, "window load");
  28. assert.equal(close.type, "close");
  29. assert.equal(close.target, window, "window load");
  30. // Note: If window is closed right after this GC won't have time
  31. // to claim loader and there for this listener, there for it's safer
  32. // to remove listener.
  33. off(events, "data", handler);
  34. loader.unload();
  35. done();
  36. }
  37. });
  38. // Open window and close it to trigger observers.
  39. let window = open();
  40. };
  41. exports["test browser events ignore other wins"] = function(assert, done) {
  42. let loader = Loader(module);
  43. let { events: windowEvents } = loader.require("sdk/window/events");
  44. let { events: browserEvents } = loader.require("sdk/browser/events");
  45. let { on, off } = loader.require("sdk/event/core");
  46. let actualBrowser = [];
  47. let actualWindow = [];
  48. function browserEventHandler(e) actualBrowser.push(e)
  49. on(browserEvents, "data", browserEventHandler);
  50. on(windowEvents, "data", function handler(e) {
  51. actualWindow.push(e);
  52. // Delay close so that if "load" is also emitted on `browserEvents`
  53. // `browserEventHandler` will be invoked.
  54. if (e.type === "load") setTimeout(window.close);
  55. if (e.type === "close") {
  56. assert.deepEqual(actualBrowser, [], "browser events were not triggered");
  57. let [ open, ready, load, close ] = actualWindow;
  58. assert.equal(open.type, "open");
  59. assert.equal(open.target, window, "window is open");
  60. assert.equal(ready.type, "DOMContentLoaded");
  61. assert.equal(ready.target, window, "window ready");
  62. assert.equal(load.type, "load");
  63. assert.equal(load.target, window, "window load");
  64. assert.equal(close.type, "close");
  65. assert.equal(close.target, window, "window load");
  66. // Note: If window is closed right after this GC won't have time
  67. // to claim loader and there for this listener, there for it's safer
  68. // to remove listener.
  69. off(windowEvents, "data", handler);
  70. off(browserEvents, "data", browserEventHandler);
  71. loader.unload();
  72. done();
  73. }
  74. });
  75. // Open window and close it to trigger observers.
  76. let window = open("data:text/html,not a browser");
  77. };
  78. require("test").run(exports);