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: "use strict"; michael@0: michael@0: const { Loader } = require("sdk/test/loader"); michael@0: const { open, close } = require("sdk/window/helpers"); michael@0: const { browserWindows: windows } = require("sdk/windows"); michael@0: const { isBrowser } = require('sdk/window/utils'); michael@0: const app = require("sdk/system/xul-app"); michael@0: michael@0: exports["test unload window observer"] = function(assert, done) { michael@0: // Hacky way to be able to create unloadable modules via makeSandboxedLoader. michael@0: let loader = Loader(module); michael@0: let observer = loader.require("sdk/windows/observer").observer; michael@0: let opened = 0; michael@0: let closed = 0; michael@0: let windowsOpen = windows.length; michael@0: michael@0: observer.on("open", onOpen); michael@0: observer.on("close", onClose); michael@0: michael@0: // On Fennec, only test that the module does not throw an error michael@0: if (app.is("Fennec")) { michael@0: assert.pass("Windows observer did not throw on Fennec"); michael@0: return cleanUp(); michael@0: } michael@0: michael@0: // Open window and close it to trigger observers. michael@0: open(). michael@0: then(close). michael@0: then(loader.unload). michael@0: then(open). michael@0: then(close). michael@0: then(function() { michael@0: // Enqueuing asserts to make sure that assertion is not performed early. michael@0: assert.equal(1, opened, "observer open was called before unload only"); michael@0: assert.equal(windowsOpen + 1, closed, "observer close was called before unload only"); michael@0: }). michael@0: then(cleanUp, assert.fail); michael@0: michael@0: function cleanUp () { michael@0: observer.removeListener("open", onOpen); michael@0: observer.removeListener("close", onClose); michael@0: done(); michael@0: } michael@0: michael@0: function onOpen(window) { michael@0: // Ignoring non-browser windows michael@0: if (isBrowser(window)) michael@0: opened++; michael@0: } michael@0: function onClose(window) { michael@0: // Ignore non-browser windows & already opened `activeWindow` (unload will michael@0: // emit close on it even though it is not actually closed). michael@0: if (isBrowser(window)) michael@0: closed++; michael@0: } michael@0: }; michael@0: michael@0: require("sdk/test").run(exports);