Bug 1333990: Part 3e - Add tests for content script cache eviction. r=aswan,billm

MozReview-Commit-ID: 6SRI8xTuZk5

--HG--
extra : rebase_source : 1618db2982bdb74c2efb8b53048c1ceb998e0049
This commit is contained in:
Kris Maglione 2017-03-11 20:17:36 -08:00
parent 4546457e5e
commit 589919e4a7
3 changed files with 107 additions and 1 deletions

View File

@ -114,6 +114,8 @@ class ScriptCache extends DefaultMap {
constructor(options) {
super(url => ChromeUtils.compileScript(url, options));
this.expiryTimeout = SCRIPT_EXPIRY_TIMEOUT_MS;
scriptCaches.add(this);
}
@ -125,7 +127,7 @@ class ScriptCache extends DefaultMap {
script.timer.cancel();
}
script.timer = Timer(this.delete.bind(this, url),
SCRIPT_EXPIRY_TIMEOUT_MS,
this.expiryTimeout,
Ci.nsITimer.TYPE_ONE_SHOT);
return script;

View File

@ -54,6 +54,7 @@ skip-if = os == 'android' # Android does not support multiple windows.
[test_ext_background_canvas.html]
[test_ext_content_security_policy.html]
[test_ext_contentscript_api_injection.html]
[test_ext_contentscript_cache.html]
[test_ext_contentscript_context.html]
[test_ext_contentscript_create_iframe.html]
[test_ext_contentscript_devtools_metadata.html]

View File

@ -0,0 +1,103 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for content script caching</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 type="text/javascript">
"use strict";
add_task(function* test_contentscript_cache() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
content_scripts: [{
"matches": ["http://example.com/"],
"js": ["content_script.js"],
"run_at": "document_start",
}],
},
background() {
browser.test.sendMessage("origin", location.origin);
},
files: {
"content_script.js": function() {
browser.test.sendMessage("content-script-loaded");
},
},
});
yield extension.startup();
let origin = yield extension.awaitMessage("origin");
let scriptUrl = `${origin}/content_script.js`;
let {ExtensionManager} = SpecialPowers.Cu.import("resource://gre/modules/ExtensionContent.jsm", {});
let ext = ExtensionManager.extensions.get(extension.id);
ext.staticScripts.expiryTimeout = 3000;
is(ext.staticScripts.size, 0, "Should have no cached scripts");
let win = window.open("http://example.com/");
yield extension.awaitMessage("content-script-loaded");
if (AppConstants.platform !== "android") {
is(ext.staticScripts.size, 1, "Should have one cached script");
ok(ext.staticScripts.has(scriptUrl), "Script cache should contain script URL");
}
let chromeScript, chromeScriptDone;
let {appinfo} = SpecialPowers.Services;
if (appinfo.processType === appinfo.PROCESS_TYPE_CONTENT) {
/* globals addMessageListener, assert */
chromeScript = SpecialPowers.loadChromeScript(() => {
addMessageListener("check-script-cache", extensionId => {
let {ExtensionManager} = Components.utils.import("resource://gre/modules/ExtensionContent.jsm", {});
let ext = ExtensionManager.extensions.get(extensionId);
assert.equal(ext.staticScripts.size, 0, "Should have no cached scripts in the parent process");
sendAsyncMessage("done");
});
});
chromeScript.sendAsyncMessage("check-script-cache", extension.id);
chromeScriptDone = chromeScript.promiseOneMessage("done");
}
SimpleTest.requestFlakyTimeout("Required to test expiry timeout");
yield new Promise(resolve => setTimeout(resolve, 3000));
is(ext.staticScripts.size, 0, "Should have no cached scripts");
if (chromeScript) {
yield chromeScriptDone;
chromeScript.destroy();
}
win.close();
win = window.open("http://example.com/");
yield extension.awaitMessage("content-script-loaded");
is(ext.staticScripts.size, 1, "Should have one cached script");
ok(ext.staticScripts.has(scriptUrl));
SpecialPowers.Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
is(ext.staticScripts.size, 0, "Should have no cached scripts after heap-minimize");
win.close();
yield extension.unload();
});
</script>
</body>
</html>