browser_scriptCache_partition.js (3720B)
1 requestLongerTimeout(2); 2 3 const TEST_SCRIPT_URL_0 = 4 "https://example.com/browser/dom/tests/browser/page_scriptCache_partition.html"; 5 6 const TEST_SCRIPT_URL_1 = 7 "https://example.org/browser/dom/tests/browser/page_scriptCache_partition.html"; 8 9 const TEST_MODULE_URL_0 = 10 "https://example.com/browser/dom/tests/browser/page_scriptCache_partition_module.html"; 11 12 const TEST_MODULE_URL_1 = 13 "https://example.org/browser/dom/tests/browser/page_scriptCache_partition_module.html"; 14 15 const TEST_SJS_URL = 16 "https://example.net/browser/dom/tests/browser/counter_server.sjs"; 17 18 async function testScriptCacheAndPartition({ enableCache, type }) { 19 await SpecialPowers.pushPrefEnv({ 20 set: [["dom.script_loader.experimental.navigation_cache", enableCache]], 21 }); 22 registerCleanupFunction(() => SpecialPowers.popPrefEnv()); 23 24 const response1 = await fetch(TEST_SJS_URL + "?reset"); 25 is(await response1.text(), "reset", "Server state should be reset"); 26 27 ChromeUtils.clearResourceCache(); 28 Services.cache2.clear(); 29 30 async function getCounter() { 31 return SpecialPowers.spawn(tab.linkedBrowser, [], () => { 32 const iframe = content.document.querySelector("iframe"); 33 return SpecialPowers.spawn(iframe, [], () => { 34 return content.document.body.getAttribute("counter"); 35 }); 36 }); 37 } 38 39 async function load(url) { 40 const loadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser); 41 tab.linkedBrowser.loadURI(Services.io.newURI(url), { 42 triggeringPrincipal: tab.linkedBrowser.nodePrincipal, 43 }); 44 await loadedPromise; 45 } 46 47 // Loading script from the different top-level frame domain should use 48 // separate cache for each partition, if the partitioning is enabled. 49 // 50 // Each top-level document are loaded into separate processes, say, 51 // process A for url0 (example.com), and process B for url1 52 // (example.org). 53 // 54 // If Fission is enabled, the iframe (example.net) is loaded into yet another 55 // process C, both with the url0 load and the url1 load. 56 // In this case, they share single SharedScriptCache instance. 57 // If partitioning is enabled, the cache for the script loaded by the iframe 58 // should be separated for each. 59 // If partitioning is disabled, the cache for the script loaded by the iframe 60 // should be shared. 61 // 62 // If Fission is not enabled, the iframe is loaded into process A and B, 63 // and they don't share the cache entry in SharedScriptCache. 64 // 65 // If navigation cache is not enabled, then the partitioning is done by 66 // the Necko side, and the Necko cache should also be partitioned. 67 const url0 = type === "script" ? TEST_SCRIPT_URL_0 : TEST_MODULE_URL_0; 68 const url1 = type === "script" ? TEST_SCRIPT_URL_1 : TEST_MODULE_URL_1; 69 70 const tab = await BrowserTestUtils.openNewForegroundTab({ 71 gBrowser, 72 url: url0, 73 }); 74 is(await getCounter(), "0"); 75 76 await load(url1); 77 is(await getCounter(), "1"); 78 79 // Reloading the page should use the cached script, for each partition. 80 await load(url0); 81 is(await getCounter(), "0"); 82 83 await load(url1); 84 is(await getCounter(), "1"); 85 86 BrowserTestUtils.removeTab(tab); 87 } 88 89 add_task(async function testScriptNoCachePartition() { 90 await testScriptCacheAndPartition({ 91 enableCache: false, 92 type: "script", 93 }); 94 }); 95 96 add_task(async function testScriptCachePartition() { 97 await testScriptCacheAndPartition({ 98 enableCache: true, 99 type: "script", 100 }); 101 }); 102 103 add_task(async function testModuleNoCachePartition() { 104 await testScriptCacheAndPartition({ 105 enableCache: false, 106 type: "module", 107 }); 108 }); 109 110 add_task(async function testModuleCachePartition() { 111 await testScriptCacheAndPartition({ 112 enableCache: true, 113 type: "module", 114 }); 115 });