browser_jsterm_screenshot_command_clipboard.js (5563B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that screenshot command works properly 5 6 "use strict"; 7 8 const TEST_URI = 9 "http://example.com/browser/devtools/client/webconsole/" + 10 "test/browser/test_jsterm_screenshot_command.html"; 11 12 // on some machines, such as macOS, dpr is set to 2. This is expected behavior, however 13 // to keep tests consistant across OSs we are setting the dpr to 1 14 const dpr = "--dpr 1"; 15 16 add_task(async function () { 17 const hud = await openNewTabAndConsole(TEST_URI); 18 ok(hud, "web console opened"); 19 20 await testClipboard(hud); 21 await testFullpageClipboard(hud); 22 await testSelectorClipboard(hud); 23 await testFullpageClipboardScrollbar(hud); 24 }); 25 26 async function testClipboard(hud) { 27 const command = `:screenshot --clipboard ${dpr}`; 28 await executeScreenshotClipboardCommand(hud, command); 29 const contentSize = await getContentSize(); 30 const imgSize = await getImageSizeFromClipboard(); 31 32 is( 33 imgSize.width, 34 contentSize.innerWidth, 35 "Clipboard: Image width matches window size" 36 ); 37 is( 38 imgSize.height, 39 contentSize.innerHeight, 40 "Clipboard: Image height matches window size" 41 ); 42 } 43 44 async function testFullpageClipboard(hud) { 45 const command = `:screenshot --fullpage --clipboard ${dpr}`; 46 await executeScreenshotClipboardCommand(hud, command); 47 const contentSize = await getContentSize(); 48 const imgSize = await getImageSizeFromClipboard(); 49 50 is( 51 imgSize.width, 52 contentSize.innerWidth + contentSize.scrollMaxX - contentSize.scrollMinX, 53 "Fullpage Clipboard: Image width matches page size" 54 ); 55 is( 56 imgSize.height, 57 contentSize.innerHeight + contentSize.scrollMaxY - contentSize.scrollMinY, 58 "Fullpage Clipboard: Image height matches page size" 59 ); 60 } 61 62 async function testSelectorClipboard(hud) { 63 const command = `:screenshot --selector "img#testImage" --clipboard ${dpr}`; 64 await executeScreenshotClipboardCommand(hud, command); 65 66 const imgSize1 = await getImageSizeFromClipboard(); 67 await SpecialPowers.spawn( 68 gBrowser.selectedBrowser, 69 [imgSize1], 70 function (imgSize) { 71 const img = content.document.querySelector("#testImage"); 72 is( 73 imgSize.width, 74 img.clientWidth, 75 "Selector Clipboard: Image width matches element size" 76 ); 77 is( 78 imgSize.height, 79 img.clientHeight, 80 "Selector Clipboard: Image height matches element size" 81 ); 82 } 83 ); 84 } 85 86 async function testFullpageClipboardScrollbar(hud) { 87 info("Test taking a fullpage image that overflows"); 88 await createScrollbarOverflow(); 89 90 const command = `:screenshot --fullpage --clipboard ${dpr}`; 91 await executeScreenshotClipboardCommand(hud, command); 92 const contentSize = await getContentSize(); 93 const imgSize = await getImageSizeFromClipboard(); 94 95 const scrollbarSize = await getScrollbarSize(); 96 is( 97 imgSize.width, 98 contentSize.innerWidth + 99 contentSize.scrollMaxX - 100 contentSize.scrollMinX - 101 scrollbarSize.width, 102 "Scroll Fullpage Clipboard: Image width matches page size minus scrollbar size" 103 ); 104 is( 105 imgSize.height, 106 contentSize.innerHeight + 107 contentSize.scrollMaxY - 108 contentSize.scrollMinY - 109 scrollbarSize.height, 110 "Scroll Fullpage Clipboard: Image height matches page size minus scrollbar size" 111 ); 112 } 113 114 /** 115 * Executes the command string and returns a Promise that resolves when the message 116 * saying that the screenshot was copied to clipboard is rendered in the console. 117 * 118 * @param {WebConsole} hud 119 * @param {string} command 120 */ 121 function executeScreenshotClipboardCommand(hud, command) { 122 return executeAndWaitForMessageByType( 123 hud, 124 command, 125 "Screenshot copied to clipboard.", 126 ".console-api" 127 ); 128 } 129 130 async function createScrollbarOverflow() { 131 // Trigger scrollbars by forcing document to overflow 132 // This only affects results on OSes with scrollbars that reduce document size 133 // (non-floating scrollbars). With default OS settings, this means Windows 134 // and Linux are affected, but Mac is not. For Mac to exhibit this behavior, 135 // change System Preferences -> General -> Show scroll bars to Always. 136 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () { 137 content.document.body.classList.add("overflow"); 138 return content.windowUtils.flushLayoutWithoutThrottledAnimations(); 139 }); 140 141 // Let's wait for next tick so scrollbars have the time to be rendered 142 await waitForTick(); 143 } 144 145 async function getScrollbarSize() { 146 const scrollbarSize = await SpecialPowers.spawn( 147 gBrowser.selectedBrowser, 148 [], 149 function () { 150 const winUtils = content.windowUtils; 151 const scrollbarHeight = {}; 152 const scrollbarWidth = {}; 153 winUtils.getScrollbarSize(true, scrollbarWidth, scrollbarHeight); 154 return { 155 width: scrollbarWidth.value, 156 height: scrollbarHeight.value, 157 }; 158 } 159 ); 160 info(`Scrollbar size: ${scrollbarSize.width}x${scrollbarSize.height}`); 161 return scrollbarSize; 162 } 163 164 async function getContentSize() { 165 const contentSize = await SpecialPowers.spawn( 166 gBrowser.selectedBrowser, 167 [], 168 function () { 169 return { 170 scrollMaxY: content.scrollMaxY, 171 scrollMaxX: content.scrollMaxX, 172 scrollMinY: content.scrollMinY, 173 scrollMinX: content.scrollMinX, 174 innerWidth: content.innerWidth, 175 innerHeight: content.innerHeight, 176 }; 177 } 178 ); 179 180 info(`content size: ${contentSize.innerWidth}x${contentSize.innerHeight}`); 181 return contentSize; 182 }