electron/spec/chromium-spec.js

1161 lines
36 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
const assert = require('assert')
const fs = require('fs')
2016-03-25 20:03:49 +00:00
const http = require('http')
const path = require('path')
const ws = require('ws')
const url = require('url')
2017-02-04 14:48:16 +00:00
const {ipcRenderer, remote, webFrame} = require('electron')
2016-12-03 18:52:57 +00:00
const {closeWindow} = require('./window-helpers')
2016-01-12 02:40:23 +00:00
const {app, BrowserWindow, ipcMain, protocol, 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
2017-04-18 23:04:27 +00:00
let w = 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
})
2017-04-18 23:04:27 +00:00
afterEach(function () {
return closeWindow(w).then(function () { w = null })
})
2017-03-14 20:32:42 +00:00
describe('heap snapshot', function () {
2016-03-25 20:03:49 +00:00
it('does not crash', function () {
2017-03-14 20:32:42 +00:00
if (process.env.TRAVIS === 'true') return
2016-03-25 20:03:49 +00:00
process.atomBinding('v8_util').takeHeapSnapshot()
})
})
describe('sending request of http protocol urls', function () {
it('does not crash', function (done) {
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)
})
})
})
xdescribe('navigator.webkitGetUserMedia', function () {
it('calls its callbacks', function (done) {
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 (isCI) {
2016-04-30 09:21:18 +00:00
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 {
2017-04-18 23:06:10 +00:00
done(new Error(`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)
})
2016-12-03 18:52:57 +00:00
it('can return new device id when cookie storage is cleared', function (done) {
const options = {
origin: null,
storages: ['cookies']
}
2016-12-12 21:33:14 +00:00
const deviceIds = []
2016-12-03 18:52:57 +00:00
const ses = session.fromPartition('persist:media-device-id')
2017-04-18 23:04:27 +00:00
w = new BrowserWindow({
2016-12-03 18:52:57 +00:00
show: false,
webPreferences: {
session: ses
}
})
w.webContents.on('ipc-message', function (event, args) {
if (args[0] === 'deviceIds') {
deviceIds.push(args[1])
}
if (deviceIds.length === 2) {
assert.notDeepEqual(deviceIds[0], deviceIds[1])
closeWindow(w).then(function () {
w = null
done()
2016-12-12 21:32:27 +00:00
}).catch(function (error) {
done(error)
2016-12-03 18:52:57 +00:00
})
} else {
ses.clearStorageData(options, function () {
w.webContents.reload()
})
}
})
w.loadURL('file://' + fixtures + '/pages/media-id-reset.html')
})
2016-03-25 20:03:49 +00:00
})
describe('navigator.language', function () {
it('should not be empty', function () {
assert.notEqual(navigator.language, '')
})
})
describe('navigator.serviceWorker', function () {
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(`file://${fixtures}/pages/service-worker/index.html`)
})
it('should register for intercepted file scheme', function (done) {
const customSession = session.fromPartition('intercept-file')
customSession.protocol.interceptBufferProtocol('file', function (request, callback) {
let file = url.parse(request.url).pathname
// Remove leading slash before drive letter on Windows
if (file[0] === '/' && process.platform === 'win32') {
file = file.slice(1)
}
const content = fs.readFileSync(path.normalize(file))
const ext = path.extname(file)
let type = 'text/html'
if (ext === '.js') {
type = 'application/javascript'
}
callback({data: content, mimeType: type})
}, function (error) {
if (error) done(error)
})
w = new BrowserWindow({
show: false,
webPreferences: {
session: customSession
}
})
w.webContents.on('ipc-message', function (event, args) {
if (args[0] === 'reload') {
w.webContents.reload()
} else if (args[0] === 'error') {
done('unexpected error : ' + args[1])
} else if (args[0] === 'response') {
assert.equal(args[1], 'Hello from serviceWorker!')
customSession.clearStorageData({
storages: ['serviceworkers']
}, function () {
customSession.protocol.uninterceptProtocol('file', (error) => done(error))
})
}
})
w.loadURL(`file://${fixtures}/pages/service-worker/index.html`)
2016-03-25 20:03:49 +00:00
})
})
describe('window.open', function () {
if (process.env.TRAVIS === 'true' && process.platform === 'darwin') {
return
}
2016-03-25 20:03:49 +00:00
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')
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')
})
it('disables node integration when it is disabled on the parent window for chrome devtools URLs', function (done) {
var b
app.once('web-contents-created', (event, contents) => {
contents.once('did-finish-load', () => {
contents.executeJavaScript('typeof process').then((typeofProcessGlobal) => {
assert.equal(typeofProcessGlobal, 'undefined')
b.close()
done()
}).catch(done)
})
})
b = window.open('chrome-devtools://devtools/bundled/inspector.html', '', 'nodeIntegration=no,show=no')
})
it('disables JavaScript when it is disabled on the parent window', function (done) {
var b
app.once('web-contents-created', (event, contents) => {
contents.once('did-finish-load', () => {
app.once('browser-window-created', (event, window) => {
const preferences = window.webContents.getWebPreferences()
assert.equal(preferences.javascript, false)
window.destroy()
b.close()
done()
})
// Click link on page
contents.sendInputEvent({type: 'mouseDown', clickCount: 1, x: 1, y: 1})
contents.sendInputEvent({type: 'mouseUp', clickCount: 1, x: 1, y: 1})
})
})
var windowUrl = require('url').format({
pathname: `${fixtures}/pages/window-no-javascript.html`,
protocol: 'file',
slashes: true
})
b = window.open(windowUrl, '', 'javascript=no,show=no')
})
it('disables the <webview> tag when it is disabled on the parent window', function (done) {
let b
listener = function (event) {
assert.equal(event.data.isWebViewGlobalUndefined, true)
b.close()
done()
}
window.addEventListener('message', listener)
var windowUrl = require('url').format({
pathname: `${fixtures}/pages/window-opener-no-webview-tag.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-webview.html`
},
slashes: true
})
b = window.open(windowUrl, '', 'webviewTag=no,nodeIntegration=yes,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)
})
2017-01-04 22:44:14 +00:00
it('handles cycles when merging the parent options into the child options', (done) => {
w = BrowserWindow.fromId(ipcRenderer.sendSync('create-window-with-options-cycle'))
w.loadURL('file://' + fixtures + '/pages/window-open.html')
2017-01-04 22:57:51 +00:00
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
assert.equal(options.show, false)
assert.deepEqual(options.foo, {
bar: null,
baz: {
hello: {
world: true
}
2017-01-04 22:54:18 +00:00
},
baz2: {
hello: {
world: true
}
2017-01-04 22:44:14 +00:00
}
})
done()
})
})
2016-03-25 20:03:49 +00:00
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
}
app.once('browser-window-created', (event, window) => {
window.webContents.once('did-finish-load', () => {
assert.equal(b.location, targetURL)
b.close()
done()
})
2016-03-25 20:03:49 +00:00
})
b = window.open(targetURL)
2016-03-25 20:03:49 +00:00
})
it('defines a window.location setter', function (done) {
2017-01-12 21:19:27 +00:00
let b
app.once('browser-window-created', (event, {webContents}) => {
webContents.once('did-finish-load', function () {
// When it loads, redirect
b.location = 'file://' + fixtures + '/pages/base-page.html'
webContents.once('did-finish-load', function () {
// After our second redirect, cleanup and callback
b.close()
done()
})
2016-03-25 20:03:49 +00:00
})
})
2017-01-12 21:19:27 +00:00
// Load a page that definitely won't redirect
b = window.open('about:blank')
2016-03-25 20:03:49 +00:00
})
it('open a blank page when no URL is specified', function (done) {
2017-01-12 21:19:27 +00:00
let b
app.once('browser-window-created', (event, {webContents}) => {
webContents.once('did-finish-load', function () {
const {location} = b
b.close()
assert.equal(location, 'about:blank')
2017-01-12 21:19:27 +00:00
let c
app.once('browser-window-created', (event, {webContents}) => {
webContents.once('did-finish-load', function () {
const {location} = c
c.close()
assert.equal(location, 'about:blank')
done()
})
})
c = window.open('')
})
})
2017-01-12 21:19:27 +00:00
b = window.open()
})
it('throws an exception when the arguments cannot be converted to strings', function () {
assert.throws(function () {
window.open('', {toString: null})
}, /Cannot convert object to primitive value/)
assert.throws(function () {
window.open('', '', {toString: 3})
}, /Cannot convert object to primitive value/)
})
it('sets the window title to the specified frameName', function (done) {
let b
app.once('browser-window-created', (event, createdWindow) => {
assert.equal(createdWindow.getTitle(), 'hello')
b.close()
done()
})
b = window.open('', 'hello')
})
it('does not throw an exception when the frameName is a built-in object property', function (done) {
let b
app.once('browser-window-created', (event, createdWindow) => {
assert.equal(createdWindow.getTitle(), '__proto__')
b.close()
done()
})
b = window.open('', '__proto__')
})
it('does not throw an exception when the features include webPreferences', function () {
let b
assert.doesNotThrow(function () {
b = window.open('', '', 'webPreferences=')
})
b.close()
})
2016-03-25 20:03:49 +00:00
})
2016-03-25 20:03:49 +00:00
describe('window.opener', function () {
let url = 'file://' + fixtures + '/pages/window-opener.html'
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) {
let b
2016-03-25 20:03:49 +00:00
listener = function (event) {
assert.equal(event.data, 'object')
b.close()
done()
}
window.addEventListener('message', listener)
b = window.open(url, '', 'show=no')
})
})
describe('window.opener access from BrowserWindow', function () {
2016-11-16 16:03:07 +00:00
const scheme = 'other'
let url = `${scheme}://${fixtures}/pages/window-opener-location.html`
let w = null
2016-11-16 00:57:12 +00:00
before(function (done) {
protocol.registerFileProtocol(scheme, function (request, callback) {
callback(`${fixtures}/pages/window-opener-location.html`)
}, function (error) {
done(error)
})
})
2016-11-16 00:57:12 +00:00
after(function () {
protocol.unregisterProtocol(scheme)
})
afterEach(function () {
w.close()
})
it('does nothing when origin of current window does not match opener', function (done) {
listener = function (event) {
assert.equal(event.data, undefined)
done()
}
window.addEventListener('message', listener)
w = window.open(url, '', 'show=no')
})
2016-11-25 17:47:28 +00:00
it('works when origin matches', function (done) {
listener = function (event) {
assert.equal(event.data, location.href)
done()
}
window.addEventListener('message', listener)
w = window.open(`file://${fixtures}/pages/window-opener-location.html`, '', 'show=no')
})
it('works when origin does not match opener but has node integration', function (done) {
listener = function (event) {
assert.equal(event.data, location.href)
done()
}
window.addEventListener('message', listener)
w = window.open(url, '', 'show=no,nodeIntegration=yes')
})
})
describe('window.opener access from <webview>', function () {
const scheme = 'other'
const srcPath = `${fixtures}/pages/webview-opener-postMessage.html`
const pageURL = `file://${fixtures}/pages/window-opener-location.html`
let webview = null
before(function (done) {
protocol.registerFileProtocol(scheme, function (request, callback) {
callback(srcPath)
}, function (error) {
done(error)
})
})
after(function () {
protocol.unregisterProtocol(scheme)
})
afterEach(function () {
if (webview != null) webview.remove()
})
it('does nothing when origin of webview src URL does not match opener', function (done) {
webview = new WebView()
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, 'null')
done()
})
webview.setAttribute('allowpopups', 'on')
webview.src = url.format({
pathname: srcPath,
protocol: scheme,
query: {
p: pageURL
},
slashes: true
})
document.body.appendChild(webview)
})
2016-11-25 17:47:28 +00:00
it('works when origin matches', function (done) {
webview = new WebView()
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, webview.src)
done()
})
webview.setAttribute('allowpopups', 'on')
webview.src = url.format({
pathname: srcPath,
protocol: 'file',
query: {
p: pageURL
},
slashes: true
})
document.body.appendChild(webview)
})
it('works when origin does not match opener but has node integration', function (done) {
webview = new WebView()
webview.addEventListener('console-message', function (e) {
webview.remove()
assert.equal(e.message, webview.src)
done()
})
webview.setAttribute('allowpopups', 'on')
webview.setAttribute('nodeintegration', 'on')
webview.src = url.format({
pathname: srcPath,
protocol: scheme,
query: {
p: pageURL
},
slashes: true
})
document.body.appendChild(webview)
})
})
2016-03-25 20:03:49 +00:00
describe('window.postMessage', function () {
it('sets the source and origin correctly', function (done) {
2017-01-12 21:19:27 +00:00
var b
2016-03-25 20:03:49 +00:00
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(event.origin, 'file://')
done()
}
window.addEventListener('message', listener)
2017-01-12 21:19:27 +00:00
app.once('browser-window-created', (event, {webContents}) => {
webContents.once('did-finish-load', function () {
b.postMessage('testing', '*')
})
2016-03-25 20:03:49 +00:00
})
2017-01-12 21:19:27 +00:00
b = window.open('file://' + fixtures + '/pages/window-open-postMessage.html', '', 'show=no')
2016-03-25 20:03:49 +00:00
})
it('throws an exception when the targetOrigin cannot be converted to a string', function () {
var b = window.open('')
assert.throws(function () {
b.postMessage('test', {toString: null})
}, /Cannot convert object to primitive value/)
b.close()
})
2016-03-25 20:03:49 +00:00
})
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)
})
describe('targetOrigin argument', function () {
let serverURL
let server
beforeEach(function (done) {
server = http.createServer(function (req, res) {
res.writeHead(200)
const filePath = path.join(fixtures, 'pages', 'window-opener-targetOrigin.html')
res.end(fs.readFileSync(filePath, 'utf8'))
})
server.listen(0, '127.0.0.1', function () {
serverURL = `http://127.0.0.1:${server.address().port}`
done()
})
})
afterEach(function () {
server.close()
})
it('delivers messages that match the origin', function (done) {
let b
listener = function (event) {
window.removeEventListener('message', listener)
b.close()
2017-04-26 18:03:11 +00:00
assert.equal(event.data, 'deliver')
done()
}
window.addEventListener('message', listener)
b = window.open(serverURL, '', 'show=no')
})
})
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)
})
2017-03-15 11:07:28 +00:00
it('Worker has no node integration by default', function (done) {
let worker = new Worker('../fixtures/workers/worker_node.js')
worker.onmessage = function (event) {
assert.equal(event.data, 'undefined undefined undefined undefined')
worker.terminate()
done()
}
})
it('Worker has node integration with nodeIntegrationInWorker', function (done) {
2017-03-16 10:51:43 +00:00
let webview = new WebView()
2017-03-15 11:07:28 +00:00
webview.addEventListener('ipc-message', function (e) {
assert.equal(e.channel, 'object function object function')
webview.remove()
done()
})
webview.src = 'file://' + fixtures + '/pages/worker.html'
webview.setAttribute('webpreferences', 'nodeIntegration, nodeIntegrationInWorker')
document.body.appendChild(webview)
})
2016-03-25 20:03:49 +00:00
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)
})
2017-03-15 11:07:28 +00:00
it('SharedWorker has no node integration by default', function (done) {
let worker = new SharedWorker('../fixtures/workers/shared_worker_node.js')
worker.port.onmessage = function (event) {
assert.equal(event.data, 'undefined undefined undefined undefined')
done()
}
})
it('SharedWorker has node integration with nodeIntegrationInWorker', function (done) {
2017-03-16 10:51:43 +00:00
let webview = new WebView()
2017-03-15 11:07:28 +00:00
webview.addEventListener('console-message', function (e) {
console.log(e)
})
webview.addEventListener('ipc-message', function (e) {
assert.equal(e.channel, 'object function object function')
webview.remove()
done()
})
webview.src = 'file://' + fixtures + '/pages/shared_worker.html'
webview.setAttribute('webpreferences', 'nodeIntegration, nodeIntegrationInWorker')
document.body.appendChild(webview)
})
2016-03-25 20:03:49 +00:00
})
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()
})
})
2016-09-21 18:35:37 +00:00
describe('custom non standard schemes', function () {
const protocolName = 'storage'
let contents = null
before(function (done) {
const handler = function (request, callback) {
let parsedUrl = url.parse(request.url)
let filename
switch (parsedUrl.pathname) {
case '/localStorage' : filename = 'local_storage.html'; break
case '/sessionStorage' : filename = 'session_storage.html'; break
case '/WebSQL' : filename = 'web_sql.html'; break
case '/indexedDB' : filename = 'indexed_db.html'; break
case '/cookie' : filename = 'cookie.html'; break
default : filename = ''
}
callback({path: fixtures + '/pages/storage/' + filename})
}
protocol.registerFileProtocol(protocolName, handler, function (error) {
done(error)
})
})
after(function (done) {
protocol.unregisterProtocol(protocolName, () => done())
})
beforeEach(function () {
contents = webContents.create({})
})
afterEach(function () {
contents.destroy()
contents = null
})
it('cannot access localStorage', function (done) {
ipcMain.once('local-storage-response', function (event, error) {
assert.equal(
error,
'Failed to read the \'localStorage\' property from \'Window\': Access is denied for this document.')
done()
})
contents.loadURL(protocolName + '://host/localStorage')
})
it('cannot access sessionStorage', function (done) {
ipcMain.once('session-storage-response', function (event, error) {
assert.equal(
error,
'Failed to read the \'sessionStorage\' property from \'Window\': Access is denied for this document.')
done()
})
contents.loadURL(protocolName + '://host/sessionStorage')
})
it('cannot access WebSQL database', function (done) {
ipcMain.once('web-sql-response', function (event, error) {
assert.equal(
error,
'An attempt was made to break through the security policy of the user agent.')
done()
})
contents.loadURL(protocolName + '://host/WebSQL')
})
it('cannot access indexedDB', function (done) {
ipcMain.once('indexed-db-response', function (event, error) {
assert.equal(error, 'The user denied permission to access the database.')
done()
})
contents.loadURL(protocolName + '://host/indexedDB')
})
it('cannot access cookie', function (done) {
ipcMain.once('cookie-response', function (event, cookie) {
assert(!cookie)
done()
})
contents.loadURL(protocolName + '://host/cookie')
})
})
2016-03-25 20:03:49 +00:00
})
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
})
})
})
2016-08-22 10:26:07 +00:00
describe('fetch', function () {
it('does not crash', function (done) {
const server = http.createServer(function (req, res) {
res.end('test')
server.close()
})
server.listen(0, '127.0.0.1', function () {
const port = server.address().port
fetch(`http://127.0.0.1:${port}`).then((res) => {
return res.body.getReader()
}).then((reader) => {
reader.read().then((r) => {
reader.cancel()
2016-08-22 10:34:59 +00:00
done()
2016-08-22 10:26:07 +00:00
})
}).catch(function (e) {
2016-08-22 10:34:59 +00:00
done(e)
2016-08-22 10:26:07 +00:00
})
})
})
})
2017-02-04 14:48:16 +00:00
describe('PDF Viewer', function () {
2017-02-14 10:58:42 +00:00
const pdfSource = url.format({
2017-03-10 05:20:26 +00:00
pathname: path.join(fixtures, 'assets', 'cat.pdf').replace(/\\/g, '/'),
2017-02-14 10:58:42 +00:00
protocol: 'file',
slashes: true
})
const pdfSourceWithParams = url.format({
pathname: path.join(fixtures, 'assets', 'cat.pdf').replace(/\\/g, '/'),
query: {
a: 1,
b: 2
},
protocol: 'file',
slashes: true
})
2017-10-19 14:15:02 +00:00
2017-02-04 14:48:16 +00:00
2017-10-19 14:15:02 +00:00
function createBrowserWindow ({plugins,preload}) {
2017-02-04 14:48:16 +00:00
w = new BrowserWindow({
2017-03-11 21:36:08 +00:00
show: false,
webPreferences: {
2017-10-19 14:15:02 +00:00
preload: path.join(fixtures, 'module', preload),
plugins: plugins
2017-03-11 21:36:08 +00:00
}
2017-02-04 14:48:16 +00:00
})
}
2017-10-19 14:15:02 +00:00
function testPDFIsLoadedInSubFrame(page,preloadFile,done) {
const pagePath = url.format({
pathname: path.join(fixtures, 'pages',page ).replace(/\\/g, '/'),
protocol: 'file',
slashes: true
})
createBrowserWindow({plugins: true,preload:preloadFile})
ipcMain.once('pdf-loaded', function (event, state) {
assert.equal(state, 'success')
done()
})
w.webContents.on('page-title-updated', function () {
const parsedURL = url.parse(w.webContents.getURL(), true)
assert.equal(parsedURL.protocol, 'chrome:')
assert.equal(parsedURL.hostname, 'pdf-viewer')
assert.equal(parsedURL.query.src, pagePath)
assert.equal(w.webContents.getTitle(), 'cat.pdf')
})
w.webContents.loadURL(pagePath)
}
2017-02-04 14:48:16 +00:00
it('opens when loading a pdf resource as top level navigation', function (done) {
2017-10-19 14:15:02 +00:00
createBrowserWindow({plugins: true,preload:'preload-pdf-loaded.js'})
2017-06-28 20:01:22 +00:00
ipcMain.once('pdf-loaded', function (event, state) {
assert.equal(state, 'success')
done()
2017-03-11 21:36:08 +00:00
})
w.webContents.on('page-title-updated', function () {
2017-02-04 14:48:16 +00:00
const parsedURL = url.parse(w.webContents.getURL(), true)
assert.equal(parsedURL.protocol, 'chrome:')
assert.equal(parsedURL.hostname, 'pdf-viewer')
assert.equal(parsedURL.query.src, pdfSource)
2017-03-10 05:20:26 +00:00
assert.equal(w.webContents.getTitle(), 'cat.pdf')
2017-02-04 14:48:16 +00:00
})
w.webContents.loadURL(pdfSource)
})
it('opens a pdf link given params, the query string should be escaped', function (done) {
2017-10-19 14:15:02 +00:00
createBrowserWindow({plugins: true,preload:'preload-pdf-loaded.js'})
ipcMain.once('pdf-loaded', function (event, state) {
assert.equal(state, 'success')
done()
})
w.webContents.on('page-title-updated', function () {
const parsedURL = url.parse(w.webContents.getURL(), true)
assert.equal(parsedURL.protocol, 'chrome:')
assert.equal(parsedURL.hostname, 'pdf-viewer')
assert.equal(parsedURL.query.src, pdfSourceWithParams)
assert.equal(parsedURL.query.b, undefined)
assert.equal(parsedURL.search, `?src=${pdfSource}%3Fa%3D1%26b%3D2`)
assert.equal(w.webContents.getTitle(), 'cat.pdf')
})
w.webContents.loadURL(pdfSourceWithParams)
})
it('should download a pdf when plugins are disabled', function (done) {
2017-10-19 14:15:02 +00:00
createBrowserWindow({plugins: false,preload:'preload-pdf-loaded.js'})
ipcRenderer.sendSync('set-download-option', false, false)
2017-05-22 07:08:47 +00:00
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
assert.equal(state, 'completed')
assert.equal(filename, 'cat.pdf')
assert.equal(mimeType, 'application/pdf')
fs.unlinkSync(path.join(fixtures, 'mock.pdf'))
done()
})
w.webContents.loadURL(pdfSource)
})
2017-02-04 14:48:16 +00:00
it('should not open when pdf is requested as sub resource', function (done) {
2017-10-19 14:15:02 +00:00
createBrowserWindow({plugins: true,preload:'preload-pdf-loaded.js'})
2017-02-04 14:48:16 +00:00
webFrame.registerURLSchemeAsPrivileged('file', {
secure: false,
bypassCSP: false,
allowServiceWorkers: false,
corsEnabled: false
})
fetch(pdfSource).then(function (res) {
assert.equal(res.status, 200)
2017-03-10 05:20:26 +00:00
assert.notEqual(document.title, 'cat.pdf')
2017-02-04 14:48:16 +00:00
done()
}).catch(function (e) {
done(e)
})
})
2017-10-19 14:15:02 +00:00
it('opens when loading a pdf resource in a iframe', function (done) {
testPDFIsLoadedInSubFrame('pdf-in-iframe.html','preload-pdf-loaded-in-subframe.js',done)
})
it('opens when loading a pdf resource in a nested iframe', function (done) {
testPDFIsLoadedInSubFrame('pdf-in-nested-iframe.html','preload-pdf-loaded-in-nested-subframe.js',done)
})
2017-02-04 14:48:16 +00:00
})
describe('window.alert(message, title)', function () {
it('throws an exception when the arguments cannot be converted to strings', function () {
assert.throws(function () {
window.alert({toString: null})
}, /Cannot convert object to primitive value/)
assert.throws(function () {
window.alert('message', {toString: 3})
}, /Cannot convert object to primitive value/)
})
})
describe('window.confirm(message, title)', function () {
it('throws an exception when the arguments cannot be converted to strings', function () {
assert.throws(function () {
window.confirm({toString: null}, 'title')
}, /Cannot convert object to primitive value/)
assert.throws(function () {
window.confirm('message', {toString: 3})
}, /Cannot convert object to primitive value/)
})
})
describe('window.history', function () {
describe('window.history.go(offset)', function () {
it('throws an exception when the argumnet cannot be converted to a string', function () {
assert.throws(function () {
window.history.go({toString: null})
}, /Cannot convert object to primitive value/)
})
})
describe('window.history.pushState', function () {
it('should push state after calling history.pushState() from the same url', (done) => {
w = new BrowserWindow({
show: false
})
w.webContents.once('did-finish-load', () => {
// History should have current page by now.
assert.equal(w.webContents.length(), 1)
w.webContents.executeJavaScript('window.history.pushState({}, "")', () => {
// Initial page + pushed state
assert.equal(w.webContents.length(), 2)
done()
})
})
2017-06-05 21:47:11 +00:00
w.loadURL('about:blank')
})
})
})
2016-03-25 20:03:49 +00:00
})