2020-03-20 20:28:31 +00:00
|
|
|
import * as path from 'path';
|
2021-02-16 22:21:40 +00:00
|
|
|
import * as url from 'url';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { BrowserWindow, session, ipcMain, app, WebContents } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { closeAllWindows } from './window-helpers';
|
2020-07-01 19:14:38 +00:00
|
|
|
import { emittedOnce, emittedUntil } from './events-helpers';
|
2020-05-13 18:05:44 +00:00
|
|
|
import { ifdescribe } from './spec-helpers';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const features = process._linkedBinding('electron_common_features');
|
2020-05-13 18:05:44 +00:00
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
async function loadWebView (w: WebContents, attributes: Record<string, string>, openDevTools: boolean = false): Promise<void> {
|
2019-09-03 17:10:58 +00:00
|
|
|
await w.executeJavaScript(`
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
for (const [k, v] of Object.entries(${JSON.stringify(attributes)})) {
|
|
|
|
webview.setAttribute(k, v)
|
|
|
|
}
|
|
|
|
document.body.appendChild(webview)
|
2020-04-13 23:39:26 +00:00
|
|
|
webview.addEventListener('dom-ready', () => {
|
|
|
|
if (${openDevTools}) {
|
|
|
|
webview.openDevTools()
|
|
|
|
}
|
|
|
|
})
|
2019-09-03 17:10:58 +00:00
|
|
|
webview.addEventListener('did-finish-load', () => {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
2020-03-20 20:28:31 +00:00
|
|
|
`);
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('<webview> tag', function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const fixtures = path.join(__dirname, '..', 'spec', 'fixtures');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(closeAllWindows);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-05-21 15:46:17 +00:00
|
|
|
function hideChildWindows (e: any, wc: WebContents) {
|
|
|
|
wc.on('new-window', (event, url, frameName, disposition, options) => {
|
|
|
|
options.show = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
app.on('web-contents-created', hideChildWindows);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
app.off('web-contents-created', hideChildWindows);
|
|
|
|
});
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('works without script tag in page', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'));
|
|
|
|
await emittedOnce(ipcMain, 'pong');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('works with sandbox', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
sandbox: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
|
|
|
|
await emittedOnce(ipcMain, 'pong');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('works with contextIsolation', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
|
|
|
|
await emittedOnce(ipcMain, 'pong');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('works with contextIsolation + sandbox', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true,
|
|
|
|
sandbox: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-isolated.html'));
|
|
|
|
await emittedOnce(ipcMain, 'pong');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2021-01-25 01:29:25 +00:00
|
|
|
it('works with Trusted Types', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-trusted-types.html'));
|
|
|
|
await emittedOnce(ipcMain, 'pong');
|
|
|
|
});
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('is disabled by default', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: path.join(fixtures, 'module', 'preload-webview.js'),
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const webview = emittedOnce(ipcMain, 'webview');
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'));
|
|
|
|
const [, type] = await webview;
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(type).to.equal('undefined', 'WebView still exists');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
// FIXME(deepak1556): Ch69 follow up.
|
|
|
|
xdescribe('document.visibilityState/hidden', () => {
|
|
|
|
afterEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.removeAllListeners('pong');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('updates when the window is shown after the ready-to-show event', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const readyToShowSignal = emittedOnce(w, 'ready-to-show');
|
|
|
|
const pongSignal1 = emittedOnce(ipcMain, 'pong');
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-visibilitychange.html'));
|
|
|
|
await pongSignal1;
|
|
|
|
const pongSignal2 = emittedOnce(ipcMain, 'pong');
|
|
|
|
await readyToShowSignal;
|
|
|
|
w.show();
|
|
|
|
|
|
|
|
const [, visibilityState, hidden] = await pongSignal2;
|
|
|
|
expect(visibilityState).to.equal('visible');
|
|
|
|
expect(hidden).to.be.false();
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('inherits the parent window visibility state and receives visibilitychange events', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-visibilitychange.html'));
|
|
|
|
const [, visibilityState, hidden] = await emittedOnce(ipcMain, 'pong');
|
|
|
|
expect(visibilityState).to.equal('hidden');
|
|
|
|
expect(hidden).to.be.true();
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
// We have to start waiting for the event
|
|
|
|
// before we ask the webContents to resize.
|
2020-03-20 20:28:31 +00:00
|
|
|
const getResponse = emittedOnce(ipcMain, 'pong');
|
|
|
|
w.webContents.emit('-window-visibility-change', 'visible');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
return getResponse.then(([, visibilityState, hidden]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(visibilityState).to.equal('visible');
|
|
|
|
expect(hidden).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
describe('did-attach-webview event', () => {
|
|
|
|
it('is emitted when a webview has been attached', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const didAttachWebview = emittedOnce(w.webContents, 'did-attach-webview');
|
|
|
|
const webviewDomReady = emittedOnce(ipcMain, 'webview-dom-ready');
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'));
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, webContents] = await didAttachWebview;
|
|
|
|
const [, id] = await webviewDomReady;
|
|
|
|
expect(webContents.id).to.equal(id);
|
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2021-02-16 22:21:40 +00:00
|
|
|
describe('did-change-theme-color event', () => {
|
|
|
|
it('emits when theme color changes', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const src = url.format({
|
|
|
|
pathname: `${fixtures.replace(/\\/g, '/')}/pages/theme-color.html`,
|
|
|
|
protocol: 'file',
|
|
|
|
slashes: true
|
|
|
|
});
|
|
|
|
const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
webview.setAttribute('src', '${src}')
|
|
|
|
webview.addEventListener('did-change-theme-color', (e) => {
|
|
|
|
resolve('ok')
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
expect(message).to.equal('ok');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('loads devtools extensions registered on the parent window', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-11-30 22:40:56 +00:00
|
|
|
w.webContents.session.removeExtension('foo');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo');
|
2020-11-30 22:40:56 +00:00
|
|
|
await w.webContents.session.loadExtension(extensionPath);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'webview-devtools.html'));
|
2020-04-13 23:39:26 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
src: `file://${path.join(__dirname, 'fixtures', 'blank.html')}`
|
|
|
|
}, true);
|
2020-03-20 20:28:31 +00:00
|
|
|
let childWebContentsId = 0;
|
2019-11-14 22:09:03 +00:00
|
|
|
app.once('web-contents-created', (e, webContents) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
childWebContentsId = webContents.id;
|
2019-11-14 22:09:03 +00:00
|
|
|
webContents.on('devtools-opened', function () {
|
|
|
|
const showPanelIntervalId = setInterval(function () {
|
|
|
|
if (!webContents.isDestroyed() && webContents.devToolsWebContents) {
|
|
|
|
webContents.devToolsWebContents.executeJavaScript('(' + function () {
|
2021-01-25 16:46:00 +00:00
|
|
|
const { UI } = (window as any);
|
|
|
|
const tabs = UI.inspectorView._tabbedPane._tabs;
|
|
|
|
const lastPanelId: any = tabs[tabs.length - 1].id;
|
|
|
|
UI.inspectorView.showPanel(lastPanelId);
|
2020-03-20 20:28:31 +00:00
|
|
|
}.toString() + ')()');
|
2019-11-14 22:09:03 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
clearInterval(showPanelIntervalId);
|
2019-11-14 22:09:03 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, { runtimeId, tabId }] = await emittedOnce(ipcMain, 'answer');
|
|
|
|
expect(runtimeId).to.match(/^[a-z]{32}$/);
|
|
|
|
expect(tabId).to.equal(childWebContentsId);
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
describe('zoom behavior', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const zoomScheme = standardScheme;
|
|
|
|
const webviewSession = session.fromPartition('webview-temp');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
before(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const protocol = webviewSession.protocol;
|
2019-09-03 17:10:58 +00:00
|
|
|
protocol.registerStringProtocol(zoomScheme, (request, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback('hello');
|
2020-06-02 16:46:18 +00:00
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
after(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const protocol = webviewSession.protocol;
|
2020-06-02 16:46:18 +00:00
|
|
|
protocol.unregisterProtocol(zoomScheme);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('inherits the zoomFactor of the parent window', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const zoomEventPromise = emittedOnce(ipcMain, 'webview-parent-zoom-level');
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-factor.html'));
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, zoomFactor, zoomLevel] = await zoomEventPromise;
|
|
|
|
expect(zoomFactor).to.equal(1.2);
|
|
|
|
expect(zoomLevel).to.equal(1);
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('maintains zoom level on navigation', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const promise = new Promise<void>((resolve) => {
|
2019-09-03 17:10:58 +00:00
|
|
|
ipcMain.on('webview-zoom-level', (event, zoomLevel, zoomFactor, newHost, final) => {
|
|
|
|
if (!newHost) {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(zoomFactor).to.equal(1.44);
|
|
|
|
expect(zoomLevel).to.equal(2.0);
|
2019-09-03 17:10:58 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(zoomFactor).to.equal(1.2);
|
|
|
|
expect(zoomLevel).to.equal(1);
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (final) {
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve();
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-custom-zoom-level.html'));
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await promise;
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('maintains zoom level when navigating within same page', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
const promise = new Promise<void>((resolve) => {
|
2019-09-03 17:10:58 +00:00
|
|
|
ipcMain.on('webview-zoom-in-page', (event, zoomLevel, zoomFactor, final) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(zoomFactor).to.equal(1.44);
|
|
|
|
expect(zoomLevel).to.equal(2.0);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
if (final) {
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve();
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-in-page-navigate.html'));
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await promise;
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('inherits zoom level for the origin when available', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
zoomFactor: 1.2
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-origin-zoom-level.html'));
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, zoomLevel] = await emittedOnce(ipcMain, 'webview-origin-zoom-level');
|
|
|
|
expect(zoomLevel).to.equal(2.0);
|
|
|
|
});
|
2020-07-28 20:00:44 +00:00
|
|
|
|
|
|
|
it('does not crash when navigating with zoom level inherited from parent', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
nodeIntegration: true,
|
|
|
|
zoomFactor: 1.2,
|
|
|
|
session: webviewSession
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const attachPromise = emittedOnce(w.webContents, 'did-attach-webview');
|
|
|
|
const readyPromise = emittedOnce(ipcMain, 'dom-ready');
|
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html'));
|
|
|
|
const [, webview] = await attachPromise;
|
|
|
|
await readyPromise;
|
|
|
|
expect(webview.getZoomFactor()).to.equal(1.2);
|
|
|
|
await w.loadURL(`${zoomScheme}://host1`);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
describe('nativeWindowOpen option', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-09-03 17:10:58 +00:00
|
|
|
beforeEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('opens window of about:blank with cross-scripting enabled', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, content] = await emittedOnce(ipcMain, 'answer');
|
|
|
|
expect(content).to.equal('Hello');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('opens window of same domain with cross-scripting enabled', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-file.html')}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, content] = await emittedOnce(ipcMain, 'answer');
|
|
|
|
expect(content).to.equal('Hello');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('returns null from window.open when allowpopups is not set', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-no-allowpopups.html')}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, { windowOpenReturnedNull }] = await emittedOnce(ipcMain, 'answer');
|
|
|
|
expect(windowOpenReturnedNull).to.be.true();
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('blocks accessing cross-origin frames', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, content] = await emittedOnce(ipcMain, 'answer');
|
2019-09-03 17:10:58 +00:00
|
|
|
const expectedContent =
|
2020-03-20 20:28:31 +00:00
|
|
|
'Blocked a frame with origin "file://" from accessing a cross-origin frame.';
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(content).to.equal(expectedContent);
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits a new-window event', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
const attributes = {
|
|
|
|
allowpopups: 'on',
|
|
|
|
nodeintegration: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-09-03 17:10:58 +00:00
|
|
|
const { url, frameName } = await w.webContents.executeJavaScript(`
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const webview = document.createElement('webview')
|
|
|
|
for (const [k, v] of Object.entries(${JSON.stringify(attributes)})) {
|
|
|
|
webview.setAttribute(k, v)
|
|
|
|
}
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
webview.addEventListener('new-window', (e) => {
|
|
|
|
resolve({url: e.url, frameName: e.frameName})
|
|
|
|
})
|
|
|
|
})
|
2020-03-20 20:28:31 +00:00
|
|
|
`);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
expect(frameName).to.equal('host');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits a browser-window-created event', async () => {
|
|
|
|
// Don't wait for loading to finish.
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await emittedOnce(app, 'browser-window-created');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-07-01 19:14:38 +00:00
|
|
|
it('emits a web-contents-created event', async () => {
|
|
|
|
const webContentsCreated = emittedUntil(app, 'web-contents-created',
|
|
|
|
(event: Electron.Event, contents: Electron.WebContents) => contents.getType() === 'window');
|
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'on',
|
|
|
|
webpreferences: 'nativeWindowOpen=1',
|
|
|
|
src: `file://${fixtures}/pages/window-open.html`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-07-01 19:14:38 +00:00
|
|
|
|
|
|
|
await webContentsCreated;
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
describe('webpreferences attribute', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-09-03 17:10:58 +00:00
|
|
|
beforeEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-09-03 17:10:58 +00:00
|
|
|
it('can enable context isolation', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
allowpopups: 'yes',
|
|
|
|
preload: `file://${fixtures}/api/isolated-preload.js`,
|
|
|
|
src: `file://${fixtures}/api/isolated.html`,
|
|
|
|
webpreferences: 'contextIsolation=yes'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, data] = await emittedOnce(ipcMain, 'isolated-world');
|
2019-09-03 17:10:58 +00:00
|
|
|
expect(data).to.deep.equal({
|
|
|
|
preloadContext: {
|
|
|
|
preloadProperty: 'number',
|
|
|
|
pageProperty: 'undefined',
|
|
|
|
typeofRequire: 'function',
|
|
|
|
typeofProcess: 'object',
|
|
|
|
typeofArrayPush: 'function',
|
|
|
|
typeofFunctionApply: 'function',
|
|
|
|
typeofPreloadExecuteJavaScriptProperty: 'undefined'
|
|
|
|
},
|
|
|
|
pageContext: {
|
|
|
|
preloadProperty: 'undefined',
|
|
|
|
pageProperty: 'string',
|
|
|
|
typeofRequire: 'undefined',
|
|
|
|
typeofProcess: 'undefined',
|
|
|
|
typeofArrayPush: 'number',
|
|
|
|
typeofFunctionApply: 'boolean',
|
|
|
|
typeofPreloadExecuteJavaScriptProperty: 'number',
|
|
|
|
typeofOpenedWindow: 'object'
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
describe('permission request handlers', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-09-03 17:10:58 +00:00
|
|
|
beforeEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const partition = 'permissionTest';
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
function setUpRequestHandler (webContentsId: number, requestedPermission: string) {
|
2021-01-22 19:25:47 +00:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2019-09-03 17:10:58 +00:00
|
|
|
session.fromPartition(partition).setPermissionRequestHandler(function (webContents, permission, callback) {
|
|
|
|
if (webContents.id === webContentsId) {
|
|
|
|
// requestMIDIAccess with sysex requests both midi and midiSysex so
|
|
|
|
// grant the first midi one and then reject the midiSysex one
|
|
|
|
if (requestedPermission === 'midiSysex' && permission === 'midi') {
|
2020-03-20 20:28:31 +00:00
|
|
|
return callback(true);
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(permission).to.equal(requestedPermission);
|
2019-09-03 17:10:58 +00:00
|
|
|
} catch (e) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return reject(e);
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(false);
|
|
|
|
resolve();
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
}
|
|
|
|
afterEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
session.fromPartition(partition).setPermissionRequestHandler(null);
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
// This is disabled because CI machines don't have cameras or microphones,
|
|
|
|
// so Chrome responds with "NotFoundError" instead of
|
|
|
|
// "PermissionDeniedError". It should be re-enabled if we find a way to mock
|
|
|
|
// the presence of a microphone & camera.
|
|
|
|
xit('emits when using navigator.getUserMedia api', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const errorFromRenderer = emittedOnce(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/media.html`,
|
|
|
|
partition,
|
|
|
|
nodeintegration: 'on'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
setUpRequestHandler(webViewContents.id, 'media');
|
|
|
|
const [, errorName] = await errorFromRenderer;
|
|
|
|
expect(errorName).to.equal('PermissionDeniedError');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits when using navigator.geolocation api', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const errorFromRenderer = emittedOnce(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/geolocation.html`,
|
|
|
|
partition,
|
|
|
|
nodeintegration: 'on'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
setUpRequestHandler(webViewContents.id, 'geolocation');
|
|
|
|
const [, error] = await errorFromRenderer;
|
|
|
|
expect(error).to.equal('User denied Geolocation');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits when using navigator.requestMIDIAccess without sysex api', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const errorFromRenderer = emittedOnce(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/midi.html`,
|
|
|
|
partition,
|
|
|
|
nodeintegration: 'on'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
setUpRequestHandler(webViewContents.id, 'midi');
|
|
|
|
const [, error] = await errorFromRenderer;
|
|
|
|
expect(error).to.equal('SecurityError');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits when using navigator.requestMIDIAccess with sysex api', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const errorFromRenderer = emittedOnce(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/midi-sysex.html`,
|
|
|
|
partition,
|
|
|
|
nodeintegration: 'on'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
setUpRequestHandler(webViewContents.id, 'midiSysex');
|
|
|
|
const [, error] = await errorFromRenderer;
|
|
|
|
expect(error).to.equal('SecurityError');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits when accessing external protocol', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
2020-03-20 15:12:18 +00:00
|
|
|
src: 'magnet:test',
|
2019-11-01 20:37:02 +00:00
|
|
|
partition
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
await setUpRequestHandler(webViewContents.id, 'openExternal');
|
|
|
|
});
|
2019-09-03 17:10:58 +00:00
|
|
|
|
|
|
|
it('emits when using Notification.requestPermission', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const errorFromRenderer = emittedOnce(ipcMain, 'message');
|
2019-09-03 17:10:58 +00:00
|
|
|
loadWebView(w.webContents, {
|
|
|
|
src: `file://${fixtures}/pages/permissions/notification.html`,
|
|
|
|
partition,
|
|
|
|
nodeintegration: 'on'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await setUpRequestHandler(webViewContents.id, 'notifications');
|
2019-09-03 17:10:58 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const [, error] = await errorFromRenderer;
|
|
|
|
expect(error).to.equal('denied');
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-05-13 18:05:44 +00:00
|
|
|
ifdescribe(features.isRemoteModuleEnabled())('enableremotemodule attribute', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-12-09 18:27:30 +00:00
|
|
|
beforeEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
const generateSpecs = (description: string, sandbox: boolean) => {
|
|
|
|
describe(description, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const preload = `file://${fixtures}/module/preload-disable-remote.js`;
|
|
|
|
const src = `file://${fixtures}/api/blank.html`;
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
it('enables the remote module by default', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
preload,
|
|
|
|
src,
|
|
|
|
sandbox: sandbox.toString()
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
2020-09-10 16:17:17 +00:00
|
|
|
const [, , message] = await emittedUntil(webViewContents, 'console-message', (event: any, level: any, message: string) => !/deprecated/.test(message));
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const typeOfRemote = JSON.parse(message);
|
|
|
|
expect(typeOfRemote).to.equal('object');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
it('disables the remote module when false', async () => {
|
|
|
|
loadWebView(w.webContents, {
|
|
|
|
preload,
|
|
|
|
src,
|
|
|
|
sandbox: sandbox.toString(),
|
|
|
|
enableremotemodule: 'false'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const [, webViewContents] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
const [, , message] = await emittedOnce(webViewContents, 'console-message');
|
|
|
|
|
|
|
|
const typeOfRemote = JSON.parse(message);
|
|
|
|
expect(typeOfRemote).to.equal('undefined');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
generateSpecs('without sandbox', false);
|
|
|
|
generateSpecs('with sandbox', true);
|
|
|
|
});
|
2020-11-04 02:15:20 +00:00
|
|
|
|
|
|
|
describe('DOM events', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('receives extra properties on DOM events when contextIsolation is enabled', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webviewTag: true,
|
|
|
|
contextIsolation: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const message = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
|
|
|
|
const webview = new WebView()
|
|
|
|
webview.setAttribute('src', 'data:text/html,<script>console.log("hi")</script>')
|
|
|
|
webview.addEventListener('console-message', (e) => {
|
|
|
|
resolve(e.message)
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})`);
|
|
|
|
expect(message).to.equal('hi');
|
|
|
|
});
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|