michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: engines: { michael@0: "Firefox": "*" michael@0: } michael@0: }; michael@0: michael@0: const { Loader } = require("sdk/test/loader"); michael@0: const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils"); michael@0: const { setTimeout } = require("sdk/timers"); michael@0: michael@0: exports["test browser events"] = function(assert, done) { michael@0: let loader = Loader(module); michael@0: let { events } = loader.require("sdk/browser/events"); michael@0: let { on, off } = loader.require("sdk/event/core"); michael@0: let actual = []; michael@0: michael@0: on(events, "data", function handler(e) { michael@0: actual.push(e); michael@0: if (e.type === "load") window.close(); michael@0: if (e.type === "close") { michael@0: // Unload the module so that all listeners set by observer are removed. michael@0: michael@0: let [ ready, load, close ] = actual; michael@0: michael@0: assert.equal(ready.type, "DOMContentLoaded"); michael@0: assert.equal(ready.target, window, "window ready"); michael@0: michael@0: assert.equal(load.type, "load"); michael@0: assert.equal(load.target, window, "window load"); michael@0: michael@0: assert.equal(close.type, "close"); michael@0: assert.equal(close.target, window, "window load"); michael@0: michael@0: // Note: If window is closed right after this GC won't have time michael@0: // to claim loader and there for this listener, there for it's safer michael@0: // to remove listener. michael@0: off(events, "data", handler); michael@0: loader.unload(); michael@0: done(); michael@0: } michael@0: }); michael@0: michael@0: // Open window and close it to trigger observers. michael@0: let window = open(); michael@0: }; michael@0: michael@0: exports["test browser events ignore other wins"] = function(assert, done) { michael@0: let loader = Loader(module); michael@0: let { events: windowEvents } = loader.require("sdk/window/events"); michael@0: let { events: browserEvents } = loader.require("sdk/browser/events"); michael@0: let { on, off } = loader.require("sdk/event/core"); michael@0: let actualBrowser = []; michael@0: let actualWindow = []; michael@0: michael@0: function browserEventHandler(e) actualBrowser.push(e) michael@0: on(browserEvents, "data", browserEventHandler); michael@0: on(windowEvents, "data", function handler(e) { michael@0: actualWindow.push(e); michael@0: // Delay close so that if "load" is also emitted on `browserEvents` michael@0: // `browserEventHandler` will be invoked. michael@0: if (e.type === "load") setTimeout(window.close); michael@0: if (e.type === "close") { michael@0: assert.deepEqual(actualBrowser, [], "browser events were not triggered"); michael@0: let [ open, ready, load, close ] = actualWindow; michael@0: michael@0: assert.equal(open.type, "open"); michael@0: assert.equal(open.target, window, "window is open"); michael@0: michael@0: michael@0: michael@0: assert.equal(ready.type, "DOMContentLoaded"); michael@0: assert.equal(ready.target, window, "window ready"); michael@0: michael@0: assert.equal(load.type, "load"); michael@0: assert.equal(load.target, window, "window load"); michael@0: michael@0: assert.equal(close.type, "close"); michael@0: assert.equal(close.target, window, "window load"); michael@0: michael@0: michael@0: // Note: If window is closed right after this GC won't have time michael@0: // to claim loader and there for this listener, there for it's safer michael@0: // to remove listener. michael@0: off(windowEvents, "data", handler); michael@0: off(browserEvents, "data", browserEventHandler); michael@0: loader.unload(); michael@0: done(); michael@0: } michael@0: }); michael@0: michael@0: // Open window and close it to trigger observers. michael@0: let window = open("data:text/html,not a browser"); michael@0: }; michael@0: michael@0: require("test").run(exports);