2016-07-14 16:10:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('assert')
|
2017-02-15 22:14:40 +00:00
|
|
|
const http = require('http')
|
2016-07-14 16:10:40 +00:00
|
|
|
const path = require('path')
|
2016-08-03 19:47:53 +00:00
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2016-12-13 23:19:15 +00:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
2017-02-15 22:14:40 +00:00
|
|
|
const {BrowserWindow, webContents, ipcMain, session} = remote
|
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
|
|
|
|
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: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => closeWindow(w).then(() => { w = null }))
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('getAllWebContents() API', () => {
|
|
|
|
it('returns an array of web contents', (done) => {
|
|
|
|
w.webContents.on('devtools-opened', () => {
|
|
|
|
const all = webContents.getAllWebContents().sort((a, b) => {
|
2016-07-14 16:37:09 +00:00
|
|
|
return a.getId() - b.getId()
|
|
|
|
})
|
2016-07-14 16:10:40 +00:00
|
|
|
|
2016-08-16 23:19:05 +00:00
|
|
|
assert.ok(all.length >= 4)
|
2016-07-14 16:37:09 +00:00
|
|
|
assert.equal(all[0].getType(), 'window')
|
2016-08-16 23:19:05 +00:00
|
|
|
assert.equal(all[all.length - 2].getType(), 'remote')
|
|
|
|
assert.equal(all[all.length - 1].getType(), 'webview')
|
2016-07-14 16:10:40 +00:00
|
|
|
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'webview-zoom-factor.html')}`)
|
2016-07-14 16:10:40 +00:00
|
|
|
w.webContents.openDevTools()
|
|
|
|
})
|
|
|
|
})
|
2016-07-14 16:17:16 +00:00
|
|
|
|
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()
|
2016-07-14 16:17:16 +00:00
|
|
|
assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-opened', () => {
|
2016-07-14 16:17:16 +00:00
|
|
|
assert.equal(specWebContents.devToolsWebContents.getId(), webContents.getFocusedWebContents().getId())
|
|
|
|
specWebContents.closeDevTools()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-closed', () => {
|
2016-07-14 16:17:16 +00:00
|
|
|
assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
specWebContents.openDevTools()
|
|
|
|
})
|
2016-08-23 17:06:05 +00:00
|
|
|
|
2017-10-27 20:45: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', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-08-23 17:06:05 +00:00
|
|
|
webContents.getFocusedWebContents()
|
|
|
|
})
|
|
|
|
specWebContents.closeDevTools()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
specWebContents.once('devtools-closed', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-08-23 17:06:05 +00:00
|
|
|
webContents.getFocusedWebContents()
|
|
|
|
})
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
specWebContents.openDevTools({mode: 'detach'})
|
|
|
|
w.inspectElement(100, 100)
|
|
|
|
})
|
2016-07-14 16:17:16 +00:00
|
|
|
})
|
2016-08-15 21:03:31 +00:00
|
|
|
|
2017-11-30 12:04:50 +00:00
|
|
|
describe('setDevToolsWebCotnents() API', () => {
|
|
|
|
it('sets arbitry webContents as devtools', (done) => {
|
|
|
|
let devtools = new BrowserWindow({show: false})
|
|
|
|
devtools.webContents.once('dom-ready', () => {
|
|
|
|
assert.ok(devtools.getURL().startsWith('chrome-devtools://devtools'))
|
|
|
|
devtools.webContents.executeJavaScript('InspectorFrontendHost.constructor.name', (name) => {
|
|
|
|
assert.ok(name, 'InspectorFrontendHostImpl')
|
|
|
|
devtools.destroy()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
w.webContents.setDevToolsWebContents(devtools.webContents)
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('isFocused() API', () => {
|
|
|
|
it('returns false when the window is hidden', () => {
|
|
|
|
BrowserWindow.getAllWindows().forEach((window) => {
|
2016-08-15 21:03:31 +00:00
|
|
|
assert.equal(!window.isVisible() && window.webContents.isFocused(), false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-10-07 21:44:35 +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', () => {
|
|
|
|
assert(!w.devToolsWebContents.getWebPreferences())
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-06 22:41:18 +00:00
|
|
|
describe('before-input-event event', () => {
|
|
|
|
it('can prevent document keyboard events', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${path.join(__dirname, 'fixtures', 'pages', 'key-events.html')}`)
|
2016-12-06 22:41:18 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
ipcMain.once('keydown', (event, key) => {
|
|
|
|
assert.equal(key, 'b')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2016-12-13 23:19:15 +00:00
|
|
|
ipcRenderer.send('prevent-next-input-event', 'a', w.webContents.id)
|
2016-12-06 22:41:18 +00:00
|
|
|
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'a'})
|
|
|
|
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'b'})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('has the correct properties', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${path.join(__dirname, 'fixtures', 'pages', 'base-page.html')}`)
|
2016-12-06 22:41:18 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
const testBeforeInput = (opts) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
w.webContents.once('before-input-event', (event, input) => {
|
|
|
|
assert.equal(input.type, opts.type)
|
|
|
|
assert.equal(input.key, opts.key)
|
2017-02-02 08:23:07 +00:00
|
|
|
assert.equal(input.code, opts.code)
|
2016-12-06 22:41:18 +00:00
|
|
|
assert.equal(input.isAutoRepeat, opts.isAutoRepeat)
|
|
|
|
assert.equal(input.shift, opts.shift)
|
|
|
|
assert.equal(input.control, opts.control)
|
|
|
|
assert.equal(input.alt, opts.alt)
|
|
|
|
assert.equal(input.meta, opts.meta)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
w.webContents.sendInputEvent({
|
|
|
|
type: opts.type,
|
|
|
|
keyCode: opts.keyCode,
|
|
|
|
modifiers: modifiers
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyDown',
|
|
|
|
key: 'A',
|
2017-02-02 08:23:07 +00:00
|
|
|
code: 'KeyA',
|
2016-12-06 22:41:18 +00:00
|
|
|
keyCode: 'a',
|
|
|
|
shift: true,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: true
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '.',
|
2017-02-02 08:23:07 +00:00
|
|
|
code: 'Period',
|
2016-12-06 22:41:18 +00:00
|
|
|
keyCode: '.',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: false
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '!',
|
2017-02-02 08:23:07 +00:00
|
|
|
code: 'Digit1',
|
2016-12-06 22:41:18 +00:00
|
|
|
keyCode: '1',
|
|
|
|
shift: true,
|
|
|
|
control: false,
|
|
|
|
alt: false,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: false
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: 'Tab',
|
2017-02-02 08:23:07 +00:00
|
|
|
code: 'Tab',
|
2016-12-06 22:41:18 +00:00
|
|
|
keyCode: 'Tab',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: false,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: true
|
|
|
|
})
|
|
|
|
}).then(done).catch(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('sendInputEvent(event)', () => {
|
|
|
|
beforeEach((done) => {
|
|
|
|
w.loadURL(`file://${path.join(__dirname, 'fixtures', 'pages', 'key-events.html')}`)
|
|
|
|
w.webContents.once('did-finish-load', () => done())
|
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) => {
|
2016-10-07 21:49:21 +00:00
|
|
|
assert.equal(key, 'a')
|
|
|
|
assert.equal(code, 'KeyA')
|
2016-10-07 21:44:35 +00:00
|
|
|
assert.equal(keyCode, 65)
|
|
|
|
assert.equal(shiftKey, false)
|
|
|
|
assert.equal(ctrlKey, false)
|
|
|
|
assert.equal(altKey, false)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-07 22:12:35 +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) => {
|
2016-10-07 21:49:21 +00:00
|
|
|
assert.equal(key, 'Z')
|
|
|
|
assert.equal(code, 'KeyZ')
|
2016-10-07 21:44:35 +00:00
|
|
|
assert.equal(keyCode, 90)
|
|
|
|
assert.equal(shiftKey, true)
|
|
|
|
assert.equal(ctrlKey, true)
|
|
|
|
assert.equal(altKey, false)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-07 22:12:35 +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) => {
|
2016-10-07 21:49:21 +00:00
|
|
|
assert.equal(key, 'Tab')
|
|
|
|
assert.equal(code, 'Tab')
|
2016-10-07 21:44:35 +00:00
|
|
|
assert.equal(keyCode, 9)
|
|
|
|
assert.equal(shiftKey, false)
|
|
|
|
assert.equal(ctrlKey, false)
|
|
|
|
assert.equal(altKey, true)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-07 22:12:35 +00:00
|
|
|
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Tab', modifiers: ['alt']})
|
|
|
|
})
|
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) => {
|
2016-10-07 22:12:35 +00:00
|
|
|
assert.equal(key, 'a')
|
|
|
|
assert.equal(code, 'KeyA')
|
|
|
|
assert.equal(keyCode, 65)
|
|
|
|
assert.equal(shiftKey, false)
|
|
|
|
assert.equal(ctrlKey, false)
|
|
|
|
assert.equal(altKey, false)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-10 16:34:15 +00:00
|
|
|
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'A'})
|
2016-10-07 22:12:35 +00:00
|
|
|
w.webContents.sendInputEvent({type: 'char', keyCode: 'A'})
|
|
|
|
})
|
|
|
|
|
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) => {
|
2016-10-07 22:12:35 +00:00
|
|
|
assert.equal(key, 'Z')
|
|
|
|
assert.equal(code, 'KeyZ')
|
|
|
|
assert.equal(keyCode, 90)
|
|
|
|
assert.equal(shiftKey, true)
|
|
|
|
assert.equal(ctrlKey, true)
|
|
|
|
assert.equal(altKey, false)
|
|
|
|
done()
|
2016-10-07 21:44:35 +00:00
|
|
|
})
|
2016-10-10 16:34:15 +00:00
|
|
|
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Z'})
|
2016-10-07 22:12:35 +00:00
|
|
|
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
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('supports inserting CSS', (done) => {
|
2016-12-19 23:48:32 +00:00
|
|
|
w.loadURL('about:blank')
|
|
|
|
w.webContents.insertCSS('body { background-repeat: round; }')
|
|
|
|
w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")', (result) => {
|
|
|
|
assert.equal(result, 'round')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-12-20 17:07:48 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('supports inspecting an element in the devtools', (done) => {
|
2016-12-20 17:07:48 +00:00
|
|
|
w.loadURL('about:blank')
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.once('devtools-opened', () => {
|
2016-12-20 17:07:48 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
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', () => {
|
|
|
|
assert.throws(() => {
|
|
|
|
w.webContents.startDrag({icon: path.join(__dirname, 'fixtures', 'assets', 'logo.png')})
|
|
|
|
}, /Must specify either 'file' or 'files' option/)
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
w.webContents.startDrag({file: __filename})
|
2017-01-26 18:38:13 +00:00
|
|
|
}, /Must specify 'icon' option/)
|
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
assert.throws(() => {
|
|
|
|
w.webContents.startDrag({file: __filename, icon: __filename})
|
|
|
|
}, /Must specify non-empty 'icon' option/)
|
|
|
|
}
|
2017-01-26 18:34:27 +00:00
|
|
|
})
|
|
|
|
})
|
2017-02-14 02:11:37 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('focus()', () => {
|
|
|
|
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) => {
|
|
|
|
assert.equal(parentFocused, true)
|
|
|
|
assert.equal(childFocused, false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.show()
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${path.join(__dirname, '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()', () => {
|
|
|
|
it('returns a valid procress id', (done) => {
|
2017-05-15 20:34:30 +00:00
|
|
|
assert.strictEqual(w.webContents.getOSProcessId(), 0)
|
2017-05-15 17:57:19 +00:00
|
|
|
|
2017-05-17 23:53:30 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
const pid = w.webContents.getOSProcessId()
|
|
|
|
assert.equal(typeof pid, 'number')
|
|
|
|
assert(pid > 0, `pid ${pid} is not greater than 0`)
|
|
|
|
done()
|
|
|
|
})
|
2017-05-15 20:34:30 +00:00
|
|
|
w.loadURL('about:blank')
|
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', () => {
|
|
|
|
remote.getCurrentWebContents().getZoomLevel((zoomLevel) => {
|
|
|
|
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>`
|
2017-02-15 22:14:40 +00:00
|
|
|
callback({data: response, mimeType: 'text/html'})
|
|
|
|
}, (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))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can set the correct zoom level', (done) => {
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
|
assert.equal(zoomLevel, 0.0)
|
|
|
|
w.webContents.setZoomLevel(0.5)
|
|
|
|
w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
|
assert.equal(zoomLevel, 0.5)
|
|
|
|
w.webContents.setZoomLevel(0)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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]
|
2017-10-27 20:45:58 +00:00
|
|
|
if (!finalNavigation) w.webContents.setZoomLevel(zoomLevel)
|
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
|
2017-02-15 22:14:40 +00:00
|
|
|
assert.equal(zoomLevel, expectedZoomLevel)
|
|
|
|
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
|
2017-02-15 22:14:40 +00:00
|
|
|
assert.equal(zoomLevel, expectedZoomLevel)
|
|
|
|
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', () => {
|
|
|
|
w.webContents.getZoomLevel((zoomLevel1) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
assert.equal(zoomLevel1, hostZoomMap.host3)
|
2017-02-15 22:14:40 +00:00
|
|
|
w2.webContents.getZoomLevel((zoomLevel2) => {
|
|
|
|
assert.equal(zoomLevel1, zoomLevel2)
|
|
|
|
w2.setClosable(true)
|
|
|
|
w2.close()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2017-02-17 04:59:52 +00:00
|
|
|
w.webContents.setZoomLevel(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', () => {
|
|
|
|
w.webContents.getZoomLevel((zoomLevel1) => {
|
2017-02-17 04:59:52 +00:00
|
|
|
assert.equal(zoomLevel1, hostZoomMap.host3)
|
2017-02-16 09:56:33 +00:00
|
|
|
w2.webContents.getZoomLevel((zoomLevel2) => {
|
|
|
|
assert.equal(zoomLevel2, 0)
|
|
|
|
assert.notEqual(zoomLevel1, zoomLevel2)
|
|
|
|
protocol.unregisterProtocol(zoomScheme, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
w2.setClosable(true)
|
|
|
|
w2.close()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2017-02-17 04:59:52 +00:00
|
|
|
w.webContents.setZoomLevel(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) {
|
|
|
|
w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
|
assert.equal(zoomLevel, 2.0)
|
|
|
|
w.webContents.setZoomLevel(0)
|
|
|
|
server.close()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.webContents.on('dom-ready', () => {
|
|
|
|
w.webContents.setZoomLevel(2.0)
|
|
|
|
})
|
|
|
|
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', () => {
|
|
|
|
w.webContents.getZoomLevel((zoomLevel1) => {
|
|
|
|
assert.equal(zoomLevel1, finalZoomLevel)
|
|
|
|
w2.webContents.getZoomLevel((zoomLevel2) => {
|
|
|
|
assert.equal(zoomLevel2, 0)
|
|
|
|
assert.notEqual(zoomLevel1, zoomLevel2)
|
|
|
|
w2.setClosable(true)
|
|
|
|
w2.close()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
ipcMain.once('temporary-zoom-set', (e, zoomLevel) => {
|
2017-02-17 17:58:29 +00:00
|
|
|
w2.loadURL(`file://${fixtures}/pages/c.html`)
|
2017-02-16 09:56:33 +00:00
|
|
|
finalZoomLevel = zoomLevel
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${fixtures}/pages/webframe-zoom.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
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) {
|
|
|
|
w.webContents.executeJavaScript(source, () => {})
|
|
|
|
} else {
|
|
|
|
w.webContents.getZoomLevel((zoomLevel) => {
|
|
|
|
assert.equal(zoomLevel, 0)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ipcMain.once('zoom-level-set', (e, zoomLevel) => {
|
|
|
|
assert.equal(zoomLevel, 0.6)
|
2017-02-17 17:58:29 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/d.html`)
|
2017-02-16 09:56:33 +00:00
|
|
|
initialNavigation = false
|
|
|
|
})
|
2017-02-17 17:58:29 +00:00
|
|
|
w.loadURL(`file://${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)
|
|
|
|
assert.equal(w.webContents.getWebRTCIPHandlingPolicy(), policy)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-04-05 06:30:36 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('will-prevent-unload event', () => {
|
|
|
|
it('does not emit if beforeunload returns undefined', (done) => {
|
|
|
|
w.once('closed', () => {
|
2017-04-28 00:28:48 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.on('will-prevent-unload', (e) => {
|
2017-04-28 00:28:48 +00:00
|
|
|
assert.fail('should not have fired')
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits if beforeunload returns false', (done) => {
|
|
|
|
w.webContents.on('will-prevent-unload', () => {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html'))
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('supports calling preventDefault on will-prevent-unload events', (done) => {
|
2017-04-28 00:28:48 +00:00
|
|
|
ipcRenderer.send('prevent-next-will-prevent-unload', w.webContents.id)
|
|
|
|
w.once('closed', () => done())
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('setIgnoreMenuShortcuts(ignore)', () => {
|
|
|
|
it('does not throw', () => {
|
2017-06-28 17:21:01 +00:00
|
|
|
assert.equal(w.webContents.setIgnoreMenuShortcuts(true), undefined)
|
|
|
|
assert.equal(w.webContents.setIgnoreMenuShortcuts(false), undefined)
|
2017-06-25 19:01:05 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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: 'did-get-redirect-request', url: `${server.url}/301` },
|
|
|
|
{ name: 'did-get-response-details', 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let gen = genNavigationEvent()
|
|
|
|
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
|
2017-08-20 14:48:13 +00:00
|
|
|
assert.equal(color, '#FFEEDD')
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${path.join(__dirname, 'fixtures', 'pages', 'base-page.html')}`)
|
2017-08-20 14:48:13 +00:00
|
|
|
} else if (count === 1) {
|
|
|
|
assert.equal(color, null)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${path.join(__dirname, '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
|
|
|
})
|
|
|
|
w.loadURL(`file://${fixtures}/pages/a.html`)
|
|
|
|
})
|
|
|
|
})
|
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') {
|
|
|
|
assert.equal(req.headers.referer, 'http://127.0.0.1:' + server.address().port + '/')
|
|
|
|
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) => {
|
|
|
|
assert.equal(referrer.url, url)
|
|
|
|
assert.equal(referrer.policy, 'no-referrer-when-downgrade')
|
|
|
|
})
|
|
|
|
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') {
|
|
|
|
assert.equal(req.headers.referer, 'http://127.0.0.1:' + server.address().port + '/')
|
|
|
|
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) => {
|
|
|
|
assert.equal(referrer.url, url)
|
|
|
|
assert.equal(referrer.policy, 'no-referrer-when-downgrade')
|
|
|
|
})
|
|
|
|
w.webContents.executeJavaScript('window.open(location.href + "should_have_referrer")')
|
|
|
|
})
|
|
|
|
w.loadURL(url)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-07-14 16:10:40 +00:00
|
|
|
})
|