addon-sdk/source/test/test-window-events.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-events.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     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 +// Opening new windows in Fennec causes issues
    1.10 +module.metadata = {
    1.11 +  engines: {
    1.12 +    'Firefox': '*'
    1.13 +  }
    1.14 +};
    1.15 +
    1.16 +const { Loader } = require("sdk/test/loader");
    1.17 +const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils");
    1.18 +
    1.19 +exports["test browser events"] = function(assert, done) {
    1.20 +  let loader = Loader(module);
    1.21 +  let { events } = loader.require("sdk/window/events");
    1.22 +  let { on, off } = loader.require("sdk/event/core");
    1.23 +  let actual = [];
    1.24 +
    1.25 +  on(events, "data", function handler(e) {
    1.26 +    actual.push(e);
    1.27 +
    1.28 +    if (e.type === "open") {
    1.29 +      assert.pass("window open has occured");
    1.30 +    }
    1.31 +    else if (e.type === "DOMContentLoaded") {
    1.32 +      assert.pass("window DOMContentLoaded has occured");
    1.33 +    }
    1.34 +    else if (e.type === "load") {
    1.35 +      assert.pass("window load has occured");
    1.36 +      window.close();
    1.37 +    }
    1.38 +    else if (e.type === "close") {
    1.39 +      // confirm the ordering of events
    1.40 +      let [ open, ready, load, close ] = actual;
    1.41 +      assert.equal(open.type, "open")
    1.42 +      assert.equal(open.target, window, "window is open")
    1.43 +
    1.44 +      assert.equal(ready.type, "DOMContentLoaded")
    1.45 +      assert.equal(ready.target, window, "window ready")
    1.46 +
    1.47 +      assert.equal(load.type, "load")
    1.48 +      assert.equal(load.target, window, "window load")
    1.49 +
    1.50 +      assert.equal(close.type, "close")
    1.51 +      assert.equal(close.target, window, "window load")
    1.52 +
    1.53 +      // Note: If window is closed right after this GC won't have time
    1.54 +      // to claim loader and there for this listener. It's better to remove
    1.55 +      // remove listener here to avoid race conditions.
    1.56 +      off(events, "data", handler);
    1.57 +      loader.unload();
    1.58 +      done();
    1.59 +    }
    1.60 +  });
    1.61 +
    1.62 +  // Open window and close it to trigger observers.
    1.63 +  let window = open();
    1.64 +};
    1.65 +
    1.66 +require("sdk/test").run(exports);

mercurial