commit 828e4c1ce857254ece8d7a93406125301a4f8c97
parent 84bbb8137b5f0d25aa8dd7eadaee3a03b8c9a4e2
Author: Tooru Fujisawa <arai_a@mac.com>
Date: Wed, 29 Oct 2025 23:24:56 +0000
Bug 1880453 - Part 1: Add testcase for script cache with default preferences. r=nbp,frontend-codestyle-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D270113
Diffstat:
7 files changed, 267 insertions(+), 0 deletions(-)
diff --git a/.prettierignore b/.prettierignore
@@ -1113,6 +1113,7 @@ dom/media/test/hls/*.ts
# Intentional broken files
dom/base/test/file_js_cache_syntax_error.js
+dom/base/test/file_js_cache_large_syntax_error.js
dom/base/test/jsmodules/test_scriptNotParsedAsModule.html
dom/base/test/jsmodules/test_syntaxError.html
dom/base/test/jsmodules/test_syntaxErrorAsync.html
diff --git a/dom/base/test/browser.toml b/dom/base/test/browser.toml
@@ -154,6 +154,14 @@ support-files = [
["browser_screen_orientation_override.js"]
+["browser_script_loader_js_cache.js"]
+support-files = [
+ "empty.html",
+ "file_js_cache_small.js",
+ "file_js_cache_large.js",
+ "file_js_cache_large_syntax_error.js",
+]
+
["browser_set_focus_after_reuse_bcg.js"]
["browser_state_notifications.js"]
diff --git a/dom/base/test/browser_script_loader_js_cache.js b/dom/base/test/browser_script_loader_js_cache.js
@@ -0,0 +1,212 @@
+const BASE_URL = "http://mochi.test:8888/browser/dom/base/test/";
+
+function ev(event, file, hasElement = true) {
+ return {
+ event,
+ url: BASE_URL + file,
+ hasElement,
+ };
+}
+async function contentTask(item) {
+ const { promise, resolve, reject } = Promise.withResolvers();
+ const observer = function (subject, topic, data) {
+ const param = {};
+ for (const line of data.split("\n")) {
+ const m = line.match(/^([^:]+):(.*)/);
+ param[m[1]] = m[2];
+ }
+
+ if (param.event === "compile:main thread") {
+ return;
+ }
+
+ const event = item.events[0];
+ if (
+ event.event === param.event &&
+ event.url === param.url &&
+ (event.hasElement ? param.id === "watchme" : !param.id)
+ ) {
+ dump("@@@ Got expected event: " + data + "\n");
+ item.events.shift();
+ if (item.events.length === 0) {
+ resolve();
+ }
+ } else {
+ dump("@@@ Got unexpected event: " + data + "\n");
+ dump("@@@ Expected: " + JSON.stringify(event) + "\n");
+ }
+ };
+ Services.obs.addObserver(observer, "ScriptLoaderTest");
+
+ const script = content.document.createElement("script");
+ script.id = "watchme";
+ script.src = item.file;
+ content.document.body.appendChild(script);
+
+ await promise;
+
+ Services.obs.removeObserver(observer, "ScriptLoaderTest");
+}
+
+async function runTests(tests) {
+ await BrowserTestUtils.withNewTab(BASE_URL + "empty.html", async browser => {
+ for (const test of tests) {
+ ChromeUtils.clearResourceCache();
+ Services.cache2.clear();
+
+ for (let i = 0; i < test.items.length; i++) {
+ const item = test.items[i];
+ info(`start: ${test.title} (item ${i})`);
+ await SpecialPowers.spawn(browser, [item], contentTask);
+ }
+
+ ok(true, "end: " + test.title);
+ }
+ });
+
+ ok(true, "Finished all tests");
+}
+
+add_task(async function testDiskCache() {
+ await SpecialPowers.pushPrefEnv({
+ set: [
+ ["dom.expose_test_interfaces", true],
+ ["dom.script_loader.experimental.navigation_cache", false],
+ ],
+ });
+
+ await runTests([
+ // A small file shouldn't be saved to the disk.
+ {
+ title: "small file",
+ items: [
+ {
+ file: "file_js_cache_small.js",
+ events: [
+ ev("load:source", "file_js_cache_small.js"),
+ ev("evaluate:classic", "file_js_cache_small.js"),
+ ev("diskcache:disabled", "file_js_cache_small.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_small.js",
+ events: [
+ ev("load:source", "file_js_cache_small.js"),
+ ev("evaluate:classic", "file_js_cache_small.js"),
+ ev("diskcache:disabled", "file_js_cache_small.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_small.js",
+ events: [
+ ev("load:source", "file_js_cache_small.js"),
+ ev("evaluate:classic", "file_js_cache_small.js"),
+ ev("diskcache:disabled", "file_js_cache_small.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_small.js",
+ events: [
+ ev("load:source", "file_js_cache_small.js"),
+ ev("evaluate:classic", "file_js_cache_small.js"),
+ ev("diskcache:disabled", "file_js_cache_small.js"),
+ ],
+ },
+ ],
+ },
+
+ // A large file should be saved to the disk on the 4th load, and should be
+ // used on the 5th load. Also the 5th load shouldn't overwrite the cache.
+ {
+ title: "large file",
+ items: [
+ {
+ file: "file_js_cache_large.js",
+ events: [
+ ev("load:source", "file_js_cache_large.js"),
+ ev("evaluate:classic", "file_js_cache_large.js"),
+ ev("diskcache:disabled", "file_js_cache_large.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large.js",
+ events: [
+ ev("load:source", "file_js_cache_large.js"),
+ ev("evaluate:classic", "file_js_cache_large.js"),
+ ev("diskcache:disabled", "file_js_cache_large.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large.js",
+ events: [
+ ev("load:source", "file_js_cache_large.js"),
+ ev("evaluate:classic", "file_js_cache_large.js"),
+ ev("diskcache:disabled", "file_js_cache_large.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large.js",
+ events: [
+ ev("load:source", "file_js_cache_large.js"),
+ ev("evaluate:classic", "file_js_cache_large.js"),
+ ev("diskcache:register", "file_js_cache_large.js"),
+ ev("diskcache:saved", "file_js_cache_large.js", false),
+ ],
+ },
+ {
+ file: "file_js_cache_large.js",
+ events: [
+ ev("load:diskcache", "file_js_cache_large.js"),
+ ev("evaluate:classic", "file_js_cache_large.js"),
+ ev("diskcache:disabled", "file_js_cache_large.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large.js",
+ events: [
+ ev("load:diskcache", "file_js_cache_large.js"),
+ ev("evaluate:classic", "file_js_cache_large.js"),
+ ev("diskcache:disabled", "file_js_cache_large.js"),
+ ],
+ },
+ ],
+ },
+
+ // A file with compile error shouldn't be saved to the disk.
+ {
+ title: "syntax error",
+ items: [
+ {
+ file: "file_js_cache_large_syntax_error.js",
+ events: [
+ ev("load:source", "file_js_cache_large_syntax_error.js"),
+ ev("diskcache:disabled", "file_js_cache_large_syntax_error.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large_syntax_error.js",
+ events: [
+ ev("load:source", "file_js_cache_large_syntax_error.js"),
+ ev("diskcache:disabled", "file_js_cache_large_syntax_error.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large_syntax_error.js",
+ events: [
+ ev("load:source", "file_js_cache_large_syntax_error.js"),
+ ev("diskcache:disabled", "file_js_cache_large_syntax_error.js"),
+ ],
+ },
+ {
+ file: "file_js_cache_large_syntax_error.js",
+ events: [
+ ev("load:source", "file_js_cache_large_syntax_error.js"),
+ ev("diskcache:disabled", "file_js_cache_large_syntax_error.js"),
+ ],
+ },
+ ],
+ },
+ ]);
+
+ await SpecialPowers.popPrefEnv();
+});
diff --git a/dom/base/test/file_js_cache_large.js b/dom/base/test/file_js_cache_large.js
@@ -0,0 +1,20 @@
+// Large file that passes the file size condition for the disk cache.
+//
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+
+function foo() {}
diff --git a/dom/base/test/file_js_cache_large_syntax_error.js b/dom/base/test/file_js_cache_large_syntax_error.js
@@ -0,0 +1,22 @@
+// Large file that passes the file size condition for the disk cache,
+// but has a syntax error.
+//
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+// A large comment block to make the file size pass the condition.
+
+function foo() {
+
diff --git a/dom/base/test/file_js_cache_small.js b/dom/base/test/file_js_cache_small.js
@@ -0,0 +1,3 @@
+// Small file that doesn't pass the file size condition for the disk cache.
+
+function foo() {}
diff --git a/eslint-ignores.config.mjs b/eslint-ignores.config.mjs
@@ -153,6 +153,7 @@ export default [
// Intentional broken files
"dom/base/test/file_js_cache_syntax_error.js",
+ "dom/base/test/file_js_cache_large_syntax_error.js",
"dom/base/test/jsmodules/test_scriptNotParsedAsModule.html",
"dom/base/test/jsmodules/test_syntaxError.html",
"dom/base/test/jsmodules/test_syntaxErrorAsync.html",