mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 11:25:00 +00:00
Part 1: Implement default_popup for chrome.pageAction (Bug 1264118) r=kmag
MozReview-Commit-ID: BrV0v66BhBz --HG-- extra : rebase_source : ec0b567c7ea3860960fb7d25f089cc053bae39b0 extra : histedit_source : d0a4e4bc6b60454eccdf017bd48ced992b87c5f9
This commit is contained in:
parent
632b11bf2f
commit
494256ef31
@ -5,6 +5,9 @@
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
|
||||
"resource://devtools/shared/event-emitter.js");
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "Services",
|
||||
"resource://gre/modules/Services.jsm");
|
||||
|
||||
// Import the android PageActions module.
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "PageActions",
|
||||
"resource://gre/modules/PageActions.jsm");
|
||||
@ -28,7 +31,15 @@ function PageAction(options, extension) {
|
||||
icon: DEFAULT_ICON,
|
||||
id: extension.id,
|
||||
clickCallback: () => {
|
||||
this.emit("click");
|
||||
if (this.default_popup) {
|
||||
let win = Services.wm.getMostRecentWindow("navigator:browser");
|
||||
win.BrowserApp.addTab(this.default_popup, {
|
||||
selected: true,
|
||||
parentId: win.BrowserApp.selectedTab.id
|
||||
});
|
||||
} else {
|
||||
this.emit("click");
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@ -85,6 +96,7 @@ extensions.registerSchemaAPI("pageAction", null, (extension, context) => {
|
||||
show(tabId) {
|
||||
pageActionMap.get(extension).show(tabId);
|
||||
},
|
||||
|
||||
hide(tabId) {
|
||||
pageActionMap.get(extension).hide(tabId);
|
||||
},
|
||||
|
@ -24,7 +24,6 @@
|
||||
"optional": true
|
||||
},
|
||||
"default_popup": {
|
||||
"unsupported": true,
|
||||
"type": "string",
|
||||
"format": "relativeUrl",
|
||||
"optional": true,
|
||||
|
@ -2,4 +2,5 @@
|
||||
support-files =
|
||||
head.js
|
||||
|
||||
[test_ext_pageAction.html]
|
||||
[test_ext_pageAction.html]
|
||||
[test_ext_pageAction_defaultPopup.html]
|
@ -0,0 +1,100 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>PageAction Test</title>
|
||||
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
|
||||
<script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
|
||||
<script type="text/javascript" src="head.js"></script>
|
||||
<link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript">
|
||||
"use strict";
|
||||
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
add_task(function* test_contentscript() {
|
||||
function backgroundScript() {
|
||||
// TODO: Use the Tabs API to obtain the tab ids for showing pageActions.
|
||||
let tabId = 1;
|
||||
browser.test.onMessage.addListener(msg => {
|
||||
if (msg === "page-action-show") {
|
||||
// TODO: switch to using .show(tabId).then(...) once bug 1270742 lands.
|
||||
browser.pageAction.show(tabId);
|
||||
browser.test.sendMessage("page-action-shown");
|
||||
} else if (msg == "page-action-close-popup") {
|
||||
browser.runtime.sendMessage("close-popup");
|
||||
}
|
||||
});
|
||||
|
||||
browser.pageAction.onClicked.addListener(tab => {
|
||||
browser.test.fail(`The onClicked listener should never fire when a popup is shown.`);
|
||||
});
|
||||
|
||||
browser.test.sendMessage("ready");
|
||||
}
|
||||
|
||||
function popupScript() {
|
||||
window.onload = () => {
|
||||
browser.test.sendMessage("from-page-action-popup-shown");
|
||||
};
|
||||
browser.runtime.onMessage.addListener(msg => {
|
||||
if (msg == "close-popup") {
|
||||
window.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let extension = ExtensionTestUtils.loadExtension({
|
||||
background: `(${backgroundScript}())`,
|
||||
manifest: {
|
||||
"name": "PageAction Extension",
|
||||
"page_action": {
|
||||
"default_title": "Page Action",
|
||||
"default_popup": "popup.html",
|
||||
},
|
||||
},
|
||||
files: {
|
||||
"popup.html": `<html><head><meta charset="utf-8"><script src="popup.js"></${"script"}></head></html>`,
|
||||
"popup.js": `(${popupScript})()`,
|
||||
},
|
||||
});
|
||||
|
||||
let tabClosedPromise = new Promise(resolve => {
|
||||
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
|
||||
let BrowserApp = chromeWin.BrowserApp;
|
||||
|
||||
let tabCloseListener = (event) => {
|
||||
BrowserApp.deck.removeEventListener("TabClose", tabCloseListener, false);
|
||||
let browser = event.target;
|
||||
let url = browser.currentURI.spec
|
||||
resolve(url);
|
||||
}
|
||||
|
||||
BrowserApp.deck.addEventListener("TabClose", tabCloseListener, false);
|
||||
});
|
||||
|
||||
yield extension.startup();
|
||||
yield extension.awaitMessage("ready");
|
||||
|
||||
extension.sendMessage("page-action-show");
|
||||
yield extension.awaitMessage("page-action-shown");
|
||||
ok(isPageActionShown(extension.id), "The PageAction should be shown");
|
||||
|
||||
clickPageAction(extension.id);
|
||||
yield extension.awaitMessage("from-page-action-popup-shown");
|
||||
|
||||
extension.sendMessage("page-action-close-popup");
|
||||
|
||||
let url = yield tabClosedPromise;
|
||||
ok(url.includes("popup.html"), "The tab for the popup should be closed");
|
||||
|
||||
yield extension.unload();
|
||||
ok(!isPageActionShown(extension.id), "The PageAction should be removed after unload");
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user