browser_zoom_commands.js (5838B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_PAGE_URL = 7 "data:text/html;charset=utf-8,<body>test_zoom_levels</body>"; 8 9 /** 10 * Waits for the zoom commands in the window to have the expected enabled 11 * state. 12 * 13 * @param {object} expectedState 14 * An object where each key represents one of the zoom commands, 15 * and the value is a boolean that is true if the command should 16 * be enabled, and false if it should be disabled. 17 * 18 * The keys are "enlarge", "reduce" and "reset" for readability, 19 * and internally this function maps those keys to the appropriate 20 * commands. 21 * @returns {Promise<void>} 22 */ 23 async function waitForCommandEnabledState(expectedState) { 24 const COMMAND_MAP = { 25 enlarge: "cmd_fullZoomEnlarge", 26 reduce: "cmd_fullZoomReduce", 27 reset: "cmd_fullZoomReset", 28 }; 29 30 await TestUtils.waitForCondition(() => { 31 for (let commandKey in expectedState) { 32 let commandID = COMMAND_MAP[commandKey]; 33 let command = document.getElementById(commandID); 34 let expectedEnabled = expectedState[commandKey]; 35 36 if (command.hasAttribute("disabled") == expectedEnabled) { 37 return false; 38 } 39 } 40 Assert.ok("Commands finally reached the expected state."); 41 return true; 42 }, "Waiting for commands to reach the right state."); 43 } 44 45 /** 46 * Tests that the "Zoom Text Only" command is in the right checked 47 * state. 48 * 49 * @param {boolean} isChecked 50 * True if the command should have its "checked" attribute set to 51 * "true". Otherwise, ensures that the attribute is set to "false". 52 */ 53 function assertTextZoomCommandCheckedState(isChecked) { 54 let command = document.getElementById("cmd_fullZoomToggle"); 55 Assert.equal( 56 command.hasAttribute("checked"), 57 isChecked, 58 "Text zoom command has expected checked attribute" 59 ); 60 } 61 62 /** 63 * Tests that zoom commands are properly updated when changing 64 * zoom levels and/or preferences on an individual browser. 65 */ 66 add_task(async function test_update_browser_zoom() { 67 await BrowserTestUtils.withNewTab(TEST_PAGE_URL, async () => { 68 let currentZoom = await FullZoomHelper.getGlobalValue(); 69 Assert.equal( 70 currentZoom, 71 1, 72 "We expect to start at the default zoom level." 73 ); 74 75 await waitForCommandEnabledState({ 76 enlarge: true, 77 reduce: true, 78 reset: false, 79 }); 80 assertTextZoomCommandCheckedState(false); 81 82 // We'll run two variations of this test - one with text zoom enabled, 83 // and the other without. 84 for (let textZoom of [true, false]) { 85 info(`Running variation with textZoom set to ${textZoom}`); 86 87 await SpecialPowers.pushPrefEnv({ 88 set: [["browser.zoom.full", !textZoom]], 89 }); 90 91 // 120% global zoom 92 info("Changing default zoom by a single level"); 93 ZoomManager.zoom = 1.2; 94 95 await waitForCommandEnabledState({ 96 enlarge: true, 97 reduce: true, 98 reset: true, 99 }); 100 await assertTextZoomCommandCheckedState(textZoom); 101 102 // Now max out the zoom level. 103 ZoomManager.zoom = ZoomManager.MAX; 104 105 await waitForCommandEnabledState({ 106 enlarge: false, 107 reduce: true, 108 reset: true, 109 }); 110 await assertTextZoomCommandCheckedState(textZoom); 111 112 // Now min out the zoom level. 113 ZoomManager.zoom = ZoomManager.MIN; 114 await waitForCommandEnabledState({ 115 enlarge: true, 116 reduce: false, 117 reset: true, 118 }); 119 await assertTextZoomCommandCheckedState(textZoom); 120 121 // Now reset back to the default zoom level 122 ZoomManager.zoom = 1; 123 await waitForCommandEnabledState({ 124 enlarge: true, 125 reduce: true, 126 reset: false, 127 }); 128 await assertTextZoomCommandCheckedState(textZoom); 129 } 130 }); 131 }); 132 133 /** 134 * Tests that zoom commands are properly updated when changing 135 * zoom levels when the default zoom is not at 1.0. 136 */ 137 add_task(async function test_update_browser_zoom() { 138 await BrowserTestUtils.withNewTab(TEST_PAGE_URL, async () => { 139 let currentZoom = await FullZoomHelper.getGlobalValue(); 140 Assert.equal( 141 currentZoom, 142 1, 143 "We expect to start at the default zoom level." 144 ); 145 146 // Now change the default zoom to 200%, which is what we'll switch 147 // back to when choosing to reset the zoom level. 148 // 149 // It's a bit maddening that changeDefaultZoom takes values in integer 150 // units from 30 to 500, whereas ZoomManager.zoom takes things in float 151 // units from 0.3 to 5.0, but c'est la vie for now. 152 await FullZoomHelper.changeDefaultZoom(200); 153 registerCleanupFunction(async () => { 154 await FullZoomHelper.changeDefaultZoom(100); 155 }); 156 157 await waitForCommandEnabledState({ 158 enlarge: true, 159 reduce: true, 160 reset: false, 161 }); 162 163 // 120% global zoom 164 info("Changing default zoom by a single level"); 165 ZoomManager.zoom = 2.2; 166 167 await waitForCommandEnabledState({ 168 enlarge: true, 169 reduce: true, 170 reset: true, 171 }); 172 await assertTextZoomCommandCheckedState(false); 173 174 // Now max out the zoom level. 175 ZoomManager.zoom = ZoomManager.MAX; 176 177 await waitForCommandEnabledState({ 178 enlarge: false, 179 reduce: true, 180 reset: true, 181 }); 182 await assertTextZoomCommandCheckedState(false); 183 184 // Now min out the zoom level. 185 ZoomManager.zoom = ZoomManager.MIN; 186 await waitForCommandEnabledState({ 187 enlarge: true, 188 reduce: false, 189 reset: true, 190 }); 191 await assertTextZoomCommandCheckedState(false); 192 193 // Now reset back to the default zoom level 194 ZoomManager.zoom = 2; 195 await waitForCommandEnabledState({ 196 enlarge: true, 197 reduce: true, 198 reset: false, 199 }); 200 await assertTextZoomCommandCheckedState(false); 201 }); 202 });