electron/spec/api-menu-spec.js

699 lines
17 KiB
JavaScript
Raw Normal View History

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 {sortMenuItems} = require('../lib/browser/api/menu-utils')
2017-02-16 18:58:12 +00:00
const {closeWindow} = require('./window-helpers')
2016-01-12 02:40:23 +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')
})
it('does not modify the specified template', () => {
const template = [{label: 'text', submenu: [{label: 'sub'}]}]
const result = ipcRenderer.sendSync('eval', `const template = [{label: 'text', submenu: [{label: 'sub'}]}]\nrequire('electron').Menu.buildFromTemplate(template)\ntemplate`)
assert.deepStrictEqual(result, template)
2016-03-25 20:03:49 +00:00
})
it('does not throw exceptions for undefined/null values', () => {
assert.doesNotThrow(() => {
Menu.buildFromTemplate([
{
label: 'text',
accelerator: undefined
2016-03-17 23:14:31 +00:00
},
{
label: 'text again',
accelerator: null
}
2016-03-25 20:03:49 +00:00
])
})
})
describe('Menu sorting and building', () => {
describe('sorts groups', () => {
it('does a simple sort', () => {
const items = [
{
label: 'two',
id: '2',
afterGroupContaining: ['1'] },
{ type: 'separator' },
{
id: '1',
label: 'one'
}
]
const expected = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two',
afterGroupContaining: ['1']
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it('resolves cycles by ignoring things that conflict', () => {
const items = [
{
id: '2',
label: 'two',
afterGroupContaining: ['1']
},
{ type: 'separator' },
{
id: '1',
label: 'one',
afterGroupContaining: ['2']
}
]
const expected = [
{
id: '1',
label: 'one',
afterGroupContaining: ['2']
},
{ type: 'separator' },
{
id: '2',
label: 'two',
afterGroupContaining: ['1']
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it('ignores references to commands that do not exist', () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two',
afterGroupContaining: ['does-not-exist']
}
]
const expected = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two',
afterGroupContaining: ['does-not-exist']
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it('only respects the first matching [before|after]GroupContaining rule in a given group', () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '3',
label: 'three',
beforeGroupContaining: ['1']
},
{
id: '4',
label: 'four',
afterGroupContaining: ['2']
},
{ type: 'separator' },
{
id: '2',
label: 'two'
}
]
const expected = [
{
id: '3',
label: 'three',
beforeGroupContaining: ['1']
},
{
id: '4',
label: 'four',
afterGroupContaining: ['2']
},
{ type: 'separator' },
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
})
describe('moves an item to a different group by merging groups', () => {
it('can move a group of one item', () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
},
{ type: 'separator' },
{
id: '3',
label: 'three',
after: ['1']
},
{ type: 'separator' }
]
const expected = [
{
id: '1',
label: 'one'
},
{
id: '3',
label: 'three',
after: ['1']
},
{ type: 'separator' },
{
id: '2',
label: 'two'
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it("moves all items in the moving item's group", () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
},
{ type: 'separator' },
{
id: '3',
label: 'three',
after: ['1']
},
{
id: '4',
label: 'four'
},
{ type: 'separator' }
]
const expected = [
{
id: '1',
label: 'one'
},
{
id: '3',
label: 'three',
after: ['1']
},
{
id: '4',
label: 'four'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it("ignores positions relative to commands that don't exist", () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
},
{ type: 'separator' },
{
id: '3',
label: 'three',
after: ['does-not-exist']
},
{
id: '4',
label: 'four',
after: ['1']
},
{ type: 'separator' }
]
const expected = [
{
id: '1',
label: 'one'
},
{
id: '3',
label: 'three',
after: ['does-not-exist']
},
{
id: '4',
label: 'four',
after: ['1']
},
{ type: 'separator' },
{
id: '2',
label: 'two'
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it('can handle recursive group merging', () => {
const items = [
{
id: '1',
label: 'one',
after: ['3']
},
{
id: '2',
label: 'two',
before: ['1']
},
{
id: '3',
label: 'three'
}
]
const expected = [
{
id: '3',
label: 'three'
},
{
id: '2',
label: 'two',
before: ['1']
},
{
id: '1',
label: 'one',
after: ['3']
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it('can merge multiple groups when given a list of before/after commands', () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
},
{ type: 'separator' },
{
id: '3',
label: 'three',
after: ['1', '2']
}
]
const expected = [
{
id: '2',
label: 'two'
},
{
id: '1',
label: 'one'
},
{
id: '3',
label: 'three',
after: ['1', '2']
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
it('can merge multiple groups based on both before/after commands', () => {
const items = [
{
id: '1',
label: 'one'
},
{ type: 'separator' },
{
id: '2',
label: 'two'
},
{ type: 'separator' },
{
id: '3',
label: 'three',
after: ['1'],
before: ['2']
}
]
const expected = [
{
id: '1',
label: 'one'
},
{
id: '3',
label: 'three',
after: ['1'],
before: ['2']
},
{
id: '2',
label: 'two'
}
]
assert.deepEqual(sortMenuItems(items), expected)
})
})
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
{
id: '2',
label: 'two'
2016-01-12 02:40:23 +00:00
}, {
id: '3',
label: 'three'
2016-01-12 02:40:23 +00:00
}, {
id: '1',
label: 'one',
before: ['2']
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
])
assert.equal(menu.items[0].label, 'one')
assert.equal(menu.items[1].label, 'two')
assert.equal(menu.items[2].label, 'three')
2016-03-25 20:03:49 +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
{
id: '2',
label: 'two',
after: ['1']
},
2016-01-12 02:40:23 +00:00
{
id: '1',
label: 'one'
2016-01-12 02:40:23 +00:00
}, {
id: '3',
label: 'three'
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
])
assert.equal(menu.items[0].label, 'one')
assert.equal(menu.items[1].label, 'two')
assert.equal(menu.items[2].label, 'three')
})
it('should filter excess menu separators', () => {
const menuOne = Menu.buildFromTemplate([
{
type: 'separator'
}, {
label: 'a'
}, {
label: 'b'
}, {
label: 'c'
}, {
type: 'separator'
}
])
assert.equal(menuOne.items.length, 3)
assert.equal(menuOne.items[0].label, 'a')
assert.equal(menuOne.items[1].label, 'b')
assert.equal(menuOne.items[2].label, 'c')
const menuTwo = Menu.buildFromTemplate([
{
type: 'separator'
}, {
type: 'separator'
}, {
label: 'a'
}, {
label: 'b'
}, {
label: 'c'
}, {
type: 'separator'
}, {
type: 'separator'
}
])
assert.equal(menuTwo.items.length, 3)
assert.equal(menuTwo.items[0].label, 'a')
assert.equal(menuTwo.items[1].label, 'b')
assert.equal(menuTwo.items[2].label, 'c')
2016-03-25 20:03:49 +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
{
id: '2',
label: 'two'
2016-01-12 02:40:23 +00:00
}, {
id: '3',
label: 'three'
2016-01-12 02:40:23 +00:00
}, {
id: '4',
label: 'four'
2016-01-12 02:40:23 +00:00
}, {
id: '5',
label: 'five'
2016-01-12 02:40:23 +00:00
}, {
id: '1',
label: 'one',
before: ['2']
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
])
assert.equal(menu.items[0].label, 'one')
assert.equal(menu.items[1].label, 'two')
assert.equal(menu.items[2].label, 'three')
assert.equal(menu.items[3].label, 'four')
assert.equal(menu.items[4].label, 'five')
2016-03-25 20:03:49 +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: 'ControlCommandF',
2017-09-27 00:05:51 +00:00
id: 'fullScreen'
}
]
}
])
const fsc = menu.getMenuItemById('fullScreen')
assert.equal(menu.items[0].submenu.items[0], fsc)
})
})
describe('Menu.insert', () => {
it('should store item in @items by its index', () => {
2017-10-25 00:27:26 +00:00
const menu = Menu.buildFromTemplate([
{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')
})
})
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'}
2017-10-25 02:40:31 +00:00
])
const item = new MenuItem({ label: 'inserted' })
2017-10-25 02:40:31 +00:00
menu.append(item)
2017-10-25 02:40:31 +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, 'inserted')
})
})
2018-01-27 17:38:55 +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-10-25 00:27:26 +00:00
])
})
2017-02-16 18:58:12 +00:00
2017-10-25 00:27:26 +00:00
afterEach(() => {
menu.closePopup()
menu.closePopup(w)
return closeWindow(w).then(() => { w = null })
2017-02-16 18:58:12 +00:00
})
it('throws an error if options is not an object', () => {
assert.throws(() => {
menu.popup()
}, /Options must be an object/)
})
2018-01-27 16:23:46 +00:00
it('should emit menu-will-show event', (done) => {
menu.on('menu-will-show', () => { done() })
menu.popup({window: w})
2018-01-27 16:23:46 +00:00
})
it('should emit menu-will-close event', (done) => {
2018-01-27 17:36:51 +00:00
menu.on('menu-will-close', () => { done() })
menu.popup({window: w})
2018-01-27 16:23:46 +00:00
menu.closePopup()
})
it('returns immediately', () => {
const input = {window: w, x: 100, y: 101}
const output = menu.popup(input)
assert.equal(output.x, input.x)
assert.equal(output.y, input.y)
assert.equal(output.browserWindow, input.window)
})
2017-12-08 22:52:21 +00:00
it('works without a given BrowserWindow and options', () => {
const {browserWindow, x, y} = menu.popup({x: 100, y: 101})
assert.equal(browserWindow.constructor.name, 'BrowserWindow')
assert.equal(x, 100)
assert.equal(y, 101)
2017-12-08 22:52:21 +00:00
})
2018-02-20 01:59:47 +00:00
it('works with a given BrowserWindow, options and callback', (done) => {
2018-02-20 16:10:53 +00:00
const {x, y} = menu.popup({
window: w,
x: 100,
y: 101,
callback: () => done()
})
assert.equal(x, 100)
assert.equal(y, 101)
menu.closePopup()
})
it('works with a given BrowserWindow, no options, and a callback', (done) => {
2018-02-20 16:10:53 +00:00
menu.popup({window: w, callback: () => done()})
menu.closePopup()
})
2017-02-16 18:58:12 +00:00
})
2017-10-25 02:40:31 +00:00
describe('Menu.setApplicationMenu', () => {
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
})
})