From a8bcea0d5ddd51575401e1236ca0aae295351dd4 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 21 Jan 2017 15:19:33 -0800 Subject: [PATCH] Bug 1332874: Check that extra windows and tabs are closed after each mochitest. r=aswan MozReview-Commit-ID: GjJxHVnLJi1 --HG-- extra : rebase_source : 695612f3d73e2feb71b44d3702c583cd3be11626 --- .../content/SpecialPowersObserverAPI.js | 8 +--- .../extensions/test/mochitest/chrome.ini | 1 + .../test/mochitest/chrome_cleanup_script.js | 47 +++++++++++++++++++ .../extensions/test/mochitest/head.js | 18 +++++++ .../test/mochitest/mochitest-common.ini | 1 + .../mochitest/test_ext_webrequest_filter.html | 2 + 6 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js diff --git a/testing/specialpowers/content/SpecialPowersObserverAPI.js b/testing/specialpowers/content/SpecialPowersObserverAPI.js index a88e46acc57a..33b7db875d1f 100644 --- a/testing/specialpowers/content/SpecialPowersObserverAPI.js +++ b/testing/specialpowers/content/SpecialPowersObserverAPI.js @@ -208,14 +208,8 @@ SpecialPowersObserverAPI.prototype = { input.close(); var status; - try { - channel.QueryInterface(Ci.nsIHttpChannel); + if (channel instanceof Ci.nsIHttpChannel) { status = channel.responseStatus; - } catch(e) { - /* The channel is not a nsIHttpCHannel, but that's fine */ - dump("-*- _readUrlAsString: Got an error while fetching " + - "chrome script '" + aUrl + "': (" + e.name + ") " + e.message + ". " + - "Ignoring.\n"); } if (status == 404) { diff --git a/toolkit/components/extensions/test/mochitest/chrome.ini b/toolkit/components/extensions/test/mochitest/chrome.ini index d9ca44b6cb10..206572ad217f 100644 --- a/toolkit/components/extensions/test/mochitest/chrome.ini +++ b/toolkit/components/extensions/test/mochitest/chrome.ini @@ -1,6 +1,7 @@ [DEFAULT] support-files = chrome_head.js + chrome_cleanup_script.js head.js head_cookies.js file_sample.html diff --git a/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js b/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js new file mode 100644 index 000000000000..d0ae08d00df2 --- /dev/null +++ b/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js @@ -0,0 +1,47 @@ +"use strict"; + +/* global addMessageListener, sendAsyncMessage */ + +Components.utils.import("resource://gre/modules/Services.jsm"); + +function* iterBrowserWindows() { + let enm = Services.wm.getEnumerator("navigator:browser"); + while (enm.hasMoreElements()) { + let win = enm.getNext(); + if (!win.closed && win.gBrowser) { + yield win; + } + } +} + +let initialTabs = new Map(); +for (let win of iterBrowserWindows()) { + initialTabs.set(win, new Set(win.gBrowser.tabs)); +} + +addMessageListener("check-cleanup", extensionId => { + let results = { + extraWindows: [], + extraTabs: [], + }; + + for (let win of iterBrowserWindows()) { + if (initialTabs.has(win)) { + let tabs = initialTabs.get(win); + + for (let tab of win.gBrowser.tabs) { + if (!tabs.has(tab)) { + results.extraTabs.push(tab.linkedBrowser.currentURI.spec); + } + } + } else { + results.extraWindows.push( + Array.from(win.gBrowser.tabs, + tab => tab.linkedBrowser.currentURI.spec)); + } + } + + initialTabs = null; + + sendAsyncMessage("cleanup-results", results); +}); diff --git a/toolkit/components/extensions/test/mochitest/head.js b/toolkit/components/extensions/test/mochitest/head.js index 1bcffb50f3ff..509d9848a5fd 100644 --- a/toolkit/components/extensions/test/mochitest/head.js +++ b/toolkit/components/extensions/test/mochitest/head.js @@ -1,5 +1,7 @@ "use strict"; +let {AppConstants} = SpecialPowers.Cu.import("resource://gre/modules/AppConstants.jsm", {}); + // We run tests under two different configurations, from mochitest.ini and // mochitest-remote.ini. When running from mochitest-remote.ini, the tests are // copied to the sub-directory "test-oop-extensions", which we detect here, and @@ -14,6 +16,22 @@ if (location.pathname.includes("test-oop-extensions")) { SpecialPowers.setIntPref("dom.ipc.keepProcessesAlive.extension", 1); } +if (AppConstants.MOZ_BUILD_APP === "browser") { + let chromeScript = SpecialPowers.loadChromeScript( + SimpleTest.getTestFileURL("chrome_cleanup_script.js")); + + SimpleTest.registerCleanupFunction(async () => { + chromeScript.sendAsyncMessage("check-cleanup"); + + let results = await chromeScript.promiseOneMessage("cleanup-results"); + chromeScript.destroy(); + + if (results.extraWindows.length || results.extraTabs.length) { + ok(false, `Test left extra windows or tabs: ${JSON.stringify(results)}\n`); + } + }); +} + /* exported waitForLoad */ function waitForLoad(win) { diff --git a/toolkit/components/extensions/test/mochitest/mochitest-common.ini b/toolkit/components/extensions/test/mochitest/mochitest-common.ini index 7188f93e63c3..c1468228a361 100644 --- a/toolkit/components/extensions/test/mochitest/mochitest-common.ini +++ b/toolkit/components/extensions/test/mochitest/mochitest-common.ini @@ -1,5 +1,6 @@ [DEFAULT] support-files = + chrome_cleanup_script.js head.js file_mixed.html head_webrequest.js diff --git a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_filter.html b/toolkit/components/extensions/test/mochitest/test_ext_webrequest_filter.html index 62e8159ad109..32319bacd69c 100644 --- a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_filter.html +++ b/toolkit/components/extensions/test/mochitest/test_ext_webrequest_filter.html @@ -172,6 +172,8 @@ add_task(function* test_webRequest_filter_background() { add_task(function* teardown() { testWindow.close(); + + yield new Promise(resolve => setTimeout(resolve, 0)); });