browser_useractivation_key_events.js (3083B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const BASE = getRootDirectory(gTestPath).replace( 7 "chrome://mochitests/content", 8 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 9 "http://example.com" 10 ); 11 const TEST_URL = BASE + "empty.html"; 12 13 async function synthesizeKeyAndTest(aBrowser, aKey, aEvent, aIsActive) { 14 let promise = SpecialPowers.spawn( 15 aBrowser, 16 [aKey, aEvent, aIsActive], 17 async (key, event, isActive) => { 18 return new Promise(aResolve => { 19 content.document.clearUserGestureActivation(); 20 content.document.addEventListener( 21 "keydown", 22 function (e) { 23 e.preventDefault(); 24 is( 25 content.document.hasBeenUserGestureActivated, 26 isActive, 27 `check has-been-user-activated for ${key} with ${JSON.stringify(event)}` 28 ); 29 is( 30 content.document.hasValidTransientUserGestureActivation, 31 isActive, 32 `check has-valid-transient-user-activation for ${key} with ${JSON.stringify(event)}` 33 ); 34 aResolve(); 35 }, 36 { once: true } 37 ); 38 }); 39 } 40 ); 41 // Ensure the event listener has registered on the remote. 42 await SpecialPowers.spawn(aBrowser, [], () => { 43 return new Promise(resolve => { 44 SpecialPowers.executeSoon(resolve); 45 }); 46 }); 47 EventUtils.synthesizeKey(aKey, aEvent); 48 return promise; 49 } 50 51 let browser; 52 add_setup(async function setup() { 53 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL); 54 browser = tab.linkedBrowser; 55 registerCleanupFunction(async () => { 56 BrowserTestUtils.removeTab(tab); 57 }); 58 }); 59 60 add_task(async function TestPrintableKey() { 61 let tests = ["a", "b", "c", "A", "B", "1", "2", "3"]; 62 63 for (let key of tests) { 64 await synthesizeKeyAndTest(browser, key, {}, true); 65 } 66 }); 67 68 add_task(async function TestNonPrintableKey() { 69 let tests = [ 70 ["KEY_Backspace", false], 71 ["KEY_Control", false], 72 ["KEY_Shift", false], 73 ["KEY_Escape", false], 74 // Treat as user input 75 ["KEY_Tab", true], 76 ["KEY_Enter", true], 77 [" ", true], 78 ]; 79 80 for (let [key, expectedResult] of tests) { 81 await synthesizeKeyAndTest(browser, key, {}, expectedResult); 82 } 83 }); 84 85 add_task(async function TestModifier() { 86 let tests = [ 87 ["a", { accelKey: true }, false], 88 ["z", { accelKey: true }, false], 89 ["a", { metaKey: true }, !navigator.platform.includes("Mac")], 90 // Treat as user input 91 ["a", { altGraphKey: true }, true], 92 ["a", { fnKey: true }, true], 93 ["a", { altKey: true }, true], 94 ["a", { shiftKey: true }, true], 95 ["c", { altKey: true }, true], 96 ["c", { accelKey: true }, true], 97 ["v", { altKey: true }, true], 98 ["v", { accelKey: true }, true], 99 ["x", { altKey: true }, true], 100 ["x", { accelKey: true }, true], 101 ]; 102 103 for (let [key, event, expectedResult] of tests) { 104 await synthesizeKeyAndTest(browser, key, event, expectedResult); 105 } 106 });