Skip to content

Commit

Permalink
bug 834961: created onFocus in window/utils module using it in tests,…
Browse files Browse the repository at this point in the history
… and testing activeWindow setter on window-utils module
  • Loading branch information
erikvold committed Feb 9, 2013
1 parent 8bef6d5 commit e826692
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 13 deletions.
13 changes: 13 additions & 0 deletions lib/sdk/private-browsing.js
Expand Up @@ -39,11 +39,24 @@ exports.removeListener = deprecateEvents(function removeListener(type, listener)
});

exports.isPrivate = function(thing) {
// if thing is defined, and we can find a window for it
// then check if the window is private
if (!!thing) {
// if the thing is a window, and the window is private
// then return true
if (isWindowPrivate(thing)) {
return true;
}

// can we find an associated window?
let window = getOwnerWindow(thing);
if (window)
return isWindowPrivate(window);
}

// if we get here, and global private browsing
// is available, and it is true, then return
// true otherwise false is returned here
return getMode();
};

Expand Down
17 changes: 10 additions & 7 deletions lib/sdk/test/assert.js
Expand Up @@ -69,14 +69,17 @@ Assert.prototype = {
return;
}
let message = e.message;
if ('operator' in e) {
message += [
" -",
source(e.expected),
e.operator,
source(e.actual)
].join(" ");
try {
if ('operator' in e) {
message += [
" -",
source(e.expected),
e.operator,
source(e.actual)
].join(" ");
}
}
catch(e) {}
this._log.fail(message);
},
pass: function pass(message) {
Expand Down
146 changes: 140 additions & 6 deletions test/test-window-utils.js
Expand Up @@ -11,6 +11,7 @@ const { loader: pbLoader, getOwnerWindow, pbUtils, pb } = require('./private-bro
const { open, close } = pbLoader.require('sdk/window/utils');
const { getFrames, getWindowTitle, onFocus } = require('sdk/window/utils');
const { isPrivate } = require('sdk/private-browsing');
const WM = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);

function toArray(iterator) {
let array = [];
Expand Down Expand Up @@ -231,9 +232,7 @@ exports['test window watcher untracker'] = function(assert, done) {
exports['test window watcher unregs 4 loading wins'] = function(assert, done) {
var myWindow;
var finished = false;
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
let browserWindow = WM.getMostRecentWindow("navigator:browser");
var counter = 0;

var delegate = {
Expand Down Expand Up @@ -308,9 +307,7 @@ exports['test window watcher without untracker'] = function(assert, done) {
};

exports['test active window'] = function(assert, done) {
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
let browserWindow = WM.getMostRecentWindow("navigator:browser");
let continueAfterFocus = function(window) onFocus(window, nextTest);

assert.equal(windowUtils.activeBrowserWindow, browserWindow,
Expand Down Expand Up @@ -350,6 +347,137 @@ exports['test active window'] = function(assert, done) {
nextTest();
};

// Test setting activeWIndow and onFocus for private windows
exports.testSettingActiveWindowIgnoresPrivateWindow = function(assert, done) {
let {browserWindows: pbWindows } = pbLoader.require('windows');
let pbWindowUtils = pbLoader.require('sdk/deprecated/window-utils');

let browserWindow = WM.getMostRecentWindow("navigator:browser");
let testSteps;

assert.equal(windowUtils.activeBrowserWindow, browserWindow,
"Browser window is the active browser window.");

// make a new private window
pbWindows.open({
private: true,
onOpen: function(win) {
let window = getOwnerWindow(win);
let continueAfterFocus = function(window) onFocus(window, nextTest);

assert.ok(window instanceof Ci.nsIDOMWindow, "window was found");

// PWPB case
if (pbUtils.isWindowPBSupported) {
assert.ok(isPrivate(window), "window is private");

assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow,
"Correct active browser window pb not supported");

// active window is different when pb mode is on vs off
assert.notDeepEqual(windowUtils.activeBrowserWindow, pbWindowUtils.activeBrowserWindow);
}
// Global case
else {
assert.ok(!isPrivate(window), "window is not private");

// active window is the same in global pb, private flag is ignored
assert.deepEqual(windowUtils.activeBrowserWindow, pbWindowUtils.activeBrowserWindow);
}

assert.deepEqual(pbWindowUtils.activeBrowserWindow, window,
"Correct active browser window pb supported");

testSteps = [
function() {
continueAfterFocus(windowUtils.activeWindow = browserWindow);
},
function() {
// PWPB case
if (pbUtils.isWindowPBSupported) {
assert.deepEqual(windowUtils.activeWindow, browserWindow,
"Correct active window when pb mode is not supported [1]");
assert.deepEqual(pbWindowUtils.activeWindow, window,
"Correct active window when pb mode is supported [1]");
}
// Global case
else {
assert.deepEqual(windowUtils.activeWindow, window,
"Correct active window when pb mode is not supported [1]");
assert.deepEqual(pbWindowUtils.activeWindow, window,
"Correct active window when pb mode is supported [1]");
}

// PWPB case
if (pbUtils.isWindowPBSupported) {
onFocus(
window,
function() {
assert.fail('this should not be called');
}
);
}
pbLoader.require('sdk/window/utils').onFocus(window, nextTest);
window.focus();
},
function() {
// PWPB case
if (pbUtils.isWindowPBSupported) {
// pb not supported
assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow,
"Correct active browser window when pb mode is not supported [2]");
assert.deepEqual(windowUtils.activeWindow, browserWindow,
"Correct active window when pb mode is not supported [2]");
}

assert.deepEqual(pbWindowUtils.activeBrowserWindow, window,
"Correct active browser window when pb mode is supported [2]");
assert.deepEqual(pbWindowUtils.activeWindow, window,
"Correct active window when pb mode is supported [2]");

windowUtils.activeWindow = window;
pbLoader.require('sdk/window/utils').onFocus(window, nextTest);
},
function() {
// PWPB case
if (pbUtils.isWindowPBSupported) {
// pb not supported
assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow,
"Correct active browser window when pb mode is not supported [3]");
assert.deepEqual(windowUtils.activeWindow, browserWindow,
"Correct active window when pb mode is not supported [3]");
}

assert.deepEqual(pbWindowUtils.activeBrowserWindow, window,
"Correct active browser window when pb mode is supported [3]");
assert.deepEqual(pbWindowUtils.activeWindow, window,
"Correct active window when pb mode is supported [3]");

continueAfterFocus(windowUtils.activeWindow = browserWindow);
},
function() {
// pb not supported
assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow,
"Correct active browser window when pb mode is not supported [4]");
assert.deepEqual(windowUtils.activeWindow, browserWindow,
"Correct active window when pb mode is not supported [4]");
// pb supported
assert.deepEqual(pbWindowUtils.activeBrowserWindow, browserWindow,
"Correct active browser window when pb mode is supported [4]");
assert.deepEqual(pbWindowUtils.activeWindow, browserWindow,
"Correct active window when pb mode is supported [4]");
win.close(function() done());
}
];
function nextTest() {
if (testSteps.length)
testSteps.shift()();
}
nextTest();
}
});
};

exports.testActiveWindowIgnoresPrivateWindow = function(assert, done) {
let {browserWindows: pbWindows } = pbLoader.require('windows');
let pbWindowUtils = pbLoader.require('sdk/deprecated/window-utils');
Expand All @@ -370,6 +498,7 @@ exports.testActiveWindowIgnoresPrivateWindow = function(assert, done) {
// PWPB case
if (pbUtils.isWindowPBSupported) {
assert.ok(pbUtils.isWindowPrivate(window), "window is private");
assert.ok(isPrivate(window), "window is private");

// pb mode is supported
assert.ok(
Expand All @@ -378,10 +507,15 @@ exports.testActiveWindowIgnoresPrivateWindow = function(assert, done) {
assert.ok(
pbUtils.isWindowPrivate(pbWindowUtils.activeBrowserWindow),
"active browser window is private when pb mode is supported");
assert.ok(isPrivate(pbWindowUtils.activeWindow),
"active window is private when pb mode is supported");
assert.ok(isPrivate(pbWindowUtils.activeBrowserWindow),
"active browser window is private when pb mode is supported");
}
// Global case
else {
assert.equal(pbUtils.isWindowPrivate(window), false, "window is not private");
assert.equal(isPrivate(window), false, "window is not private");
}
win.close(function() done());
}
Expand Down

0 comments on commit e826692

Please sign in to comment.