browser_scriptCache_container.js (2212B)
1 requestLongerTimeout(2); 2 3 const TEST_SCRIPT_URL = 4 "https://example.com/browser/dom/tests/browser/page_scriptCache_container.html"; 5 const TEST_MODULE_URL = 6 "https://example.com/browser/dom/tests/browser/page_scriptCache_container_module.html"; 7 8 const TEST_SJS_URL = 9 "https://example.com/browser/dom/tests/browser/counter_server.sjs"; 10 11 async function testCache({ enableCache, type }) { 12 await SpecialPowers.pushPrefEnv({ 13 set: [["dom.script_loader.experimental.navigation_cache", enableCache]], 14 }); 15 registerCleanupFunction(() => SpecialPowers.popPrefEnv()); 16 17 const response1 = await fetch(TEST_SJS_URL + "?reset"); 18 is(await response1.text(), "reset", "Server state should be reset"); 19 20 ChromeUtils.clearResourceCache(); 21 Services.cache2.clear(); 22 23 async function getCounter(tab) { 24 return SpecialPowers.spawn(tab.linkedBrowser, [], () => { 25 return content.document.body.getAttribute("counter"); 26 }); 27 } 28 29 async function openTab(url, userContextId) { 30 const tab = BrowserTestUtils.addTab(gBrowser, url, { userContextId }); 31 await BrowserTestUtils.browserLoaded(tab.linkedBrowser); 32 return tab; 33 } 34 35 const url = type === "script" ? TEST_SCRIPT_URL : TEST_MODULE_URL; 36 37 // Loading script from different containers should use separate cache. 38 const tab0 = await openTab(url, 1); 39 is(await getCounter(tab0), "0"); 40 41 const tab1 = await openTab(url, 2); 42 is(await getCounter(tab1), "1"); 43 44 // Reloading the page should use the cached script, for each container. 45 await BrowserTestUtils.reloadTab(tab0); 46 is(await getCounter(tab0), "0"); 47 48 await BrowserTestUtils.reloadTab(tab1); 49 is(await getCounter(tab1), "1"); 50 51 BrowserTestUtils.removeTab(tab0); 52 BrowserTestUtils.removeTab(tab1); 53 } 54 55 add_task(async function testScriptNoCache() { 56 await testCache({ 57 enableCache: false, 58 type: "script", 59 }); 60 }); 61 62 add_task(async function testScriptCache() { 63 await testCache({ 64 enableCache: true, 65 type: "script", 66 }); 67 }); 68 69 add_task(async function testModuleNoCache() { 70 await testCache({ 71 enableCache: false, 72 type: "module", 73 }); 74 }); 75 76 add_task(async function testModuleCache() { 77 await testCache({ 78 enableCache: true, 79 type: "module", 80 }); 81 });