2020-03-20 13:28:31 -07:00
|
|
|
import { expect } from 'chai';
|
2020-04-06 17:04:09 -07:00
|
|
|
import { globalShortcut } from 'electron/main';
|
2020-03-20 13:28:31 -07:00
|
|
|
import { ifdescribe } from './spec-helpers';
|
2018-06-18 09:50:37 -07:00
|
|
|
|
2019-10-30 16:38:21 -07:00
|
|
|
ifdescribe(process.platform !== 'win32')('globalShortcut module', () => {
|
2016-08-09 15:10:51 -07:00
|
|
|
beforeEach(() => {
|
2020-03-20 13:28:31 -07:00
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
});
|
2016-08-09 15:10:51 -07:00
|
|
|
|
2018-11-07 09:40:38 -08:00
|
|
|
it('can register and unregister single accelerators', () => {
|
2020-03-20 13:28:31 -07:00
|
|
|
const accelerator = 'CmdOrCtrl+A+B+C';
|
2016-08-09 15:10:51 -07:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
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');
|
2016-08-09 15:10:51 -07:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
globalShortcut.register(accelerator, () => {});
|
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.true('reregistration worked');
|
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.false('re-unregistration worked');
|
|
|
|
});
|
2018-11-07 09:40:38 -08:00
|
|
|
|
|
|
|
it('can register and unregister multiple accelerators', () => {
|
2020-03-20 13:28:31 -07:00
|
|
|
const accelerators = ['CmdOrCtrl+X', 'CmdOrCtrl+Y'];
|
2018-11-07 09:40:38 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first initially unregistered');
|
|
|
|
expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second initially unregistered');
|
2018-11-07 09:40:38 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
globalShortcut.registerAll(accelerators, () => {});
|
2018-11-07 09:40:38 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
expect(globalShortcut.isRegistered(accelerators[0])).to.be.true('first registration worked');
|
|
|
|
expect(globalShortcut.isRegistered(accelerators[1])).to.be.true('second registration worked');
|
2018-11-07 09:40:38 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
globalShortcut.unregisterAll();
|
2018-11-07 09:40:38 -08:00
|
|
|
|
2020-03-20 13:28:31 -07:00
|
|
|
expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first unregistered');
|
|
|
|
expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second unregistered');
|
|
|
|
});
|
2020-05-28 05:56:48 -07:00
|
|
|
|
|
|
|
it('does not crash when registering media keys as global shortcuts', () => {
|
|
|
|
const accelerators = [
|
|
|
|
'VolumeUp',
|
|
|
|
'VolumeDown',
|
|
|
|
'VolumeMute',
|
|
|
|
'MediaNextTrack',
|
|
|
|
'MediaPreviousTrack',
|
|
|
|
'MediaStop', 'MediaPlayPause'
|
|
|
|
];
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
globalShortcut.registerAll(accelerators, () => {});
|
|
|
|
}).to.not.throw();
|
|
|
|
|
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
});
|
2020-03-20 13:28:31 -07:00
|
|
|
});
|