123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995 |
- import * as cp from 'child_process'
- import * as path from 'path'
- import { expect } from 'chai'
- import { BrowserWindow, globalShortcut, Menu, MenuItem } from 'electron'
- import { sortMenuItems } from '../lib/browser/api/menu-utils'
- import { emittedOnce } from './events-helpers'
- import { ifit } from './spec-helpers'
- import { closeWindow } from './window-helpers'
- const fixturesPath = path.resolve(__dirname, 'fixtures')
- describe('Menu module', function () {
- this.timeout(5000)
- describe('Menu.buildFromTemplate', () => {
- it('should be able to attach extra fields', () => {
- const menu = Menu.buildFromTemplate([
- {
- label: 'text',
- extra: 'field'
- } as MenuItem | Record<string, any>
- ])
- expect((menu.items[0] as any).extra).to.equal('field')
- })
- it('should be able to accept only MenuItems', () => {
- const menu = Menu.buildFromTemplate([
- new MenuItem({ label: 'one' }),
- new MenuItem({ label: 'two' })
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[1].label).to.equal('two')
- })
- it('should be able to accept only MenuItems in a submenu', () => {
- const menu = Menu.buildFromTemplate([
- {
- label: 'one',
- submenu: [
- new MenuItem({ label: 'two' }) as any
- ]
- }
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[0].submenu!.items[0].label).to.equal('two')
- })
- it('should be able to accept MenuItems and plain objects', () => {
- const menu = Menu.buildFromTemplate([
- new MenuItem({ label: 'one' }),
- { label: 'two' }
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[1].label).to.equal('two')
- })
- it('does not modify the specified template', () => {
- const template = [{ label: 'text', submenu: [{ label: 'sub' }] }]
- const templateCopy = JSON.parse(JSON.stringify(template))
- Menu.buildFromTemplate(template)
- expect(template).to.deep.equal(templateCopy)
- })
- it('does not throw exceptions for undefined/null values', () => {
- expect(() => {
- Menu.buildFromTemplate([
- {
- label: 'text',
- accelerator: undefined
- },
- {
- label: 'text again',
- accelerator: null as any
- }
- ])
- }).to.not.throw()
- })
- it('does throw exceptions for empty objects and null values', () => {
- expect(() => {
- Menu.buildFromTemplate([{}, null as any])
- }).to.throw(/Invalid template for MenuItem: must have at least one of label, role or type/)
- })
- it('does throw exception for object without role, label, or type attribute', () => {
- expect(() => {
- Menu.buildFromTemplate([{ 'visible': true }])
- }).to.throw(/Invalid template for MenuItem: must have at least one of label, role or type/)
- })
- it('does throw exception for undefined', () => {
- expect(() => {
- Menu.buildFromTemplate([undefined as any])
- }).to.throw(/Invalid template for MenuItem: must have at least one of label, role or type/)
- })
- it('throws when an non-array is passed as a template', () => {
- expect(() => {
- Menu.buildFromTemplate('hello' as any);
- }).to.throw(/Invalid template for Menu: Menu template must be an array/);
- });
- 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']
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(expected)
- })
- it('does a simple sort with MenuItems', () => {
- const firstItem = new MenuItem({ id: '1', label: 'one' })
- const secondItem = new MenuItem({
- label: 'two',
- id: '2',
- afterGroupContaining: ['1']
- })
- const sep = new MenuItem({ type: 'separator' })
- const items = [ secondItem, sep, firstItem ]
- const expected = [ firstItem, sep, secondItem ]
- expect(sortMenuItems(items)).to.deep.equal(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']
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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']
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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'
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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'
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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'
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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'
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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']
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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']
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(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'
- }
- ]
- expect(sortMenuItems(items)).to.deep.equal(expected)
- })
- })
- it('should position before existing item', () => {
- const menu = Menu.buildFromTemplate([
- {
- id: '2',
- label: 'two'
- }, {
- id: '3',
- label: 'three'
- }, {
- id: '1',
- label: 'one',
- before: ['2']
- }
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[1].label).to.equal('two')
- expect(menu.items[2].label).to.equal('three')
- })
- it('should position after existing item', () => {
- const menu = Menu.buildFromTemplate([
- {
- id: '2',
- label: 'two',
- after: ['1']
- },
- {
- id: '1',
- label: 'one'
- }, {
- id: '3',
- label: 'three'
- }
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[1].label).to.equal('two')
- expect(menu.items[2].label).to.equal('three')
- })
- it('should filter excess menu separators', () => {
- const menuOne = Menu.buildFromTemplate([
- {
- type: 'separator'
- }, {
- label: 'a'
- }, {
- label: 'b'
- }, {
- label: 'c'
- }, {
- type: 'separator'
- }
- ])
- expect(menuOne.items).to.have.length(3)
- expect(menuOne.items[0].label).to.equal('a')
- expect(menuOne.items[1].label).to.equal('b')
- expect(menuOne.items[2].label).to.equal('c')
- const menuTwo = Menu.buildFromTemplate([
- {
- type: 'separator'
- }, {
- type: 'separator'
- }, {
- label: 'a'
- }, {
- label: 'b'
- }, {
- label: 'c'
- }, {
- type: 'separator'
- }, {
- type: 'separator'
- }
- ])
- expect(menuTwo.items).to.have.length(3)
- expect(menuTwo.items[0].label).to.equal('a')
- expect(menuTwo.items[1].label).to.equal('b')
- expect(menuTwo.items[2].label).to.equal('c')
- })
- it('should continue inserting items at next index when no specifier is present', () => {
- const menu = Menu.buildFromTemplate([
- {
- id: '2',
- label: 'two'
- }, {
- id: '3',
- label: 'three'
- }, {
- id: '4',
- label: 'four'
- }, {
- id: '5',
- label: 'five'
- }, {
- id: '1',
- label: 'one',
- before: ['2']
- }
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[1].label).to.equal('two')
- expect(menu.items[2].label).to.equal('three')
- expect(menu.items[3].label).to.equal('four')
- expect(menu.items[4].label).to.equal('five')
- })
- it('should continue inserting MenuItems at next index when no specifier is present', () => {
- const menu = Menu.buildFromTemplate([
- new MenuItem({
- id: '2',
- label: 'two'
- }), new MenuItem({
- id: '3',
- label: 'three'
- }), new MenuItem({
- id: '4',
- label: 'four'
- }), new MenuItem({
- id: '5',
- label: 'five'
- }), new MenuItem({
- id: '1',
- label: 'one',
- before: ['2']
- })
- ])
- expect(menu.items[0].label).to.equal('one')
- expect(menu.items[1].label).to.equal('two')
- expect(menu.items[2].label).to.equal('three')
- expect(menu.items[3].label).to.equal('four')
- expect(menu.items[4].label).to.equal('five')
- })
- })
- })
- describe('Menu.getMenuItemById', () => {
- it('should return the item with the given id', () => {
- const menu = Menu.buildFromTemplate([
- {
- label: 'View',
- submenu: [
- {
- label: 'Enter Fullscreen',
- accelerator: 'ControlCommandF',
- id: 'fullScreen'
- }
- ]
- }
- ])
- const fsc = menu.getMenuItemById('fullScreen')
- expect(menu.items[0].submenu!.items[0]).to.equal(fsc)
- })
- it('should return the separator with the given id', () => {
- const menu = Menu.buildFromTemplate([
- {
- label: 'Item 1',
- id: 'item_1'
- },
- {
- id: 'separator',
- type: 'separator'
- },
- {
- label: 'Item 2',
- id: 'item_2'
- }
- ])
- const separator = menu.getMenuItemById('separator')
- expect(separator).to.be.an('object')
- expect(separator).to.equal(menu.items[1])
- })
- })
- describe('Menu.insert', () => {
- it('should throw when attempting to insert at out-of-range indices', () => {
- const menu = Menu.buildFromTemplate([
- { label: '1' },
- { label: '2' },
- { label: '3' }
- ])
- const item = new MenuItem({ label: 'badInsert' })
- expect(() => {
- menu.insert(9999, item)
- }).to.throw(/Position 9999 cannot be greater than the total MenuItem count/)
- expect(() => {
- menu.insert(-9999, item)
- }).to.throw(/Position -9999 cannot be less than 0/)
- })
- it('should store item in @items by its index', () => {
- const menu = Menu.buildFromTemplate([
- { label: '1' },
- { label: '2' },
- { label: '3' }
- ])
- const item = new MenuItem({ label: 'inserted' })
- menu.insert(1, item)
- expect(menu.items[0].label).to.equal('1')
- expect(menu.items[1].label).to.equal('inserted')
- expect(menu.items[2].label).to.equal('2')
- expect(menu.items[3].label).to.equal('3')
- })
- })
- describe('Menu.append', () => {
- it('should add the item to the end of the menu', () => {
- const menu = Menu.buildFromTemplate([
- { label: '1' },
- { label: '2' },
- { label: '3' }
- ])
- const item = new MenuItem({ label: 'inserted' })
- menu.append(item)
- expect(menu.items[0].label).to.equal('1')
- expect(menu.items[1].label).to.equal('2')
- expect(menu.items[2].label).to.equal('3')
- expect(menu.items[3].label).to.equal('inserted')
- })
- })
- describe('Menu.popup', () => {
- let w: BrowserWindow
- let menu: Menu
- beforeEach(() => {
- w = new BrowserWindow({ show: false, width: 200, height: 200 })
- menu = Menu.buildFromTemplate([
- { label: '1' },
- { label: '2' },
- { label: '3' }
- ])
- })
- afterEach(async () => {
- menu.closePopup()
- menu.closePopup(w)
- await closeWindow(w)
- w = null as unknown as BrowserWindow
- })
- it('throws an error if options is not an object', () => {
- expect(() => {
- menu.popup('this is a string, not an object' as any)
- }).to.throw(/Options must be an object/)
- })
- it('allows for options to be optional', () => {
- expect(() => {
- menu.popup({})
- }).to.not.throw()
- })
- it('should emit menu-will-show event', (done) => {
- menu.on('menu-will-show', () => { done() })
- menu.popup({ window: w })
- })
- it('should emit menu-will-close event', (done) => {
- menu.on('menu-will-close', () => { done() })
- menu.popup({ window: w })
- // https://github.com/electron/electron/issues/19411
- setTimeout(() => {
- menu.closePopup()
- })
- })
- it('returns immediately', () => {
- const input = { window: w, x: 100, y: 101 }
- const output = menu.popup(input) as unknown as {x: number, y: number, browserWindow: BrowserWindow}
- expect(output.x).to.equal(input.x)
- expect(output.y).to.equal(input.y)
- expect(output.browserWindow).to.equal(input.window)
- })
- it('works without a given BrowserWindow and options', () => {
- const { browserWindow, x, y } = menu.popup({ x: 100, y: 101 }) as unknown as {x: number, y: number, browserWindow: BrowserWindow}
- expect(browserWindow.constructor.name).to.equal('BrowserWindow')
- expect(x).to.equal(100)
- expect(y).to.equal(101)
- })
- it('works with a given BrowserWindow, options and callback', (done) => {
- const { x, y } = menu.popup({
- window: w,
- x: 100,
- y: 101,
- callback: () => done()
- }) as unknown as {x: number, y: number}
- expect(x).to.equal(100)
- expect(y).to.equal(101)
- // https://github.com/electron/electron/issues/19411
- setTimeout(() => {
- menu.closePopup()
- })
- })
- it('works with a given BrowserWindow, no options, and a callback', (done) => {
- menu.popup({ window: w, callback: () => done() })
- // https://github.com/electron/electron/issues/19411
- setTimeout(() => {
- menu.closePopup()
- })
- })
- it('prevents menu from getting garbage-collected when popuping', (done) => {
- let menu = Menu.buildFromTemplate([{role: 'paste'}])
- menu.popup({ window: w })
- // Keep a weak reference to the menu.
- const v8Util = process.electronBinding('v8_util')
- const map = (v8Util as any).createIDWeakMap() as any
- map.set(0, menu)
- setTimeout(() => {
- // Do garbage collection, since |menu| is not referenced in this closure
- // it would be gone after next call.
- v8Util.requestGarbageCollectionForTesting()
- setTimeout(() => {
- // Try to receive menu from weak reference.
- if (map.has(0)) {
- map.get(0).closePopup()
- done()
- } else {
- done('Menu is garbage-collected while popuping')
- }
- })
- })
- })
- })
- describe('Menu.setApplicationMenu', () => {
- it('sets a menu', () => {
- const menu = Menu.buildFromTemplate([
- { label: '1' },
- { label: '2' }
- ])
- Menu.setApplicationMenu(menu)
- expect(Menu.getApplicationMenu()).to.not.be.null('application menu')
- })
- it('unsets a menu with null', () => {
- Menu.setApplicationMenu(null)
- expect(Menu.getApplicationMenu()).to.be.null('application menu')
- })
- ifit(process.platform !== 'darwin')('does not override menu visibility on startup', async () => {
- const appPath = path.join(fixturesPath, 'api', 'test-menu-visibility')
- const appProcess = cp.spawn(process.execPath, [appPath])
- let output = ''
- appProcess.stdout.on('data', data => { output += data })
- await emittedOnce(appProcess, 'close')
- expect(output).to.include('Window has no menu')
- })
- ifit(process.platform !== 'darwin')('does not override null menu on startup', async () => {
- const appPath = path.join(fixturesPath, 'api', 'test-menu-null')
- const appProcess = cp.spawn(process.execPath, [appPath])
- let output = ''
- appProcess.stdout.on('data', data => { output += data })
- await emittedOnce(appProcess, 'close')
- expect(output).to.include('Window has no menu')
- })
- })
- describe('menu accelerators', async () => {
- const sendRobotjsKey = (key: string, modifiers: string | string[] = [], delay = 500) => {
- return new Promise((resolve, reject) => {
- try {
- require('robotjs').keyTap(key, modifiers)
- setTimeout(() => {
- resolve()
- }, delay)
- } catch (e) {
- reject(e)
- }
- })
- }
- before(async function () {
- // --ci flag breaks accelerator and robotjs interaction
- if (isCI) {
- this.skip()
- }
- // before accelerator tests, use globalShortcut to test if
- // RobotJS is working at all
- let isKeyPressed = false
- globalShortcut.register('q', () => {
- isKeyPressed = true
- })
- try {
- await sendRobotjsKey('q')
- } catch (e) {
- this.skip()
- }
- if (!isKeyPressed) {
- this.skip()
- }
- globalShortcut.unregister('q')
- })
- it('should perform the specified action', async () => {
- let hasBeenClicked = false
- const menu = Menu.buildFromTemplate([
- {
- label: 'Test',
- submenu: [
- {
- label: 'Test Item',
- accelerator: 'T',
- click: (a, b, event) => {
- hasBeenClicked = true
- expect(event).to.deep.equal({
- shiftKey: false,
- ctrlKey: false,
- altKey: false,
- metaKey: false,
- triggeredByAccelerator: true
- })
- },
- id: 'test'
- }
- ]
- }
- ])
- Menu.setApplicationMenu(menu)
- expect(Menu.getApplicationMenu()).to.not.be.null('application menu')
- await sendRobotjsKey('t')
- expect(hasBeenClicked).to.equal(true)
- })
- it('should not activate upon clicking another key combination', async () => {
- let hasBeenClicked = false
- const menu = Menu.buildFromTemplate([
- {
- label: 'Test',
- submenu: [
- {
- label: 'Test Item',
- accelerator: 'T',
- click: (a, b, event) => {
- hasBeenClicked = true
- },
- id: 'test'
- }
- ]
- }
- ])
- Menu.setApplicationMenu(menu)
- expect(Menu.getApplicationMenu()).to.not.be.null('application menu')
- await sendRobotjsKey('t', 'shift')
- expect(hasBeenClicked).to.equal(false)
- })
- })
- })
|