browser_toolbox_window_shortcuts.js (2492B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 var Startup = Cc["@mozilla.org/devtools/startup-clh;1"].getService( 7 Ci.nsISupports 8 ).wrappedJSObject; 9 var { Toolbox } = require("resource://devtools/client/framework/toolbox.js"); 10 11 var gToolbox, 12 toolIDs, 13 toolShortcuts = [], 14 idIndex, 15 modifiedPrefs = []; 16 17 async function test() { 18 addTab("about:blank").then(async function () { 19 toolIDs = []; 20 for (const [id, definition] of gDevTools._tools) { 21 const shortcut = Startup.KeyShortcuts.filter(s => s.toolId == id)[0]; 22 if (!shortcut) { 23 continue; 24 } 25 toolIDs.push(id); 26 toolShortcuts.push(shortcut); 27 28 // Enable disabled tools 29 const pref = definition.visibilityswitch; 30 if (pref) { 31 const prefValue = Services.prefs.getBoolPref(pref, false); 32 if (!prefValue) { 33 modifiedPrefs.push(pref); 34 Services.prefs.setBoolPref(pref, true); 35 } 36 } 37 } 38 const tab = gBrowser.selectedTab; 39 idIndex = 0; 40 gDevTools 41 .showToolboxForTab(tab, { 42 toolId: toolIDs[0], 43 hostType: Toolbox.HostType.WINDOW, 44 }) 45 .then(testShortcuts); 46 }); 47 } 48 49 function testShortcuts(toolbox, index) { 50 if (index === undefined) { 51 index = 1; 52 } else if (index == toolIDs.length) { 53 tidyUp(); 54 return; 55 } 56 57 gToolbox = toolbox; 58 info("Toolbox fired a `ready` event"); 59 60 gToolbox.once("select", selectCB); 61 62 const shortcut = toolShortcuts[index]; 63 const key = shortcut.shortcut; 64 const toolModifiers = shortcut.modifiers; 65 const modifiers = { 66 accelKey: toolModifiers.includes("accel"), 67 altKey: toolModifiers.includes("alt"), 68 shiftKey: toolModifiers.includes("shift"), 69 }; 70 idIndex = index; 71 info( 72 "Testing shortcut for tool " + 73 index + 74 ":" + 75 toolIDs[index] + 76 " using key " + 77 key 78 ); 79 EventUtils.synthesizeKey(key, modifiers, gToolbox.win.parent); 80 } 81 82 function selectCB(id) { 83 info("toolbox-select event from " + id); 84 85 is( 86 toolIDs.indexOf(id), 87 idIndex, 88 "Correct tool is selected on pressing the shortcut for " + id 89 ); 90 91 testShortcuts(gToolbox, idIndex + 1); 92 } 93 94 function tidyUp() { 95 gToolbox.destroy().then(function () { 96 gBrowser.removeCurrentTab(); 97 98 for (const pref of modifiedPrefs) { 99 Services.prefs.clearUserPref(pref); 100 } 101 gToolbox = toolIDs = idIndex = modifiedPrefs = Toolbox = null; 102 finish(); 103 }); 104 }