test_console_serviceworker_cached.html (3804B)
1 <!DOCTYPE HTML> 2 <html lang="en"> 3 <head> 4 <meta charset="utf8"> 5 <title>Test for getCachedMessages and Service Workers</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <script type="text/javascript" src="common.js"></script> 8 <!-- Any copyright is dedicated to the Public Domain. 9 - http://creativecommons.org/publicdomain/zero/1.0/ --> 10 </head> 11 <body> 12 <p>Test for getCachedMessages and Service Workers</p> 13 14 <script class="testbody" type="text/javascript"> 15 "use strict"; 16 17 SimpleTest.waitForExplicitFinish(); 18 19 const BASE_URL = "https://example.com/chrome/devtools/shared/webconsole/test/chrome/"; 20 const SERVICE_WORKER_URL = BASE_URL + "helper_serviceworker.js"; 21 const FRAME_URL = BASE_URL + "sandboxed_iframe.html"; 22 23 const firstTabExpectedCalls = [ 24 { 25 message: { 26 level: "log", 27 filename: /helper_serviceworker/, 28 arguments: ['script evaluation'], 29 } 30 }, { 31 message: { 32 level: "log", 33 filename: /helper_serviceworker/, 34 arguments: ['Here is a SAB'], 35 } 36 }, { 37 message: { 38 level: "log", 39 filename: /helper_serviceworker/, 40 arguments: ['install event'], 41 } 42 }, { 43 message: { 44 level: "log", 45 filename: /helper_serviceworker/, 46 arguments: ['activate event'], 47 } 48 }, 49 ]; 50 51 const secondTabExpectedCalls = [ 52 { 53 message: { 54 level: "log", 55 filename: /helper_serviceworker/, 56 arguments: ['fetch event: ' + FRAME_URL], 57 } 58 } 59 ]; 60 61 const startTest = async function () { 62 removeEventListener("load", startTest); 63 64 await new Promise(resolve => { 65 SpecialPowers.pushPrefEnv({"set": [ 66 ["dom.serviceWorkers.enabled", true], 67 ["devtools.webconsole.filter.serviceworkers", true], 68 [ 69 "dom.postMessage.sharedArrayBuffer.bypassCOOP_COEP.insecure.enabled", 70 true 71 ], 72 ]}, resolve); 73 }); 74 75 info("Adding a tab and attaching a service worker"); 76 const tab1 = await addTab(FRAME_URL); 77 const swr = await withActiveServiceWorker(tab1.linkedBrowser.contentWindow, 78 SERVICE_WORKER_URL); 79 80 info("Attaching console to tab 1"); 81 let {state} = await attachConsoleToTab(["ConsoleAPI"]); 82 let calls = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]); 83 checkConsoleAPICalls(calls.messages, firstTabExpectedCalls); 84 await closeDebugger(state); 85 86 // Because this tab is being added after the original messages happened, 87 // they shouldn't show up in a call to getCachedMessages. 88 // However, there is a fetch event which is logged due to loading the tab. 89 info("Adding a new tab at the same URL"); 90 91 await addTab(FRAME_URL); 92 info("Attaching console to tab 2"); 93 state = (await attachConsoleToTab(["ConsoleAPI"])).state; 94 calls = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]); 95 checkConsoleAPICalls(calls.messages, secondTabExpectedCalls); 96 await closeDebugger(state); 97 98 await swr.unregister(); 99 100 SimpleTest.finish(); 101 }; 102 addEventListener("load", startTest); 103 104 // This test needs to add tabs that are controlled by a service worker 105 // so use some special powers to dig around and find gBrowser 106 const {gBrowser} = SpecialPowers._getTopChromeWindow(SpecialPowers.window); 107 108 SimpleTest.registerCleanupFunction(() => { 109 while (gBrowser.tabs.length > 1) { 110 gBrowser.removeCurrentTab(); 111 } 112 }); 113 114 function addTab(url) { 115 info("Adding a new tab with URL: '" + url + "'"); 116 return new Promise(resolve => { 117 const tab = gBrowser.selectedTab = gBrowser.addTab(url, { 118 triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(), 119 }); 120 gBrowser.selectedBrowser.addEventListener("load", function () { 121 info("URL '" + url + "' loading complete"); 122 resolve(tab); 123 }, {capture: true, once: true}); 124 }); 125 } 126 127 </script> 128 </body> 129 </html>