Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>BrowserAction Test</title>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
9
</head>
10
<body>
11
12
<script type="text/javascript">
13
"use strict";
14
15
var {BrowserActions} = SpecialPowers.Cu.import("resource://gre/modules/BrowserActions.jsm", {});
16
17
async function background() {
18
browser.test.assertTrue("browserAction" in browser, "Namespace 'browserAction' exists in browser");
19
browser.test.assertTrue("onClicked" in browser.browserAction, "API method 'onClicked' exists in browser.browserAction");
20
21
const tabs = await browser.tabs.query({active: true, currentWindow: true});
22
23
browser.browserAction.onClicked.addListener(tab => {
24
browser.test.sendMessage("browser-action-clicked", tab);
25
});
26
27
browser.test.sendMessage("ready", tabs[0]);
28
}
29
30
function createExtension(name) {
31
return ExtensionTestUtils.loadExtension({
32
background,
33
manifest: {
34
"name": name,
35
"browser_action": {
36
"default_title": "Browser Action",
37
},
38
},
39
});
40
}
41
42
function* checkBrowserAction(extension, id, tab) {
43
ok(BrowserActions.isShown(id), "The BrowerAction should be shown");
44
BrowserActions.synthesizeClick(id);
45
const clickedTab = yield extension.awaitMessage("browser-action-clicked");
46
is(clickedTab.id, tab.id, "Got the expected tab id in the browserAction.onClicked event");
47
}
48
49
add_task(async function test_browserAction() {
50
const extension = createExtension("BrowserAction Extension");
51
await extension.startup();
52
const tab = await extension.awaitMessage("ready");
53
let id = `{${extension.uuid}}`;
54
await checkBrowserAction(extension, id, tab);
55
await extension.unload();
56
57
ok(!BrowserActions.isShown(id), "The BrowserAction should be removed after the extension unloads");
58
});
59
60
add_task(async function test_multiple_browserActions() {
61
const ext1 = createExtension("BrowserAction Extension 1");
62
const ext2 = createExtension("BrowserAction Extension 2");
63
64
// Start the first extension and test its browser action.
65
await ext1.startup();
66
const tab1 = await ext1.awaitMessage("ready");
67
let id1 = `{${ext1.uuid}}`;
68
await checkBrowserAction(ext1, id1, tab1);
69
70
// Start the second extension and test its browser action.
71
await ext2.startup();
72
const tab2 = await ext2.awaitMessage("ready");
73
let id2 = `{${ext2.uuid}}`;
74
await checkBrowserAction(ext2, id2, tab2);
75
76
// Verify that the first browser action is still active.
77
await checkBrowserAction(ext1, id1, tab1);
78
79
// Unload the first extension and verify that the browser action is removed.
80
await ext1.unload();
81
ok(!BrowserActions.isShown(id1), "The first BrowserAction should be removed after ext1 unloads");
82
83
// Verify that the second browser action is still active.
84
await checkBrowserAction(ext2, id2, tab2);
85
86
// Unload the second extension and verify that the browser action is removed.
87
await ext2.unload();
88
ok(!BrowserActions.isShown(id2), "The second BrowserAction should be removed after ext2 unloads");
89
});
90
</script>
91
92
</body>
93
</html>