browser_target_command_reload.js (4125B)
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 TargetCommand's reload method 7 // 8 // Note that we reload against main process, 9 // but this is hard/impossible to test as it reloads the test script itself 10 // and so stops its execution. 11 12 // Load a page with a JS script that change its value everytime we load it 13 // (that's to see if the reload loads from cache or not) 14 const TEST_URL = URL_ROOT + "incremental-js-value-script.sjs"; 15 16 add_task(async function () { 17 info(" ### Test reloading a Tab"); 18 19 // Load "?setup" to reinitialize the sjs state if necessary. 20 const tab = await addTab(TEST_URL + "?setup"); 21 22 // Now navigate to actual test URL. 23 const onLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); 24 BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, TEST_URL); 25 await onLoaded; 26 27 // Create a TargetCommand for a given test tab 28 const commands = await CommandsFactory.forTab(tab); 29 const targetCommand = commands.targetCommand; 30 31 // We have to start listening in order to ensure having a targetFront available 32 await targetCommand.startListening(); 33 34 const firstJSValue = await getContentVariable(); 35 is(firstJSValue, "1", "Got an initial value for the JS variable"); 36 37 const onReloaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); 38 await targetCommand.reloadTopLevelTarget(); 39 info("Wait for the tab to be reloaded"); 40 await onReloaded; 41 42 const secondJSValue = await getContentVariable(); 43 is( 44 secondJSValue, 45 "1", 46 "The first reload didn't bypass the cache, so the JS Script is the same and we got the same value" 47 ); 48 49 const onSecondReloaded = BrowserTestUtils.browserLoaded( 50 gBrowser.selectedBrowser 51 ); 52 await targetCommand.reloadTopLevelTarget(true); 53 info("Wait for the tab to be reloaded"); 54 await onSecondReloaded; 55 56 // The value is 3 and not 2, because we got a HTTP request, but it returned 304 and the browser fetched his cached content 57 const thirdJSValue = await getContentVariable(); 58 is( 59 thirdJSValue, 60 "3", 61 "The second reload did bypass the cache, so the JS Script is different and we got a new value" 62 ); 63 64 BrowserTestUtils.removeTab(tab); 65 66 await commands.destroy(); 67 }); 68 69 add_task(async function () { 70 info(" ### Test reloading an Add-on"); 71 72 const extension = ExtensionTestUtils.loadExtension({ 73 useAddonManager: "temporary", 74 background() { 75 const { browser } = this; 76 browser.test.log("background script executed"); 77 }, 78 }); 79 80 await extension.startup(); 81 82 const commands = await CommandsFactory.forAddon(extension.id); 83 const targetCommand = commands.targetCommand; 84 85 // We have to start listening in order to ensure having a targetFront available 86 await targetCommand.startListening(); 87 88 // Observe the new document being reported while reloading 89 const reloadedTargets = []; 90 await commands.resourceCommand.watchResources( 91 [commands.resourceCommand.TYPES.DOCUMENT_EVENT], 92 { 93 onAvailable(resources) { 94 for (const resource of resources) { 95 if (resource.name == "dom-complete") { 96 reloadedTargets.push( 97 resource.targetFront.isFallbackExtensionDocument 98 ); 99 } 100 } 101 }, 102 ignoreExistingResources: true, 103 } 104 ); 105 106 const targets = await targetCommand.getAllTargets(targetCommand.ALL_TYPES); 107 for (const targetFront of targets) { 108 is( 109 targetFront.addonId, 110 extension.id, 111 `Target ${targetFront.actorID} has the right addonId attribute` 112 ); 113 } 114 115 await targetCommand.reloadTopLevelTarget(); 116 117 info("Wait for next dom-loading DOCUMENT_EVENT"); 118 await waitFor(() => reloadedTargets.length == 1); 119 120 // We only get a new background document 121 // (the fallback stays alive) 122 Assert.deepEqual(reloadedTargets, [false], "All the targets got reloaded"); 123 124 await commands.destroy(); 125 126 await extension.unload(); 127 }); 128 function getContentVariable() { 129 return SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () { 130 return content.wrappedJSObject.jsValue; 131 }); 132 }