From f33b3b39c0217e36a7b0eddf440e434db461827f Mon Sep 17 00:00:00 2001 From: Jo IE Date: Mon, 7 Oct 2019 18:02:40 +0100 Subject: [PATCH 01/31] remove HTML entities from tooltip --- src/js/popup.js | 70 ++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/js/popup.js b/src/js/popup.js index 80986a2..e5b4dbf 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -12,15 +12,15 @@ const NEW_CONTAINER_ID = "new"; const ONBOARDING_STORAGE_KEY = "onboarding-stage"; // List of panels -const P_ONBOARDING_1 = "onboarding1"; -const P_ONBOARDING_2 = "onboarding2"; -const P_ONBOARDING_3 = "onboarding3"; -const P_ONBOARDING_4 = "onboarding4"; -const P_ONBOARDING_5 = "onboarding5"; -const P_CONTAINERS_LIST = "containersList"; -const P_CONTAINERS_EDIT = "containersEdit"; -const P_CONTAINER_INFO = "containerInfo"; -const P_CONTAINER_EDIT = "containerEdit"; +const P_ONBOARDING_1 = "onboarding1"; +const P_ONBOARDING_2 = "onboarding2"; +const P_ONBOARDING_3 = "onboarding3"; +const P_ONBOARDING_4 = "onboarding4"; +const P_ONBOARDING_5 = "onboarding5"; +const P_CONTAINERS_LIST = "containersList"; +const P_CONTAINERS_EDIT = "containersEdit"; +const P_CONTAINER_INFO = "containerInfo"; +const P_CONTAINER_EDIT = "containerEdit"; const P_CONTAINER_DELETE = "containerDelete"; const P_CONTAINERS_ACHIEVEMENT = "containersAchievement"; @@ -32,7 +32,7 @@ const P_CONTAINERS_ACHIEVEMENT = "containersAchievement"; * @return {string} The escaped string. */ function escapeXML(str) { - const replacements = {"&": "&", "\"": """, "'": "'", "<": "<", ">": ">", "/": "/"}; + const replacements = { "&": "&", "\"": """, "'": "'", "<": "<", ">": ">", "/": "/" }; return String(str).replace(/[&"'<>/]/g, m => replacements[m]); } @@ -85,7 +85,7 @@ const Logic = { try { await identitiesPromise; - } catch(e) { + } catch (e) { throw new Error("Failed to retrieve the identities or variation. We cannot continue. ", e.message); } @@ -125,7 +125,7 @@ const Logic = { async showAchievementOrContainersListPanel() { // Do we need to show an achievement panel? let showAchievements = false; - const achievementsStorage = await browser.storage.local.get({achievements: []}); + const achievementsStorage = await browser.storage.local.get({ achievements: [] }); for (const achievement of achievementsStorage.achievements) { if (!achievement.done) { showAchievements = true; @@ -142,7 +142,7 @@ const Logic = { // they have to click the "Done" button to stop the panel // from showing async setAchievementDone(achievementName) { - const achievementsStorage = await browser.storage.local.get({achievements: []}); + const achievementsStorage = await browser.storage.local.get({ achievements: [] }); const achievements = achievementsStorage.achievements; achievements.forEach((achievement, index, achievementsArray) => { if (achievement.name === achievementName) { @@ -150,7 +150,7 @@ const Logic = { achievementsArray[index] = achievement; } }); - browser.storage.local.set({achievements}); + browser.storage.local.set({ achievements }); }, setOnboardingStage(stage) { @@ -161,9 +161,9 @@ const Logic = { async clearBrowserActionBadge() { const extensionInfo = await getExtensionInfo(); - const storage = await browser.storage.local.get({browserActionBadgesClicked: []}); - browser.browserAction.setBadgeBackgroundColor({color: null}); - browser.browserAction.setBadgeText({text: ""}); + const storage = await browser.storage.local.get({ browserActionBadgesClicked: [] }); + browser.browserAction.setBadgeBackgroundColor({ color: null }); + browser.browserAction.setBadgeText({ text: "" }); storage.browserActionBadgesClicked.push(extensionInfo.version); // use set and spread to create a unique array const browserActionBadgesClicked = [...new Set(storage.browserActionBadgesClicked)]; @@ -184,7 +184,7 @@ const Logic = { // Handle old style rejection with null and also Promise.reject new style try { return await browser.contextualIdentities.get(cookieStoreId) || defaultContainer; - } catch(e) { + } catch (e) { return defaultContainer; } }, @@ -207,7 +207,7 @@ const Logic = { }, async currentTab() { - const activeTabs = await browser.tabs.query({active: true, windowId: browser.windows.WINDOW_ID_CURRENT}); + const activeTabs = await browser.tabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT }); if (activeTabs.length > 0) { return activeTabs[0]; } @@ -215,7 +215,7 @@ const Logic = { }, async numTabs() { - const activeTabs = await browser.tabs.query({windowId: browser.windows.WINDOW_ID_CURRENT}); + const activeTabs = await browser.tabs.query({ windowId: browser.windows.WINDOW_ID_CURRENT }); return activeTabs.length; }, @@ -259,7 +259,7 @@ const Logic = { getPanelSelector(panel) { if (this._onboardingVariation === "securityOnboarding" && - panel.hasOwnProperty("securityPanelSelector")) { + panel.hasOwnProperty("securityPanelSelector")) { return panel.securityPanelSelector; } else { return panel.panelSelector; @@ -333,7 +333,7 @@ const Logic = { return browser.runtime.sendMessage({ method: "deleteContainer", - message: {userContextId} + message: { userContextId } }); }, @@ -347,7 +347,7 @@ const Logic = { getAssignmentObjectByContainer(userContextId) { return browser.runtime.sendMessage({ method: "getAssignmentObjectByContainer", - message: {userContextId} + message: { userContextId } }); }, @@ -376,7 +376,7 @@ const Logic = { }); // Here we find the first valid id. - for (let id = 1;; ++id) { + for (let id = 1; ; ++id) { if (ids.indexOf(id) === -1) { return defaultName + (id < 10 ? "0" : "") + id; } @@ -511,7 +511,7 @@ Logic.registerPanel(P_CONTAINERS_LIST, { }); Logic.addEnterHandler(document.querySelector("#edit-containers-link"), (e) => { - if (!e.target.classList.contains("disable-edit-containers")){ + if (!e.target.classList.contains("disable-edit-containers")) { Logic.showPanel(P_CONTAINERS_EDIT); } }); @@ -637,9 +637,9 @@ Logic.registerPanel(P_CONTAINERS_LIST, { context.classList.add("userContext-wrapper", "open-newtab", "clickable"); manage.classList.add("show-tabs", "pop-button"); - manage.title = escaped`View ${identity.name} container`; + manage.setAttribute("title", `View ${identity.name} container`); context.setAttribute("tabindex", "0"); - context.title = escaped`Create ${identity.name} tab`; + context.setAttribute("title", `Create ${identity.name} tab`); context.innerHTML = escaped`
{ if (e.target.matches(".open-newtab") - || e.target.parentNode.matches(".open-newtab") - || e.type === "keydown") { + || e.target.parentNode.matches(".open-newtab") + || e.type === "keydown") { try { browser.tabs.create({ cookieStoreId: identity.cookieStoreId @@ -747,7 +747,7 @@ Logic.registerPanel(P_CONTAINER_INFO, { return; } else if (numTabs === 1) { Logic._disableMoveTabs("Cannot move a tab from a single-tab window."); - return; + return; } Logic.addEnterHandler(moveTabsEl, async () => { await browser.runtime.sendMessage({ @@ -808,7 +808,7 @@ Logic.registerPanel(P_CONTAINER_INFO, {
${tab.title}
`; tr.querySelector("td").appendChild(Utils.createFavIconElement(tab.favIconUrl)); document.getElementById("container-info-table").appendChild(fragment); - + // On click, we activate this tab. But only if this tab is active. if (!tab.hiddenState) { const closeImage = document.createElement("img"); @@ -830,7 +830,7 @@ Logic.registerPanel(P_CONTAINER_INFO, { tr.classList.add("clickable"); Logic.addEnterHandler(tr, async () => { - await browser.tabs.update(tab.id, {active: true}); + await browser.tabs.update(tab.id, { active: true }); window.close(); }); @@ -842,7 +842,7 @@ Logic.registerPanel(P_CONTAINER_INFO, { }); } } - } + } }, }); @@ -1018,7 +1018,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, { return escaped`
From 426e81b88ba62e77488944529309299a789c3a06 Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:02:02 +0100 Subject: [PATCH 25/31] Fix eslint errors after bump to 6.6.0 (#1595) * Bump eslint from 3.19.0 to 6.6.0 Bumps [eslint](https://github.com/eslint/eslint) from 3.19.0 to 6.6.0. - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/master/CHANGELOG.md) - [Commits](https://github.com/eslint/eslint/compare/v3.19.0...v6.6.0) Signed-off-by: dependabot[bot] * Fix (and suppress) eslint errors --- package.json | 2 +- src/js/background/messageHandler.js | 1 + src/js/popup.js | 25 +++++++++++++------------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index b72d3d5..eb31d53 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "addons-linter": "^1.3.2", "ajv": "^6.6.2", "chai": "^4.1.2", - "eslint": "^3.17.1", + "eslint": "^6.6.0", "eslint-plugin-no-unsanitized": "^2.0.0", "eslint-plugin-promise": "^3.4.0", "htmllint-cli": "0.0.7", diff --git a/src/js/background/messageHandler.js b/src/js/background/messageHandler.js index fa95181..9578e6e 100644 --- a/src/js/background/messageHandler.js +++ b/src/js/background/messageHandler.js @@ -82,6 +82,7 @@ const messageHandler = { if (!extensionInfo.permissions.includes("contextualIdentities")) { throw new Error("Missing contextualIdentities permission"); } + // eslint-disable-next-line require-atomic-updates externalExtensionAllowed[sender.id] = true; } let response; diff --git a/src/js/popup.js b/src/js/popup.js index 433db3c..64dca45 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -259,6 +259,7 @@ const Logic = { getPanelSelector(panel) { if (this._onboardingVariation === "securityOnboarding" && + // eslint-disable-next-line no-prototype-builtins panel.hasOwnProperty("securityPanelSelector")) { return panel.securityPanelSelector; } else { @@ -570,21 +571,21 @@ Logic.registerPanel(P_CONTAINERS_LIST, { break; } case 39: - { - const showTabs = element.parentNode.querySelector(".show-tabs"); - if(showTabs) { - showTabs.click(); - } - break; + { + const showTabs = element.parentNode.querySelector(".show-tabs"); + if(showTabs) { + showTabs.click(); } + break; + } case 37: - { - const hideTabs = document.querySelector(".panel-back-arrow"); - if(hideTabs) { - hideTabs.click(); - } - break; + { + const hideTabs = document.querySelector(".panel-back-arrow"); + if(hideTabs) { + hideTabs.click(); } + break; + } default: if ((e.keyCode >= 49 && e.keyCode <= 57) && Logic._currentPanel === "containersList") { From 96ab4685e922e5120aac5245a5ae77bd94952aa0 Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:13:41 +0100 Subject: [PATCH 26/31] Bump test dependencies --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index eb31d53..3bfcfba 100644 --- a/package.json +++ b/package.json @@ -11,17 +11,17 @@ "devDependencies": { "addons-linter": "^1.3.2", "ajv": "^6.6.2", - "chai": "^4.1.2", + "chai": "^4.2.0", "eslint": "^6.6.0", "eslint-plugin-no-unsanitized": "^2.0.0", "eslint-plugin-promise": "^3.4.0", "htmllint-cli": "0.0.7", "jsdom": "^11.6.2", "json": "^9.0.6", - "mocha": "^5.0.0", + "mocha": "^6.2.2", "npm-run-all": "^4.0.0", - "sinon": "^4.4.0", - "sinon-chai": "^2.14.0", + "sinon": "^7.5.0", + "sinon-chai": "^3.3.0", "stylelint": "^7.9.0", "stylelint-config-standard": "^16.0.0", "stylelint-order": "^0.3.0", From 77fd843b3de62903f56ffd72ce92421610bebfab Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:18:10 +0100 Subject: [PATCH 27/31] Replace manual mock/jsdom with webextensions-jsdom --- package.json | 4 +- test/.eslintrc.js | 4 ++ test/browser.mock.js | 149 ------------------------------------------- test/helper.js | 63 +++++++++--------- test/setup.js | 110 +++++++++++++------------------- 5 files changed, 81 insertions(+), 249 deletions(-) delete mode 100644 test/browser.mock.js diff --git a/package.json b/package.json index 3bfcfba..bf05207 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ "eslint-plugin-no-unsanitized": "^2.0.0", "eslint-plugin-promise": "^3.4.0", "htmllint-cli": "0.0.7", - "jsdom": "^11.6.2", "json": "^9.0.6", "mocha": "^6.2.2", "npm-run-all": "^4.0.0", @@ -25,7 +24,8 @@ "stylelint": "^7.9.0", "stylelint-config-standard": "^16.0.0", "stylelint-order": "^0.3.0", - "web-ext": "^2.9.3" + "web-ext": "^2.9.3", + "webextensions-jsdom": "^1.1.0" }, "homepage": "https://github.com/mozilla/multi-account-containers#readme", "license": "MPL-2.0", diff --git a/test/.eslintrc.js b/test/.eslintrc.js index 0b29fac..d32e265 100644 --- a/test/.eslintrc.js +++ b/test/.eslintrc.js @@ -3,10 +3,14 @@ module.exports = { "node": true, "mocha": true }, + "parserOptions": { + "ecmaVersion": 2018 + }, globals: { "sinon": false, "expect": false, "nextTick": false, + "buildDom": false, "buildBackgroundDom": false, "background": false, "buildPopupDom": false, diff --git a/test/browser.mock.js b/test/browser.mock.js deleted file mode 100644 index ad52c3d..0000000 --- a/test/browser.mock.js +++ /dev/null @@ -1,149 +0,0 @@ -module.exports = () => { - const _storage = {}; - - // could maybe be replaced by https://github.com/acvetkov/sinon-chrome - const browserMock = { - _storage, - runtime: { - onMessage: { - addListener: sinon.stub(), - }, - onMessageExternal: { - addListener: sinon.stub(), - }, - sendMessage: sinon.stub().resolves(), - }, - webRequest: { - onBeforeRequest: { - addListener: sinon.stub() - }, - onCompleted: { - addListener: sinon.stub() - }, - onErrorOccurred: { - addListener: sinon.stub() - } - }, - windows: { - getCurrent: sinon.stub().resolves({}), - onFocusChanged: { - addListener: sinon.stub(), - } - }, - tabs: { - onActivated: { - addListener: sinon.stub() - }, - onCreated: { - addListener: sinon.stub() - }, - onUpdated: { - addListener: sinon.stub() - }, - sendMessage: sinon.stub(), - query: sinon.stub().resolves([{}]), - get: sinon.stub(), - create: sinon.stub().resolves({}), - remove: sinon.stub().resolves() - }, - history: { - deleteUrl: sinon.stub() - }, - storage: { - local: { - get: sinon.stub(), - set: sinon.stub() - } - }, - contextualIdentities: { - create: sinon.stub(), - get: sinon.stub(), - query: sinon.stub().resolves([]), - onCreated: { - addListener: sinon.stub() - }, - onUpdated: { - addListener: sinon.stub() - }, - onRemoved: { - addListener: sinon.stub() - } - }, - contextMenus: { - create: sinon.stub(), - remove: sinon.stub(), - onClicked: { - addListener: sinon.stub() - } - }, - browserAction: { - setBadgeBackgroundColor: sinon.stub(), - setBadgeText: sinon.stub() - }, - management: { - get: sinon.stub(), - onInstalled: { - addListener: sinon.stub() - }, - onUninstalled: { - addListener: sinon.stub() - } - }, - extension: { - getURL: sinon.stub().returns("moz-extension://multi-account-containers/confirm-page.html") - }, - permissions: { - contains: sinon.stub().returns(true) - } - }; - - // inmemory local storage - browserMock.storage.local = { - get: sinon.spy(async key => { - if (!key) { - return _storage; - } - let result = {}; - if (Array.isArray(key)) { - key.map(akey => { - if (typeof _storage[akey] !== "undefined") { - result[akey] = _storage[akey]; - } - }); - } else if (typeof key === "object") { - // TODO support nested objects - Object.keys(key).map(oKey => { - if (typeof _storage[oKey] !== "undefined") { - result[oKey] = _storage[oKey]; - } else { - result[oKey] = key[oKey]; - } - }); - } else { - result = _storage[key]; - } - return result; - }), - set: sinon.spy(async (key, value) => { - if (typeof key === "object") { - // TODO support nested objects - Object.keys(key).map(oKey => { - _storage[oKey] = key[oKey]; - }); - } else { - _storage[key] = value; - } - }), - remove: sinon.spy(async (key) => { - if (Array.isArray(key)) { - key.map(aKey => { - delete _storage[aKey]; - }); - } else { - delete _storage[key]; - } - }), - }; - - return browserMock; -}; diff --git a/test/helper.js b/test/helper.js index 555ee7a..2704bac 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,47 +1,44 @@ module.exports = { browser: { - async initializeWithTab(tab) { - await buildBackgroundDom({ - beforeParse(window) { - window.browser.tabs.get.resolves(tab); - window.browser.tabs.query.resolves([tab]); - window.browser.contextualIdentities.get.resolves({ - cookieStoreId: tab.cookieStoreId - }); - } - }); - await buildPopupDom({ - beforeParse(window) { - window.browser.tabs.get.resolves(tab); - window.browser.tabs.query.resolves([tab]); + async initializeWithTab(details = { + cookieStoreId: "firefox-default" + }) { + let tab; + await buildDom({ + background: { + async afterBuild(background) { + tab = await background.browser.tabs._create(details); + } + }, + popup: { + jsdom: { + beforeParse(window) { + window.browser.storage.local.set({ + "browserActionBadgesClicked": [], + "onboarding-stage": 5, + "achievements": [] + }); + window.browser.storage.local.set.resetHistory(); + } + } } }); + + return tab; }, async openNewTab(tab, options = {}) { - if (options.resetHistory) { - background.browser.tabs.create.resetHistory(); - background.browser.tabs.remove.resetHistory(); - } - background.browser.tabs.get.resolves(tab); - background.browser.tabs.onCreated.addListener.yield(tab); - const [promise] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: tab.id, - url: tab.url, - requestId: options.requestId - }); - - return promise; + return background.browser.tabs._create(tab, options); } }, popup: { async clickElementById(id) { - const clickEvent = popup.document.createEvent("HTMLEvents"); - clickEvent.initEvent("click"); - popup.document.getElementById(id).dispatchEvent(clickEvent); - await nextTick(); + await popup.helper.clickElementById(id); + }, + + async clickLastMatchingElementByQuerySelector(querySelector) { + await popup.helper.clickElementByQuerySelectorAll(querySelector, "last"); } - }, + } }; diff --git a/test/setup.js b/test/setup.js index b6d563d..0cc45ee 100644 --- a/test/setup.js +++ b/test/setup.js @@ -2,7 +2,6 @@ if (!process.listenerCount("unhandledRejection")) { // eslint-disable-next-line no-console process.on("unhandledRejection", r => console.log(r)); } -const jsdom = require("jsdom"); const path = require("path"); const chai = require("chai"); const sinonChai = require("sinon-chai"); @@ -19,83 +18,64 @@ global.nextTick = () => { }; global.helper = require("./helper"); -const browserMock = require("./browser.mock"); -const srcBasePath = path.resolve(path.join(__dirname, "..", "src")); -const srcJsBackgroundPath = path.join(srcBasePath, "js", "background"); -global.buildBackgroundDom = async (options = {}) => { - const dom = await jsdom.JSDOM.fromFile(path.join(srcJsBackgroundPath, "index.html"), { - runScripts: "dangerously", - resources: "usable", - virtualConsole: (new jsdom.VirtualConsole).sendTo(console), - beforeParse(window) { - window.browser = browserMock(); - window.fetch = sinon.stub().resolves({ - json: sinon.stub().resolves({}) - }); - if (options.beforeParse) { - options.beforeParse(window); +const webExtensionsJSDOM = require("webextensions-jsdom"); +const manifestPath = path.resolve(path.join(__dirname, "../src/manifest.json")); + +global.buildDom = async ({background = {}, popup = {}}) => { + background = { + ...background, + jsdom: { + ...background.jsom, + beforeParse(window) { + window.browser.permissions.getAll.resolves({permissions: ["bookmarks"]}); } } - }); - await new Promise(resolve => { - dom.window.document.addEventListener("DOMContentLoaded", resolve); - }); - await nextTick(); - - global.background = { - dom, - browser: dom.window.browser }; -}; -global.buildPopupDom = async (options = {}) => { - const dom = await jsdom.JSDOM.fromFile(path.join(srcBasePath, "popup.html"), { - runScripts: "dangerously", - resources: "usable", - virtualConsole: (new jsdom.VirtualConsole).sendTo(console), - beforeParse(window) { - window.browser = browserMock(); - window.browser.storage.local.set("browserActionBadgesClicked", []); - window.browser.storage.local.set("onboarding-stage", 5); - window.browser.storage.local.set("achievements", []); - window.browser.storage.local.set.resetHistory(); - window.fetch = sinon.stub().resolves({ - json: sinon.stub().resolves({}) - }); - - if (options.beforeParse) { - options.beforeParse(window); - } + popup = { + ...popup, + jsdom: { + ...popup.jsdom, + pretendToBeVisual: true } - }); - await new Promise(resolve => { - dom.window.document.addEventListener("DOMContentLoaded", resolve); - }); - await nextTick(); - dom.window.browser.runtime.sendMessage.resetHistory(); - - if (global.background) { - dom.window.browser.runtime.sendMessage = sinon.spy(function() { - global.background.browser.runtime.onMessage.addListener.yield(...arguments); - }); - } - - global.popup = { - dom, - document: dom.window.document, - browser: dom.window.browser }; + + const webExtension = await webExtensionsJSDOM.fromManifest(manifestPath, { + apiFake: true, + wiring: true, + sinon: global.sinon, + background, + popup + }); + + // eslint-disable-next-line require-atomic-updates + global.background = webExtension.background; + // eslint-disable-next-line require-atomic-updates + global.popup = webExtension.popup; }; +global.buildBackgroundDom = async background => { + await global.buildDom({ + background, + popup: false + }); +}; + +global.buildPopupDom = async popup => { + await global.buildDom({ + popup, + background: false + }); +}; + + global.afterEach(() => { if (global.background) { - global.background.dom.window.close(); - delete global.background; + global.background.destroy(); } if (global.popup) { - global.popup.dom.window.close(); - delete global.popup; + global.popup.destroy(); } }); From 5eb79949e9876a45aba96b8103ed43ec0c996371 Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:19:01 +0100 Subject: [PATCH 28/31] Adapt existing tests to use webextensions-jsdom --- test/features/assignment.test.js | 49 ++-- test/features/external-webextensions.test.js | 48 ++-- test/issues/940.test.js | 236 +++++++++---------- 3 files changed, 153 insertions(+), 180 deletions(-) diff --git a/test/features/assignment.test.js b/test/features/assignment.test.js index 632c60b..43bb981 100644 --- a/test/features/assignment.test.js +++ b/test/features/assignment.test.js @@ -1,12 +1,12 @@ describe("Assignment Feature", () => { - const activeTab = { - id: 1, - cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0 - }; + const url = "http://example.com"; + + let activeTab; beforeEach(async () => { - await helper.browser.initializeWithTab(activeTab); + activeTab = await helper.browser.initializeWithTab({ + cookieStoreId: "firefox-container-1", + url + }); }); describe("click the 'Always open in' checkbox in the popup", () => { @@ -16,23 +16,24 @@ describe("Assignment Feature", () => { }); describe("open new Tab with the assigned URL in the default container", () => { - const newTab = { - id: 2, - cookieStoreId: "firefox-default", - url: activeTab.url, - index: 1, - active: true - }; + let newTab; beforeEach(async () => { // new Tab opening activeTab.url in default container - await helper.browser.openNewTab(newTab); + newTab = await helper.browser.openNewTab({ + cookieStoreId: "firefox-default", + url + }, { + options: { + webRequestError: true // because request is canceled due to reopening + } + }); }); it("should open the confirm page", async () => { // should have created a new tab with the confirm page - background.browser.tabs.create.should.have.been.calledWith({ - url: "moz-extension://multi-account-containers/confirm-page.html?" + - `url=${encodeURIComponent(activeTab.url)}` + + background.browser.tabs.create.should.have.been.calledWithMatch({ + url: "moz-extension://fake/confirm-page.html?" + + `url=${encodeURIComponent(url)}` + `&cookieStoreId=${activeTab.cookieStoreId}`, cookieStoreId: undefined, openerTabId: null, @@ -53,16 +54,12 @@ describe("Assignment Feature", () => { }); describe("open new Tab with the no longer assigned URL in the default container", () => { - const newTab = { - id: 3, - cookieStoreId: "firefox-default", - url: activeTab.url, - index: 3, - active: true - }; beforeEach(async () => { // new Tab opening activeTab.url in default container - await helper.browser.openNewTab(newTab); + await helper.browser.openNewTab({ + cookieStoreId: "firefox-default", + url + }); }); it("should not open the confirm page", async () => { diff --git a/test/features/external-webextensions.test.js b/test/features/external-webextensions.test.js index fe32e4d..30e6b49 100644 --- a/test/features/external-webextensions.test.js +++ b/test/features/external-webextensions.test.js @@ -1,12 +1,11 @@ describe("External Webextensions", () => { - const activeTab = { - id: 1, - cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0 - }; + const url = "http://example.com"; + beforeEach(async () => { - await helper.browser.initializeWithTab(activeTab); + await helper.browser.initializeWithTab({ + cookieStoreId: "firefox-container-1", + url + }); await helper.popup.clickElementById("container-page-assigned"); }); @@ -18,25 +17,18 @@ describe("External Webextensions", () => { const message = { method: "getAssignment", - url: "http://example.com" + url }; const sender = { id: "external-webextension" }; - // currently not possible to get the return value of yielding with sinon - // so we expect that if no error is thrown and the storage was called, everything is ok - // maybe i get around to provide a PR https://github.com/sinonjs/sinon/issues/903 - // - // the alternative would be to expose the actual messageHandler and call it directly - // but personally i think that goes against the black-box-ish nature of these feature tests - const rejectionStub = sinon.stub(); - process.on("unhandledRejection", rejectionStub); - background.browser.runtime.onMessageExternal.addListener.yield(message, sender); - await nextTick(); - process.removeListener("unhandledRejection", rejectionStub); - rejectionStub.should.not.have.been.called; - background.browser.storage.local.get.should.have.been.called; + const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender); + const answer = await promise; + expect(answer).to.deep.equal({ + userContextId: "1", + neverAsk: false + }); }); }); @@ -48,20 +40,16 @@ describe("External Webextensions", () => { const message = { method: "getAssignment", - url: "http://example.com" + url }; const sender = { id: "external-webextension" }; - const rejectionStub = sinon.spy(); - process.on("unhandledRejection", rejectionStub); - background.browser.runtime.onMessageExternal.addListener.yield(message, sender); - await nextTick(); - process.removeListener("unhandledRejection", rejectionStub); - rejectionStub.should.have.been.calledWith(sinon.match({ - message: "Missing contextualIdentities permission" - })); + const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender); + return promise.catch(error => { + expect(error.message).to.equal("Missing contextualIdentities permission"); + }); }); }); }); diff --git a/test/issues/940.test.js b/test/issues/940.test.js index b889035..4a7eb0e 100644 --- a/test/issues/940.test.js +++ b/test/issues/940.test.js @@ -1,112 +1,82 @@ describe("#940", () => { describe("when other onBeforeRequestHandlers are faster and redirect with the same requestId", () => { it("should not open two confirm pages", async () => { - // init - const activeTab = { - id: 1, + await helper.browser.initializeWithTab({ cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0 - }; - await helper.browser.initializeWithTab(activeTab); - // assign the activeTab.url + url: "http://example.com" + }); await helper.popup.clickElementById("container-page-assigned"); - // start request and don't await the requests at all - // so the second request below is actually comparable to an actual redirect that also fires immediately - const newTab = { - id: 2, - cookieStoreId: "firefox-default", - url: activeTab.url, - index: 1, - active: true - }; - helper.browser.openNewTab(newTab, { - requestId: 1 + const responses = {}; + await helper.browser.openNewTab({ + url: "http://example.com" + }, { + options: { + webRequestRedirects: ["https://example.com"], + webRequestError: true, + instantRedirects: true + }, + responses }); - // other addon sees the same request - // and redirects to the https version of activeTab.url - // since it's a redirect the request has the same requestId - background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://example.com", - requestId: 1 + const result = await responses.webRequest.onBeforeRequest[1]; + expect(result).to.deep.equal({ + cancel: true }); - await nextTick(); - background.browser.tabs.create.should.have.been.calledOnce; }); }); describe("when redirects change requestId midflight", () => { - let promiseResults; + let newTab; + const newTabResponses = {}; + const redirectedRequest = async (options = {}) => { + global.clock = sinon.useFakeTimers(); + newTab = await helper.browser.openNewTab({ + url: "http://youtube.com" + }, { + options: Object.assign({ + webRequestRedirects: [ + "https://youtube.com", + "https://www.youtube.com", + { + url: "https://www.youtube.com", + webRequest: { + requestId: 2 + } + } + ], + webRequestError: true, + instantRedirects: true + }, options), + responses: newTabResponses + }); + }; + beforeEach(async () => { - // init - const activeTab = { - id: 1, + await helper.browser.initializeWithTab({ cookieStoreId: "firefox-container-1", - url: "https://www.youtube.com", - index: 0 - }; - await helper.browser.initializeWithTab(activeTab); - // assign the activeTab.url + url: "https://www.youtube.com" + }); await helper.popup.clickElementById("container-page-assigned"); - - // http://youtube.com - const newTab = { - id: 2, - cookieStoreId: "firefox-default", - url: "http://youtube.com", - index: 1, - active: true - }; - const promise1 = helper.browser.openNewTab(newTab, { - requestId: 1 - }); - - // https://youtube.com - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://youtube.com", - requestId: 1 - }); - - // https://www.youtube.com - const [promise3] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://www.youtube.com", - requestId: 1 - }); - - // https://www.youtube.com - const [promise4] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://www.youtube.com", - requestId: 2 - }); - - promiseResults = await Promise.all([promise1, promise2, promise3, promise4]); }); it("should not open two confirm pages", async () => { + await redirectedRequest(); + // http://youtube.com is not assigned, no cancel, no reopening - expect(promiseResults[0]).to.deep.equal({}); + expect(await newTabResponses.webRequest.onBeforeRequest[0]).to.deep.equal({}); // https://youtube.com is not assigned, no cancel, no reopening - expect(promiseResults[1]).to.deep.equal({}); + expect(await newTabResponses.webRequest.onBeforeRequest[1]).to.deep.equal({}); // https://www.youtube.com is assigned, this triggers reopening, cancel - expect(promiseResults[2]).to.deep.equal({ + expect(await newTabResponses.webRequest.onBeforeRequest[2]).to.deep.equal({ cancel: true }); // https://www.youtube.com is assigned, this was a redirect, cancel early, no reopening - expect(promiseResults[3]).to.deep.equal({ + expect(await newTabResponses.webRequest.onBeforeRequest[3]).to.deep.equal({ cancel: true }); @@ -114,67 +84,85 @@ describe("#940", () => { }); it("should uncancel after webRequest.onCompleted", async () => { - const [promise1] = background.browser.webRequest.onCompleted.addListener.yield({ - tabId: 2 + await redirectedRequest(); + // remove onCompleted listeners because in the real world this request would never complete + // and thus might trigger unexpected behavior because the tab gets removed when reopening + background.browser.webRequest.onCompleted.addListener = sinon.stub(); + background.browser.tabs.create.resetHistory(); + // we create a tab with the same id and use the same request id to see if uncanceled + await helper.browser.openNewTab({ + id: newTab.id, + url: "https://www.youtube.com" + }, { + options: { + webRequest: { + requestId: newTabResponses.webRequest.request.requestId + } + } }); - await promise1; - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: 2, - url: "https://www.youtube.com", - requestId: 123 - }); - await promise2; - - background.browser.tabs.create.should.have.been.calledTwice; + background.browser.tabs.create.should.have.been.calledOnce; }); it("should uncancel after webRequest.onErrorOccurred", async () => { - const [promise1] = background.browser.webRequest.onErrorOccurred.addListener.yield({ - tabId: 2 + await redirectedRequest(); + background.browser.tabs.create.resetHistory(); + // we create a tab with the same id and use the same request id to see if uncanceled + await helper.browser.openNewTab({ + id: newTab.id, + url: "https://www.youtube.com" + }, { + options: { + webRequest: { + requestId: newTabResponses.webRequest.request.requestId + }, + webRequestError: true + } }); - await promise1; - // request to assigned url in same tab - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: 2, - url: "https://www.youtube.com", - requestId: 123 - }); - await promise2; - - background.browser.tabs.create.should.have.been.calledTwice; + background.browser.tabs.create.should.have.been.calledOnce; }); it("should uncancel after 2 seconds", async () => { - await new Promise(resolve => setTimeout(resolve, 2000)); - // request to assigned url in same tab - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: 2, - url: "https://www.youtube.com", - requestId: 123 + await redirectedRequest({ + webRequestDontYield: ["onCompleted", "onErrorOccurred"] }); - await promise2; + global.clock.tick(2000); - background.browser.tabs.create.should.have.been.calledTwice; - }).timeout(2002); + background.browser.tabs.create.resetHistory(); + // we create a tab with the same id and use the same request id to see if uncanceled + await helper.browser.openNewTab({ + id: newTab.id, + url: "https://www.youtube.com" + }, { + options: { + webRequest: { + requestId: newTabResponses.webRequest.request.requestId + }, + webRequestError: true + } + }); + + background.browser.tabs.create.should.have.been.calledOnce; + }); it("should not influence the canceled url in other tabs", async () => { - const newTab = { - id: 123, + await redirectedRequest(); + background.browser.tabs.create.resetHistory(); + await helper.browser.openNewTab({ cookieStoreId: "firefox-default", - url: "https://www.youtube.com", - index: 10, - active: true - }; - await helper.browser.openNewTab(newTab, { - requestId: 321 + url: "https://www.youtube.com" + }, { + options: { + webRequestError: true + } }); - background.browser.tabs.create.should.have.been.calledTwice; + background.browser.tabs.create.should.have.been.calledOnce; + }); + + afterEach(() => { + global.clock.restore(); }); }); }); From 703a7cd3c20c2c8be4ef744471a1a3eef8fa2d6b Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:24:05 +0100 Subject: [PATCH 29/31] Add test coverage with nyc --- .eslintignore | 1 + .gitignore | 4 ++++ package.json | 6 ++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.eslintignore b/.eslintignore index 9f1953c..3a5b5a0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ lib/testpilot/*.js +coverage \ No newline at end of file diff --git a/.gitignore b/.gitignore index c745683..ff6bfab 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ src/web-ext-artifacts/* # JetBrains IDE files .idea + +# IstanbulJS +.nyc_output +coverage \ No newline at end of file diff --git a/package.json b/package.json index bf05207..753f292 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "json": "^9.0.6", "mocha": "^6.2.2", "npm-run-all": "^4.0.0", + "nyc": "^14.1.1", "sinon": "^7.5.0", "sinon-chai": "^3.3.0", "stylelint": "^7.9.0", @@ -42,7 +43,8 @@ "lint:html": "htmllint *.html", "lint:js": "eslint .", "package": "rm -rf src/web-ext-artifacts && npm run build && mv src/web-ext-artifacts/firefox_multi-account_containers-*.zip addon.xpi", - "test": "npm run lint && mocha ./test/setup.js test/**/*.test.js", - "test-watch": "mocha ./test/setup.js test/**/*.test.js --watch" + "test": "npm run lint && npm run coverage", + "test-watch": "mocha ./test/setup.js test/**/*.test.js --watch", + "coverage": "nyc --reporter=html --reporter=text mocha ./test/setup.js test/**/*.test.js --timeout 60000" } } From e37440720f91eec22f099e2c9f8e95ed6ea5046d Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:24:17 +0100 Subject: [PATCH 30/31] Add container feature test --- test/features/containers.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/features/containers.test.js diff --git a/test/features/containers.test.js b/test/features/containers.test.js new file mode 100644 index 0000000..ac5bccd --- /dev/null +++ b/test/features/containers.test.js @@ -0,0 +1,28 @@ +describe("Containers Management", () => { + beforeEach(async () => { + await helper.browser.initializeWithTab(); + }); + + describe("creating a new container", () => { + beforeEach(async () => { + await helper.popup.clickElementById("container-add-link"); + await helper.popup.clickElementById("edit-container-ok-link"); + }); + + it("should create it in the browser as well", () => { + background.browser.contextualIdentities.create.should.have.been.calledOnce; + }); + + describe("removing it afterwards", () => { + beforeEach(async () => { + await helper.popup.clickElementById("edit-containers-link"); + await helper.popup.clickLastMatchingElementByQuerySelector(".delete-container-icon"); + await helper.popup.clickElementById("delete-container-ok-link"); + }); + + it("should remove it in the browser as well", () => { + background.browser.contextualIdentities.remove.should.have.been.calledOnce; + }); + }); + }); +}); \ No newline at end of file From dc9e8f6399ffc1d434254a56ab64cb20a77e6cd4 Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:24:52 +0100 Subject: [PATCH 31/31] Add test for unresolved issue See: https://github.com/mozilla/multi-account-containers/issues/1168#issuecomment-378394091 --- test/issues/1168.test.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/issues/1168.test.js diff --git a/test/issues/1168.test.js b/test/issues/1168.test.js new file mode 100644 index 0000000..50f463a --- /dev/null +++ b/test/issues/1168.test.js @@ -0,0 +1,34 @@ +describe("#1168", () => { + describe("when navigation happens too slow after opening new tab to a page which then redirects", () => { + let clock, tab; + + beforeEach(async () => { + await helper.browser.initializeWithTab({ + cookieStoreId: "firefox-container-1", + url: "https://bugzilla.mozilla.org" + }); + await helper.popup.clickElementById("container-page-assigned"); + + clock = sinon.useFakeTimers(); + tab = await helper.browser.openNewTab({}); + + clock.tick(2000); + + await background.browser.tabs._navigate(tab.id, "https://duckduckgo.com/?q=%21bugzilla+thing&t=ffab"); + await background.browser.tabs._redirect(tab.id, [ + "https://bugzilla.mozilla.org" + ]); + }); + + // Not solved yet + // See: https://github.com/mozilla/multi-account-containers/issues/1168#issuecomment-378394091 + it.skip("should remove the old tab", async () => { + expect(background.browser.tabs.create).to.have.been.calledOnce; + expect(background.browser.tabs.remove).to.have.been.calledWith(tab.id); + }); + + afterEach(() => { + clock.restore(); + }); + }); +}); \ No newline at end of file