browser_resources_target_switching.js (2771B)
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 behavior of ResourceCommand when the top level target changes 7 8 const TEST_URI = 9 "data:text/html;charset=utf-8,<script>console.log('foo');</script>"; 10 11 add_task(async function () { 12 const tab = await addTab(TEST_URI); 13 14 const { client, resourceCommand, targetCommand } = 15 await initResourceCommand(tab); 16 const { CONSOLE_MESSAGE, SOURCE } = resourceCommand.TYPES; 17 18 info("Check the resources gotten from getAllResources at initial"); 19 is( 20 resourceCommand.getAllResources(CONSOLE_MESSAGE).length, 21 0, 22 "There is no resources before calling watchResources" 23 ); 24 25 info( 26 "Start to watch the available resources in order to compare with resources gotten from getAllResources" 27 ); 28 const availableResources = []; 29 const onAvailable = resources => { 30 availableResources.push(...resources); 31 }; 32 await resourceCommand.watchResources([CONSOLE_MESSAGE], { onAvailable }); 33 34 is(availableResources.length, 1, "Got the page message"); 35 is( 36 availableResources[0].arguments[0], 37 "foo", 38 "Got the expected page message" 39 ); 40 41 // Register another listener before unregistering the console listener 42 // otherwise the resource command stop watching for targets 43 const onSourceAvailable = () => {}; 44 await resourceCommand.watchResources([SOURCE], { 45 onAvailable: onSourceAvailable, 46 }); 47 48 info( 49 "Unregister the console listener and check that we no longer listen for console messages" 50 ); 51 resourceCommand.unwatchResources([CONSOLE_MESSAGE], { 52 onAvailable, 53 }); 54 55 let onSwitched = targetCommand.once("switched-target"); 56 info("Navigate to another process"); 57 BrowserTestUtils.startLoadingURIString( 58 gBrowser.selectedBrowser, 59 "about:robots" 60 ); 61 await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); 62 await onSwitched; 63 64 is( 65 availableResources.length, 66 1, 67 "about:robots doesn't fire any new message, so we should have a new one" 68 ); 69 70 info("Navigate back to data: URI"); 71 onSwitched = targetCommand.once("switched-target"); 72 BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, TEST_URI); 73 await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); 74 await onSwitched; 75 76 is( 77 availableResources.length, 78 1, 79 "the data:URI fired a message, but we are no longer listening to it, so no new one should be notified" 80 ); 81 is( 82 resourceCommand.getAllResources(CONSOLE_MESSAGE).length, 83 0, 84 "As we are no longer listening to CONSOLE message, we should not collect any" 85 ); 86 87 resourceCommand.unwatchResources([SOURCE], { 88 onAvailable: onSourceAvailable, 89 }); 90 91 targetCommand.destroy(); 92 await client.close(); 93 });