Skip to content

Commit

Permalink
Bug 1190321: [webext] Complete test coverage for the browserAction AP…
Browse files Browse the repository at this point in the history
…I. r=gabor

--HG--
rename : browser/components/extensions/test/browser/browser_ext_pageAction_popup.js => browser/components/extensions/test/browser/browser_ext_browserAction_popup.js
extra : commitid : KNrOPlO2bOP
extra : rebase_source : 367ab2d367b76512485920a17c8f979b0668beb3
extra : histedit_source : 2d114fc7778b21a9e5cd085a1b78d21b172376bd
  • Loading branch information
kmaglione committed Nov 15, 2015
1 parent e716dce commit 29636e2
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 33 deletions.
1 change: 1 addition & 0 deletions browser/components/extensions/test/browser/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ support-files =
[browser_ext_browserAction_disabled.js]
[browser_ext_pageAction_context.js]
[browser_ext_pageAction_popup.js]
[browser_ext_browserAction_popup.js]
[browser_ext_contextMenus.js]
[browser_ext_getViews.js]
[browser_ext_tabs_executeScript.js]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,24 @@ add_task(function* testTabSwitchContext() {
var tests = [
expect => {
browser.test.log("Initial state, expect default properties.");
expect(details[0]);
expectDefaults(details[0]).then(() => {
expect(details[0]);
});
},
expect => {
browser.test.log("Change the icon in the current tab. Expect default properties excluding the icon.");
browser.browserAction.setIcon({ tabId: tabs[0], path: "1.png" });
expect(details[1]);
expectDefaults(details[0]).then(() => {
expect(details[1]);
});
},
expect => {
browser.test.log("Create a new tab. Expect default properties.");
browser.tabs.create({ active: true, url: "about:blank?0" }, tab => {
tabs.push(tab.id);
expect(details[0]);
expectDefaults(details[0]).then(() => {
expect(details[0]);
});
});
},
expect => {
Expand All @@ -80,7 +86,9 @@ add_task(function* testTabSwitchContext() {
browser.browserAction.setBadgeBackgroundColor({ tabId, color: [0xff, 0, 0] });
browser.browserAction.disable(tabId);

expect(details[2]);
expectDefaults(details[0]).then(() => {
expect(details[2]);
});
},
expect => {
browser.test.log("Navigate to a new page. Expect no changes.");
Expand Down Expand Up @@ -110,17 +118,23 @@ add_task(function* testTabSwitchContext() {
browser.browserAction.setBadgeText({ text: "d2" });
browser.browserAction.setBadgeBackgroundColor({ color: [0, 0xff, 0] });
browser.browserAction.disable();
expect(details[3]);
expectDefaults(details[3]).then(() => {
expect(details[3]);
});
},
expect => {
browser.test.log("Re-enable by default. Expect enabled.");
browser.browserAction.enable();
expect(details[4]);
expectDefaults(details[4]).then(() => {
expect(details[4]);
});
},
expect => {
browser.test.log("Switch back to tab 2. Expect former value, unaffected by changes to defaults in previous step.");
browser.tabs.update(tabs[1], { active: true }, () => {
expect(details[2]);
expectDefaults(details[3]).then(() => {
expect(details[2]);
});
});
},
expect => {
Expand All @@ -146,25 +160,40 @@ add_task(function* testTabSwitchContext() {

// Gets the current details of the browser action, and returns a
// promise that resolves to an object containing them.
function getDetails() {
return new Promise(resolve => {
return browser.tabs.query({ active: true, currentWindow: true }, resolve);
}).then(tabs => {
var tabId = tabs[0].id;

return Promise.all([
new Promise(resolve => browser.browserAction.getTitle({tabId}, resolve)),
new Promise(resolve => browser.browserAction.getPopup({tabId}, resolve)),
new Promise(resolve => browser.browserAction.getBadgeText({tabId}, resolve)),
new Promise(resolve => browser.browserAction.getBadgeBackgroundColor({tabId}, resolve))])
}).then(details => {
function getDetails(tabId) {
return Promise.all([
new Promise(resolve => browser.browserAction.getTitle({tabId}, resolve)),
new Promise(resolve => browser.browserAction.getPopup({tabId}, resolve)),
new Promise(resolve => browser.browserAction.getBadgeText({tabId}, resolve)),
new Promise(resolve => browser.browserAction.getBadgeBackgroundColor({tabId}, resolve))]
).then(details => {
return Promise.resolve({ title: details[0],
popup: details[1],
badge: details[2],
badgeBackgroundColor: details[3] });
});
}

function checkDetails(expecting, tabId) {
return getDetails(tabId).then(details => {
browser.test.assertEq(expecting.title, details.title,
"expected value from getTitle");

browser.test.assertEq(expecting.popup, details.popup,
"expected value from getPopup");

browser.test.assertEq(expecting.badge, details.badge,
"expected value from getBadge");

browser.test.assertEq(String(expecting.badgeBackgroundColor),
String(details.badgeBackgroundColor),
"expected value from getBadgeBackgroundColor");
});
}

function expectDefaults(expecting) {
return checkDetails(expecting);
}

// Runs the next test in the `tests` array, checks the results,
// and passes control back to the outer test scope.
Expand All @@ -174,20 +203,11 @@ add_task(function* testTabSwitchContext() {
test(expecting => {
// Check that the API returns the expected values, and then
// run the next test.
getDetails().then(details => {
browser.test.assertEq(expecting.title, details.title,
"expected value from getTitle");

browser.test.assertEq(expecting.popup, details.popup,
"expected value from getPopup");

browser.test.assertEq(expecting.badge, details.badge,
"expected value from getBadge");

browser.test.assertEq(String(expecting.badgeBackgroundColor),
String(details.badgeBackgroundColor),
"expected value from getBadgeBackgroundColor");

new Promise(resolve => {
return browser.tabs.query({ active: true, currentWindow: true }, resolve);
}).then(tabs => {
return checkDetails(expecting, tabs[0].id);
}).then(() => {
// Check that the actual icon has the expected values, then
// run the next test.
browser.test.sendMessage("nextTest", expecting, tests.length);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function promisePopupShown(popup) {
return new Promise(resolve => {
if (popup.popupOpen) {
resolve();
} else {
let onPopupShown = event => {
popup.removeEventListener("popupshown", onPopupShown);
resolve();
};
popup.addEventListener("popupshown", onPopupShown);
}
});
}

add_task(function* testPageActionPopup() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
"background": {
"page": "data/background.html"
},
"browser_action": {
"default_popup": "popup-a.html"
}
},

files: {
"popup-a.html": `<script src="popup-a.js"></script>`,
"popup-a.js": function() {
browser.runtime.sendMessage("from-popup-a");
},

"data/popup-b.html": `<script src="popup-b.js"></script>`,
"data/popup-b.js": function() {
browser.runtime.sendMessage("from-popup-b");
},

"data/background.html": `<script src="background.js"></script>`,

"data/background.js": function() {
var tests = [
() => {
sendClick({ expectEvent: false, expectPopup: "a" });
},
() => {
sendClick({ expectEvent: false, expectPopup: "a" });
},
() => {
browser.browserAction.setPopup({ popup: "popup-b.html" });
sendClick({ expectEvent: false, expectPopup: "b" });
},
() => {
sendClick({ expectEvent: false, expectPopup: "b" });
},
() => {
browser.browserAction.setPopup({ popup: "" });
sendClick({ expectEvent: true, expectPopup: null });
},
() => {
sendClick({ expectEvent: true, expectPopup: null });
},
() => {
browser.browserAction.setPopup({ popup: "/popup-a.html" });
sendClick({ expectEvent: false, expectPopup: "a" });
},
];

var expect = {};
function sendClick({ expectEvent, expectPopup }) {
expect = { event: expectEvent, popup: expectPopup };
browser.test.sendMessage("send-click");
}

browser.runtime.onMessage.addListener(msg => {
if (expect.popup) {
browser.test.assertEq(msg, `from-popup-${expect.popup}`,
"expected popup opened");
} else {
browser.test.fail("unexpected popup");
}

expect.popup = null;
browser.test.sendMessage("next-test");
});

browser.browserAction.onClicked.addListener(() => {
if (expect.event) {
browser.test.succeed("expected click event received");
} else {
browser.test.fail("unexpected click event");
}

expect.event = false;
browser.test.sendMessage("next-test");
});

browser.test.onMessage.addListener((msg) => {
if (msg != "next-test") {
browser.test.fail("Expecting 'next-test' message");
}

if (tests.length) {
var test = tests.shift();
test();
} else {
browser.test.notifyPass("browseraction-tests-done");
}
});

browser.test.sendMessage("next-test");
},
},
});

let browserActionId = makeWidgetId(extension.id) + "-browser-action";
let panelId = makeWidgetId(extension.id) + "-panel";

extension.onMessage("send-click", () => {
let button = document.getElementById(browserActionId);

EventUtils.synthesizeMouseAtCenter(button, {}, window);
});

extension.onMessage("next-test", Task.async(function* () {
let panel = document.getElementById(panelId);
if (panel) {
yield promisePopupShown(panel);
panel.hidePopup();

panel = document.getElementById(panelId);
is(panel, undefined, "panel successfully removed from document after hiding");
}

extension.sendMessage("next-test");
}));


yield Promise.all([extension.startup(), extension.awaitFinish("browseraction-tests-done")]);

yield extension.unload();

let panel = document.getElementById(panelId);
is(panel, undefined, "browserAction panel removed from document");
});

0 comments on commit 29636e2

Please sign in to comment.