electron/spec/api-app-spec.js

202 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
const assert = require('assert')
const ChildProcess = require('child_process')
2016-04-18 16:23:44 +00:00
const https = require('https')
const fs = require('fs')
2016-03-25 19:57:17 +00:00
const path = require('path')
const remote = require('electron').remote
const app = remote.require('electron').app
const BrowserWindow = remote.require('electron').BrowserWindow
describe('electron module', function () {
it('allows old style require by default', function () {
require('shell')
})
it('can prevent exposing internal modules to require', function (done) {
const electron = require('electron')
const clipboard = require('clipboard')
assert.equal(typeof clipboard, 'object')
electron.hideInternalModules()
2016-01-27 08:44:10 +00:00
try {
2016-03-25 19:57:17 +00:00
require('clipboard')
2016-03-29 00:35:49 +00:00
} catch (err) {
2016-03-25 19:57:17 +00:00
assert.equal(err.message, "Cannot find module 'clipboard'")
done()
2016-01-27 08:44:10 +00:00
}
2016-03-25 19:57:17 +00: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 () {
appProcess != null ? appProcess.kill() : void 0
})
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 02:40:23 +00:00
if (process.platform !== 'win32') {
2016-03-25 19:57:17 +00:00
assert.notEqual(output.indexOf('Exit event with code: 123'), -1)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
assert.equal(code, 123)
done()
})
})
})
2016-04-18 16:23:44 +00:00
describe('app.importClientCertificate', function () {
if (process.platform !== 'linux')
return
this.timeout(5000)
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) {
res.writeHead(200);
res.end('authorized');
}
})
afterEach(function () {
if (w != null) {
w.destroy()
}
w = null
})
it('can import certificate into platform cert store', function (done) {
let options = {
clientCertificate: path.join(certPath, 'client.p12'),
password: 'electron'
}
w = new BrowserWindow({
show: false
})
w.webContents.on('did-finish-load', function () {
server.close()
done()
})
2016-04-18 17:27:37 +00:00
app.on('select-client-certificate', function (event, webContents, url, list, callback) {
assert.equal(list.length, 1)
assert.equal(list[0].issuerName, 'Intermediate CA')
callback(list[0])
})
2016-04-18 16:23:44 +00:00
app.importClientCertificate(options, function (result) {
assert(!result)
2016-04-18 17:27:37 +00: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 16:23:44 +00:00
})
})
})
2016-03-25 19:57:17 +00:00
describe('BrowserWindow events', function () {
var w = null
2016-03-25 19:57:17 +00:00
afterEach(function () {
2016-01-12 02:40:23 +00:00
if (w != null) {
2016-03-25 19:57:17 +00:00
w.destroy()
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
w = null
})
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 02:40:23 +00:00
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', function (done) {
app.once('browser-window-blur', function (e, window) {
assert.equal(w.id, window.id)
done()
})
2016-01-12 02:40:23 +00:00
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', function (done) {
app.once('browser-window-created', function (e, window) {
setImmediate(function () {
assert.equal(w.id, window.id)
done()
})
})
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
2016-03-25 19:57:17 +00:00
})
w.emit('blur')
})
})
})