12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352 |
- const assert = require('assert')
- const chai = require('chai')
- const dirtyChai = require('dirty-chai')
- const fs = require('fs')
- const http = require('http')
- const path = require('path')
- const ws = require('ws')
- const url = require('url')
- const ChildProcess = require('child_process')
- const { ipcRenderer, remote } = require('electron')
- const { closeWindow } = require('./window-helpers')
- const { resolveGetters } = require('./assert-helpers')
- const { app, BrowserWindow, ipcMain, protocol, session, webContents } = remote
- const isCI = remote.getGlobal('isCi')
- const features = process.atomBinding('features')
- const { expect } = chai
- chai.use(dirtyChai)
- /* Most of the APIs here don't use standard callbacks */
- /* eslint-disable standard/no-callback-literal */
- describe('chromium feature', () => {
- const fixtures = path.resolve(__dirname, 'fixtures')
- let listener = null
- let w = null
- afterEach(() => {
- if (listener != null) {
- window.removeEventListener('message', listener)
- }
- listener = null
- })
- describe('command line switches', () => {
- describe('--lang switch', () => {
- const currentLocale = app.getLocale()
- const testLocale = (locale, result, done) => {
- const appPath = path.join(__dirname, 'fixtures', 'api', 'locale-check')
- const electronPath = remote.getGlobal('process').execPath
- let output = ''
- const appProcess = ChildProcess.spawn(electronPath, [appPath, `--lang=${locale}`])
- appProcess.stdout.on('data', (data) => { output += data })
- appProcess.stdout.on('end', () => {
- output = output.replace(/(\r\n|\n|\r)/gm, '')
- assert.strictEqual(output, result)
- done()
- })
- }
- it('should set the locale', (done) => testLocale('fr', 'fr', done))
- it('should not set an invalid locale', (done) => testLocale('asdfkl', currentLocale, done))
- })
- describe('--remote-debugging-port switch', () => {
- it('should display the discovery page', (done) => {
- const electronPath = remote.getGlobal('process').execPath
- let output = ''
- const appProcess = ChildProcess.spawn(electronPath, [`--remote-debugging-port=`])
- appProcess.stderr.on('data', (data) => {
- output += data
- const m = /DevTools listening on ws:\/\/127.0.0.1:(\d+)\//.exec(output)
- if (m) {
- appProcess.stderr.removeAllListeners('data')
- const port = m[1]
- http.get(`http://127.0.0.1:${port}`, (res) => {
- res.destroy()
- appProcess.kill()
- expect(res.statusCode).to.eql(200)
- expect(parseInt(res.headers['content-length'])).to.be.greaterThan(0)
- done()
- })
- }
- })
- })
- })
- })
- afterEach(() => closeWindow(w).then(() => { w = null }))
- describe('heap snapshot', () => {
- it('does not crash', function () {
- process.atomBinding('v8_util').takeHeapSnapshot()
- })
- })
- describe('sending request of http protocol urls', () => {
- it('does not crash', (done) => {
- const server = http.createServer((req, res) => {
- res.end()
- server.close()
- done()
- })
- server.listen(0, '127.0.0.1', () => {
- const port = server.address().port
- $.get(`http://127.0.0.1:${port}`)
- })
- })
- })
- describe('accessing key names also used as Node.js module names', () => {
- it('does not crash', (done) => {
- w = new BrowserWindow({ show: false })
- w.webContents.once('did-finish-load', () => { done() })
- w.webContents.once('crashed', () => done(new Error('WebContents crashed.')))
- w.loadFile(path.join(fixtures, 'pages', 'external-string.html'))
- })
- })
- describe('loading jquery', () => {
- it('does not crash', (done) => {
- w = new BrowserWindow({
- show: false,
- webPreferences: {
- nodeIntegration: false
- }
- })
- w.webContents.once('did-finish-load', () => { done() })
- w.webContents.once('crashed', () => done(new Error('WebContents crashed.')))
- w.loadFile(path.join(fixtures, 'pages', 'jquery.html'))
- })
- })
- describe('navigator.webkitGetUserMedia', () => {
- it('calls its callbacks', (done) => {
- navigator.webkitGetUserMedia({
- audio: true,
- video: false
- }, () => done(),
- () => done())
- })
- })
- describe('navigator.mediaDevices', () => {
- if (isCI) return
- afterEach(() => {
- remote.getGlobal('permissionChecks').allow()
- })
- it('can return labels of enumerated devices', (done) => {
- navigator.mediaDevices.enumerateDevices().then((devices) => {
- const labels = devices.map((device) => device.label)
- const labelFound = labels.some((label) => !!label)
- if (labelFound) {
- done()
- } else {
- done(new Error(`No device labels found: ${JSON.stringify(labels)}`))
- }
- }).catch(done)
- })
- it('does not return labels of enumerated devices when permission denied', (done) => {
- remote.getGlobal('permissionChecks').reject()
- navigator.mediaDevices.enumerateDevices().then((devices) => {
- const labels = devices.map((device) => device.label)
- const labelFound = labels.some((label) => !!label)
- if (labelFound) {
- done(new Error(`Device labels were found: ${JSON.stringify(labels)}`))
- } else {
- done()
- }
- }).catch(done)
- })
- it('can return new device id when cookie storage is cleared', (done) => {
- const options = {
- origin: null,
- storages: ['cookies']
- }
- const deviceIds = []
- const ses = session.fromPartition('persist:media-device-id')
- w = new BrowserWindow({
- show: false,
- webPreferences: {
- session: ses
- }
- })
- w.webContents.on('ipc-message', (event, args) => {
- if (args[0] === 'deviceIds') deviceIds.push(args[1])
- if (deviceIds.length === 2) {
- assert.notDeepStrictEqual(deviceIds[0], deviceIds[1])
- closeWindow(w).then(() => {
- w = null
- done()
- }).catch((error) => done(error))
- } else {
- ses.clearStorageData(options, () => {
- w.webContents.reload()
- })
- }
- })
- w.loadFile(path.join(fixtures, 'pages', 'media-id-reset.html'))
- })
- })
- describe('navigator.language', () => {
- it('should not be empty', () => {
- assert.notStrictEqual(navigator.language, '')
- })
- })
- describe('navigator.languages', (done) => {
- it('should return the system locale only', () => {
- const appLocale = app.getLocale()
- assert.strictEqual(navigator.languages.length, 1)
- assert.strictEqual(navigator.languages[0], appLocale)
- })
- })
- describe('navigator.serviceWorker', () => {
- it('should register for file scheme', (done) => {
- w = new BrowserWindow({
- show: false,
- webPreferences: {
- partition: 'sw-file-scheme-spec'
- }
- })
- w.webContents.on('ipc-message', (event, args) => {
- if (args[0] === 'reload') {
- w.webContents.reload()
- } else if (args[0] === 'error') {
- done(args[1])
- } else if (args[0] === 'response') {
- assert.strictEqual(args[1], 'Hello from serviceWorker!')
- session.fromPartition('sw-file-scheme-spec').clearStorageData({
- storages: ['serviceworkers']
- }, () => done())
- }
- })
- w.webContents.on('crashed', () => done(new Error('WebContents crashed.')))
- w.loadFile(path.join(fixtures, 'pages', 'service-worker', 'index.html'))
- })
- it('should register for intercepted file scheme', (done) => {
- const customSession = session.fromPartition('intercept-file')
- customSession.protocol.interceptBufferProtocol('file', (request, callback) => {
- let file = url.parse(request.url).pathname
- 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 })
- }, (error) => {
- if (error) done(error)
- })
- w = new BrowserWindow({
- show: false,
- webPreferences: { session: customSession }
- })
- w.webContents.on('ipc-message', (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.strictEqual(args[1], 'Hello from serviceWorker!')
- customSession.clearStorageData({
- storages: ['serviceworkers']
- }, () => {
- customSession.protocol.uninterceptProtocol('file', (error) => done(error))
- })
- }
- })
- w.webContents.on('crashed', () => done(new Error('WebContents crashed.')))
- w.loadFile(path.join(fixtures, 'pages', 'service-worker', 'index.html'))
- })
- })
- describe('navigator.geolocation', () => {
- before(function () {
- if (!features.isFakeLocationProviderEnabled()) {
- return this.skip()
- }
- })
- it('returns position when permission is granted', (done) => {
- navigator.geolocation.getCurrentPosition((position) => {
- assert(position.timestamp)
- done()
- }, (error) => {
- done(error)
- })
- })
- it('returns error when permission is denied', (done) => {
- w = new BrowserWindow({
- show: false,
- webPreferences: {
- partition: 'geolocation-spec'
- }
- })
- w.webContents.on('ipc-message', (event, args) => {
- if (args[0] === 'success') {
- done()
- } else {
- done('unexpected response from geolocation api')
- }
- })
- w.webContents.session.setPermissionRequestHandler((wc, permission, callback) => {
- if (permission === 'geolocation') {
- callback(false)
- } else {
- callback(true)
- }
- })
- w.loadFile(path.join(fixtures, 'pages', 'geolocation', 'index.html'))
- })
- })
- describe('window.open', () => {
- it('returns a BrowserWindowProxy object', () => {
- const b = window.open('about:blank', '', 'show=no')
- assert.strictEqual(b.closed, false)
- assert.strictEqual(b.constructor.name, 'BrowserWindowProxy')
- b.close()
- })
- it('accepts "nodeIntegration" as feature', (done) => {
- let b = null
- listener = (event) => {
- assert.strictEqual(event.data.isProcessGlobalUndefined, true)
- 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', (done) => {
- let b = null
- listener = (event) => {
- const ref1 = remote.getCurrentWindow().getSize()
- const width = ref1[0]
- const height = ref1[1]
- assert.strictEqual(event.data, `size: ${width} ${height}`)
- b.close()
- done()
- }
- window.addEventListener('message', listener)
- b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no')
- })
- for (const show of [true, false]) {
- it(`inherits parent visibility over parent {show=${show}} option`, (done) => {
- const w = new BrowserWindow({ show })
- // toggle visibility
- if (show) {
- w.hide()
- } else {
- w.show()
- }
- w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
- assert.strictEqual(options.show, w.isVisible())
- w.close()
- done()
- })
- w.loadFile(path.join(fixtures, 'pages', 'window-open.html'))
- })
- }
- it('disables node integration when it is disabled on the parent window', (done) => {
- let b = null
- listener = (event) => {
- assert.strictEqual(event.data.isProcessGlobalUndefined, true)
- b.close()
- done()
- }
- window.addEventListener('message', listener)
- const windowUrl = require('url').format({
- pathname: `${fixtures}/pages/window-opener-no-node-integration.html`,
- protocol: 'file',
- query: {
- p: `${fixtures}/pages/window-opener-node.html`
- },
- slashes: true
- })
- b = window.open(windowUrl, '', 'nodeIntegration=no,show=no')
- })
- it('disables webviewTag when node integration is disabled on the parent window', (done) => {
- let b = null
- listener = (event) => {
- assert.strictEqual(event.data.isWebViewUndefined, true)
- b.close()
- done()
- }
- window.addEventListener('message', listener)
- const windowUrl = require('url').format({
- pathname: `${fixtures}/pages/window-opener-no-web-view-tag.html`,
- protocol: 'file',
- query: {
- p: `${fixtures}/pages/window-opener-web-view.html`
- },
- slashes: true
- })
- b = window.open(windowUrl, '', 'nodeIntegration=no,show=no')
- })
- // TODO(codebytere): re-enable this test
- xit('disables node integration when it is disabled on the parent window for chrome devtools URLs', (done) => {
- let b = null
- app.once('web-contents-created', (event, contents) => {
- contents.once('did-finish-load', () => {
- contents.executeJavaScript('typeof process').then((typeofProcessGlobal) => {
- assert.strictEqual(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', (done) => {
- let b = null
- app.once('web-contents-created', (event, contents) => {
- contents.once('did-finish-load', () => {
- app.once('browser-window-created', (event, window) => {
- const preferences = window.webContents.getLastWebPreferences()
- assert.strictEqual(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 })
- })
- })
- const 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', (done) => {
- let b = null
- listener = (event) => {
- assert.strictEqual(event.data.isWebViewGlobalUndefined, true)
- b.close()
- done()
- }
- window.addEventListener('message', listener)
- const 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')
- })
- it('does not override child options', (done) => {
- let b = null
- const size = {
- width: 350,
- height: 450
- }
- listener = (event) => {
- assert.strictEqual(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('handles cycles when merging the parent options into the child options', (done) => {
- w = BrowserWindow.fromId(ipcRenderer.sendSync('create-window-with-options-cycle'))
- w.loadFile(path.join(fixtures, 'pages', 'window-open.html'))
- w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
- assert.strictEqual(options.show, false)
- assert.deepStrictEqual(...resolveGetters(options.foo, {
- bar: undefined,
- baz: {
- hello: {
- world: true
- }
- },
- baz2: {
- hello: {
- world: true
- }
- }
- }))
- done()
- })
- })
- it('defines a window.location getter', (done) => {
- let b = null
- let targetURL
- if (process.platform === 'win32') {
- targetURL = `file:///${fixtures.replace(/\\/g, '/')}/pages/base-page.html`
- } else {
- targetURL = `file://${fixtures}/pages/base-page.html`
- }
- app.once('browser-window-created', (event, window) => {
- window.webContents.once('did-finish-load', () => {
- assert.strictEqual(b.location, targetURL)
- b.close()
- done()
- })
- })
- b = window.open(targetURL)
- })
- it('defines a window.location setter', (done) => {
- let b = null
- app.once('browser-window-created', (event, { webContents }) => {
- webContents.once('did-finish-load', () => {
- // When it loads, redirect
- b.location = `file://${fixtures}/pages/base-page.html`
- webContents.once('did-finish-load', () => {
- // After our second redirect, cleanup and callback
- b.close()
- done()
- })
- })
- })
- b = window.open('about:blank')
- })
- it('open a blank page when no URL is specified', (done) => {
- let b = null
- app.once('browser-window-created', (event, { webContents }) => {
- webContents.once('did-finish-load', () => {
- const { location } = b
- b.close()
- assert.strictEqual(location, 'about:blank')
- let c = null
- app.once('browser-window-created', (event, { webContents }) => {
- webContents.once('did-finish-load', () => {
- const { location } = c
- c.close()
- assert.strictEqual(location, 'about:blank')
- done()
- })
- })
- c = window.open('')
- })
- })
- b = window.open()
- })
- it('throws an exception when the arguments cannot be converted to strings', () => {
- assert.throws(() => {
- window.open('', { toString: null })
- }, /Cannot convert object to primitive value/)
- assert.throws(() => {
- window.open('', '', { toString: 3 })
- }, /Cannot convert object to primitive value/)
- })
- it('sets the window title to the specified frameName', (done) => {
- let b = null
- app.once('browser-window-created', (event, createdWindow) => {
- assert.strictEqual(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', (done) => {
- let b = null
- app.once('browser-window-created', (event, createdWindow) => {
- assert.strictEqual(createdWindow.getTitle(), '__proto__')
- b.close()
- done()
- })
- b = window.open('', '__proto__')
- })
- it('does not throw an exception when the features include webPreferences', () => {
- let b = null
- assert.doesNotThrow(() => {
- b = window.open('', '', 'webPreferences=')
- })
- b.close()
- })
- })
- describe('window.opener', () => {
- const url = `file://${fixtures}/pages/window-opener.html`
- it('is null for main window', (done) => {
- w = new BrowserWindow({ show: false })
- w.webContents.once('ipc-message', (event, args) => {
- assert.deepStrictEqual(args, ['opener', null])
- done()
- })
- w.loadFile(path.join(fixtures, 'pages', 'window-opener.html'))
- })
- it('is not null for window opened by window.open', (done) => {
- let b = null
- listener = (event) => {
- assert.strictEqual(event.data, 'object')
- b.close()
- done()
- }
- window.addEventListener('message', listener)
- b = window.open(url, '', 'show=no')
- })
- })
- describe('window.opener access from BrowserWindow', () => {
- const scheme = 'other'
- const url = `${scheme}://${fixtures}/pages/window-opener-location.html`
- let w = null
- before((done) => {
- protocol.registerFileProtocol(scheme, (request, callback) => {
- callback(`${fixtures}/pages/window-opener-location.html`)
- }, (error) => done(error))
- })
- after(() => {
- protocol.unregisterProtocol(scheme)
- })
- afterEach(() => {
- w.close()
- })
- it('does nothing when origin of current window does not match opener', (done) => {
- listener = (event) => {
- assert.strictEqual(event.data, null)
- done()
- }
- window.addEventListener('message', listener)
- w = window.open(url, '', 'show=no,nodeIntegration=no')
- })
- it('works when origin matches', (done) => {
- listener = (event) => {
- assert.strictEqual(event.data, location.href)
- done()
- }
- window.addEventListener('message', listener)
- w = window.open(`file://${fixtures}/pages/window-opener-location.html`, '', 'show=no,nodeIntegration=no')
- })
- it('works when origin does not match opener but has node integration', (done) => {
- listener = (event) => {
- assert.strictEqual(event.data, location.href)
- done()
- }
- window.addEventListener('message', listener)
- w = window.open(url, '', 'show=no,nodeIntegration=yes')
- })
- })
- describe('window.opener access from <webview>', () => {
- const scheme = 'other'
- const srcPath = `${fixtures}/pages/webview-opener-postMessage.html`
- const pageURL = `file://${fixtures}/pages/window-opener-location.html`
- let webview = null
- before((done) => {
- protocol.registerFileProtocol(scheme, (request, callback) => {
- callback(srcPath)
- }, (error) => done(error))
- })
- after(() => {
- protocol.unregisterProtocol(scheme)
- })
- afterEach(() => {
- if (webview != null) webview.remove()
- })
- it('does nothing when origin of webview src URL does not match opener', (done) => {
- webview = new WebView()
- webview.addEventListener('console-message', (e) => {
- assert.strictEqual(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)
- })
- it('works when origin matches', (done) => {
- webview = new WebView()
- webview.addEventListener('console-message', (e) => {
- assert.strictEqual(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', (done) => {
- webview = new WebView()
- webview.addEventListener('console-message', (e) => {
- webview.remove()
- assert.strictEqual(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)
- })
- })
- describe('window.postMessage', () => {
- it('sets the source and origin correctly', (done) => {
- let b = null
- listener = (event) => {
- window.removeEventListener('message', listener)
- b.close()
- const message = JSON.parse(event.data)
- assert.strictEqual(message.data, 'testing')
- assert.strictEqual(message.origin, 'file://')
- assert.strictEqual(message.sourceEqualsOpener, true)
- assert.strictEqual(event.origin, 'file://')
- done()
- }
- window.addEventListener('message', listener)
- app.once('browser-window-created', (event, { webContents }) => {
- webContents.once('did-finish-load', () => {
- b.postMessage('testing', '*')
- })
- })
- b = window.open(`file://${fixtures}/pages/window-open-postMessage.html`, '', 'show=no')
- })
- it('throws an exception when the targetOrigin cannot be converted to a string', () => {
- const b = window.open('')
- assert.throws(() => {
- b.postMessage('test', { toString: null })
- }, /Cannot convert object to primitive value/)
- b.close()
- })
- })
- describe('window.opener.postMessage', () => {
- it('sets source and origin correctly', (done) => {
- let b = null
- listener = (event) => {
- window.removeEventListener('message', listener)
- b.close()
- assert.strictEqual(event.source, b)
- assert.strictEqual(event.origin, 'file://')
- done()
- }
- window.addEventListener('message', listener)
- b = window.open(`file://${fixtures}/pages/window-opener-postMessage.html`, '', 'show=no')
- })
- it('supports windows opened from a <webview>', (done) => {
- const webview = new WebView()
- webview.addEventListener('console-message', (e) => {
- webview.remove()
- assert.strictEqual(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', () => {
- let serverURL
- let server
- beforeEach((done) => {
- server = http.createServer((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', () => {
- serverURL = `http://127.0.0.1:${server.address().port}`
- done()
- })
- })
- afterEach(() => {
- server.close()
- })
- it('delivers messages that match the origin', (done) => {
- let b = null
- listener = (event) => {
- window.removeEventListener('message', listener)
- b.close()
- assert.strictEqual(event.data, 'deliver')
- done()
- }
- window.addEventListener('message', listener)
- b = window.open(serverURL, '', 'show=no')
- })
- })
- })
- describe('creating a Uint8Array under browser side', () => {
- it('does not crash', () => {
- const RUint8Array = remote.getGlobal('Uint8Array')
- const arr = new RUint8Array()
- assert(arr)
- })
- })
- describe('webgl', () => {
- before(function () {
- if (isCI && process.platform === 'win32') {
- this.skip()
- }
- })
- it('can be get as context in canvas', () => {
- if (process.platform === 'linux') {
- // FIXME(alexeykuzmin): Skip the test.
- // this.skip()
- return
- }
- const webgl = document.createElement('canvas').getContext('webgl')
- assert.notStrictEqual(webgl, null)
- })
- })
- describe('web workers', () => {
- it('Worker can work', (done) => {
- const worker = new Worker('../fixtures/workers/worker.js')
- const message = 'ping'
- worker.onmessage = (event) => {
- assert.strictEqual(event.data, message)
- worker.terminate()
- done()
- }
- worker.postMessage(message)
- })
- it('Worker has no node integration by default', (done) => {
- const worker = new Worker('../fixtures/workers/worker_node.js')
- worker.onmessage = (event) => {
- assert.strictEqual(event.data, 'undefined undefined undefined undefined')
- worker.terminate()
- done()
- }
- })
- it('Worker has node integration with nodeIntegrationInWorker', (done) => {
- const webview = new WebView()
- webview.addEventListener('ipc-message', (e) => {
- assert.strictEqual(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)
- })
- it('SharedWorker can work', (done) => {
- const worker = new SharedWorker('../fixtures/workers/shared_worker.js')
- const message = 'ping'
- worker.port.onmessage = (event) => {
- assert.strictEqual(event.data, message)
- done()
- }
- worker.port.postMessage(message)
- })
- it('SharedWorker has no node integration by default', (done) => {
- const worker = new SharedWorker('../fixtures/workers/shared_worker_node.js')
- worker.port.onmessage = (event) => {
- assert.strictEqual(event.data, 'undefined undefined undefined undefined')
- done()
- }
- })
- it('SharedWorker has node integration with nodeIntegrationInWorker', (done) => {
- const webview = new WebView()
- webview.addEventListener('console-message', (e) => {
- console.log(e)
- })
- webview.addEventListener('ipc-message', (e) => {
- assert.strictEqual(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)
- })
- })
- describe('iframe', () => {
- let iframe = null
- beforeEach(() => {
- iframe = document.createElement('iframe')
- })
- afterEach(() => {
- document.body.removeChild(iframe)
- })
- it('does not have node integration', (done) => {
- iframe.src = `file://${fixtures}/pages/set-global.html`
- document.body.appendChild(iframe)
- iframe.onload = () => {
- assert.strictEqual(iframe.contentWindow.test, 'undefined undefined undefined')
- done()
- }
- })
- })
- describe('storage', () => {
- describe('DOM storage quota override', () => {
- ['localStorage', 'sessionStorage'].forEach((storageName) => {
- it(`allows saving at least 50MiB in ${storageName}`, () => {
- const storage = window[storageName]
- const testKeyName = '_electronDOMStorageQuotaOverrideTest'
- // 25 * 2^20 UTF-16 characters will require 50MiB
- const arraySize = 25 * Math.pow(2, 20)
- storage[testKeyName] = new Array(arraySize).fill('X').join('')
- expect(storage[testKeyName]).to.have.lengthOf(arraySize)
- delete storage[testKeyName]
- })
- })
- })
- it('requesting persitent quota works', (done) => {
- navigator.webkitPersistentStorage.requestQuota(1024 * 1024, (grantedBytes) => {
- assert.strictEqual(grantedBytes, 1048576)
- done()
- })
- })
- describe('custom non standard schemes', () => {
- const protocolName = 'storage'
- let contents = null
- before((done) => {
- const handler = (request, callback) => {
- const 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, (error) => done(error))
- })
- after((done) => {
- protocol.unregisterProtocol(protocolName, () => done())
- })
- beforeEach(() => {
- contents = webContents.create({})
- })
- afterEach(() => {
- contents.destroy()
- contents = null
- })
- it('cannot access localStorage', (done) => {
- ipcMain.once('local-storage-response', (event, error) => {
- assert.strictEqual(
- 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', (done) => {
- ipcMain.once('session-storage-response', (event, error) => {
- assert.strictEqual(
- 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', (done) => {
- ipcMain.once('web-sql-response', (event, error) => {
- assert.strictEqual(
- 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', (done) => {
- ipcMain.once('indexed-db-response', (event, error) => {
- assert.strictEqual(error, 'The user denied permission to access the database.')
- done()
- })
- contents.loadURL(`${protocolName}://host/indexedDB`)
- })
- it('cannot access cookie', (done) => {
- ipcMain.once('cookie-response', (event, cookie) => {
- assert(!cookie)
- done()
- })
- contents.loadURL(`${protocolName}://host/cookie`)
- })
- })
- })
- describe('websockets', () => {
- let wss = null
- let server = null
- const WebSocketServer = ws.Server
- afterEach(() => {
- wss.close()
- server.close()
- })
- it('has user agent', (done) => {
- server = http.createServer()
- server.listen(0, '127.0.0.1', () => {
- const port = server.address().port
- wss = new WebSocketServer({ server: server })
- wss.on('error', done)
- wss.on('connection', (ws, upgradeReq) => {
- if (upgradeReq.headers['user-agent']) {
- done()
- } else {
- done('user agent is empty')
- }
- })
- const socket = new WebSocket(`ws://127.0.0.1:${port}`)
- assert(socket)
- })
- })
- })
- describe('Promise', () => {
- it('resolves correctly in Node.js calls', (done) => {
- document.registerElement('x-element', {
- prototype: Object.create(HTMLElement.prototype, {
- createdCallback: {
- value: () => {}
- }
- })
- })
- setImmediate(() => {
- let called = false
- Promise.resolve().then(() => {
- done(called ? void 0 : new Error('wrong sequence'))
- })
- document.createElement('x-element')
- called = true
- })
- })
- it('resolves correctly in Electron calls', (done) => {
- document.registerElement('y-element', {
- prototype: Object.create(HTMLElement.prototype, {
- createdCallback: {
- value: () => {}
- }
- })
- })
- remote.getGlobal('setImmediate')(() => {
- let called = false
- Promise.resolve().then(() => {
- done(called ? void 0 : new Error('wrong sequence'))
- })
- document.createElement('y-element')
- called = true
- })
- })
- })
- describe('fetch', () => {
- it('does not crash', (done) => {
- const server = http.createServer((req, res) => {
- res.end('test')
- server.close()
- })
- server.listen(0, '127.0.0.1', () => {
- const port = server.address().port
- fetch(`http://127.0.0.1:${port}`).then((res) => res.body.getReader())
- .then((reader) => {
- reader.read().then((r) => {
- reader.cancel()
- done()
- })
- }).catch((e) => done(e))
- })
- })
- })
- describe('PDF Viewer', () => {
- before(function () {
- if (!features.isPDFViewerEnabled()) {
- return this.skip()
- }
- })
- beforeEach(() => {
- this.pdfSource = url.format({
- pathname: path.join(fixtures, 'assets', 'cat.pdf').replace(/\\/g, '/'),
- protocol: 'file',
- slashes: true
- })
- this.pdfSourceWithParams = url.format({
- pathname: path.join(fixtures, 'assets', 'cat.pdf').replace(/\\/g, '/'),
- query: {
- a: 1,
- b: 2
- },
- protocol: 'file',
- slashes: true
- })
- this.createBrowserWindow = ({ plugins, preload }) => {
- w = new BrowserWindow({
- show: false,
- webPreferences: {
- preload: path.join(fixtures, 'module', preload),
- plugins: plugins
- }
- })
- }
- this.testPDFIsLoadedInSubFrame = (page, preloadFile, done) => {
- const pagePath = url.format({
- pathname: path.join(fixtures, 'pages', page).replace(/\\/g, '/'),
- protocol: 'file',
- slashes: true
- })
- this.createBrowserWindow({ plugins: true, preload: preloadFile })
- ipcMain.once('pdf-loaded', (event, state) => {
- assert.strictEqual(state, 'success')
- done()
- })
- w.webContents.on('page-title-updated', () => {
- const parsedURL = url.parse(w.webContents.getURL(), true)
- assert.strictEqual(parsedURL.protocol, 'chrome:')
- assert.strictEqual(parsedURL.hostname, 'pdf-viewer')
- assert.strictEqual(parsedURL.query.src, pagePath)
- assert.strictEqual(w.webContents.getTitle(), 'cat.pdf')
- })
- w.loadFile(path.join(fixtures, 'pages', page))
- }
- })
- it('opens when loading a pdf resource as top level navigation', (done) => {
- this.createBrowserWindow({ plugins: true, preload: 'preload-pdf-loaded.js' })
- ipcMain.once('pdf-loaded', (event, state) => {
- assert.strictEqual(state, 'success')
- done()
- })
- w.webContents.on('page-title-updated', () => {
- const parsedURL = url.parse(w.webContents.getURL(), true)
- assert.strictEqual(parsedURL.protocol, 'chrome:')
- assert.strictEqual(parsedURL.hostname, 'pdf-viewer')
- assert.strictEqual(parsedURL.query.src, this.pdfSource)
- assert.strictEqual(w.webContents.getTitle(), 'cat.pdf')
- })
- w.webContents.loadURL(this.pdfSource)
- })
- it('opens a pdf link given params, the query string should be escaped', (done) => {
- this.createBrowserWindow({ plugins: true, preload: 'preload-pdf-loaded.js' })
- ipcMain.once('pdf-loaded', (event, state) => {
- assert.strictEqual(state, 'success')
- done()
- })
- w.webContents.on('page-title-updated', () => {
- const parsedURL = url.parse(w.webContents.getURL(), true)
- assert.strictEqual(parsedURL.protocol, 'chrome:')
- assert.strictEqual(parsedURL.hostname, 'pdf-viewer')
- assert.strictEqual(parsedURL.query.src, this.pdfSourceWithParams)
- assert.strictEqual(parsedURL.query.b, undefined)
- assert(parsedURL.search.endsWith('%3Fa%3D1%26b%3D2'))
- assert.strictEqual(w.webContents.getTitle(), 'cat.pdf')
- })
- w.webContents.loadURL(this.pdfSourceWithParams)
- })
- it('should download a pdf when plugins are disabled', (done) => {
- this.createBrowserWindow({ plugins: false, preload: 'preload-pdf-loaded.js' })
- ipcRenderer.sendSync('set-download-option', false, false)
- ipcRenderer.once('download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) => {
- assert.strictEqual(state, 'completed')
- assert.strictEqual(filename, 'cat.pdf')
- assert.strictEqual(mimeType, 'application/pdf')
- fs.unlinkSync(path.join(fixtures, 'mock.pdf'))
- done()
- })
- w.webContents.loadURL(this.pdfSource)
- })
- it('should not open when pdf is requested as sub resource', (done) => {
- fetch(this.pdfSource).then((res) => {
- assert.strictEqual(res.status, 200)
- assert.notStrictEqual(document.title, 'cat.pdf')
- done()
- }).catch((e) => done(e))
- })
- it('opens when loading a pdf resource in a iframe', (done) => {
- this.testPDFIsLoadedInSubFrame('pdf-in-iframe.html', 'preload-pdf-loaded-in-subframe.js', done)
- })
- it('opens when loading a pdf resource in a nested iframe', (done) => {
- this.testPDFIsLoadedInSubFrame('pdf-in-nested-iframe.html', 'preload-pdf-loaded-in-nested-subframe.js', done)
- })
- })
- describe('window.alert(message, title)', () => {
- it('throws an exception when the arguments cannot be converted to strings', () => {
- assert.throws(() => {
- window.alert({ toString: null })
- }, /Cannot convert object to primitive value/)
- })
- })
- describe('window.confirm(message, title)', () => {
- it('throws an exception when the arguments cannot be converted to strings', () => {
- assert.throws(() => {
- window.confirm({ toString: null }, 'title')
- }, /Cannot convert object to primitive value/)
- })
- })
- describe('window.history', () => {
- describe('window.history.go(offset)', () => {
- it('throws an exception when the argumnet cannot be converted to a string', () => {
- assert.throws(() => {
- window.history.go({ toString: null })
- }, /Cannot convert object to primitive value/)
- })
- })
- describe('window.history.pushState', () => {
- 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.strictEqual(w.webContents.length(), 1)
- w.webContents.executeJavaScript('window.history.pushState({}, "")', () => {
- // Initial page + pushed state
- assert.strictEqual(w.webContents.length(), 2)
- done()
- })
- })
- w.loadURL('about:blank')
- })
- })
- })
- describe('SpeechSynthesis', () => {
- before(function () {
- if (isCI || !features.isTtsEnabled()) {
- this.skip()
- }
- })
- it('should emit lifecycle events', async () => {
- const sentence = `long sentence which will take at least a few seconds to
- utter so that it's possible to pause and resume before the end`
- const utter = new SpeechSynthesisUtterance(sentence)
- // Create a dummy utterence so that speech synthesis state
- // is initialized for later calls.
- speechSynthesis.speak(new SpeechSynthesisUtterance())
- speechSynthesis.cancel()
- speechSynthesis.speak(utter)
- // paused state after speak()
- expect(speechSynthesis.paused).to.be.false()
- await new Promise((resolve) => { utter.onstart = resolve })
- // paused state after start event
- expect(speechSynthesis.paused).to.be.false()
- speechSynthesis.pause()
- // paused state changes async, right before the pause event
- expect(speechSynthesis.paused).to.be.false()
- await new Promise((resolve) => { utter.onpause = resolve })
- expect(speechSynthesis.paused).to.be.true()
- speechSynthesis.resume()
- await new Promise((resolve) => { utter.onresume = resolve })
- // paused state after resume event
- expect(speechSynthesis.paused).to.be.false()
- await new Promise((resolve) => { utter.onend = resolve })
- })
- })
- })
|