api-global-shortcut-spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { expect } from 'chai';
  2. import { globalShortcut } from 'electron/main';
  3. import { ifdescribe } from './lib/spec-helpers';
  4. ifdescribe(process.platform !== 'win32')('globalShortcut module', () => {
  5. beforeEach(() => {
  6. globalShortcut.unregisterAll();
  7. });
  8. it('can register and unregister single accelerators', () => {
  9. const accelerator = 'CmdOrCtrl+A+B+C';
  10. expect(globalShortcut.isRegistered(accelerator)).to.be.false('initially registered');
  11. globalShortcut.register(accelerator, () => {});
  12. expect(globalShortcut.isRegistered(accelerator)).to.be.true('registration worked');
  13. globalShortcut.unregister(accelerator);
  14. expect(globalShortcut.isRegistered(accelerator)).to.be.false('unregistration worked');
  15. globalShortcut.register(accelerator, () => {});
  16. expect(globalShortcut.isRegistered(accelerator)).to.be.true('reregistration worked');
  17. globalShortcut.unregisterAll();
  18. expect(globalShortcut.isRegistered(accelerator)).to.be.false('re-unregistration worked');
  19. });
  20. it('can register and unregister multiple accelerators', () => {
  21. const accelerators = ['CmdOrCtrl+X', 'CmdOrCtrl+Y'];
  22. expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first initially unregistered');
  23. expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second initially unregistered');
  24. globalShortcut.registerAll(accelerators, () => {});
  25. expect(globalShortcut.isRegistered(accelerators[0])).to.be.true('first registration worked');
  26. expect(globalShortcut.isRegistered(accelerators[1])).to.be.true('second registration worked');
  27. globalShortcut.unregisterAll();
  28. expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first unregistered');
  29. expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second unregistered');
  30. });
  31. it('does not crash when registering media keys as global shortcuts', () => {
  32. const accelerators = [
  33. 'VolumeUp',
  34. 'VolumeDown',
  35. 'VolumeMute',
  36. 'MediaNextTrack',
  37. 'MediaPreviousTrack',
  38. 'MediaStop', 'MediaPlayPause'
  39. ];
  40. expect(() => {
  41. globalShortcut.registerAll(accelerators, () => {});
  42. }).to.not.throw();
  43. globalShortcut.unregisterAll();
  44. });
  45. });