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
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for content script teardown</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<script>
"use strict";
add_task(function* test_contentscript_reload_and_unload() {
function contentScript() {
browser.test.sendMessage("contentscript-run");
}
function background() {
let removedTabs = 0;
browser.tabs.onRemoved.addListener(() => {
browser.test.assertEq(1, ++removedTabs,
"Expected only one tab to be removed during the test");
browser.test.sendMessage("tab-closed");
});
}
let extensionData = {
background,
manifest: {
content_scripts: [{
"matches": ["http://mochi.test/*/file_sample.html"],
"js": ["contentscript.js"],
}],
},
files: {
"contentscript.js": contentScript,
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
yield extension.startup();
let chromeScript = SpecialPowers.loadChromeScript(
SimpleTest.getTestFileURL("file_teardown_test.js"));
yield chromeScript.promiseOneMessage("chromescript-startup");
function* getContextEvents() {
chromeScript.sendAsyncMessage("get-context-events");
let contextEvents = yield chromeScript.promiseOneMessage("context-events");
return contextEvents.filter(event => event.extensionId == extension.id);
}
let win = window.open("file_sample.html");
yield extension.awaitMessage("contentscript-run");
let tabUrl = win.location.href;
let contextEvents = yield* getContextEvents();
is(contextEvents.length, 1,
"ExtensionContext state change after loading a content script");
is(contextEvents[0].eventType, "load",
"Create ExtensionContext for content script");
is(contextEvents[0].url, tabUrl, "ExtensionContext URL = page");
let promiseReload = extension.awaitMessage("contentscript-run");
win.location.reload();
yield promiseReload;
contextEvents = yield* getContextEvents();
is(contextEvents.length, 2,
"ExtensionContext state changes after reloading a content script");
is(contextEvents[0].eventType, "unload", "Unload old ExtensionContext");
is(contextEvents[0].url, tabUrl, "ExtensionContext URL = page");
is(contextEvents[1].eventType, "load",
"Create new ExtensionContext for content script");
is(contextEvents[1].url, tabUrl, "ExtensionContext URL = page");
let tabClosePromise = extension.awaitMessage("tab-closed");
win.close();
yield tabClosePromise;
contextEvents = yield* getContextEvents();
is(contextEvents.length, 1,
"ExtensionContext state change after unloading a content script");
is(contextEvents[0].eventType, "unload",
"Unload ExtensionContext after closing the tab with the content script");
is(contextEvents[0].url, tabUrl, "ExtensionContext URL = page");
chromeScript.sendAsyncMessage("cleanup");
chromeScript.destroy();
yield extension.unload();
});
</script>
</body>
</html>
|