2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
const remote = require('electron').remote
|
|
|
|
const ipcRenderer = require('electron').ipcRenderer
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
const Menu = remote.require('electron').Menu
|
|
|
|
const MenuItem = remote.require('electron').MenuItem
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('menu module', function () {
|
|
|
|
describe('Menu.buildFromTemplate', function () {
|
|
|
|
it('should be able to attach extra fields', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('does not modify the specified template', function () {
|
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('does not throw exceptions for undefined/null values', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('Menu.buildFromTemplate should reorder based on item position specifiers', function () {
|
|
|
|
it('should position before existing item', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('should position after existing item', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('should position at endof existing separator groups', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('should create separator group if endof does not reference existing separator group', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('should continue inserting items at next index when no specifier is present', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('Menu.insert', function () {
|
|
|
|
it('should store item in @items by its index', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
])
|
2016-02-17 01:09:41 +00:00
|
|
|
var item = new MenuItem({
|
2016-01-12 02:40:23 +00:00
|
|
|
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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('MenuItem.click', function () {
|
|
|
|
it('should be called with the item object passed', function (done) {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('MenuItem with checked property', function () {
|
|
|
|
it('clicking an checkbox item should flip the checked property', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('clicking an radio item should always make checked property true', function () {
|
2016-02-17 01:09:41 +00:00
|
|
|
var 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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('at least have one item checked in each group', function () {
|
|
|
|
var i, j, k, menu, template
|
|
|
|
template = []
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = j = 0; j <= 10; i = ++j) {
|
|
|
|
template.push({
|
2016-03-25 20:03:49 +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
|
|
|
}
|
|
|
|
template.push({
|
|
|
|
type: 'separator'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = k = 12; k <= 20; i = ++k) {
|
|
|
|
template.push({
|
2016-03-25 20:03:49 +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
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
menu = Menu.buildFromTemplate(template)
|
|
|
|
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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('should assign groupId automatically', function () {
|
|
|
|
var groupId, i, j, k, l, m, menu, template
|
|
|
|
template = []
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = j = 0; j <= 10; i = ++j) {
|
|
|
|
template.push({
|
2016-03-25 20:03:49 +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
|
|
|
}
|
|
|
|
template.push({
|
|
|
|
type: 'separator'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = k = 12; k <= 20; i = ++k) {
|
|
|
|
template.push({
|
2016-03-25 20:03:49 +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
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
menu = Menu.buildFromTemplate(template)
|
|
|
|
groupId = menu.items[0].groupId
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = l = 0; l <= 10; i = ++l) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].groupId, groupId)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
for (i = m = 12; m <= 20; i = ++m) {
|
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
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it("setting 'checked' should flip other items' 'checked' property", function () {
|
|
|
|
var i, j, k, l, m, menu, n, o, p, q, template
|
|
|
|
template = []
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = j = 0; j <= 10; i = ++j) {
|
|
|
|
template.push({
|
2016-03-25 20:03:49 +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
|
|
|
}
|
|
|
|
template.push({
|
|
|
|
type: 'separator'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = k = 12; k <= 20; i = ++k) {
|
|
|
|
template.push({
|
2016-03-25 20:03:49 +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
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
menu = Menu.buildFromTemplate(template)
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = l = 0; l <= 10; i = ++l) {
|
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)
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = m = 1; m <= 10; i = ++m) {
|
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)
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = n = 0; n <= 9; i = ++n) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(menu.items[i].checked, false)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
for (i = o = 12; o <= 20; i = ++o) {
|
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)
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = p = 0; p <= 9; i = ++p) {
|
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)
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = q = 13; q <= 20; i = ++q) {
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|