2018-06-30 07:25:28 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { remote } = require('electron')
|
|
|
|
const { BrowserWindow, app, Menu, MenuItem } = remote
|
2018-02-23 13:53:59 +00:00
|
|
|
const roles = require('../lib/browser/api/menu-item-roles')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
2018-02-23 13:53:59 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
const { expect } = chai
|
2018-06-30 07:25:28 +00:00
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2018-02-23 13:53:59 +00:00
|
|
|
describe('MenuItems', () => {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}])
|
|
|
|
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'
|
|
|
|
}])
|
|
|
|
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(menu.items[0].checked).to.be.false()
|
2018-02-23 13:53:59 +00:00
|
|
|
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(menu.items[0].checked).to.be.true()
|
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)
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(menu.items[0].checked).to.be.true()
|
2018-02-23 13:53:59 +00:00
|
|
|
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(menu.items[0].checked).to.be.true()
|
2018-02-23 13:53:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('MenuItem group properties', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const template = []
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
const findRadioGroups = (template) => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const groups = []
|
2018-02-23 13:53:59 +00:00
|
|
|
let cur = null
|
|
|
|
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)
|
|
|
|
const findChecked = (menuItems, begin, end) => {
|
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 => {
|
2018-06-30 07:25:28 +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 => {
|
|
|
|
const groupId = menu.items[g.begin].groupId
|
|
|
|
|
|
|
|
// groupId should be previously unused
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(usedGroupIds.has(groupId)).to.be.false()
|
2018-02-23 13:53:59 +00:00
|
|
|
usedGroupIds.add(groupId)
|
|
|
|
|
|
|
|
// everything in the group should have the same id
|
|
|
|
for (let i = g.begin; i < g.end; ++i) {
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(menu.items[i].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 => {
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(findChecked(menu.items, g.begin, g.end)).to.deep.equal([])
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
menu.items[g.begin].checked = true
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(findChecked(menu.items, g.begin, g.end)).to.deep.equal([g.begin])
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
menu.items[g.end - 1].checked = true
|
2018-06-30 07:25:28 +00:00
|
|
|
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', () => {
|
|
|
|
it('does not try to execute roles without a valid role property', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
let win = new BrowserWindow({ show: false, width: 200, height: 200 })
|
2018-10-02 01:56:31 +00:00
|
|
|
const item = new MenuItem({ role: 'asdfghjkl' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
const canExecute = roles.execute(item.role, win, win.webContents)
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(canExecute).to.be.false()
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
closeWindow(win).then(() => { win = null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('executes roles with native role functions', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
let 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
|
|
|
|
|
|
|
const canExecute = roles.execute(item.role, win, win.webContents)
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(canExecute).to.be.true()
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
closeWindow(win).then(() => { win = null })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('execute roles with non-native role functions', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
let win = new BrowserWindow({ show: false, width: 200, height: 200 })
|
2018-10-02 01:56:31 +00:00
|
|
|
const item = new MenuItem({ role: 'resetzoom' })
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
const canExecute = roles.execute(item.role, win, win.webContents)
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(canExecute).to.be.true()
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
closeWindow(win).then(() => { win = null })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(commandId).to.not.be.undefined()
|
2018-02-23 13:53:59 +00:00
|
|
|
item.commandId = `${commandId}-modified`
|
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',
|
|
|
|
type: 'not-a-type'
|
|
|
|
}])
|
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', () => {
|
|
|
|
const roleList = [
|
|
|
|
'close',
|
|
|
|
'copy',
|
|
|
|
'cut',
|
|
|
|
'forcereload',
|
|
|
|
'hide',
|
|
|
|
'hideothers',
|
|
|
|
'minimize',
|
|
|
|
'paste',
|
|
|
|
'pasteandmatchstyle',
|
|
|
|
'quit',
|
|
|
|
'redo',
|
|
|
|
'reload',
|
|
|
|
'resetzoom',
|
|
|
|
'selectall',
|
|
|
|
'toggledevtools',
|
|
|
|
'togglefullscreen',
|
|
|
|
'undo',
|
|
|
|
'zoomin',
|
|
|
|
'zoomout'
|
|
|
|
]
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const role in roleList) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const item = new MenuItem({ role })
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.getDefaultRoleAccelerator()).to.be.undefined()
|
2018-02-23 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the correct default label', () => {
|
|
|
|
const roleList = {
|
|
|
|
'close': process.platform === 'darwin' ? 'Close Window' : 'Close',
|
|
|
|
'copy': 'Copy',
|
|
|
|
'cut': 'Cut',
|
|
|
|
'forcereload': 'Force Reload',
|
|
|
|
'hide': 'Hide Electron Test',
|
|
|
|
'hideothers': 'Hide Others',
|
|
|
|
'minimize': 'Minimize',
|
|
|
|
'paste': 'Paste',
|
|
|
|
'pasteandmatchstyle': 'Paste and Match Style',
|
2019-04-30 20:55:33 +00:00
|
|
|
'quit': (process.platform === 'darwin') ? `Quit ${app.name}` : (process.platform === 'win32') ? 'Exit' : 'Quit',
|
2018-02-23 13:53:59 +00:00
|
|
|
'redo': 'Redo',
|
|
|
|
'reload': 'Reload',
|
|
|
|
'resetzoom': 'Actual Size',
|
|
|
|
'selectall': 'Select All',
|
|
|
|
'toggledevtools': 'Toggle Developer Tools',
|
|
|
|
'togglefullscreen': 'Toggle Full Screen',
|
|
|
|
'undo': 'Undo',
|
|
|
|
'zoomin': 'Zoom In',
|
|
|
|
'zoomout': 'Zoom Out'
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const role in roleList) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const item = new MenuItem({ role })
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.label).to.equal(roleList[role])
|
2018-02-23 13:53:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the correct default accelerator', () => {
|
|
|
|
const roleList = {
|
|
|
|
'close': 'CommandOrControl+W',
|
|
|
|
'copy': 'CommandOrControl+C',
|
|
|
|
'cut': 'CommandOrControl+X',
|
|
|
|
'forcereload': 'Shift+CmdOrCtrl+R',
|
|
|
|
'hide': 'Command+H',
|
|
|
|
'hideothers': 'Command+Alt+H',
|
|
|
|
'minimize': 'CommandOrControl+M',
|
|
|
|
'paste': 'CommandOrControl+V',
|
|
|
|
'pasteandmatchstyle': 'Shift+CommandOrControl+V',
|
|
|
|
'quit': process.platform === 'win32' ? null : 'CommandOrControl+Q',
|
|
|
|
'redo': process.platform === 'win32' ? 'Control+Y' : 'Shift+CommandOrControl+Z',
|
|
|
|
'reload': 'CmdOrCtrl+R',
|
|
|
|
'resetzoom': 'CommandOrControl+0',
|
|
|
|
'selectall': 'CommandOrControl+A',
|
|
|
|
'toggledevtools': process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
|
|
|
|
'togglefullscreen': process.platform === 'darwin' ? 'Control+Command+F' : 'F11',
|
|
|
|
'undo': 'CommandOrControl+Z',
|
|
|
|
'zoomin': 'CommandOrControl+Plus',
|
|
|
|
'zoomout': 'CommandOrControl+-'
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const role in roleList) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const item = new MenuItem({ role })
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(item.getDefaultRoleAccelerator()).to.equal(roleList[role])
|
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-01-10 13:32:03 +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')
|
|
|
|
})
|
|
|
|
|
|
|
|
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-01-10 13:32:03 +00:00
|
|
|
expect(item.submenu.items[0].role).to.equal('close')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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') {
|
|
|
|
expect(item.submenu.items[0].role).to.equal('close')
|
|
|
|
} else {
|
|
|
|
expect(item.submenu.items[0].role).to.equal('quit')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'fileMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'about'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
expect(item.label).to.equal('File')
|
|
|
|
expect(item.submenu.items[0].role).to.equal('about')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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')
|
|
|
|
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') {
|
2018-06-30 07:25:28 +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')
|
2019-01-10 13:32:03 +00:00
|
|
|
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')
|
|
|
|
} else {
|
2018-06-30 07:25:28 +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')
|
|
|
|
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')
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('overrides default layout when submenu is specified', () => {
|
|
|
|
const item = new MenuItem({
|
|
|
|
role: 'viewMenu',
|
|
|
|
submenu: [{
|
|
|
|
role: 'close'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
expect(item.label).to.equal('View')
|
|
|
|
expect(item.submenu.items[0].role).to.equal('close')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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')
|
|
|
|
expect(item.submenu.items[0].role).to.equal('minimize')
|
2018-12-03 19:52:46 +00:00
|
|
|
expect(item.submenu.items[1].role).to.equal('zoom')
|
2018-02-23 13:53:59 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2018-06-30 07:25:28 +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 {
|
|
|
|
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')
|
|
|
|
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)
|
|
|
|
menu.items[0].submenu.append(new MenuItem({
|
|
|
|
label: 'item 1',
|
|
|
|
customProp: 'bar',
|
|
|
|
overrideProperty: 'oops not allowed'
|
|
|
|
}))
|
|
|
|
|
2018-06-30 07:25:28 +00:00
|
|
|
expect(menu.items[0].customProp).to.equal('foo')
|
|
|
|
expect(menu.items[0].submenu.items[0].label).to.equal('item 1')
|
|
|
|
expect(menu.items[0].submenu.items[0].customProp).to.equal('bar')
|
|
|
|
expect(menu.items[0].submenu.items[0].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
|
|
|
})
|