123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- 'use strict'
- const assert = require('assert')
- const path = require('path')
- const {closeWindow} = require('./window-helpers')
- const {ipcRenderer, remote} = require('electron')
- const {BrowserWindow, webContents, ipcMain} = remote
- const isCi = remote.getGlobal('isCi')
- describe('webContents module', function () {
- const fixtures = path.resolve(__dirname, 'fixtures')
- let w
- beforeEach(function () {
- w = new BrowserWindow({
- show: false,
- width: 400,
- height: 400,
- webPreferences: {
- backgroundThrottling: false
- }
- })
- })
- afterEach(function () {
- return closeWindow(w).then(function () { w = null })
- })
- describe('getAllWebContents() API', function () {
- it('returns an array of web contents', function (done) {
- w.webContents.on('devtools-opened', function () {
- const all = webContents.getAllWebContents().sort(function (a, b) {
- return a.getId() - b.getId()
- })
- assert.ok(all.length >= 4)
- assert.equal(all[0].getType(), 'window')
- assert.equal(all[all.length - 2].getType(), 'remote')
- assert.equal(all[all.length - 1].getType(), 'webview')
- done()
- })
- w.loadURL('file://' + path.join(fixtures, 'pages', 'webview-zoom-factor.html'))
- w.webContents.openDevTools()
- })
- })
- describe('getFocusedWebContents() API', function () {
- it('returns the focused web contents', function (done) {
- if (isCi) return done()
- const specWebContents = remote.getCurrentWebContents()
- assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
- specWebContents.once('devtools-opened', function () {
- assert.equal(specWebContents.devToolsWebContents.getId(), webContents.getFocusedWebContents().getId())
- specWebContents.closeDevTools()
- })
- specWebContents.once('devtools-closed', function () {
- assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
- done()
- })
- specWebContents.openDevTools()
- })
- 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)
- })
- })
- 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)
- })
- })
- })
- 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()
- })
- ipcRenderer.send('prevent-next-input-event', 'a', w.webContents.id)
- 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)
- })
- })
- })
- describe('sendInputEvent(event)', function () {
- beforeEach(function (done) {
- w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'key-events.html'))
- w.webContents.once('did-finish-load', function () {
- done()
- })
- })
- it('can send keydown events', function (done) {
- ipcMain.once('keydown', 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()
- })
- w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'A'})
- })
- it('can send keydown events with modifiers', function (done) {
- ipcMain.once('keydown', 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()
- })
- w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Z', modifiers: ['shift', 'ctrl']})
- })
- it('can send keydown events with special keys', function (done) {
- ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
- assert.equal(key, 'Tab')
- assert.equal(code, 'Tab')
- assert.equal(keyCode, 9)
- assert.equal(shiftKey, false)
- assert.equal(ctrlKey, false)
- assert.equal(altKey, true)
- done()
- })
- w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Tab', modifiers: ['alt']})
- })
- 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()
- })
- w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'A'})
- 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()
- })
- w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Z'})
- w.webContents.sendInputEvent({type: 'char', keyCode: 'Z', modifiers: ['shift', 'ctrl']})
- })
- })
- })
|