browser_resources_clear_resources.js (3027B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test the clearResources function of the ResourceCommand 7 8 add_task(async () => { 9 const tab = await addTab(`${URL_ROOT_SSL}empty.html`); 10 const { client, resourceCommand, targetCommand } = 11 await initResourceCommand(tab); 12 13 info("Assert the initial no of resources"); 14 assertNoOfResources(resourceCommand, 0, 0); 15 16 const onAvailable = () => {}; 17 const onUpdated = () => {}; 18 19 await resourceCommand.watchResources( 20 [ 21 resourceCommand.TYPES.CONSOLE_MESSAGE, 22 resourceCommand.TYPES.NETWORK_EVENT, 23 ], 24 { onAvailable, onUpdated } 25 ); 26 27 info("Log some messages"); 28 await logConsoleMessages(tab.linkedBrowser, ["log1", "log2", "log3"]); 29 30 info("Trigger some network requests"); 31 const EXAMPLE_DOMAIN = "https://example.com/"; 32 await triggerNetworkRequests(tab.linkedBrowser, [ 33 `await fetch("${EXAMPLE_DOMAIN}/request1.html", { method: "GET" });`, 34 `await fetch("${EXAMPLE_DOMAIN}/request2.html", { method: "GET" });`, 35 ]); 36 37 info("Wait for initial message resources"); 38 await waitFor( 39 () => 40 resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE) 41 .length == 3 42 ); 43 info("Wait for initial network resources"); 44 await waitFor( 45 () => 46 resourceCommand.getAllResources(resourceCommand.TYPES.NETWORK_EVENT) 47 .length == 2 48 ); 49 50 assertNoOfResources(resourceCommand, 3, 2); 51 52 info("Clear the network event resources"); 53 await resourceCommand.clearResources([resourceCommand.TYPES.NETWORK_EVENT]); 54 assertNoOfResources(resourceCommand, 3, 0); 55 56 info("Clear the console message resources"); 57 await resourceCommand.clearResources([resourceCommand.TYPES.CONSOLE_MESSAGE]); 58 assertNoOfResources(resourceCommand, 0, 0); 59 60 resourceCommand.unwatchResources( 61 [ 62 resourceCommand.TYPES.CONSOLE_MESSAGE, 63 resourceCommand.TYPES.NETWORK_EVENT, 64 ], 65 { onAvailable, onUpdated, ignoreExistingResources: true } 66 ); 67 68 targetCommand.destroy(); 69 await client.close(); 70 }); 71 72 function assertNoOfResources( 73 resourceCommand, 74 expectedNoOfConsoleMessageResources, 75 expectedNoOfNetworkEventResources 76 ) { 77 const actualNoOfConsoleMessageResources = resourceCommand.getAllResources( 78 resourceCommand.TYPES.CONSOLE_MESSAGE 79 ).length; 80 is( 81 actualNoOfConsoleMessageResources, 82 expectedNoOfConsoleMessageResources, 83 `There are ${actualNoOfConsoleMessageResources} console messages resources` 84 ); 85 86 const actualNoOfNetworkEventResources = resourceCommand.getAllResources( 87 resourceCommand.TYPES.NETWORK_EVENT 88 ).length; 89 is( 90 actualNoOfNetworkEventResources, 91 expectedNoOfNetworkEventResources, 92 `There are ${actualNoOfNetworkEventResources} network event resources` 93 ); 94 } 95 96 function logConsoleMessages(browser, messages) { 97 return SpecialPowers.spawn(browser, [messages], innerMessages => { 98 for (const message of innerMessages) { 99 content.console.log(message); 100 } 101 }); 102 }