electron/spec/api-web-contents-spec.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-07-14 16:10:40 +00:00
'use strict'
const assert = require('assert')
const path = require('path')
const {closeWindow} = require('./window-helpers')
2016-07-14 16:10:40 +00:00
const {remote} = require('electron')
2016-07-14 16:25:59 +00:00
const {BrowserWindow, webContents} = remote
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 () {
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 () {
const all = webContents.getAllWebContents().sort(function (a, b) {
return a.getId() - b.getId()
})
2016-07-14 16:10:40 +00:00
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')
2016-07-14 16:10:40 +00:00
done()
})
w.loadURL('file://' + path.join(fixtures, 'pages', 'webview-zoom-factor.html'))
w.webContents.openDevTools()
})
})
describe('getFocusedWebContents() API', function () {
if (isCi) {
return
}
it('returns the focused web contents', function (done) {
2016-07-14 16:33:16 +00:00
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()
})
})
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-07-14 16:10:40 +00:00
})