browser_target_configuration_command_dppx.js (5667B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test device pixel ratio override. 7 const TEST_DOCUMENT = "target_configuration_test_doc.sjs"; 8 const TEST_URI = URL_ROOT_COM_SSL + TEST_DOCUMENT; 9 10 add_task(async function () { 11 const tab = await addTab(TEST_URI); 12 13 info("Create commands for the tab"); 14 const commands = await CommandsFactory.forTab(tab); 15 16 const targetConfigurationCommand = commands.targetConfigurationCommand; 17 const targetCommand = commands.targetCommand; 18 await targetCommand.startListening(); 19 20 const originalDpr = await getTopLevelDocumentDevicePixelRatio(); 21 22 info("Update configuration to change device pixel ratio"); 23 const CUSTOM_DPR = 5.5; 24 25 await targetConfigurationCommand.updateConfiguration({ 26 overrideDPPX: CUSTOM_DPR, 27 }); 28 29 is( 30 await getTopLevelDocumentDevicePixelRatio(), 31 CUSTOM_DPR, 32 "The ratio is properly set on the top level document after updating the configuration" 33 ); 34 is( 35 await getIframeDocumentDevicePixelRatio(), 36 CUSTOM_DPR, 37 "The ratio is properly set on the iframe after updating the configuration" 38 ); 39 40 info("Reload the page"); 41 await BrowserTestUtils.reloadTab(tab, { 42 includeSubFrames: true, 43 }); 44 45 is( 46 await getTopLevelDocumentDevicePixelRatioAtStartup(), 47 CUSTOM_DPR, 48 "The custom ratio was set in the content page when it loaded after reloading" 49 ); 50 is( 51 await getTopLevelDocumentDevicePixelRatio(), 52 CUSTOM_DPR, 53 "The custom ratio is set in the content page after reloading" 54 ); 55 is( 56 await getIframeDocumentDevicePixelRatioAtStartup(), 57 CUSTOM_DPR, 58 "The custom ratio was set in the remote iframe when it loaded after reloading" 59 ); 60 is( 61 await getIframeDocumentDevicePixelRatio(), 62 CUSTOM_DPR, 63 "The custom ratio is set in the remote iframe after reloading" 64 ); 65 66 const previousBrowsingContextId = gBrowser.selectedBrowser.browsingContext.id; 67 info( 68 "Check that navigating to a page that forces the creation of a new browsing context keep the simulation enabled" 69 ); 70 71 const onPageLoaded = BrowserTestUtils.browserLoaded( 72 gBrowser.selectedBrowser, 73 /* includeSubFrames */ true 74 ); 75 BrowserTestUtils.startLoadingURIString( 76 gBrowser.selectedBrowser, 77 URL_ROOT_ORG_SSL + TEST_DOCUMENT + "?crossOriginIsolated=true" 78 ); 79 await onPageLoaded; 80 81 isnot( 82 gBrowser.selectedBrowser.browsingContext.id, 83 previousBrowsingContextId, 84 "A new browsing context was created" 85 ); 86 87 is( 88 await getTopLevelDocumentDevicePixelRatioAtStartup(), 89 CUSTOM_DPR, 90 "The custom ratio was set in the content page when it loaded after navigating to a new browsing context" 91 ); 92 is( 93 await getTopLevelDocumentDevicePixelRatio(), 94 CUSTOM_DPR, 95 "The custom ratio is set in the content page after navigating to a new browsing context" 96 ); 97 is( 98 await getIframeDocumentDevicePixelRatioAtStartup(), 99 CUSTOM_DPR, 100 "The custom ratio was set in the remote iframe when it loaded after navigating to a new browsing context" 101 ); 102 is( 103 await getIframeDocumentDevicePixelRatio(), 104 CUSTOM_DPR, 105 "The custom ratio is set in the remote iframe after navigating to a new browsing context" 106 ); 107 108 info( 109 "Create another commands instance and check that destroying it won't reset the ratio" 110 ); 111 const otherCommands = await CommandsFactory.forTab(tab); 112 const otherTargetConfigurationCommand = 113 otherCommands.targetConfigurationCommand; 114 const otherTargetCommand = otherCommands.targetCommand; 115 await otherTargetCommand.startListening(); 116 117 // Let's update the configuration with this commands instance to make sure we hit the TargetConfigurationActor 118 await otherTargetConfigurationCommand.updateConfiguration({ 119 colorSchemeSimulation: "dark", 120 }); 121 122 otherTargetCommand.destroy(); 123 await otherCommands.destroy(); 124 125 is( 126 await getTopLevelDocumentDevicePixelRatio(), 127 CUSTOM_DPR, 128 "The custom ratio is still set on the page after destroying another commands instance" 129 ); 130 131 info( 132 "Check that destroying the commands we overrode the ratio in will reset the page ratio" 133 ); 134 targetCommand.destroy(); 135 await commands.destroy(); 136 137 is( 138 await getTopLevelDocumentDevicePixelRatio(), 139 originalDpr, 140 "The ratio was reset in the content page after destroying the commands" 141 ); 142 is( 143 await getIframeDocumentDevicePixelRatio(), 144 originalDpr, 145 "The ratio was reset in the remote iframe after destroying the commands" 146 ); 147 }); 148 149 function getDevicePixelRatio(browserOrBrowsingContext) { 150 return SpecialPowers.spawn( 151 browserOrBrowsingContext, 152 [], 153 () => content.browsingContext.top.overrideDPPX || content.devicePixelRatio 154 ); 155 } 156 157 function getDevicePixelRatioAtStartup(browserOrBrowsingContext) { 158 return SpecialPowers.spawn( 159 browserOrBrowsingContext, 160 [], 161 () => content.wrappedJSObject.initialDevicePixelRatio 162 ); 163 } 164 165 function getTopLevelDocumentDevicePixelRatio() { 166 return getDevicePixelRatio(gBrowser.selectedBrowser); 167 } 168 169 function getTopLevelDocumentDevicePixelRatioAtStartup() { 170 return getDevicePixelRatioAtStartup(gBrowser.selectedBrowser); 171 } 172 173 function getIframeBrowsingContext() { 174 return SpecialPowers.spawn( 175 gBrowser.selectedBrowser, 176 [], 177 () => content.document.querySelector("iframe").browsingContext 178 ); 179 } 180 181 async function getIframeDocumentDevicePixelRatio() { 182 const iframeBC = await getIframeBrowsingContext(); 183 return getDevicePixelRatio(iframeBC); 184 } 185 186 async function getIframeDocumentDevicePixelRatioAtStartup() { 187 const iframeBC = await getIframeBrowsingContext(); 188 return getDevicePixelRatioAtStartup(iframeBC); 189 }