2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-06-21 23:03:41 +00:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
2017-02-16 18:58:12 +00:00
|
|
|
const {BrowserWindow, Menu, MenuItem} = remote
|
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu module', () => {
|
|
|
|
describe('Menu.buildFromTemplate', () => {
|
|
|
|
it('should be able to attach extra fields', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
extra: 'field'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].extra, 'field')
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not modify the specified template', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const template = ipcRenderer.sendSync('eval', "var template = [{label: 'text', submenu: [{label: 'sub'}]}];\nrequire('electron').Menu.buildFromTemplate(template);\ntemplate;")
|
2016-02-17 01:39:11 +00:00
|
|
|
assert.deepStrictEqual(template, [
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'sub'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not throw exceptions for undefined/null values', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-03-17 23:09:16 +00:00
|
|
|
Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
accelerator: undefined
|
2016-03-17 23:14:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'text again',
|
|
|
|
accelerator: null
|
2016-03-17 23:09:16 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
2016-03-17 23:09:16 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu.buildFromTemplate should reorder based on item position specifiers', () => {
|
|
|
|
it('should position before existing item', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: '2',
|
|
|
|
id: '2'
|
|
|
|
}, {
|
|
|
|
label: '3',
|
|
|
|
id: '3'
|
|
|
|
}, {
|
|
|
|
label: '1',
|
|
|
|
id: '1',
|
|
|
|
position: 'before=2'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].label, '1')
|
|
|
|
assert.equal(menu.items[1].label, '2')
|
|
|
|
assert.equal(menu.items[2].label, '3')
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should position after existing item', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: '1',
|
|
|
|
id: '1'
|
|
|
|
}, {
|
|
|
|
label: '3',
|
|
|
|
id: '3'
|
|
|
|
}, {
|
|
|
|
label: '2',
|
|
|
|
id: '2',
|
|
|
|
position: 'after=1'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].label, '1')
|
|
|
|
assert.equal(menu.items[1].label, '2')
|
|
|
|
assert.equal(menu.items[2].label, '3')
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should position at endof existing separator groups', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
type: 'separator',
|
|
|
|
id: 'numbers'
|
|
|
|
}, {
|
|
|
|
type: 'separator',
|
|
|
|
id: 'letters'
|
|
|
|
}, {
|
|
|
|
label: 'a',
|
|
|
|
id: 'a',
|
|
|
|
position: 'endof=letters'
|
|
|
|
}, {
|
|
|
|
label: '1',
|
|
|
|
id: '1',
|
|
|
|
position: 'endof=numbers'
|
|
|
|
}, {
|
|
|
|
label: 'b',
|
|
|
|
id: 'b',
|
|
|
|
position: 'endof=letters'
|
|
|
|
}, {
|
|
|
|
label: '2',
|
|
|
|
id: '2',
|
|
|
|
position: 'endof=numbers'
|
|
|
|
}, {
|
|
|
|
label: 'c',
|
|
|
|
id: 'c',
|
|
|
|
position: 'endof=letters'
|
|
|
|
}, {
|
|
|
|
label: '3',
|
|
|
|
id: '3',
|
|
|
|
position: 'endof=numbers'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].id, 'numbers')
|
|
|
|
assert.equal(menu.items[1].label, '1')
|
|
|
|
assert.equal(menu.items[2].label, '2')
|
|
|
|
assert.equal(menu.items[3].label, '3')
|
|
|
|
assert.equal(menu.items[4].id, 'letters')
|
|
|
|
assert.equal(menu.items[5].label, 'a')
|
|
|
|
assert.equal(menu.items[6].label, 'b')
|
|
|
|
assert.equal(menu.items[7].label, 'c')
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should create separator group if endof does not reference existing separator group', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: 'a',
|
|
|
|
id: 'a',
|
|
|
|
position: 'endof=letters'
|
|
|
|
}, {
|
|
|
|
label: '1',
|
|
|
|
id: '1',
|
|
|
|
position: 'endof=numbers'
|
|
|
|
}, {
|
|
|
|
label: 'b',
|
|
|
|
id: 'b',
|
|
|
|
position: 'endof=letters'
|
|
|
|
}, {
|
|
|
|
label: '2',
|
|
|
|
id: '2',
|
|
|
|
position: 'endof=numbers'
|
|
|
|
}, {
|
|
|
|
label: 'c',
|
|
|
|
id: 'c',
|
|
|
|
position: 'endof=letters'
|
|
|
|
}, {
|
|
|
|
label: '3',
|
|
|
|
id: '3',
|
|
|
|
position: 'endof=numbers'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].id, 'letters')
|
|
|
|
assert.equal(menu.items[1].label, 'a')
|
|
|
|
assert.equal(menu.items[2].label, 'b')
|
|
|
|
assert.equal(menu.items[3].label, 'c')
|
|
|
|
assert.equal(menu.items[4].id, 'numbers')
|
|
|
|
assert.equal(menu.items[5].label, '1')
|
|
|
|
assert.equal(menu.items[6].label, '2')
|
|
|
|
assert.equal(menu.items[7].label, '3')
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should continue inserting items at next index when no specifier is present', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: '4',
|
|
|
|
id: '4'
|
|
|
|
}, {
|
|
|
|
label: '5',
|
|
|
|
id: '5'
|
|
|
|
}, {
|
|
|
|
label: '1',
|
|
|
|
id: '1',
|
|
|
|
position: 'before=4'
|
|
|
|
}, {
|
|
|
|
label: '2',
|
|
|
|
id: '2'
|
|
|
|
}, {
|
|
|
|
label: '3',
|
|
|
|
id: '3'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].label, '1')
|
|
|
|
assert.equal(menu.items[1].label, '2')
|
|
|
|
assert.equal(menu.items[2].label, '3')
|
|
|
|
assert.equal(menu.items[3].label, '4')
|
|
|
|
assert.equal(menu.items[4].label, '5')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu.getMenuItemById', () => {
|
|
|
|
it('should return the item with the given id', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2017-09-27 00:05:51 +00:00
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Enter Fullscreen',
|
|
|
|
accelerator: 'Control+Command+F',
|
|
|
|
id: 'fullScreen'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Exit Fullscreen',
|
|
|
|
accelerator: 'Control+Command+F'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
])
|
|
|
|
const fsc = menu.getMenuItemById('fullScreen')
|
|
|
|
assert.equal(menu.items[0].submenu.items[0], fsc)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu.insert', () => {
|
|
|
|
it('should store item in @items by its index', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: '1'
|
|
|
|
}, {
|
|
|
|
label: '2'
|
|
|
|
}, {
|
|
|
|
label: '3'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
2017-10-25 02:40:31 +00:00
|
|
|
|
2017-10-25 00:27:26 +00:00
|
|
|
const item = new MenuItem({ label: 'inserted' })
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
menu.insert(1, item)
|
|
|
|
assert.equal(menu.items[0].label, '1')
|
|
|
|
assert.equal(menu.items[1].label, 'inserted')
|
|
|
|
assert.equal(menu.items[2].label, '2')
|
|
|
|
assert.equal(menu.items[3].label, '3')
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu.append', () => {
|
|
|
|
it('should add the item to the end of the menu', () => {
|
2017-10-25 02:40:31 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: '1'
|
|
|
|
}, {
|
|
|
|
label: '2'
|
|
|
|
}, {
|
|
|
|
label: '3'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
const item = new MenuItem({ label: 'inserted' })
|
|
|
|
|
|
|
|
menu.append(item)
|
|
|
|
assert.equal(menu.items[0].label, '1')
|
|
|
|
assert.equal(menu.items[1].label, '2')
|
|
|
|
assert.equal(menu.items[2].label, '3')
|
|
|
|
assert.equal(menu.items[3].label, 'inserted')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu.popup', () => {
|
2017-02-16 18:58:12 +00:00
|
|
|
let w = null
|
2017-10-25 00:27:26 +00:00
|
|
|
let menu
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
w = new BrowserWindow({show: false, width: 200, height: 200})
|
|
|
|
menu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: '1'
|
|
|
|
}, {
|
|
|
|
label: '2'
|
|
|
|
}, {
|
|
|
|
label: '3'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
})
|
2017-02-16 18:58:12 +00:00
|
|
|
|
2017-10-25 00:27:26 +00:00
|
|
|
afterEach(() => {
|
2017-10-27 20:45:58 +00:00
|
|
|
return closeWindow(w).then(() => { w = null })
|
2017-02-16 18:58:12 +00:00
|
|
|
})
|
|
|
|
|
2017-09-13 21:13:45 +00:00
|
|
|
it('returns immediately', () => {
|
2017-12-12 10:59:34 +00:00
|
|
|
menu.popup(w, {x: 100, y: 100, async: true})
|
2017-12-11 21:54:43 +00:00
|
|
|
menu.closePopup(w)
|
|
|
|
})
|
2017-02-16 18:58:12 +00:00
|
|
|
})
|
2017-10-25 02:40:31 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('Menu.setApplicationMenu', () => {
|
2017-11-07 21:29:37 +00:00
|
|
|
it('sets a menu', () => {
|
|
|
|
const menu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: '1'
|
|
|
|
}, {
|
|
|
|
label: '2'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
assert.notEqual(Menu.getApplicationMenu(), null)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('unsets a menu with null', () => {
|
|
|
|
Menu.setApplicationMenu(null)
|
|
|
|
assert.equal(Menu.getApplicationMenu(), null)
|
|
|
|
})
|
2017-10-25 02:40:31 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem.click', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
it('should be called with the item object passed', function (done) {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
2016-03-25 20:03:49 +00:00
|
|
|
click: function (item) {
|
|
|
|
assert.equal(item.constructor.name, 'MenuItem')
|
|
|
|
assert.equal(item.label, 'text')
|
|
|
|
done()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
2016-06-22 04:36:10 +00:00
|
|
|
menu.delegate.executeCommand({}, menu.items[0].commandId)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem with checked property', () => {
|
|
|
|
it('clicking an checkbox item should flip the checked property', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
type: 'checkbox'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
|
|
|
assert.equal(menu.items[0].checked, false)
|
2016-06-22 04:36:10 +00:00
|
|
|
menu.delegate.executeCommand({}, menu.items[0].commandId)
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[0].checked, true)
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('clicking an radio item should always make checked property true', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2016-01-12 02:40:23 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
type: 'radio'
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
])
|
2016-06-22 04:36:10 +00:00
|
|
|
menu.delegate.executeCommand({}, menu.items[0].commandId)
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[0].checked, true)
|
2016-06-22 04:36:10 +00:00
|
|
|
menu.delegate.executeCommand({}, menu.items[0].commandId)
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[0].checked, true)
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('at least have one item checked in each group', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const template = []
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
2016-01-12 02:40:23 +00:00
|
|
|
template.push({
|
2017-10-25 00:27:26 +00:00
|
|
|
label: `${i}`,
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'radio'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
template.push({type: 'separator'})
|
|
|
|
for (let i = 12; i <= 20; i++) {
|
2016-01-12 02:40:23 +00:00
|
|
|
template.push({
|
2017-10-25 00:27:26 +00:00
|
|
|
label: `${i}`,
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'radio'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate(template)
|
2016-03-25 20:03:49 +00:00
|
|
|
menu.delegate.menuWillShow()
|
|
|
|
assert.equal(menu.items[0].checked, true)
|
|
|
|
assert.equal(menu.items[12].checked, true)
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should assign groupId automatically', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const template = []
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
2016-01-12 02:40:23 +00:00
|
|
|
template.push({
|
2017-10-25 00:27:26 +00:00
|
|
|
label: `${i}`,
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'radio'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
template.push({type: 'separator'})
|
|
|
|
for (let i = 12; i <= 20; i++) {
|
2016-01-12 02:40:23 +00:00
|
|
|
template.push({
|
2017-10-25 00:27:26 +00:00
|
|
|
label: `${i}`,
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'radio'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
const groupId = menu.items[0].groupId
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].groupId, groupId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
for (let i = 12; i <= 20; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].groupId, groupId + 1)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it("setting 'checked' should flip other items' 'checked' property", () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const template = []
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
2016-01-12 02:40:23 +00:00
|
|
|
template.push({
|
2017-10-25 00:27:26 +00:00
|
|
|
label: `${i}`,
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'radio'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
template.push({type: 'separator'})
|
|
|
|
for (let i = 12; i <= 20; i++) {
|
2016-01-12 02:40:23 +00:00
|
|
|
template.push({
|
2017-10-25 00:27:26 +00:00
|
|
|
label: `${i}`,
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'radio'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
for (let i = 0; i <= 10; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
menu.items[0].checked = true
|
|
|
|
assert.equal(menu.items[0].checked, true)
|
2017-10-25 00:27:26 +00:00
|
|
|
for (let i = 1; i <= 10; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
menu.items[10].checked = true
|
|
|
|
assert.equal(menu.items[10].checked, true)
|
2017-10-25 00:27:26 +00:00
|
|
|
for (let i = 0; i <= 9; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-25 00:27:26 +00:00
|
|
|
for (let i = 12; i <= 20; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
menu.items[12].checked = true
|
|
|
|
assert.equal(menu.items[10].checked, true)
|
2017-10-25 00:27:26 +00:00
|
|
|
for (let i = 0; i <= 9; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[12].checked, true)
|
2017-10-25 00:27:26 +00:00
|
|
|
for (let i = 13; i <= 20; i++) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-06-21 22:59:02 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem command id', () => {
|
|
|
|
it('cannot be overwritten', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const item = new MenuItem({label: 'item'})
|
2016-06-22 17:06:54 +00:00
|
|
|
|
2017-10-25 00:27:26 +00:00
|
|
|
const commandId = item.commandId
|
|
|
|
assert(commandId)
|
|
|
|
item.commandId = `${commandId}-modified`
|
2016-06-22 17:06:54 +00:00
|
|
|
assert.equal(item.commandId, commandId)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem with invalid type', () => {
|
|
|
|
it('throws an exception', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
assert.throws(() => {
|
2016-06-22 17:06:54 +00:00
|
|
|
Menu.buildFromTemplate([
|
2016-06-21 22:59:02 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
type: 'not-a-type'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}, /Unknown menu item type: not-a-type/)
|
|
|
|
})
|
|
|
|
})
|
2016-06-21 23:02:15 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem with submenu type and missing submenu', () => {
|
|
|
|
it('throws an exception', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
assert.throws(() => {
|
2016-06-22 17:06:54 +00:00
|
|
|
Menu.buildFromTemplate([
|
2016-06-21 23:02:15 +00:00
|
|
|
{
|
|
|
|
label: 'text',
|
|
|
|
type: 'submenu'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}, /Invalid submenu/)
|
|
|
|
})
|
|
|
|
})
|
2016-06-22 21:07:46 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem role', () => {
|
|
|
|
it('includes a default label and accelerator', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
let item = new MenuItem({role: 'close'})
|
2016-07-26 00:27:45 +00:00
|
|
|
assert.equal(item.label, process.platform === 'darwin' ? 'Close Window' : 'Close')
|
2016-07-07 18:55:59 +00:00
|
|
|
assert.equal(item.accelerator, undefined)
|
|
|
|
assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+W')
|
2016-06-22 21:07:46 +00:00
|
|
|
|
2016-07-07 18:55:59 +00:00
|
|
|
item = new MenuItem({role: 'close', label: 'Other', accelerator: 'D'})
|
2016-06-22 21:07:46 +00:00
|
|
|
assert.equal(item.label, 'Other')
|
|
|
|
assert.equal(item.accelerator, 'D')
|
2016-07-07 18:55:59 +00:00
|
|
|
assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+W')
|
2016-06-22 21:07:46 +00:00
|
|
|
|
2016-06-29 16:37:10 +00:00
|
|
|
item = new MenuItem({role: 'help'})
|
2016-06-22 21:07:46 +00:00
|
|
|
assert.equal(item.label, 'Help')
|
|
|
|
assert.equal(item.accelerator, undefined)
|
2016-07-07 18:55:59 +00:00
|
|
|
assert.equal(item.getDefaultRoleAccelerator(), undefined)
|
2016-06-22 21:22:15 +00:00
|
|
|
|
2016-06-29 16:37:10 +00:00
|
|
|
item = new MenuItem({role: 'hide'})
|
2016-06-22 22:35:58 +00:00
|
|
|
assert.equal(item.label, 'Hide Electron Test')
|
2016-07-07 18:55:59 +00:00
|
|
|
assert.equal(item.accelerator, undefined)
|
|
|
|
assert.equal(item.getDefaultRoleAccelerator(), 'Command+H')
|
2016-09-16 16:26:07 +00:00
|
|
|
|
|
|
|
item = new MenuItem({role: 'undo'})
|
|
|
|
assert.equal(item.label, 'Undo')
|
|
|
|
assert.equal(item.accelerator, undefined)
|
|
|
|
assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+Z')
|
|
|
|
|
|
|
|
item = new MenuItem({role: 'redo'})
|
|
|
|
assert.equal(item.label, 'Redo')
|
|
|
|
assert.equal(item.accelerator, undefined)
|
|
|
|
assert.equal(item.getDefaultRoleAccelerator(), process.platform === 'win32' ? 'Control+Y' : 'Shift+CommandOrControl+Z')
|
2016-06-22 21:07:46 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-05 19:24:08 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem editMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const item = new MenuItem({role: 'editMenu'})
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.label, 'Edit')
|
|
|
|
assert.equal(item.submenu.items[0].role, 'undo')
|
|
|
|
assert.equal(item.submenu.items[1].role, 'redo')
|
|
|
|
assert.equal(item.submenu.items[2].type, 'separator')
|
|
|
|
assert.equal(item.submenu.items[3].role, 'cut')
|
|
|
|
assert.equal(item.submenu.items[4].role, 'copy')
|
|
|
|
assert.equal(item.submenu.items[5].role, 'paste')
|
2017-03-29 19:27:59 +00:00
|
|
|
|
2017-03-24 11:31:49 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.submenu.items[6].role, 'pasteandmatchstyle')
|
|
|
|
assert.equal(item.submenu.items[7].role, 'delete')
|
|
|
|
assert.equal(item.submenu.items[8].role, 'selectall')
|
|
|
|
}
|
2017-03-29 19:27:59 +00:00
|
|
|
|
2017-03-24 11:31:49 +00:00
|
|
|
if (process.platform === 'win32') {
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.submenu.items[6].role, 'delete')
|
|
|
|
assert.equal(item.submenu.items[7].type, 'separator')
|
|
|
|
assert.equal(item.submenu.items[8].role, 'selectall')
|
|
|
|
}
|
|
|
|
})
|
2017-03-29 19:27:59 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('overrides default layout when submenu is specified', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const item = new MenuItem({role: 'editMenu', submenu: [{role: 'close'}]})
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.label, 'Edit')
|
|
|
|
assert.equal(item.submenu.items[0].role, 'close')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem windowMenu', () => {
|
|
|
|
it('includes a default submenu layout when submenu is empty', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const item = new MenuItem({role: 'windowMenu'})
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.label, 'Window')
|
|
|
|
assert.equal(item.submenu.items[0].role, 'minimize')
|
|
|
|
assert.equal(item.submenu.items[1].role, 'close')
|
2017-03-29 19:27:59 +00:00
|
|
|
|
2017-03-24 11:31:49 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.submenu.items[2].type, 'separator')
|
|
|
|
assert.equal(item.submenu.items[3].role, 'front')
|
|
|
|
}
|
|
|
|
})
|
2017-03-29 19:27:59 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('overrides default layout when submenu is specified', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const item = new MenuItem({role: 'windowMenu', submenu: [{role: 'copy'}]})
|
2017-03-24 11:14:08 +00:00
|
|
|
assert.equal(item.label, 'Window')
|
|
|
|
assert.equal(item.submenu.items[0].role, 'copy')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('MenuItem with custom properties in constructor', () => {
|
|
|
|
it('preserves the custom properties', () => {
|
2017-10-25 00:27:26 +00:00
|
|
|
const template = [{
|
2017-02-16 17:14:31 +00:00
|
|
|
label: 'menu 1',
|
|
|
|
customProp: 'foo',
|
|
|
|
submenu: []
|
|
|
|
}]
|
2016-10-05 19:24:08 +00:00
|
|
|
|
2017-10-25 00:27:26 +00:00
|
|
|
const menu = Menu.buildFromTemplate(template)
|
2017-02-16 17:14:31 +00:00
|
|
|
menu.items[0].submenu.append(new MenuItem({
|
|
|
|
label: 'item 1',
|
|
|
|
customProp: 'bar',
|
|
|
|
overrideProperty: 'oops not allowed'
|
|
|
|
}))
|
2016-10-05 19:24:08 +00:00
|
|
|
|
2017-02-16 17:14:31 +00:00
|
|
|
assert.equal(menu.items[0].customProp, 'foo')
|
|
|
|
assert.equal(menu.items[0].submenu.items[0].label, 'item 1')
|
|
|
|
assert.equal(menu.items[0].submenu.items[0].customProp, 'bar')
|
|
|
|
assert.equal(typeof menu.items[0].submenu.items[0].overrideProperty, 'function')
|
|
|
|
})
|
2016-10-05 19:24:08 +00:00
|
|
|
})
|
|
|
|
})
|