Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs executeScript noCreate 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 testExecuteScriptAtOnUpdated() {
17
const URL = BASE + "file_iframe_document.html";
18
// This is a regression test for bug 1325830.
19
// The bug (executeScript not completing any more) occurred when executeScript
20
// was called early at the onUpdated event, unless the tabs.create method is
21
// called. So this test does not use tabs.create to open new tabs.
22
// Note that if this test is run together with other tests that do call
23
// tabs.create, then this test case does not properly test the conditions of
24
// the regression any more. To verify that the regression has been resolved,
25
// this test must be run in isolation.
26
27
function background() {
28
// Using variables to prevent listeners from running more than once, instead
29
// of removing the listener. This is to minimize any IPC, since the bug that
30
// is being tested is sensitive to timing.
31
let ignore = false;
32
let url;
33
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
34
if (url && changeInfo.status === "loading" && tab.url === url && !ignore) {
35
ignore = true;
36
browser.tabs.executeScript(tabId, {
37
code: "document.URL",
38
}).then(results => {
39
browser.test.assertEq(url, results[0], "Content script should run");
40
browser.test.notifyPass("executeScript-at-onUpdated");
41
}, error => {
42
browser.test.fail(`Unexpected error: ${error} :: ${error.stack}`);
43
browser.test.notifyFail("executeScript-at-onUpdated");
44
});
45
// (running this log call after executeScript to minimize IPC between
46
// onUpdated and executeScript.)
47
browser.test.log(`Found expected navigation to ${url}`);
48
} else {
49
// The bug occurs when executeScript is called before a tab is
50
// initialized.
51
browser.tabs.executeScript(tabId, {code: ""});
52
}
53
});
54
browser.test.onMessage.addListener(testUrl => {
55
url = testUrl;
56
browser.test.sendMessage("open-test-tab");
57
});
58
}
59
60
let extension = ExtensionTestUtils.loadExtension({
61
manifest: {
62
"permissions": ["http://mochi.test/", "tabs"],
63
},
64
background,
65
});
66
67
await extension.startup();
68
69
extension.sendMessage(URL);
70
await extension.awaitMessage("open-test-tab");
71
72
let tab = window.open(URL);
73
await extension.awaitFinish("executeScript-at-onUpdated");
74
75
await extension.unload();
76
77
tab.close();
78
});
79
</script>
80
81
</body>
82
</html>