addon-sdk/source/test/test-window-observer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-window-observer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,61 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +const { Loader } = require("sdk/test/loader");
    1.10 +const { open, close } = require("sdk/window/helpers");
    1.11 +const { browserWindows: windows } = require("sdk/windows");
    1.12 +const { isBrowser } = require('sdk/window/utils');
    1.13 +const app = require("sdk/system/xul-app");
    1.14 +
    1.15 +exports["test unload window observer"] = function(assert, done) {
    1.16 +  // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
    1.17 +  let loader = Loader(module);
    1.18 +  let observer = loader.require("sdk/windows/observer").observer;
    1.19 +  let opened = 0;
    1.20 +  let closed = 0;
    1.21 +  let windowsOpen = windows.length;
    1.22 +
    1.23 +  observer.on("open", onOpen);
    1.24 +  observer.on("close", onClose);
    1.25 +
    1.26 +  // On Fennec, only test that the module does not throw an error
    1.27 +  if (app.is("Fennec")) {
    1.28 +    assert.pass("Windows observer did not throw on Fennec");
    1.29 +    return cleanUp();
    1.30 +  }
    1.31 +
    1.32 +  // Open window and close it to trigger observers.
    1.33 +  open().
    1.34 +    then(close).
    1.35 +    then(loader.unload).
    1.36 +    then(open).
    1.37 +    then(close).
    1.38 +    then(function() {
    1.39 +      // Enqueuing asserts to make sure that assertion is not performed early.
    1.40 +      assert.equal(1, opened, "observer open was called before unload only");
    1.41 +      assert.equal(windowsOpen + 1, closed, "observer close was called before unload only");
    1.42 +    }).
    1.43 +    then(cleanUp, assert.fail);
    1.44 +
    1.45 +  function cleanUp () {
    1.46 +    observer.removeListener("open", onOpen);
    1.47 +    observer.removeListener("close", onClose);
    1.48 +    done();
    1.49 +  }
    1.50 +
    1.51 +  function onOpen(window) {
    1.52 +    // Ignoring non-browser windows
    1.53 +    if (isBrowser(window))
    1.54 +      opened++;
    1.55 +  }
    1.56 +  function onClose(window) {
    1.57 +    // Ignore non-browser windows & already opened `activeWindow` (unload will
    1.58 +    // emit close on it even though it is not actually closed).
    1.59 +    if (isBrowser(window))
    1.60 +      closed++;
    1.61 +  }
    1.62 +};
    1.63 +
    1.64 +require("sdk/test").run(exports);

mercurial