test_browser.xhtml (9075B)
1 <?xml version="1.0"?> 2 3 <window title="Browser element keyhandling tests" 4 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 5 onload="test();"> 6 7 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 8 <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> 9 <script src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"/> 10 11 <script type="application/javascript"> 12 <![CDATA[ 13 SimpleTest.waitForExplicitFinish(); 14 15 const IS_MAC = navigator.platform.indexOf("Mac") === 0; 16 const VK = {}; 17 const CHARS = {}; 18 19 // Copied values from NativeKeyCodes.js and EventUtils.js 20 if (IS_MAC) { 21 VK.LEFT = MAC_VK_LeftArrow; 22 CHARS.LEFT = "\uF702"; 23 VK.RIGHT = MAC_VK_RightArrow; 24 CHARS.RIGHT = "\uF703"; 25 VK.UP = MAC_VK_UpArrow; 26 CHARS.UP = "\uF700"; 27 VK.DOWN = MAC_VK_DownArrow; 28 CHARS.DOWN = "\uF701"; 29 VK.SPACE = MAC_VK_Space; 30 VK.PGDOWN = MAC_VK_PageDown; 31 CHARS.PGDOWN = "\uF72D"; 32 VK.PGUP = MAC_VK_PageUp; 33 CHARS.PGUP = "\uF72C"; 34 VK.C = MAC_VK_ANSI_C; 35 VK.HOME = MAC_VK_Home; 36 CHARS.HOME = "\uF729"; 37 VK.END = MAC_VK_End; 38 CHARS.END = "\uF72B"; 39 } else { 40 VK.LEFT = WIN_VK_LEFT; 41 CHARS.LEFT = ""; 42 VK.RIGHT = WIN_VK_RIGHT; 43 CHARS.RIGHT = ""; 44 VK.UP = WIN_VK_UP; 45 CHARS.UP = ""; 46 VK.DOWN = WIN_VK_DOWN; 47 CHARS.DOWN = ""; 48 VK.SPACE = WIN_VK_SPACE; 49 VK.PGDOWN = WIN_VK_NEXT; 50 CHARS.PGDOWN = ""; 51 VK.PGUP = WIN_VK_PRIOR; 52 CHARS.PGUP = ""; 53 VK.C = WIN_VK_C; 54 VK.HOME = WIN_VK_HOME; 55 CHARS.HOME = ""; 56 VK.END = WIN_VK_END; 57 CHARS.END = ""; 58 } 59 60 function waitForEvent(target, event) { 61 info(`Waiting for ${event} event.`); 62 return new Promise(resolve => { 63 browser.addEventListener(event, resolve, { once: true }); 64 }); 65 } 66 67 function synthesizeKey(keyCode, modifiers, chars) { 68 return new Promise((resolve, reject) => { 69 if (!synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, keyCode, modifiers, chars, chars, resolve)) { 70 reject(); 71 } 72 }); 73 } 74 75 function getWindowProperties(browser, properties) { 76 let results = {}; 77 for (let prop of properties) { 78 results[prop] = browser.contentWindow[prop]; 79 } 80 81 return results; 82 } 83 84 function getScrollPosition(browser) { 85 return getWindowProperties(browser, ["scrollX", "scrollY"]); 86 } 87 88 async function test() { 89 // Smooth scrolling makes scroll events take time and it's difficult to know 90 // when they've ended, so turn it off for this test. 91 await SpecialPowers.pushPrefEnv({"set": [["general.smoothScroll", false]] }); 92 93 let browser = document.getElementById("browser"); 94 browser.focus(); 95 let { scrollX, scrollY } = await getScrollPosition(browser); 96 is(scrollX, 0, "Should not be scrolled"); 97 is(scrollY, 0, "Should not be scrolled"); 98 99 info("down"); 100 await synthesizeKey(VK.DOWN, {}, CHARS.DOWN); 101 await waitForEvent(browser.contentWindow, "scroll"); 102 let { scrollX: lineScrollX, scrollY: lineScrollY } = await getScrollPosition(browser); 103 is(lineScrollX, 0, "Should not be scrolled"); 104 ok(lineScrollY > 0, "Should be scrolled"); 105 106 info("up"); 107 await synthesizeKey(VK.UP, {}, CHARS.UP); 108 await waitForEvent(browser.contentWindow, "scroll"); 109 { 110 let { scrollX, scrollY } = await getScrollPosition(browser); 111 is(scrollX, 0, "Should not be scrolled"); 112 is(scrollY, 0, "Should not be scrolled"); 113 } 114 115 info("right"); 116 await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT); 117 await waitForEvent(browser.contentWindow, "scroll"); 118 let { scrollX: rightScrollX, scrollY: rightScrollY } = await getScrollPosition(browser); 119 ok(rightScrollX > 0, "Should be scrolled"); 120 is(rightScrollY, 0, "Should not be scrolled"); 121 122 info("left"); 123 await synthesizeKey(VK.LEFT, {}, CHARS.LEFT); 124 await waitForEvent(browser.contentWindow, "scroll"); 125 { 126 let { scrollX, scrollY } = await getScrollPosition(browser); 127 is(scrollX, 0, "Should not be scrolled"); 128 is(scrollY, 0, "Should not be scrolled"); 129 } 130 131 info("space"); 132 await synthesizeKey(VK.SPACE, {}, " "); 133 await waitForEvent(browser.contentWindow, "scroll"); 134 let { scrollX: pageScrollX, scrollY: pageScrollY } = await getScrollPosition(browser); 135 is(pageScrollX, 0, "Should not be scrolled"); 136 ok(pageScrollY > lineScrollY, "Should be scrolled more than a single line"); 137 138 info("shift+space"); 139 await synthesizeKey(VK.SPACE, { shiftKey: true }, " "); 140 await waitForEvent(browser.contentWindow, "scroll"); 141 { 142 let { scrollX, scrollY } = await getScrollPosition(browser); 143 is(scrollX, 0, "Should not be scrolled"); 144 is(scrollY, 0, "Should not be scrolled"); 145 } 146 147 info("page down"); 148 await synthesizeKey(VK.PGDOWN, {}, CHARS.PGDOWN); 149 await waitForEvent(browser.contentWindow, "scroll"); 150 { 151 let { scrollX, scrollY } = await getScrollPosition(browser); 152 is(scrollX, 0, "Should not be scrolled"); 153 is(scrollY, pageScrollY, "Should be scrolled a page"); 154 } 155 156 info("page up"); 157 await synthesizeKey(VK.PGUP, {}, CHARS.PGUP); 158 await waitForEvent(browser.contentWindow, "scroll"); 159 { 160 let { scrollX, scrollY } = await getScrollPosition(browser); 161 is(scrollX, 0, "Should not be scrolled"); 162 is(scrollY, 0, "Should not be scrolled"); 163 } 164 165 info("accel+down"); 166 await synthesizeKey(VK.DOWN, { accelKey: true }, CHARS.DOWN); 167 await waitForEvent(browser.contentWindow, "scroll"); 168 { 169 let { scrollX, scrollY, innerHeight } = await getWindowProperties(browser, ["scrollX", "scrollY", "innerHeight"]); 170 is(scrollX, 0, "Should not be scrolled"); 171 // We can't know the scrollbar height so check that we're scrolled to within 100px of what we expect. 172 isfuzzy(scrollY, browser.contentDocument.body.clientHeight - innerHeight, 100, "Should be scrolled to the end."); 173 } 174 175 info("accel+up"); 176 await synthesizeKey(VK.UP, { accelKey: true }, CHARS.UP); 177 await waitForEvent(browser.contentWindow, "scroll"); 178 { 179 let { scrollX, scrollY } = await getScrollPosition(browser); 180 is(scrollX, 0, "Should not be scrolled"); 181 is(scrollY, 0, "Should not be scrolled"); 182 } 183 184 info("end"); 185 await synthesizeKey(VK.END, {}, CHARS.END); 186 await waitForEvent(browser.contentWindow, "scroll"); 187 { 188 let { scrollX, scrollY, innerHeight } = await getWindowProperties(browser, ["scrollX", "scrollY", "innerHeight"]); 189 is(scrollX, 0, "Should not be scrolled"); 190 // We can't know the scrollbar height so check that we're scrolled to within 100px of what we expect. 191 isfuzzy(scrollY, browser.contentDocument.body.clientHeight - innerHeight, 100, "Should be scrolled to the end."); 192 } 193 194 info("home"); 195 await synthesizeKey(VK.HOME, {}, CHARS.HOME); 196 await waitForEvent(browser.contentWindow, "scroll"); 197 { 198 let { scrollX, scrollY } = await getScrollPosition(browser); 199 is(scrollX, 0, "Should not be scrolled"); 200 is(scrollY, 0, "Should not be scrolled"); 201 } 202 203 // Select the start of the first paragraph 204 let paragraph = browser.contentDocument.getElementById("paragraph"); 205 let selection = browser.contentWindow.getSelection(); 206 selection.setBaseAndExtent(paragraph.firstChild, 0, paragraph.firstChild, "Lots of".length); 207 208 info("copy"); 209 await SimpleTest.promiseClipboardChange("Lots of", () => { 210 synthesizeKey(VK.C, { accelKey: true }, "c"); 211 }); 212 213 for (let i = 0; i < " paragraphs".length; i++) { 214 info("select right"); 215 await synthesizeKey(VK.RIGHT, { shiftKey: true }, CHARS.RIGHT); 216 } 217 218 info("copy"); 219 await SimpleTest.promiseClipboardChange("Lots of paragraphs", () => { 220 synthesizeKey(VK.C, { accelKey: true }, "c"); 221 }); 222 223 for (let i = 0; i < " paragraphs".length; i++) { 224 info("select left"); 225 await synthesizeKey(VK.LEFT, { shiftKey: true }, CHARS.LEFT); 226 } 227 228 info("copy"); 229 await SimpleTest.promiseClipboardChange("Lots of", () => { 230 synthesizeKey(VK.C, { accelKey: true }, "c"); 231 }); 232 233 info("select down"); 234 await synthesizeKey(VK.DOWN, { shiftKey: true }, CHARS.DOWN); 235 236 info("copy"); 237 await SimpleTest.promiseClipboardChange("Lots of paragraphs to make a vertical scrollbar.\n\nLots of", () => { 238 synthesizeKey(VK.C, { accelKey: true }, "c"); 239 }); 240 241 SimpleTest.finish(); 242 } 243 ]]> 244 </script> 245 246 <body xmlns="http://www.w3.org/1999/xhtml"> 247 <p id="display"></p> 248 <div id="content" style="display:none;"></div> 249 <pre id="test"></pre> 250 </body> 251 <browser id="browser" src="browsertest.html" style="height: 500px"/> 252 </window>