2019-11-07 00:15:55 +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');
|
2019-09-03 17:10:58 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2018-09-13 16:10:51 +00:00
|
|
|
const { emittedOnce, waitForEvent } = require('./events-helpers');
|
2020-06-30 22:10:36 +00:00
|
|
|
const { ifdescribe, ifit, delay } = require('./spec-helpers');
|
2018-06-19 11:25:26 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const features = process._linkedBinding('electron_common_features');
|
2019-09-03 17:10:58 +00:00
|
|
|
const nativeModulesEnabled = process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS;
|
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;
|
2018-06-01 08:56:48 +00:00
|
|
|
|
2018-06-19 11:25:26 +00:00
|
|
|
const loadWebView = async (webview, attributes = {}) => {
|
2018-05-14 22:00:49 +00:00
|
|
|
for (const [name, value] of Object.entries(attributes)) {
|
|
|
|
webview.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
document.body.appendChild(webview);
|
2018-06-19 11:25:26 +00:00
|
|
|
await waitForEvent(webview, 'did-finish-load');
|
|
|
|
return webview;
|
2018-05-14 22:00:49 +00:00
|
|
|
};
|
|
|
|
|
2018-06-19 11:25:26 +00:00
|
|
|
const startLoadingWebViewAndWaitForMessage = async (webview, attributes = {}) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, attributes); // Don't wait for load to be finished.
|
2018-06-19 11:25:26 +00:00
|
|
|
const event = await waitForEvent(webview, 'console-message');
|
|
|
|
return event.message;
|
2018-05-14 22:00:49 +00:00
|
|
|
};
|
|
|
|
|
2021-04-07 01:46:23 +00:00
|
|
|
async function loadFileInWebView (webview, attributes = {}) {
|
|
|
|
const thisFile = url.format({
|
|
|
|
pathname: __filename.replace(/\\/g, '/'),
|
|
|
|
protocol: 'file',
|
|
|
|
slashes: true
|
|
|
|
});
|
|
|
|
const src = `<script>
|
|
|
|
function loadFile() {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
fetch('${thisFile}').then(
|
|
|
|
() => resolve('loaded'),
|
|
|
|
() => resolve('failed')
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.log('ok');
|
|
|
|
</script>`;
|
|
|
|
attributes.src = `data:text/html;base64,${btoa(unescape(encodeURIComponent(src)))}`;
|
|
|
|
await startLoadingWebViewAndWaitForMessage(webview, attributes);
|
|
|
|
return await webview.executeJavaScript('loadFile()');
|
|
|
|
}
|
|
|
|
|
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();
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
expect(message).to.equal('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`;
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { message } = await waitForEvent(webview, 'console-message');
|
2018-06-01 08:56:48 +00:00
|
|
|
expect(message).to.equal('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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
src: '../fixtures/pages/e.html'
|
|
|
|
});
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.equal('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', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(webview.src).to.equal('');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
for (const emptyValue of ['', null, undefined]) {
|
|
|
|
webview.src = emptyValue;
|
2018-06-01 08:56:48 +00:00
|
|
|
expect(webview.src).to.equal('');
|
2018-05-14 22:00:49 +00:00
|
|
|
}
|
2016-09-08 23:40:31 +00:00
|
|
|
});
|
2019-07-10 13:20:28 +00:00
|
|
|
|
|
|
|
it('does not wait until loadURL is resolved', async () => {
|
|
|
|
await loadWebView(webview, { src: 'about:blank' });
|
|
|
|
|
|
|
|
const before = Date.now();
|
|
|
|
webview.src = 'https://github.com';
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
// Setting src is essentially sending a sync IPC message, which should
|
|
|
|
// not exceed more than a few ms.
|
|
|
|
//
|
|
|
|
// This is for testing #18638.
|
|
|
|
expect(now - before).to.be.below(100);
|
|
|
|
});
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/c.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2018-06-01 08:56:48 +00:00
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
nodeintegration: 'on',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/d.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2018-06-01 08:56:48 +00:00
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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') {
|
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-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
nodeintegration: 'on',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/post.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2018-06-01 08:56:48 +00:00
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function',
|
|
|
|
module: 'object',
|
|
|
|
process: 'object'
|
|
|
|
});
|
2016-03-30 17:33:15 +00:00
|
|
|
});
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('disables node integration on child windows when it is disabled on the webview', async () => {
|
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',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
const { message } = await waitForEvent(webview, 'console-message');
|
|
|
|
expect(JSON.parse(message).isProcessGlobalUndefined).to.be.true();
|
2018-08-24 21:24:24 +00:00
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2018-08-24 21:24:24 +00:00
|
|
|
(nativeModulesEnabled ? it : it.skip)('loads native modules when navigation happens', async function () {
|
2018-05-14 22:00:49 +00:00
|
|
|
await loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/native-module.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
webview.reload();
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { message } = await waitForEvent(webview, 'console-message');
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.equal('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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
preload: `${fixtures}/module/preload.js`,
|
2021-03-01 21:52:29 +00:00
|
|
|
src: `file://${fixtures}/pages/e.html`,
|
|
|
|
contextIsolation: false
|
2018-05-14 22:00:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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-07-13 02:17:11 +00:00
|
|
|
it('runs in the correct scope when sandboxed', async () => {
|
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
preload: `${fixtures}/module/preload-context.js`,
|
|
|
|
src: `file://${fixtures}/api/blank.html`,
|
|
|
|
webpreferences: 'sandbox=yes'
|
|
|
|
});
|
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'function', // arguments passed to it should be availale
|
|
|
|
electron: 'undefined', // objects from the scope it is called from should not be available
|
|
|
|
window: 'object', // the window object should be available
|
|
|
|
localVar: 'undefined' // but local variables should not be exposed to the window
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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 () => {
|
2018-06-01 08:56:48 +00:00
|
|
|
await loadWebView(webview, {
|
|
|
|
preload: `${fixtures}/module/preload-ipc.js`,
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2018-06-01 08:56:48 +00:00
|
|
|
const message = 'boom!';
|
2018-05-14 22:00:49 +00:00
|
|
|
webview.send('ping', message);
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { channel, args } = await waitForEvent(webview, 'ipc-message');
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(channel).to.equal('pong');
|
|
|
|
expect(args).to.deep.equal([message]);
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2016-04-12 05:57:40 +00:00
|
|
|
|
2021-08-25 07:46:46 +00:00
|
|
|
it('<webview>.sendToFrame()', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'contextIsolation=no',
|
|
|
|
preload: `${fixtures}/module/preload-ipc.js`,
|
|
|
|
src: `file://${fixtures}/pages/ipc-message.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
const { frameId } = await waitForEvent(webview, 'ipc-message');
|
|
|
|
|
|
|
|
const message = 'boom!';
|
|
|
|
webview.sendToFrame(frameId, 'ping', message);
|
|
|
|
|
|
|
|
const { channel, args } = await waitForEvent(webview, 'ipc-message');
|
|
|
|
expect(channel).to.equal('pong');
|
|
|
|
expect(args).to.deep.equal([message]);
|
|
|
|
});
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('works without script tag in page', async () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(webview.preload).to.equal('');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
for (const emptyValue of ['', null, undefined]) {
|
|
|
|
webview.preload = emptyValue;
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(webview.preload).to.equal('');
|
2018-05-14 22:00:49 +00:00
|
|
|
}
|
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) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
expect(req.headers.referer).to.equal(referrer);
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
} finally {
|
|
|
|
res.end();
|
|
|
|
server.close();
|
|
|
|
}
|
2018-04-06 07:22:52 +00:00
|
|
|
}).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-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/useragent.html`,
|
|
|
|
useragent: referrer
|
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
expect(message).to.equal(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 () => {
|
2021-04-07 01:46:23 +00:00
|
|
|
const result = await loadFileInWebView(webview);
|
|
|
|
expect(result).to.equal('failed');
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('disables web security when set', async () => {
|
2021-04-07 01:46:23 +00:00
|
|
|
const result = await loadFileInWebView(webview, { disablewebsecurity: '' });
|
|
|
|
expect(result).to.equal('loaded');
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
disablewebsecurity: '',
|
|
|
|
nodeintegration: 'on',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/d.html`
|
2016-05-30 06:20:53 +00:00
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
disablewebsecurity: '',
|
|
|
|
preload: `${fixtures}/module/preload.js`,
|
|
|
|
src: `file://${fixtures}/pages/e.html`
|
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
partition: 'test1',
|
|
|
|
src: `file://${fixtures}/pages/c.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
nodeintegration: 'on',
|
|
|
|
partition: 'test2',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/d.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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
|
|
|
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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);
|
|
|
|
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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', () => {
|
2019-05-27 00:44:54 +00:00
|
|
|
const generateSpecs = (description, webpreferences = '') => {
|
|
|
|
describe(description, () => {
|
|
|
|
it('can not open new window when not set', async () => {
|
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
webpreferences,
|
|
|
|
src: `file://${fixtures}/pages/window-open-hide.html`
|
|
|
|
});
|
|
|
|
expect(message).to.equal('null');
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-27 00:44:54 +00:00
|
|
|
it('can open new window when set', async () => {
|
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
webpreferences,
|
|
|
|
allowpopups: 'on',
|
|
|
|
src: `file://${fixtures}/pages/window-open-hide.html`
|
|
|
|
});
|
|
|
|
expect(message).to.equal('window');
|
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
});
|
2019-05-27 00:44:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
generateSpecs('without sandbox');
|
|
|
|
generateSpecs('with sandbox', 'sandbox=yes');
|
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 () => {
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/d.html`,
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'nodeIntegration,contextIsolation=no'
|
2016-10-14 23:04:33 +00:00
|
|
|
});
|
2018-06-01 08:56:48 +00:00
|
|
|
|
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
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 () => {
|
2021-04-07 01:46:23 +00:00
|
|
|
const result = await loadFileInWebView(webview, { webpreferences: 'webSecurity=no, nodeIntegration=yes, contextIsolation=no' });
|
|
|
|
expect(result).to.equal('loaded');
|
|
|
|
const type = await webview.executeJavaScript('typeof require');
|
|
|
|
expect(type).to.equal('function');
|
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, {
|
2021-04-13 19:36:38 +00:00
|
|
|
src: `file://${fixtures}/pages/window-open.html`,
|
|
|
|
allowpopups: true
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-09-13 16:10:51 +00:00
|
|
|
const { url, frameName } = await waitForEvent(webview, 'new-window');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
expect(frameName).to.equal('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, {
|
2021-04-13 19:36:38 +00:00
|
|
|
src: `file://${fixtures}/pages/target-name.html`,
|
|
|
|
allowpopups: true
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-09-13 16:10:51 +00:00
|
|
|
const { url, frameName } = await waitForEvent(webview, 'new-window');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
expect(frameName).to.equal('target');
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ipc-message event', () => {
|
2019-01-22 16:47:58 +00:00
|
|
|
it('emits when guest sends an ipc message to browser', async () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/ipc-message.html`
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2021-08-25 07:46:46 +00:00
|
|
|
const { frameId, channel, args } = await waitForEvent(webview, 'ipc-message');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2021-08-25 07:46:46 +00:00
|
|
|
expect(frameId).to.be.an('array').that.has.lengthOf(2);
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(channel).to.equal('channel');
|
|
|
|
expect(args).to.deep.equal(['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-09-13 16:10:51 +00:00
|
|
|
const { title, explicitSet } = await waitForEvent(webview, 'page-title-set');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(title).to.equal('test');
|
|
|
|
expect(explicitSet).to.be.true();
|
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-09-13 16:10:51 +00:00
|
|
|
const { favicons } = await waitForEvent(webview, 'page-favicon-updated');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(favicons).to.be.an('array').of.length(2);
|
2018-05-14 22:00:49 +00:00
|
|
|
if (process.platform === 'win32') {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(favicons[0]).to.match(/^file:\/\/\/[A-Z]:\/favicon.png$/i);
|
2018-05-14 22:00:49 +00:00
|
|
|
} else {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(favicons[0]).to.equal('file:///favicon.png');
|
2018-05-14 22:00:49 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-23 14:26:00 +00:00
|
|
|
describe('did-redirect-navigation event', () => {
|
|
|
|
let server = null;
|
|
|
|
let uri = null;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/302') {
|
|
|
|
res.setHeader('Location', '/200');
|
|
|
|
res.statusCode = 302;
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
uri = `http://127.0.0.1:${(server.address()).port}`;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is emitted on redirects', async () => {
|
|
|
|
loadWebView(webview, {
|
|
|
|
src: `${uri}/302`
|
|
|
|
});
|
|
|
|
|
|
|
|
const event = await waitForEvent(webview, 'did-redirect-navigation');
|
|
|
|
|
|
|
|
expect(event.url).to.equal(`${uri}/200`);
|
|
|
|
expect(event.isInPlace).to.be.false();
|
|
|
|
expect(event.isMainFrame).to.be.true();
|
|
|
|
expect(event.frameProcessId).to.be.a('number');
|
|
|
|
expect(event.frameRoutingId).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-09-13 16:10:51 +00:00
|
|
|
const { url } = await waitForEvent(webview, 'will-navigate');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.equal('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 () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, { src: pageUrl });
|
|
|
|
const { url } = await waitForEvent(webview, 'did-navigate');
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.equal(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-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, { src: pageUrl });
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'did-navigate-in-page');
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(event.url).to.equal(`${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-09-13 16:10:51 +00:00
|
|
|
const { url } = await waitForEvent(webview, 'did-navigate-in-page');
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.equal('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-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, { src: pageUrl });
|
2018-05-14 22:00:49 +00:00
|
|
|
const event = await waitForEvent(webview, 'did-navigate-in-page');
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(event.url).to.equal(`${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 () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, { src: `file://${fixtures}/pages/close.html` });
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'close');
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
// FIXME(zcbenz): Disabled because of moving to OOPIF webview.
|
|
|
|
xdescribe('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);
|
|
|
|
|
|
|
|
// Setup an event handler for further usage.
|
|
|
|
const waitForDomReady = waitForEvent(webview2, 'dom-ready');
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, { src: 'about:blank' });
|
2018-05-14 22:00:49 +00:00
|
|
|
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();
|
2019-05-21 17:07:17 +00:00
|
|
|
expect(devtools.getURL().startsWith('devtools://devtools')).to.be.true();
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-03-14 19:08:54 +00:00
|
|
|
const name = await devtools.executeJavaScript('InspectorFrontendHost.constructor.name');
|
2018-06-01 08:56:48 +00:00
|
|
|
document.body.removeChild(webview2);
|
|
|
|
|
|
|
|
expect(name).to.be.equal('InspectorFrontendHostImpl');
|
2017-11-30 12:04:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-29 16:01:31 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-29 16:01:31 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-29 16:01:31 +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',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
src: `file://${fixtures}/pages/beforeunload-false.html`
|
|
|
|
});
|
|
|
|
|
|
|
|
// Event handler has to be added before reload.
|
|
|
|
const waitForOnbeforeunload = waitForEvent(webview, 'ipc-message');
|
|
|
|
|
|
|
|
webview.reload();
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { channel } = await waitForOnbeforeunload;
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(channel).to.equal('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) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(e.channel).to.equal('history');
|
|
|
|
expect(e.args[0]).to.equal(1);
|
|
|
|
expect(webview.canGoBack()).to.be.false();
|
|
|
|
expect(webview.canGoForward()).to.be.false();
|
2016-09-17 00:06:16 +00:00
|
|
|
} else if (loadCount === 2) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(e.channel).to.equal('history');
|
|
|
|
expect(e.args[0]).to.equal(2);
|
|
|
|
expect(webview.canGoBack()).to.be.false();
|
|
|
|
expect(webview.canGoForward()).to.be.true();
|
2016-09-17 00:06:16 +00:00
|
|
|
webview.removeEventListener('ipc-message', listener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
const loadListener = () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
if (loadCount === 1) {
|
|
|
|
webview.src = `file://${fixtures}/pages/base-page.html`;
|
|
|
|
} else if (loadCount === 2) {
|
|
|
|
expect(webview.canGoBack()).to.be.true();
|
|
|
|
expect(webview.canGoForward()).to.be.false();
|
|
|
|
|
|
|
|
webview.goBack();
|
|
|
|
} else if (loadCount === 3) {
|
|
|
|
webview.goForward();
|
|
|
|
} else if (loadCount === 4) {
|
|
|
|
expect(webview.canGoBack()).to.be.true();
|
|
|
|
expect(webview.canGoForward()).to.be.false();
|
|
|
|
|
|
|
|
webview.removeEventListener('did-finish-load', loadListener);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCount += 1;
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-23 23:54:41 +00:00
|
|
|
// FIXME: https://github.com/electron/electron/issues/19397
|
|
|
|
xdescribe('<webview>.clearHistory()', () => {
|
2018-05-14 22:00:49 +00:00
|
|
|
it('should clear the navigation history', async () => {
|
2019-07-02 20:48:26 +00:00
|
|
|
const message = waitForEvent(webview, 'ipc-message');
|
|
|
|
await loadWebView(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${fixtures}/pages/history.html`
|
|
|
|
});
|
2019-07-02 20:48:26 +00:00
|
|
|
const event = await message;
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(event.channel).to.equal('history');
|
|
|
|
expect(event.args[0]).to.equal(2);
|
|
|
|
expect(webview.canGoBack()).to.be.true();
|
2018-05-14 22:00:49 +00:00
|
|
|
|
|
|
|
webview.clearHistory();
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(webview.canGoBack()).to.be.false();
|
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) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
expect(e.channel).to.equal(message);
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
loadWebView(webview, {
|
|
|
|
nodeintegration: 'on',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
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 =
|
|
|
|
'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';
|
|
|
|
|
2019-03-14 19:08:54 +00:00
|
|
|
const result = await webview.executeJavaScript(jsScript);
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(result).to.equal(expectedResult);
|
2019-03-14 19:08:54 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
|
2019-06-17 15:39:36 +00:00
|
|
|
it('supports inserting CSS', async () => {
|
|
|
|
await loadWebView(webview, { src: `file://${fixtures}/pages/base-page.html` });
|
|
|
|
await webview.insertCSS('body { background-repeat: round; }');
|
|
|
|
const result = await webview.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")');
|
|
|
|
expect(result).to.equal('round');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports removing inserted CSS', async () => {
|
|
|
|
await loadWebView(webview, { src: `file://${fixtures}/pages/base-page.html` });
|
|
|
|
const key = await webview.insertCSS('body { background-repeat: round; }');
|
|
|
|
await webview.removeInsertedCSS(key);
|
|
|
|
const result = await webview.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")');
|
|
|
|
expect(result).to.equal('repeat');
|
|
|
|
});
|
|
|
|
|
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',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
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
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { channel, args } = await waitForIpcMessage;
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(channel).to.equal('keyup');
|
|
|
|
expect(args).to.deep.equal(['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',
|
2021-03-01 21:52:29 +00:00
|
|
|
webpreferences: 'contextIsolation=no',
|
2018-05-14 22:00:49 +00:00
|
|
|
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
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { channel, args } = await waitForIpcMessage;
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(channel).to.equal('mouseup');
|
|
|
|
expect(args).to.deep.equal([10, 20, false, true]);
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-06 03:41:34 +00:00
|
|
|
describe('context-menu event', () => {
|
|
|
|
it('emits when right-clicked in page', async () => {
|
|
|
|
await loadWebView(webview, { src: 'about:blank' });
|
|
|
|
|
|
|
|
const promise = waitForEvent(webview, 'context-menu');
|
|
|
|
|
|
|
|
// Simulate right-click to create context-menu event.
|
|
|
|
const opts = { x: 0, y: 0, button: 'right' };
|
|
|
|
webview.sendInputEvent({ ...opts, type: 'mouseDown' });
|
|
|
|
webview.sendInputEvent({ ...opts, type: 'mouseUp' });
|
|
|
|
|
|
|
|
const { params } = await promise;
|
|
|
|
|
|
|
|
expect(params.pageURL).to.equal(webview.getURL());
|
|
|
|
expect(params.frame).to.be.undefined();
|
|
|
|
expect(params.x).to.be.a('number');
|
|
|
|
expect(params.y).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-30 11:50:08 +00:00
|
|
|
describe('media-started-playing media-paused events', () => {
|
2021-01-07 22:43:20 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
if (!document.createElement('audio').canPlayType('audio/wav')) {
|
|
|
|
this.skip();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-14 22:00:49 +00:00
|
|
|
it('emits when audio starts and stops playing', async () => {
|
2018-10-09 00:59:40 +00:00
|
|
|
await loadWebView(webview, { src: `file://${fixtures}/pages/base-page.html` });
|
|
|
|
|
|
|
|
// With the new autoplay policy, audio elements must be unmuted
|
|
|
|
// see https://goo.gl/xX8pDD.
|
|
|
|
const source = `
|
|
|
|
const audio = document.createElement("audio")
|
|
|
|
audio.src = "../assets/tone.wav"
|
|
|
|
document.body.appendChild(audio);
|
|
|
|
audio.play()
|
|
|
|
`;
|
|
|
|
webview.executeJavaScript(source, true);
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'media-started-playing');
|
2018-05-30 11:50:08 +00:00
|
|
|
|
|
|
|
webview.executeJavaScript('document.querySelector("audio").pause()', true);
|
2018-05-14 22:00:49 +00:00
|
|
|
await waitForEvent(webview, 'media-paused');
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-16 22:57:40 +00:00
|
|
|
describe('found-in-page event', () => {
|
2020-07-01 19:14:38 +00:00
|
|
|
it('emits when a request is made', async () => {
|
|
|
|
const didFinishLoad = waitForEvent(webview, 'did-finish-load');
|
2018-09-13 16:10:51 +00:00
|
|
|
loadWebView(webview, { src: `file://${fixtures}/pages/content.html` });
|
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();
|
2020-07-01 19:14:38 +00:00
|
|
|
await didFinishLoad;
|
|
|
|
|
|
|
|
const activeMatchOrdinal = [];
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const foundInPage = waitForEvent(webview, 'found-in-page');
|
|
|
|
const requestId = webview.findInPage('virtual');
|
|
|
|
const event = await foundInPage;
|
|
|
|
|
|
|
|
expect(event.result.requestId).to.equal(requestId);
|
|
|
|
expect(event.result.matches).to.equal(3);
|
|
|
|
|
|
|
|
activeMatchOrdinal.push(event.result.activeMatchOrdinal);
|
|
|
|
|
|
|
|
if (event.result.activeMatchOrdinal === event.result.matches) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(activeMatchOrdinal).to.deep.equal([1, 2, 3]);
|
|
|
|
webview.stopFindInPage('clearSelection');
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-26 01:57:27 +00:00
|
|
|
describe('<webview>.getWebContentsId', () => {
|
|
|
|
it('can return the WebContents ID', async () => {
|
|
|
|
const src = 'about:blank';
|
|
|
|
await loadWebView(webview, { src });
|
|
|
|
|
2019-11-08 20:46:35 +00:00
|
|
|
expect(webview.getWebContentsId()).to.be.a('number');
|
2019-01-08 22:27:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
// TODO(nornagon): this seems to have become much less reliable as of
|
|
|
|
// https://github.com/electron/electron/pull/32419. Tracked at
|
|
|
|
// https://github.com/electron/electron/issues/32705.
|
|
|
|
describe.skip('<webview>.capturePage()', () => {
|
2019-11-19 17:57:35 +00:00
|
|
|
before(function () {
|
|
|
|
// TODO(miniak): figure out why this is failing on windows
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
this.skip();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-12 20:56:17 +00:00
|
|
|
it('returns a Promise with a NativeImage', async () => {
|
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebView(webview, { src });
|
|
|
|
|
|
|
|
const image = await webview.capturePage();
|
|
|
|
const imgBuffer = image.toPNG();
|
|
|
|
|
|
|
|
// Check the 25th byte in the PNG.
|
|
|
|
// Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha
|
|
|
|
expect(imgBuffer[25]).to.equal(6);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-11 01:46:45 +00:00
|
|
|
ifdescribe(features.isPrintingEnabled())('<webview>.printToPDF()', () => {
|
2020-02-11 03:35:05 +00:00
|
|
|
it('rejects on incorrectly typed parameters', async () => {
|
|
|
|
const badTypes = {
|
|
|
|
marginsType: 'terrible',
|
|
|
|
scaleFactor: 'not-a-number',
|
|
|
|
landscape: [],
|
2020-03-20 15:12:18 +00:00
|
|
|
pageRanges: { oops: 'im-not-the-right-key' },
|
2020-02-11 03:35:05 +00:00
|
|
|
headerFooter: '123',
|
|
|
|
printSelectionOnly: 1,
|
|
|
|
printBackground: 2,
|
|
|
|
pageSize: 'IAmAPageSize'
|
|
|
|
};
|
|
|
|
|
|
|
|
// These will hard crash in Chromium unless we type-check
|
|
|
|
for (const [key, value] of Object.entries(badTypes)) {
|
|
|
|
const param = { [key]: value };
|
|
|
|
|
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebView(webview, { src });
|
|
|
|
await expect(webview.printToPDF(param)).to.eventually.be.rejected();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can print to PDF', async () => {
|
2019-02-11 19:20:04 +00:00
|
|
|
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
|
|
|
|
await loadWebView(webview, { src });
|
|
|
|
|
|
|
|
const data = await webview.printToPDF({});
|
2019-10-09 17:59:08 +00:00
|
|
|
expect(data).to.be.an.instanceof(Uint8Array).that.is.not.empty();
|
2019-02-11 19:20:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-09 19:49:14 +00:00
|
|
|
describe('will-attach-webview event', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
it('does not emit when src is not changed', async () => {
|
|
|
|
console.log('loadWebView(webview)');
|
2018-07-21 02:11:28 +00:00
|
|
|
loadWebView(webview);
|
2020-06-30 22:10:36 +00:00
|
|
|
await delay();
|
|
|
|
const expectedErrorMessage =
|
|
|
|
'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);
|
2018-07-21 02:11:28 +00:00
|
|
|
});
|
|
|
|
|
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-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
nodeintegration: 'yes',
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
2017-02-03 20:21:46 +00:00
|
|
|
});
|
2018-05-14 22:00:49 +00:00
|
|
|
|
2018-06-01 08:56:48 +00:00
|
|
|
const types = JSON.parse(message);
|
|
|
|
expect(types).to.include({
|
|
|
|
require: 'undefined',
|
|
|
|
module: 'undefined',
|
|
|
|
process: 'undefined',
|
|
|
|
global: 'undefined'
|
|
|
|
});
|
2017-02-03 20:21:46 +00:00
|
|
|
});
|
|
|
|
|
2022-01-12 07:41:20 +00:00
|
|
|
it('handler modifying params.instanceId does not break <webview>', async () => {
|
|
|
|
ipcRenderer.send('break-next-will-attach-webview');
|
|
|
|
|
|
|
|
await startLoadingWebViewAndWaitForMessage(webview, {
|
|
|
|
src: `file://${fixtures}/pages/a.html`
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2018-06-19 11:25:26 +00:00
|
|
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
2018-05-14 22:00:49 +00:00
|
|
|
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
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.equal('undefined');
|
2017-05-17 20:47:00 +00:00
|
|
|
});
|
2017-02-03 20:21:46 +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();
|
|
|
|
});
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const generateSpecs = (description, sandbox) => {
|
|
|
|
describe(description, () => {
|
2022-03-23 16:40:29 +00:00
|
|
|
// TODO(nornagon): disabled during chromium roll 2019-06-11 due to a
|
|
|
|
// 'ResizeObserver loop limit exceeded' error on Windows
|
|
|
|
xit('emits resize events', async () => {
|
2019-01-22 01:08:16 +00:00
|
|
|
const firstResizeSignal = waitForEvent(webview, 'resize');
|
|
|
|
const domReadySignal = waitForEvent(webview, 'dom-ready');
|
2018-08-16 22:57:40 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/a.html`;
|
|
|
|
webview.webpreferences = `sandbox=${sandbox ? 'yes' : 'no'}`;
|
|
|
|
div.appendChild(webview);
|
|
|
|
document.body.appendChild(div);
|
2018-06-18 14:47:17 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const firstResizeEvent = await firstResizeSignal;
|
|
|
|
expect(firstResizeEvent.target).to.equal(webview);
|
|
|
|
expect(firstResizeEvent.newWidth).to.equal(100);
|
|
|
|
expect(firstResizeEvent.newHeight).to.equal(10);
|
2018-06-18 14:47:17 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
await domReadySignal;
|
2018-08-16 22:57:40 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const secondResizeSignal = waitForEvent(webview, 'resize');
|
2018-06-18 14:47:17 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const newWidth = 1234;
|
|
|
|
const newHeight = 789;
|
|
|
|
div.style.width = `${newWidth}px`;
|
|
|
|
div.style.height = `${newHeight}px`;
|
2018-06-18 14:47:17 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const secondResizeEvent = await secondResizeSignal;
|
|
|
|
expect(secondResizeEvent.target).to.equal(webview);
|
|
|
|
expect(secondResizeEvent.newWidth).to.equal(newWidth);
|
|
|
|
expect(secondResizeEvent.newHeight).to.equal(newHeight);
|
|
|
|
});
|
2018-08-28 18:35:44 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
it('emits focus event', async () => {
|
|
|
|
const domReadySignal = waitForEvent(webview, 'dom-ready');
|
|
|
|
webview.src = `file://${fixtures}/pages/a.html`;
|
|
|
|
webview.webpreferences = `sandbox=${sandbox ? 'yes' : 'no'}`;
|
|
|
|
document.body.appendChild(webview);
|
2018-08-28 18:35:44 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
await domReadySignal;
|
2018-08-28 18:35:44 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// If this test fails, check if webview.focus() still works.
|
|
|
|
const focusSignal = waitForEvent(webview, 'focus');
|
|
|
|
webview.focus();
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
await focusSignal;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
generateSpecs('without sandbox', false);
|
|
|
|
generateSpecs('with sandbox', true);
|
2016-11-03 22:12:54 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|