Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs onUpdated Test</title>
5
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
9
</head>
10
<body>
11
12
<script type="text/javascript">
13
"use strict";
14
15
add_task(async function test_onUpdated() {
16
let extension = ExtensionTestUtils.loadExtension({
17
manifest: {
18
"permissions": ["tabs"],
19
"content_scripts": [{
21
"js": ["content-script.js"],
22
"run_at": "document_start",
23
}],
24
},
25
26
background: function() {
28
29
let expectedSequence = [
30
{status: "loading"},
31
{status: "loading", url: pageURL},
32
{status: "complete"},
33
];
34
35
let collectedSequence = [];
36
37
let tabId;
38
browser.tabs.onUpdated.addListener(function(tabId, updatedInfo) {
39
// onUpdated also fires with updatedInfo.faviconUrl, so explicitly
40
// check for updatedInfo.status before recording the event.
41
if ("status" in updatedInfo) {
42
collectedSequence.push(updatedInfo);
43
}
44
});
45
46
browser.runtime.onMessage.addListener(async msg => {
47
if (collectedSequence.length !== expectedSequence.length) {
48
browser.test.assertEq(
49
JSON.stringify(expectedSequence),
50
JSON.stringify(collectedSequence),
51
"got unexpected number of updateInfo data"
52
);
53
} else {
54
for (let i = 0; i < expectedSequence.length; i++) {
55
browser.test.assertEq(
56
expectedSequence[i].status,
57
collectedSequence[i].status,
58
"check updatedInfo status"
59
);
60
if (expectedSequence[i].url || collectedSequence[i].url) {
61
browser.test.assertEq(
62
expectedSequence[i].url,
63
collectedSequence[i].url,
64
"check updatedInfo url"
65
);
66
}
67
}
68
}
69
70
await browser.tabs.remove(tabId);
71
browser.test.notifyPass("tabs.onUpdated");
72
});
73
74
browser.tabs.create({url: pageURL}).then(tab => {
75
tabId = tab.id;
76
});
77
},
78
files: {
79
"content-script.js": `
80
window.addEventListener("message", function(evt) {
81
if (evt.data == "frame-updated") {
82
browser.runtime.sendMessage("load-completed");
83
}
84
}, true);
85
`,
86
},
87
});
88
89
await Promise.all([
90
extension.startup(),
91
extension.awaitFinish("tabs.onUpdated"),
92
]);
93
94
await extension.unload();
95
});
96
97
function* do_test_update(background, withPermissions = true) {
98
let manifest = {};
99
if (withPermissions) {
100
manifest.permissions = ["tabs", "http://mochi.test/"];
101
}
102
let extension = ExtensionTestUtils.loadExtension({manifest, background});
103
104
yield Promise.all([
105
yield extension.startup(),
106
yield extension.awaitFinish("finish"),
107
]);
108
109
yield extension.unload();
110
}
111
112
add_task(async function test_url() {
113
await do_test_update(function background() {
114
// Create a new tab for testing update.
115
browser.tabs.create({}, function(tab) {
116
browser.tabs.onUpdated.addListener(async function onUpdated(tabId, changeInfo) {
117
// Check callback
118
browser.test.assertEq(tabId, tab.id, "Check tab id");
119
browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
120
if ("url" in changeInfo) {
121
browser.test.assertEq("about:blank", changeInfo.url,
122
"Check changeInfo.url");
123
browser.tabs.onUpdated.removeListener(onUpdated);
124
// Remove created tab.
125
await browser.tabs.remove(tabId);
126
browser.test.notifyPass("finish");
127
}
128
});
129
browser.tabs.update(tab.id, {url: "about:blank"});
130
});
131
});
132
});
133
134
add_task(async function test_title() {
135
await do_test_update(async function background() {
137
const tab = await browser.tabs.create({url});
138
139
browser.tabs.onUpdated.addListener(async function onUpdated(tabId, changeInfo) {
140
browser.test.assertEq(tabId, tab.id, "Check tab id");
141
browser.test.log(`onUpdated: ${JSON.stringify(changeInfo)}`);
142
if ("title" in changeInfo && changeInfo.title === "New Message (1)") {
143
browser.test.log("changeInfo.title is correct");
144
browser.tabs.onUpdated.removeListener(onUpdated);
145
await browser.tabs.remove(tabId);
146
browser.test.notifyPass("finish");
147
}
148
});
149
150
browser.tabs.executeScript(tab.id, {code: "document.title = 'New Message (1)'"});
151
});
152
});
153
154
add_task(async function test_without_tabs_permission() {
155
await do_test_update(async function background() {
157
const tab = await browser.tabs.create({url});
158
let count = 0;
159
160
browser.tabs.onUpdated.addListener(async function onUpdated(tabId, changeInfo) {
161
browser.test.assertEq(tabId, tab.id, "Check tab id");
162
browser.test.log(`onUpdated: ${JSON.stringify(changeInfo)}`);
163
164
browser.test.assertFalse("url" in changeInfo, "url should not be included without tabs permission");
165
browser.test.assertFalse("favIconUrl" in changeInfo, "favIconUrl should not be included without tabs permission");
166
browser.test.assertFalse("title" in changeInfo, "title should not be included without tabs permission");
167
168
if (changeInfo.status == "complete") {
169
count++;
170
if (count === 2) {
171
browser.test.log("Reload complete");
172
browser.tabs.onUpdated.removeListener(onUpdated);
173
await browser.tabs.remove(tabId);
174
browser.test.notifyPass("finish");
175
}
176
}
177
});
178
179
browser.tabs.reload(tab.id);
180
}, false /* withPermissions */);
181
});
182
</script>
183
184
</body>
185
</html>