From ac1165d7780dfbf96e294b48464ba401b0dcf778 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:48:30 +0100 Subject: [PATCH] test: exercise fuller `globalShortcut` matrix (#44627) test: test fuller globalShortcut matrix Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- spec/api-global-shortcut-spec.ts | 62 ++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/spec/api-global-shortcut-spec.ts b/spec/api-global-shortcut-spec.ts index 2025c151dda..a88d0864734 100644 --- a/spec/api-global-shortcut-spec.ts +++ b/spec/api-global-shortcut-spec.ts @@ -4,24 +4,66 @@ import { expect } from 'chai'; import { ifdescribe } from './lib/spec-helpers'; +const modifiers = [ + 'CmdOrCtrl', + 'Alt', + process.platform === 'darwin' ? 'Option' : null, + 'AltGr', + 'Shift', + 'Super', + 'Meta' +].filter(Boolean); + +const keyCodes = [ + ...Array.from({ length: 10 }, (_, i) => `${i}`), // 0 to 9 + ...Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i)), // A to Z + ...Array.from({ length: 24 }, (_, i) => `F${i + 1}`), // F1 to F24 + ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', ':', ';', ':', '+', '=', + '<', ',', '_', '-', '>', '.', '?', '/', '~', '`', '{', ']', '[', '|', '\\', + '}', '"', 'Plus', 'Space', 'Tab', 'Capslock', 'Numlock', 'Scrolllock', + 'Backspace', 'Delete', 'Insert', 'Return', 'Enter', 'Up', 'Down', 'Left', + 'Right', 'Home', 'End', 'PageUp', 'PageDown', 'Escape', 'Esc', 'PrintScreen', + 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8', 'num9', + 'numdec', 'numadd', 'numsub', 'nummult', 'numdiv' +]; + ifdescribe(process.platform !== 'win32')('globalShortcut module', () => { beforeEach(() => { globalShortcut.unregisterAll(); }); it('can register and unregister single accelerators', () => { - const accelerator = 'CmdOrCtrl+A+B+C'; + const singleModifierCombinations = modifiers.flatMap( + mod => keyCodes.map(key => { + return key === '+' ? `${mod}+Plus` : `${mod}+${key}`; + }) + ); - expect(globalShortcut.isRegistered(accelerator)).to.be.false('initially registered'); - globalShortcut.register(accelerator, () => {}); - expect(globalShortcut.isRegistered(accelerator)).to.be.true('registration worked'); - globalShortcut.unregister(accelerator); - expect(globalShortcut.isRegistered(accelerator)).to.be.false('unregistration worked'); + const doubleModifierCombinations = modifiers.flatMap( + (mod1, i) => modifiers.slice(i + 1).flatMap( + mod2 => keyCodes.map(key => { + return key === '+' ? `${mod1}+${mod2}+Plus` : `${mod1}+${mod2}+${key}`; + }) + ) + ); - globalShortcut.register(accelerator, () => {}); - expect(globalShortcut.isRegistered(accelerator)).to.be.true('reregistration worked'); - globalShortcut.unregisterAll(); - expect(globalShortcut.isRegistered(accelerator)).to.be.false('re-unregistration worked'); + const combinations = [...singleModifierCombinations, ...doubleModifierCombinations]; + + combinations.forEach((accelerator) => { + expect(globalShortcut.isRegistered(accelerator)).to.be.false(`Initially registered for ${accelerator}`); + + globalShortcut.register(accelerator, () => { }); + expect(globalShortcut.isRegistered(accelerator)).to.be.true(`Registration failed for ${accelerator}`); + + globalShortcut.unregister(accelerator); + expect(globalShortcut.isRegistered(accelerator)).to.be.false(`Unregistration failed for ${accelerator}`); + + globalShortcut.register(accelerator, () => { }); + expect(globalShortcut.isRegistered(accelerator)).to.be.true(`Re-registration failed for ${accelerator}`); + + globalShortcut.unregisterAll(); + expect(globalShortcut.isRegistered(accelerator)).to.be.false(`Re-unregistration failed for ${accelerator}`); + }); }); it('can register and unregister multiple accelerators', () => {