electron/spec/webview-spec.js

1188 lines
38 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const { expect } = require('chai');
const path = require('path');
const http = require('http');
const url = require('url');
const { ipcRenderer } = require('electron');
const { emittedOnce, waitForEvent } = require('./events-helpers');
const { ifdescribe, ifit, delay } = require('./spec-helpers');
2018-06-19 11:25:26 +00:00
const features = process._linkedBinding('electron_common_features');
2020-03-20 20:28:31 +00:00
const nativeModulesEnabled = process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS;
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 () {
2020-03-20 20:28:31 +00:00
this.timeout(3 * 60 * 1000);
2020-03-20 20:28:31 +00:00
const fixtures = path.join(__dirname, 'fixtures');
let webview = null;
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)) {
2020-03-20 20:28:31 +00:00
webview.setAttribute(name, value);
2018-05-14 22:00:49 +00:00
}
2020-03-20 20:28:31 +00:00
document.body.appendChild(webview);
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 = {}) => {
2020-03-20 20:28:31 +00:00
loadWebView(webview, attributes); // Don't wait for load to be finished.
const event = await waitForEvent(webview, 'console-message');
return event.message;
};
2018-05-14 22:00:49 +00:00
beforeEach(() => {
2020-03-20 20:28:31 +00:00
webview = new WebView();
});
afterEach(() => {
if (!document.body.contains(webview)) {
2020-03-20 20:28:31 +00:00
document.body.appendChild(webview);
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
webview.remove();
});
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`
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.src = `file://${fixtures}/pages/b.html`;
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const { message } = await waitForEvent(webview, 'console-message');
expect(message).to.equal('b');
});
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'
2020-03-20 20:28:31 +00:00
});
expect(message).to.equal('Window script is loaded before preload script');
});
2016-09-08 23:56:29 +00:00
it('ignores empty values', () => {
2020-03-20 20:28:31 +00:00
expect(webview.src).to.equal('');
2018-05-14 22:00:49 +00:00
for (const emptyValue of ['', null, undefined]) {
2020-03-20 20:28:31 +00:00
webview.src = emptyValue;
expect(webview.src).to.equal('');
2018-05-14 22:00:49 +00:00
}
2020-03-20 20:28:31 +00:00
});
it('does not wait until loadURL is resolved', async () => {
2020-03-20 20:28:31 +00:00
await loadWebView(webview, { src: 'about:blank' });
2020-03-20 20:28:31 +00:00
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.
2020-03-20 20:28:31 +00:00
expect(now - before).to.be.below(100);
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'undefined',
module: 'undefined',
process: 'undefined',
global: 'undefined'
2020-03-20 20:28:31 +00:00
});
});
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',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/d.html`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object'
2020-03-20 20:28:31 +00:00
});
});
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 () {
// FIXME Figure out why this is timing out on AppVeyor
if (process.env.APPVEYOR === 'True') {
2020-03-20 20:28:31 +00:00
this.skip();
return;
}
2018-06-19 11:25:26 +00:00
const message = await startLoadingWebViewAndWaitForMessage(webview, {
2018-05-14 22:00:49 +00:00
nodeintegration: 'on',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/post.html`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object'
2020-03-20 20:28:31 +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({
pathname: `${fixtures}/pages/webview-opener-no-node-integration.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-node.html`
},
slashes: true
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
loadWebView(webview, {
allowpopups: 'on',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src
2020-03-20 20:28:31 +00:00
});
const { message } = await waitForEvent(webview, 'console-message');
expect(JSON.parse(message).isProcessGlobalUndefined).to.be.true();
});
2018-05-14 22:00:49 +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',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/native-module.html`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.reload();
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const { message } = await waitForEvent(webview, 'console-message');
expect(message).to.equal('function');
});
});
2016-03-25 20:03:49 +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`,
src: `file://${fixtures}/pages/e.html`,
contextIsolation: false
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
2018-05-14 22:00:49 +00:00
expect(types).to.include({
process: 'object',
Buffer: 'function'
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +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'
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
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
2020-03-20 20:28:31 +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 () => {
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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
2018-05-14 22:00:49 +00:00
expect(types).to.include({
process: 'object',
Buffer: 'function'
2020-03-20 20:28:31 +00:00
});
});
2018-05-14 22:00:49 +00:00
it('receives ipc message in preload script', async () => {
await loadWebView(webview, {
preload: `${fixtures}/module/preload-ipc.js`,
src: `file://${fixtures}/pages/e.html`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const message = 'boom!';
webview.send('ping', message);
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
2018-05-14 22:00:49 +00:00
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object',
Buffer: 'function'
2020-03-20 20:28: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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
2018-05-14 22:00:49 +00:00
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object',
Buffer: 'function'
2020-03-20 20:28:31 +00:00
});
});
2016-09-08 23:56:29 +00:00
it('ignores empty values', () => {
2020-03-20 20:28:31 +00:00
expect(webview.preload).to.equal('');
2018-05-14 22:00:49 +00:00
for (const emptyValue of ['', null, undefined]) {
2020-03-20 20:28:31 +00:00
webview.preload = emptyValue;
expect(webview.preload).to.equal('');
2018-05-14 22:00:49 +00:00
}
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +00:00
describe('httpreferrer attribute', () => {
it('sets the referrer url', (done) => {
2020-03-20 20:28:31 +00:00
const referrer = 'http://github.com/';
const server = http.createServer((req, res) => {
try {
expect(req.headers.referer).to.equal(referrer);
done();
} catch (e) {
done(e);
} finally {
res.end();
server.close();
}
}).listen(0, '127.0.0.1', () => {
2020-03-20 20:28:31 +00:00
const port = server.address().port;
2018-05-14 22:00:49 +00:00
loadWebView(webview, {
httpreferrer: referrer,
src: `http://127.0.0.1:${port}`
2020-03-20 20:28:31 +00:00
});
});
});
});
2016-03-25 20:03:49 +00:00
describe('useragent attribute', () => {
2018-05-14 22:00:49 +00:00
it('sets the user agent', async () => {
2020-03-20 20:28:31 +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
2020-03-20 20:28:31 +00:00
});
expect(message).to.equal(referrer);
});
});
2016-03-25 20:03:49 +00:00
describe('disablewebsecurity attribute', () => {
2018-05-14 22:00:49 +00:00
it('does not disable web security when not set', async () => {
2020-03-20 20:28:31 +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
2018-06-19 11:25:26 +00:00
const message = await startLoadingWebViewAndWaitForMessage(webview, {
2018-05-14 22:00:49 +00:00
src: `data:text/html;base64,${encoded}`
2020-03-20 20:28:31 +00:00
});
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 () => {
2020-03-20 20:28:31 +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
2018-06-19 11:25:26 +00:00
const message = await startLoadingWebViewAndWaitForMessage(webview, {
2018-05-14 22:00:49 +00:00
disablewebsecurity: '',
src: `data:text/html;base64,${encoded}`
2020-03-20 20:28:31 +00:00
});
expect(message).to.equal('ok');
});
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',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/d.html`
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object'
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object',
Buffer: 'function'
2020-03-20 20:28:31 +00:00
});
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'undefined',
module: 'undefined',
process: 'undefined',
global: 'undefined'
2020-03-20 20:28:31 +00:00
});
});
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',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/d.html`
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object'
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +00:00
2018-05-14 22:00:49 +00:00
it('isolates storage for different id', async () => {
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const parsedMessage = JSON.parse(message);
2018-05-14 22:00:49 +00:00
expect(parsedMessage).to.include({
numberOfEntries: 0,
testValue: null
2020-03-20 20:28:31 +00:00
});
});
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 () => {
2020-03-20 20:28:31 +00:00
const testValue = 'one';
window.localStorage.setItem('test', testValue);
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
src: `file://${fixtures}/pages/partition/one.html`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const parsedMessage = JSON.parse(message);
2018-05-14 22:00:49 +00:00
expect(parsedMessage).to.include({
numberOfEntries: 1,
testValue
2020-03-20 20:28:31 +00:00
});
});
});
2016-03-25 20:03:49 +00:00
describe('allowpopups attribute', () => {
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`
2020-03-20 20:28:31 +00:00
});
expect(message).to.equal('null');
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
expect(message).to.equal('window');
});
});
};
2020-03-20 20:28:31 +00:00
generateSpecs('without sandbox');
generateSpecs('with sandbox', 'sandbox=yes');
generateSpecs('with nativeWindowOpen', 'nativeWindowOpen=yes');
});
2016-03-25 20:03:49 +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`,
webpreferences: 'nodeIntegration,contextIsolation=no'
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'function',
module: 'object',
process: 'object'
2020-03-20 20:28:31 +00:00
});
});
ifit(features.isRemoteModuleEnabled())('can disable the remote module', async () => {
const message = await startLoadingWebViewAndWaitForMessage(webview, {
preload: `${fixtures}/module/preload-disable-remote.js`,
src: `file://${fixtures}/api/blank.html`,
webpreferences: 'enableRemoteModule=no'
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const typeOfRemote = JSON.parse(message);
expect(typeOfRemote).to.equal('undefined');
});
2018-05-14 22:00:49 +00:00
it('can disables web security and enable nodeintegration', async () => {
2020-03-20 20:28:31 +00:00
const jqueryPath = path.join(__dirname, '/static/jquery-2.0.3.min.js');
const src = `<script src='file://${jqueryPath}'></script> <script>console.log(typeof require);</script>`;
const encoded = btoa(unescape(encodeURIComponent(src)));
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
src: `data:text/html;base64,${encoded}`,
webpreferences: 'webSecurity=no, nodeIntegration=yes, contextIsolation=no'
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(message).to.equal('function');
});
});
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`
2020-03-20 20:28:31 +00:00
});
const { url, frameName } = await waitForEvent(webview, 'new-window');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +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, {
src: `file://${fixtures}/pages/target-name.html`
2020-03-20 20:28:31 +00:00
});
const { url, frameName } = await waitForEvent(webview, 'new-window');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(url).to.equal('http://host/');
expect(frameName).to.equal('target');
});
});
2016-03-25 20:03:49 +00:00
describe('ipc-message event', () => {
it('emits when guest sends an ipc message to browser', async () => {
2018-05-14 22:00:49 +00:00
loadWebView(webview, {
nodeintegration: 'on',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/ipc-message.html`
2020-03-20 20:28:31 +00:00
});
const { channel, args } = await waitForEvent(webview, 'ipc-message');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(channel).to.equal('channel');
expect(args).to.deep.equal(['arg1', 'arg2']);
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
const { title, explicitSet } = await waitForEvent(webview, 'page-title-set');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(title).to.equal('test');
expect(explicitSet).to.be.true();
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
const { favicons } = await waitForEvent(webview, 'page-favicon-updated');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(favicons).to.be.an('array').of.length(2);
2018-05-14 22:00:49 +00:00
if (process.platform === 'win32') {
2020-03-20 20:28:31 +00:00
expect(favicons[0]).to.match(/^file:\/\/\/[A-Z]:\/favicon.png$/i);
2018-05-14 22:00:49 +00:00
} else {
2020-03-20 20:28:31 +00:00
expect(favicons[0]).to.equal('file:///favicon.png');
2018-05-14 22:00:49 +00:00
}
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
const { url } = await waitForEvent(webview, 'will-navigate');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(url).to.equal('http://host/');
});
});
2016-03-25 20:03:49 +00:00
describe('did-navigate event', () => {
2020-03-20 20:28:31 +00:00
let p = path.join(fixtures, 'pages', 'webview-will-navigate.html');
p = p.replace(/\\/g, '/');
const pageUrl = url.format({
2016-01-12 02:40:23 +00:00
protocol: 'file',
slashes: true,
pathname: p
2020-03-20 20:28:31 +00:00
});
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 () => {
2020-03-20 20:28:31 +00:00
loadWebView(webview, { src: pageUrl });
const { url } = await waitForEvent(webview, 'did-navigate');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(url).to.equal(pageUrl);
});
});
2016-03-25 20:03:49 +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 () => {
2020-03-20 20:28:31 +00:00
let p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page.html');
p = p.replace(/\\/g, '/');
const pageUrl = url.format({
2016-01-12 02:40:23 +00:00
protocol: 'file',
slashes: true,
pathname: p
2020-03-20 20:28:31 +00:00
});
loadWebView(webview, { src: pageUrl });
const event = await waitForEvent(webview, 'did-navigate-in-page');
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`
2020-03-20 20:28:31 +00:00
});
const { url } = await waitForEvent(webview, 'did-navigate-in-page');
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 () => {
2020-03-20 20:28:31 +00:00
let p = path.join(fixtures, 'pages', 'webview-did-navigate-in-page-with-hash.html');
p = p.replace(/\\/g, '/');
const pageUrl = url.format({
2016-01-12 02:40:23 +00:00
protocol: 'file',
slashes: true,
pathname: p
2020-03-20 20:28:31 +00:00
});
loadWebView(webview, { src: pageUrl });
const event = await waitForEvent(webview, 'did-navigate-in-page');
expect(event.url).to.equal(`${pageUrl}#test`);
});
});
2016-03-25 20:03:49 +00:00
describe('close event', () => {
2018-05-14 22:00:49 +00:00
it('should fire when interior page calls window.close', async () => {
2020-03-20 20:28:31 +00:00
loadWebView(webview, { src: `file://${fixtures}/pages/close.html` });
await waitForEvent(webview, 'close');
});
});
2016-03-25 20:03:49 +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 () => {
2020-03-20 20:28:31 +00:00
const webview2 = new WebView();
loadWebView(webview2);
2018-05-14 22:00:49 +00:00
// Setup an event handler for further usage.
2020-03-20 20:28:31 +00:00
const waitForDomReady = waitForEvent(webview2, 'dom-ready');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
loadWebView(webview, { src: 'about:blank' });
await waitForEvent(webview, 'dom-ready');
webview.getWebContents().setDevToolsWebContents(webview2.getWebContents());
webview.getWebContents().openDevTools();
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
await waitForDomReady;
2018-05-14 22:00:49 +00:00
// Its WebContents should be a DevTools.
2020-03-20 20:28:31 +00:00
const devtools = webview2.getWebContents();
expect(devtools.getURL().startsWith('devtools://devtools')).to.be.true();
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const name = await devtools.executeJavaScript('InspectorFrontendHost.constructor.name');
document.body.removeChild(webview2);
2020-03-20 20:28:31 +00:00
expect(name).to.be.equal('InspectorFrontendHostImpl');
});
});
2017-11-30 12:04:50 +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`
2020-03-20 20:28:31 +00:00
});
await waitForEvent(webview, 'dom-ready');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.openDevTools();
await waitForEvent(webview, 'devtools-opened');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.closeDevTools();
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
await waitForEvent(webview, 'dom-ready');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.openDevTools();
await waitForEvent(webview, 'devtools-opened');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.closeDevTools();
await waitForEvent(webview, 'devtools-closed');
});
});
2016-03-25 20:03:49 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const waitForDevToolsFocused = waitForEvent(webview, 'devtools-focused');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
await waitForEvent(webview, 'dom-ready');
webview.openDevTools();
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
await waitForDevToolsFocused;
webview.closeDevTools();
});
});
2016-03-25 20:03:49 +00:00
describe('<webview>.reload()', () => {
2018-05-14 22:00:49 +00:00
it('should emit beforeunload handler', async () => {
await loadWebView(webview, {
nodeintegration: 'on',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/beforeunload-false.html`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
// Event handler has to be added before reload.
2020-03-20 20:28:31 +00:00
const waitForOnbeforeunload = waitForEvent(webview, 'ipc-message');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
webview.reload();
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const { channel } = await waitForOnbeforeunload;
expect(channel).to.equal('onbeforeunload');
});
});
2016-03-25 20:03:49 +00:00
describe('<webview>.goForward()', () => {
it('should work after a replaced history entry', (done) => {
2020-03-20 20:28:31 +00:00
let loadCount = 1;
const listener = (e) => {
if (loadCount === 1) {
2020-03-20 20:28:31 +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();
} else if (loadCount === 2) {
2020-03-20 20:28:31 +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();
webview.removeEventListener('ipc-message', listener);
}
2020-03-20 20:28:31 +00:00
};
2018-05-14 22:00:49 +00:00
const loadListener = () => {
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);
}
2020-03-20 20:28:31 +00:00
};
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +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 () => {
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
const event = await message;
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +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
2020-03-20 20:28:31 +00:00
webview.clearHistory();
expect(webview.canGoBack()).to.be.false();
});
});
2016-03-25 20:03:49 +00:00
describe('basic auth', () => {
2020-03-20 20:28:31 +00:00
const auth = require('basic-auth');
2016-03-25 20:03:49 +00:00
it('should authenticate with correct credentials', (done) => {
2020-03-20 20:28:31 +00:00
const message = 'Authenticated';
const server = http.createServer((req, res) => {
2020-03-20 20:28:31 +00:00
const credentials = auth(req);
2016-01-12 02:40:23 +00:00
if (credentials.name === 'test' && credentials.pass === 'test') {
2020-03-20 20:28:31 +00:00
res.end(message);
2016-01-12 02:40:23 +00:00
} else {
2020-03-20 20:28:31 +00:00
res.end('failed');
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
server.close();
});
server.listen(0, '127.0.0.1', () => {
2020-03-20 20:28:31 +00:00
const port = server.address().port;
webview.addEventListener('ipc-message', (e) => {
try {
expect(e.channel).to.equal(message);
done();
} catch (e) {
done(e);
}
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
loadWebView(webview, {
nodeintegration: 'on',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/basic-auth.html?port=${port}`
2020-03-20 20:28:31 +00:00
});
});
});
});
2016-03-25 20:03:49 +00:00
describe('dom-ready event', () => {
it('emits when document is loaded', (done) => {
2020-03-20 20:28:31 +00:00
const server = http.createServer(() => {});
server.listen(0, '127.0.0.1', () => {
2020-03-20 20:28:31 +00:00
const port = server.address().port;
webview.addEventListener('dom-ready', () => {
2020-03-20 20:28:31 +00:00
done();
});
2018-05-14 22:00:49 +00:00
loadWebView(webview, {
src: `file://${fixtures}/pages/dom-ready.html?port=${port}`
2020-03-20 20:28:31 +00:00
});
});
});
2016-03-25 20:03:49 +00:00
it('throws a custom error when an API method is called before the event is emitted', () => {
const expectedErrorMessage =
'The WebView must be attached to the DOM ' +
2020-03-20 20:28:31 +00:00
'and the dom-ready event emitted before this method can be called.';
expect(() => { webview.stop(); }).to.throw(expectedErrorMessage);
});
});
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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
// Event handler has to be added before js execution.
2020-03-20 20:28:31 +00:00
const waitForEnterHtmlFullScreen = waitForEvent(webview, 'enter-html-full-screen');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const jsScript = "document.querySelector('video').webkitRequestFullscreen()";
webview.executeJavaScript(jsScript, true);
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
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'
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const jsScript = "'4'+2";
const expectedResult = '42';
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const result = await webview.executeJavaScript(jsScript);
expect(result).to.equal(expectedResult);
});
});
2016-03-25 20:03:49 +00:00
2019-06-17 15:39:36 +00:00
it('supports inserting CSS', async () => {
2020-03-20 20:28:31 +00:00
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');
});
2019-06-17 15:39:36 +00:00
it('supports removing inserted CSS', async () => {
2020-03-20 20:28:31 +00:00
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');
});
2019-06-17 15:39:36 +00:00
describe('sendInputEvent', () => {
2018-05-14 22:00:49 +00:00
it('can send keyboard event', async () => {
loadWebView(webview, {
nodeintegration: 'on',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/onkeyup.html`
2020-03-20 20:28:31 +00:00
});
await waitForEvent(webview, 'dom-ready');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const waitForIpcMessage = waitForEvent(webview, 'ipc-message');
2018-05-14 22:00:49 +00:00
webview.sendInputEvent({
type: 'keyup',
keyCode: 'c',
modifiers: ['shift']
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const { channel, args } = await waitForIpcMessage;
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',
webpreferences: 'contextIsolation=no',
2018-05-14 22:00:49 +00:00
src: `file://${fixtures}/pages/onmouseup.html`
2020-03-20 20:28:31 +00:00
});
await waitForEvent(webview, 'dom-ready');
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const waitForIpcMessage = waitForEvent(webview, 'ipc-message');
2018-05-14 22:00:49 +00:00
webview.sendInputEvent({
type: 'mouseup',
modifiers: ['ctrl'],
x: 10,
y: 20
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const { channel, args } = await waitForIpcMessage;
expect(channel).to.equal('mouseup');
expect(args).to.deep.equal([10, 20, false, true]);
});
});
2016-03-25 20:03:49 +00:00
describe('media-started-playing media-paused events', () => {
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 () => {
2020-03-20 20:28:31 +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()
2020-03-20 20:28:31 +00:00
`;
webview.executeJavaScript(source, true);
await waitForEvent(webview, 'media-started-playing');
2020-03-20 20:28:31 +00:00
webview.executeJavaScript('document.querySelector("audio").pause()', true);
await waitForEvent(webview, 'media-paused');
});
});
2016-03-25 20:03:49 +00:00
describe('found-in-page event', () => {
it('emits when a request is made', async () => {
const didFinishLoad = waitForEvent(webview, 'did-finish-load');
2020-03-20 20:28:31 +00:00
loadWebView(webview, { src: `file://${fixtures}/pages/content.html` });
// 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.
2020-03-20 20:28:31 +00:00
webview.focus();
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');
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +00:00
describe('<webview>.getWebContentsId', () => {
it('can return the WebContents ID', async () => {
2020-03-20 20:28:31 +00:00
const src = 'about:blank';
await loadWebView(webview, { src });
2020-03-20 20:28:31 +00:00
expect(webview.getWebContentsId()).to.be.a('number');
});
});
describe('<webview>.capturePage()', () => {
before(function () {
// TODO(miniak): figure out why this is failing on windows
if (process.platform === 'win32') {
2020-03-20 20:28:31 +00:00
this.skip();
}
2020-03-20 20:28:31 +00:00
});
it('returns a Promise with a NativeImage', async () => {
2020-03-20 20:28:31 +00:00
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
await loadWebView(webview, { src });
2020-03-20 20:28:31 +00:00
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
2020-03-20 20:28:31 +00:00
expect(imgBuffer[25]).to.equal(6);
});
});
ifdescribe(features.isPrintingEnabled())('<webview>.printToPDF()', () => {
it('rejects on incorrectly typed parameters', async () => {
const badTypes = {
marginsType: 'terrible',
scaleFactor: 'not-a-number',
landscape: [],
pageRanges: { oops: 'im-not-the-right-key' },
headerFooter: '123',
printSelectionOnly: 1,
printBackground: 2,
pageSize: 'IAmAPageSize'
2020-03-20 20:28:31 +00:00
};
// These will hard crash in Chromium unless we type-check
for (const [key, value] of Object.entries(badTypes)) {
2020-03-20 20:28:31 +00:00
const param = { [key]: value };
2020-03-20 20:28:31 +00:00
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();
}
2020-03-20 20:28:31 +00:00
});
it('can print to PDF', async () => {
2020-03-20 20:28:31 +00:00
const src = 'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E';
await loadWebView(webview, { src });
2020-03-20 20:28:31 +00:00
const data = await webview.printToPDF({});
expect(data).to.be.an.instanceof(Uint8Array).that.is.not.empty();
});
});
2017-02-09 19:49:14 +00:00
describe('will-attach-webview event', () => {
it('does not emit when src is not changed', async () => {
console.log('loadWebView(webview)');
2020-03-20 20:28:31 +00:00
loadWebView(webview);
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);
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
it('supports changing the web preferences', async () => {
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
const types = JSON.parse(message);
expect(types).to.include({
require: 'undefined',
module: 'undefined',
process: 'undefined',
global: 'undefined'
2020-03-20 20:28:31 +00:00
});
});
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 () => {
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
await waitForEvent(webview, 'destroyed');
});
2018-05-14 22:00:49 +00:00
it('supports removing the preload script', async () => {
2020-03-20 20:28:31 +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`
2020-03-20 20:28:31 +00:00
});
2018-05-14 22:00:49 +00:00
2020-03-20 20:28:31 +00:00
expect(message).to.equal('undefined');
});
});
2017-02-03 20:21:46 +00:00
describe('DOM events', () => {
2020-03-20 20:28:31 +00:00
let div;
2016-11-03 22:12:54 +00:00
beforeEach(() => {
2020-03-20 20:28:31 +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%';
});
2016-11-03 22:12:54 +00:00
afterEach(() => {
2020-03-20 20:28:31 +00:00
if (div != null) div.remove();
});
2016-11-03 22:12:54 +00:00
const generateSpecs = (description, sandbox) => {
describe(description, () => {
chore: bump chromium to f1d9522c04ca8fa0a906f88ababe9 (master) (#18648) * chore: bump chromium in DEPS to 675d7dc9f3334b15c3ec28c27db3dc19b26bd12e * chore: update patches * chore: bump chromium in DEPS to dce3562696f165a324273fcb6893f0e1fef42ab1 * chore: const interfaces are being removed from //content Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1631749 Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=908139 * chore: update patches * chore: blink::MediaStreamType is now consistent and deduplicated * chore: update patches and printing code for ref -> uniq * chore: bridge_impl() --> GetInProcessNSWindowBridge Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1642988 * fixme: TotalMarkedObjectSize has been removed * chore: fix linting * chore: bump chromium in DEPS to 9503e1a2fcbf17db08094d8caae3e1407e918af3 * chore: fix slightly broken printing patch * chore: update patches for SiteInstanceImpl changes Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1612025 * chore: update patches for SiteInstanceImpl changes * chore: bump chromium in DEPS to 6801e6c1ddd1b7b73e594e97157ddd539ca335d7 * chore: update patches * chore: bump chromium in DEPS to 27e198912d7c1767052ec785c22e2e88b2cb4d8b * chore: remove system_request_context Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1647172 * chore: creation of FtpProtocolHandler needs an auth cache Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1639683 * fixme: disable marked spec * chore: bump chromium in DEPS to 3dcd7fe453ad13a22b114b95f05590eba74c5471 * chore: bump chromium in DEPS to bdc24128b75008743d819e298557a53205706e7c * chore: bump chromium in DEPS to 7da330b58fbe0ba94b9b94abbb8085bead220228 * update patches * remove TotalMarkedObjectSize https://chromium-review.googlesource.com/c/chromium/src/+/1631708 * add libvulkan.so to dist zip manifest on linux * chore: bump chromium in DEPS to 1e85d0f45b52649efd0010cc9dab6d2804f24443 * update patches * add angle features to gpuinfo https://chromium-review.googlesource.com/c/chromium/src/+/1638658 * mark 'marked' property as deprecated * disable webview resize test * FIXME: disable vulkan on 32-bit arm * chore: bump chromium in DEPS to cd0297c6a83fdd2b1f6bc312e7d5acca736a3c56 * Revert "FIXME: disable vulkan on 32-bit arm" This reverts commit 5c1e0ef302a6db1e72231d4e823f91bb08e281af. * backport from upstream: fix swiftshader build on arm https://swiftshader-review.googlesource.com/c/SwiftShader/+/32768/ * update patches * viz: update OutputDeviceWin to new shared memory api https://chromium-review.googlesource.com/c/chromium/src/+/1649574 * base::Contains{Key,Value} => base::Contains https://chromium-review.googlesource.com/c/chromium/src/+/1649478 * fixup! viz: update OutputDeviceWin to new shared memory api * stub out StatusIconLinuxDbus-related delegate methods https://chromium-review.googlesource.com/c/chromium/src/+/1638180 * chore: bump chromium in DEPS to 964ea3fd4bdc006d62533f5755043076220181f1 * Remove the BrowserContext methods to create URLRequestContexts for main/media partitions when a partition_domain is specified https://chromium-review.googlesource.com/c/chromium/src/+/1655087 * fixup! stub out StatusIconLinuxDbus-related delegate methods * add remote_cocoa to chromium_src deps https://chromium-review.googlesource.com/c/chromium/src/+/1657068 * fixup! stub out StatusIconLinuxDbus-related delegate methods * attempt at fix linux-debug build * add swiftshader/libvulkan.so to arm manifest * chore: bump chromium in DEPS to 28688f76afef27c36631aa274691e333ddecdc22 * update patches * chore: bump chromium in DEPS to fe7450e1578a9584189f87d59d0d1a8548bf6b90 * chore: bump chromium in DEPS to f304dfd682dc86a755a6c49a16ee6876e0db45fb * chore: bump chromium in DEPS to f0fd4d6c365aad9edd83bdfff9954c47d271b75c * Update patches * Remove no longer needed WOA patch * Put back IOThread in BrowserProcess We need this until we enable the network service. * move atom.ico to inputs * Update to latest LKGR to fix no template named 'bitset' in namespace 'std' * fixup! Put back IOThread in BrowserProcess * chore: bump chromium in DEPS to dcf9662dc9a896a175d791001350324167b1cad3 * Update patches content_allow_embedder_to_prevent_locking_scheme_registry.patch is no longer necessary as it was upstreamed via https://chromium-review.googlesource.com/c/chromium/src/+/1637040 * Fix renamed enum * Use newer docker container Contains updated dependencies * Try to track down arm test failures * Fix arm tests * chore: bump chromium in DEPS to 8cbceef57b37ee14b9c4c3405a3f7663922c5b5d * Update patches * Add needed dependencies for testing 32-bit linux * Remove arm debugging. * Remove additional debugging * Fix compiler errors * Handle new macOS helper * Fix compile error on Linux * chore: bump chromium in DEPS to 66a93991ddaff6a9f1b13d110959947cb03a1860 * Add new helper files to manifests * fix BUILD.gn for macOS * Fix compile errors * Add patch to put back colors needed for autofill/datalist * chore: bump chromium in DEPS to e89617079f11e33f33cdb3924f719a579c73704b * Updated patches * Remove no longer needed patch * Remove no longer needed patch * Fix compile error with patch * Really fix the patch * chore: bump chromium in DEPS to c70f12476a45840408f1d5ff5968e7f7ceaad9d4 * chore: bump chromium in DEPS to 06d2dd7a8933b41545a7c26349c802f570563fd5 * chore: bump chromium in DEPS to b0b9ff8f727deb519ccbec7cf1c8d9ed543d88ab * Update patches * Fix compiler errors * Fix removed ChromeNetLog * Revert "Fix removed ChromeNetLog" This reverts commit 426dfd90b5ab0a9c1df415d71c88e8aed2bd5bbe. * Remove ChromeNetLog. https://chromium-review.googlesource.com/c/chromium/src/+/1663846 * chore: bump chromium in DEPS to fefcc4926d58dccd59ac95be65eab3a4ebfe2f29 * Update patches * Update v8 patches * Fix lint error * Fix compile errors * chore: bump chromium in DEPS to 4de815ef92ef2eef515506fe09bdc466526a8fd9 * Use custom protocol to test baseURLForDataURL * Use newer SDK (10.0.18362) for Windows * Update patches * Update arm manifest since swiftshader reenabled. * Don't delete dir that isn't ever there. * Fix compile errors. * Need src dir created * Update for removed InspectorFrontendAPI.addExtensions * Revert "Use newer SDK (10.0.18362) for Windows" This reverts commit 68763a0c88cdc44b971462e49662aecc167d3d99. * Revert "Need src dir created" This reverts commit 7daedc29d0844316d4097648dde7f40f1a3848fb. * Revert "Don't delete dir that isn't ever there." This reverts commit bf424bc30ffcb23b1d9a634d4df410342536640e. * chore: bump chromium in DEPS to 97dab6b0124ea53244caf123921b5d14893bcca7 * chore: bump chromium in DEPS to c87d16d49a85dc7122781f6c979d354c20f7f78b * chore: bump chromium in DEPS to 004bcee2ea336687cedfda8f8a151806ac757d15 * chore: bump chromium in DEPS to 24428b26a9d15a013b2a253e1084ec3cb54b660b * chore: bump chromium in DEPS to fd25914e875237df88035a6abf89a70bf1360b57 * Update patches * Update node to fix build error * Fix compile errors * chore: bump chromium in DEPS to 3062b7cf090f1d9522c04ca8fa0a906f88ababe9 * chore: update node ref for pushed tags * chore: update patches for new chromium * chore: fix printing patches * Use new (10.0.18362) Windows SDK * roll node to fix v8 build issues in debug build * Add support for plugin helper * fix: add patch to fix gpu info enumeration Can be removed once CL lands upstream. Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1685993 * spec: navigator.requestMIDIAccess now requires a secure origin This test requires a secure origin so we fake one. Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1657952 * FIXME: temporarily disable SharedWorker tests * use released version of node-abstractsocket * fix abstract-socket
2019-07-03 01:22:09 +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 () => {
2020-03-20 20:28:31 +00:00
const firstResizeSignal = waitForEvent(webview, 'resize');
const domReadySignal = waitForEvent(webview, 'dom-ready');
2020-03-20 20:28:31 +00:00
webview.src = `file://${fixtures}/pages/a.html`;
webview.webpreferences = `sandbox=${sandbox ? 'yes' : 'no'}`;
div.appendChild(webview);
document.body.appendChild(div);
2020-03-20 20:28:31 +00:00
const firstResizeEvent = await firstResizeSignal;
expect(firstResizeEvent.target).to.equal(webview);
expect(firstResizeEvent.newWidth).to.equal(100);
expect(firstResizeEvent.newHeight).to.equal(10);
2020-03-20 20:28:31 +00:00
await domReadySignal;
2020-03-20 20:28:31 +00:00
const secondResizeSignal = waitForEvent(webview, 'resize');
2020-03-20 20:28:31 +00:00
const newWidth = 1234;
const newHeight = 789;
div.style.width = `${newWidth}px`;
div.style.height = `${newHeight}px`;
2020-03-20 20:28:31 +00:00
const secondResizeEvent = await secondResizeSignal;
expect(secondResizeEvent.target).to.equal(webview);
expect(secondResizeEvent.newWidth).to.equal(newWidth);
expect(secondResizeEvent.newHeight).to.equal(newHeight);
});
it('emits focus event', async () => {
2020-03-20 20:28:31 +00:00
const domReadySignal = waitForEvent(webview, 'dom-ready');
webview.src = `file://${fixtures}/pages/a.html`;
webview.webpreferences = `sandbox=${sandbox ? 'yes' : 'no'}`;
document.body.appendChild(webview);
2020-03-20 20:28:31 +00:00
await domReadySignal;
// If this test fails, check if webview.focus() still works.
2020-03-20 20:28:31 +00:00
const focusSignal = waitForEvent(webview, 'focus');
webview.focus();
await focusSignal;
});
});
};
generateSpecs('without sandbox', false);
generateSpecs('with sandbox', true);
});
});