зеркало из https://github.com/mozilla/gecko-dev.git
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:
Родитель
6bf0866c5f
Коммит
67341496bf
|
@ -125,6 +125,19 @@ global.makeWidgetId = id => {
|
|||
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 {
|
||||
constructor(extension, viewNode, popupURL) {
|
||||
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.
|
||||
resizeBrowser() {
|
||||
if (!this.browser) {
|
||||
return;
|
||||
}
|
||||
|
||||
let width, height;
|
||||
try {
|
||||
let w = {}, h = {};
|
||||
|
@ -310,8 +327,13 @@ global.PanelPopup = class PanelPopup extends BasePopup {
|
|||
}
|
||||
|
||||
closePopup() {
|
||||
promisePopupShown(this.viewNode).then(() => {
|
||||
// Make sure we're not already destroyed.
|
||||
if (this.viewNode) {
|
||||
this.viewNode.hidePopup();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
global.ViewPopup = class ViewPopup extends BasePopup {
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
"use strict";
|
||||
|
||||
function* testInArea(area) {
|
||||
let scriptPage = url => `<html><head><meta charset="utf-8"><script src="${url}"></script></head></html>`;
|
||||
|
||||
let extension = ExtensionTestUtils.loadExtension({
|
||||
manifest: {
|
||||
"background": {
|
||||
|
@ -14,17 +16,22 @@ function* testInArea(area) {
|
|||
},
|
||||
|
||||
files: {
|
||||
"popup-a.html": `<script src="popup-a.js"></script>`,
|
||||
"popup-a.html": scriptPage("popup-a.js"),
|
||||
"popup-a.js": function() {
|
||||
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() {
|
||||
browser.runtime.sendMessage("from-popup-b");
|
||||
},
|
||||
|
||||
"data/background.html": `<script src="background.js"></script>`,
|
||||
"data/background.html": scriptPage("background.js"),
|
||||
|
||||
"data/background.js": function() {
|
||||
let sendClick;
|
||||
|
@ -51,26 +58,37 @@ function* testInArea(area) {
|
|||
},
|
||||
() => {
|
||||
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 = {};
|
||||
sendClick = ({expectEvent, expectPopup}) => {
|
||||
expect = {event: expectEvent, popup: expectPopup};
|
||||
sendClick = ({expectEvent, expectPopup, runNextTest}) => {
|
||||
expect = {event: expectEvent, popup: expectPopup, runNextTest};
|
||||
browser.test.sendMessage("send-click");
|
||||
};
|
||||
|
||||
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}`,
|
||||
"expected popup opened");
|
||||
} else {
|
||||
browser.test.fail("unexpected popup");
|
||||
browser.test.fail(`unexpected popup: ${msg}`);
|
||||
}
|
||||
|
||||
expect.popup = null;
|
||||
if (expect.runNextTest) {
|
||||
expect.runNextTest = false;
|
||||
tests.shift()();
|
||||
} else {
|
||||
browser.test.sendMessage("next-test");
|
||||
}
|
||||
});
|
||||
|
||||
browser.browserAction.onClicked.addListener(() => {
|
||||
|
@ -107,13 +125,20 @@ function* testInArea(area) {
|
|||
});
|
||||
|
||||
let widget;
|
||||
extension.onMessage("next-test", Task.async(function* () {
|
||||
extension.onMessage("next-test", Task.async(function* (expecting = {}) {
|
||||
if (!widget) {
|
||||
widget = getBrowserActionWidget(extension);
|
||||
CustomizableUI.addWidgetToArea(widget.id, area);
|
||||
}
|
||||
|
||||
if (expecting.expectClosed) {
|
||||
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");
|
||||
}));
|
||||
|
|
|
@ -19,6 +19,11 @@ add_task(function* testPageActionPopup() {
|
|||
"popup-a.html": scriptPage("popup-a.js"),
|
||||
"popup-a.js": function() {
|
||||
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"),
|
||||
|
@ -55,26 +60,37 @@ add_task(function* testPageActionPopup() {
|
|||
},
|
||||
() => {
|
||||
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 = {};
|
||||
sendClick = ({expectEvent, expectPopup}) => {
|
||||
expect = {event: expectEvent, popup: expectPopup};
|
||||
sendClick = ({expectEvent, expectPopup, runNextTest}) => {
|
||||
expect = {event: expectEvent, popup: expectPopup, runNextTest};
|
||||
browser.test.sendMessage("send-click");
|
||||
};
|
||||
|
||||
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}`,
|
||||
"expected popup opened");
|
||||
} else {
|
||||
browser.test.fail("unexpected popup");
|
||||
browser.test.fail(`unexpected popup: ${msg}`);
|
||||
}
|
||||
|
||||
expect.popup = null;
|
||||
if (expect.runNextTest) {
|
||||
expect.runNextTest = false;
|
||||
tests.shift()();
|
||||
} else {
|
||||
browser.test.sendMessage("next-test");
|
||||
}
|
||||
});
|
||||
|
||||
browser.pageAction.onClicked.addListener(() => {
|
||||
|
@ -118,12 +134,19 @@ add_task(function* testPageActionPopup() {
|
|||
clickPageAction(extension);
|
||||
});
|
||||
|
||||
extension.onMessage("next-test", Task.async(function* () {
|
||||
extension.onMessage("next-test", Task.async(function* (expecting = {}) {
|
||||
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);
|
||||
panel.hidePopup();
|
||||
}
|
||||
|
||||
if (panel) {
|
||||
panel = document.getElementById(panelId);
|
||||
is(panel, null, "panel successfully removed from document after hiding");
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* clickBrowserAction clickPageAction
|
||||
* getBrowserActionPopup getPageActionPopup
|
||||
* closeBrowserAction closePageAction
|
||||
* promisePopupShown
|
||||
* promisePopupShown promisePopupHidden
|
||||
*/
|
||||
|
||||
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) {
|
||||
return CustomizableUI.getWidget(makeWidgetId(extension.id) + "-browser-action");
|
||||
}
|
||||
|
@ -68,6 +78,8 @@ function getBrowserActionPopup(extension, win = window) {
|
|||
|
||||
if (group.areaType == CustomizableUI.TYPE_TOOLBAR) {
|
||||
return win.document.getElementById("customizationui-widget-panel");
|
||||
} else {
|
||||
return win.PanelUI.panel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче