2019-07-15 11:29:29 +03:00
|
|
|
import { expect } from 'chai';
|
2021-08-17 20:44:17 +03:00
|
|
|
import { globalShortcut, BrowserWindow } from 'electron/main';
|
2019-10-31 02:38:21 +03:00
|
|
|
import { ifdescribe } from './spec-helpers';
|
2018-06-18 19:50:37 +03:00
|
|
|
|
2019-10-31 02:38:21 +03:00
|
|
|
ifdescribe(process.platform !== 'win32')('globalShortcut module', () => {
|
2016-08-10 01:10:51 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
});
|
|
|
|
|
2018-11-07 20:40:38 +03:00
|
|
|
it('can register and unregister single accelerators', () => {
|
|
|
|
const accelerator = 'CmdOrCtrl+A+B+C';
|
2016-08-10 01:10:51 +03:00
|
|
|
|
2019-07-15 11:29:29 +03:00
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.false('initially registered');
|
2016-08-10 01:10:51 +03:00
|
|
|
globalShortcut.register(accelerator, () => {});
|
2019-07-15 11:29:29 +03:00
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.true('registration worked');
|
2016-08-10 01:10:51 +03:00
|
|
|
globalShortcut.unregister(accelerator);
|
2019-07-15 11:29:29 +03:00
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.false('unregistration worked');
|
2016-08-10 01:10:51 +03:00
|
|
|
|
|
|
|
globalShortcut.register(accelerator, () => {});
|
2019-07-15 11:29:29 +03:00
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.true('reregistration worked');
|
2016-08-10 01:10:51 +03:00
|
|
|
globalShortcut.unregisterAll();
|
2019-07-15 11:29:29 +03:00
|
|
|
expect(globalShortcut.isRegistered(accelerator)).to.be.false('re-unregistration worked');
|
2016-08-10 01:10:51 +03:00
|
|
|
});
|
2018-11-07 20:40:38 +03:00
|
|
|
|
|
|
|
it('can register and unregister multiple accelerators', () => {
|
|
|
|
const accelerators = ['CmdOrCtrl+X', 'CmdOrCtrl+Y'];
|
|
|
|
|
2019-07-15 11:29:29 +03: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 20:40:38 +03:00
|
|
|
|
|
|
|
globalShortcut.registerAll(accelerators, () => {});
|
|
|
|
|
2019-07-15 11:29:29 +03: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 20:40:38 +03:00
|
|
|
|
|
|
|
globalShortcut.unregisterAll();
|
|
|
|
|
2019-07-15 11:29:29 +03:00
|
|
|
expect(globalShortcut.isRegistered(accelerators[0])).to.be.false('first unregistered');
|
|
|
|
expect(globalShortcut.isRegistered(accelerators[1])).to.be.false('second unregistered');
|
2018-11-07 20:40:38 +03:00
|
|
|
});
|
2020-05-28 15:56:48 +03: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();
|
|
|
|
});
|
2021-08-17 20:44:17 +03:00
|
|
|
|
|
|
|
it('successfully registers and calls the callback for media keys', function (done) {
|
|
|
|
let robotjs;
|
|
|
|
try {
|
|
|
|
robotjs = require('robotjs');
|
|
|
|
} catch (err) {
|
|
|
|
this.skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
globalShortcut.register('MediaPlayPause', () => done());
|
|
|
|
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
|
|
|
|
robotjs.keyTap('audio_play');
|
|
|
|
});
|
2016-08-10 01:10:51 +03:00
|
|
|
});
|