electron/spec/api-app-spec.js

961 lines
28 KiB
JavaScript
Raw Normal View History

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const dirtyChai = require('dirty-chai')
2016-03-25 19:57:17 +00:00
const ChildProcess = require('child_process')
2016-04-18 16:23:44 +00:00
const https = require('https')
2016-06-03 03:12:20 +00:00
const net = require('net')
2016-04-18 16:23:44 +00:00
const fs = require('fs')
2016-03-25 19:57:17 +00:00
const path = require('path')
2016-12-02 17:34:16 +00:00
const {ipcRenderer, remote} = require('electron')
const {closeWindow} = require('./window-helpers')
2016-03-25 19:57:17 +00:00
const {expect} = chai
const {app, BrowserWindow, Menu, ipcMain} = remote
2016-03-25 19:57:17 +00:00
2017-02-08 02:24:35 +00:00
const isCI = remote.getGlobal('isCi')
chai.use(chaiAsPromised)
chai.use(dirtyChai)
describe('electron module', () => {
it('does not expose internal modules to require', () => {
expect(() => {
2016-03-25 19:57:17 +00:00
require('clipboard')
}).to.throw(/Cannot find module 'clipboard'/)
2016-03-25 19:57:17 +00:00
})
2016-05-23 20:12:02 +00:00
describe('require("electron")', () => {
2016-05-23 20:12:02 +00:00
let window = null
beforeEach(() => {
2016-05-23 20:12:02 +00:00
window = new BrowserWindow({
show: false,
width: 400,
height: 400
})
})
afterEach(() => {
return closeWindow(window).then(() => { window = null })
2016-05-23 20:12:02 +00:00
})
it('always returns the internal electron module', (done) => {
ipcMain.once('answer', () => done())
window.loadURL(`file://${path.join(__dirname, 'fixtures', 'api', 'electron-module-app', 'index.html')}`)
2016-05-23 20:12:02 +00:00
})
})
2016-03-25 19:57:17 +00:00
})
describe('app module', () => {
2016-12-02 17:34:16 +00:00
let server, secureUrl
const certPath = path.join(__dirname, 'fixtures', 'certificates')
Fix some flaky tests in CI (#12153) * Guard whole InitPrefs with ScopedAllowIO Saw a crash: 0 0x7f8d2f7d918d base::debug::StackTrace::StackTrace() 1 0x7f8d2f7d755c base::debug::StackTrace::StackTrace() 2 0x7f8d2f867caa logging::LogMessage::~LogMessage() 3 0x7f8d2fa157c7 base::ThreadRestrictions::AssertIOAllowed() 4 0x7f8d2f83453a base::OpenFile() 5 0x7f8d2f82a967 base::ReadFileToStringWithMaxSize() 6 0x7f8d2f82ad44 base::ReadFileToString() 7 0x7f8d2f846f73 JSONFileValueDeserializer::ReadFileToString() 8 0x7f8d2f84738c JSONFileValueDeserializer::Deserialize() 9 0x7f8d35a5d1f6 <unknown> 10 0x7f8d35a5c217 JsonPrefStore::ReadPrefs() 11 0x7f8d35a87d3e PrefService::InitFromStorage() 12 0x7f8d35a87c60 PrefService::PrefService() 13 0x7f8d35a91a10 PrefServiceFactory::Create() 14 0x000000e86e1b brightray::BrowserContext::InitPrefs() 15 0x000000c2bd64 atom::AtomBrowserContext::AtomBrowserContext() 16 0x000000c320db atom::AtomBrowserContext::From() 17 0x000000b4b8b5 atom::api::Session::FromPartition() * Fix done being called twice in setInterval test The callback passed to browser process is called asyncly, so it is possible that multiple callbacks has already been scheduled before we can clearInternval. * Fix failing test when dir name has special chars The pdfSource is not escaped while parsedURL.search is. * Call done with Error instead of string * Fix crash caused by not removing input observer Solve crash: 0 libcontent.dylib content::RenderWidgetHostImpl::DispatchInputEventWithLatencyInfo(blink::WebInputEvent const&, ui::LatencyInfo*) + 214 1 libcontent.dylib content::RenderWidgetHostImpl::ForwardMouseEventWithLatencyInfo(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 1350 2 libcontent.dylib content::RenderWidgetHostViewMac::ProcessMouseEvent(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 44 3 libcontent.dylib content::RenderWidgetHostInputEventRouter::RouteMouseEvent(content::RenderWidgetHostViewBase*, blink::WebMouseEvent*, ui::LatencyInfo const&) + 1817 * Print detailed error * Run tests after server is ready
2018-03-07 05:40:27 +00:00
before((done) => {
2016-12-02 17:34:16 +00:00
const options = {
key: fs.readFileSync(path.join(certPath, 'server.key')),
cert: fs.readFileSync(path.join(certPath, 'server.pem')),
ca: [
fs.readFileSync(path.join(certPath, 'rootCA.pem')),
fs.readFileSync(path.join(certPath, 'intermediateCA.pem'))
],
requestCert: true,
rejectUnauthorized: false
}
server = https.createServer(options, (req, res) => {
2016-12-02 17:34:16 +00:00
if (req.client.authorized) {
res.writeHead(200)
res.end('<title>authorized</title>')
} else {
res.writeHead(401)
res.end('<title>denied</title>')
}
})
server.listen(0, '127.0.0.1', () => {
2016-12-02 17:34:16 +00:00
const port = server.address().port
secureUrl = `https://127.0.0.1:${port}`
Fix some flaky tests in CI (#12153) * Guard whole InitPrefs with ScopedAllowIO Saw a crash: 0 0x7f8d2f7d918d base::debug::StackTrace::StackTrace() 1 0x7f8d2f7d755c base::debug::StackTrace::StackTrace() 2 0x7f8d2f867caa logging::LogMessage::~LogMessage() 3 0x7f8d2fa157c7 base::ThreadRestrictions::AssertIOAllowed() 4 0x7f8d2f83453a base::OpenFile() 5 0x7f8d2f82a967 base::ReadFileToStringWithMaxSize() 6 0x7f8d2f82ad44 base::ReadFileToString() 7 0x7f8d2f846f73 JSONFileValueDeserializer::ReadFileToString() 8 0x7f8d2f84738c JSONFileValueDeserializer::Deserialize() 9 0x7f8d35a5d1f6 <unknown> 10 0x7f8d35a5c217 JsonPrefStore::ReadPrefs() 11 0x7f8d35a87d3e PrefService::InitFromStorage() 12 0x7f8d35a87c60 PrefService::PrefService() 13 0x7f8d35a91a10 PrefServiceFactory::Create() 14 0x000000e86e1b brightray::BrowserContext::InitPrefs() 15 0x000000c2bd64 atom::AtomBrowserContext::AtomBrowserContext() 16 0x000000c320db atom::AtomBrowserContext::From() 17 0x000000b4b8b5 atom::api::Session::FromPartition() * Fix done being called twice in setInterval test The callback passed to browser process is called asyncly, so it is possible that multiple callbacks has already been scheduled before we can clearInternval. * Fix failing test when dir name has special chars The pdfSource is not escaped while parsedURL.search is. * Call done with Error instead of string * Fix crash caused by not removing input observer Solve crash: 0 libcontent.dylib content::RenderWidgetHostImpl::DispatchInputEventWithLatencyInfo(blink::WebInputEvent const&, ui::LatencyInfo*) + 214 1 libcontent.dylib content::RenderWidgetHostImpl::ForwardMouseEventWithLatencyInfo(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 1350 2 libcontent.dylib content::RenderWidgetHostViewMac::ProcessMouseEvent(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 44 3 libcontent.dylib content::RenderWidgetHostInputEventRouter::RouteMouseEvent(content::RenderWidgetHostViewBase*, blink::WebMouseEvent*, ui::LatencyInfo const&) + 1817 * Print detailed error * Run tests after server is ready
2018-03-07 05:40:27 +00:00
done()
2016-12-02 17:34:16 +00:00
})
})
after(done => {
Fix some flaky tests in CI (#12153) * Guard whole InitPrefs with ScopedAllowIO Saw a crash: 0 0x7f8d2f7d918d base::debug::StackTrace::StackTrace() 1 0x7f8d2f7d755c base::debug::StackTrace::StackTrace() 2 0x7f8d2f867caa logging::LogMessage::~LogMessage() 3 0x7f8d2fa157c7 base::ThreadRestrictions::AssertIOAllowed() 4 0x7f8d2f83453a base::OpenFile() 5 0x7f8d2f82a967 base::ReadFileToStringWithMaxSize() 6 0x7f8d2f82ad44 base::ReadFileToString() 7 0x7f8d2f846f73 JSONFileValueDeserializer::ReadFileToString() 8 0x7f8d2f84738c JSONFileValueDeserializer::Deserialize() 9 0x7f8d35a5d1f6 <unknown> 10 0x7f8d35a5c217 JsonPrefStore::ReadPrefs() 11 0x7f8d35a87d3e PrefService::InitFromStorage() 12 0x7f8d35a87c60 PrefService::PrefService() 13 0x7f8d35a91a10 PrefServiceFactory::Create() 14 0x000000e86e1b brightray::BrowserContext::InitPrefs() 15 0x000000c2bd64 atom::AtomBrowserContext::AtomBrowserContext() 16 0x000000c320db atom::AtomBrowserContext::From() 17 0x000000b4b8b5 atom::api::Session::FromPartition() * Fix done being called twice in setInterval test The callback passed to browser process is called asyncly, so it is possible that multiple callbacks has already been scheduled before we can clearInternval. * Fix failing test when dir name has special chars The pdfSource is not escaped while parsedURL.search is. * Call done with Error instead of string * Fix crash caused by not removing input observer Solve crash: 0 libcontent.dylib content::RenderWidgetHostImpl::DispatchInputEventWithLatencyInfo(blink::WebInputEvent const&, ui::LatencyInfo*) + 214 1 libcontent.dylib content::RenderWidgetHostImpl::ForwardMouseEventWithLatencyInfo(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 1350 2 libcontent.dylib content::RenderWidgetHostViewMac::ProcessMouseEvent(blink::WebMouseEvent const&, ui::LatencyInfo const&) + 44 3 libcontent.dylib content::RenderWidgetHostInputEventRouter::RouteMouseEvent(content::RenderWidgetHostViewBase*, blink::WebMouseEvent*, ui::LatencyInfo const&) + 1817 * Print detailed error * Run tests after server is ready
2018-03-07 05:40:27 +00:00
server.close(() => done())
2016-12-02 17:34:16 +00:00
})
describe('app.getVersion()', () => {
it('returns the version field of package.json', () => {
expect(app.getVersion()).to.equal('0.1.0')
2016-03-25 19:57:17 +00:00
})
})
describe('app.setVersion(version)', () => {
it('overrides the version', () => {
expect(app.getVersion()).to.equal('0.1.0')
2016-03-25 19:57:17 +00:00
app.setVersion('test-version')
expect(app.getVersion()).to.equal('test-version')
2016-03-25 19:57:17 +00:00
app.setVersion('0.1.0')
})
})
describe('app.getName()', () => {
it('returns the name field of package.json', () => {
expect(app.getName()).to.equal('Electron Test')
2016-03-25 19:57:17 +00:00
})
})
describe('app.setName(name)', () => {
it('overrides the name', () => {
expect(app.getName()).to.equal('Electron Test')
2016-03-25 19:57:17 +00:00
app.setName('test-name')
expect(app.getName()).to.equal('test-name')
2016-03-25 19:57:17 +00:00
app.setName('Electron Test')
})
})
describe('app.getLocale()', () => {
it('should not be empty', () => {
expect(app.getLocale()).to.not.be.empty()
2016-03-25 19:57:17 +00:00
})
})
describe('app.isPackaged', () => {
it('should be false durings tests', () => {
expect(app.isPackaged).to.be.false()
})
})
describe('app.isInApplicationsFolder()', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('should be false during tests', () => {
expect(app.isInApplicationsFolder()).to.be.false()
})
})
describe('app.exit(exitCode)', () => {
let appProcess = null
2016-03-25 19:57:17 +00:00
afterEach(() => {
2016-11-29 22:19:20 +00:00
if (appProcess != null) appProcess.kill()
2016-03-25 19:57:17 +00:00
})
it('emits a process exit event with the code', done => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
const electronPath = remote.getGlobal('process').execPath
let output = ''
2016-03-25 19:57:17 +00:00
appProcess = ChildProcess.spawn(electronPath, [appPath])
appProcess.stdout.on('data', data => { output += data })
appProcess.on('close', code => {
2016-01-12 02:40:23 +00:00
if (process.platform !== 'win32') {
expect(output).to.include('Exit event with code: 123')
2016-01-12 02:40:23 +00:00
}
expect(code).to.equal(123)
2016-03-25 19:57:17 +00:00
done()
})
})
it('closes all windows', done => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'exit-closes-all-windows-app')
const electronPath = remote.getGlobal('process').execPath
appProcess = ChildProcess.spawn(electronPath, [appPath])
appProcess.on('close', code => {
expect(code).to.equal(123)
done()
})
})
2018-01-11 22:50:35 +00:00
it('exits gracefully', function (done) {
if (!['darwin', 'linux'].includes(process.platform)) {
2018-01-12 18:40:48 +00:00
this.skip()
2018-01-11 22:50:35 +00:00
}
2018-01-11 22:50:35 +00:00
const electronPath = remote.getGlobal('process').execPath
const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
2018-01-11 22:50:35 +00:00
appProcess = ChildProcess.spawn(electronPath, [appPath])
// Singleton will send us greeting data to let us know it's running.
// After that, ask it to exit gracefully and confirm that it does.
appProcess.stdout.on('data', data => appProcess.kill())
appProcess.on('exit', (code, sig) => {
const message = `code:\n${code}\nsig:\n${sig}`
expect(code).to.equal(0, message)
expect(sig).to.be.null(message)
done()
2018-01-11 22:50:35 +00:00
})
})
2016-03-25 19:57:17 +00:00
})
// TODO(MarshallOfSound) - Remove in 4.0.0
describe('app.makeSingleInstance', () => {
it('prevents the second launch of app', function (done) {
this.timeout(120000)
const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton-old')
// First launch should exit with 0.
const first = ChildProcess.spawn(remote.process.execPath, [appPath])
first.once('exit', code => {
expect(code).to.equal(0)
})
// Start second app when received output.
first.stdout.once('data', () => {
// Second launch should exit with 1.
const second = ChildProcess.spawn(remote.process.execPath, [appPath])
second.once('exit', code => {
expect(code).to.equal(1)
done()
})
})
})
})
describe('app.requestSingleInstanceLock', () => {
it('prevents the second launch of app', function (done) {
this.timeout(120000)
const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
const first = ChildProcess.spawn(remote.process.execPath, [appPath])
first.once('exit', code => {
expect(code).to.equal(0)
})
// Start second app when received output.
first.stdout.once('data', () => {
const second = ChildProcess.spawn(remote.process.execPath, [appPath])
second.once('exit', code => {
expect(code).to.equal(1)
done()
})
})
})
})
describe('app.relaunch', () => {
2016-06-03 03:12:20 +00:00
let server = null
2016-06-29 16:37:10 +00:00
const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-relaunch' : '/tmp/electron-app-relaunch'
2016-06-03 03:12:20 +00:00
beforeEach(done => {
2016-06-29 16:37:10 +00:00
fs.unlink(socketPath, () => {
2016-06-03 03:12:20 +00:00
server = net.createServer()
server.listen(socketPath)
done()
})
})
afterEach((done) => {
2016-06-03 03:12:20 +00:00
server.close(() => {
if (process.platform === 'win32') {
done()
} else {
fs.unlink(socketPath, () => done())
2016-06-03 03:12:20 +00:00
}
})
})
it('relaunches the app', function (done) {
this.timeout(120000)
2016-06-03 03:12:20 +00:00
let state = 'none'
server.once('error', error => done(error))
server.on('connection', client => {
client.once('data', data => {
2016-06-03 03:12:20 +00:00
if (String(data) === 'false' && state === 'none') {
state = 'first-launch'
} else if (String(data) === 'true' && state === 'first-launch') {
done()
} else {
done(`Unexpected state: ${state}`)
}
})
})
const appPath = path.join(__dirname, 'fixtures', 'api', 'relaunch')
ChildProcess.spawn(remote.process.execPath, [appPath])
})
})
describe('app.setUserActivity(type, userInfo)', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('sets the current activity', () => {
2016-06-29 16:37:10 +00:00
app.setUserActivity('com.electron.testActivity', {testData: '123'})
expect(app.getCurrentActivityType()).to.equal('com.electron.testActivity')
})
})
xdescribe('app.importCertificate', () => {
let w = null
2016-04-18 16:23:44 +00:00
before(function () {
if (process.platform !== 'linux') {
this.skip()
}
})
afterEach(() => closeWindow(w).then(() => { w = null }))
2016-04-18 16:23:44 +00:00
it('can import certificate into platform cert store', done => {
const options = {
certificate: path.join(certPath, 'client.p12'),
2016-04-18 16:23:44 +00:00
password: 'electron'
}
w = new BrowserWindow({ show: false })
2016-04-18 16:23:44 +00:00
w.webContents.on('did-finish-load', () => {
expect(w.webContents.getTitle()).to.equal('authorized')
2016-04-18 16:23:44 +00:00
done()
})
ipcRenderer.once('select-client-certificate', (event, webContentsId, list) => {
expect(webContentsId).to.equal(w.webContents.id)
expect(list).to.have.lengthOf(1)
expect(list[0]).to.deep.equal({
issuerName: 'Intermediate CA',
subjectName: 'Client Cert',
issuer: { commonName: 'Intermediate CA' },
subject: { commonName: 'Client Cert' }
})
2016-12-07 10:03:56 +00:00
event.sender.send('client-certificate-response', list[0])
})
app.importCertificate(options, result => {
expect(result).toNotExist()
2016-12-02 17:34:16 +00:00
ipcRenderer.sendSync('set-client-certificate-option', false)
w.loadURL(secureUrl)
2016-04-18 16:23:44 +00:00
})
})
})
describe('BrowserWindow events', () => {
let w = null
afterEach(() => closeWindow(w).then(() => { w = null }))
2016-03-25 19:57:17 +00:00
it('should emit browser-window-focus event when window is focused', (done) => {
app.once('browser-window-focus', (e, window) => {
expect(w.id).to.equal(window.id)
2016-03-25 19:57:17 +00:00
done()
})
w = new BrowserWindow({ show: false })
2016-03-25 19:57:17 +00:00
w.emit('focus')
})
it('should emit browser-window-blur event when window is blured', (done) => {
app.once('browser-window-blur', (e, window) => {
expect(w.id).to.equal(window.id)
2016-03-25 19:57:17 +00:00
done()
})
w = new BrowserWindow({ show: false })
2016-03-25 19:57:17 +00:00
w.emit('blur')
})
it('should emit browser-window-created event when window is created', (done) => {
app.once('browser-window-created', (e, window) => {
setImmediate(() => {
expect(w.id).to.equal(window.id)
2016-03-25 19:57:17 +00:00
done()
})
})
w = new BrowserWindow({ show: false })
2016-06-13 16:05:04 +00:00
})
it('should emit web-contents-created event when a webContents is created', (done) => {
app.once('web-contents-created', (e, webContents) => {
setImmediate(() => {
expect(w.webContents.id).to.equal(webContents.id)
2016-06-13 16:05:04 +00:00
done()
})
})
w = new BrowserWindow({ show: false })
2016-03-25 19:57:17 +00:00
})
})
describe('app.setBadgeCount', () => {
const platformIsNotSupported =
(process.platform === 'win32') ||
(process.platform === 'linux' && !app.isUnityRunning())
const platformIsSupported = !platformIsNotSupported
const expectedBadgeCount = 42
let returnValue = null
beforeEach(() => { returnValue = app.setBadgeCount(expectedBadgeCount) })
after(() => {
// Remove the badge.
app.setBadgeCount(0)
})
describe('on supported platform', () => {
before(function () {
if (platformIsNotSupported) {
this.skip()
}
})
it('returns true', () => {
expect(returnValue).to.be.true()
})
it('sets a badge count', () => {
expect(app.getBadgeCount()).to.equal(expectedBadgeCount)
})
})
describe('on unsupported platform', () => {
before(function () {
if (platformIsSupported) {
this.skip()
}
})
it('returns false', () => {
expect(returnValue).to.be.false()
})
it('does not set a badge count', () => {
expect(app.getBadgeCount()).to.equal(0)
})
})
})
2016-07-06 20:34:14 +00:00
describe('app.get/setLoginItemSettings API', () => {
const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
const processStartArgs = [
'--processStart', `"${path.basename(process.execPath)}"`,
'--process-start-args', `"--hidden"`
]
before(function () {
if (process.platform === 'linux') {
this.skip()
}
})
beforeEach(() => {
2016-07-07 23:29:09 +00:00
app.setLoginItemSettings({openAtLogin: false})
app.setLoginItemSettings({openAtLogin: false, path: updateExe, args: processStartArgs})
})
afterEach(() => {
2016-07-07 23:29:09 +00:00
app.setLoginItemSettings({openAtLogin: false})
app.setLoginItemSettings({openAtLogin: false, path: updateExe, args: processStartArgs})
2016-07-06 20:34:14 +00:00
})
it('returns the login item status of the app', done => {
2016-07-07 23:29:09 +00:00
app.setLoginItemSettings({openAtLogin: true})
expect(app.getLoginItemSettings()).to.deep.equal({
2016-07-07 23:29:09 +00:00
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
2016-07-07 23:29:09 +00:00
restoreState: false
})
app.setLoginItemSettings({openAtLogin: true, openAsHidden: true})
expect(app.getLoginItemSettings()).to.deep.equal({
2016-07-07 23:29:09 +00:00
openAtLogin: true,
2017-11-17 01:49:23 +00:00
openAsHidden: process.platform === 'darwin' && !process.mas, // Only available on macOS
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
2016-07-07 23:29:09 +00:00
restoreState: false
})
2016-07-06 20:34:14 +00:00
2016-07-07 23:29:09 +00:00
app.setLoginItemSettings({})
2017-11-20 01:15:45 +00:00
// Wait because login item settings are not applied immediately in MAS build
2017-11-20 01:17:34 +00:00
const delay = process.mas ? 100 : 0
2017-11-20 01:15:45 +00:00
setTimeout(() => {
expect(app.getLoginItemSettings()).to.deep.equal({
2017-11-20 01:15:45 +00:00
openAtLogin: false,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false
})
done()
2017-11-20 01:17:34 +00:00
}, delay)
2016-07-06 20:34:14 +00:00
})
it('allows you to pass a custom executable and arguments', function () {
if (process.platform !== 'win32') {
// FIXME(alexeykuzmin): Skip the test.
// this.skip()
return
}
app.setLoginItemSettings({openAtLogin: true, path: updateExe, args: processStartArgs})
expect(app.getLoginItemSettings().openAtLogin).to.be.false()
expect(app.getLoginItemSettings({
path: updateExe,
args: processStartArgs
}).openAtLogin).to.be.true()
})
2016-07-06 20:34:14 +00:00
})
describe('isAccessibilitySupportEnabled API', () => {
it('returns whether the Chrome has accessibility APIs enabled', () => {
expect(app.isAccessibilitySupportEnabled()).to.be.a('boolean')
})
})
2016-10-06 16:57:25 +00:00
describe('getPath(name)', () => {
it('returns paths that exist', () => {
const paths = [
fs.existsSync(app.getPath('exe')),
fs.existsSync(app.getPath('home')),
fs.existsSync(app.getPath('temp'))
]
expect(paths).to.deep.equal([true, true, true])
2016-10-06 16:57:25 +00:00
})
it('throws an error when the name is invalid', () => {
expect(() => {
2016-10-06 16:57:25 +00:00
app.getPath('does-not-exist')
}).to.throw(/Failed to get 'does-not-exist' path/)
2016-10-06 16:57:25 +00:00
})
it('returns the overridden path', () => {
2016-10-06 16:57:25 +00:00
app.setPath('music', __dirname)
expect(app.getPath('music')).to.equal(__dirname)
2016-10-06 16:57:25 +00:00
})
})
2016-12-02 17:34:16 +00:00
2017-10-23 07:54:38 +00:00
describe('select-client-certificate event', () => {
2016-12-02 17:34:16 +00:00
let w = null
before(function () {
if (process.platform === 'linux') {
this.skip()
}
})
beforeEach(() => {
2016-12-02 17:34:16 +00:00
w = new BrowserWindow({
show: false,
webPreferences: {
partition: 'empty-certificate'
}
})
})
afterEach(() => closeWindow(w).then(() => { w = null }))
2016-12-02 17:34:16 +00:00
it('can respond with empty certificate list', done => {
w.webContents.on('did-finish-load', () => {
expect(w.webContents.getTitle()).to.equal('denied')
2016-12-02 17:34:16 +00:00
done()
})
ipcRenderer.sendSync('set-client-certificate-option', true)
w.webContents.loadURL(secureUrl)
})
})
2017-02-02 16:11:34 +00:00
describe('setAsDefaultProtocolClient(protocol, path, args)', () => {
2017-02-02 16:41:14 +00:00
const protocol = 'electron-test'
2017-02-02 16:11:34 +00:00
const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
const processStartArgs = [
'--processStart', `"${path.basename(process.execPath)}"`,
'--process-start-args', `"--hidden"`
]
let Winreg
let classesKey
before(function () {
if (process.platform !== 'win32') {
this.skip()
} else {
Winreg = require('winreg')
classesKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Classes\\'
})
}
})
after(function (done) {
if (process.platform !== 'win32') {
done()
} else {
const protocolKey = new Winreg({
hive: Winreg.HKCU,
key: `\\Software\\Classes\\${protocol}`
})
// The last test leaves the registry dirty,
// delete the protocol key for those of us who test at home
protocolKey.destroy(() => done())
2017-12-05 21:20:34 +00:00
}
})
2017-02-02 16:11:34 +00:00
beforeEach(() => {
2017-02-02 16:41:14 +00:00
app.removeAsDefaultProtocolClient(protocol)
app.removeAsDefaultProtocolClient(protocol, updateExe, processStartArgs)
2017-02-02 16:11:34 +00:00
})
afterEach(() => {
2017-02-02 16:41:14 +00:00
app.removeAsDefaultProtocolClient(protocol)
expect(app.isDefaultProtocolClient(protocol)).to.be.false()
2017-02-02 16:41:14 +00:00
app.removeAsDefaultProtocolClient(protocol, updateExe, processStartArgs)
expect(app.isDefaultProtocolClient(protocol, updateExe, processStartArgs)).to.be.false()
2017-02-02 16:11:34 +00:00
})
it('sets the app as the default protocol client', () => {
expect(app.isDefaultProtocolClient(protocol)).to.be.false()
2017-02-02 16:41:14 +00:00
app.setAsDefaultProtocolClient(protocol)
expect(app.isDefaultProtocolClient(protocol)).to.be.true()
2017-02-02 16:11:34 +00:00
})
it('allows a custom path and args to be specified', () => {
expect(app.isDefaultProtocolClient(protocol, updateExe, processStartArgs)).to.be.false()
2017-02-02 16:41:14 +00:00
app.setAsDefaultProtocolClient(protocol, updateExe, processStartArgs)
expect(app.isDefaultProtocolClient(protocol, updateExe, processStartArgs)).to.be.true()
expect(app.isDefaultProtocolClient(protocol)).to.be.false()
2017-02-02 16:11:34 +00:00
})
it('creates a registry entry for the protocol class', (done) => {
app.setAsDefaultProtocolClient(protocol)
2017-12-05 19:38:19 +00:00
classesKey.keys((error, keys) => {
if (error) throw error
2017-12-05 19:38:19 +00:00
const exists = !!keys.find(key => key.key.includes(protocol))
expect(exists).to.be.true()
done()
})
})
it('completely removes a registry entry for the protocol class', (done) => {
app.setAsDefaultProtocolClient(protocol)
app.removeAsDefaultProtocolClient(protocol)
2017-12-05 19:38:19 +00:00
classesKey.keys((error, keys) => {
if (error) throw error
2017-12-05 19:38:19 +00:00
const exists = !!keys.find(key => key.key.includes(protocol))
expect(exists).to.be.false()
2017-12-05 19:38:19 +00:00
done()
})
})
it('only unsets a class registry key if it contains other data', (done) => {
app.setAsDefaultProtocolClient(protocol)
const protocolKey = new Winreg({
hive: Winreg.HKCU,
key: `\\Software\\Classes\\${protocol}`
})
protocolKey.set('test-value', 'REG_BINARY', '123', () => {
app.removeAsDefaultProtocolClient(protocol)
classesKey.keys((error, keys) => {
if (error) throw error
2017-12-05 19:38:19 +00:00
const exists = !!keys.find(key => key.key.includes(protocol))
expect(exists).to.be.true()
2017-12-05 19:38:19 +00:00
done()
})
})
})
2017-02-02 16:11:34 +00:00
})
2016-11-06 10:59:17 +00:00
describe('app launch through uri', () => {
before(function () {
if (process.platform !== 'win32') {
this.skip()
}
})
it('does not launch for argument following a URL', done => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
// App should exit with non 123 code.
const first = ChildProcess.spawn(remote.process.execPath, [appPath, 'electron-test:?', 'abc'])
first.once('exit', code => {
expect(code).to.not.equal(123)
done()
})
})
it('launches successfully for argument following a file path', done => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
// App should exit with code 123.
const first = ChildProcess.spawn(remote.process.execPath, [appPath, 'e:\\abc', 'abc'])
first.once('exit', code => {
expect(code).to.equal(123)
done()
})
})
it('launches successfully for multiple URIs following --', done => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
// App should exit with code 123.
const first = ChildProcess.spawn(remote.process.execPath, [appPath, '--', 'http://electronjs.org', 'electron-test://testdata'])
first.once('exit', code => {
expect(code).to.equal(123)
done()
})
})
})
describe('getFileIcon() API', () => {
2016-11-10 18:34:30 +00:00
const iconPath = path.join(__dirname, 'fixtures/assets/icon.ico')
2016-11-06 10:59:17 +00:00
const sizes = {
small: 16,
normal: 32,
2017-02-07 19:20:27 +00:00
large: process.platform === 'win32' ? 32 : 48
2016-11-06 11:03:16 +00:00
}
2016-11-06 10:59:17 +00:00
// (alexeykuzmin): `.skip()` called in `before`
// doesn't affect nested `describe`s.
beforeEach(function () {
// FIXME Get these specs running on Linux CI
if (process.platform === 'linux' && isCI) {
this.skip()
}
})
it('fetches a non-empty icon', done => {
app.getFileIcon(iconPath, (err, icon) => {
expect(err).to.be.null()
expect(icon.isEmpty()).to.be.false()
2016-11-06 10:59:17 +00:00
done()
})
})
it('fetches normal icon size by default', done => {
app.getFileIcon(iconPath, (err, icon) => {
2016-11-06 10:59:17 +00:00
const size = icon.getSize()
expect(err).to.be.null()
expect(size.height).to.equal(sizes.normal)
expect(size.width).to.equal(sizes.normal)
2016-11-06 10:59:17 +00:00
done()
})
})
describe('size option', () => {
it('fetches a small icon', (done) => {
app.getFileIcon(iconPath, { size: 'small' }, (err, icon) => {
2016-11-06 10:59:17 +00:00
const size = icon.getSize()
expect(err).to.be.null()
expect(size.height).to.equal(sizes.small)
expect(size.width).to.equal(sizes.small)
2016-11-06 10:59:17 +00:00
done()
})
})
it('fetches a normal icon', (done) => {
app.getFileIcon(iconPath, { size: 'normal' }, (err, icon) => {
2016-11-06 10:59:17 +00:00
const size = icon.getSize()
expect(err).to.be.null()
expect(size.height).to.equal(sizes.normal)
expect(size.width).to.equal(sizes.normal)
2016-11-06 10:59:17 +00:00
done()
})
})
it('fetches a large icon', function (done) {
2017-02-07 18:16:09 +00:00
// macOS does not support large icons
if (process.platform === 'darwin') {
// FIXME(alexeykuzmin): Skip the test.
// this.skip()
return done()
}
2017-02-07 18:16:09 +00:00
app.getFileIcon(iconPath, { size: 'large' }, (err, icon) => {
2016-11-06 10:59:17 +00:00
const size = icon.getSize()
expect(err).to.be.null()
expect(size.height).to.equal(sizes.large)
expect(size.width).to.equal(sizes.large)
2016-11-06 10:59:17 +00:00
done()
})
})
})
2017-04-27 15:38:17 +00:00
})
// TODO(marshallofsound): [Ch66] Failed on Windows x64 + ia32 on CI, passes locally
xdescribe('getAppMetrics() API', () => {
2018-06-18 04:00:17 +00:00
it('returns memory and cpu stats of all running electron processes', () => {
2017-05-16 00:41:45 +00:00
const appMetrics = app.getAppMetrics()
expect(appMetrics).to.be.an('array').and.have.lengthOf.at.least(1, 'App memory info object is not > 0')
2017-05-26 15:53:26 +00:00
const types = []
2017-05-16 00:41:45 +00:00
for (const {memory, pid, type, cpu} of appMetrics) {
expect(memory.workingSetSize).to.be.above(0, 'working set size is not > 0')
expect(memory.privateBytes).to.be.above(0, 'private bytes is not > 0')
expect(memory.sharedBytes).to.be.above(0, 'shared bytes is not > 0')
expect(pid).to.be.above(0, 'pid is not > 0')
expect(type).to.be.a('string').that.is.not.empty()
2017-05-26 15:53:26 +00:00
types.push(type)
expect(cpu).to.have.own.property('percentCPUUsage').that.is.a('number')
expect(cpu).to.have.own.property('idleWakeupsPerSecond').that.is.a('number')
}
2017-05-26 15:53:26 +00:00
2017-05-26 17:10:56 +00:00
if (process.platform === 'darwin') {
expect(types).to.include('GPU')
2017-05-26 16:14:17 +00:00
}
expect(types).to.include('Browser')
expect(types).to.include('Tab')
})
2016-11-06 10:59:17 +00:00
})
2017-05-30 20:00:00 +00:00
describe('getGPUFeatureStatus() API', () => {
it('returns the graphic features statuses', () => {
2017-05-30 20:00:00 +00:00
const features = app.getGPUFeatureStatus()
expect(features).to.have.own.property('webgl').that.is.a('string')
expect(features).to.have.own.property('gpu_compositing').that.is.a('string')
2017-05-30 20:00:00 +00:00
})
})
2017-06-28 15:33:06 +00:00
describe('mixed sandbox option', () => {
let appProcess = null
let server = null
const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-mixed-sandbox' : '/tmp/electron-mixed-sandbox'
2017-06-28 15:33:06 +00:00
beforeEach(function (done) {
// XXX(alexeykuzmin): Calling `.skip()` inside a `before` hook
// doesn't affect nested `describe`s.
// FIXME Get these specs running on Linux
if (process.platform === 'linux') {
this.skip()
}
fs.unlink(socketPath, () => {
server = net.createServer()
server.listen(socketPath)
done()
})
})
afterEach(done => {
if (appProcess != null) appProcess.kill()
server.close(() => {
if (process.platform === 'win32') {
done()
} else {
fs.unlink(socketPath, () => done())
}
})
2017-06-28 15:33:06 +00:00
})
describe('when app.enableMixedSandbox() is called', () => {
// TODO(zcbenz): Find out why it fails in CI.
before(function () {
if (isCI && process.platform === 'win32') {
this.skip()
}
})
it('adds --enable-sandbox to render processes created with sandbox: true', done => {
2017-06-28 15:33:06 +00:00
const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
appProcess = ChildProcess.spawn(remote.process.execPath, [appPath])
2017-06-28 15:33:06 +00:00
server.once('error', error => { done(error) })
2017-06-28 15:33:06 +00:00
server.on('connection', client => {
client.once('data', data => {
const argv = JSON.parse(data)
expect(argv.sandbox).to.include('--enable-sandbox')
expect(argv.sandbox).to.not.include('--no-sandbox')
expect(argv.noSandbox).to.not.include('--enable-sandbox')
expect(argv.noSandbox).to.include('--no-sandbox')
done()
})
2017-06-28 15:33:06 +00:00
})
})
})
describe('when the app is launched with --enable-mixed-sandbox', () => {
// TODO(zcbenz): Find out why it fails in CI.
before(function () {
if (isCI && process.platform === 'win32') {
this.skip()
}
})
it('adds --enable-sandbox to render processes created with sandbox: true', done => {
2017-06-28 15:33:06 +00:00
const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'])
2017-06-28 15:33:06 +00:00
server.once('error', error => { done(error) })
2017-06-28 15:33:06 +00:00
server.on('connection', client => {
client.once('data', data => {
const argv = JSON.parse(data)
expect(argv.sandbox).to.include('--enable-sandbox')
expect(argv.sandbox).to.not.include('--no-sandbox')
expect(argv.noSandbox).to.not.include('--enable-sandbox')
expect(argv.noSandbox).to.include('--no-sandbox')
expect(argv.noSandboxDevtools).to.be.true()
expect(argv.sandboxDevtools).to.be.true()
done()
})
2017-06-28 15:33:06 +00:00
})
})
})
})
describe('disableDomainBlockingFor3DAPIs() API', () => {
it('throws when called after app is ready', () => {
expect(() => {
app.disableDomainBlockingFor3DAPIs()
}).to.throw(/before app is ready/)
})
})
describe('dock.setMenu', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('keeps references to the menu', () => {
app.dock.setMenu(new Menu())
const v8Util = process.atomBinding('v8_util')
v8Util.requestGarbageCollectionForTesting()
})
})
describe('whenReady', () => {
it('returns a Promise', () => {
expect(app.whenReady()).to.be.a('promise')
})
it('becomes fulfilled if the app is already ready', () => {
expect(app.isReady()).to.be.true()
return expect(app.whenReady()).to.be.eventually.fulfilled
})
})
2016-03-25 19:57:17 +00:00
})