2016-07-14 16:10:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
2018-11-08 15:57:28 +00:00
|
|
|
const ChildProcess = require('child_process')
|
2018-09-18 18:00:31 +00:00
|
|
|
const fs = require('fs')
|
2017-02-15 22:14:40 +00:00
|
|
|
const http = require('http')
|
2016-07-14 16:10:40 +00:00
|
|
|
const path = require('path')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
|
|
|
const { emittedOnce } = require('./events-helpers')
|
2018-07-12 11:35:11 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const features = process.electronBinding('features')
|
2018-09-27 15:41:09 +00:00
|
|
|
const { ipcRenderer, remote, clipboard } = require('electron')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { BrowserWindow, webContents, ipcMain, session } = remote
|
|
|
|
const { expect } = chai
|
2016-07-14 16:17:16 +00:00
|
|
|
|
2016-07-14 16:33:16 +00:00
|
|
|
const isCi = remote.getGlobal('isCi')
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2018-07-12 11:35:11 +00:00
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
/* The whole webContents API doesn't use standard callbacks */
|
|
|
|
/* eslint-disable standard/no-callback-literal */
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('webContents module', () => {
|
2016-07-14 16:33:16 +00:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
|
let w
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach(() => {
|
2016-07-14 16:10:40 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
2019-01-07 19:19:27 +00:00
|
|
|
backgroundThrottling: false,
|
|
|
|
nodeIntegration: true,
|
|
|
|
webviewTag: true
|
2016-07-14 16:10:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => closeWindow(w).then(() => { w = null }))
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2018-12-06 20:16:19 +00:00
|
|
|
describe('loadURL() promise API', () => {
|
|
|
|
it('resolves when done loading', async () => {
|
|
|
|
await expect(w.loadURL('about:blank')).to.eventually.be.fulfilled
|
|
|
|
})
|
|
|
|
|
|
|
|
it('resolves when done loading a file URL', async () => {
|
|
|
|
await expect(w.loadFile(path.join(fixtures, 'pages', 'base-page.html'))).to.eventually.be.fulfilled
|
|
|
|
})
|
|
|
|
|
|
|
|
it('rejects when failing to load a file URL', async () => {
|
|
|
|
await expect(w.loadURL('file:non-existent')).to.eventually.be.rejected
|
|
|
|
.and.have.property('code', 'ERR_FILE_NOT_FOUND')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('rejects when loading fails due to DNS not resolved', async () => {
|
|
|
|
await expect(w.loadURL('https://err.name.not.resolved')).to.eventually.be.rejected
|
|
|
|
.and.have.property('code', 'ERR_NAME_NOT_RESOLVED')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('rejects when navigation is cancelled due to a bad scheme', async () => {
|
|
|
|
await expect(w.loadURL('bad-scheme://foo')).to.eventually.be.rejected
|
|
|
|
.and.have.property('code', 'ERR_FAILED')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sets appropriate error information on rejection', async () => {
|
|
|
|
let err
|
|
|
|
try {
|
|
|
|
await w.loadURL('file:non-existent')
|
|
|
|
} catch (e) {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
expect(err).not.to.be.null()
|
|
|
|
expect(err.code).to.eql('ERR_FILE_NOT_FOUND')
|
|
|
|
expect(err.errno).to.eql(-6)
|
|
|
|
expect(err.url).to.eql(process.platform === 'win32' ? 'file://non-existent/' : 'file:///non-existent')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('rejects if the load is aborted', async () => {
|
|
|
|
const s = http.createServer((req, res) => { /* never complete the request */ })
|
|
|
|
await new Promise(resolve => s.listen(0, '127.0.0.1', resolve))
|
|
|
|
const { port } = s.address()
|
|
|
|
const p = expect(w.loadURL(`http://127.0.0.1:${port}`)).to.eventually.be.rejectedWith(Error, /ERR_ABORTED/)
|
|
|
|
// load a different file before the first load completes, causing the
|
|
|
|
// first load to be aborted.
|
|
|
|
await w.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
|
|
|
|
await p
|
|
|
|
s.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("doesn't reject when a subframe fails to load", async () => {
|
|
|
|
let resp = null
|
|
|
|
const s = http.createServer((req, res) => {
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/html' })
|
|
|
|
res.write('<iframe src="http://err.name.not.resolved"></iframe>')
|
|
|
|
resp = res
|
|
|
|
// don't end the response yet
|
|
|
|
})
|
|
|
|
await new Promise(resolve => s.listen(0, '127.0.0.1', resolve))
|
|
|
|
const { port } = s.address()
|
|
|
|
const p = new Promise(resolve => {
|
|
|
|
w.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL, isMainFrame, frameProcessId, frameRoutingId) => {
|
|
|
|
if (!isMainFrame) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
const main = w.loadURL(`http://127.0.0.1:${port}`)
|
|
|
|
await p
|
|
|
|
resp.end()
|
|
|
|
await main
|
|
|
|
s.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("doesn't resolve when a subframe loads", async () => {
|
|
|
|
let resp = null
|
|
|
|
const s = http.createServer((req, res) => {
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/html' })
|
|
|
|
res.write('<iframe src="data:text/html,hi"></iframe>')
|
|
|
|
resp = res
|
|
|
|
// don't end the response yet
|
|
|
|
})
|
|
|
|
await new Promise(resolve => s.listen(0, '127.0.0.1', resolve))
|
|
|
|
const { port } = s.address()
|
|
|
|
const p = new Promise(resolve => {
|
|
|
|
w.webContents.on('did-frame-finish-load', (event, errorCode, errorDescription, validatedURL, isMainFrame, frameProcessId, frameRoutingId) => {
|
|
|
|
if (!isMainFrame) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
const main = w.loadURL(`http://127.0.0.1:${port}`)
|
|
|
|
await p
|
|
|
|
resp.destroy() // cause the main request to fail
|
|
|
|
await expect(main).to.eventually.be.rejected
|
|
|
|
.and.have.property('errno', -355) // ERR_INCOMPLETE_CHUNKED_ENCODING
|
|
|
|
s.close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('getFocusedWebContents() API', () => {
|
|
|
|
it('returns the focused web contents', (done) => {
|
2016-08-23 17:06:05 +00:00
|
|
|
if (isCi) return done()
|
|
|
|
|
2016-07-14 16:33:16 +00:00
|
|
|
const specWebContents = remote.getCurrentWebContents()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(specWebContents.id).to.equal(webContents.getFocusedWebContents().id)
|
2016-07-14 16:17:16 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-opened', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(specWebContents.devToolsWebContents.id).to.equal(webContents.getFocusedWebContents().id)
|
2016-07-14 16:17:16 +00:00
|
|
|
specWebContents.closeDevTools()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-closed', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(specWebContents.id).to.equal(webContents.getFocusedWebContents().id)
|
2016-07-14 16:17:16 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
specWebContents.openDevTools()
|
|
|
|
})
|
2016-08-23 17:06:05 +00:00
|
|
|
|
2018-05-11 11:41:58 +00:00
|
|
|
it('does not crash when called on a detached dev tools window', (done) => {
|
2016-08-23 17:06:05 +00:00
|
|
|
const specWebContents = w.webContents
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-opened', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2016-08-23 17:06:05 +00:00
|
|
|
webContents.getFocusedWebContents()
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.not.throw()
|
2016-08-23 17:06:05 +00:00
|
|
|
specWebContents.closeDevTools()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-closed', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2016-08-23 17:06:05 +00:00
|
|
|
webContents.getFocusedWebContents()
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.not.throw()
|
2016-08-23 17:06:05 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
specWebContents.openDevTools({ mode: 'detach' })
|
2016-08-23 17:06:05 +00:00
|
|
|
w.inspectElement(100, 100)
|
|
|
|
})
|
2016-07-14 16:17:16 +00:00
|
|
|
})
|
2016-08-15 21:03:31 +00:00
|
|
|
|
2018-04-12 01:57:40 +00:00
|
|
|
describe('setDevToolsWebContents() API', () => {
|
2019-04-30 14:08:33 +00:00
|
|
|
it('sets arbitrary webContents as devtools', async () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const devtools = new BrowserWindow({ show: false })
|
2019-04-30 14:08:33 +00:00
|
|
|
const promise = emittedOnce(devtools.webContents, 'dom-ready')
|
2017-11-30 12:04:50 +00:00
|
|
|
w.webContents.setDevToolsWebContents(devtools.webContents)
|
|
|
|
w.webContents.openDevTools()
|
2019-04-30 14:08:33 +00:00
|
|
|
await promise
|
2019-05-17 22:37:09 +00:00
|
|
|
expect(devtools.getURL().startsWith('devtools://devtools')).to.be.true()
|
2019-04-30 14:08:33 +00:00
|
|
|
const result = await devtools.webContents.executeJavaScript('InspectorFrontendHost.constructor.name')
|
|
|
|
expect(result).to.equal('InspectorFrontendHostImpl')
|
|
|
|
devtools.destroy()
|
2017-11-30 12:04:50 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('isFocused() API', () => {
|
|
|
|
it('returns false when the window is hidden', () => {
|
|
|
|
BrowserWindow.getAllWindows().forEach((window) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(!window.isVisible() && window.webContents.isFocused()).to.be.false()
|
2016-08-15 21:03:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-10-07 21:44:35 +00:00
|
|
|
|
2018-10-11 13:26:37 +00:00
|
|
|
describe('isCurrentlyAudible() API', () => {
|
2018-07-12 11:35:11 +00:00
|
|
|
it('returns whether audio is playing', async () => {
|
2018-09-05 21:00:05 +00:00
|
|
|
const webContents = remote.getCurrentWebContents()
|
|
|
|
const context = new window.AudioContext()
|
|
|
|
// Start in suspended state, because of the
|
|
|
|
// new web audio api policy.
|
|
|
|
context.suspend()
|
|
|
|
const oscillator = context.createOscillator()
|
|
|
|
oscillator.connect(context.destination)
|
|
|
|
oscillator.start()
|
2018-11-29 01:52:19 +00:00
|
|
|
let p = emittedOnce(webContents, '-audio-state-changed')
|
2018-09-05 21:00:05 +00:00
|
|
|
await context.resume()
|
2019-05-20 17:04:18 +00:00
|
|
|
await p
|
2018-09-05 21:00:05 +00:00
|
|
|
expect(webContents.isCurrentlyAudible()).to.be.true()
|
2018-11-29 01:52:19 +00:00
|
|
|
p = emittedOnce(webContents, '-audio-state-changed')
|
2018-09-05 21:00:05 +00:00
|
|
|
oscillator.stop()
|
2018-11-29 01:52:19 +00:00
|
|
|
await p
|
2018-09-05 21:00:05 +00:00
|
|
|
expect(webContents.isCurrentlyAudible()).to.be.false()
|
|
|
|
oscillator.disconnect()
|
|
|
|
context.close()
|
2018-07-12 11:35:11 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-14 02:19:22 +00:00
|
|
|
describe('getWebPreferences() API', () => {
|
|
|
|
it('should not crash when called for devTools webContents', (done) => {
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
w.webContents.once('devtools-opened', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.devToolsWebContents.getWebPreferences()).to.be.null()
|
2018-03-14 02:19:22 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-27 09:34:44 +00:00
|
|
|
describe('openDevTools() API', () => {
|
|
|
|
it('can show window with activation', async () => {
|
2019-01-11 23:33:35 +00:00
|
|
|
const focused = emittedOnce(w, 'focus')
|
2018-11-27 09:34:44 +00:00
|
|
|
w.show()
|
2019-01-11 23:33:35 +00:00
|
|
|
await focused
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.isFocused()).to.be.true()
|
2018-11-29 01:52:19 +00:00
|
|
|
const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened')
|
2018-11-27 09:34:44 +00:00
|
|
|
w.webContents.openDevTools({ mode: 'detach', activate: true })
|
2018-11-29 01:52:19 +00:00
|
|
|
await devtoolsOpened
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.isFocused()).to.be.false()
|
2018-11-27 09:34:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('can show window without activation', async () => {
|
2018-11-29 01:52:19 +00:00
|
|
|
const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened')
|
2018-11-27 09:34:44 +00:00
|
|
|
w.webContents.openDevTools({ mode: 'detach', activate: false })
|
2018-11-29 01:52:19 +00:00
|
|
|
await devtoolsOpened
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.isDevToolsOpened()).to.be.true()
|
2018-11-27 09:34:44 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-06 22:41:18 +00:00
|
|
|
describe('before-input-event event', () => {
|
2018-12-19 06:44:03 +00:00
|
|
|
it('can prevent document keyboard events', async () => {
|
|
|
|
await w.loadFile(path.join(fixtures, 'pages', 'key-events.html'))
|
|
|
|
const keyDown = new Promise(resolve => {
|
|
|
|
ipcMain.once('keydown', (event, key) => resolve(key))
|
2016-12-06 22:41:18 +00:00
|
|
|
})
|
2018-12-19 06:44:03 +00:00
|
|
|
ipcRenderer.sendSync('prevent-next-input-event', 'a', w.webContents.id)
|
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'a' })
|
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'b' })
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(await keyDown).to.equal('b')
|
2016-12-06 22:41:18 +00:00
|
|
|
})
|
|
|
|
|
2018-12-12 01:01:48 +00:00
|
|
|
it('has the correct properties', async () => {
|
|
|
|
await w.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
|
2018-12-19 06:44:03 +00:00
|
|
|
const testBeforeInput = async (opts) => {
|
|
|
|
const modifiers = []
|
|
|
|
if (opts.shift) modifiers.push('shift')
|
|
|
|
if (opts.control) modifiers.push('control')
|
|
|
|
if (opts.alt) modifiers.push('alt')
|
|
|
|
if (opts.meta) modifiers.push('meta')
|
|
|
|
if (opts.isAutoRepeat) modifiers.push('isAutoRepeat')
|
|
|
|
|
|
|
|
const p = emittedOnce(w.webContents, 'before-input-event')
|
|
|
|
w.webContents.sendInputEvent({
|
|
|
|
type: opts.type,
|
|
|
|
keyCode: opts.keyCode,
|
|
|
|
modifiers: modifiers
|
2018-12-12 01:01:48 +00:00
|
|
|
})
|
2018-12-19 06:44:03 +00:00
|
|
|
const [, input] = await p
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(input.type).to.equal(opts.type)
|
|
|
|
expect(input.key).to.equal(opts.key)
|
|
|
|
expect(input.code).to.equal(opts.code)
|
|
|
|
expect(input.isAutoRepeat).to.equal(opts.isAutoRepeat)
|
|
|
|
expect(input.shift).to.equal(opts.shift)
|
|
|
|
expect(input.control).to.equal(opts.control)
|
|
|
|
expect(input.alt).to.equal(opts.alt)
|
|
|
|
expect(input.meta).to.equal(opts.meta)
|
2018-12-12 01:01:48 +00:00
|
|
|
}
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyDown',
|
|
|
|
key: 'A',
|
|
|
|
code: 'KeyA',
|
|
|
|
keyCode: 'a',
|
|
|
|
shift: true,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: true
|
|
|
|
})
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '.',
|
|
|
|
code: 'Period',
|
|
|
|
keyCode: '.',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: false
|
|
|
|
})
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '!',
|
|
|
|
code: 'Digit1',
|
|
|
|
keyCode: '1',
|
|
|
|
shift: true,
|
|
|
|
control: false,
|
|
|
|
alt: false,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: false
|
|
|
|
})
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: 'Tab',
|
|
|
|
code: 'Tab',
|
|
|
|
keyCode: 'Tab',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: false,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: true
|
2016-12-06 22:41:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-09-27 15:41:09 +00:00
|
|
|
|
2019-05-14 06:40:40 +00:00
|
|
|
describe('zoom-changed', () => {
|
|
|
|
beforeEach(function () {
|
|
|
|
// On Mac, zooming isn't done with the mouse wheel.
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
return closeWindow(w).then(() => {
|
|
|
|
w = null
|
|
|
|
this.skip()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('is emitted with the correct zooming info', async () => {
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
|
|
|
|
await emittedOnce(w.webContents, 'did-finish-load')
|
|
|
|
|
|
|
|
const testZoomChanged = async ({ zoomingIn }) => {
|
|
|
|
const promise = emittedOnce(w.webContents, 'zoom-changed')
|
|
|
|
|
|
|
|
w.webContents.sendInputEvent({
|
|
|
|
type: 'mousewheel',
|
|
|
|
x: 300,
|
|
|
|
y: 300,
|
|
|
|
deltaX: 0,
|
|
|
|
deltaY: zoomingIn ? 1 : -1,
|
|
|
|
wheelTicksX: 0,
|
|
|
|
wheelTicksY: zoomingIn ? 1 : -1,
|
|
|
|
phase: 'began',
|
|
|
|
modifiers: ['control', 'meta']
|
|
|
|
})
|
|
|
|
|
|
|
|
const [, zoomDirection] = await promise
|
|
|
|
expect(zoomDirection).to.equal(zoomingIn ? 'in' : 'out')
|
|
|
|
}
|
|
|
|
|
|
|
|
await testZoomChanged({ zoomingIn: true })
|
|
|
|
await testZoomChanged({ zoomingIn: false })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-28 20:18:10 +00:00
|
|
|
describe('devtools window', () => {
|
2018-09-27 15:41:09 +00:00
|
|
|
let testFn = it
|
|
|
|
if (process.platform === 'darwin' && isCi) {
|
|
|
|
testFn = it.skip
|
|
|
|
}
|
2019-03-09 01:07:10 +00:00
|
|
|
if (process.platform === 'win32' && isCi) {
|
|
|
|
testFn = it.skip
|
|
|
|
}
|
2018-09-27 15:41:09 +00:00
|
|
|
try {
|
|
|
|
// We have other tests that check if native modules work, if we fail to require
|
|
|
|
// robotjs let's skip this test to avoid false negatives
|
|
|
|
require('robotjs')
|
|
|
|
} catch (err) {
|
|
|
|
testFn = it.skip
|
|
|
|
}
|
|
|
|
|
|
|
|
testFn('can receive and handle menu events', async function () {
|
|
|
|
this.timeout(5000)
|
|
|
|
w.show()
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'key-events.html'))
|
|
|
|
// Ensure the devtools are loaded
|
|
|
|
w.webContents.closeDevTools()
|
|
|
|
const opened = emittedOnce(w.webContents, 'devtools-opened')
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
await opened
|
|
|
|
await emittedOnce(w.webContents.devToolsWebContents, 'did-finish-load')
|
|
|
|
w.webContents.devToolsWebContents.focus()
|
|
|
|
|
|
|
|
// Focus an input field
|
|
|
|
await w.webContents.devToolsWebContents.executeJavaScript(
|
|
|
|
`const input = document.createElement('input');
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
document.body.appendChild(input)
|
|
|
|
input.focus();`
|
|
|
|
)
|
|
|
|
|
|
|
|
// Write something to the clipboard
|
|
|
|
clipboard.writeText('test value')
|
|
|
|
|
|
|
|
// Fake a paste request using robotjs to emulate a REAL keyboard paste event
|
|
|
|
require('robotjs').keyTap('v', process.platform === 'darwin' ? ['command'] : ['control'])
|
|
|
|
|
|
|
|
const start = Date.now()
|
|
|
|
let val
|
|
|
|
|
|
|
|
// Check every now and again for the pasted value (paste is async)
|
|
|
|
while (val !== 'test value' && Date.now() - start <= 1000) {
|
|
|
|
val = await w.webContents.devToolsWebContents.executeJavaScript(
|
|
|
|
`document.querySelector('input').value`
|
|
|
|
)
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we're done expect the paste to have been successful
|
|
|
|
expect(val).to.equal('test value', 'value should eventually become the pasted value')
|
|
|
|
})
|
|
|
|
})
|
2016-12-06 22:41:18 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('sendInputEvent(event)', () => {
|
2019-03-11 20:35:44 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
await w.loadFile(path.join(fixtures, 'pages', 'key-events.html'))
|
2016-10-07 22:12:35 +00:00
|
|
|
})
|
2016-10-07 21:44:35 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send keydown events', (done) => {
|
|
|
|
ipcMain.once('keydown', (event, key, code, keyCode, shiftKey, ctrlKey, altKey) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(key).to.equal('a')
|
|
|
|
expect(code).to.equal('KeyA')
|
|
|
|
expect(keyCode).to.equal(65)
|
|
|
|
expect(shiftKey).to.be.false()
|
|
|
|
expect(ctrlKey).to.be.false()
|
|
|
|
expect(altKey).to.be.false()
|
2016-10-07 21:44:35 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'A' })
|
2016-10-07 21:44:35 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send keydown events with modifiers', (done) => {
|
|
|
|
ipcMain.once('keydown', (event, key, code, keyCode, shiftKey, ctrlKey, altKey) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(key).to.equal('Z')
|
|
|
|
expect(code).to.equal('KeyZ')
|
|
|
|
expect(keyCode).to.equal(90)
|
|
|
|
expect(shiftKey).to.be.true()
|
|
|
|
expect(ctrlKey).to.be.true()
|
|
|
|
expect(altKey).to.be.false()
|
2016-10-07 21:44:35 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Z', modifiers: ['shift', 'ctrl'] })
|
2016-10-07 21:44:35 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send keydown events with special keys', (done) => {
|
|
|
|
ipcMain.once('keydown', (event, key, code, keyCode, shiftKey, ctrlKey, altKey) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(key).to.equal('Tab')
|
|
|
|
expect(code).to.equal('Tab')
|
|
|
|
expect(keyCode).to.equal(9)
|
|
|
|
expect(shiftKey).to.be.false()
|
|
|
|
expect(ctrlKey).to.be.false()
|
|
|
|
expect(altKey).to.be.true()
|
2016-10-07 21:44:35 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Tab', modifiers: ['alt'] })
|
2016-10-07 22:12:35 +00:00
|
|
|
})
|
2016-10-07 21:44:35 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send char events', (done) => {
|
|
|
|
ipcMain.once('keypress', (event, key, code, keyCode, shiftKey, ctrlKey, altKey) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(key).to.equal('a')
|
|
|
|
expect(code).to.equal('KeyA')
|
|
|
|
expect(keyCode).to.equal(65)
|
|
|
|
expect(shiftKey).to.be.false()
|
|
|
|
expect(ctrlKey).to.be.false()
|
|
|
|
expect(altKey).to.be.false()
|
2016-10-07 22:12:35 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'A' })
|
|
|
|
w.webContents.sendInputEvent({ type: 'char', keyCode: 'A' })
|
2016-10-07 22:12:35 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send char events with modifiers', (done) => {
|
|
|
|
ipcMain.once('keypress', (event, key, code, keyCode, shiftKey, ctrlKey, altKey) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(key).to.equal('Z')
|
|
|
|
expect(code).to.equal('KeyZ')
|
|
|
|
expect(keyCode).to.equal(90)
|
|
|
|
expect(shiftKey).to.be.true()
|
|
|
|
expect(ctrlKey).to.be.true()
|
|
|
|
expect(altKey).to.be.false()
|
2016-10-07 22:12:35 +00:00
|
|
|
done()
|
2016-10-07 21:44:35 +00:00
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Z' })
|
|
|
|
w.webContents.sendInputEvent({ type: 'char', keyCode: 'Z', modifiers: ['shift', 'ctrl'] })
|
2016-10-07 21:44:35 +00:00
|
|
|
})
|
|
|
|
})
|
2016-12-19 23:48:32 +00:00
|
|
|
|
2019-04-30 14:08:33 +00:00
|
|
|
it('supports inserting CSS', async () => {
|
2016-12-19 23:48:32 +00:00
|
|
|
w.loadURL('about:blank')
|
2019-06-17 15:39:36 +00:00
|
|
|
await w.webContents.insertCSS('body { background-repeat: round; }')
|
2019-04-30 14:08:33 +00:00
|
|
|
const result = await w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")')
|
|
|
|
expect(result).to.equal('round')
|
2016-12-19 23:48:32 +00:00
|
|
|
})
|
2016-12-20 17:07:48 +00:00
|
|
|
|
2019-06-17 15:39:36 +00:00
|
|
|
it('supports removing inserted CSS', async () => {
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
const key = await w.webContents.insertCSS('body { background-repeat: round; }')
|
|
|
|
await w.webContents.removeInsertedCSS(key)
|
|
|
|
const result = await w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")')
|
|
|
|
expect(result).to.equal('repeat')
|
|
|
|
})
|
|
|
|
|
2018-05-11 11:41:58 +00:00
|
|
|
it('supports inspecting an element in the devtools', (done) => {
|
2016-12-20 17:07:48 +00:00
|
|
|
w.loadURL('about:blank')
|
2019-04-30 14:08:33 +00:00
|
|
|
w.webContents.once('devtools-opened', () => { done() })
|
2016-12-20 17:07:48 +00:00
|
|
|
w.webContents.inspectElement(10, 10)
|
|
|
|
})
|
2017-01-26 18:34:27 +00:00
|
|
|
|
|
|
|
describe('startDrag({file, icon})', () => {
|
|
|
|
it('throws errors for a missing file or a missing/empty icon', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.startDrag({ icon: path.join(fixtures, 'assets', 'logo.png') })
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(`Must specify either 'file' or 'files' option`)
|
2017-01-26 18:34:27 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.startDrag({ file: __filename })
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(`Must specify 'icon' option`)
|
2017-01-26 18:38:13 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.startDrag({ file: __filename, icon: __filename })
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(`Must specify non-empty 'icon' option`)
|
2017-01-26 18:38:13 +00:00
|
|
|
}
|
2017-01-26 18:34:27 +00:00
|
|
|
})
|
|
|
|
})
|
2017-02-14 02:11:37 +00:00
|
|
|
|
2018-09-07 18:21:58 +00:00
|
|
|
describe('focus()', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('when the web contents is hidden', () => {
|
|
|
|
it('does not blur the focused window', (done) => {
|
2017-02-14 02:22:57 +00:00
|
|
|
ipcMain.once('answer', (event, parentFocused, childFocused) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(parentFocused).to.be.true()
|
|
|
|
expect(childFocused).to.be.false()
|
2017-02-14 02:22:57 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.show()
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'focus-web-contents.html'))
|
2017-02-14 02:11:37 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-02-15 22:14:40 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('getOSProcessId()', () => {
|
2018-12-19 06:44:03 +00:00
|
|
|
it('returns a valid procress id', async () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.webContents.getOSProcessId()).to.equal(0)
|
2017-05-15 17:57:19 +00:00
|
|
|
|
2018-12-19 06:44:03 +00:00
|
|
|
await w.loadURL('about:blank')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.webContents.getOSProcessId()).to.be.above(0)
|
2017-05-15 17:57:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-02-15 22:14:40 +00:00
|
|
|
describe('zoom api', () => {
|
|
|
|
const zoomScheme = remote.getGlobal('zoomScheme')
|
|
|
|
const hostZoomMap = {
|
2017-02-17 04:59:52 +00:00
|
|
|
host1: 0.3,
|
|
|
|
host2: 0.7,
|
|
|
|
host3: 0.2
|
2017-02-15 22:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
before((done) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
const protocol = session.defaultSession.protocol
|
2017-02-15 22:14:40 +00:00
|
|
|
protocol.registerStringProtocol(zoomScheme, (request, callback) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
const response = `<script>
|
|
|
|
const {ipcRenderer, remote} = require('electron')
|
|
|
|
ipcRenderer.send('set-zoom', window.location.hostname)
|
|
|
|
ipcRenderer.on(window.location.hostname + '-zoom-set', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
const { zoomLevel } = remote.getCurrentWebContents()
|
2019-01-21 07:40:27 +00:00
|
|
|
ipcRenderer.send(window.location.hostname + '-zoom-level', zoomLevel)
|
2017-02-15 22:14:40 +00:00
|
|
|
})
|
2017-02-17 04:59:52 +00:00
|
|
|
</script>`
|
2018-09-13 16:10:51 +00:00
|
|
|
callback({ data: response, mimeType: 'text/html' })
|
2017-02-15 22:14:40 +00:00
|
|
|
}, (error) => done(error))
|
|
|
|
})
|
|
|
|
|
|
|
|
after((done) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
const protocol = session.defaultSession.protocol
|
2017-02-15 22:14:40 +00:00
|
|
|
protocol.unregisterProtocol(zoomScheme, (error) => done(error))
|
|
|
|
})
|
|
|
|
|
2019-07-03 15:57:10 +00:00
|
|
|
// TODO(codebytere): remove in Electron v8.0.0
|
|
|
|
it('can set the correct zoom level (functions)', async () => {
|
2019-01-21 07:40:27 +00:00
|
|
|
try {
|
|
|
|
await w.loadURL('about:blank')
|
|
|
|
const zoomLevel = w.webContents.getZoomLevel()
|
|
|
|
expect(zoomLevel).to.eql(0.0)
|
|
|
|
w.webContents.setZoomLevel(0.5)
|
|
|
|
const newZoomLevel = w.webContents.getZoomLevel()
|
|
|
|
expect(newZoomLevel).to.eql(0.5)
|
|
|
|
} finally {
|
|
|
|
w.webContents.setZoomLevel(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-03 15:57:10 +00:00
|
|
|
it('can set the correct zoom level', async () => {
|
|
|
|
try {
|
|
|
|
await w.loadURL('about:blank')
|
|
|
|
const zoomLevel = w.webContents.zoomLevel
|
|
|
|
expect(zoomLevel).to.eql(0.0)
|
|
|
|
w.webContents.zoomLevel = 0.5
|
|
|
|
const newZoomLevel = w.webContents.zoomLevel
|
|
|
|
expect(newZoomLevel).to.eql(0.5)
|
|
|
|
} finally {
|
|
|
|
w.webContents.zoomLevel = 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-02-15 22:14:40 +00:00
|
|
|
it('can persist zoom level across navigation', (done) => {
|
|
|
|
let finalNavigation = false
|
|
|
|
ipcMain.on('set-zoom', (e, host) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
const zoomLevel = hostZoomMap[host]
|
2019-07-03 15:57:10 +00:00
|
|
|
if (!finalNavigation) w.webContents.zoomLevel = zoomLevel
|
|
|
|
console.log()
|
2017-02-15 22:14:40 +00:00
|
|
|
e.sender.send(`${host}-zoom-set`)
|
|
|
|
})
|
|
|
|
ipcMain.on('host1-zoom-level', (e, zoomLevel) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
const expectedZoomLevel = hostZoomMap.host1
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel).to.equal(expectedZoomLevel)
|
2017-02-15 22:14:40 +00:00
|
|
|
if (finalNavigation) {
|
|
|
|
done()
|
|
|
|
} else {
|
|
|
|
w.loadURL(`${zoomScheme}://host2`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ipcMain.once('host2-zoom-level', (e, zoomLevel) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
const expectedZoomLevel = hostZoomMap.host2
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel).to.equal(expectedZoomLevel)
|
2017-02-15 22:14:40 +00:00
|
|
|
finalNavigation = true
|
|
|
|
w.webContents.goBack()
|
|
|
|
})
|
|
|
|
w.loadURL(`${zoomScheme}://host1`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can propagate zoom level across same session', (done) => {
|
|
|
|
const w2 = new BrowserWindow({
|
|
|
|
show: false
|
|
|
|
})
|
|
|
|
w2.webContents.on('did-finish-load', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel1 = w.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel1).to.equal(hostZoomMap.host3)
|
2019-01-21 07:40:27 +00:00
|
|
|
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel2 = w2.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel1).to.equal(zoomLevel2)
|
2019-01-21 07:40:27 +00:00
|
|
|
w2.setClosable(true)
|
|
|
|
w2.close()
|
|
|
|
done()
|
2017-02-15 22:14:40 +00:00
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
w.webContents.zoomLevel = hostZoomMap.host3
|
2017-02-15 22:14:40 +00:00
|
|
|
w2.loadURL(`${zoomScheme}://host3`)
|
|
|
|
})
|
|
|
|
w.loadURL(`${zoomScheme}://host3`)
|
|
|
|
})
|
|
|
|
|
2017-02-16 09:56:33 +00:00
|
|
|
it('cannot propagate zoom level across different session', (done) => {
|
|
|
|
const w2 = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
partition: 'temp'
|
|
|
|
}
|
|
|
|
})
|
2017-02-17 04:59:52 +00:00
|
|
|
const protocol = w2.webContents.session.protocol
|
2017-02-16 09:56:33 +00:00
|
|
|
protocol.registerStringProtocol(zoomScheme, (request, callback) => {
|
|
|
|
callback('hello')
|
|
|
|
}, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
w2.webContents.on('did-finish-load', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel1 = w.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel1).to.equal(hostZoomMap.host3)
|
2019-01-21 07:40:27 +00:00
|
|
|
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel2 = w2.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel2).to.equal(0)
|
|
|
|
expect(zoomLevel1).to.not.equal(zoomLevel2)
|
2019-01-21 07:40:27 +00:00
|
|
|
|
|
|
|
protocol.unregisterProtocol(zoomScheme, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
w2.setClosable(true)
|
|
|
|
w2.close()
|
|
|
|
done()
|
2017-02-16 09:56:33 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
w.webContents.zoomLevel = hostZoomMap.host3
|
2017-02-16 09:56:33 +00:00
|
|
|
w2.loadURL(`${zoomScheme}://host3`)
|
|
|
|
})
|
|
|
|
w.loadURL(`${zoomScheme}://host3`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-02-15 22:14:40 +00:00
|
|
|
it('can persist when it contains iframe', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
2017-02-15 22:14:40 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
res.end()
|
2017-02-17 04:59:52 +00:00
|
|
|
}, 200)
|
2017-02-15 22:14:40 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
2017-02-15 22:14:40 +00:00
|
|
|
const url = 'http://127.0.0.1:' + server.address().port
|
|
|
|
const content = `<iframe src=${url}></iframe>`
|
|
|
|
w.webContents.on('did-frame-finish-load', (e, isMainFrame) => {
|
|
|
|
if (!isMainFrame) {
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel = w.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel).to.equal(2.0)
|
2019-01-21 07:40:27 +00:00
|
|
|
|
2019-07-03 15:57:10 +00:00
|
|
|
w.webContents.zoomLevel = 0
|
2019-01-21 07:40:27 +00:00
|
|
|
server.close()
|
|
|
|
done()
|
2017-02-15 22:14:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
w.webContents.on('dom-ready', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
w.webContents.zoomLevel = 2.0
|
2017-02-15 22:14:40 +00:00
|
|
|
})
|
|
|
|
w.loadURL(`data:text/html,${content}`)
|
|
|
|
})
|
|
|
|
})
|
2017-02-16 09:56:33 +00:00
|
|
|
|
|
|
|
it('cannot propagate when used with webframe', (done) => {
|
|
|
|
let finalZoomLevel = 0
|
|
|
|
const w2 = new BrowserWindow({
|
|
|
|
show: false
|
|
|
|
})
|
|
|
|
w2.webContents.on('did-finish-load', () => {
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel1 = w.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel1).to.equal(finalZoomLevel)
|
2019-01-21 07:40:27 +00:00
|
|
|
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel2 = w2.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel2).to.equal(0)
|
|
|
|
expect(zoomLevel1).to.not.equal(zoomLevel2)
|
2019-01-21 07:40:27 +00:00
|
|
|
|
|
|
|
w2.setClosable(true)
|
|
|
|
w2.close()
|
|
|
|
done()
|
2017-02-16 09:56:33 +00:00
|
|
|
})
|
|
|
|
ipcMain.once('temporary-zoom-set', (e, zoomLevel) => {
|
2018-09-04 14:50:53 +00:00
|
|
|
w2.loadFile(path.join(fixtures, 'pages', 'c.html'))
|
2017-02-16 09:56:33 +00:00
|
|
|
finalZoomLevel = zoomLevel
|
|
|
|
})
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webframe-zoom.html'))
|
2017-02-16 09:56:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('cannot persist zoom level after navigation with webFrame', (done) => {
|
|
|
|
let initialNavigation = true
|
|
|
|
const source = `
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.setZoomLevel(0.6)
|
|
|
|
ipcRenderer.send('zoom-level-set', webFrame.getZoomLevel())
|
|
|
|
`
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
if (initialNavigation) {
|
2019-04-30 14:08:33 +00:00
|
|
|
w.webContents.executeJavaScript(source)
|
2017-02-16 09:56:33 +00:00
|
|
|
} else {
|
2019-07-03 15:57:10 +00:00
|
|
|
const zoomLevel = w.webContents.zoomLevel
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel).to.equal(0)
|
2019-01-21 07:40:27 +00:00
|
|
|
done()
|
2017-02-16 09:56:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
ipcMain.once('zoom-level-set', (e, zoomLevel) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(zoomLevel).to.equal(0.6)
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'd.html'))
|
2017-02-16 09:56:33 +00:00
|
|
|
initialNavigation = false
|
|
|
|
})
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'c.html'))
|
2017-02-16 09:56:33 +00:00
|
|
|
})
|
2017-02-15 22:14:40 +00:00
|
|
|
})
|
2017-03-09 03:06:26 +00:00
|
|
|
|
|
|
|
describe('webrtc ip policy api', () => {
|
2017-03-09 17:53:01 +00:00
|
|
|
it('can set and get webrtc ip policies', () => {
|
2017-03-09 03:06:26 +00:00
|
|
|
const policies = [
|
|
|
|
'default',
|
|
|
|
'default_public_interface_only',
|
|
|
|
'default_public_and_private_interfaces',
|
|
|
|
'disable_non_proxied_udp'
|
|
|
|
]
|
|
|
|
policies.forEach((policy) => {
|
|
|
|
w.webContents.setWebRTCIPHandlingPolicy(policy)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(w.webContents.getWebRTCIPHandlingPolicy()).to.equal(policy)
|
2017-03-09 03:06:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-04-05 06:30:36 +00:00
|
|
|
|
2018-12-05 08:03:39 +00:00
|
|
|
describe('render view deleted events', () => {
|
|
|
|
let server = null
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
const respond = () => {
|
|
|
|
if (req.url === '/redirect-cross-site') {
|
|
|
|
res.setHeader('Location', `${server.cross_site_url}/redirected`)
|
|
|
|
res.statusCode = 302
|
|
|
|
res.end()
|
|
|
|
} else if (req.url === '/redirected') {
|
|
|
|
res.end('<html><script>window.localStorage</script></html>')
|
|
|
|
} else {
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(respond, 0)
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
server.url = `http://127.0.0.1:${server.address().port}`
|
|
|
|
server.cross_site_url = `http://localhost:${server.address().port}`
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close()
|
|
|
|
server = null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not emit current-render-view-deleted when speculative RVHs are deleted', (done) => {
|
|
|
|
let currentRenderViewDeletedEmitted = false
|
|
|
|
w.webContents.once('destroyed', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(currentRenderViewDeletedEmitted).to.be.false('current-render-view-deleted was emitted')
|
2018-12-05 08:03:39 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
const renderViewDeletedHandler = () => {
|
|
|
|
currentRenderViewDeletedEmitted = true
|
|
|
|
}
|
|
|
|
w.webContents.on('current-render-view-deleted', renderViewDeletedHandler)
|
|
|
|
w.webContents.on('did-finish-load', (e) => {
|
|
|
|
w.webContents.removeListener('current-render-view-deleted', renderViewDeletedHandler)
|
|
|
|
w.close()
|
|
|
|
})
|
|
|
|
w.loadURL(`${server.url}/redirect-cross-site`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits current-render-view-deleted if the current RVHs are deleted', (done) => {
|
|
|
|
let currentRenderViewDeletedEmitted = false
|
|
|
|
w.webContents.once('destroyed', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(currentRenderViewDeletedEmitted).to.be.true('current-render-view-deleted wasn\'t emitted')
|
2018-12-05 08:03:39 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.on('current-render-view-deleted', () => {
|
|
|
|
currentRenderViewDeletedEmitted = true
|
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', (e) => {
|
|
|
|
w.close()
|
|
|
|
})
|
|
|
|
w.loadURL(`${server.url}/redirect-cross-site`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits render-view-deleted if any RVHs are deleted', (done) => {
|
|
|
|
let rvhDeletedCount = 0
|
|
|
|
w.webContents.once('destroyed', () => {
|
|
|
|
const expectedRenderViewDeletedEventCount = 3 // 1 speculative upon redirection + 2 upon window close.
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(rvhDeletedCount).to.equal(expectedRenderViewDeletedEventCount, 'render-view-deleted wasn\'t emitted the expected nr. of times')
|
2018-12-05 08:03:39 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.on('render-view-deleted', () => {
|
|
|
|
rvhDeletedCount++
|
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', (e) => {
|
|
|
|
w.close()
|
|
|
|
})
|
|
|
|
w.loadURL(`${server.url}/redirect-cross-site`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('setIgnoreMenuShortcuts(ignore)', () => {
|
|
|
|
it('does not throw', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
|
|
|
w.webContents.setIgnoreMenuShortcuts(true)
|
|
|
|
w.webContents.setIgnoreMenuShortcuts(false)
|
|
|
|
}).to.not.throw()
|
2017-06-25 19:01:05 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-08 15:57:28 +00:00
|
|
|
describe('create()', () => {
|
|
|
|
it('does not crash on exit', async () => {
|
|
|
|
const appPath = path.join(__dirname, 'fixtures', 'api', 'leak-exit-webcontents.js')
|
|
|
|
const electronPath = remote.getGlobal('process').execPath
|
|
|
|
const appProcess = ChildProcess.spawn(electronPath, [appPath])
|
|
|
|
const [code] = await emittedOnce(appProcess, 'close')
|
|
|
|
expect(code).to.equal(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-09-14 08:18:45 +00:00
|
|
|
// Destroying webContents in its event listener is going to crash when
|
|
|
|
// Electron is built in Debug mode.
|
|
|
|
xdescribe('destroy()', () => {
|
2017-04-05 06:30:36 +00:00
|
|
|
let server
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
before((done) => {
|
2017-04-05 06:30:36 +00:00
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
switch (request.url) {
|
|
|
|
case '/404':
|
|
|
|
response.statusCode = '404'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/301':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/200':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end('hello')
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
done('unsupported endpoint')
|
|
|
|
}
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
server.url = 'http://127.0.0.1:' + server.address().port
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
after(() => {
|
2017-04-05 06:30:36 +00:00
|
|
|
server.close()
|
|
|
|
server = null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not crash when invoked synchronously inside navigation observer', (done) => {
|
|
|
|
const events = [
|
|
|
|
{ name: 'did-start-loading', url: `${server.url}/200` },
|
|
|
|
{ name: 'dom-ready', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-stop-loading', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-finish-load', url: `${server.url}/200` },
|
|
|
|
// FIXME: Multiple Emit calls inside an observer assume that object
|
|
|
|
// will be alive till end of the observer. Synchronous `destroy` api
|
|
|
|
// violates this contract and crashes.
|
|
|
|
// { name: 'did-frame-finish-load', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-fail-load', url: `${server.url}/404` }
|
|
|
|
]
|
|
|
|
const responseEvent = 'webcontents-destroyed'
|
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
function * genNavigationEvent () {
|
2017-04-05 06:30:36 +00:00
|
|
|
let eventOptions = null
|
|
|
|
while ((eventOptions = events.shift()) && events.length) {
|
|
|
|
eventOptions.responseEvent = responseEvent
|
|
|
|
ipcRenderer.send('test-webcontents-navigation-observer', eventOptions)
|
|
|
|
yield 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const gen = genNavigationEvent()
|
2017-04-05 06:30:36 +00:00
|
|
|
ipcRenderer.on(responseEvent, () => {
|
|
|
|
if (!gen.next().value) done()
|
|
|
|
})
|
|
|
|
gen.next()
|
|
|
|
})
|
|
|
|
})
|
2017-08-20 14:48:13 +00:00
|
|
|
|
|
|
|
describe('did-change-theme-color event', () => {
|
|
|
|
it('is triggered with correct theme color', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
let count = 0
|
2017-08-20 14:48:13 +00:00
|
|
|
w.webContents.on('did-change-theme-color', (e, color) => {
|
|
|
|
if (count === 0) {
|
2017-10-27 20:45:58 +00:00
|
|
|
count += 1
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(color).to.equal('#FFEEDD')
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
|
2017-08-20 14:48:13 +00:00
|
|
|
} else if (count === 1) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(color).to.be.null()
|
2017-08-20 14:48:13 +00:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'theme-color.html'))
|
2017-08-20 14:48:13 +00:00
|
|
|
})
|
|
|
|
})
|
2017-11-08 07:45:27 +00:00
|
|
|
|
|
|
|
describe('console-message event', () => {
|
|
|
|
it('is triggered with correct log message', (done) => {
|
|
|
|
w.webContents.on('console-message', (e, level, message) => {
|
2017-11-08 08:09:54 +00:00
|
|
|
// Don't just assert as Chromium might emit other logs that we should ignore.
|
|
|
|
if (message === 'a') {
|
|
|
|
done()
|
|
|
|
}
|
2017-11-08 07:45:27 +00:00
|
|
|
})
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'a.html'))
|
2017-11-08 07:45:27 +00:00
|
|
|
})
|
|
|
|
})
|
2018-04-05 23:13:24 +00:00
|
|
|
|
2019-01-22 16:47:58 +00:00
|
|
|
describe('ipc-message event', () => {
|
|
|
|
it('emits when the renderer process sends an asynchronous message', async () => {
|
|
|
|
const webContents = remote.getCurrentWebContents()
|
|
|
|
const promise = emittedOnce(webContents, 'ipc-message')
|
|
|
|
|
|
|
|
ipcRenderer.send('message', 'Hello World!')
|
|
|
|
|
|
|
|
const [, channel, message] = await promise
|
|
|
|
expect(channel).to.equal('message')
|
|
|
|
expect(message).to.equal('Hello World!')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('ipc-message-sync event', () => {
|
|
|
|
it('emits when the renderer process sends a synchronous message', async () => {
|
|
|
|
const webContents = remote.getCurrentWebContents()
|
|
|
|
const promise = emittedOnce(webContents, 'ipc-message-sync')
|
|
|
|
|
|
|
|
ipcRenderer.send('handle-next-ipc-message-sync', 'foobar')
|
|
|
|
const result = ipcRenderer.sendSync('message', 'Hello World!')
|
|
|
|
|
|
|
|
const [, channel, message] = await promise
|
|
|
|
expect(channel).to.equal('message')
|
|
|
|
expect(message).to.equal('Hello World!')
|
|
|
|
expect(result).to.equal('foobar')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-04-05 23:13:24 +00:00
|
|
|
describe('referrer', () => {
|
|
|
|
it('propagates referrer information to new target=_blank windows', (done) => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/should_have_referrer') {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(req.headers.referer).to.equal(`http://127.0.0.1:${server.address().port}/`)
|
2018-04-05 23:13:24 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
res.end('<a id="a" href="/should_have_referrer" target="_blank">link</a>')
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const url = 'http://127.0.0.1:' + server.address().port + '/'
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
w.webContents.once('new-window', (event, newUrl, frameName, disposition, options, features, referrer) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(referrer.url).to.equal(url)
|
|
|
|
expect(referrer.policy).to.equal('no-referrer-when-downgrade')
|
2018-04-05 23:13:24 +00:00
|
|
|
})
|
|
|
|
w.webContents.executeJavaScript('a.click()')
|
|
|
|
})
|
|
|
|
w.loadURL(url)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(jeremy): window.open() in a real browser passes the referrer, but
|
|
|
|
// our hacked-up window.open() shim doesn't. It should.
|
|
|
|
xit('propagates referrer information to windows opened with window.open', (done) => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/should_have_referrer') {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(req.headers.referer).to.equal(`http://127.0.0.1:${server.address().port}/`)
|
2018-04-05 23:13:24 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
res.end('')
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const url = 'http://127.0.0.1:' + server.address().port + '/'
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
w.webContents.once('new-window', (event, newUrl, frameName, disposition, options, features, referrer) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(referrer.url).to.equal(url)
|
|
|
|
expect(referrer.policy).to.equal('no-referrer-when-downgrade')
|
2018-04-05 23:13:24 +00:00
|
|
|
})
|
|
|
|
w.webContents.executeJavaScript('window.open(location.href + "should_have_referrer")')
|
|
|
|
})
|
|
|
|
w.loadURL(url)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-04-12 01:57:40 +00:00
|
|
|
|
|
|
|
describe('webframe messages in sandboxed contents', () => {
|
2019-03-14 19:08:54 +00:00
|
|
|
it('responds to executeJavaScript', async () => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await w.loadURL('about:blank')
|
|
|
|
const result = await w.webContents.executeJavaScript('37 + 5')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(result).to.equal(42)
|
2019-03-14 19:08:54 +00:00
|
|
|
})
|
2018-04-12 01:57:40 +00:00
|
|
|
})
|
2018-09-18 18:00:31 +00:00
|
|
|
|
2019-01-18 11:03:43 +00:00
|
|
|
describe('preload-error event', () => {
|
|
|
|
const generateSpecs = (description, sandbox) => {
|
|
|
|
describe(description, () => {
|
|
|
|
it('is triggered when unhandled exception is thrown', async () => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'preload-error-exception.js')
|
|
|
|
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox,
|
|
|
|
preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'preload-error')
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
|
|
|
|
const [, preloadPath, error] = await promise
|
|
|
|
expect(preloadPath).to.equal(preload)
|
|
|
|
expect(error.message).to.equal('Hello World!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('is triggered on syntax errors', async () => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'preload-error-syntax.js')
|
|
|
|
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox,
|
|
|
|
preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'preload-error')
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
|
|
|
|
const [, preloadPath, error] = await promise
|
|
|
|
expect(preloadPath).to.equal(preload)
|
|
|
|
expect(error.message).to.equal('foobar is not defined')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('is triggered when preload script loading fails', async () => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'preload-invalid.js')
|
|
|
|
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox,
|
|
|
|
preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'preload-error')
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
|
|
|
|
const [, preloadPath, error] = await promise
|
|
|
|
expect(preloadPath).to.equal(preload)
|
|
|
|
expect(error.message).to.contain('preload-invalid.js')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
generateSpecs('without sandbox', false)
|
|
|
|
generateSpecs('with sandbox', true)
|
|
|
|
})
|
|
|
|
|
2018-09-18 18:00:31 +00:00
|
|
|
describe('takeHeapSnapshot()', () => {
|
|
|
|
it('works with sandboxed renderers', async () => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-06 20:16:19 +00:00
|
|
|
await w.loadURL('about:blank')
|
2018-09-18 18:00:31 +00:00
|
|
|
|
|
|
|
const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot')
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(filePath)
|
|
|
|
} catch (e) {
|
|
|
|
// ignore error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await w.webContents.takeHeapSnapshot(filePath)
|
|
|
|
const stats = fs.statSync(filePath)
|
|
|
|
expect(stats.size).not.to.be.equal(0)
|
|
|
|
} finally {
|
|
|
|
cleanup()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('fails with invalid file path', async () => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-06 20:16:19 +00:00
|
|
|
await w.loadURL('about:blank')
|
2018-09-18 18:00:31 +00:00
|
|
|
|
|
|
|
const promise = w.webContents.takeHeapSnapshot('')
|
|
|
|
return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed')
|
|
|
|
})
|
|
|
|
})
|
2018-10-19 00:32:22 +00:00
|
|
|
|
|
|
|
describe('setBackgroundThrottling()', () => {
|
|
|
|
it('does not crash when allowing', (done) => {
|
|
|
|
w.webContents.setBackgroundThrottling(true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not crash when disallowing', (done) => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
w.webContents.setBackgroundThrottling(false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not crash when called via BrowserWindow', (done) => {
|
|
|
|
w.setBackgroundThrottling(true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2018-11-09 03:42:34 +00:00
|
|
|
|
|
|
|
describe('getPrinterList()', () => {
|
|
|
|
before(function () {
|
|
|
|
if (!features.isPrintingEnabled()) {
|
|
|
|
return closeWindow(w).then(() => {
|
|
|
|
w = null
|
|
|
|
this.skip()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-19 06:44:03 +00:00
|
|
|
it('can get printer list', async () => {
|
2018-11-09 03:42:34 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
2018-12-19 06:44:03 +00:00
|
|
|
await w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
|
|
|
|
const printers = w.webContents.getPrinters()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printers).to.be.an('array')
|
2018-11-09 03:42:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('printToPDF()', () => {
|
|
|
|
before(function () {
|
|
|
|
if (!features.isPrintingEnabled()) {
|
|
|
|
return closeWindow(w).then(() => {
|
|
|
|
w = null
|
|
|
|
this.skip()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-11 19:20:04 +00:00
|
|
|
it('can print to PDF', async () => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
|
|
|
|
const data = await w.webContents.printToPDF({})
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty()
|
2019-02-11 19:20:04 +00:00
|
|
|
})
|
2018-11-09 03:42:34 +00:00
|
|
|
})
|
2016-07-14 16:10:40 +00:00
|
|
|
})
|