Bug 1190663: [webext] Add tests for window.close() in browserAction and pageAction popups. r=rpl

MozReview-Commit-ID: 8r08YRw7IGr

--HG--
extra : rebase_source : 6a88ac0d76b7a9d4659737edec09e62d98a21a1b
This commit is contained in:
Kris Maglione 2016-02-27 15:29:42 -08:00
Родитель 6bf0866c5f
Коммит 67341496bf
4 изменённых файлов: 104 добавлений и 22 удалений

Просмотреть файл

@ -125,6 +125,19 @@ global.makeWidgetId = id => {
return id.replace(/[^a-z0-9_-]/g, "_"); return id.replace(/[^a-z0-9_-]/g, "_");
}; };
function promisePopupShown(popup) {
return new Promise(resolve => {
if (popup.state == "open") {
resolve();
} else {
popup.addEventListener("popupshown", function onPopupShown(event) {
popup.removeEventListener("popupshown", onPopupShown);
resolve();
});
}
});
}
class BasePopup { class BasePopup {
constructor(extension, viewNode, popupURL) { constructor(extension, viewNode, popupURL) {
let popupURI = Services.io.newURI(popupURL, null, extension.baseURI); let popupURI = Services.io.newURI(popupURL, null, extension.baseURI);
@ -254,6 +267,10 @@ class BasePopup {
// Resizes the browser to match the preferred size of the content. // Resizes the browser to match the preferred size of the content.
resizeBrowser() { resizeBrowser() {
if (!this.browser) {
return;
}
let width, height; let width, height;
try { try {
let w = {}, h = {}; let w = {}, h = {};
@ -310,7 +327,12 @@ global.PanelPopup = class PanelPopup extends BasePopup {
} }
closePopup() { closePopup() {
this.viewNode.hidePopup(); promisePopupShown(this.viewNode).then(() => {
// Make sure we're not already destroyed.
if (this.viewNode) {
this.viewNode.hidePopup();
}
});
} }
}; };

Просмотреть файл

@ -3,6 +3,8 @@
"use strict"; "use strict";
function* testInArea(area) { function* testInArea(area) {
let scriptPage = url => `<html><head><meta charset="utf-8"><script src="${url}"></script></head></html>`;
let extension = ExtensionTestUtils.loadExtension({ let extension = ExtensionTestUtils.loadExtension({
manifest: { manifest: {
"background": { "background": {
@ -14,17 +16,22 @@ function* testInArea(area) {
}, },
files: { files: {
"popup-a.html": `<script src="popup-a.js"></script>`, "popup-a.html": scriptPage("popup-a.js"),
"popup-a.js": function() { "popup-a.js": function() {
browser.runtime.sendMessage("from-popup-a"); browser.runtime.sendMessage("from-popup-a");
browser.runtime.onMessage.addListener(msg => {
if (msg == "close-popup") {
window.close();
}
});
}, },
"data/popup-b.html": `<script src="popup-b.js"></script>`, "data/popup-b.html": scriptPage("popup-b.js"),
"data/popup-b.js": function() { "data/popup-b.js": function() {
browser.runtime.sendMessage("from-popup-b"); browser.runtime.sendMessage("from-popup-b");
}, },
"data/background.html": `<script src="background.js"></script>`, "data/background.html": scriptPage("background.js"),
"data/background.js": function() { "data/background.js": function() {
let sendClick; let sendClick;
@ -51,26 +58,37 @@ function* testInArea(area) {
}, },
() => { () => {
browser.browserAction.setPopup({popup: "/popup-a.html"}); browser.browserAction.setPopup({popup: "/popup-a.html"});
sendClick({expectEvent: false, expectPopup: "a"}); sendClick({expectEvent: false, expectPopup: "a", runNextTest: true});
},
() => {
browser.test.sendMessage("next-test", {expectClosed: true});
browser.runtime.sendMessage("close-popup");
}, },
]; ];
let expect = {}; let expect = {};
sendClick = ({expectEvent, expectPopup}) => { sendClick = ({expectEvent, expectPopup, runNextTest}) => {
expect = {event: expectEvent, popup: expectPopup}; expect = {event: expectEvent, popup: expectPopup, runNextTest};
browser.test.sendMessage("send-click"); browser.test.sendMessage("send-click");
}; };
browser.runtime.onMessage.addListener(msg => { browser.runtime.onMessage.addListener(msg => {
if (expect.popup) { if (msg == "close-popup") {
return;
} else if (expect.popup) {
browser.test.assertEq(msg, `from-popup-${expect.popup}`, browser.test.assertEq(msg, `from-popup-${expect.popup}`,
"expected popup opened"); "expected popup opened");
} else { } else {
browser.test.fail("unexpected popup"); browser.test.fail(`unexpected popup: ${msg}`);
} }
expect.popup = null; expect.popup = null;
browser.test.sendMessage("next-test"); if (expect.runNextTest) {
expect.runNextTest = false;
tests.shift()();
} else {
browser.test.sendMessage("next-test");
}
}); });
browser.browserAction.onClicked.addListener(() => { browser.browserAction.onClicked.addListener(() => {
@ -107,13 +125,20 @@ function* testInArea(area) {
}); });
let widget; let widget;
extension.onMessage("next-test", Task.async(function* () { extension.onMessage("next-test", Task.async(function* (expecting = {}) {
if (!widget) { if (!widget) {
widget = getBrowserActionWidget(extension); widget = getBrowserActionWidget(extension);
CustomizableUI.addWidgetToArea(widget.id, area); CustomizableUI.addWidgetToArea(widget.id, area);
} }
if (expecting.expectClosed) {
yield closeBrowserAction(extension); let panel = getBrowserActionPopup(extension);
ok(panel, "Expect panel to exist");
yield promisePopupShown(panel);
yield promisePopupHidden(panel);
ok(true, "Panel is closed");
} else {
yield closeBrowserAction(extension);
}
extension.sendMessage("next-test"); extension.sendMessage("next-test");
})); }));

Просмотреть файл

@ -19,6 +19,11 @@ add_task(function* testPageActionPopup() {
"popup-a.html": scriptPage("popup-a.js"), "popup-a.html": scriptPage("popup-a.js"),
"popup-a.js": function() { "popup-a.js": function() {
browser.runtime.sendMessage("from-popup-a"); browser.runtime.sendMessage("from-popup-a");
browser.runtime.onMessage.addListener(msg => {
if (msg == "close-popup") {
window.close();
}
});
}, },
"data/popup-b.html": scriptPage("popup-b.js"), "data/popup-b.html": scriptPage("popup-b.js"),
@ -55,26 +60,37 @@ add_task(function* testPageActionPopup() {
}, },
() => { () => {
browser.pageAction.setPopup({tabId, popup: "/popup-a.html"}); browser.pageAction.setPopup({tabId, popup: "/popup-a.html"});
sendClick({expectEvent: false, expectPopup: "a"}); sendClick({expectEvent: false, expectPopup: "a", runNextTest: true});
},
() => {
browser.test.sendMessage("next-test", {expectClosed: true});
browser.runtime.sendMessage("close-popup");
}, },
]; ];
let expect = {}; let expect = {};
sendClick = ({expectEvent, expectPopup}) => { sendClick = ({expectEvent, expectPopup, runNextTest}) => {
expect = {event: expectEvent, popup: expectPopup}; expect = {event: expectEvent, popup: expectPopup, runNextTest};
browser.test.sendMessage("send-click"); browser.test.sendMessage("send-click");
}; };
browser.runtime.onMessage.addListener(msg => { browser.runtime.onMessage.addListener(msg => {
if (expect.popup) { if (msg == "close-popup") {
return;
} else if (expect.popup) {
browser.test.assertEq(msg, `from-popup-${expect.popup}`, browser.test.assertEq(msg, `from-popup-${expect.popup}`,
"expected popup opened"); "expected popup opened");
} else { } else {
browser.test.fail("unexpected popup"); browser.test.fail(`unexpected popup: ${msg}`);
} }
expect.popup = null; expect.popup = null;
browser.test.sendMessage("next-test"); if (expect.runNextTest) {
expect.runNextTest = false;
tests.shift()();
} else {
browser.test.sendMessage("next-test");
}
}); });
browser.pageAction.onClicked.addListener(() => { browser.pageAction.onClicked.addListener(() => {
@ -118,12 +134,19 @@ add_task(function* testPageActionPopup() {
clickPageAction(extension); clickPageAction(extension);
}); });
extension.onMessage("next-test", Task.async(function* () { extension.onMessage("next-test", Task.async(function* (expecting = {}) {
let panel = document.getElementById(panelId); let panel = document.getElementById(panelId);
if (panel) { if (expecting.expectClosed) {
ok(panel, "Expect panel to exist");
yield promisePopupShown(panel);
yield promisePopupHidden(panel);
ok(true, `Panel is closed`);
} else if (panel) {
yield promisePopupShown(panel); yield promisePopupShown(panel);
panel.hidePopup(); panel.hidePopup();
}
if (panel) {
panel = document.getElementById(panelId); panel = document.getElementById(panelId);
is(panel, null, "panel successfully removed from document after hiding"); is(panel, null, "panel successfully removed from document after hiding");
} }

Просмотреть файл

@ -7,7 +7,7 @@
* clickBrowserAction clickPageAction * clickBrowserAction clickPageAction
* getBrowserActionPopup getPageActionPopup * getBrowserActionPopup getPageActionPopup
* closeBrowserAction closePageAction * closeBrowserAction closePageAction
* promisePopupShown * promisePopupShown promisePopupHidden
*/ */
var {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm"); var {AppConstants} = Cu.import("resource://gre/modules/AppConstants.jsm");
@ -59,6 +59,16 @@ function promisePopupShown(popup) {
}); });
} }
function promisePopupHidden(popup) {
return new Promise(resolve => {
let onPopupHidden = event => {
popup.removeEventListener("popuphidden", onPopupHidden);
resolve();
};
popup.addEventListener("popuphidden", onPopupHidden);
});
}
function getBrowserActionWidget(extension) { function getBrowserActionWidget(extension) {
return CustomizableUI.getWidget(makeWidgetId(extension.id) + "-browser-action"); return CustomizableUI.getWidget(makeWidgetId(extension.id) + "-browser-action");
} }
@ -68,6 +78,8 @@ function getBrowserActionPopup(extension, win = window) {
if (group.areaType == CustomizableUI.TYPE_TOOLBAR) { if (group.areaType == CustomizableUI.TYPE_TOOLBAR) {
return win.document.getElementById("customizationui-widget-panel"); return win.document.getElementById("customizationui-widget-panel");
} else {
return win.PanelUI.panel;
} }
return null; return null;
} }