addon-sdk/source/test/test-browser-events.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 engines: {
michael@0 9 "Firefox": "*"
michael@0 10 }
michael@0 11 };
michael@0 12
michael@0 13 const { Loader } = require("sdk/test/loader");
michael@0 14 const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils");
michael@0 15 const { setTimeout } = require("sdk/timers");
michael@0 16
michael@0 17 exports["test browser events"] = function(assert, done) {
michael@0 18 let loader = Loader(module);
michael@0 19 let { events } = loader.require("sdk/browser/events");
michael@0 20 let { on, off } = loader.require("sdk/event/core");
michael@0 21 let actual = [];
michael@0 22
michael@0 23 on(events, "data", function handler(e) {
michael@0 24 actual.push(e);
michael@0 25 if (e.type === "load") window.close();
michael@0 26 if (e.type === "close") {
michael@0 27 // Unload the module so that all listeners set by observer are removed.
michael@0 28
michael@0 29 let [ ready, load, close ] = actual;
michael@0 30
michael@0 31 assert.equal(ready.type, "DOMContentLoaded");
michael@0 32 assert.equal(ready.target, window, "window ready");
michael@0 33
michael@0 34 assert.equal(load.type, "load");
michael@0 35 assert.equal(load.target, window, "window load");
michael@0 36
michael@0 37 assert.equal(close.type, "close");
michael@0 38 assert.equal(close.target, window, "window load");
michael@0 39
michael@0 40 // Note: If window is closed right after this GC won't have time
michael@0 41 // to claim loader and there for this listener, there for it's safer
michael@0 42 // to remove listener.
michael@0 43 off(events, "data", handler);
michael@0 44 loader.unload();
michael@0 45 done();
michael@0 46 }
michael@0 47 });
michael@0 48
michael@0 49 // Open window and close it to trigger observers.
michael@0 50 let window = open();
michael@0 51 };
michael@0 52
michael@0 53 exports["test browser events ignore other wins"] = function(assert, done) {
michael@0 54 let loader = Loader(module);
michael@0 55 let { events: windowEvents } = loader.require("sdk/window/events");
michael@0 56 let { events: browserEvents } = loader.require("sdk/browser/events");
michael@0 57 let { on, off } = loader.require("sdk/event/core");
michael@0 58 let actualBrowser = [];
michael@0 59 let actualWindow = [];
michael@0 60
michael@0 61 function browserEventHandler(e) actualBrowser.push(e)
michael@0 62 on(browserEvents, "data", browserEventHandler);
michael@0 63 on(windowEvents, "data", function handler(e) {
michael@0 64 actualWindow.push(e);
michael@0 65 // Delay close so that if "load" is also emitted on `browserEvents`
michael@0 66 // `browserEventHandler` will be invoked.
michael@0 67 if (e.type === "load") setTimeout(window.close);
michael@0 68 if (e.type === "close") {
michael@0 69 assert.deepEqual(actualBrowser, [], "browser events were not triggered");
michael@0 70 let [ open, ready, load, close ] = actualWindow;
michael@0 71
michael@0 72 assert.equal(open.type, "open");
michael@0 73 assert.equal(open.target, window, "window is open");
michael@0 74
michael@0 75
michael@0 76
michael@0 77 assert.equal(ready.type, "DOMContentLoaded");
michael@0 78 assert.equal(ready.target, window, "window ready");
michael@0 79
michael@0 80 assert.equal(load.type, "load");
michael@0 81 assert.equal(load.target, window, "window load");
michael@0 82
michael@0 83 assert.equal(close.type, "close");
michael@0 84 assert.equal(close.target, window, "window load");
michael@0 85
michael@0 86
michael@0 87 // Note: If window is closed right after this GC won't have time
michael@0 88 // to claim loader and there for this listener, there for it's safer
michael@0 89 // to remove listener.
michael@0 90 off(windowEvents, "data", handler);
michael@0 91 off(browserEvents, "data", browserEventHandler);
michael@0 92 loader.unload();
michael@0 93 done();
michael@0 94 }
michael@0 95 });
michael@0 96
michael@0 97 // Open window and close it to trigger observers.
michael@0 98 let window = open("data:text/html,not a browser");
michael@0 99 };
michael@0 100
michael@0 101 require("test").run(exports);

mercurial