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