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
var {ContentTaskUtils} = SpecialPowers.Cu.import("resource://testing-common/ContentTaskUtils.jsm", {});
17
18
add_task(async function test_setTitle_and_getTitle() {
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 createAndTestNewTab(title, url) {
29
// First make sure the default title is correct.
30
let defaultTitle = await browser.browserAction.getTitle({});
31
browser.test.assertEq("Browser Action", defaultTitle, "Expected the default title to be returned");
32
33
// Create a tab.
34
let [tab] = await Promise.all([
35
browser.tabs.create({url}),
36
tabCreatedPromise,
37
]);
38
39
// Test that the default title is returned before the title is set for the tab.
40
let tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
41
browser.test.assertEq("Browser Action", tabTitle, "Expected the default title to be returned");
42
43
// Set the title for the new tab and test that getTitle returns the correct title.
44
await browser.browserAction.setTitle({tabId: tab.id, title});
45
tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
46
browser.test.assertEq(title, tabTitle, "Expected the new tab title to be returned");
47
48
return tab;
49
}
50
51
// Create and test 3 new tabs.
52
let tab1 = await createAndTestNewTab("tab 1", "about:blank");
53
let tab2 = await createAndTestNewTab("tab 2", "about:blank");
54
let tab3 = await createAndTestNewTab("tab 3", "about:blank");
55
56
// Test the default title again.
57
let title = await browser.browserAction.getTitle({});
58
browser.test.assertEq("Browser Action", title, "Expected the default title to be returned");
59
60
// Update the default title and confirm that the new title is returned.
61
await browser.browserAction.setTitle({title: "Updated Title"});
62
title = await browser.browserAction.getTitle({});
63
browser.test.assertEq("Updated Title", title, "Expected the default title to be updated");
64
65
// Try setting the default title to an empty string and confirm that the original title is still used.
66
browser.browserAction.setTitle({title: ""});
67
title = await browser.browserAction.getTitle({});
68
browser.test.assertEq("Updated Title", title, "Expected the default title to be returned");
69
70
// Check all of the created tabs now.
71
title = await browser.browserAction.getTitle({tabId: tab1.id});
72
browser.test.assertEq("tab 1", title, "Expected the first tab title");
73
title = await browser.browserAction.getTitle({tabId: tab2.id});
74
browser.test.assertEq("tab 2", title, "Expected the second tab title");
75
title = await browser.browserAction.getTitle({tabId: tab3.id});
76
browser.test.assertEq("tab 3", title, "Expected the third tab title");
77
78
// Unset the title for the first tab and confirm that it is unset.
79
browser.browserAction.setTitle({tabId: tab1.id, title: ""});
80
title = await browser.browserAction.getTitle({tabId: tab1.id});
81
browser.test.assertEq("Updated Title", title, `Expected the default title to be returned`);
82
83
browser.test.onMessage.addListener(async (msg, data) => {
84
if (msg === "select-tab") {
85
await browser.tabs.update(data, {active: true});
86
browser.test.sendMessage("tab-selected");
87
} else if (msg == "finish") {
88
// Close the tabs
89
await browser.tabs.remove([tab1.id, tab2.id, tab3.id]);
90
browser.test.notifyPass("browserAction.setTitleAndGetTitle");
91
}
92
});
93
94
browser.test.sendMessage("tabs", {tab1, tab2, tab3});
95
}
96
97
const extension = ExtensionTestUtils.loadExtension({
98
background,
99
manifest: {
100
"name": name,
101
"browser_action": {
102
"default_title": "Browser Action",
103
},
104
},
105
});
106
107
await extension.startup();
108
109
let tabs = await extension.awaitMessage("tabs");
110
111
async function checkTab(tab, name) {
112
extension.sendMessage("select-tab", tab.id);
113
await extension.awaitMessage("tab-selected");
114
// Wait until the browser action has updated to the correct title.
115
await ContentTaskUtils.waitForCondition(() => {
116
return BrowserActions.getNameForActiveTab(`{${extension.uuid}}`) === name;
117
});
118
}
119
120
await checkTab(tabs.tab1, "Updated Title");
121
await checkTab(tabs.tab2, "tab 2");
122
await checkTab(tabs.tab3, "tab 3");
123
await checkTab(tabs.tab1, "Updated Title");
124
125
extension.sendMessage("finish");
126
await extension.awaitFinish("browserAction.setTitleAndGetTitle");
127
128
await extension.unload();
129
});
130
131
add_task(async function test_setTitle_activeTab() {
132
async function background() {
133
const tabs = await browser.tabs.query({active: true});
134
const tabId = tabs[0].id;
135
136
const title = "Customized browserAction title";
137
await browser.browserAction.setTitle({tabId, title});
138
139
browser.test.notifyPass("browserAction_setTitle_activeTab.done");
140
}
141
142
const extension = ExtensionTestUtils.loadExtension({
143
background,
144
manifest: {
145
"browser_action": {
146
"default_title": "Browser Action default title",
147
},
148
},
149
});
150
151
await extension.startup();
152
153
await extension.awaitFinish("browserAction_setTitle_activeTab.done");
154
155
is(BrowserActions.getNameForActiveTab(`{${extension.uuid}}`),
156
"Customized browserAction title",
157
"The browserAction title has been updated on the currently activeTab");
158
159
await extension.unload();
160
});
161
</script>
162
163
</body>
164
</html>