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

Commit

Permalink
Bug 863456: Report failures for tests that leave unexpected tabs or w…
Browse files Browse the repository at this point in the history
…indows behind.
  • Loading branch information
Mossop committed Apr 18, 2014
1 parent 98b2491 commit 934c61d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 62 deletions.
31 changes: 29 additions & 2 deletions lib/sdk/deprecated/unit-test.js
Expand Up @@ -11,6 +11,8 @@ module.metadata = {
const memory = require('./memory');
var timer = require("../timers");
var cfxArgs = require("@test/options");
const { getTabs, getURI } = require("../tabs/utils");
const { windows, isBrowser } = require("../window/utils");

exports.findAndRunTests = function findAndRunTests(options) {
var TestFinder = require("./unit-test-finder").TestFinder;
Expand Down Expand Up @@ -278,14 +280,39 @@ TestRunner.prototype = {
this.failed++;
this.test.failed++;
}


let wins = windows(null, { includePrivate: true });
let tabs = [];
for (let win of wins.filter(isBrowser)) {
for (let tab of getTabs(win)) {
tabs.push(tab);
}
}

if (wins.length != 1)
this.fail("Should not be any unexpected windows open");
if (tabs.length != 1)
this.fail("Should not be any unexpected tabs open");
if (tabs.length != 1 || wins.length != 1) {
console.log("Windows open:");
for (let win of wins) {
if (isBrowser(win)) {
tabs = getTabs(win);
console.log(win.location + " - " + tabs.map(getURI).join(", "));
}
else {
console.log(win.location);
}
}
}

this.testRunSummary.push({
name: this.test.name,
passed: this.test.passed,
failed: this.test.failed,
errors: [error for (error in this.test.errors)].join(", ")
});

if (this.onDone !== null) {
var onDone = this.onDone;
var self = this;
Expand Down
9 changes: 7 additions & 2 deletions lib/sdk/tabs/tab-firefox.js
Expand Up @@ -17,6 +17,7 @@ const viewNS = require('../core/namespace').ns();
const { deprecateUsage } = require('../util/deprecate');
const { getURL } = require('../url/utils');
const { viewFor } = require('../view/core');
const { observer } = require('./observer');

// Array of the inner instances of all the wrapped tabs.
const TABS = [];
Expand Down Expand Up @@ -256,8 +257,12 @@ const TabTrait = Trait.compose(EventEmitter, {
callback();
return;
}
if (callback)
this.once(EVENTS.close.name, callback);
if (callback) {
if (this.window.tabs.activeTab && (this.window.tabs.activeTab.id == this.id))
observer.once('select', callback);
else
this.once(EVENTS.close.name, callback);
}
this._window.gBrowser.removeTab(this._tab);
},
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/sdk/tabs/utils.js
Expand Up @@ -91,7 +91,7 @@ function getTabs(window) {
return window.BrowserApp.tabs;

// firefox - default
return Array.slice(getTabContainer(window).children);
return Array.filter(getTabContainer(window).children, function(t) !t.closing);
}
exports.getTabs = getTabs;

Expand Down
2 changes: 2 additions & 0 deletions test/addons/page-mod-debugger-post/main.js
Expand Up @@ -6,6 +6,7 @@
const { Cu } = require('chrome');
const { PageMod } = require('sdk/page-mod');
const tabs = require('sdk/tabs');
const { closeTab } = require('sdk/tabs/utils');
const promise = require('sdk/core/promise')
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const { data } = require('sdk/self');
Expand Down Expand Up @@ -47,6 +48,7 @@ exports.testDebugger = function(assert, done) {
then(_ => { assert.pass('testDebuggerStatement called') }).
then(closeConnection).
then(_ => { assert.pass('closeConnection called') }).
then(_ => { tab.close() }).
then(done).
then(null, aError => {
ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
Expand Down
2 changes: 2 additions & 0 deletions test/addons/page-mod-debugger-pre/main.js
Expand Up @@ -6,6 +6,7 @@
const { Cu } = require('chrome');
const { PageMod } = require('sdk/page-mod');
const tabs = require('sdk/tabs');
const { closeTab } = require('sdk/tabs/utils');
const promise = require('sdk/core/promise')
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const { data } = require('sdk/self');
Expand Down Expand Up @@ -54,6 +55,7 @@ exports.testDebugger = function(assert, done) {
then(_ => { assert.pass('testDebuggerStatement called') }).
then(closeConnection).
then(_ => { assert.pass('closeConnection called') }).
then(_ => { tab.close() }).
then(done).
then(null, aError => {
ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
Expand Down
46 changes: 17 additions & 29 deletions test/addons/private-browsing-supported/test-selection.js
Expand Up @@ -86,25 +86,6 @@ function open(url, options) {
return promise;
};

/**
* Close the Active Tab
*/
function close(window) {
let { promise, resolve } = defer();

if (window && typeof(window.close) === "function") {
closeWindow(window).then(function() resolve());
}
else {
// Here we assuming that the most recent browser window is the one we're
// doing the test, and the active tab is the one we just opened.
closeTab(getActiveTab(getMostRecentBrowserWindow()));
resolve();
}

return promise;
}

/**
* Reload the window given and return a promise, that will be resolved with the
* content window after a small delay.
Expand Down Expand Up @@ -249,7 +230,7 @@ exports["test PWPB Selection Listener"] = function(assert, done) {

assert.equal(selection.text, "fo");

close(window).
closeWindow(window).
then(loader.unload).
then(done).
then(null, assert.fail);
Expand Down Expand Up @@ -279,7 +260,7 @@ exports["test PWPB Textarea OnSelect Listener"] = function(assert, done) {
focus(window).then(function() {
assert.equal(selection.text, "noodles");

close(window).
closeWindow(window).
then(loader.unload).
then(done).
then(null, assert.fail);
Expand All @@ -298,7 +279,7 @@ exports["test PWPB Single DOM Selection"] = function(assert, done) {

open(URL, {private: true, title: "PWPB Single DOM Selection"}).
then(selectFirstDiv).
then(focus).then(function() {
then(focus).then(function(window) {
assert.equal(selection.isContiguous, true,
"selection.isContiguous with single DOM Selection works.");

Expand All @@ -321,7 +302,9 @@ exports["test PWPB Single DOM Selection"] = function(assert, done) {

assert.equal(selectionCount, 1,
"One iterable selection");
}).then(close).then(loader.unload).then(done).then(null, assert.fail);

return closeWindow(window);
}).then(loader.unload).then(done).then(null, assert.fail);
}

exports["test PWPB Textarea Selection"] = function(assert, done) {
Expand All @@ -331,7 +314,7 @@ exports["test PWPB Textarea Selection"] = function(assert, done) {
open(URL, {private: true, title: "PWPB Textarea Listener"}).
then(selectTextarea).
then(focus).
then(function() {
then(function(window) {

assert.equal(selection.isContiguous, true,
"selection.isContiguous with Textarea Selection works.");
Expand All @@ -355,7 +338,9 @@ exports["test PWPB Textarea Selection"] = function(assert, done) {

assert.equal(selectionCount, 1,
"One iterable selection");
}).then(close).then(loader.unload).then(done).then(null, assert.fail);

return closeWindow(window);
}).then(loader.unload).then(done).then(null, assert.fail);
};

exports["test PWPB Set HTML in Multiple DOM Selection"] = function(assert, done) {
Expand All @@ -365,7 +350,7 @@ exports["test PWPB Set HTML in Multiple DOM Selection"] = function(assert, done)
open(URL, {private: true, title: "PWPB Set HTML in Multiple DOM Selection"}).
then(selectAllDivs).
then(focus).
then(function() {
then(function(window) {
let html = "<span>b<b>a</b>r</span>";

let expectedText = ["bar", "and"];
Expand Down Expand Up @@ -393,7 +378,9 @@ exports["test PWPB Set HTML in Multiple DOM Selection"] = function(assert, done)

assert.equal(selectionCount, 2,
"Two iterable selections");
}).then(close).then(loader.unload).then(done).then(null, assert.fail);

return closeWindow(window);
}).then(loader.unload).then(done).then(null, assert.fail);
};

exports["test PWPB Set Text in Textarea Selection"] = function(assert, done) {
Expand All @@ -403,7 +390,7 @@ exports["test PWPB Set Text in Textarea Selection"] = function(assert, done) {
open(URL, {private: true, title: "test PWPB Set Text in Textarea Selection"}).
then(selectTextarea).
then(focus).
then(function() {
then(function(window) {

let text = "bar";

Expand All @@ -429,7 +416,8 @@ exports["test PWPB Set Text in Textarea Selection"] = function(assert, done) {
assert.equal(selectionCount, 1,
"One iterable selection");

}).then(close).then(loader.unload).then(done).then(null, assert.fail);
return closeWindow(window);
}).then(loader.unload).then(done).then(null, assert.fail);
};

// If the platform doesn't support the PBPW, we're replacing PBPW tests
Expand Down
37 changes: 34 additions & 3 deletions test/tabs/test-firefox-tabs.js
Expand Up @@ -368,6 +368,33 @@ exports.testTabMove = function(assert, done) {
}).then(null, assert.fail);
};

exports.testIgnoreClosing = function(assert, done) {
let originalWindow = browserWindows.activeWindow;
openBrowserWindow(function(window, browser) {
let url = "data:text/html;charset=utf-8,foobar";

assert.equal(tabs.length, 2, "should be two windows open each with one tab");

tabs.on('ready', function onReady(tab) {
tabs.removeListener('ready', onReady);

let win = tab.window;
assert.equal(win.tabs.length, 2, "should be two tabs in the new window");
assert.equal(tabs.length, 3, "should be three tabs in total");

tab.close(function() {
assert.equal(win.tabs.length, 1, "should be one tab in the new window");
assert.equal(tabs.length, 2, "should be two tabs in total");

originalWindow.once("activate", done);
close(window);
});
});

tabs.open(url);
});
};

// TEST: open tab with default options
exports.testOpen = function(assert, done) {
let url = "data:text/html;charset=utf-8,default";
Expand Down Expand Up @@ -413,6 +440,8 @@ exports.testPinUnpin = function(assert, done) {

// TEST: open tab in background
exports.testInBackground = function(assert, done) {
assert.equal(tabs.length, 1, "Should be one tab");

let window = getMostRecentBrowserWindow();
let activeUrl = tabs.activeTab.url;
let url = "data:text/html;charset=utf-8,background";
Expand Down Expand Up @@ -942,9 +971,11 @@ exports['test unique tab ids'] = function(assert, done) {
var one = openWindow(), two = openWindow();
all([one, two]).then(function(results) {
assert.notEqual(results[0].id, results[1].id, "tab Ids should not be equal.");
results[0].win.close();
results[1].win.close();
done();
results[0].win.close(function() {
results[1].win.close(function () {
done();
});
});
});
}

Expand Down
3 changes: 1 addition & 2 deletions test/test-clipboard.js
Expand Up @@ -90,8 +90,7 @@ function comparePixelImages(imageA, imageB, callback) {
compared = pixels;
this.emit("draw-image", imageB);
} else {
callback(compared === pixels);
tab.close()
tab.close(callback.bind(null, compared === pixels))
}
});

Expand Down

0 comments on commit 934c61d

Please sign in to comment.