electron/spec/chromium-spec.js

500 lines
14 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
const assert = require('assert')
const http = require('http')
const path = require('path')
const ws = require('ws')
const url = require('url')
2016-03-25 20:03:49 +00:00
const remote = require('electron').remote
2016-01-12 02:40:23 +00:00
2016-06-09 20:37:50 +00:00
const {BrowserWindow, session, webContents} = remote
2016-01-12 02:40:23 +00:00
2016-03-25 20:03:49 +00:00
const isCI = remote.getGlobal('isCi')
2016-03-25 20:03:49 +00:00
describe('chromium feature', function () {
var fixtures = path.resolve(__dirname, 'fixtures')
var listener = null
2016-03-25 20:03:49 +00:00
afterEach(function () {
2016-01-12 02:40:23 +00:00
if (listener != null) {
2016-03-25 20:03:49 +00:00
window.removeEventListener('message', listener)
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
listener = null
})
xdescribe('heap snapshot', function () {
it('does not crash', function () {
process.atomBinding('v8_util').takeHeapSnapshot()
})
})
describe('sending request of http protocol urls', function () {
it('does not crash', function (done) {
this.timeout(5000)
var server = http.createServer(function (req, res) {
res.end()
server.close()
done()
})
server.listen(0, '127.0.0.1', function () {
var port = server.address().port
$.get('http://127.0.0.1:' + port)
})
})
})
describe('document.hidden', function () {
var url = 'file://' + fixtures + '/pages/document-hidden.html'
var w = null
afterEach(function () {
w != null ? w.destroy() : void 0
})
it('is set correctly when window is not shown', function (done) {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
2016-03-25 20:03:49 +00:00
})
2016-08-03 17:50:01 +00:00
w.webContents.once('ipc-message', function (event, args) {
2016-03-25 20:03:49 +00:00
assert.deepEqual(args, ['hidden', true])
done()
})
w.loadURL(url)
})
if (isCI && process.platform === 'win32') {
return
}
2016-03-25 20:03:49 +00:00
it('is set correctly when window is inactive', function (done) {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
2016-03-25 20:03:49 +00:00
})
2016-08-03 17:50:01 +00:00
w.webContents.once('ipc-message', function (event, args) {
2016-03-25 20:03:49 +00:00
assert.deepEqual(args, ['hidden', false])
done()
})
w.showInactive()
w.loadURL(url)
})
})
xdescribe('navigator.webkitGetUserMedia', function () {
it('calls its callbacks', function (done) {
this.timeout(5000)
2016-02-17 01:39:11 +00:00
navigator.webkitGetUserMedia({
2016-01-12 02:40:23 +00:00
audio: true,
video: false
2016-03-25 20:03:49 +00:00
}, function () {
done()
}, function () {
done()
})
})
})
describe('navigator.mediaDevices', function () {
if (process.env.TRAVIS === 'true') {
2016-03-25 20:03:49 +00:00
return
}
if (isCI && process.platform === 'linux') {
2016-03-25 20:03:49 +00:00
return
}
2016-04-30 09:21:18 +00:00
if (isCI && process.platform === 'win32') {
return
}
2016-03-25 20:03:49 +00:00
it('can return labels of enumerated devices', function (done) {
navigator.mediaDevices.enumerateDevices().then((devices) => {
2016-03-25 20:03:49 +00:00
const labels = devices.map((device) => device.label)
const labelFound = labels.some((label) => !!label)
2016-03-28 23:19:18 +00:00
if (labelFound) {
2016-03-25 20:03:49 +00:00
done()
2016-03-28 23:19:18 +00:00
} else {
2016-03-25 20:03:49 +00:00
done('No device labels found: ' + JSON.stringify(labels))
2016-03-28 23:19:18 +00:00
}
2016-03-25 20:03:49 +00:00
}).catch(done)
})
})
describe('navigator.language', function () {
it('should not be empty', function () {
assert.notEqual(navigator.language, '')
})
})
describe('navigator.serviceWorker', function () {
var url = 'file://' + fixtures + '/pages/service-worker/index.html'
var w = null
afterEach(function () {
w != null ? w.destroy() : void 0
})
it('should register for file scheme', function (done) {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
2016-03-25 20:03:49 +00:00
})
w.webContents.on('ipc-message', function (event, args) {
2016-01-12 02:40:23 +00:00
if (args[0] === 'reload') {
2016-03-25 20:03:49 +00:00
w.webContents.reload()
2016-01-12 02:40:23 +00:00
} else if (args[0] === 'error') {
2016-03-25 20:03:49 +00:00
done('unexpected error : ' + args[1])
2016-01-12 02:40:23 +00:00
} else if (args[0] === 'response') {
2016-03-25 20:03:49 +00:00
assert.equal(args[1], 'Hello from serviceWorker!')
2016-02-17 01:39:11 +00:00
session.defaultSession.clearStorageData({
2016-01-12 02:40:23 +00:00
storages: ['serviceworkers']
2016-03-25 20:03:49 +00:00
}, function () {
done()
})
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
})
w.loadURL(url)
})
})
describe('window.open', function () {
if (process.env.TRAVIS === 'true' && process.platform === 'darwin') {
return
}
2016-03-25 20:03:49 +00:00
this.timeout(20000)
it('returns a BrowserWindowProxy object', function () {
var b = window.open('about:blank', '', 'show=no')
assert.equal(b.closed, false)
assert.equal(b.constructor.name, 'BrowserWindowProxy')
2016-06-09 20:45:05 +00:00
// Check that guestId is not writeable
assert(b.guestId)
b.guestId = 'anotherValue'
assert.notEqual(b.guestId, 'anoterValue')
2016-03-25 20:03:49 +00:00
b.close()
})
it('accepts "nodeIntegration" as feature', function (done) {
var b
listener = function (event) {
assert.equal(event.data.isProcessGlobalUndefined, true)
2016-03-25 20:03:49 +00:00
b.close()
done()
}
window.addEventListener('message', listener)
b = window.open('file://' + fixtures + '/pages/window-opener-node.html', '', 'nodeIntegration=no,show=no')
})
it('inherit options of parent window', function (done) {
var b
listener = function (event) {
2016-03-28 23:19:18 +00:00
var ref1 = remote.getCurrentWindow().getSize()
var width = ref1[0]
var height = ref1[1]
2016-03-25 20:03:49 +00:00
assert.equal(event.data, 'size: ' + width + ' ' + height)
b.close()
done()
}
window.addEventListener('message', listener)
b = window.open('file://' + fixtures + '/pages/window-open-size.html', '', 'show=no')
})
it('disables node integration when it is disabled on the parent window', function (done) {
var b
listener = function (event) {
assert.equal(event.data.isProcessGlobalUndefined, true)
b.close()
done()
}
window.addEventListener('message', listener)
var windowUrl = require('url').format({
2016-03-24 00:40:25 +00:00
pathname: `${fixtures}/pages/window-opener-no-node-integration.html`,
protocol: 'file',
query: {
2016-03-24 00:40:25 +00:00
p: `${fixtures}/pages/window-opener-node.html`
},
slashes: true
})
2016-03-24 21:12:46 +00:00
b = window.open(windowUrl, '', 'nodeIntegration=no,show=no')
})
2016-03-25 20:03:49 +00:00
it('does not override child options', function (done) {
var b, size
2016-01-12 02:40:23 +00:00
size = {
width: 350,
height: 450
2016-03-25 20:03:49 +00:00
}
listener = function (event) {
assert.equal(event.data, 'size: ' + size.width + ' ' + size.height)
b.close()
done()
}
window.addEventListener('message', listener)
b = window.open('file://' + fixtures + '/pages/window-open-size.html', '', 'show=no,width=' + size.width + ',height=' + size.height)
})
it('defines a window.location getter', function (done) {
var b, targetURL
2016-06-29 16:37:10 +00:00
if (process.platform === 'win32') {
targetURL = 'file:///' + fixtures.replace(/\\/g, '/') + '/pages/base-page.html'
2016-06-29 16:37:10 +00:00
} else {
targetURL = 'file://' + fixtures + '/pages/base-page.html'
2016-06-29 16:37:10 +00:00
}
2016-03-25 20:03:49 +00:00
b = window.open(targetURL)
2016-06-09 20:37:50 +00:00
webContents.fromId(b.guestId).once('did-finish-load', function () {
2016-03-25 20:03:49 +00:00
assert.equal(b.location, targetURL)
b.close()
done()
})
})
it('defines a window.location setter', function (done) {
// Load a page that definitely won't redirect
2016-03-25 20:03:49 +00:00
var b
b = window.open('about:blank')
2016-06-09 20:37:50 +00:00
webContents.fromId(b.guestId).once('did-finish-load', function () {
// When it loads, redirect
2016-03-25 20:03:49 +00:00
b.location = 'file://' + fixtures + '/pages/base-page.html'
2016-06-09 20:37:50 +00:00
webContents.fromId(b.guestId).once('did-finish-load', function () {
// After our second redirect, cleanup and callback
2016-03-25 20:03:49 +00:00
b.close()
done()
})
})
})
})
2016-03-25 20:03:49 +00:00
describe('window.opener', function () {
this.timeout(10000)
2016-02-17 01:39:11 +00:00
2016-03-25 20:03:49 +00:00
var url = 'file://' + fixtures + '/pages/window-opener.html'
var w = null
2016-02-17 01:39:11 +00:00
2016-03-25 20:03:49 +00:00
afterEach(function () {
w != null ? w.destroy() : void 0
})
2016-02-17 01:39:11 +00:00
2016-03-25 20:03:49 +00:00
it('is null for main window', function (done) {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
2016-03-25 20:03:49 +00:00
})
2016-08-03 17:50:01 +00:00
w.webContents.once('ipc-message', function (event, args) {
2016-03-25 20:03:49 +00:00
assert.deepEqual(args, ['opener', null])
done()
})
w.loadURL(url)
})
it('is not null for window opened by window.open', function (done) {
var b
listener = function (event) {
assert.equal(event.data, 'object')
b.close()
done()
}
window.addEventListener('message', listener)
b = window.open(url, '', 'show=no')
})
})
describe('window.postMessage', function () {
it('sets the source and origin correctly', function (done) {
var b, sourceId
sourceId = remote.getCurrentWindow().id
listener = function (event) {
window.removeEventListener('message', listener)
b.close()
var message = JSON.parse(event.data)
assert.equal(message.data, 'testing')
assert.equal(message.origin, 'file://')
assert.equal(message.sourceEqualsOpener, true)
assert.equal(message.sourceId, sourceId)
assert.equal(event.origin, 'file://')
done()
}
window.addEventListener('message', listener)
b = window.open('file://' + fixtures + '/pages/window-open-postMessage.html', '', 'show=no')
2016-06-09 20:37:50 +00:00
webContents.fromId(b.guestId).once('did-finish-load', function () {
2016-03-25 20:03:49 +00:00
b.postMessage('testing', '*')
})
})
})
describe('window.opener.postMessage', function () {
it('sets source and origin correctly', function (done) {
var b
listener = function (event) {
window.removeEventListener('message', listener)
b.close()
assert.equal(event.source, b)
assert.equal(event.origin, 'file://')
done()
}
window.addEventListener('message', listener)
b = window.open('file://' + fixtures + '/pages/window-opener-postMessage.html', '', 'show=no')
})
2016-06-09 19:06:49 +00:00
it('supports windows opened from a <webview>', function (done) {
const webview = new WebView()
webview.addEventListener('console-message', function (e) {
webview.remove()
assert.equal(e.message, 'message')
done()
})
webview.allowpopups = true
webview.src = url.format({
pathname: `${fixtures}/pages/webview-opener-postMessage.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-postMessage.html`
},
slashes: true
})
document.body.appendChild(webview)
})
2016-03-25 20:03:49 +00:00
})
describe('creating a Uint8Array under browser side', function () {
it('does not crash', function () {
var RUint8Array = remote.getGlobal('Uint8Array')
2016-03-28 23:11:00 +00:00
var arr = new RUint8Array()
assert(arr)
2016-03-25 20:03:49 +00:00
})
})
describe('webgl', function () {
2016-04-30 09:21:18 +00:00
if (isCI && process.platform === 'win32') {
return
}
2016-03-25 20:03:49 +00:00
it('can be get as context in canvas', function () {
2016-03-28 23:11:00 +00:00
if (process.platform === 'linux') return
2016-03-25 20:03:49 +00:00
var webgl = document.createElement('canvas').getContext('webgl')
assert.notEqual(webgl, null)
})
})
describe('web workers', function () {
it('Worker can work', function (done) {
var worker = new Worker('../fixtures/workers/worker.js')
var message = 'ping'
worker.onmessage = function (event) {
assert.equal(event.data, message)
worker.terminate()
done()
}
worker.postMessage(message)
})
it('SharedWorker can work', function (done) {
var worker = new SharedWorker('../fixtures/workers/shared_worker.js')
var message = 'ping'
worker.port.onmessage = function (event) {
assert.equal(event.data, message)
done()
}
worker.port.postMessage(message)
})
})
describe('iframe', function () {
var iframe = null
beforeEach(function () {
iframe = document.createElement('iframe')
})
afterEach(function () {
document.body.removeChild(iframe)
})
it('does not have node integration', function (done) {
iframe.src = 'file://' + fixtures + '/pages/set-global.html'
document.body.appendChild(iframe)
iframe.onload = function () {
assert.equal(iframe.contentWindow.test, 'undefined undefined undefined')
done()
}
})
})
describe('storage', function () {
it('requesting persitent quota works', function (done) {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024, function (grantedBytes) {
assert.equal(grantedBytes, 1048576)
done()
})
})
})
describe('websockets', function () {
var wss = null
var server = null
var WebSocketServer = ws.Server
afterEach(function () {
wss.close()
server.close()
})
it('has user agent', function (done) {
server = http.createServer()
server.listen(0, '127.0.0.1', function () {
var port = server.address().port
2016-01-12 02:40:23 +00:00
wss = new WebSocketServer({
server: server
2016-03-25 20:03:49 +00:00
})
wss.on('error', done)
wss.on('connection', function (ws) {
2016-01-12 02:40:23 +00:00
if (ws.upgradeReq.headers['user-agent']) {
2016-03-25 20:03:49 +00:00
done()
2016-01-12 02:40:23 +00:00
} else {
2016-03-25 20:03:49 +00:00
done('user agent is empty')
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
})
var socket = new WebSocket(`ws://127.0.0.1:${port}`)
2016-03-28 23:11:00 +00:00
assert(socket)
2016-03-25 20:03:49 +00:00
})
})
})
describe('Promise', function () {
it('resolves correctly in Node.js calls', function (done) {
2016-01-12 02:40:23 +00:00
document.registerElement('x-element', {
prototype: Object.create(HTMLElement.prototype, {
createdCallback: {
2016-03-25 20:03:49 +00:00
value: function () {}
2016-01-12 02:40:23 +00:00
}
})
2016-03-25 20:03:49 +00:00
})
setImmediate(function () {
var called = false
Promise.resolve().then(function () {
done(called ? void 0 : new Error('wrong sequence'))
})
document.createElement('x-element')
called = true
})
})
it('resolves correctly in Electron calls', function (done) {
2016-01-12 02:40:23 +00:00
document.registerElement('y-element', {
prototype: Object.create(HTMLElement.prototype, {
createdCallback: {
2016-03-25 20:03:49 +00:00
value: function () {}
2016-01-12 02:40:23 +00:00
}
})
2016-03-25 20:03:49 +00:00
})
remote.getGlobal('setImmediate')(function () {
var called = false
Promise.resolve().then(function () {
done(called ? void 0 : new Error('wrong sequence'))
})
document.createElement('y-element')
called = true
})
})
})
})