2016-07-14 16:10:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('assert')
|
|
|
|
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')
|
2016-10-07 21:44:35 +00:00
|
|
|
const {BrowserWindow, webContents, ipcMain} = 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
|
|
|
|
|
|
|
describe('webContents module', function () {
|
2016-07-14 16:33:16 +00:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
|
let w
|
2016-07-14 16:10:40 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
2016-08-03 19:47:53 +00:00
|
|
|
return closeWindow(w).then(function () { w = null })
|
2016-07-14 16:10:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('getAllWebContents() API', function () {
|
|
|
|
it('returns an array of web contents', function (done) {
|
|
|
|
w.webContents.on('devtools-opened', function () {
|
2016-07-14 16:37:09 +00:00
|
|
|
const all = webContents.getAllWebContents().sort(function (a, b) {
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'pages', 'webview-zoom-factor.html'))
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
})
|
|
|
|
})
|
2016-07-14 16:17:16 +00:00
|
|
|
|
|
|
|
describe('getFocusedWebContents() API', function () {
|
|
|
|
it('returns the focused web contents', function (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())
|
|
|
|
|
2016-07-21 08:08:43 +00:00
|
|
|
specWebContents.once('devtools-opened', function () {
|
2016-07-14 16:17:16 +00:00
|
|
|
assert.equal(specWebContents.devToolsWebContents.getId(), webContents.getFocusedWebContents().getId())
|
|
|
|
specWebContents.closeDevTools()
|
|
|
|
})
|
|
|
|
|
2016-07-21 08:08:43 +00:00
|
|
|
specWebContents.once('devtools-closed', function () {
|
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
|
|
|
|
|
|
|
it('does not crash when called on a detached dev tools window', function (done) {
|
|
|
|
const specWebContents = w.webContents
|
|
|
|
|
|
|
|
specWebContents.once('devtools-opened', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
webContents.getFocusedWebContents()
|
|
|
|
})
|
|
|
|
specWebContents.closeDevTools()
|
|
|
|
})
|
|
|
|
|
|
|
|
specWebContents.once('devtools-closed', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
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
|
|
|
|
|
|
|
describe('isFocused() API', function () {
|
|
|
|
it('returns false when the window is hidden', function () {
|
|
|
|
BrowserWindow.getAllWindows().forEach(function (window) {
|
|
|
|
assert.equal(!window.isVisible() && window.webContents.isFocused(), false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-10-07 21:44:35 +00:00
|
|
|
|
2016-12-06 22:41:18 +00:00
|
|
|
describe('before-input-event event', () => {
|
|
|
|
it('can prevent document keyboard events', (done) => {
|
|
|
|
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'key-events.html'))
|
|
|
|
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) => {
|
|
|
|
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'base-page.html'))
|
|
|
|
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)
|
|
|
|
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',
|
|
|
|
keyCode: 'a',
|
|
|
|
shift: true,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: true
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '.',
|
|
|
|
keyCode: '.',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: false
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '!',
|
|
|
|
keyCode: '1',
|
|
|
|
shift: true,
|
|
|
|
control: false,
|
|
|
|
alt: false,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: false
|
|
|
|
})
|
|
|
|
}).then(() => {
|
|
|
|
return testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: 'Tab',
|
|
|
|
keyCode: 'Tab',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: false,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: true
|
|
|
|
})
|
|
|
|
}).then(done).catch(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-07 21:44:35 +00:00
|
|
|
describe('sendInputEvent(event)', function () {
|
2016-10-07 22:12:35 +00:00
|
|
|
beforeEach(function (done) {
|
|
|
|
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'key-events.html'))
|
|
|
|
w.webContents.once('did-finish-load', function () {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-10-07 21:44:35 +00:00
|
|
|
|
2016-10-07 22:12:35 +00:00
|
|
|
it('can send keydown events', function (done) {
|
2016-10-07 21:44:35 +00:00
|
|
|
ipcMain.once('keydown', function (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
|
|
|
})
|
|
|
|
|
|
|
|
it('can send keydown events with modifiers', function (done) {
|
|
|
|
ipcMain.once('keydown', function (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
|
|
|
})
|
|
|
|
|
|
|
|
it('can send keydown events with special keys', function (done) {
|
|
|
|
ipcMain.once('keydown', function (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
|
|
|
|
2016-10-07 22:12:35 +00:00
|
|
|
it('can send char events', function (done) {
|
|
|
|
ipcMain.once('keypress', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
|
|
|
|
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'})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can send char events with modifiers', function (done) {
|
|
|
|
ipcMain.once('keypress', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
|
|
|
|
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
|
|
|
|
|
|
|
it('supports inserting CSS', function (done) {
|
|
|
|
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
|
|
|
|
|
|
|
it('supports inspecting an element in the devtools', function (done) {
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
w.webContents.once('devtools-opened', function () {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.inspectElement(10, 10)
|
|
|
|
})
|
2016-07-14 16:10:40 +00:00
|
|
|
})
|