electron/spec/api-app-spec.js

363 строки
10 KiB
JavaScript
Исходник Обычный вид История

2016-03-25 22:57:17 +03:00
const assert = require('assert')
const ChildProcess = require('child_process')
2016-04-18 19:23:44 +03:00
const https = require('https')
2016-06-03 06:12:20 +03:00
const net = require('net')
2016-04-18 19:23:44 +03:00
const fs = require('fs')
2016-03-25 22:57:17 +03:00
const path = require('path')
2016-05-23 23:12:02 +03:00
const {remote} = require('electron')
const {closeWindow} = require('./window-helpers')
2016-03-25 22:57:17 +03:00
2016-05-23 23:12:02 +03:00
const {app, BrowserWindow, ipcMain} = remote
2016-03-25 22:57:17 +03:00
describe('electron module', function () {
2016-04-28 21:21:52 +03:00
it('does not expose internal modules to require', function () {
assert.throws(function () {
2016-03-25 22:57:17 +03:00
require('clipboard')
}, /Cannot find module 'clipboard'/)
2016-03-25 22:57:17 +03:00
})
2016-05-23 23:12:02 +03:00
describe('require("electron")', function () {
let window = null
beforeEach(function () {
window = new BrowserWindow({
show: false,
width: 400,
height: 400
})
})
afterEach(function () {
return closeWindow(window).then(function () { window = null })
2016-05-23 23:12:02 +03:00
})
it('always returns the internal electron module', function (done) {
ipcMain.once('answer', function () {
done()
})
window.loadURL('file://' + path.join(__dirname, 'fixtures', 'api', 'electron-module-app', 'index.html'))
})
})
2016-03-25 22:57:17 +03:00
})
describe('app module', function () {
describe('app.getVersion()', function () {
it('returns the version field of package.json', function () {
assert.equal(app.getVersion(), '0.1.0')
})
})
describe('app.setVersion(version)', function () {
it('overrides the version', function () {
assert.equal(app.getVersion(), '0.1.0')
app.setVersion('test-version')
assert.equal(app.getVersion(), 'test-version')
app.setVersion('0.1.0')
})
})
describe('app.getName()', function () {
it('returns the name field of package.json', function () {
assert.equal(app.getName(), 'Electron Test')
})
})
describe('app.setName(name)', function () {
it('overrides the name', function () {
assert.equal(app.getName(), 'Electron Test')
app.setName('test-name')
assert.equal(app.getName(), 'test-name')
app.setName('Electron Test')
})
})
describe('app.getLocale()', function () {
it('should not be empty', function () {
assert.notEqual(app.getLocale(), '')
})
})
describe('app.exit(exitCode)', function () {
var appProcess = null
afterEach(function () {
2016-11-30 01:19:20 +03:00
if (appProcess != null) appProcess.kill()
2016-03-25 22:57:17 +03:00
})
it('emits a process exit event with the code', function (done) {
var appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
var electronPath = remote.getGlobal('process').execPath
var output = ''
appProcess = ChildProcess.spawn(electronPath, [appPath])
appProcess.stdout.on('data', function (data) {
output += data
})
appProcess.on('close', function (code) {
2016-01-12 05:40:23 +03:00
if (process.platform !== 'win32') {
2016-03-25 22:57:17 +03:00
assert.notEqual(output.indexOf('Exit event with code: 123'), -1)
2016-01-12 05:40:23 +03:00
}
2016-03-25 22:57:17 +03:00
assert.equal(code, 123)
done()
})
})
})
2016-06-03 06:12:20 +03:00
describe('app.relaunch', function () {
let server = null
2016-06-29 19:37:10 +03:00
const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-relaunch' : '/tmp/electron-app-relaunch'
2016-06-03 06:12:20 +03:00
beforeEach(function (done) {
2016-06-29 19:37:10 +03:00
fs.unlink(socketPath, () => {
2016-06-03 06:12:20 +03:00
server = net.createServer()
server.listen(socketPath)
done()
})
})
afterEach(function (done) {
server.close(() => {
if (process.platform === 'win32') {
done()
} else {
2016-06-29 19:37:10 +03:00
fs.unlink(socketPath, () => {
2016-06-03 06:12:20 +03:00
done()
})
}
})
})
it('relaunches the app', function (done) {
this.timeout(120000)
2016-06-03 06:12:20 +03:00
let state = 'none'
server.once('error', (error) => {
done(error)
})
server.on('connection', (client) => {
client.once('data', function (data) {
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)', function () {
if (process.platform !== 'darwin') {
return
}
it('sets the current activity', function () {
2016-06-29 19:37:10 +03:00
app.setUserActivity('com.electron.testActivity', {testData: '123'})
assert.equal(app.getCurrentActivityType(), 'com.electron.testActivity')
})
})
describe('app.importCertificate', function () {
2016-06-29 19:37:10 +03:00
if (process.platform !== 'linux') return
2016-04-18 19:23:44 +03:00
var w = null
var certPath = path.join(__dirname, 'fixtures', 'certificates')
var 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
}
var server = https.createServer(options, function (req, res) {
if (req.client.authorized) {
2016-06-29 19:37:10 +03:00
res.writeHead(200)
res.end('authorized')
2016-04-18 19:23:44 +03:00
}
})
afterEach(function () {
return closeWindow(w).then(function () { w = null })
2016-04-18 19:23:44 +03:00
})
it('can import certificate into platform cert store', function (done) {
let options = {
certificate: path.join(certPath, 'client.p12'),
2016-04-18 19:23:44 +03:00
password: 'electron'
}
w = new BrowserWindow({
show: false
})
w.webContents.on('did-finish-load', function () {
server.close()
done()
})
2016-04-18 20:27:37 +03:00
app.on('select-client-certificate', function (event, webContents, url, list, callback) {
assert.equal(list.length, 1)
assert.equal(list[0].issuerName, 'Intermediate CA')
assert.equal(list[0].subjectName, 'Client Cert')
assert.equal(list[0].issuer.commonName, 'Intermediate CA')
assert.equal(list[0].subject.commonName, 'Client Cert')
2016-04-18 20:27:37 +03:00
callback(list[0])
})
app.importCertificate(options, function (result) {
2016-04-18 19:23:44 +03:00
assert(!result)
2016-04-18 20:27:37 +03:00
server.listen(0, '127.0.0.1', function () {
var port = server.address().port
w.loadURL(`https://127.0.0.1:${port}`)
})
2016-04-18 19:23:44 +03:00
})
})
})
2016-03-25 22:57:17 +03:00
describe('BrowserWindow events', function () {
var w = null
2016-03-25 22:57:17 +03:00
afterEach(function () {
return closeWindow(w).then(function () { w = null })
2016-03-25 22:57:17 +03:00
})
it('should emit browser-window-focus event when window is focused', function (done) {
app.once('browser-window-focus', function (e, window) {
assert.equal(w.id, window.id)
done()
})
2016-01-12 05:40:23 +03:00
w = new BrowserWindow({
show: false
2016-03-25 22:57:17 +03:00
})
w.emit('focus')
})
it('should emit browser-window-blur event when window is blured', function (done) {
app.once('browser-window-blur', function (e, window) {
assert.equal(w.id, window.id)
done()
})
2016-01-12 05:40:23 +03:00
w = new BrowserWindow({
show: false
2016-03-25 22:57:17 +03:00
})
w.emit('blur')
})
it('should emit browser-window-created event when window is created', function (done) {
app.once('browser-window-created', function (e, window) {
setImmediate(function () {
assert.equal(w.id, window.id)
done()
})
})
2016-01-12 05:40:23 +03:00
w = new BrowserWindow({
show: false
2016-03-25 22:57:17 +03:00
})
2016-06-13 19:05:04 +03:00
})
it('should emit web-contents-created event when a webContents is created', function (done) {
app.once('web-contents-created', function (e, webContents) {
setImmediate(function () {
assert.equal(w.webContents.id, webContents.id)
done()
})
})
w = new BrowserWindow({
show: false
})
2016-03-25 22:57:17 +03:00
})
})
describe('app.setBadgeCount API', function () {
const shouldFail = process.platform === 'win32' ||
2016-07-02 04:36:11 +03:00
(process.platform === 'linux' && !app.isUnityRunning())
afterEach(function () {
app.setBadgeCount(0)
})
it('returns false when failed', function () {
assert.equal(app.setBadgeCount(42), !shouldFail)
})
2016-07-01 11:39:01 +03:00
it('should set a badge count', function () {
app.setBadgeCount(42)
assert.equal(app.getBadgeCount(), shouldFail ? 0 : 42)
})
})
2016-07-06 23:34:14 +03:00
2016-07-08 02:29:09 +03:00
describe('app.get/setLoginItemSettings API', function () {
2016-07-11 22:30:40 +03:00
if (process.platform === 'linux') return
2016-07-06 23:34:14 +03:00
beforeEach(function () {
2016-07-08 02:29:09 +03:00
app.setLoginItemSettings({openAtLogin: false})
})
2016-07-06 23:34:14 +03:00
afterEach(function () {
2016-07-08 02:29:09 +03:00
app.setLoginItemSettings({openAtLogin: false})
2016-07-06 23:34:14 +03:00
})
it('returns the login item status of the app', function () {
2016-07-08 02:29:09 +03:00
app.setLoginItemSettings({openAtLogin: true})
assert.deepEqual(app.getLoginItemSettings(), {
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
2016-07-08 02:29:09 +03:00
restoreState: false
})
app.setLoginItemSettings({openAtLogin: true, openAsHidden: true})
assert.deepEqual(app.getLoginItemSettings(), {
openAtLogin: true,
2016-07-11 22:30:40 +03:00
openAsHidden: process.platform === 'darwin', // Only available on macOS
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
2016-07-08 02:29:09 +03:00
restoreState: false
})
2016-07-06 23:34:14 +03:00
2016-07-08 02:29:09 +03:00
app.setLoginItemSettings({})
assert.deepEqual(app.getLoginItemSettings(), {
openAtLogin: false,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
2016-07-08 02:29:09 +03:00
restoreState: false
})
2016-07-06 23:34:14 +03:00
})
})
describe('isAccessibilitySupportEnabled API', function () {
it('returns whether the Chrome has accessibility APIs enabled', function () {
2016-07-12 00:48:48 +03:00
assert.equal(typeof app.isAccessibilitySupportEnabled(), 'boolean')
})
})
2016-10-06 19:57:25 +03:00
describe('getPath(name)', function () {
it('returns paths that exist', function () {
assert.equal(fs.existsSync(app.getPath('exe')), true)
assert.equal(fs.existsSync(app.getPath('home')), true)
assert.equal(fs.existsSync(app.getPath('temp')), true)
})
it('throws an error when the name is invalid', function () {
assert.throws(function () {
app.getPath('does-not-exist')
}, /Failed to get 'does-not-exist' path/)
})
it('returns the overridden path', function () {
app.setPath('music', __dirname)
assert.equal(app.getPath('music'), __dirname)
})
})
2016-03-25 22:57:17 +03:00
})