2020-04-07 03:04:09 +03:00
|
|
|
import { BrowserWindow, app, Menu, MenuItem, MenuItemConstructorOptions } from 'electron/main';
|
2019-07-16 22:54:18 +03:00
|
|
|
import { expect } from 'chai';
|
2023-04-04 16:48:51 +03:00
|
|
|
import { ifdescribe } from './lib/spec-helpers';
|
2023-01-26 00:01:25 +03:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2023-02-19 12:27:03 +03:00
|
|
|
import { roleList, execute } from '../lib/browser/api/menu-item-roles';
|
|
|
|
|
|
|
|
function keys<Key extends string, Value> (record: Record<Key, Value>) {
|
|
|
|
return Object.keys(record) as Key[];
|
|
|
|
}
|
2018-06-30 10:25:28 +03:00
|
|
|
|
2018-02-23 16:53:59 +03:00
|
|
|
describe('MenuItems', () => {
|
2019-07-18 00:32:47 +03:00
|
|
|
describe('MenuItem instance properties', () => {
|
|
|
|
it('should have default MenuItem properties', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
id: '1',
|
|
|
|
label: 'hello',
|
|
|
|
role: 'close',
|
|
|
|
sublabel: 'goodbye',
|
|
|
|
accelerator: 'CmdOrControl+Q',
|
|
|
|
click: () => { },
|
|
|
|
enabled: true,
|
|
|
|
visible: true,
|
|
|
|
checked: false,
|
|
|
|
type: 'normal',
|
|
|
|
registerAccelerator: true,
|
|
|
|
submenu: [{ role: 'about' }]
|
|
|
|
});
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2019-07-18 00:32:47 +03:00
|
|
|
expect(item).to.have.property('id').that.is.a('string');
|
|
|
|
expect(item).to.have.property('label').that.is.a('string').equal('hello');
|
|
|
|
expect(item).to.have.property('sublabel').that.is.a('string').equal('goodbye');
|
|
|
|
expect(item).to.have.property('accelerator').that.is.a('string').equal('CmdOrControl+Q');
|
|
|
|
expect(item).to.have.property('click').that.is.a('function');
|
|
|
|
expect(item).to.have.property('enabled').that.is.a('boolean').and.is.true('item is enabled');
|
|
|
|
expect(item).to.have.property('visible').that.is.a('boolean').and.is.true('item is visible');
|
|
|
|
expect(item).to.have.property('checked').that.is.a('boolean').and.is.false('item is not checked');
|
|
|
|
expect(item).to.have.property('registerAccelerator').that.is.a('boolean').and.is.true('item can register accelerator');
|
|
|
|
expect(item).to.have.property('type').that.is.a('string').equal('normal');
|
|
|
|
expect(item).to.have.property('commandId').that.is.a('number');
|
|
|
|
expect(item).to.have.property('toolTip').that.is.a('string');
|
|
|
|
expect(item).to.have.property('role').that.is.a('string');
|
|
|
|
expect(item).to.have.property('icon');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-23 16:53:59 +03:00
|
|
|
describe('MenuItem.click', () => {
|
2018-06-30 10:25:28 +03:00
|
|
|
it('should be called with the item object passed', done => {
|
2018-02-23 16:53:59 +03:00
|
|
|
const menu = Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
click: (item) => {
|
2020-07-01 01:10:36 +03:00
|
|
|
try {
|
|
|
|
expect(item.constructor.name).to.equal('MenuItem');
|
|
|
|
expect(item.label).to.equal('text');
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
2019-11-01 23:37:02 +03:00
|
|
|
}]);
|
2020-04-03 02:07:56 +03:00
|
|
|
menu._executeCommand({}, menu.items[0].commandId);
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem with checked/radio property', () => {
|
|
|
|
it('clicking an checkbox item should flip the checked property', () => {
|
|
|
|
const menu = Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
type: 'checkbox'
|
|
|
|
}]);
|
|
|
|
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(menu.items[0].checked).to.be.false('menu item checked');
|
2020-04-03 02:07:56 +03:00
|
|
|
menu._executeCommand({}, menu.items[0].commandId);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(menu.items[0].checked).to.be.true('menu item checked');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('clicking an radio item should always make checked property true', () => {
|
|
|
|
const menu = Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
type: 'radio'
|
|
|
|
}]);
|
|
|
|
|
2020-04-03 02:07:56 +03:00
|
|
|
menu._executeCommand({}, menu.items[0].commandId);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(menu.items[0].checked).to.be.true('menu item checked');
|
2020-04-03 02:07:56 +03:00
|
|
|
menu._executeCommand({}, menu.items[0].commandId);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(menu.items[0].checked).to.be.true('menu item checked');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem group properties', () => {
|
2019-07-16 22:54:18 +03:00
|
|
|
const template: MenuItemConstructorOptions[] = [];
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2019-07-16 22:54:18 +03:00
|
|
|
const findRadioGroups = (template: MenuItemConstructorOptions[]) => {
|
2018-10-02 04:56:31 +03:00
|
|
|
const groups = [];
|
2019-07-16 22:54:18 +03:00
|
|
|
let cur: { begin?: number, end?: number } | null = null;
|
2018-02-23 16:53:59 +03:00
|
|
|
for (let i = 0; i <= template.length; i++) {
|
|
|
|
if (cur && ((i === template.length) || (template[i].type !== 'radio'))) {
|
|
|
|
cur.end = i;
|
|
|
|
groups.push(cur);
|
|
|
|
cur = null;
|
|
|
|
} else if (!cur && i < template.length && template[i].type === 'radio') {
|
|
|
|
cur = { begin: i };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
};
|
|
|
|
|
|
|
|
// returns array of checked menuitems in [begin,end)
|
2019-07-16 22:54:18 +03:00
|
|
|
const findChecked = (menuItems: MenuItem[], begin: number, end: number) => {
|
2018-10-02 04:56:31 +03:00
|
|
|
const checked = [];
|
2018-02-23 16:53:59 +03:00
|
|
|
for (let i = begin; i < end; i++) {
|
|
|
|
if (menuItems[i].checked) checked.push(i);
|
|
|
|
}
|
|
|
|
return checked;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
|
|
|
template.push({
|
|
|
|
label: `${i}`,
|
|
|
|
type: 'radio'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:10:51 +03:00
|
|
|
template.push({ type: 'separator' });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
|
|
|
for (let i = 12; i <= 20; i++) {
|
|
|
|
template.push({
|
|
|
|
label: `${i}`,
|
|
|
|
type: 'radio'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('at least have one item checked in each group', () => {
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
2020-04-03 02:07:56 +03:00
|
|
|
menu._menuWillShow();
|
2018-02-23 16:53:59 +03:00
|
|
|
|
|
|
|
const groups = findRadioGroups(template);
|
|
|
|
|
2023-08-31 17:36:43 +03:00
|
|
|
for (const g of groups) {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.begin]);
|
2023-08-31 17:36:43 +03:00
|
|
|
}
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should assign groupId automatically', () => {
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
|
|
|
|
2018-10-02 04:56:31 +03:00
|
|
|
const usedGroupIds = new Set();
|
2018-02-23 16:53:59 +03:00
|
|
|
const groups = findRadioGroups(template);
|
2023-08-31 17:36:43 +03:00
|
|
|
for (const g of groups) {
|
2019-07-16 22:54:18 +03:00
|
|
|
const groupId = (menu.items[g.begin!] as any).groupId;
|
2018-02-23 16:53:59 +03:00
|
|
|
|
|
|
|
// groupId should be previously unused
|
2019-11-01 23:37:02 +03:00
|
|
|
// expect(usedGroupIds.has(groupId)).to.be.false('group id present')
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(usedGroupIds).not.to.contain(groupId);
|
2018-02-23 16:53:59 +03:00
|
|
|
usedGroupIds.add(groupId);
|
|
|
|
|
|
|
|
// everything in the group should have the same id
|
2019-07-16 22:54:18 +03:00
|
|
|
for (let i = g.begin!; i < g.end!; ++i) {
|
|
|
|
expect((menu.items[i] as any).groupId).to.equal(groupId);
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
2023-08-31 17:36:43 +03:00
|
|
|
}
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("setting 'checked' should flip other items' 'checked' property", () => {
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
|
|
|
|
|
|
|
const groups = findRadioGroups(template);
|
2023-08-31 17:36:43 +03:00
|
|
|
for (const g of groups) {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([]);
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2019-07-16 22:54:18 +03:00
|
|
|
menu.items[g.begin!].checked = true;
|
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.begin!]);
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2019-07-16 22:54:18 +03:00
|
|
|
menu.items[g.end! - 1].checked = true;
|
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.end! - 1]);
|
2023-08-31 17:36:43 +03:00
|
|
|
}
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem role execution', () => {
|
2019-07-16 22:54:18 +03:00
|
|
|
afterEach(closeAllWindows);
|
2018-02-23 16:53:59 +03:00
|
|
|
it('does not try to execute roles without a valid role property', () => {
|
2019-07-16 22:54:18 +03:00
|
|
|
const win = new BrowserWindow({ show: false, width: 200, height: 200 });
|
|
|
|
const item = new MenuItem({ role: 'asdfghjkl' as any });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2023-02-19 12:27:03 +03:00
|
|
|
const canExecute = execute(item.role as any, win, win.webContents);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(canExecute).to.be.false('can execute');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('executes roles with native role functions', () => {
|
2019-07-16 22:54:18 +03:00
|
|
|
const win = new BrowserWindow({ show: false, width: 200, height: 200 });
|
2018-10-02 04:56:31 +03:00
|
|
|
const item = new MenuItem({ role: 'reload' });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2023-02-19 12:27:03 +03:00
|
|
|
const canExecute = execute(item.role as any, win, win.webContents);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(canExecute).to.be.true('can execute');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('execute roles with non-native role functions', () => {
|
2019-07-16 22:54:18 +03:00
|
|
|
const win = new BrowserWindow({ show: false, width: 200, height: 200 });
|
2019-08-01 00:13:56 +03:00
|
|
|
const item = new MenuItem({ role: 'resetZoom' });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2023-02-19 12:27:03 +03:00
|
|
|
const canExecute = execute(item.role as any, win, win.webContents);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(canExecute).to.be.true('can execute');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem command id', () => {
|
|
|
|
it('cannot be overwritten', () => {
|
2018-09-13 19:10:51 +03:00
|
|
|
const item = new MenuItem({ label: 'item' });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
|
|
|
const commandId = item.commandId;
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(commandId).to.not.be.undefined('command id');
|
|
|
|
expect(() => {
|
|
|
|
item.commandId = `${commandId}-modified` as any;
|
|
|
|
}).to.throw(/Cannot assign to read only property/);
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(item.commandId).to.equal(commandId);
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem with invalid type', () => {
|
|
|
|
it('throws an exception', () => {
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(() => {
|
2018-02-23 16:53:59 +03:00
|
|
|
Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
2019-07-16 22:54:18 +03:00
|
|
|
type: 'not-a-type' as any
|
2018-02-23 16:53:59 +03:00
|
|
|
}]);
|
2018-06-30 10:25:28 +03:00
|
|
|
}).to.throw(/Unknown menu item type: not-a-type/);
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem with submenu type and missing submenu', () => {
|
|
|
|
it('throws an exception', () => {
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(() => {
|
2018-02-23 16:53:59 +03:00
|
|
|
Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
type: 'submenu'
|
|
|
|
}]);
|
2018-06-30 10:25:28 +03:00
|
|
|
}).to.throw(/Invalid submenu/);
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem role', () => {
|
|
|
|
it('returns undefined for items without default accelerator', () => {
|
2023-02-19 12:27:03 +03:00
|
|
|
const list = keys(roleList).filter(key => !roleList[key].accelerator);
|
2019-07-16 22:54:18 +03:00
|
|
|
|
|
|
|
for (const role of list) {
|
|
|
|
const item = new MenuItem({ role: role as any });
|
|
|
|
expect(item.getDefaultRoleAccelerator()).to.be.undefined('default accelerator');
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the correct default label', () => {
|
2023-02-19 12:27:03 +03:00
|
|
|
for (const role of keys(roleList)) {
|
2019-07-16 22:54:18 +03:00
|
|
|
const item = new MenuItem({ role: role as any });
|
2019-07-18 00:32:47 +03:00
|
|
|
const label: string = roleList[role].label;
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.label).to.equal(label);
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the correct default accelerator', () => {
|
2023-02-19 12:27:03 +03:00
|
|
|
const list = keys(roleList).filter(key => roleList[key].accelerator);
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2019-07-18 00:32:47 +03:00
|
|
|
for (const role of list) {
|
2019-07-16 22:54:18 +03:00
|
|
|
const item = new MenuItem({ role: role as any });
|
2023-02-19 12:27:03 +03:00
|
|
|
const accelerator = roleList[role].accelerator;
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.getDefaultRoleAccelerator()).to.equal(accelerator);
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows a custom accelerator and label to be set', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'close',
|
|
|
|
label: 'Custom Close!',
|
|
|
|
accelerator: 'D'
|
|
|
|
});
|
|
|
|
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(item.label).to.equal('Custom Close!');
|
|
|
|
expect(item.accelerator).to.equal('D');
|
|
|
|
expect(item.getDefaultRoleAccelerator()).to.equal('CommandOrControl+W');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-04 16:48:51 +03:00
|
|
|
ifdescribe(process.platform === 'darwin')('MenuItem appMenu', () => {
|
2019-01-10 16:32:03 +03:00
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
|
|
|
const item = new MenuItem({ role: 'appMenu' });
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2019-04-30 23:55:33 +03:00
|
|
|
expect(item.label).to.equal(app.name);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('about');
|
|
|
|
expect(item.submenu!.items[1].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[2].role).to.equal('services');
|
|
|
|
expect(item.submenu!.items[3].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[4].role).to.equal('hide');
|
|
|
|
expect(item.submenu!.items[5].role).to.equal('hideothers');
|
|
|
|
expect(item.submenu!.items[6].role).to.equal('unhide');
|
|
|
|
expect(item.submenu!.items[7].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[8].role).to.equal('quit');
|
2019-01-10 16:32:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'appMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'close'
|
|
|
|
}]
|
|
|
|
});
|
2019-04-30 23:55:33 +03:00
|
|
|
expect(item.label).to.equal(app.name);
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close');
|
2019-01-10 16:32:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem fileMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
|
|
|
const item = new MenuItem({ role: 'fileMenu' });
|
|
|
|
|
|
|
|
expect(item.label).to.equal('File');
|
|
|
|
if (process.platform === 'darwin') {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close');
|
2019-01-10 16:32:03 +03:00
|
|
|
} else {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('quit');
|
2019-01-10 16:32:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'fileMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'about'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
expect(item.label).to.equal('File');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('about');
|
2019-01-10 16:32:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-23 16:53:59 +03:00
|
|
|
describe('MenuItem editMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
2018-09-13 19:10:51 +03:00
|
|
|
const item = new MenuItem({ role: 'editMenu' });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(item.label).to.equal('Edit');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('undo');
|
|
|
|
expect(item.submenu!.items[1].role).to.equal('redo');
|
|
|
|
expect(item.submenu!.items[2].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[3].role).to.equal('cut');
|
|
|
|
expect(item.submenu!.items[4].role).to.equal('copy');
|
|
|
|
expect(item.submenu!.items[5].role).to.equal('paste');
|
2018-02-23 16:53:59 +03:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[6].role).to.equal('pasteandmatchstyle');
|
|
|
|
expect(item.submenu!.items[7].role).to.equal('delete');
|
|
|
|
expect(item.submenu!.items[8].role).to.equal('selectall');
|
|
|
|
expect(item.submenu!.items[9].type).to.equal('separator');
|
2022-01-06 21:41:28 +03:00
|
|
|
expect(item.submenu!.items[10].label).to.equal('Substitutions');
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[0].role).to.equal('showsubstitutions');
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[1].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[2].role).to.equal('togglesmartquotes');
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[3].role).to.equal('togglesmartdashes');
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[4].role).to.equal('toggletextreplacement');
|
|
|
|
expect(item.submenu!.items[11].label).to.equal('Speech');
|
|
|
|
expect(item.submenu!.items[11].submenu!.items[0].role).to.equal('startspeaking');
|
|
|
|
expect(item.submenu!.items[11].submenu!.items[1].role).to.equal('stopspeaking');
|
2019-01-10 16:32:03 +03:00
|
|
|
} else {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[6].role).to.equal('delete');
|
|
|
|
expect(item.submenu!.items[7].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[8].role).to.equal('selectall');
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'editMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'close'
|
|
|
|
}]
|
|
|
|
});
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(item.label).to.equal('Edit');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-01-10 16:32:03 +03:00
|
|
|
describe('MenuItem viewMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
|
|
|
const item = new MenuItem({ role: 'viewMenu' });
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2019-01-10 16:32:03 +03:00
|
|
|
expect(item.label).to.equal('View');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('reload');
|
|
|
|
expect(item.submenu!.items[1].role).to.equal('forcereload');
|
|
|
|
expect(item.submenu!.items[2].role).to.equal('toggledevtools');
|
|
|
|
expect(item.submenu!.items[3].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[4].role).to.equal('resetzoom');
|
|
|
|
expect(item.submenu!.items[5].role).to.equal('zoomin');
|
|
|
|
expect(item.submenu!.items[6].role).to.equal('zoomout');
|
|
|
|
expect(item.submenu!.items[7].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[8].role).to.equal('togglefullscreen');
|
2019-01-10 16:32:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'viewMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'close'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
expect(item.label).to.equal('View');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close');
|
2019-01-10 16:32:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-23 16:53:59 +03:00
|
|
|
describe('MenuItem windowMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
2018-09-13 19:10:51 +03:00
|
|
|
const item = new MenuItem({ role: 'windowMenu' });
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(item.label).to.equal('Window');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('minimize');
|
|
|
|
expect(item.submenu!.items[1].role).to.equal('zoom');
|
2018-02-23 16:53:59 +03:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[2].type).to.equal('separator');
|
|
|
|
expect(item.submenu!.items[3].role).to.equal('front');
|
2019-01-10 16:32:03 +03:00
|
|
|
} else {
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[2].role).to.equal('close');
|
2018-02-23 16:53:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'windowMenu',
|
2018-09-13 19:10:51 +03:00
|
|
|
submenu: [{ role: 'copy' }]
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
|
2018-06-30 10:25:28 +03:00
|
|
|
expect(item.label).to.equal('Window');
|
2019-07-16 22:54:18 +03:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('copy');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('MenuItem with custom properties in constructor', () => {
|
|
|
|
it('preserves the custom properties', () => {
|
|
|
|
const template = [{
|
|
|
|
label: 'menu 1',
|
|
|
|
customProp: 'foo',
|
|
|
|
submenu: []
|
|
|
|
}];
|
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
2019-07-16 22:54:18 +03:00
|
|
|
menu.items[0].submenu!.append(new MenuItem({
|
2018-02-23 16:53:59 +03:00
|
|
|
label: 'item 1',
|
|
|
|
customProp: 'bar',
|
|
|
|
overrideProperty: 'oops not allowed'
|
2019-07-16 22:54:18 +03:00
|
|
|
} as any));
|
2018-02-23 16:53:59 +03:00
|
|
|
|
2019-07-16 22:54:18 +03:00
|
|
|
expect((menu.items[0] as any).customProp).to.equal('foo');
|
|
|
|
expect(menu.items[0].submenu!.items[0].label).to.equal('item 1');
|
|
|
|
expect((menu.items[0].submenu!.items[0] as any).customProp).to.equal('bar');
|
|
|
|
expect((menu.items[0].submenu!.items[0] as any).overrideProperty).to.be.a('function');
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|
|
|
|
});
|
2018-10-31 17:13:44 +03:00
|
|
|
|
|
|
|
describe('MenuItem accelerators', () => {
|
|
|
|
const isDarwin = () => {
|
|
|
|
return (process.platform === 'darwin');
|
|
|
|
};
|
|
|
|
|
2018-11-02 17:50:17 +03:00
|
|
|
it('should display modifiers correctly for simple keys', () => {
|
2018-10-31 17:13:44 +03:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'CmdOrCtrl+A' },
|
|
|
|
{ label: 'text', accelerator: 'Shift+A' },
|
|
|
|
{ label: 'text', accelerator: 'Alt+A' }
|
|
|
|
]);
|
|
|
|
|
2021-09-27 17:58:03 +03:00
|
|
|
expect(menu._getAcceleratorTextAt(0)).to.equal(isDarwin() ? 'Command+A' : 'Ctrl+A');
|
|
|
|
expect(menu._getAcceleratorTextAt(1)).to.equal('Shift+A');
|
|
|
|
expect(menu._getAcceleratorTextAt(2)).to.equal('Alt+A');
|
2018-10-31 17:13:44 +03:00
|
|
|
});
|
|
|
|
|
2018-11-02 17:50:17 +03:00
|
|
|
it('should display modifiers correctly for special keys', () => {
|
2018-10-31 17:13:44 +03:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'CmdOrCtrl+Tab' },
|
|
|
|
{ label: 'text', accelerator: 'Shift+Tab' },
|
|
|
|
{ label: 'text', accelerator: 'Alt+Tab' }
|
|
|
|
]);
|
|
|
|
|
2021-09-27 17:58:03 +03:00
|
|
|
expect(menu._getAcceleratorTextAt(0)).to.equal(isDarwin() ? 'Command+Tab' : 'Ctrl+Tab');
|
|
|
|
expect(menu._getAcceleratorTextAt(1)).to.equal('Shift+Tab');
|
|
|
|
expect(menu._getAcceleratorTextAt(2)).to.equal('Alt+Tab');
|
2018-10-31 17:13:44 +03:00
|
|
|
});
|
|
|
|
|
2018-11-02 17:50:17 +03:00
|
|
|
it('should not display modifiers twice', () => {
|
2018-10-31 17:13:44 +03:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'Shift+Shift+A' },
|
|
|
|
{ label: 'text', accelerator: 'Shift+Shift+Tab' }
|
|
|
|
]);
|
|
|
|
|
2021-09-27 17:58:03 +03:00
|
|
|
expect(menu._getAcceleratorTextAt(0)).to.equal('Shift+A');
|
|
|
|
expect(menu._getAcceleratorTextAt(1)).to.equal('Shift+Tab');
|
2018-10-31 17:13:44 +03:00
|
|
|
});
|
|
|
|
|
2021-06-02 10:32:48 +03:00
|
|
|
it('should display correctly for shifted keys', () => {
|
2018-10-31 17:13:44 +03:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'Control+Shift+=' },
|
2021-06-02 10:32:48 +03:00
|
|
|
{ label: 'text', accelerator: 'Control+Plus' },
|
|
|
|
{ label: 'text', accelerator: 'Control+Shift+3' },
|
|
|
|
{ label: 'text', accelerator: 'Control+#' },
|
|
|
|
{ label: 'text', accelerator: 'Control+Shift+/' },
|
|
|
|
{ label: 'text', accelerator: 'Control+?' }
|
2018-10-31 17:13:44 +03:00
|
|
|
]);
|
|
|
|
|
2021-09-27 17:58:03 +03:00
|
|
|
expect(menu._getAcceleratorTextAt(0)).to.equal('Ctrl+Shift+=');
|
|
|
|
expect(menu._getAcceleratorTextAt(1)).to.equal('Ctrl++');
|
|
|
|
expect(menu._getAcceleratorTextAt(2)).to.equal('Ctrl+Shift+3');
|
|
|
|
expect(menu._getAcceleratorTextAt(3)).to.equal('Ctrl+#');
|
|
|
|
expect(menu._getAcceleratorTextAt(4)).to.equal('Ctrl+Shift+/');
|
|
|
|
expect(menu._getAcceleratorTextAt(5)).to.equal('Ctrl+?');
|
2018-10-31 17:13:44 +03:00
|
|
|
});
|
|
|
|
});
|
2018-02-23 16:53:59 +03:00
|
|
|
});
|