Skip to content

Instantly share code, notes, and snippets.

@kmaglione
Created January 31, 2017 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmaglione/18166a8578318aa0b64ba87c48e45d26 to your computer and use it in GitHub Desktop.
Save kmaglione/18166a8578318aa0b64ba87c48e45d26 to your computer and use it in GitHub Desktop.
changeset: 379613:8de9e957ada2
user: Kris Maglione <maglione.k@gmail.com>
date: Tue Jan 31 11:55:43 2017 -0800
summary: Bug 1260548: Part 9 - Make sure Android mochitests do not leave extra tabs open.
diff --git a/mobile/android/components/extensions/test/mochitest/head.js b/mobile/android/components/extensions/test/mochitest/head.js
--- a/mobile/android/components/extensions/test/mochitest/head.js
+++ b/mobile/android/components/extensions/test/mochitest/head.js
@@ -1,15 +1,31 @@
"use strict";
/* exported isPageActionShown clickPageAction */
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/PageActions.jsm");
+{
+ 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`);
+ }
+ });
+}
+
function isPageActionShown(uuid) {
return PageActions.isShown(uuid);
}
function clickPageAction(uuid) {
PageActions.synthesizeClick(uuid);
}
diff --git a/mobile/android/components/extensions/test/mochitest/mochitest.ini b/mobile/android/components/extensions/test/mochitest/mochitest.ini
--- a/mobile/android/components/extensions/test/mochitest/mochitest.ini
+++ b/mobile/android/components/extensions/test/mochitest/mochitest.ini
@@ -1,11 +1,12 @@
[DEFAULT]
support-files =
../../../../../../toolkit/components/extensions/test/mochitest/test_ext_all_apis.js
+ ../../../../../../toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
context.html
context_tabs_onUpdated_iframe.html
context_tabs_onUpdated_page.html
file_bypass_cache.sjs
file_dummy.html
file_iframe_document.html
file_iframe_document.sjs
tags = webextensions
diff --git a/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js b/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
--- a/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
+++ b/toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
@@ -1,47 +1,57 @@
"use strict";
/* global addMessageListener, sendAsyncMessage */
+Components.utils.import("resource://gre/modules/AppConstants.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
+let getBrowserApp, getTabBrowser;
+if (AppConstants.MOZ_BUILD_APP === "mobile/android") {
+ getBrowserApp = win => win.BrowserApp;
+ getTabBrowser = tab => tab.browser;
+} else {
+ getBrowserApp = win => win.gBrowser;
+ getTabBrowser = tab => tab.linkedBrowser;
+}
+
function* iterBrowserWindows() {
let enm = Services.wm.getEnumerator("navigator:browser");
while (enm.hasMoreElements()) {
let win = enm.getNext();
- if (!win.closed && win.gBrowser) {
+ if (!win.closed && getBrowserApp(win)) {
yield win;
}
}
}
let initialTabs = new Map();
for (let win of iterBrowserWindows()) {
- initialTabs.set(win, new Set(win.gBrowser.tabs));
+ initialTabs.set(win, new Set(getBrowserApp(win).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) {
+ for (let tab of getBrowserApp(win).tabs) {
if (!tabs.has(tab)) {
- results.extraTabs.push(tab.linkedBrowser.currentURI.spec);
+ results.extraTabs.push(getTabBrowser(tab).currentURI.spec);
}
}
} else {
results.extraWindows.push(
Array.from(win.gBrowser.tabs,
- tab => tab.linkedBrowser.currentURI.spec));
+ tab => getTabBrowser(tab).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
--- a/toolkit/components/extensions/test/mochitest/head.js
+++ b/toolkit/components/extensions/test/mochitest/head.js
@@ -1,27 +1,29 @@
"use strict";
-let {AppConstants} = SpecialPowers.Cu.import("resource://gre/modules/AppConstants.jsm", {});
+/* exported AppConstants */
+
+var {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
// use to select our configuration.
if (location.pathname.includes("test-oop-extensions")) {
SpecialPowers.pushPrefEnv({set: [
["dom.ipc.processCount.extension", 1],
["extensions.webextensions.remote", true],
]});
// We don't want to reset this at the end of the test, so that we don't have
// to spawn a new extension child process for each test unit.
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment