feat: use default-app behavior in packaged apps (#16310)

Unify the behavior between default app and packaged apps:
- create default application menu unless the app has one
- default window-all-closed handling unless the app handles the event
This commit is contained in:
Milan Burda
2019-01-15 21:35:53 +01:00
committed by Alexey Kuzmin
parent 8e2ab8b20b
commit 23d44e322d
11 changed files with 133 additions and 37 deletions

View File

@@ -1179,12 +1179,12 @@ describe('app module', () => {
describe('commandLine.hasSwitch (existing argv)', () => {
it('returns true when present', async () => {
const { hasSwitch } = await runCommandLineTestApp('--foobar')
const { hasSwitch } = await runTestApp('command-line', '--foobar')
expect(hasSwitch).to.be.true()
})
it('returns false when not present', async () => {
const { hasSwitch } = await runCommandLineTestApp()
const { hasSwitch } = await runTestApp('command-line')
expect(hasSwitch).to.be.false()
})
})
@@ -1207,31 +1207,62 @@ describe('app module', () => {
describe('commandLine.getSwitchValue (existing argv)', () => {
it('returns the value when present', async () => {
const { getSwitchValue } = await runCommandLineTestApp('--foobar=test')
const { getSwitchValue } = await runTestApp('command-line', '--foobar=test')
expect(getSwitchValue).to.equal('test')
})
it('returns an empty string when present without value', async () => {
const { getSwitchValue } = await runCommandLineTestApp('--foobar')
const { getSwitchValue } = await runTestApp('command-line', '--foobar')
expect(getSwitchValue).to.equal('')
})
it('returns an empty string when not present', async () => {
const { getSwitchValue } = await runCommandLineTestApp()
const { getSwitchValue } = await runTestApp('command-line')
expect(getSwitchValue).to.equal('')
})
})
async function runCommandLineTestApp (...args) {
const appPath = path.join(__dirname, 'fixtures', 'api', 'command-line')
const electronPath = remote.getGlobal('process').execPath
const appProcess = cp.spawn(electronPath, [appPath, ...args])
let output = ''
appProcess.stdout.on('data', (data) => { output += data })
await emittedOnce(appProcess.stdout, 'end')
return JSON.parse(output)
}
})
describe('default behavior', () => {
describe('application menu', () => {
it('creates the default menu if the app does not set it', async () => {
const result = await runTestApp('default-menu')
expect(result).to.equal(false)
})
it('does not create the default menu if the app sets a custom menu', async () => {
const result = await runTestApp('default-menu', '--custom-menu')
expect(result).to.equal(true)
})
it('does not create the default menu if the app sets a null menu', async () => {
const result = await runTestApp('default-menu', '--null-menu')
expect(result).to.equal(true)
})
})
describe('window-all-closed', () => {
it('quits when the app does not handle the event', async () => {
const result = await runTestApp('window-all-closed')
expect(result).to.equal(false)
})
it('does not quit when the app handles the event', async () => {
const result = await runTestApp('window-all-closed', '--handle-event')
expect(result).to.equal(true)
})
})
})
async function runTestApp (name, ...args) {
const appPath = path.join(__dirname, 'fixtures', 'api', name)
const electronPath = remote.getGlobal('process').execPath
const appProcess = cp.spawn(electronPath, [appPath, ...args])
let output = ''
appProcess.stdout.on('data', (data) => { output += data })
await emittedOnce(appProcess.stdout, 'end')
return JSON.parse(output)
}

32
spec/fixtures/api/default-menu/main.js vendored Normal file
View File

@@ -0,0 +1,32 @@
const { app, Menu } = require('electron')
function output (value) {
process.stdout.write(JSON.stringify(value))
process.stdout.end()
app.quit()
}
try {
let expectedMenu
if (app.commandLine.hasSwitch('custom-menu')) {
expectedMenu = new Menu()
Menu.setApplicationMenu(expectedMenu)
} else if (app.commandLine.hasSwitch('null-menu')) {
expectedMenu = null
Menu.setApplicationMenu(null)
}
app.on('ready', () => {
setImmediate(() => {
try {
output(Menu.getApplicationMenu() === expectedMenu)
} catch (error) {
output(null)
}
})
})
} catch (error) {
output(null)
}

View File

@@ -0,0 +1,4 @@
{
"name": "default-menu",
"main": "main.js"
}

View File

@@ -0,0 +1,20 @@
const { app, BrowserWindow } = require('electron')
let handled = false
if (app.commandLine.hasSwitch('handle-event')) {
app.on('window-all-closed', () => {
handled = true
app.quit()
})
}
app.on('quit', () => {
process.stdout.write(JSON.stringify(handled))
process.stdout.end()
})
app.on('ready', () => {
const win = new BrowserWindow()
win.close()
})

View File

@@ -0,0 +1,4 @@
{
"name": "window-all-closed",
"main": "main.js"
}