Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 1098946 - Expose "activate" and "deactivate" for windows & browse…
Browse files Browse the repository at this point in the history
…rs. r=erikvold

Conflicts:
	test/test-window-events.js
  • Loading branch information
Gozala authored and erikvold committed Nov 14, 2014
1 parent 0f59089 commit ba96f49
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
21 changes: 14 additions & 7 deletions lib/sdk/window/events.js
Expand Up @@ -13,16 +13,23 @@ const { open } = require("../event/dom");
const { windows } = require("../window/utils");
const { filter, merge, map, expand } = require("../event/utils");

function statesFor(window) {
let interactive = open(window, "DOMContentLoaded", { capture: true });
let complete = open(window, "load", { capture: true });
let events = merge([interactive, complete]);
let states = filter(events, ({target}) => target === window.document);
return map(states, ({type, target}) => ({type: type,
target: target.defaultView}));
}

// Function registers single shot event listeners for relevant window events
// that forward events to exported event stream.
function eventsFor(window) {
let interactive = open(window, "DOMContentLoaded", { capture: true });
let complete = open(window, "load", { capture: true });
let states = merge([interactive, complete]);
let changes = filter(states, function({target}) target === window.document);
return map(changes, function({type, target}) {
return { type: type, target: target.defaultView }
});
return merge([
statesFor(window),
open(window, "activate"),
open(window, "deactivate")
]);
}

// In addition to observing windows that are open we also observe windows
Expand Down
23 changes: 19 additions & 4 deletions test/test-browser-events.js
Expand Up @@ -26,14 +26,20 @@ exports["test browser events"] = function(assert, done) {
if (e.type === "close") {
// Unload the module so that all listeners set by observer are removed.

let [ ready, load, close ] = actual;
let [ ready, load, deactivate, activate, close ] = actual;

assert.equal(ready.type, "DOMContentLoaded");
assert.equal(ready.target, window, "window ready");

assert.equal(load.type, "load");
assert.equal(load.target, window, "window load");

assert.equal(deactivate.type, "deactivate", "deactivate window")
assert.notEqual(deactivate.target, window, "other window deactivated")

assert.equal(activate.type, "activate", "activate event")
assert.equal(activate.target, window, "target is window")

assert.equal(close.type, "close");
assert.equal(close.target, window, "window load");

Expand Down Expand Up @@ -66,20 +72,29 @@ exports["test browser events ignore other wins"] = function(assert, done) {
// `browserEventHandler` will be invoked.
if (e.type === "load") setTimeout(window.close);
if (e.type === "close") {
assert.deepEqual(actualBrowser, [], "browser events were not triggered");
let [ open, ready, load, close ] = actualWindow;
// Ignore "deactivate" events since browser may have a focus.
assert.deepEqual(actualBrowser.filter(e => e.type !== "deactivate"), [],
"browser events were not triggered");
let [ open, ready, load, deactivate, activate, close ] = actualWindow;
assert.equal(open.type, "open");
assert.equal(open.target, window, "window is open");
assert.equal(deactivate.type, "deactivate", "deactivate window")
assert.notEqual(deactivate.target, window, "other window deactivated")
assert.equal(ready.type, "DOMContentLoaded");
assert.equal(ready.target, window, "window ready");
assert.equal(load.type, "load");
assert.equal(load.target, window, "window load");

assert.equal(deactivate.type, "deactivate", "deactivate window")
assert.notEqual(deactivate.target, window, "other window deactivated")

assert.equal(activate.type, "activate", "activate event")
assert.equal(activate.target, window, "target is window")

assert.equal(close.type, "close");
assert.equal(close.target, window, "window load");

Expand Down
8 changes: 7 additions & 1 deletion test/test-window-events.js
Expand Up @@ -34,7 +34,7 @@ exports["test browser events"] = function(assert, done) {
}
else if (e.type === "close") {
// confirm the ordering of events
let [ open, ready, load, close ] = actual;
let [ open, ready, load, deactivate, activate, close ] = actual;
assert.equal(open.type, "open")
assert.equal(open.target, window, "window is open")

Expand All @@ -44,6 +44,12 @@ exports["test browser events"] = function(assert, done) {
assert.equal(load.type, "load")
assert.equal(load.target, window, "window load")

assert.equal(deactivate.type, "deactivate", "deactivate window")
assert.notEqual(deactivate.target, window, "other window deactivated")

assert.equal(activate.type, "activate", "activate event")
assert.equal(activate.target, window, "target is window")

assert.equal(close.type, "close")
assert.equal(close.target, window, "window load")

Expand Down

0 comments on commit ba96f49

Please sign in to comment.