Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>PageAction 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 {ContentTaskUtils} = SpecialPowers.Cu.import("resource://testing-common/ContentTaskUtils.jsm", {});
16
var {PageActions} = SpecialPowers.Cu.import("resource://gre/modules/PageActions.jsm", {});
17
18
add_task(async function test_pageAction() {
19
async function background() {
20
let tabCreatedPromise = new Promise(resolve => {
21
let onTabCreated = tab => {
22
browser.tabs.onCreated.removeListener(onTabCreated);
23
resolve();
24
};
25
browser.tabs.onCreated.addListener(onTabCreated);
26
});
27
28
async function createNewTab(url) {
29
let [tab] = await Promise.all([
30
browser.tabs.create({url}),
31
tabCreatedPromise,
32
]);
33
return tab;
34
}
35
36
let tab1 = await createNewTab("about:blank");
37
let tab2 = await createNewTab("about:blank");
38
39
browser.test.onMessage.addListener(async (msg, data) => {
40
if (msg === "select-tab") {
41
const onActivatedListener = ({tabId}) => {
42
if (tabId === data) {
43
browser.tabs.onActivated.removeListener(onActivatedListener);
44
browser.test.sendMessage("tab-selected");
45
}
46
};
47
browser.tabs.onActivated.addListener(onActivatedListener);
48
49
await browser.tabs.update(data, {active: true});
50
} else if (msg === "pageAction-show") {
51
browser.pageAction.show(data).then(() => {
52
browser.test.sendMessage("page-action-shown");
53
});
54
} else if (msg === "pageAction-hide") {
55
browser.pageAction.hide(data).then(() => {
56
browser.test.sendMessage("page-action-hidden");
57
});
58
} else if (msg === "finish") {
59
await browser.tabs.remove([tab1.id, tab2.id]);
60
browser.test.notifyPass("pageAction");
61
}
62
});
63
64
browser.pageAction.onClicked.addListener(tab => {
65
browser.test.sendMessage("page-action-clicked", tab);
66
});
67
68
browser.test.sendMessage("tabs", {tab1, tab2});
69
}
70
71
const extension = ExtensionTestUtils.loadExtension({
72
background,
73
manifest: {
74
"name": "PageAction Extension",
75
"page_action": {
76
"default_title": "Page Action",
77
"default_icon": {
78
"18": "extension.png",
79
},
80
},
81
"applications": {
82
"gecko": {
83
"id": "foo@bar.com",
84
},
85
},
86
},
87
files: {
88
"extension.png": TEST_ICON_ARRAYBUFFER,
89
},
90
});
91
92
await extension.startup();
93
94
async function checkTab(tabId, uuid, showAfterSelecting) {
95
ok(!PageActions.isShown(uuid), "The PageAction should not be shown");
96
97
if (showAfterSelecting) {
98
extension.sendMessage("select-tab", tabId);
99
await extension.awaitMessage("tab-selected");
100
ok(!PageActions.isShown(uuid), "The PageAction should still not be shown");
101
extension.sendMessage("pageAction-show", tabId);
102
await extension.awaitMessage("page-action-shown");
103
ok(PageActions.isShown(uuid), "The PageAction should be shown");
104
} else {
105
extension.sendMessage("pageAction-show", tabId);
106
await extension.awaitMessage("page-action-shown");
107
ok(!PageActions.isShown(uuid), "The PageAction should still not be shown");
108
extension.sendMessage("select-tab", tabId);
109
await extension.awaitMessage("tab-selected");
110
// Wait until the page action is shown.
111
await ContentTaskUtils.waitForCondition(() => PageActions.isShown(uuid));
112
}
113
114
PageActions.synthesizeClick(uuid);
115
const clickedTab = await extension.awaitMessage("page-action-clicked");
116
is(clickedTab.id, tabId, "Got the expected tab id in the pageAction.onClicked event");
117
118
ok(PageActions.isShown(uuid), "The PageAction should be shown");
119
extension.sendMessage("pageAction-hide", tabId);
120
await extension.awaitMessage("page-action-hidden");
121
}
122
123
const tabs = await extension.awaitMessage("tabs");
124
const uuid = `{${extension.uuid}}`;
125
126
await checkTab(tabs.tab1.id, uuid, true /* showAfterSelecting */);
127
await checkTab(tabs.tab2.id, uuid, false /* showAfterSelecting */);
128
129
// Show the page action for the active tab.
130
extension.sendMessage("pageAction-show", tabs.tab2.id);
131
await extension.awaitMessage("page-action-shown");
132
ok(PageActions.isShown(uuid), "The PageAction should be shown");
133
134
extension.sendMessage("finish");
135
await extension.awaitFinish("pageAction");
136
137
await extension.unload();
138
ok(!PageActions.isShown(uuid), "The PageAction should be removed after unload");
139
});
140
</script>
141
142
</body>
143
</html>