2019-07-16 19:54:18 +00:00
|
|
|
import { BrowserWindow, app, Menu, MenuItem, MenuItemConstructorOptions } from 'electron'
|
|
|
|
const { roleList, execute } = require('../lib/browser/api/menu-item-roles')
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import { closeAllWindows } from './window-helpers';
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2018-06-30 07:25:28 +00:00
|
|
|
|
2018-02-23 13:53:59 +00:00
|
|
|
describe('MenuItems', () => {
|
2019-07-17 21:32:47 +00: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' }]
|
|
|
|
})
|
|
|
|
|
|
|
|
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 13:53:59 +00:00
|
|
|
describe('MenuItem.click', () => {
|
2018-06-30 07:25:28 +00:00
|
|
|
it('should be called with the item object passed', done => {
|
2018-02-23 13:53:59 +00:00
|
|
|
const menu = Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
click: (item) => {
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.constructor.name).to.equal('MenuItem')
|
|
|
|
expect(item.label).to.equal('text')
|
2018-02-23 13:53:59 +00:00
|
|
|
done()
|
|
|
|
}
|
2019-07-16 19:54:18 +00:00
|
|
|
}]);
|
2018-02-23 13:53:59 +00:00
|
|
|
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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 19:54:18 +00:00
|
|
|
expect(menu.items[0].checked).to.be.false('menu item checked')
|
2018-02-23 13:53:59 +00:00
|
|
|
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(menu.items[0].checked).to.be.true('menu item checked')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('clicking an radio item should always make checked property true', () => {
|
|
|
|
const menu = Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
type: 'radio'
|
|
|
|
}])
|
|
|
|
|
|
|
|
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(menu.items[0].checked).to.be.true('menu item checked')
|
2018-02-23 13:53:59 +00:00
|
|
|
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(menu.items[0].checked).to.be.true('menu item checked')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem group properties', () => {
|
2019-07-16 19:54:18 +00:00
|
|
|
const template: MenuItemConstructorOptions[] = []
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00:00
|
|
|
const findRadioGroups = (template: MenuItemConstructorOptions[]) => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const groups = []
|
2019-07-16 19:54:18 +00:00
|
|
|
let cur: { begin?: number, end?: number } | null = null
|
2018-02-23 13:53:59 +00: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 19:54:18 +00:00
|
|
|
const findChecked = (menuItems: MenuItem[], begin: number, end: number) => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const checked = []
|
2018-02-23 13:53:59 +00: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 16:10:51 +00:00
|
|
|
template.push({ type: 'separator' })
|
2018-02-23 13:53:59 +00: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)
|
|
|
|
menu.delegate.menuWillShow(menu)
|
|
|
|
|
|
|
|
const groups = findRadioGroups(template)
|
|
|
|
|
|
|
|
groups.forEach(g => {
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.begin])
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should assign groupId automatically', () => {
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const usedGroupIds = new Set()
|
2018-02-23 13:53:59 +00:00
|
|
|
const groups = findRadioGroups(template)
|
|
|
|
groups.forEach(g => {
|
2019-07-16 19:54:18 +00:00
|
|
|
const groupId = (menu.items[g.begin!] as any).groupId
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
// groupId should be previously unused
|
2019-07-16 19:54:18 +00:00
|
|
|
//expect(usedGroupIds.has(groupId)).to.be.false('group id present')
|
|
|
|
expect(usedGroupIds).not.to.contain(groupId)
|
2018-02-23 13:53:59 +00:00
|
|
|
usedGroupIds.add(groupId)
|
|
|
|
|
|
|
|
// everything in the group should have the same id
|
2019-07-16 19:54:18 +00:00
|
|
|
for (let i = g.begin!; i < g.end!; ++i) {
|
|
|
|
expect((menu.items[i] as any).groupId).to.equal(groupId)
|
2018-02-23 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it("setting 'checked' should flip other items' 'checked' property", () => {
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
|
|
|
|
const groups = findRadioGroups(template)
|
|
|
|
groups.forEach(g => {
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([])
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00:00
|
|
|
menu.items[g.begin!].checked = true
|
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.begin!])
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00:00
|
|
|
menu.items[g.end! - 1].checked = true
|
|
|
|
expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.end! - 1])
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem role execution', () => {
|
2019-07-16 19:54:18 +00:00
|
|
|
afterEach(closeAllWindows)
|
2018-02-23 13:53:59 +00:00
|
|
|
it('does not try to execute roles without a valid role property', () => {
|
2019-07-16 19:54:18 +00:00
|
|
|
const win = new BrowserWindow({ show: false, width: 200, height: 200 })
|
|
|
|
const item = new MenuItem({ role: 'asdfghjkl' as any })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00:00
|
|
|
const canExecute = execute(item.role, win, win.webContents)
|
|
|
|
expect(canExecute).to.be.false('can execute')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('executes roles with native role functions', () => {
|
2019-07-16 19:54:18 +00:00
|
|
|
const win = new BrowserWindow({ show: false, width: 200, height: 200 })
|
2018-10-02 01:56:31 +00:00
|
|
|
const item = new MenuItem({ role: 'reload' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00:00
|
|
|
const canExecute = execute(item.role, win, win.webContents)
|
|
|
|
expect(canExecute).to.be.true('can execute')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('execute roles with non-native role functions', () => {
|
2019-07-16 19:54:18 +00:00
|
|
|
const win = new BrowserWindow({ show: false, width: 200, height: 200 })
|
2019-07-31 21:13:56 +00:00
|
|
|
const item = new MenuItem({ role: 'resetZoom' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00:00
|
|
|
const canExecute = execute(item.role, win, win.webContents)
|
|
|
|
expect(canExecute).to.be.true('can execute')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem command id', () => {
|
|
|
|
it('cannot be overwritten', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const item = new MenuItem({ label: 'item' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
const commandId = item.commandId
|
2019-07-16 19:54:18 +00: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 07:25:28 +00:00
|
|
|
expect(item.commandId).to.equal(commandId)
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem with invalid type', () => {
|
|
|
|
it('throws an exception', () => {
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(() => {
|
2018-02-23 13:53:59 +00:00
|
|
|
Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
2019-07-16 19:54:18 +00:00
|
|
|
type: 'not-a-type' as any
|
2018-02-23 13:53:59 +00:00
|
|
|
}])
|
2018-06-30 07:25:28 +00:00
|
|
|
}).to.throw(/Unknown menu item type: not-a-type/)
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem with submenu type and missing submenu', () => {
|
|
|
|
it('throws an exception', () => {
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(() => {
|
2018-02-23 13:53:59 +00:00
|
|
|
Menu.buildFromTemplate([{
|
|
|
|
label: 'text',
|
|
|
|
type: 'submenu'
|
|
|
|
}])
|
2018-06-30 07:25:28 +00:00
|
|
|
}).to.throw(/Invalid submenu/)
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem role', () => {
|
|
|
|
it('returns undefined for items without default accelerator', () => {
|
2019-07-16 19:54:18 +00:00
|
|
|
const list = Object.keys(roleList).filter(key => !roleList[key].accelerator)
|
|
|
|
|
|
|
|
for (const role of list) {
|
|
|
|
const item = new MenuItem({ role: role as any })
|
|
|
|
expect(item.getDefaultRoleAccelerator()).to.be.undefined('default accelerator')
|
2018-02-23 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the correct default label', () => {
|
2019-07-17 21:32:47 +00:00
|
|
|
for (const role of Object.keys(roleList)) {
|
2019-07-16 19:54:18 +00:00
|
|
|
const item = new MenuItem({ role: role as any })
|
2019-07-17 21:32:47 +00:00
|
|
|
const label: string = roleList[role].label
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.label).to.equal(label)
|
2018-02-23 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the correct default accelerator', () => {
|
2019-07-17 21:32:47 +00:00
|
|
|
const list = Object.keys(roleList).filter(key => roleList[key].accelerator)
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-17 21:32:47 +00:00
|
|
|
for (const role of list) {
|
2019-07-16 19:54:18 +00:00
|
|
|
const item = new MenuItem({ role: role as any })
|
2019-07-17 21:32:47 +00:00
|
|
|
const accelerator: string = roleList[role].accelerator
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.getDefaultRoleAccelerator()).to.equal(accelerator)
|
2018-02-23 13:53:59 +00: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 07:25:28 +00:00
|
|
|
expect(item.label).to.equal('Custom Close!')
|
|
|
|
expect(item.accelerator).to.equal('D')
|
|
|
|
expect(item.getDefaultRoleAccelerator()).to.equal('CommandOrControl+W')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-01-10 13:32:03 +00:00
|
|
|
describe('MenuItem appMenu', () => {
|
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
|
|
|
const item = new MenuItem({ role: 'appMenu' })
|
|
|
|
|
2019-04-30 20:55:33 +00:00
|
|
|
expect(item.label).to.equal(app.name)
|
2019-07-16 19:54:18 +00: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 13:32:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'appMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'close'
|
|
|
|
}]
|
|
|
|
})
|
2019-04-30 20:55:33 +00:00
|
|
|
expect(item.label).to.equal(app.name)
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close')
|
2019-01-10 13:32:03 +00: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 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close')
|
2019-01-10 13:32:03 +00:00
|
|
|
} else {
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('quit')
|
2019-01-10 13:32:03 +00: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 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('about')
|
2019-01-10 13:32:03 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-02-23 13:53:59 +00:00
|
|
|
describe('MenuItem editMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const item = new MenuItem({ role: 'editMenu' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.label).to.equal('Edit')
|
2019-07-16 19:54:18 +00: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 13:53:59 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2019-07-16 19:54:18 +00: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')
|
|
|
|
expect(item.submenu!.items[10].label).to.equal('Speech')
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[0].role).to.equal('startspeaking')
|
|
|
|
expect(item.submenu!.items[10].submenu!.items[1].role).to.equal('stopspeaking')
|
2019-01-10 13:32:03 +00:00
|
|
|
} else {
|
2019-07-16 19:54:18 +00: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 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'editMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'close'
|
|
|
|
}]
|
|
|
|
})
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.label).to.equal('Edit')
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close')
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-01-10 13:32:03 +00:00
|
|
|
describe('MenuItem viewMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
|
|
|
const item = new MenuItem({ role: 'viewMenu' })
|
|
|
|
|
|
|
|
expect(item.label).to.equal('View')
|
2019-07-16 19:54:18 +00: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 13:32:03 +00: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 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('close')
|
2019-01-10 13:32:03 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-02-23 13:53:59 +00:00
|
|
|
describe('MenuItem windowMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const item = new MenuItem({ role: 'windowMenu' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.label).to.equal('Window')
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('minimize')
|
|
|
|
expect(item.submenu!.items[1].role).to.equal('zoom')
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[2].type).to.equal('separator')
|
|
|
|
expect(item.submenu!.items[3].role).to.equal('front')
|
2019-01-10 13:32:03 +00:00
|
|
|
} else {
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[2].role).to.equal('close')
|
2018-02-23 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'windowMenu',
|
2018-09-13 16:10:51 +00:00
|
|
|
submenu: [{ role: 'copy' }]
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.label).to.equal('Window')
|
2019-07-16 19:54:18 +00:00
|
|
|
expect(item.submenu!.items[0].role).to.equal('copy')
|
2018-02-23 13:53:59 +00: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 19:54:18 +00:00
|
|
|
menu.items[0].submenu!.append(new MenuItem({
|
2018-02-23 13:53:59 +00:00
|
|
|
label: 'item 1',
|
|
|
|
customProp: 'bar',
|
|
|
|
overrideProperty: 'oops not allowed'
|
2019-07-16 19:54:18 +00:00
|
|
|
} as any))
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-07-16 19:54:18 +00: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 13:53:59 +00:00
|
|
|
})
|
|
|
|
})
|
2018-10-31 14:13:44 +00:00
|
|
|
|
|
|
|
describe('MenuItem accelerators', () => {
|
|
|
|
const isDarwin = () => {
|
|
|
|
return (process.platform === 'darwin')
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:50:17 +00:00
|
|
|
it('should display modifiers correctly for simple keys', () => {
|
2018-10-31 14:13:44 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'CmdOrCtrl+A' },
|
|
|
|
{ label: 'text', accelerator: 'Shift+A' },
|
|
|
|
{ label: 'text', accelerator: 'Alt+A' }
|
|
|
|
])
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(menu.getAcceleratorTextAt(0)).to.equal(isDarwin() ? '⌘A' : 'Ctrl+A')
|
|
|
|
expect(menu.getAcceleratorTextAt(1)).to.equal(isDarwin() ? '⇧A' : 'Shift+A')
|
|
|
|
expect(menu.getAcceleratorTextAt(2)).to.equal(isDarwin() ? '⌥A' : 'Alt+A')
|
2018-10-31 14:13:44 +00:00
|
|
|
})
|
|
|
|
|
2018-11-02 14:50:17 +00:00
|
|
|
it('should display modifiers correctly for special keys', () => {
|
2018-10-31 14:13:44 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'CmdOrCtrl+Tab' },
|
|
|
|
{ label: 'text', accelerator: 'Shift+Tab' },
|
|
|
|
{ label: 'text', accelerator: 'Alt+Tab' }
|
|
|
|
])
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(menu.getAcceleratorTextAt(0)).to.equal(isDarwin() ? '⌘⇥\u0000' : 'Ctrl+Tab')
|
|
|
|
expect(menu.getAcceleratorTextAt(1)).to.equal(isDarwin() ? '⇧⇥\u0000' : 'Shift+Tab')
|
|
|
|
expect(menu.getAcceleratorTextAt(2)).to.equal(isDarwin() ? '⌥⇥\u0000' : 'Alt+Tab')
|
2018-10-31 14:13:44 +00:00
|
|
|
})
|
|
|
|
|
2018-11-02 14:50:17 +00:00
|
|
|
it('should not display modifiers twice', () => {
|
2018-10-31 14:13:44 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'Shift+Shift+A' },
|
|
|
|
{ label: 'text', accelerator: 'Shift+Shift+Tab' }
|
|
|
|
])
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(menu.getAcceleratorTextAt(0)).to.equal(isDarwin() ? '⇧A' : 'Shift+A')
|
|
|
|
expect(menu.getAcceleratorTextAt(1)).to.equal(isDarwin() ? '⇧⇥\u0000' : 'Shift+Tab')
|
2018-10-31 14:13:44 +00:00
|
|
|
})
|
|
|
|
|
2018-11-02 14:50:17 +00:00
|
|
|
it('should display correctly for edge cases', () => {
|
2018-10-31 14:13:44 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{ label: 'text', accelerator: 'Control+Shift+=' },
|
|
|
|
{ label: 'text', accelerator: 'Control+Plus' }
|
|
|
|
])
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(menu.getAcceleratorTextAt(0)).to.equal(isDarwin() ? '⌃⇧=' : 'Ctrl+Shift+=')
|
|
|
|
expect(menu.getAcceleratorTextAt(1)).to.equal(isDarwin() ? '⌃⇧=' : 'Ctrl+Shift+=')
|
2018-10-31 14:13:44 +00:00
|
|
|
})
|
|
|
|
})
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|