2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2018-05-14 18:17:31 +00:00
|
|
|
const {expect} = require('chai')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
|
|
|
const http = require('http')
|
|
|
|
const url = require('url')
|
2017-02-03 20:21:46 +00:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
2017-01-16 20:56:39 +00:00
|
|
|
const {app, session, getGuestWebContents, ipcMain, BrowserWindow, webContents} = remote
|
2016-11-03 23:43:16 +00:00
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-03-30 17:33:15 +00:00
|
|
|
|
2017-07-11 11:12:57 +00:00
|
|
|
const isCI = remote.getGlobal('isCi')
|
2017-05-25 23:40:15 +00:00
|
|
|
const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
|
2017-01-16 20:56:39 +00:00
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
/* Most of the APIs here don't use standard callbacks */
|
|
|
|
/* eslint-disable standard/no-callback-literal */
|
|
|
|
|
2017-05-07 05:14:52 +00:00
|
|
|
describe('<webview> tag', function () {
|
2016-12-22 21:31:30 +00:00
|
|
|
this.timeout(3 * 60 * 1000)
|
2016-02-05 18:27:05 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
|
|
|
let webview = null
|
2016-05-17 08:04:13 +00:00
|
|
|
let w = null
|
2016-02-05 18:27:05 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const waitForEvent = (eventTarget, eventName) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
eventTarget.addEventListener(eventName, resolve, {once: true})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadWebView = (webview, attributes = {}) => {
|
|
|
|
for (const [name, value] of Object.entries(attributes)) {
|
|
|
|
webview.setAttribute(name, value)
|
|
|
|
}
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
return waitForEvent(webview, 'did-finish-load')
|
|
|
|
}
|
|
|
|
|
|
|
|
const startLoadingWebViewAndWaitForMessage = (webview, attributes = {}) => {
|
|
|
|
loadWebView(webview, attributes) // Don't wait for load to be finished.
|
|
|
|
return waitForEvent(webview, 'console-message')
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach(() => {
|
2016-03-28 20:47:31 +00:00
|
|
|
webview = new WebView()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-02-05 18:27:05 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => {
|
2016-09-30 16:55:24 +00:00
|
|
|
if (!document.body.contains(webview)) {
|
|
|
|
document.body.appendChild(webview)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-09-30 16:55:24 +00:00
|
|
|
webview.remove()
|
2017-10-27 20:45:58 +00:00
|
|
|
return closeWindow(w).then(() => { w = null })
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('works without script tag in page', (done) => {
|
2016-05-17 08:04:13 +00:00
|
|
|
w = new BrowserWindow({show: false})
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.once('pong', () => done())
|
2016-04-12 06:10:26 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/pages/webview-no-script.html')
|
2017-05-07 05:10:42 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('is disabled when nodeIntegration is disabled', (done) => {
|
2017-05-07 05:10:42 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false,
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-webview.js')
|
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.once('webview', (event, type) => {
|
2017-05-07 05:10:42 +00:00
|
|
|
if (type === 'undefined') {
|
|
|
|
done()
|
|
|
|
} else {
|
|
|
|
done('WebView still exists')
|
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-no-script.html`)
|
2017-05-07 05:10:42 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('is enabled when the webviewTag option is enabled and the nodeIntegration option is disabled', (done) => {
|
2017-05-07 05:10:42 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false,
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-webview.js'),
|
2017-05-17 20:09:24 +00:00
|
|
|
webviewTag: true
|
2017-05-07 05:10:42 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.once('webview', (event, type) => {
|
2017-05-07 05:10:42 +00:00
|
|
|
if (type !== 'undefined') {
|
|
|
|
done()
|
|
|
|
} else {
|
|
|
|
done('WebView is not created')
|
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-no-script.html`)
|
2016-04-12 06:10:26 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('src attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('specifies the page to load', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(message, 'a')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('navigates to new page when changed', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
webview.src = `file://${fixtures}/pages/b.html`
|
|
|
|
|
|
|
|
const {message} = await waitForEvent(webview, 'console-message')
|
|
|
|
assert.equal(message, 'b')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-09-08 23:40:31 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('resolves relative URLs', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: '../fixtures/pages/e.html'
|
|
|
|
})
|
|
|
|
assert.equal(message, 'Window script is loaded before preload script')
|
2016-09-08 23:56:29 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('ignores empty values', () => {
|
2016-09-08 23:40:31 +00:00
|
|
|
assert.equal(webview.src, '')
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
for (const emptyValue of ['', null, undefined]) {
|
|
|
|
webview.src = emptyValue
|
|
|
|
assert.equal(webview.src, '')
|
|
|
|
}
|
2016-09-08 23:40:31 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('nodeintegration attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('inserts no node symbols when not set', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/c.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
assert.equal(message, '{"require":"undefined","module":"undefined","process":"undefined","global":"undefined"}')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('inserts node symbols when set', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/d.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
assert.equal(message, '{"require":"function","module":"object","process":"object"}')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('loads node symbols after POST navigation when set', async function () {
|
2017-03-27 16:15:40 +00:00
|
|
|
// FIXME Figure out why this is timing out on AppVeyor
|
2017-11-15 21:05:46 +00:00
|
|
|
if (process.env.APPVEYOR === 'True') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test.
|
2018-05-14 22:00:49 +00:00
|
|
|
this.skip()
|
|
|
|
return
|
2017-11-15 21:05:46 +00:00
|
|
|
}
|
2017-03-27 16:15:40 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/post.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
assert.equal(message, '{"require":"function","module":"object","process":"object"}')
|
2016-03-30 17:33:15 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('disables node integration on child windows when it is disabled on the webview', (done) => {
|
|
|
|
app.once('browser-window-created', (event, window) => {
|
2016-03-30 18:17:24 +00:00
|
|
|
assert.equal(window.webContents.getWebPreferences().nodeIntegration, false)
|
2016-03-30 17:33:15 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const src = url.format({
|
2016-03-30 17:33:15 +00:00
|
|
|
pathname: `${fixtures}/pages/webview-opener-no-node-integration.html`,
|
|
|
|
protocol: 'file',
|
|
|
|
query: {
|
|
|
|
p: `${fixtures}/pages/window-opener-node.html`
|
|
|
|
},
|
|
|
|
slashes: true
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
src
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-02-05 18:27:05 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('loads native modules when navigation happens', async function () {
|
|
|
|
if (!nativeModulesEnabled) {
|
|
|
|
this.skip()
|
|
|
|
return
|
2017-05-25 23:40:15 +00:00
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
await loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/native-module.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
webview.reload()
|
|
|
|
|
|
|
|
const {message} = await waitForEvent(webview, 'console-message')
|
|
|
|
assert.equal(message, 'function')
|
2017-05-25 23:40:15 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('preload attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('loads the script before other scripts in window', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
preload: `${fixtures}/module/preload.js`,
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(message).to.be.a('string')
|
|
|
|
expect(message).to.be.not.equal('Window script is loaded before preload script')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('preload script can still use "process" and "Buffer" when nodeintegration is off', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
preload: `${fixtures}/module/preload-node-off.js`,
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
const types = JSON.parse(message)
|
|
|
|
expect(types).to.include({
|
|
|
|
process: 'object',
|
|
|
|
Buffer: 'function'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('preload script can require modules that still use "process" and "Buffer" when nodeintegration is off', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
preload: `${fixtures}/module/preload-node-off-wrapper.js`,
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
const types = JSON.parse(message)
|
|
|
|
expect(types).to.include({
|
|
|
|
process: 'object',
|
|
|
|
Buffer: 'function'
|
2017-02-23 19:26:37 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('receives ipc message in preload script', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const message = 'boom!'
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.setAttribute('preload', `${fixtures}/module/preload-ipc.js`)
|
|
|
|
webview.src = `file://${fixtures}/pages/e.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
document.body.appendChild(webview)
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
await waitForEvent(webview, 'did-finish-load')
|
|
|
|
webview.send('ping', message)
|
|
|
|
const e = await waitForEvent(webview, 'ipc-message')
|
|
|
|
|
|
|
|
assert.equal(e.channel, 'pong')
|
|
|
|
assert.deepEqual(e.args, [message])
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-12 05:57:40 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('works without script tag in page', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
preload: `${fixtures}/module/preload.js`,
|
|
|
|
src: `file://${fixtures}pages/base-page.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
const types = JSON.parse(message)
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function',
|
|
|
|
module: 'object',
|
|
|
|
process: 'object',
|
|
|
|
Buffer: 'function'
|
|
|
|
})
|
2016-04-12 05:57:40 +00:00
|
|
|
})
|
2016-09-08 23:40:31 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('resolves relative URLs', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
preload: '../fixtures/module/preload.js',
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
const types = JSON.parse(message)
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function',
|
|
|
|
module: 'object',
|
|
|
|
process: 'object',
|
|
|
|
Buffer: 'function'
|
|
|
|
})
|
2016-09-08 23:56:29 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('ignores empty values', () => {
|
2016-09-08 23:40:31 +00:00
|
|
|
assert.equal(webview.preload, '')
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
for (const emptyValue of ['', null, undefined]) {
|
|
|
|
webview.preload = emptyValue
|
|
|
|
assert.equal(webview.preload, '')
|
|
|
|
}
|
2016-09-08 23:40:31 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('httpreferrer attribute', () => {
|
|
|
|
it('sets the referrer url', (done) => {
|
|
|
|
const referrer = 'http://github.com/'
|
2018-04-06 07:22:52 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.end()
|
|
|
|
server.close()
|
|
|
|
assert.equal(req.headers.referer, referrer)
|
|
|
|
done()
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
const port = server.address().port
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
httpreferrer: referrer,
|
|
|
|
src: `http://127.0.0.1:${port}`
|
|
|
|
})
|
2018-04-06 07:22:52 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('useragent attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('sets the user agent', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const referrer = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko'
|
2018-05-14 22:00:49 +00:00
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/useragent.html`,
|
|
|
|
useragent: referrer
|
|
|
|
})
|
|
|
|
assert.equal(message, referrer)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('disablewebsecurity attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('does not disable web security when not set', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
|
|
|
|
const src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`
|
|
|
|
const encoded = btoa(unescape(encodeURIComponent(src)))
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `data:text/html;base64,${encoded}`
|
|
|
|
})
|
|
|
|
expect(message).to.be.a('string')
|
|
|
|
expect(message).to.contain('Not allowed to load local resource')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('disables web security when set', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
|
|
|
|
const src = `<script src='file://${jqueryPath}'></script> <script>console.log('ok');</script>`
|
|
|
|
const encoded = btoa(unescape(encodeURIComponent(src)))
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
disablewebsecurity: '',
|
|
|
|
src: `data:text/html;base64,${encoded}`
|
|
|
|
})
|
|
|
|
assert.equal(message, 'ok')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-05-30 06:20:53 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('does not break node integration', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
disablewebsecurity: '',
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/d.html`
|
2016-05-30 06:20:53 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(message, '{"require":"function","module":"object","process":"object"}')
|
2016-05-30 06:20:53 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('does not break preload script', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
disablewebsecurity: '',
|
|
|
|
preload: `${fixtures}/module/preload.js`,
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
})
|
|
|
|
assert.equal(message, '{"require":"function","module":"object","process":"object","Buffer":"function"}')
|
2016-05-30 06:20:53 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('partition attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('inserts no node symbols when not set', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
partition: 'test1',
|
|
|
|
src: `file://${fixtures}/pages/c.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(message, '{"require":"undefined","module":"undefined","process":"undefined","global":"undefined"}')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('inserts node symbols when set', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
partition: 'test2',
|
|
|
|
src: `file://${fixtures}/pages/d.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(message, '{"require":"function","module":"object","process":"object"}')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('isolates storage for different id', async () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
window.localStorage.setItem('test', 'one')
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
partition: 'test3',
|
|
|
|
src: `file://${fixtures}/pages/partition/one.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
const parsedMessage = JSON.parse(message)
|
|
|
|
expect(parsedMessage).to.include({
|
|
|
|
numberOfEntries: 0,
|
|
|
|
testValue: null
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('uses current session storage when no id is provided', async () => {
|
|
|
|
const testValue = 'one'
|
|
|
|
window.localStorage.setItem('test', testValue)
|
|
|
|
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/partition/one.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
const parsedMessage = JSON.parse(message)
|
|
|
|
expect(parsedMessage).to.include({
|
|
|
|
numberOfEntries: 1,
|
|
|
|
testValue
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('allowpopups attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can not open new window when not set', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/window-open-hide.html`
|
|
|
|
})
|
|
|
|
assert.equal(message, 'null')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can open new window when set', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
src: `file://${fixtures}/pages/window-open-hide.html`
|
|
|
|
})
|
|
|
|
assert.equal(message, 'window')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('webpreferences attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can enable nodeintegration', async () => {
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/d.html`,
|
|
|
|
webpreferences: 'nodeIntegration'
|
2016-10-14 23:04:33 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(message, '{"require":"function","module":"object","process":"object"}')
|
2016-10-14 23:04:33 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can disables web security and enable nodeintegration', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js')
|
2018-05-14 22:00:49 +00:00
|
|
|
const src = `<script src='file://${jqueryPath}'></script> <script>console.log(typeof require);</script>`
|
2017-10-27 20:45:58 +00:00
|
|
|
const encoded = btoa(unescape(encodeURIComponent(src)))
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `data:text/html;base64,${encoded}`,
|
|
|
|
webpreferences: 'webSecurity=no, nodeIntegration=yes'
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.equal(message, 'function')
|
2016-10-14 23:04:33 +00:00
|
|
|
})
|
2017-01-05 18:17:06 +00:00
|
|
|
|
|
|
|
it('can enable context isolation', (done) => {
|
|
|
|
ipcMain.once('isolated-world', (event, data) => {
|
|
|
|
assert.deepEqual(data, {
|
|
|
|
preloadContext: {
|
|
|
|
preloadProperty: 'number',
|
|
|
|
pageProperty: 'undefined',
|
|
|
|
typeofRequire: 'function',
|
|
|
|
typeofProcess: 'object',
|
|
|
|
typeofArrayPush: 'function',
|
|
|
|
typeofFunctionApply: 'function'
|
|
|
|
},
|
|
|
|
pageContext: {
|
|
|
|
preloadProperty: 'undefined',
|
|
|
|
pageProperty: 'string',
|
|
|
|
typeofRequire: 'undefined',
|
|
|
|
typeofProcess: 'undefined',
|
|
|
|
typeofArrayPush: 'number',
|
|
|
|
typeofFunctionApply: 'boolean',
|
2017-01-12 00:36:59 +00:00
|
|
|
typeofPreloadExecuteJavaScriptProperty: 'number',
|
2017-05-22 18:10:10 +00:00
|
|
|
typeofOpenedWindow: 'object'
|
2017-01-05 18:17:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
allowpopups: 'yes',
|
|
|
|
preload: path.join(fixtures, 'api', 'isolated-preload.js'),
|
|
|
|
src: `file://${fixtures}/api/isolated.html`,
|
|
|
|
webpreferences: 'contextIsolation=yes'
|
|
|
|
})
|
2017-01-05 18:17:06 +00:00
|
|
|
})
|
2016-10-14 23:04:33 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('new-window event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when window.open is called', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'new-window')
|
|
|
|
|
|
|
|
assert.equal(event.url, 'http://host/')
|
|
|
|
assert.equal(event.frameName, 'host')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when link with target is called', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/target-name.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'new-window')
|
|
|
|
|
|
|
|
assert.equal(event.url, 'http://host/')
|
|
|
|
assert.equal(event.frameName, 'target')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ipc-message event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when guest sends a ipc message to browser', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/ipc-message.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'ipc-message')
|
|
|
|
|
|
|
|
assert.equal(event.channel, 'channel')
|
|
|
|
assert.deepEqual(event.args, ['arg1', 'arg2'])
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('page-title-set event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when title is set', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'page-title-set')
|
|
|
|
|
|
|
|
assert.equal(event.title, 'test')
|
|
|
|
assert(event.explicitSet)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('page-favicon-updated event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when favicon urls are received', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const {favicons} = await waitForEvent(webview, 'page-favicon-updated')
|
|
|
|
|
|
|
|
assert(favicons)
|
|
|
|
assert.equal(favicons.length, 2)
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
assert(/^file:\/\/\/[A-Z]:\/favicon.png$/i.test(favicons[0]))
|
|
|
|
} else {
|
|
|
|
assert.equal(favicons[0], 'file:///favicon.png')
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('will-navigate event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when a url that leads to oustide of the page is clicked', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/webview-will-navigate.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const {url} = await waitForEvent(webview, 'will-navigate')
|
|
|
|
|
|
|
|
assert.equal(url, 'http://host/')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('did-navigate event', () => {
|
|
|
|
let p = path.join(fixtures, 'pages', 'webview-will-navigate.html')
|
2016-03-25 20:03:49 +00:00
|
|
|
p = p.replace(/\\/g, '/')
|
2017-10-27 20:45:58 +00:00
|
|
|
const pageUrl = url.format({
|
2016-01-12 02:40:23 +00:00
|
|
|
protocol: 'file',
|
|
|
|
slashes: true,
|
|
|
|
pathname: p
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when a url that leads to outside of the page is clicked', async () => {
|
|
|
|
loadWebView(webview, {src: pageUrl})
|
|
|
|
const {url} = await waitForEvent(webview, 'did-navigate')
|
|
|
|
|
|
|
|
assert.equal(url, pageUrl)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('did-navigate-in-page event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when an anchor link is clicked', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
let p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page.html')
|
2016-03-25 20:03:49 +00:00
|
|
|
p = p.replace(/\\/g, '/')
|
2017-10-27 20:45:58 +00:00
|
|
|
const pageUrl = url.format({
|
2016-01-12 02:40:23 +00:00
|
|
|
protocol: 'file',
|
|
|
|
slashes: true,
|
|
|
|
pathname: p
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {src: pageUrl})
|
|
|
|
const event = await waitForEvent(webview, 'did-navigate-in-page')
|
|
|
|
assert.equal(event.url, `${pageUrl}#test_content`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when window.history.replaceState is called', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/webview-did-navigate-in-page-with-history.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const {url} = await waitForEvent(webview, 'did-navigate-in-page')
|
|
|
|
assert.equal(url, 'http://host/')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when window.location.hash is changed', async () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
let p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page-with-hash.html')
|
2016-03-25 20:03:49 +00:00
|
|
|
p = p.replace(/\\/g, '/')
|
2017-10-27 20:45:58 +00:00
|
|
|
const pageUrl = url.format({
|
2016-01-12 02:40:23 +00:00
|
|
|
protocol: 'file',
|
|
|
|
slashes: true,
|
|
|
|
pathname: p
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {src: pageUrl})
|
|
|
|
const event = await waitForEvent(webview, 'did-navigate-in-page')
|
|
|
|
assert.equal(event.url, `${pageUrl}#test`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('close event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should fire when interior page calls window.close', async () => {
|
|
|
|
loadWebView(webview, {src: `file://${fixtures}/pages/close.html`})
|
|
|
|
await waitForEvent(webview, 'close')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-04-06 07:22:52 +00:00
|
|
|
describe('setDevToolsWebContents() API', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('sets webContents of webview as devtools', async () => {
|
2017-11-30 12:04:50 +00:00
|
|
|
const webview2 = new WebView()
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview2)
|
|
|
|
await waitForEvent(webview2, 'did-attach')
|
|
|
|
|
|
|
|
// Setup an event handler for further usage.
|
|
|
|
const waitForDomReady = waitForEvent(webview2, 'dom-ready')
|
|
|
|
|
|
|
|
loadWebView(webview, {src: 'about:blank'})
|
|
|
|
await waitForEvent(webview, 'dom-ready')
|
|
|
|
webview.getWebContents().setDevToolsWebContents(webview2.getWebContents())
|
|
|
|
webview.getWebContents().openDevTools()
|
|
|
|
|
|
|
|
await waitForDomReady
|
|
|
|
|
|
|
|
// Its WebContents should be a DevTools.
|
|
|
|
const devtools = webview2.getWebContents()
|
|
|
|
assert.ok(devtools.getURL().startsWith('chrome-devtools://devtools'))
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
devtools.executeJavaScript('InspectorFrontendHost.constructor.name', (name) => {
|
|
|
|
document.body.removeChild(webview2)
|
|
|
|
expect(name).to.be.equal('InspectorFrontendHostImpl')
|
|
|
|
resolve()
|
2017-11-30 12:04:50 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('devtools-opened event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should fire when webview.openDevTools() is called', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/base-page.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'dom-ready')
|
|
|
|
|
|
|
|
webview.openDevTools()
|
|
|
|
await waitForEvent(webview, 'devtools-opened')
|
|
|
|
|
|
|
|
webview.closeDevTools()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('devtools-closed event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should fire when webview.closeDevTools() is called', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/base-page.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'dom-ready')
|
|
|
|
|
|
|
|
webview.openDevTools()
|
|
|
|
await waitForEvent(webview, 'devtools-opened')
|
|
|
|
|
|
|
|
webview.closeDevTools()
|
|
|
|
await waitForEvent(webview, 'devtools-closed')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('devtools-focused event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should fire when webview.openDevTools() is called', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/base-page.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const waitForDevToolsFocused = waitForEvent(webview, 'devtools-focused')
|
|
|
|
|
|
|
|
await waitForEvent(webview, 'dom-ready')
|
|
|
|
webview.openDevTools()
|
|
|
|
|
|
|
|
await waitForDevToolsFocused
|
|
|
|
webview.closeDevTools()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('<webview>.reload()', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should emit beforeunload handler', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/beforeunload-false.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
// Event handler has to be added before reload.
|
|
|
|
const waitForOnbeforeunload = waitForEvent(webview, 'ipc-message')
|
|
|
|
|
|
|
|
webview.reload()
|
|
|
|
|
|
|
|
const {channel} = await waitForOnbeforeunload
|
|
|
|
assert.equal(channel, 'onbeforeunload')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('<webview>.goForward()', () => {
|
|
|
|
it('should work after a replaced history entry', (done) => {
|
|
|
|
let loadCount = 1
|
|
|
|
const listener = (e) => {
|
2016-09-17 00:06:16 +00:00
|
|
|
if (loadCount === 1) {
|
|
|
|
assert.equal(e.channel, 'history')
|
|
|
|
assert.equal(e.args[0], 1)
|
|
|
|
assert(!webview.canGoBack())
|
|
|
|
assert(!webview.canGoForward())
|
|
|
|
} else if (loadCount === 2) {
|
|
|
|
assert.equal(e.channel, 'history')
|
|
|
|
assert.equal(e.args[0], 2)
|
|
|
|
assert(!webview.canGoBack())
|
|
|
|
assert(webview.canGoForward())
|
|
|
|
webview.removeEventListener('ipc-message', listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const loadListener = () => {
|
2016-09-17 00:06:16 +00:00
|
|
|
if (loadCount === 1) {
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/base-page.html`
|
2016-09-17 00:06:16 +00:00
|
|
|
} else if (loadCount === 2) {
|
|
|
|
assert(webview.canGoBack())
|
|
|
|
assert(!webview.canGoForward())
|
|
|
|
|
|
|
|
webview.goBack()
|
|
|
|
} else if (loadCount === 3) {
|
|
|
|
webview.goForward()
|
|
|
|
} else if (loadCount === 4) {
|
|
|
|
assert(webview.canGoBack())
|
|
|
|
assert(!webview.canGoForward())
|
|
|
|
|
|
|
|
webview.removeEventListener('did-finish-load', loadListener)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
loadCount += 1
|
2016-09-17 00:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
webview.addEventListener('ipc-message', listener)
|
|
|
|
webview.addEventListener('did-finish-load', loadListener)
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/history-replace.html`
|
|
|
|
})
|
2016-09-17 00:06:16 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('<webview>.clearHistory()', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should clear the navigation history', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/history.html`
|
|
|
|
})
|
|
|
|
const event = await waitForEvent(webview, 'ipc-message')
|
|
|
|
|
|
|
|
assert.equal(event.channel, 'history')
|
|
|
|
assert.equal(event.args[0], 2)
|
|
|
|
assert(webview.canGoBack())
|
|
|
|
|
|
|
|
webview.clearHistory()
|
|
|
|
assert(!webview.canGoBack())
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('basic auth', () => {
|
|
|
|
const auth = require('basic-auth')
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should authenticate with correct credentials', (done) => {
|
|
|
|
const message = 'Authenticated'
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
const credentials = auth(req)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (credentials.name === 'test' && credentials.pass === 'test') {
|
2016-03-25 20:03:49 +00:00
|
|
|
res.end(message)
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-25 20:03:49 +00:00
|
|
|
res.end('failed')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
server.close()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = server.address().port
|
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(e.channel, message)
|
|
|
|
done()
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/basic-auth.html?port=${port}`
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('dom-ready event', () => {
|
|
|
|
it('emits when document is loaded', (done) => {
|
|
|
|
const server = http.createServer(() => {})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = server.address().port
|
|
|
|
webview.addEventListener('dom-ready', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/dom-ready.html?port=${port}`
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('throws a custom error when an API method is called before the event is emitted', () => {
|
2018-05-14 18:17:31 +00:00
|
|
|
const expectedErrorMessage =
|
|
|
|
'Cannot call stop because the webContents is unavailable. ' +
|
|
|
|
'The WebView must be attached to the DOM ' +
|
|
|
|
'and the dom-ready event emitted before this method can be called.'
|
|
|
|
expect(() => { webview.stop() }).to.throw(expectedErrorMessage)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-05 18:27:05 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('executeJavaScript', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should support user gesture', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/fullscreen.html`
|
|
|
|
})
|
|
|
|
|
|
|
|
// Event handler has to be added before js execution.
|
|
|
|
const waitForEnterHtmlFullScreen = waitForEvent(webview, 'enter-html-full-screen')
|
|
|
|
|
|
|
|
const jsScript = "document.querySelector('video').webkitRequestFullscreen()"
|
|
|
|
webview.executeJavaScript(jsScript, true)
|
|
|
|
|
|
|
|
return waitForEnterHtmlFullScreen
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can return the result of the executed script', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
src: 'about:blank'
|
|
|
|
})
|
|
|
|
|
|
|
|
const jsScript = "'4'+2"
|
|
|
|
const expectedResult = '42'
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.executeJavaScript(jsScript, false, (result) => {
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(result, expectedResult)
|
|
|
|
resolve()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('sendInputEvent', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can send keyboard event', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/onkeyup.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'dom-ready')
|
|
|
|
|
|
|
|
const waitForIpcMessage = waitForEvent(webview, 'ipc-message')
|
|
|
|
webview.sendInputEvent({
|
|
|
|
type: 'keyup',
|
|
|
|
keyCode: 'c',
|
|
|
|
modifiers: ['shift']
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const event = await waitForIpcMessage
|
|
|
|
assert.equal(event.channel, 'keyup')
|
|
|
|
assert.deepEqual(event.args, ['C', 'KeyC', 67, true, false])
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can send mouse event', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/onmouseup.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'dom-ready')
|
|
|
|
|
|
|
|
const waitForIpcMessage = waitForEvent(webview, 'ipc-message')
|
|
|
|
webview.sendInputEvent({
|
|
|
|
type: 'mouseup',
|
|
|
|
modifiers: ['ctrl'],
|
|
|
|
x: 10,
|
|
|
|
y: 20
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const event = await waitForIpcMessage
|
|
|
|
assert.equal(event.channel, 'mouseup')
|
|
|
|
assert.deepEqual(event.args, [10, 20, false, true])
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('media-started-playing media-paused events', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when audio starts and stops playing', async () => {
|
|
|
|
loadWebView(webview, {src: `file://${fixtures}/pages/audio.html`})
|
|
|
|
await waitForEvent(webview, 'media-started-playing')
|
|
|
|
await waitForEvent(webview, 'media-paused')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-25 12:21:26 +00:00
|
|
|
describe('found-in-page event', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when a request is made', (done) => {
|
2016-09-08 05:27:10 +00:00
|
|
|
let requestId = null
|
|
|
|
let activeMatchOrdinal = []
|
2017-10-27 20:45:58 +00:00
|
|
|
const listener = (e) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(e.result.requestId, requestId)
|
2016-09-08 05:27:10 +00:00
|
|
|
assert.equal(e.result.matches, 3)
|
|
|
|
activeMatchOrdinal.push(e.result.activeMatchOrdinal)
|
|
|
|
if (e.result.activeMatchOrdinal === e.result.matches) {
|
|
|
|
assert.deepEqual(activeMatchOrdinal, [1, 2, 3])
|
|
|
|
webview.stopFindInPage('clearSelection')
|
|
|
|
done()
|
2016-03-14 01:19:45 +00:00
|
|
|
} else {
|
2016-09-08 05:27:10 +00:00
|
|
|
listener2()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2017-10-27 20:45:58 +00:00
|
|
|
const listener2 = () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
requestId = webview.findInPage('virtual')
|
|
|
|
}
|
|
|
|
webview.addEventListener('found-in-page', listener)
|
|
|
|
webview.addEventListener('did-finish-load', listener2)
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/content.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
document.body.appendChild(webview)
|
2017-11-25 14:48:01 +00:00
|
|
|
// TODO(deepak1556): With https://codereview.chromium.org/2836973002
|
|
|
|
// focus of the webContents is required when triggering the api.
|
|
|
|
// Remove this workaround after determining the cause for
|
|
|
|
// incorrect focus.
|
|
|
|
webview.focus()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('did-change-theme-color event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when theme color changes', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/theme-color.html`
|
|
|
|
})
|
|
|
|
await waitForEvent(webview, 'did-change-theme-color')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('permission-request event', () => {
|
2016-06-29 16:37:10 +00:00
|
|
|
function setUpRequestHandler (webview, requestedPermission, completed) {
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.ok(webview.partition)
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const listener = function (webContents, permission, callback) {
|
2016-03-25 20:03:49 +00:00
|
|
|
if (webContents.getId() === webview.getId()) {
|
2016-12-19 23:11:17 +00:00
|
|
|
// requestMIDIAccess with sysex requests both midi and midiSysex so
|
|
|
|
// grant the first midi one and then reject the midiSysex one
|
2016-12-20 00:27:38 +00:00
|
|
|
if (requestedPermission === 'midiSysex' && permission === 'midi') {
|
2016-12-19 23:11:17 +00:00
|
|
|
return callback(true)
|
|
|
|
}
|
|
|
|
|
2016-06-29 16:37:10 +00:00
|
|
|
assert.equal(permission, requestedPermission)
|
2016-03-25 20:03:49 +00:00
|
|
|
callback(false)
|
2016-06-29 16:37:10 +00:00
|
|
|
if (completed) completed()
|
2016-01-31 21:35:34 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
|
|
|
session.fromPartition(webview.partition).setPermissionRequestHandler(listener)
|
2016-01-31 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when using navigator.getUserMedia api', (done) => {
|
|
|
|
if (isCI) return done()
|
2017-07-11 11:12:57 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2016-10-28 18:14:14 +00:00
|
|
|
assert.equal(e.channel, 'message')
|
|
|
|
assert.deepEqual(e.args, ['PermissionDeniedError'])
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/permissions/media.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
|
|
|
setUpRequestHandler(webview, 'media')
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when using navigator.geolocation api', (done) => {
|
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2016-10-28 18:14:14 +00:00
|
|
|
assert.equal(e.channel, 'message')
|
|
|
|
assert.deepEqual(e.args, ['User denied Geolocation'])
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/permissions/geolocation.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
|
|
|
setUpRequestHandler(webview, 'geolocation')
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when using navigator.requestMIDIAccess without sysex api', (done) => {
|
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2016-10-28 18:14:14 +00:00
|
|
|
assert.equal(e.channel, 'message')
|
|
|
|
assert.deepEqual(e.args, ['SecurityError'])
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/permissions/midi.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
2016-12-19 23:11:17 +00:00
|
|
|
setUpRequestHandler(webview, 'midi')
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when using navigator.requestMIDIAccess with sysex api', (done) => {
|
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2016-12-19 23:11:17 +00:00
|
|
|
assert.equal(e.channel, 'message')
|
|
|
|
assert.deepEqual(e.args, ['SecurityError'])
|
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/permissions/midi-sysex.html`
|
2016-12-19 23:11:17 +00:00
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
2016-03-25 20:03:49 +00:00
|
|
|
setUpRequestHandler(webview, 'midiSysex')
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
2016-04-20 16:55:15 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when accessing external protocol', (done) => {
|
2016-04-20 16:55:15 +00:00
|
|
|
webview.src = 'magnet:test'
|
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
setUpRequestHandler(webview, 'openExternal', done)
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
2016-10-28 17:34:11 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits when using Notification.requestPermission', (done) => {
|
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2016-10-28 17:34:11 +00:00
|
|
|
assert.equal(e.channel, 'message')
|
|
|
|
assert.deepEqual(e.args, ['granted'])
|
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/permissions/notification.html`
|
2016-10-28 17:34:11 +00:00
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
2017-10-27 20:45:58 +00:00
|
|
|
session.fromPartition(webview.partition).setPermissionRequestHandler((webContents, permission, callback) => {
|
2016-10-28 17:34:11 +00:00
|
|
|
if (webContents.getId() === webview.getId()) {
|
|
|
|
assert.equal(permission, 'notifications')
|
2017-10-27 20:45:58 +00:00
|
|
|
setTimeout(() => { callback(true) }, 10)
|
2016-10-28 17:34:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('<webview>.getWebContents', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('can return the webcontents associated', async () => {
|
|
|
|
const src = 'about:blank'
|
|
|
|
await loadWebView(webview, {src})
|
|
|
|
|
|
|
|
const webviewContents = webview.getWebContents()
|
|
|
|
assert(webviewContents)
|
|
|
|
assert.equal(webviewContents.getURL(), src)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-04-08 19:55:20 +00:00
|
|
|
|
2018-04-23 19:46:12 +00:00
|
|
|
describe('did-get-response-details event (deprecated)', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits for the page and its resources', (done) => {
|
2016-04-08 19:55:20 +00:00
|
|
|
// expected {fileName: resourceType} pairs
|
2017-10-27 20:45:58 +00:00
|
|
|
const expectedResources = {
|
2016-04-08 19:55:20 +00:00
|
|
|
'did-get-response-details.html': 'mainFrame',
|
|
|
|
'logo.png': 'image'
|
|
|
|
}
|
2017-10-27 20:45:58 +00:00
|
|
|
let responses = 0
|
2018-04-23 19:46:12 +00:00
|
|
|
webview.addEventListener('-did-get-response-details', (event) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
responses += 1
|
|
|
|
const fileName = event.newURL.slice(event.newURL.lastIndexOf('/') + 1)
|
|
|
|
const expectedType = expectedResources[fileName]
|
2016-04-08 19:55:20 +00:00
|
|
|
assert(!!expectedType, `Unexpected response details for ${event.newURL}`)
|
|
|
|
assert(typeof event.status === 'boolean', 'status should be boolean')
|
|
|
|
assert.equal(event.httpResponseCode, 200)
|
|
|
|
assert.equal(event.requestMethod, 'GET')
|
|
|
|
assert(typeof event.referrer === 'string', 'referrer should be string')
|
|
|
|
assert(!!event.headers, 'headers should be present')
|
|
|
|
assert(typeof event.headers === 'object', 'headers should be object')
|
|
|
|
assert.equal(event.resourceType, expectedType, 'Incorrect resourceType')
|
2017-10-27 20:45:58 +00:00
|
|
|
if (responses === Object.keys(expectedResources).length) done()
|
2016-04-08 19:55:20 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${path.join(fixtures, 'pages', 'did-get-response-details.html')}`
|
2016-04-08 19:55:20 +00:00
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
})
|
2016-05-19 21:46:11 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('document.visibilityState/hidden', () => {
|
|
|
|
afterEach(() => {
|
2017-06-12 19:51:27 +00:00
|
|
|
ipcMain.removeAllListeners('pong')
|
2016-05-24 19:37:26 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('updates when the window is shown after the ready-to-show event', (done) => {
|
|
|
|
w = new BrowserWindow({ show: false })
|
|
|
|
w.once('ready-to-show', () => {
|
2017-06-12 19:51:27 +00:00
|
|
|
w.show()
|
2016-05-24 19:37:26 +00:00
|
|
|
})
|
2017-02-24 19:56:27 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.on('pong', (event, visibilityState, hidden) => {
|
2017-06-12 19:51:27 +00:00
|
|
|
if (!hidden) {
|
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-visibilitychange.html`)
|
2016-05-24 19:37:26 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('inherits the parent window visibility state and receives visibilitychange events', (done) => {
|
|
|
|
w = new BrowserWindow({ show: false })
|
2017-06-12 19:51:27 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.once('pong', (event, visibilityState, hidden) => {
|
2017-06-12 19:51:27 +00:00
|
|
|
assert.equal(visibilityState, 'hidden')
|
|
|
|
assert.equal(hidden, true)
|
|
|
|
|
|
|
|
ipcMain.once('pong', function (event, visibilityState, hidden) {
|
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
assert.equal(hidden, false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.emit('-window-visibility-change', 'visible')
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-visibilitychange.html`)
|
2017-06-12 19:51:27 +00:00
|
|
|
})
|
2016-05-24 19:37:26 +00:00
|
|
|
})
|
2016-06-07 16:50:06 +00:00
|
|
|
|
2017-02-09 19:49:14 +00:00
|
|
|
describe('will-attach-webview event', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('supports changing the web preferences', async () => {
|
2017-02-03 20:55:37 +00:00
|
|
|
ipcRenderer.send('disable-node-on-next-will-attach-webview')
|
2018-05-14 22:00:49 +00:00
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
nodeintegration: 'yes',
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2017-02-03 20:21:46 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
assert.equal(message,
|
|
|
|
'{"require":"undefined","module":"undefined","process":"undefined","global":"undefined"}')
|
2017-02-03 20:21:46 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('supports preventing a webview from being created', async () => {
|
2017-02-03 20:55:37 +00:00
|
|
|
ipcRenderer.send('prevent-next-will-attach-webview')
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/c.html`
|
2017-02-03 20:21:46 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'destroyed')
|
2017-02-03 20:21:46 +00:00
|
|
|
})
|
2017-05-17 20:47:00 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('supports removing the preload script', async () => {
|
2017-05-17 20:47:00 +00:00
|
|
|
ipcRenderer.send('disable-preload-on-next-will-attach-webview')
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
const {message} = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
nodeintegration: 'yes',
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-set-global.js'),
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2017-05-17 20:47:00 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
assert.equal(message, 'undefined')
|
2017-05-17 20:47:00 +00:00
|
|
|
})
|
2017-02-03 20:21:46 +00:00
|
|
|
})
|
|
|
|
|
2017-10-06 16:31:41 +00:00
|
|
|
describe('did-attach-webview event', () => {
|
|
|
|
it('is emitted when a webview has been attached', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
w = new BrowserWindow({ show: false })
|
2017-10-06 16:31:41 +00:00
|
|
|
w.webContents.on('did-attach-webview', (event, webContents) => {
|
|
|
|
ipcMain.once('webview-dom-ready', (event, id) => {
|
|
|
|
assert.equal(webContents.id, id)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-did-attach-event.html`)
|
2017-10-06 16:31:41 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('loads devtools extensions registered on the parent window', (done) => {
|
|
|
|
w = new BrowserWindow({ show: false })
|
2016-06-07 16:50:06 +00:00
|
|
|
BrowserWindow.removeDevToolsExtension('foo')
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
2016-06-07 16:50:06 +00:00
|
|
|
BrowserWindow.addDevToolsExtension(extensionPath)
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-devtools.html`)
|
2016-06-07 16:50:06 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.once('answer', (event, message) => {
|
2016-06-07 16:50:06 +00:00
|
|
|
assert.equal(message.runtimeId, 'foo')
|
2016-06-07 17:38:01 +00:00
|
|
|
assert.notEqual(message.tabId, w.webContents.id)
|
2016-06-07 16:50:06 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('guestinstance attribute', () => {
|
|
|
|
it('before loading there is no attribute', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview) // Don't wait for loading to finish.
|
2016-09-08 17:01:01 +00:00
|
|
|
assert(!webview.hasAttribute('guestinstance'))
|
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('loading a page sets the guest view', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const instance = webview.getAttribute('guestinstance')
|
|
|
|
assert.equal(instance, parseInt(instance))
|
|
|
|
|
|
|
|
const guest = getGuestWebContents(parseInt(instance))
|
|
|
|
assert.equal(guest, webview.getWebContents())
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('deleting the attribute destroys the webview', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const instance = parseInt(webview.getAttribute('guestinstance'))
|
|
|
|
const waitForDestroy = waitForEvent(webview, 'destroyed')
|
|
|
|
webview.removeAttribute('guestinstance')
|
|
|
|
|
|
|
|
await waitForDestroy
|
|
|
|
assert.equal(getGuestWebContents(instance), null)
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('setting the attribute on a new webview moves the contents', (done) => {
|
|
|
|
const loadListener = () => {
|
|
|
|
const webContents = webview.getWebContents()
|
|
|
|
const instance = webview.getAttribute('guestinstance')
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const destroyListener = () => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert.equal(webContents, webview2.getWebContents())
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2016-09-08 17:01:01 +00:00
|
|
|
// Make sure that events are hooked up to the right webview now
|
2017-10-27 20:45:58 +00:00
|
|
|
webview2.addEventListener('console-message', (e) => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert.equal(e.message, 'a')
|
|
|
|
document.body.removeChild(webview2)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
webview2.src = `file://${fixtures}/pages/a.html`
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview.addEventListener('destroyed', destroyListener, {once: true})
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const webview2 = new WebView()
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview2, {
|
|
|
|
guestinstance: instance
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview.addEventListener('did-finish-load', loadListener, {once: true})
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('setting the attribute to an invalid guestinstance does nothing', async () => {
|
|
|
|
await loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
|
|
|
webview.setAttribute('guestinstance', 55)
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
// Make sure that events are still hooked up to the webview
|
|
|
|
const waitForMessage = waitForEvent(webview, 'console-message')
|
|
|
|
webview.src = `file://${fixtures}/pages/a.html`
|
|
|
|
const {message} = await waitForMessage
|
|
|
|
assert.equal(message, 'a')
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('setting the attribute on an existing webview moves the contents', (done) => {
|
|
|
|
const load1Listener = () => {
|
|
|
|
const webContents = webview.getWebContents()
|
|
|
|
const instance = webview.getAttribute('guestinstance')
|
|
|
|
let destroyedInstance
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const destroyListener = () => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert.equal(webContents, webview2.getWebContents())
|
|
|
|
assert.equal(null, getGuestWebContents(parseInt(destroyedInstance)))
|
|
|
|
|
|
|
|
// Make sure that events are hooked up to the right webview now
|
2017-10-27 20:45:58 +00:00
|
|
|
webview2.addEventListener('console-message', (e) => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert.equal(e.message, 'a')
|
|
|
|
document.body.removeChild(webview2)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
webview2.src = 'file://' + fixtures + '/pages/a.html'
|
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview.addEventListener('destroyed', destroyListener, {once: true})
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const webview2 = new WebView()
|
|
|
|
const load2Listener = () => {
|
2016-09-08 17:01:01 +00:00
|
|
|
destroyedInstance = webview2.getAttribute('guestinstance')
|
|
|
|
assert.notEqual(instance, destroyedInstance)
|
|
|
|
|
|
|
|
webview2.setAttribute('guestinstance', instance)
|
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview2.addEventListener('did-finish-load', load2Listener, {once: true})
|
|
|
|
loadWebView(webview2, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
webview.addEventListener('did-finish-load', load1Listener, {once: true})
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('moving a guest back to its original webview should work', (done) => {
|
|
|
|
const loadListener = () => {
|
|
|
|
const webContents = webview.getWebContents()
|
|
|
|
const instance = webview.getAttribute('guestinstance')
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const destroy1Listener = () => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert.equal(webContents, webview2.getWebContents())
|
|
|
|
assert.equal(null, webview.getWebContents())
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const destroy2Listener = () => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert.equal(webContents, webview.getWebContents())
|
|
|
|
assert.equal(null, webview2.getWebContents())
|
|
|
|
|
|
|
|
// Make sure that events are hooked up to the right webview now
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('console-message', (e) => {
|
2016-09-08 17:01:01 +00:00
|
|
|
document.body.removeChild(webview2)
|
2018-05-14 22:00:49 +00:00
|
|
|
assert.equal(e.message, 'a')
|
2016-09-08 17:01:01 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/a.html`
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview2.addEventListener('destroyed', destroy2Listener, {once: true})
|
2016-09-08 17:01:01 +00:00
|
|
|
webview.setAttribute('guestinstance', instance)
|
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview.addEventListener('destroyed', destroy1Listener, {once: true})
|
2016-09-08 17:01:01 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const webview2 = new WebView()
|
2016-09-08 17:01:01 +00:00
|
|
|
webview2.setAttribute('guestinstance', instance)
|
|
|
|
document.body.appendChild(webview2)
|
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
webview.addEventListener('did-finish-load', loadListener, {once: true})
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/api/blank.html`
|
|
|
|
})
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('setting the attribute on a webview in a different window moves the contents', (done) => {
|
|
|
|
const loadListener = () => {
|
|
|
|
const instance = webview.getAttribute('guestinstance')
|
2016-09-08 17:01:01 +00:00
|
|
|
|
|
|
|
w = new BrowserWindow({ show: false })
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
ipcMain.once('pong', () => {
|
2016-09-08 17:01:01 +00:00
|
|
|
assert(!webview.hasAttribute('guestinstance'))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.webContents.send('guestinstance', instance)
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-move-to-window.html`)
|
2016-09-08 17:01:01 +00:00
|
|
|
}
|
2018-05-14 22:00:49 +00:00
|
|
|
webview.addEventListener('did-finish-load', loadListener, {once: true})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/api/blank.html`
|
2016-09-08 17:01:01 +00:00
|
|
|
document.body.appendChild(webview)
|
2016-11-02 19:00:28 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not delete the guestinstance attribute when moving the webview to another parent node', (done) => {
|
2016-11-03 00:25:52 +00:00
|
|
|
webview.addEventListener('dom-ready', function domReadyListener () {
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('did-attach', () => {
|
2016-11-03 00:25:52 +00:00
|
|
|
assert(webview.guestinstance != null)
|
|
|
|
assert(webview.getWebContents() != null)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
document.body.replaceChild(webview, div)
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/a.html`
|
2016-11-03 00:25:52 +00:00
|
|
|
|
|
|
|
const div = document.createElement('div')
|
|
|
|
div.appendChild(webview)
|
|
|
|
document.body.appendChild(div)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not destroy the webContents when hiding/showing the webview (regression)', (done) => {
|
2016-11-02 19:00:28 +00:00
|
|
|
webview.addEventListener('dom-ready', function domReadyListener () {
|
|
|
|
const instance = webview.getAttribute('guestinstance')
|
2016-11-03 00:25:52 +00:00
|
|
|
assert(instance != null)
|
2016-11-02 19:00:28 +00:00
|
|
|
|
|
|
|
// Wait for event directly since attach happens asynchronously over IPC
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.once('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', () => {
|
2016-11-02 19:00:28 +00:00
|
|
|
assert(webview.getWebContents() != null)
|
|
|
|
assert.equal(instance, webview.getAttribute('guestinstance'))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
webview.style.display = 'none'
|
2017-11-23 22:22:43 +00:00
|
|
|
webview.offsetHeight // eslint-disable-line
|
2016-11-02 19:00:28 +00:00
|
|
|
webview.style.display = 'block'
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/a.html`
|
2016-11-02 19:00:28 +00:00
|
|
|
document.body.appendChild(webview)
|
2016-11-02 22:35:00 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not reload the webContents when hiding/showing the webview (regression)', (done) => {
|
2016-11-02 22:35:00 +00:00
|
|
|
webview.addEventListener('dom-ready', function domReadyListener () {
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('did-start-loading', () => {
|
2016-11-02 22:35:00 +00:00
|
|
|
done(new Error('webview started loading unexpectedly'))
|
|
|
|
})
|
|
|
|
|
|
|
|
// Wait for event directly since attach happens asynchronously over IPC
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('did-attach', () => {
|
2016-11-02 22:35:00 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
webview.style.display = 'none'
|
2017-11-23 22:22:43 +00:00
|
|
|
webview.offsetHeight // eslint-disable-line
|
2016-11-02 22:35:00 +00:00
|
|
|
webview.style.display = 'block'
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/a.html`
|
2016-11-02 22:35:00 +00:00
|
|
|
document.body.appendChild(webview)
|
2016-09-08 17:01:01 +00:00
|
|
|
})
|
|
|
|
})
|
2016-11-03 22:12:54 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('DOM events', () => {
|
2016-11-03 22:12:54 +00:00
|
|
|
let div
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach(() => {
|
2016-11-03 22:12:54 +00:00
|
|
|
div = document.createElement('div')
|
|
|
|
div.style.width = '100px'
|
|
|
|
div.style.height = '10px'
|
|
|
|
div.style.overflow = 'hidden'
|
|
|
|
webview.style.height = '100%'
|
|
|
|
webview.style.width = '100%'
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => {
|
2016-11-03 22:12:54 +00:00
|
|
|
if (div != null) div.remove()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits resize events', (done) => {
|
|
|
|
webview.addEventListener('dom-ready', () => {
|
2016-11-03 22:12:54 +00:00
|
|
|
div.style.width = '1234px'
|
|
|
|
div.style.height = '789px'
|
|
|
|
})
|
|
|
|
|
|
|
|
webview.addEventListener('resize', function onResize (event) {
|
|
|
|
webview.removeEventListener('resize', onResize)
|
2018-03-13 03:15:40 +00:00
|
|
|
assert.equal(event.newWidth, 100)
|
|
|
|
assert.equal(event.newHeight, 10)
|
2016-11-03 22:12:54 +00:00
|
|
|
assert.equal(event.target, webview)
|
2018-03-13 03:15:40 +00:00
|
|
|
webview.addEventListener('resize', function onResizeAgain (event) {
|
|
|
|
// This will be triggered after setting the new div width and height.
|
|
|
|
webview.removeEventListener('resize', onResizeAgain)
|
|
|
|
assert.equal(event.newWidth, 1234)
|
|
|
|
assert.equal(event.newHeight, 789)
|
|
|
|
assert.equal(event.target, webview)
|
|
|
|
done()
|
|
|
|
})
|
2016-11-03 22:12:54 +00:00
|
|
|
})
|
|
|
|
|
2016-11-07 16:28:02 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/a.html`
|
2016-11-03 22:12:54 +00:00
|
|
|
div.appendChild(webview)
|
|
|
|
document.body.appendChild(div)
|
|
|
|
})
|
|
|
|
})
|
2016-10-25 05:17:38 +00:00
|
|
|
|
|
|
|
describe('disableguestresize attribute', () => {
|
|
|
|
it('does not have attribute by default', () => {
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
assert(!webview.hasAttribute('disableguestresize'))
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('resizes guest when attribute is not present', (done) => {
|
2016-11-15 21:47:14 +00:00
|
|
|
w = new BrowserWindow({show: false, width: 200, height: 200})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-guest-resize.html`)
|
2016-10-25 05:17:38 +00:00
|
|
|
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
const CONTENT_SIZE = 300
|
|
|
|
|
|
|
|
const elementResizePromise = new Promise(resolve => {
|
|
|
|
ipcMain.once('webview-element-resize', (event, width, height) => {
|
|
|
|
assert.equal(width, CONTENT_SIZE)
|
2016-11-15 21:47:14 +00:00
|
|
|
assert.equal(height, CONTENT_SIZE)
|
2016-10-25 05:17:38 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const guestResizePromise = new Promise(resolve => {
|
|
|
|
ipcMain.once('webview-guest-resize', (event, width, height) => {
|
|
|
|
assert.equal(width, CONTENT_SIZE)
|
2016-11-15 21:47:14 +00:00
|
|
|
assert.equal(height, CONTENT_SIZE)
|
2016-10-25 05:17:38 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Promise.all([elementResizePromise, guestResizePromise]).then(() => done())
|
|
|
|
|
|
|
|
w.setContentSize(CONTENT_SIZE, CONTENT_SIZE)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not resize guest when attribute is present', done => {
|
2016-11-15 21:47:14 +00:00
|
|
|
w = new BrowserWindow({show: false, width: 200, height: 200})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`)
|
2016-10-25 05:17:38 +00:00
|
|
|
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
const CONTENT_SIZE = 300
|
|
|
|
|
|
|
|
const elementResizePromise = new Promise(resolve => {
|
|
|
|
ipcMain.once('webview-element-resize', (event, width, height) => {
|
|
|
|
assert.equal(width, CONTENT_SIZE)
|
2016-11-15 21:47:14 +00:00
|
|
|
assert.equal(height, CONTENT_SIZE)
|
2016-10-25 05:17:38 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const noGuestResizePromise = new Promise(resolve => {
|
|
|
|
const onGuestResize = (event, width, height) => {
|
2016-11-15 21:47:14 +00:00
|
|
|
done(new Error('Unexpected guest resize message'))
|
2016-10-25 05:17:38 +00:00
|
|
|
}
|
|
|
|
ipcMain.once('webview-guest-resize', onGuestResize)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
ipcMain.removeListener('webview-guest-resize', onGuestResize)
|
|
|
|
resolve()
|
|
|
|
}, 500)
|
|
|
|
})
|
|
|
|
|
|
|
|
Promise.all([elementResizePromise, noGuestResizePromise]).then(() => done())
|
|
|
|
|
|
|
|
w.setContentSize(CONTENT_SIZE, CONTENT_SIZE)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('dispatches element resize event even when attribute is present', done => {
|
2016-11-15 21:47:14 +00:00
|
|
|
w = new BrowserWindow({show: false, width: 200, height: 200})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`)
|
2016-10-25 05:17:38 +00:00
|
|
|
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
const CONTENT_SIZE = 300
|
|
|
|
|
|
|
|
ipcMain.once('webview-element-resize', (event, width, height) => {
|
|
|
|
assert.equal(width, CONTENT_SIZE)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.setContentSize(CONTENT_SIZE, CONTENT_SIZE)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('can be manually resized with setSize even when attribute is present', function (done) {
|
2016-11-15 21:47:14 +00:00
|
|
|
w = new BrowserWindow({show: false, width: 200, height: 200})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-no-guest-resize.html`)
|
2016-10-25 05:17:38 +00:00
|
|
|
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
const GUEST_WIDTH = 10
|
|
|
|
const GUEST_HEIGHT = 20
|
|
|
|
|
|
|
|
ipcMain.once('webview-guest-resize', (event, width, height) => {
|
|
|
|
assert.equal(width, GUEST_WIDTH)
|
|
|
|
assert.equal(height, GUEST_HEIGHT)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2016-11-15 21:47:14 +00:00
|
|
|
for (const wc of webContents.getAllWebContents()) {
|
2016-10-25 05:17:38 +00:00
|
|
|
if (wc.hostWebContents &&
|
|
|
|
wc.hostWebContents.id === w.webContents.id) {
|
|
|
|
wc.setSize({
|
|
|
|
normal: {
|
|
|
|
width: GUEST_WIDTH,
|
|
|
|
height: GUEST_HEIGHT
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-02-17 15:48:30 +00:00
|
|
|
|
|
|
|
describe('zoom behavior', () => {
|
2017-02-17 17:58:29 +00:00
|
|
|
const zoomScheme = remote.getGlobal('zoomScheme')
|
|
|
|
const webviewSession = session.fromPartition('webview-temp')
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
const protocol = webviewSession.protocol
|
|
|
|
protocol.registerStringProtocol(zoomScheme, (request, callback) => {
|
|
|
|
callback('hello')
|
|
|
|
}, (error) => done(error))
|
|
|
|
})
|
|
|
|
|
|
|
|
after((done) => {
|
|
|
|
const protocol = webviewSession.protocol
|
|
|
|
protocol.unregisterProtocol(zoomScheme, (error) => done(error))
|
|
|
|
})
|
|
|
|
|
2017-02-17 15:48:30 +00:00
|
|
|
it('inherits the zoomFactor of the parent window', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
|
|
|
})
|
2017-02-17 17:58:29 +00:00
|
|
|
ipcMain.once('webview-parent-zoom-level', (event, zoomFactor, zoomLevel) => {
|
2017-02-17 15:48:30 +00:00
|
|
|
assert.equal(zoomFactor, 1.2)
|
|
|
|
assert.equal(zoomLevel, 1)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-zoom-factor.html`)
|
|
|
|
})
|
|
|
|
|
2017-02-17 17:58:29 +00:00
|
|
|
it('maintains zoom level on navigation', (done) => {
|
2017-02-17 15:48:30 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
|
|
|
})
|
2017-02-17 17:58:29 +00:00
|
|
|
ipcMain.on('webview-zoom-level', (event, zoomLevel, zoomFactor, newHost, final) => {
|
|
|
|
if (!newHost) {
|
2017-02-17 15:48:30 +00:00
|
|
|
assert.equal(zoomFactor, 1.44)
|
|
|
|
assert.equal(zoomLevel, 2.0)
|
|
|
|
} else {
|
|
|
|
assert.equal(zoomFactor, 1.2)
|
|
|
|
assert.equal(zoomLevel, 1)
|
|
|
|
}
|
2017-02-17 17:58:29 +00:00
|
|
|
if (final) done()
|
2017-02-17 15:48:30 +00:00
|
|
|
})
|
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-custom-zoom-level.html`)
|
|
|
|
})
|
2017-02-17 17:58:29 +00:00
|
|
|
|
|
|
|
it('maintains zoom level when navigating within same page', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ipcMain.on('webview-zoom-in-page', (event, zoomLevel, zoomFactor, final) => {
|
|
|
|
assert.equal(zoomFactor, 1.44)
|
|
|
|
assert.equal(zoomLevel, 2.0)
|
|
|
|
if (final) done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-in-page-navigate.html`)
|
|
|
|
})
|
2017-03-08 13:35:24 +00:00
|
|
|
|
|
|
|
it('inherits zoom level for the origin when available', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ipcMain.once('webview-origin-zoom-level', (event, zoomLevel) => {
|
|
|
|
assert.equal(zoomLevel, 2.0)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${fixtures}/pages/webview-origin-zoom-level.html`)
|
|
|
|
})
|
2017-02-17 15:48:30 +00:00
|
|
|
})
|
2017-05-23 20:24:51 +00:00
|
|
|
|
|
|
|
describe('nativeWindowOpen option', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach(() => {
|
2017-05-23 21:22:09 +00:00
|
|
|
webview.setAttribute('allowpopups', 'on')
|
2017-05-23 20:59:13 +00:00
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
|
|
|
webview.setAttribute('webpreferences', 'nativeWindowOpen=1')
|
|
|
|
})
|
|
|
|
|
2017-05-23 20:28:24 +00:00
|
|
|
it('opens window of about:blank with cross-scripting enabled', (done) => {
|
2017-05-23 20:24:51 +00:00
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'Hello')
|
|
|
|
done()
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}`
|
|
|
|
})
|
2017-05-23 20:24:51 +00:00
|
|
|
})
|
2017-05-23 20:28:24 +00:00
|
|
|
|
|
|
|
it('opens window of same domain with cross-scripting enabled', (done) => {
|
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'Hello')
|
|
|
|
done()
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-file.html')}`
|
|
|
|
})
|
2017-05-23 22:56:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('returns null from window.open when allowpopups is not set', (done) => {
|
|
|
|
webview.removeAttribute('allowpopups')
|
|
|
|
ipcMain.once('answer', (event, {windowOpenReturnedNull}) => {
|
|
|
|
assert.equal(windowOpenReturnedNull, true)
|
|
|
|
done()
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-no-allowpopups.html')}`
|
|
|
|
})
|
2017-05-25 16:39:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('blocks accessing cross-origin frames', (done) => {
|
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'Blocked a frame with origin "file://" from accessing a cross-origin frame.')
|
|
|
|
done()
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}`
|
|
|
|
})
|
2017-05-23 20:28:24 +00:00
|
|
|
})
|
2017-05-23 20:59:13 +00:00
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits a new-window event', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
2017-05-23 20:59:13 +00:00
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'new-window')
|
|
|
|
|
|
|
|
assert.equal(event.url, 'http://host/')
|
|
|
|
assert.equal(event.frameName, 'host')
|
2017-05-23 20:59:13 +00:00
|
|
|
})
|
2017-05-23 21:31:20 +00:00
|
|
|
|
|
|
|
it('emits a browser-window-created event', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
app.once('browser-window-created', () => done())
|
|
|
|
webview.src = `file://${fixtures}/pages/window-open.html`
|
2017-05-23 21:31:20 +00:00
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits a web-contents-created event', (done) => {
|
|
|
|
app.on('web-contents-created', function listener (event, contents) {
|
|
|
|
if (contents.getType() === 'window') {
|
|
|
|
app.removeListener('web-contents-created', listener)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
|
|
|
})
|
2017-05-23 21:31:20 +00:00
|
|
|
})
|
2017-05-23 20:24:51 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|