2019-11-07 00:15:55 +00:00
|
|
|
import { expect } from 'chai';
|
2019-07-26 14:09:33 +00:00
|
|
|
import { AddressInfo } from 'net';
|
2019-04-02 01:28:11 +00:00
|
|
|
import * as path from 'path';
|
2019-09-30 22:00:47 +00:00
|
|
|
import * as fs from 'fs';
|
2019-07-26 14:09:33 +00:00
|
|
|
import * as http from 'http';
|
2021-04-07 07:16:10 +00:00
|
|
|
import { BrowserWindow, ipcMain, webContents, session, WebContents, app, BrowserView } from 'electron/main';
|
2023-01-25 21:01:25 +00:00
|
|
|
import { emittedOnce } from './lib/events-helpers';
|
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
|
|
|
import { ifdescribe, delay, defer, waitUntil } from './lib/spec-helpers';
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2020-01-30 20:12:35 +00:00
|
|
|
const pdfjs = require('pdfjs-dist');
|
2022-08-16 19:23:13 +00:00
|
|
|
const fixturesPath = path.resolve(__dirname, 'fixtures');
|
2020-08-24 16:58:55 +00:00
|
|
|
const mainFixturesPath = path.resolve(__dirname, 'fixtures');
|
2020-06-23 03:32:45 +00:00
|
|
|
const features = process._linkedBinding('electron_common_features');
|
2019-04-02 01:28:11 +00:00
|
|
|
|
|
|
|
describe('webContents module', () => {
|
|
|
|
describe('getAllWebContents() API', () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
afterEach(closeAllWindows);
|
2019-04-02 01:28:11 +00:00
|
|
|
it('returns an array of web contents', async () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: { webviewTag: true }
|
|
|
|
});
|
2019-04-02 01:28:11 +00:00
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'webview-zoom-factor.html'));
|
|
|
|
|
|
|
|
await emittedOnce(w.webContents, 'did-attach-webview');
|
|
|
|
|
|
|
|
w.webContents.openDevTools();
|
|
|
|
|
|
|
|
await emittedOnce(w.webContents, 'devtools-opened');
|
|
|
|
|
|
|
|
const all = webContents.getAllWebContents().sort((a, b) => {
|
|
|
|
return a.id - b.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(all).to.have.length(3);
|
|
|
|
expect(all[0].getType()).to.equal('window');
|
|
|
|
expect(all[all.length - 2].getType()).to.equal('webview');
|
|
|
|
expect(all[all.length - 1].getType()).to.equal('remote');
|
|
|
|
});
|
|
|
|
});
|
2019-05-29 20:38:14 +00:00
|
|
|
|
2020-11-23 17:21:00 +00:00
|
|
|
describe('fromId()', () => {
|
|
|
|
it('returns undefined for an unknown id', () => {
|
|
|
|
expect(webContents.fromId(12345)).to.be.undefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-26 16:37:08 +00:00
|
|
|
describe('fromFrame()', () => {
|
|
|
|
it('returns WebContents for mainFrame', () => {
|
|
|
|
const contents = (webContents as any).create() as WebContents;
|
|
|
|
expect(webContents.fromFrame(contents.mainFrame)).to.equal(contents);
|
|
|
|
});
|
|
|
|
it('returns undefined for disposed frame', async () => {
|
|
|
|
const contents = (webContents as any).create() as WebContents;
|
|
|
|
const { mainFrame } = contents;
|
|
|
|
contents.destroy();
|
|
|
|
await waitUntil(() => typeof webContents.fromFrame(mainFrame) === 'undefined');
|
|
|
|
});
|
|
|
|
it('throws when passing invalid argument', async () => {
|
|
|
|
let errored = false;
|
|
|
|
try {
|
|
|
|
webContents.fromFrame({} as any);
|
|
|
|
} catch {
|
|
|
|
errored = true;
|
|
|
|
}
|
|
|
|
expect(errored).to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-27 21:01:24 +00:00
|
|
|
describe('fromDevToolsTargetId()', () => {
|
|
|
|
it('returns WebContents for attached DevTools target', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
try {
|
|
|
|
await w.webContents.debugger.attach('1.3');
|
|
|
|
const { targetInfo } = await w.webContents.debugger.sendCommand('Target.getTargetInfo');
|
|
|
|
expect(webContents.fromDevToolsTargetId(targetInfo.targetId)).to.equal(w.webContents);
|
|
|
|
} finally {
|
|
|
|
await w.webContents.debugger.detach();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns undefined for an unknown id', () => {
|
|
|
|
expect(webContents.fromDevToolsTargetId('nope')).to.be.undefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-18 15:04:41 +00:00
|
|
|
describe('will-prevent-unload event', function () {
|
2019-07-26 14:09:33 +00:00
|
|
|
afterEach(closeAllWindows);
|
2021-04-07 07:16:10 +00:00
|
|
|
it('does not emit if beforeunload returns undefined in a BrowserWindow', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.webContents.once('will-prevent-unload', () => {
|
2019-05-29 20:38:14 +00:00
|
|
|
expect.fail('should not have fired');
|
|
|
|
});
|
2020-06-02 02:32:39 +00:00
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-undefined.html'));
|
2020-05-26 13:21:38 +00:00
|
|
|
const wait = emittedOnce(w, 'closed');
|
|
|
|
w.close();
|
|
|
|
await wait;
|
2019-05-29 20:38:14 +00:00
|
|
|
});
|
|
|
|
|
2021-04-07 07:16:10 +00:00
|
|
|
it('does not emit if beforeunload returns undefined in a BrowserView', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const view = new BrowserView();
|
|
|
|
w.setBrowserView(view);
|
|
|
|
view.setBounds(w.getBounds());
|
|
|
|
|
|
|
|
view.webContents.once('will-prevent-unload', () => {
|
|
|
|
expect.fail('should not have fired');
|
|
|
|
});
|
|
|
|
|
|
|
|
await view.webContents.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-undefined.html'));
|
|
|
|
const wait = emittedOnce(w, 'closed');
|
|
|
|
w.close();
|
|
|
|
await wait;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits if beforeunload returns false in a BrowserWindow', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
2020-06-02 02:32:39 +00:00
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
2020-05-26 13:21:38 +00:00
|
|
|
w.close();
|
2019-11-14 22:09:03 +00:00
|
|
|
await emittedOnce(w.webContents, 'will-prevent-unload');
|
2019-05-29 20:38:14 +00:00
|
|
|
});
|
|
|
|
|
2021-04-07 07:16:10 +00:00
|
|
|
it('emits if beforeunload returns false in a BrowserView', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const view = new BrowserView();
|
|
|
|
w.setBrowserView(view);
|
|
|
|
view.setBounds(w.getBounds());
|
|
|
|
|
|
|
|
await view.webContents.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
|
|
|
w.close();
|
|
|
|
await emittedOnce(view.webContents, 'will-prevent-unload');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports calling preventDefault on will-prevent-unload events in a BrowserWindow', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
2019-05-29 20:38:14 +00:00
|
|
|
w.webContents.once('will-prevent-unload', event => event.preventDefault());
|
2020-06-02 02:32:39 +00:00
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
2020-05-26 13:21:38 +00:00
|
|
|
const wait = emittedOnce(w, 'closed');
|
|
|
|
w.close();
|
|
|
|
await wait;
|
2019-05-29 20:38:14 +00:00
|
|
|
});
|
|
|
|
});
|
2019-07-26 14:09:33 +00:00
|
|
|
|
|
|
|
describe('webContents.send(channel, args...)', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('throws an error when the channel is missing', () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
2019-07-26 14:09:33 +00:00
|
|
|
expect(() => {
|
|
|
|
(w.webContents.send as any)();
|
|
|
|
}).to.throw('Missing required channel argument');
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.send(null as any);
|
|
|
|
}).to.throw('Missing required channel argument');
|
|
|
|
});
|
2019-08-12 17:38:41 +00:00
|
|
|
|
|
|
|
it('does not block node async APIs when sent before document is ready', (done) => {
|
|
|
|
// Please reference https://github.com/electron/electron/issues/19368 if
|
|
|
|
// this test fails.
|
|
|
|
ipcMain.once('async-node-api-done', () => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
sandbox: false,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'send-after-node.html'));
|
|
|
|
setTimeout(() => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w.webContents.send('test');
|
2019-08-12 17:38:41 +00:00
|
|
|
}, 50);
|
|
|
|
});
|
2019-07-26 14:09:33 +00:00
|
|
|
});
|
|
|
|
|
2019-10-14 20:49:21 +00:00
|
|
|
ifdescribe(features.isPrintingEnabled())('webContents.print()', () => {
|
2020-01-31 02:49:13 +00:00
|
|
|
let w: BrowserWindow;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
w = new BrowserWindow({ show: false });
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
afterEach(closeAllWindows);
|
2020-01-31 02:49:13 +00:00
|
|
|
|
2019-08-13 06:44:14 +00:00
|
|
|
it('throws when invalid settings are passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
// @ts-ignore this line is intentionally incorrect
|
|
|
|
w.webContents.print(true);
|
|
|
|
}).to.throw('webContents.print(): Invalid print settings specified.');
|
2020-01-31 02:49:13 +00:00
|
|
|
});
|
2019-08-13 06:44:14 +00:00
|
|
|
|
2020-01-31 02:49:13 +00:00
|
|
|
it('throws when an invalid callback is passed', () => {
|
2019-08-13 06:44:14 +00:00
|
|
|
expect(() => {
|
|
|
|
// @ts-ignore this line is intentionally incorrect
|
|
|
|
w.webContents.print({}, true);
|
|
|
|
}).to.throw('webContents.print(): Invalid optional callback provided.');
|
|
|
|
});
|
|
|
|
|
2022-07-13 10:23:24 +00:00
|
|
|
it('fails when an invalid deviceName is passed', (done) => {
|
|
|
|
w.webContents.print({ deviceName: 'i-am-a-nonexistent-printer' }, (success, reason) => {
|
|
|
|
expect(success).to.equal(false);
|
|
|
|
expect(reason).to.match(/Invalid deviceName provided/);
|
|
|
|
done();
|
|
|
|
});
|
2020-01-31 02:49:13 +00:00
|
|
|
});
|
|
|
|
|
2020-02-05 04:25:02 +00:00
|
|
|
it('throws when an invalid pageSize is passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
// @ts-ignore this line is intentionally incorrect
|
|
|
|
w.webContents.print({ pageSize: 'i-am-a-bad-pagesize' }, () => {});
|
|
|
|
}).to.throw('Unsupported pageSize: i-am-a-bad-pagesize');
|
|
|
|
});
|
|
|
|
|
2020-07-10 16:42:22 +00:00
|
|
|
it('throws when an invalid custom pageSize is passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.print({
|
|
|
|
pageSize: {
|
|
|
|
width: 100,
|
|
|
|
height: 200
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).to.throw('height and width properties must be minimum 352 microns.');
|
|
|
|
});
|
|
|
|
|
2020-02-13 17:15:25 +00:00
|
|
|
it('does not crash with custom margins', () => {
|
2019-08-13 06:44:14 +00:00
|
|
|
expect(() => {
|
2020-02-13 17:15:25 +00:00
|
|
|
w.webContents.print({
|
|
|
|
silent: true,
|
|
|
|
margins: {
|
|
|
|
marginType: 'custom',
|
|
|
|
top: 1,
|
|
|
|
bottom: 1,
|
|
|
|
left: 1,
|
|
|
|
right: 1
|
|
|
|
}
|
|
|
|
});
|
2019-08-13 06:44:14 +00:00
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-26 14:09:33 +00:00
|
|
|
describe('webContents.executeJavaScript', () => {
|
|
|
|
describe('in about:blank', () => {
|
|
|
|
const expected = 'hello, world!';
|
|
|
|
const expectedErrorMsg = 'woops!';
|
|
|
|
const code = `(() => "${expected}")()`;
|
|
|
|
const asyncCode = `(() => new Promise(r => setTimeout(() => r("${expected}"), 500)))()`;
|
|
|
|
const badAsyncCode = `(() => new Promise((r, e) => setTimeout(() => e("${expectedErrorMsg}"), 500)))()`;
|
|
|
|
const errorTypes = new Set([
|
|
|
|
Error,
|
|
|
|
ReferenceError,
|
|
|
|
EvalError,
|
|
|
|
RangeError,
|
|
|
|
SyntaxError,
|
|
|
|
TypeError,
|
|
|
|
URIError
|
|
|
|
]);
|
|
|
|
let w: BrowserWindow;
|
|
|
|
|
|
|
|
before(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: false } });
|
2019-07-26 14:09:33 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
after(closeAllWindows);
|
|
|
|
|
|
|
|
it('resolves the returned promise with the result', async () => {
|
|
|
|
const result = await w.webContents.executeJavaScript(code);
|
|
|
|
expect(result).to.equal(expected);
|
|
|
|
});
|
2022-06-16 07:46:11 +00:00
|
|
|
it('resolves the returned promise with the result if the code returns an asynchronous promise', async () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
const result = await w.webContents.executeJavaScript(asyncCode);
|
|
|
|
expect(result).to.equal(expected);
|
|
|
|
});
|
|
|
|
it('rejects the returned promise if an async error is thrown', async () => {
|
|
|
|
await expect(w.webContents.executeJavaScript(badAsyncCode)).to.eventually.be.rejectedWith(expectedErrorMsg);
|
|
|
|
});
|
|
|
|
it('rejects the returned promise with an error if an Error.prototype is thrown', async () => {
|
|
|
|
for (const error of errorTypes) {
|
|
|
|
await expect(w.webContents.executeJavaScript(`Promise.reject(new ${error.name}("Wamp-wamp"))`))
|
|
|
|
.to.eventually.be.rejectedWith(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
describe('on a real page', () => {
|
2019-07-26 14:09:33 +00:00
|
|
|
let w: BrowserWindow;
|
|
|
|
beforeEach(() => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w = new BrowserWindow({ show: false });
|
2019-07-26 14:09:33 +00:00
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
let server: http.Server = null as unknown as http.Server;
|
|
|
|
let serverUrl: string = null as unknown as string;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
response.end();
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
serverUrl = 'http://127.0.0.1:' + (server.address() as AddressInfo).port;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('works after page load and during subframe load', async () => {
|
|
|
|
await w.loadURL(serverUrl);
|
|
|
|
// initiate a sub-frame load, then try and execute script during it
|
|
|
|
await w.webContents.executeJavaScript(`
|
|
|
|
var iframe = document.createElement('iframe')
|
|
|
|
iframe.src = '${serverUrl}/slow'
|
|
|
|
document.body.appendChild(iframe)
|
|
|
|
null // don't return the iframe
|
|
|
|
`);
|
|
|
|
await w.webContents.executeJavaScript('console.log(\'hello\')');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('executes after page load', async () => {
|
|
|
|
const executeJavaScript = w.webContents.executeJavaScript('(() => "test")()');
|
2019-07-26 14:09:33 +00:00
|
|
|
w.loadURL(serverUrl);
|
2020-06-30 22:10:36 +00:00
|
|
|
const result = await executeJavaScript;
|
|
|
|
expect(result).to.equal('test');
|
2019-07-26 14:09:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-08-30 00:45:41 +00:00
|
|
|
|
2019-11-22 23:33:55 +00:00
|
|
|
describe('webContents.executeJavaScriptInIsolatedWorld', () => {
|
|
|
|
let w: BrowserWindow;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves the returned promise with the result', async () => {
|
|
|
|
await w.webContents.executeJavaScriptInIsolatedWorld(999, [{ code: 'window.X = 123' }]);
|
|
|
|
const isolatedResult = await w.webContents.executeJavaScriptInIsolatedWorld(999, [{ code: 'window.X' }]);
|
|
|
|
const mainWorldResult = await w.webContents.executeJavaScript('window.X');
|
|
|
|
expect(isolatedResult).to.equal(123);
|
|
|
|
expect(mainWorldResult).to.equal(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
describe('loadURL() promise API', () => {
|
|
|
|
let w: BrowserWindow;
|
2022-08-29 12:53:24 +00:00
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
beforeEach(async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w = new BrowserWindow({ show: false });
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('resolves when done loading', async () => {
|
|
|
|
await expect(w.loadURL('about:blank')).to.eventually.be.fulfilled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resolves when done loading a file URL', async () => {
|
|
|
|
await expect(w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'))).to.eventually.be.fulfilled();
|
|
|
|
});
|
|
|
|
|
2022-10-26 20:57:39 +00:00
|
|
|
it('resolves when navigating within the page', async () => {
|
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
await new Promise(resolve => setTimeout(resolve));
|
|
|
|
await expect(w.loadURL(w.getURL() + '#foo')).to.eventually.be.fulfilled();
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
it('rejects when failing to load a file URL', async () => {
|
|
|
|
await expect(w.loadURL('file:non-existent')).to.eventually.be.rejected()
|
|
|
|
.and.have.property('code', 'ERR_FILE_NOT_FOUND');
|
|
|
|
});
|
|
|
|
|
2019-09-03 17:13:06 +00:00
|
|
|
// Temporarily disable on WOA until
|
|
|
|
// https://github.com/electron/electron/issues/20008 is resolved
|
|
|
|
const testFn = (process.platform === 'win32' && process.arch === 'arm64' ? it.skip : it);
|
|
|
|
testFn('rejects when loading fails due to DNS not resolved', async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
await expect(w.loadURL('https://err.name.not.resolved')).to.eventually.be.rejected()
|
|
|
|
.and.have.property('code', 'ERR_NAME_NOT_RESOLVED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects when navigation is cancelled due to a bad scheme', async () => {
|
|
|
|
await expect(w.loadURL('bad-scheme://foo')).to.eventually.be.rejected()
|
|
|
|
.and.have.property('code', 'ERR_FAILED');
|
|
|
|
});
|
|
|
|
|
2022-08-29 12:53:24 +00:00
|
|
|
it('does not crash when loading a new URL with emulation settings set', async () => {
|
|
|
|
const setEmulation = async () => {
|
|
|
|
if (w.webContents) {
|
|
|
|
w.webContents.debugger.attach('1.3');
|
|
|
|
|
|
|
|
const deviceMetrics = {
|
|
|
|
width: 700,
|
|
|
|
height: 600,
|
|
|
|
deviceScaleFactor: 2,
|
|
|
|
mobile: true,
|
|
|
|
dontSetVisibleSize: true
|
|
|
|
};
|
|
|
|
await w.webContents.debugger.sendCommand(
|
|
|
|
'Emulation.setDeviceMetricsOverride',
|
|
|
|
deviceMetrics
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await w.loadURL(`file://${fixturesPath}/pages/blank.html`);
|
|
|
|
await setEmulation();
|
|
|
|
await w.loadURL('data:text/html,<h1>HELLO</h1>');
|
|
|
|
await setEmulation();
|
|
|
|
} catch (e) {
|
|
|
|
expect((e as Error).message).to.match(/Debugger is already attached to the target/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
it('sets appropriate error information on rejection', async () => {
|
2022-01-31 11:37:40 +00:00
|
|
|
let err: any;
|
2019-08-30 00:45:41 +00:00
|
|
|
try {
|
|
|
|
await w.loadURL('file:non-existent');
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
expect(err).not.to.be.null();
|
|
|
|
expect(err.code).to.eql('ERR_FILE_NOT_FOUND');
|
|
|
|
expect(err.errno).to.eql(-6);
|
|
|
|
expect(err.url).to.eql(process.platform === 'win32' ? 'file://non-existent/' : 'file:///non-existent');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects if the load is aborted', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const s = http.createServer(() => { /* never complete the request */ });
|
2021-01-22 19:25:47 +00:00
|
|
|
await new Promise<void>(resolve => s.listen(0, '127.0.0.1', resolve));
|
2019-08-30 00:45:41 +00:00
|
|
|
const { port } = s.address() as AddressInfo;
|
|
|
|
const p = expect(w.loadURL(`http://127.0.0.1:${port}`)).to.eventually.be.rejectedWith(Error, /ERR_ABORTED/);
|
|
|
|
// load a different file before the first load completes, causing the
|
|
|
|
// first load to be aborted.
|
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
await p;
|
|
|
|
s.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't reject when a subframe fails to load", async () => {
|
|
|
|
let resp = null as unknown as http.ServerResponse;
|
|
|
|
const s = http.createServer((req, res) => {
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
|
|
res.write('<iframe src="http://err.name.not.resolved"></iframe>');
|
|
|
|
resp = res;
|
|
|
|
// don't end the response yet
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
await new Promise<void>(resolve => s.listen(0, '127.0.0.1', resolve));
|
2019-08-30 00:45:41 +00:00
|
|
|
const { port } = s.address() as AddressInfo;
|
2021-01-22 19:25:47 +00:00
|
|
|
const p = new Promise<void>(resolve => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL, isMainFrame) => {
|
2019-08-30 00:45:41 +00:00
|
|
|
if (!isMainFrame) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const main = w.loadURL(`http://127.0.0.1:${port}`);
|
|
|
|
await p;
|
|
|
|
resp.end();
|
|
|
|
await main;
|
|
|
|
s.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't resolve when a subframe loads", async () => {
|
|
|
|
let resp = null as unknown as http.ServerResponse;
|
|
|
|
const s = http.createServer((req, res) => {
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
2020-09-21 08:00:36 +00:00
|
|
|
res.write('<iframe src="about:blank"></iframe>');
|
2019-08-30 00:45:41 +00:00
|
|
|
resp = res;
|
|
|
|
// don't end the response yet
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
await new Promise<void>(resolve => s.listen(0, '127.0.0.1', resolve));
|
2019-08-30 00:45:41 +00:00
|
|
|
const { port } = s.address() as AddressInfo;
|
2021-01-22 19:25:47 +00:00
|
|
|
const p = new Promise<void>(resolve => {
|
2019-11-01 20:37:02 +00:00
|
|
|
w.webContents.on('did-frame-finish-load', (event, isMainFrame) => {
|
2019-08-30 00:45:41 +00:00
|
|
|
if (!isMainFrame) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const main = w.loadURL(`http://127.0.0.1:${port}`);
|
|
|
|
await p;
|
|
|
|
resp.destroy(); // cause the main request to fail
|
|
|
|
await expect(main).to.eventually.be.rejected()
|
|
|
|
.and.have.property('errno', -355); // ERR_INCOMPLETE_CHUNKED_ENCODING
|
|
|
|
s.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getFocusedWebContents() API', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2019-09-03 17:13:06 +00:00
|
|
|
|
|
|
|
const testFn = (process.platform === 'win32' && process.arch === 'arm64' ? it.skip : it);
|
|
|
|
testFn('returns the focused web contents', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: true });
|
2021-05-19 09:27:35 +00:00
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'blank.html'));
|
2019-08-30 00:45:41 +00:00
|
|
|
expect(webContents.getFocusedWebContents().id).to.equal(w.webContents.id);
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
const devToolsOpened = emittedOnce(w.webContents, 'devtools-opened');
|
|
|
|
w.webContents.openDevTools();
|
|
|
|
await devToolsOpened;
|
2020-04-20 23:51:27 +00:00
|
|
|
expect(webContents.getFocusedWebContents().id).to.equal(w.webContents.devToolsWebContents!.id);
|
2019-08-30 00:45:41 +00:00
|
|
|
const devToolsClosed = emittedOnce(w.webContents, 'devtools-closed');
|
|
|
|
w.webContents.closeDevTools();
|
|
|
|
await devToolsClosed;
|
|
|
|
expect(webContents.getFocusedWebContents().id).to.equal(w.webContents.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not crash when called on a detached dev tools window', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: true });
|
2019-08-30 00:45:41 +00:00
|
|
|
|
|
|
|
w.webContents.openDevTools({ mode: 'detach' });
|
|
|
|
w.webContents.inspectElement(100, 100);
|
|
|
|
|
|
|
|
// For some reason we have to wait for two focused events...?
|
|
|
|
await emittedOnce(w.webContents, 'devtools-focused');
|
|
|
|
|
|
|
|
expect(() => { webContents.getFocusedWebContents(); }).to.not.throw();
|
|
|
|
|
|
|
|
// Work around https://github.com/electron/electron/issues/19985
|
2020-06-26 20:59:54 +00:00
|
|
|
await delay();
|
2019-08-30 00:45:41 +00:00
|
|
|
|
|
|
|
const devToolsClosed = emittedOnce(w.webContents, 'devtools-closed');
|
|
|
|
w.webContents.closeDevTools();
|
|
|
|
await devToolsClosed;
|
|
|
|
expect(() => { webContents.getFocusedWebContents(); }).to.not.throw();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setDevToolsWebContents() API', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('sets arbitrary webContents as devtools', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const devtools = new BrowserWindow({ show: false });
|
|
|
|
const promise = emittedOnce(devtools.webContents, 'dom-ready');
|
|
|
|
w.webContents.setDevToolsWebContents(devtools.webContents);
|
|
|
|
w.webContents.openDevTools();
|
|
|
|
await promise;
|
|
|
|
expect(devtools.webContents.getURL().startsWith('devtools://devtools')).to.be.true();
|
|
|
|
const result = await devtools.webContents.executeJavaScript('InspectorFrontendHost.constructor.name');
|
|
|
|
expect(result).to.equal('InspectorFrontendHostImpl');
|
|
|
|
devtools.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isFocused() API', () => {
|
|
|
|
it('returns false when the window is hidden', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(w.isVisible()).to.be.false();
|
|
|
|
expect(w.webContents.isFocused()).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isCurrentlyAudible() API', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('returns whether audio is playing', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
await w.webContents.executeJavaScript(`
|
|
|
|
window.context = new AudioContext
|
|
|
|
// Start in suspended state, because of the
|
|
|
|
// new web audio api policy.
|
|
|
|
context.suspend()
|
|
|
|
window.oscillator = context.createOscillator()
|
|
|
|
oscillator.connect(context.destination)
|
|
|
|
oscillator.start()
|
|
|
|
`);
|
|
|
|
let p = emittedOnce(w.webContents, '-audio-state-changed');
|
|
|
|
w.webContents.executeJavaScript('context.resume()');
|
|
|
|
await p;
|
|
|
|
expect(w.webContents.isCurrentlyAudible()).to.be.true();
|
|
|
|
p = emittedOnce(w.webContents, '-audio-state-changed');
|
|
|
|
w.webContents.executeJavaScript('oscillator.stop()');
|
|
|
|
await p;
|
|
|
|
expect(w.webContents.isCurrentlyAudible()).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('openDevTools() API', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('can show window with activation', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
2019-08-30 00:45:41 +00:00
|
|
|
const focused = emittedOnce(w, 'focus');
|
|
|
|
w.show();
|
|
|
|
await focused;
|
|
|
|
expect(w.isFocused()).to.be.true();
|
2021-06-03 08:05:04 +00:00
|
|
|
const blurred = emittedOnce(w, 'blur');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.openDevTools({ mode: 'detach', activate: true });
|
2019-09-25 21:38:50 +00:00
|
|
|
await Promise.all([
|
|
|
|
emittedOnce(w.webContents, 'devtools-opened'),
|
2019-11-01 20:37:02 +00:00
|
|
|
emittedOnce(w.webContents, 'devtools-focused')
|
2019-09-25 21:38:50 +00:00
|
|
|
]);
|
2021-06-03 08:05:04 +00:00
|
|
|
await blurred;
|
2019-08-30 00:45:41 +00:00
|
|
|
expect(w.isFocused()).to.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can show window without activation', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
2019-08-30 00:45:41 +00:00
|
|
|
const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened');
|
|
|
|
w.webContents.openDevTools({ mode: 'detach', activate: false });
|
|
|
|
await devtoolsOpened;
|
|
|
|
expect(w.webContents.isDevToolsOpened()).to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('before-input-event event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('can prevent document keyboard events', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-08-30 00:45:41 +00:00
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'key-events.html'));
|
|
|
|
const keyDown = new Promise(resolve => {
|
|
|
|
ipcMain.once('keydown', (event, key) => resolve(key));
|
|
|
|
});
|
|
|
|
w.webContents.once('before-input-event', (event, input) => {
|
2019-11-01 20:37:02 +00:00
|
|
|
if (input.key === 'a') event.preventDefault();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'a' });
|
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'b' });
|
|
|
|
expect(await keyDown).to.equal('b');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has the correct properties', async () => {
|
2019-11-01 20:37:02 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
2019-08-30 00:45:41 +00:00
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
const testBeforeInput = async (opts: any) => {
|
|
|
|
const modifiers = [];
|
|
|
|
if (opts.shift) modifiers.push('shift');
|
|
|
|
if (opts.control) modifiers.push('control');
|
|
|
|
if (opts.alt) modifiers.push('alt');
|
|
|
|
if (opts.meta) modifiers.push('meta');
|
|
|
|
if (opts.isAutoRepeat) modifiers.push('isAutoRepeat');
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
const p = emittedOnce(w.webContents, 'before-input-event');
|
|
|
|
w.webContents.sendInputEvent({
|
|
|
|
type: opts.type,
|
|
|
|
keyCode: opts.keyCode,
|
|
|
|
modifiers: modifiers as any
|
|
|
|
});
|
|
|
|
const [, input] = await p;
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
expect(input.type).to.equal(opts.type);
|
|
|
|
expect(input.key).to.equal(opts.key);
|
|
|
|
expect(input.code).to.equal(opts.code);
|
|
|
|
expect(input.isAutoRepeat).to.equal(opts.isAutoRepeat);
|
|
|
|
expect(input.shift).to.equal(opts.shift);
|
|
|
|
expect(input.control).to.equal(opts.control);
|
|
|
|
expect(input.alt).to.equal(opts.alt);
|
|
|
|
expect(input.meta).to.equal(opts.meta);
|
|
|
|
};
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyDown',
|
|
|
|
key: 'A',
|
|
|
|
code: 'KeyA',
|
|
|
|
keyCode: 'a',
|
|
|
|
shift: true,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: true
|
|
|
|
});
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '.',
|
|
|
|
code: 'Period',
|
|
|
|
keyCode: '.',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: true,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: false
|
|
|
|
});
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: '!',
|
|
|
|
code: 'Digit1',
|
|
|
|
keyCode: '1',
|
|
|
|
shift: true,
|
|
|
|
control: false,
|
|
|
|
alt: false,
|
|
|
|
meta: true,
|
|
|
|
isAutoRepeat: false
|
|
|
|
});
|
|
|
|
await testBeforeInput({
|
|
|
|
type: 'keyUp',
|
|
|
|
key: 'Tab',
|
|
|
|
code: 'Tab',
|
|
|
|
keyCode: 'Tab',
|
|
|
|
shift: false,
|
|
|
|
control: true,
|
|
|
|
alt: false,
|
|
|
|
meta: false,
|
|
|
|
isAutoRepeat: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// On Mac, zooming isn't done with the mouse wheel.
|
|
|
|
ifdescribe(process.platform !== 'darwin')('zoom-changed', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2020-06-17 17:08:10 +00:00
|
|
|
it('is emitted with the correct zoom-in info', async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
|
2020-06-17 17:08:10 +00:00
|
|
|
const testZoomChanged = async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.sendInputEvent({
|
|
|
|
type: 'mouseWheel',
|
|
|
|
x: 300,
|
|
|
|
y: 300,
|
|
|
|
deltaX: 0,
|
2020-06-17 17:08:10 +00:00
|
|
|
deltaY: 1,
|
2019-08-30 00:45:41 +00:00
|
|
|
wheelTicksX: 0,
|
2020-06-17 17:08:10 +00:00
|
|
|
wheelTicksY: 1,
|
2019-08-30 00:45:41 +00:00
|
|
|
modifiers: ['control', 'meta']
|
|
|
|
});
|
|
|
|
|
|
|
|
const [, zoomDirection] = await emittedOnce(w.webContents, 'zoom-changed');
|
2020-06-17 17:08:10 +00:00
|
|
|
expect(zoomDirection).to.equal('in');
|
2019-08-30 00:45:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-17 17:08:10 +00:00
|
|
|
await testZoomChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is emitted with the correct zoom-out info', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
|
|
|
|
const testZoomChanged = async () => {
|
|
|
|
w.webContents.sendInputEvent({
|
|
|
|
type: 'mouseWheel',
|
|
|
|
x: 300,
|
|
|
|
y: 300,
|
|
|
|
deltaX: 0,
|
|
|
|
deltaY: -1,
|
|
|
|
wheelTicksX: 0,
|
|
|
|
wheelTicksY: -1,
|
|
|
|
modifiers: ['control', 'meta']
|
|
|
|
});
|
|
|
|
|
|
|
|
const [, zoomDirection] = await emittedOnce(w.webContents, 'zoom-changed');
|
|
|
|
expect(zoomDirection).to.equal('out');
|
|
|
|
};
|
|
|
|
|
|
|
|
await testZoomChanged();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sendInputEvent(event)', () => {
|
|
|
|
let w: BrowserWindow;
|
|
|
|
beforeEach(async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-08-30 00:45:41 +00:00
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'key-events.html'));
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('can send keydown events', async () => {
|
|
|
|
const keydown = emittedOnce(ipcMain, 'keydown');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'A' });
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, key, code, keyCode, shiftKey, ctrlKey, altKey] = await keydown;
|
|
|
|
expect(key).to.equal('a');
|
|
|
|
expect(code).to.equal('KeyA');
|
|
|
|
expect(keyCode).to.equal(65);
|
|
|
|
expect(shiftKey).to.be.false();
|
|
|
|
expect(ctrlKey).to.be.false();
|
|
|
|
expect(altKey).to.be.false();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('can send keydown events with modifiers', async () => {
|
|
|
|
const keydown = emittedOnce(ipcMain, 'keydown');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Z', modifiers: ['shift', 'ctrl'] });
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, key, code, keyCode, shiftKey, ctrlKey, altKey] = await keydown;
|
|
|
|
expect(key).to.equal('Z');
|
|
|
|
expect(code).to.equal('KeyZ');
|
|
|
|
expect(keyCode).to.equal(90);
|
|
|
|
expect(shiftKey).to.be.true();
|
|
|
|
expect(ctrlKey).to.be.true();
|
|
|
|
expect(altKey).to.be.false();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('can send keydown events with special keys', async () => {
|
|
|
|
const keydown = emittedOnce(ipcMain, 'keydown');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Tab', modifiers: ['alt'] });
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, key, code, keyCode, shiftKey, ctrlKey, altKey] = await keydown;
|
|
|
|
expect(key).to.equal('Tab');
|
|
|
|
expect(code).to.equal('Tab');
|
|
|
|
expect(keyCode).to.equal(9);
|
|
|
|
expect(shiftKey).to.be.false();
|
|
|
|
expect(ctrlKey).to.be.false();
|
|
|
|
expect(altKey).to.be.true();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('can send char events', async () => {
|
|
|
|
const keypress = emittedOnce(ipcMain, 'keypress');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'A' });
|
|
|
|
w.webContents.sendInputEvent({ type: 'char', keyCode: 'A' });
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, key, code, keyCode, shiftKey, ctrlKey, altKey] = await keypress;
|
|
|
|
expect(key).to.equal('a');
|
|
|
|
expect(code).to.equal('KeyA');
|
|
|
|
expect(keyCode).to.equal(65);
|
|
|
|
expect(shiftKey).to.be.false();
|
|
|
|
expect(ctrlKey).to.be.false();
|
|
|
|
expect(altKey).to.be.false();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('can send char events with modifiers', async () => {
|
|
|
|
const keypress = emittedOnce(ipcMain, 'keypress');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Z' });
|
|
|
|
w.webContents.sendInputEvent({ type: 'char', keyCode: 'Z', modifiers: ['shift', 'ctrl'] });
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, key, code, keyCode, shiftKey, ctrlKey, altKey] = await keypress;
|
|
|
|
expect(key).to.equal('Z');
|
|
|
|
expect(code).to.equal('KeyZ');
|
|
|
|
expect(keyCode).to.equal(90);
|
|
|
|
expect(shiftKey).to.be.true();
|
|
|
|
expect(ctrlKey).to.be.true();
|
|
|
|
expect(altKey).to.be.false();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('insertCSS', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('supports inserting CSS', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
await w.webContents.insertCSS('body { background-repeat: round; }');
|
|
|
|
const result = await w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")');
|
|
|
|
expect(result).to.equal('round');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports removing inserted CSS', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
const key = await w.webContents.insertCSS('body { background-repeat: round; }');
|
|
|
|
await w.webContents.removeInsertedCSS(key);
|
|
|
|
const result = await w.webContents.executeJavaScript('window.getComputedStyle(document.body).getPropertyValue("background-repeat")');
|
|
|
|
expect(result).to.equal('repeat');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('inspectElement()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('supports inspecting an element in the devtools', (done) => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.once('devtools-opened', () => { done(); });
|
|
|
|
w.webContents.inspectElement(10, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('startDrag({file, icon})', () => {
|
|
|
|
it('throws errors for a missing file or a missing/empty icon', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.startDrag({ icon: path.join(fixturesPath, 'assets', 'logo.png') } as any);
|
2020-03-20 15:12:18 +00:00
|
|
|
}).to.throw('Must specify either \'file\' or \'files\' option');
|
2019-08-30 00:45:41 +00:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.startDrag({ file: __filename } as any);
|
2021-01-04 20:58:31 +00:00
|
|
|
}).to.throw('\'icon\' parameter is required');
|
2019-08-30 00:45:41 +00:00
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
expect(() => {
|
2020-08-24 16:58:55 +00:00
|
|
|
w.webContents.startDrag({ file: __filename, icon: path.join(mainFixturesPath, 'blank.png') });
|
2021-01-04 20:58:31 +00:00
|
|
|
}).to.throw(/Failed to load image from path (.+)/);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-01 10:28:57 +00:00
|
|
|
describe('focus APIs', () => {
|
|
|
|
describe('focus()', () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
afterEach(closeAllWindows);
|
2022-02-01 10:28:57 +00:00
|
|
|
it('does not blur the focused window when the web contents is hidden', async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
w.show();
|
2019-11-14 22:09:03 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
w.focus();
|
|
|
|
const child = new BrowserWindow({ show: false });
|
|
|
|
child.loadURL('about:blank');
|
|
|
|
child.webContents.focus();
|
|
|
|
const currentFocused = w.isFocused();
|
|
|
|
const childFocused = child.isFocused();
|
|
|
|
child.close();
|
|
|
|
expect(currentFocused).to.be.true();
|
|
|
|
expect(childFocused).to.be.false();
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
});
|
2022-02-01 10:28:57 +00:00
|
|
|
|
2022-02-02 15:32:53 +00:00
|
|
|
const moveFocusToDevTools = async (win: BrowserWindow) => {
|
|
|
|
const devToolsOpened = emittedOnce(win.webContents, 'devtools-opened');
|
|
|
|
win.webContents.openDevTools({ mode: 'right' });
|
|
|
|
await devToolsOpened;
|
|
|
|
win.webContents.devToolsWebContents!.focus();
|
|
|
|
};
|
|
|
|
|
2022-02-01 10:28:57 +00:00
|
|
|
describe('focus event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2022-02-02 15:32:53 +00:00
|
|
|
|
2022-02-01 10:28:57 +00:00
|
|
|
it('is triggered when web contents is focused', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
2022-02-02 15:32:53 +00:00
|
|
|
await moveFocusToDevTools(w);
|
2022-02-01 10:28:57 +00:00
|
|
|
const focusPromise = emittedOnce(w.webContents, 'focus');
|
|
|
|
w.webContents.focus();
|
|
|
|
await expect(focusPromise).to.eventually.be.fulfilled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('blur event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('is triggered when web contents is blurred', async () => {
|
|
|
|
const w = new BrowserWindow({ show: true });
|
|
|
|
await w.loadURL('about:blank');
|
2022-02-02 15:32:53 +00:00
|
|
|
w.webContents.focus();
|
2022-02-01 10:28:57 +00:00
|
|
|
const blurPromise = emittedOnce(w.webContents, 'blur');
|
2022-02-02 15:32:53 +00:00
|
|
|
await moveFocusToDevTools(w);
|
2022-02-01 10:28:57 +00:00
|
|
|
await expect(blurPromise).to.eventually.be.fulfilled();
|
|
|
|
});
|
|
|
|
});
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getOSProcessId()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2022-06-16 07:46:11 +00:00
|
|
|
it('returns a valid process id', async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
expect(w.webContents.getOSProcessId()).to.equal(0);
|
|
|
|
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
expect(w.webContents.getOSProcessId()).to.be.above(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-26 00:03:59 +00:00
|
|
|
describe('getMediaSourceId()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('returns a valid stream id', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
expect(w.webContents.getMediaSourceId(w.webContents)).to.be.a('string').that.is.not.empty();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-13 17:16:08 +00:00
|
|
|
describe('userAgent APIs', () => {
|
2022-07-29 15:09:47 +00:00
|
|
|
it('is not empty by default', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const userAgent = w.webContents.getUserAgent();
|
|
|
|
expect(userAgent).to.be.a('string').that.is.not.empty();
|
|
|
|
});
|
|
|
|
|
2020-03-13 17:16:08 +00:00
|
|
|
it('can set the user agent (functions)', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const userAgent = w.webContents.getUserAgent();
|
|
|
|
|
|
|
|
w.webContents.setUserAgent('my-user-agent');
|
|
|
|
expect(w.webContents.getUserAgent()).to.equal('my-user-agent');
|
|
|
|
|
|
|
|
w.webContents.setUserAgent(userAgent);
|
|
|
|
expect(w.webContents.getUserAgent()).to.equal(userAgent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can set the user agent (properties)', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const userAgent = w.webContents.userAgent;
|
|
|
|
|
|
|
|
w.webContents.userAgent = 'my-user-agent';
|
|
|
|
expect(w.webContents.userAgent).to.equal('my-user-agent');
|
|
|
|
|
|
|
|
w.webContents.userAgent = userAgent;
|
|
|
|
expect(w.webContents.userAgent).to.equal(userAgent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('audioMuted APIs', () => {
|
|
|
|
it('can set the audio mute level (functions)', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
|
|
|
|
w.webContents.setAudioMuted(true);
|
|
|
|
expect(w.webContents.isAudioMuted()).to.be.true();
|
|
|
|
|
|
|
|
w.webContents.setAudioMuted(false);
|
|
|
|
expect(w.webContents.isAudioMuted()).to.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can set the audio mute level (functions)', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
|
|
|
|
w.webContents.audioMuted = true;
|
|
|
|
expect(w.webContents.audioMuted).to.be.true();
|
|
|
|
|
|
|
|
w.webContents.audioMuted = false;
|
|
|
|
expect(w.webContents.audioMuted).to.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
describe('zoom api', () => {
|
|
|
|
const hostZoomMap: Record<string, number> = {
|
|
|
|
host1: 0.3,
|
|
|
|
host2: 0.7,
|
|
|
|
host3: 0.2
|
|
|
|
};
|
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
before(() => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const protocol = session.defaultSession.protocol;
|
2022-01-07 08:52:47 +00:00
|
|
|
protocol.registerStringProtocol(standardScheme, (request, callback) => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const response = `<script>
|
2020-05-13 18:05:44 +00:00
|
|
|
const {ipcRenderer} = require('electron')
|
2019-08-30 00:45:41 +00:00
|
|
|
ipcRenderer.send('set-zoom', window.location.hostname)
|
|
|
|
ipcRenderer.on(window.location.hostname + '-zoom-set', () => {
|
2020-05-13 18:05:44 +00:00
|
|
|
ipcRenderer.send(window.location.hostname + '-zoom-level')
|
2019-08-30 00:45:41 +00:00
|
|
|
})
|
|
|
|
</script>`;
|
|
|
|
callback({ data: response, mimeType: 'text/html' });
|
2020-06-02 16:46:18 +00:00
|
|
|
});
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
after(() => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const protocol = session.defaultSession.protocol;
|
2022-01-07 08:52:47 +00:00
|
|
|
protocol.unregisterProtocol(standardScheme);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
2020-03-13 23:13:05 +00:00
|
|
|
it('throws on an invalid zoomFactor', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.setZoomFactor(0.0);
|
|
|
|
}).to.throw(/'zoomFactor' must be a double greater than 0.0/);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.setZoomFactor(-2.0);
|
|
|
|
}).to.throw(/'zoomFactor' must be a double greater than 0.0/);
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
it('can set the correct zoom level (functions)', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
try {
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const zoomLevel = w.webContents.getZoomLevel();
|
|
|
|
expect(zoomLevel).to.eql(0.0);
|
|
|
|
w.webContents.setZoomLevel(0.5);
|
|
|
|
const newZoomLevel = w.webContents.getZoomLevel();
|
|
|
|
expect(newZoomLevel).to.eql(0.5);
|
|
|
|
} finally {
|
|
|
|
w.webContents.setZoomLevel(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-13 17:16:08 +00:00
|
|
|
it('can set the correct zoom level (properties)', async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
try {
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const zoomLevel = w.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel).to.eql(0.0);
|
|
|
|
w.webContents.zoomLevel = 0.5;
|
|
|
|
const newZoomLevel = w.webContents.zoomLevel;
|
|
|
|
expect(newZoomLevel).to.eql(0.5);
|
|
|
|
} finally {
|
|
|
|
w.webContents.zoomLevel = 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-13 17:16:08 +00:00
|
|
|
it('can set the correct zoom factor (functions)', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
try {
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const zoomFactor = w.webContents.getZoomFactor();
|
|
|
|
expect(zoomFactor).to.eql(1.0);
|
|
|
|
|
|
|
|
w.webContents.setZoomFactor(0.5);
|
|
|
|
const newZoomFactor = w.webContents.getZoomFactor();
|
|
|
|
expect(newZoomFactor).to.eql(0.5);
|
|
|
|
} finally {
|
|
|
|
w.webContents.setZoomFactor(1.0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can set the correct zoom factor (properties)', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
try {
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const zoomFactor = w.webContents.zoomFactor;
|
|
|
|
expect(zoomFactor).to.eql(1.0);
|
|
|
|
|
|
|
|
w.webContents.zoomFactor = 0.5;
|
|
|
|
const newZoomFactor = w.webContents.zoomFactor;
|
|
|
|
expect(newZoomFactor).to.eql(0.5);
|
|
|
|
} finally {
|
|
|
|
w.webContents.zoomFactor = 1.0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-30 00:45:41 +00:00
|
|
|
it('can persist zoom level across navigation', (done) => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-08-30 00:45:41 +00:00
|
|
|
let finalNavigation = false;
|
|
|
|
ipcMain.on('set-zoom', (e, host) => {
|
|
|
|
const zoomLevel = hostZoomMap[host];
|
|
|
|
if (!finalNavigation) w.webContents.zoomLevel = zoomLevel;
|
|
|
|
e.sender.send(`${host}-zoom-set`);
|
|
|
|
});
|
2020-05-13 18:05:44 +00:00
|
|
|
ipcMain.on('host1-zoom-level', (e) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
const zoomLevel = e.sender.getZoomLevel();
|
|
|
|
const expectedZoomLevel = hostZoomMap.host1;
|
|
|
|
expect(zoomLevel).to.equal(expectedZoomLevel);
|
|
|
|
if (finalNavigation) {
|
|
|
|
done();
|
|
|
|
} else {
|
2022-01-07 08:52:47 +00:00
|
|
|
w.loadURL(`${standardScheme}://host2`);
|
2020-06-30 22:10:36 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
2019-08-30 00:45:41 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-13 18:05:44 +00:00
|
|
|
ipcMain.once('host2-zoom-level', (e) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
const zoomLevel = e.sender.getZoomLevel();
|
|
|
|
const expectedZoomLevel = hostZoomMap.host2;
|
|
|
|
expect(zoomLevel).to.equal(expectedZoomLevel);
|
|
|
|
finalNavigation = true;
|
|
|
|
w.webContents.goBack();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
2022-01-07 08:52:47 +00:00
|
|
|
w.loadURL(`${standardScheme}://host1`);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('can propagate zoom level across same session', async () => {
|
2020-05-13 18:05:44 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
2019-08-30 00:45:41 +00:00
|
|
|
const w2 = new BrowserWindow({ show: false });
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
defer(() => {
|
2019-08-30 00:45:41 +00:00
|
|
|
w2.setClosable(true);
|
|
|
|
w2.close();
|
|
|
|
});
|
2020-06-30 22:10:36 +00:00
|
|
|
|
2022-01-07 08:52:47 +00:00
|
|
|
await w.loadURL(`${standardScheme}://host3`);
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.zoomLevel = hostZoomMap.host3;
|
|
|
|
|
2022-01-07 08:52:47 +00:00
|
|
|
await w2.loadURL(`${standardScheme}://host3`);
|
2020-06-30 22:10:36 +00:00
|
|
|
const zoomLevel1 = w.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel1).to.equal(hostZoomMap.host3);
|
|
|
|
|
|
|
|
const zoomLevel2 = w2.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel1).to.equal(zoomLevel2);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('cannot propagate zoom level across different session', async () => {
|
2019-08-30 00:45:41 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
const w2 = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
partition: 'temp'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const protocol = w2.webContents.session.protocol;
|
2022-01-07 08:52:47 +00:00
|
|
|
protocol.registerStringProtocol(standardScheme, (request, callback) => {
|
2019-08-30 00:45:41 +00:00
|
|
|
callback('hello');
|
|
|
|
});
|
2020-06-02 16:46:18 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
defer(() => {
|
2020-06-02 16:46:18 +00:00
|
|
|
w2.setClosable(true);
|
|
|
|
w2.close();
|
2020-07-01 19:14:38 +00:00
|
|
|
|
2022-01-07 08:52:47 +00:00
|
|
|
protocol.unregisterProtocol(standardScheme);
|
2020-06-02 16:46:18 +00:00
|
|
|
});
|
2020-06-30 22:10:36 +00:00
|
|
|
|
2022-01-07 08:52:47 +00:00
|
|
|
await w.loadURL(`${standardScheme}://host3`);
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.zoomLevel = hostZoomMap.host3;
|
|
|
|
|
2022-01-07 08:52:47 +00:00
|
|
|
await w2.loadURL(`${standardScheme}://host3`);
|
2020-06-30 22:10:36 +00:00
|
|
|
const zoomLevel1 = w.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel1).to.equal(hostZoomMap.host3);
|
|
|
|
|
|
|
|
const zoomLevel2 = w2.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel2).to.equal(0);
|
|
|
|
expect(zoomLevel1).to.not.equal(zoomLevel2);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can persist when it contains iframe', (done) => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
res.end();
|
|
|
|
}, 200);
|
|
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port;
|
|
|
|
const content = `<iframe src=${url}></iframe>`;
|
|
|
|
w.webContents.on('did-frame-finish-load', (e, isMainFrame) => {
|
|
|
|
if (!isMainFrame) {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
const zoomLevel = w.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel).to.equal(2.0);
|
2019-08-30 00:45:41 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.zoomLevel = 0;
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
} finally {
|
|
|
|
server.close();
|
|
|
|
}
|
2019-08-30 00:45:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
w.webContents.on('dom-ready', () => {
|
|
|
|
w.webContents.zoomLevel = 2.0;
|
|
|
|
});
|
|
|
|
w.loadURL(`data:text/html,${content}`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('cannot propagate when used with webframe', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-06-30 22:10:36 +00:00
|
|
|
const w2 = new BrowserWindow({ show: false });
|
2019-08-30 00:45:41 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
const temporaryZoomSet = emittedOnce(ipcMain, 'temporary-zoom-set');
|
2019-08-30 00:45:41 +00:00
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'webframe-zoom.html'));
|
2020-06-30 22:10:36 +00:00
|
|
|
await temporaryZoomSet;
|
|
|
|
|
|
|
|
const finalZoomLevel = w.webContents.getZoomLevel();
|
|
|
|
await w2.loadFile(path.join(fixturesPath, 'pages', 'c.html'));
|
|
|
|
const zoomLevel1 = w.webContents.zoomLevel;
|
|
|
|
const zoomLevel2 = w2.webContents.zoomLevel;
|
|
|
|
|
|
|
|
w2.setClosable(true);
|
|
|
|
w2.close();
|
|
|
|
|
|
|
|
expect(zoomLevel1).to.equal(finalZoomLevel);
|
|
|
|
expect(zoomLevel2).to.equal(0);
|
|
|
|
expect(zoomLevel1).to.not.equal(zoomLevel2);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
|
2020-02-25 02:11:06 +00:00
|
|
|
describe('with unique domains', () => {
|
|
|
|
let server: http.Server;
|
|
|
|
let serverUrl: string;
|
|
|
|
let crossSiteUrl: string;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
setTimeout(() => res.end('hey'), 0);
|
|
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
|
|
|
crossSiteUrl = `http://localhost:${(server.address() as AddressInfo).port}`;
|
2019-08-30 00:45:41 +00:00
|
|
|
done();
|
2020-02-25 02:11:06 +00:00
|
|
|
});
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
2020-02-25 02:11:06 +00:00
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cannot persist zoom level after navigation with webFrame', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2020-02-25 02:11:06 +00:00
|
|
|
const source = `
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.setZoomLevel(0.6)
|
|
|
|
ipcRenderer.send('zoom-level-set', webFrame.getZoomLevel())
|
|
|
|
`;
|
|
|
|
const zoomLevelPromise = emittedOnce(ipcMain, 'zoom-level-set');
|
|
|
|
await w.loadURL(serverUrl);
|
|
|
|
await w.webContents.executeJavaScript(source);
|
|
|
|
let [, zoomLevel] = await zoomLevelPromise;
|
2019-08-30 00:45:41 +00:00
|
|
|
expect(zoomLevel).to.equal(0.6);
|
2020-02-25 02:11:06 +00:00
|
|
|
const loadPromise = emittedOnce(w.webContents, 'did-finish-load');
|
|
|
|
await w.loadURL(crossSiteUrl);
|
|
|
|
await loadPromise;
|
|
|
|
zoomLevel = w.webContents.zoomLevel;
|
|
|
|
expect(zoomLevel).to.equal(0);
|
2019-08-30 00:45:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-09-04 21:44:13 +00:00
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
describe('webrtc ip policy api', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('can set and get webrtc ip policies', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const policies = [
|
|
|
|
'default',
|
|
|
|
'default_public_interface_only',
|
|
|
|
'default_public_and_private_interfaces',
|
|
|
|
'disable_non_proxied_udp'
|
|
|
|
];
|
|
|
|
policies.forEach((policy) => {
|
|
|
|
w.webContents.setWebRTCIPHandlingPolicy(policy as any);
|
|
|
|
expect(w.webContents.getWebRTCIPHandlingPolicy()).to.equal(policy);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-26 16:37:08 +00:00
|
|
|
describe('opener api', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('can get opener with window.open()', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const childPromise = emittedOnce(w.webContents, 'did-create-window');
|
|
|
|
w.webContents.executeJavaScript('window.open("about:blank")', true);
|
|
|
|
const [childWindow] = await childPromise;
|
|
|
|
expect(childWindow.webContents.opener).to.equal(w.webContents.mainFrame);
|
|
|
|
});
|
|
|
|
it('has no opener when using "noopener"', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const childPromise = emittedOnce(w.webContents, 'did-create-window');
|
|
|
|
w.webContents.executeJavaScript('window.open("about:blank", undefined, "noopener")', true);
|
|
|
|
const [childWindow] = await childPromise;
|
|
|
|
expect(childWindow.webContents.opener).to.be.null();
|
|
|
|
});
|
|
|
|
it('can get opener with a[target=_blank][rel=opener]', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const childPromise = emittedOnce(w.webContents, 'did-create-window');
|
|
|
|
w.webContents.executeJavaScript(`(function() {
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.target = '_blank';
|
|
|
|
a.rel = 'opener';
|
|
|
|
a.href = 'about:blank';
|
|
|
|
a.click();
|
|
|
|
}())`, true);
|
|
|
|
const [childWindow] = await childPromise;
|
|
|
|
expect(childWindow.webContents.opener).to.equal(w.webContents.mainFrame);
|
|
|
|
});
|
|
|
|
it('has no opener with a[target=_blank][rel=noopener]', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const childPromise = emittedOnce(w.webContents, 'did-create-window');
|
|
|
|
w.webContents.executeJavaScript(`(function() {
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.target = '_blank';
|
|
|
|
a.rel = 'noopener';
|
|
|
|
a.href = 'about:blank';
|
|
|
|
a.click();
|
|
|
|
}())`, true);
|
|
|
|
const [childWindow] = await childPromise;
|
|
|
|
expect(childWindow.webContents.opener).to.be.null();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
describe('render view deleted events', () => {
|
|
|
|
let server: http.Server;
|
|
|
|
let serverUrl: string;
|
|
|
|
let crossSiteUrl: string;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
const respond = () => {
|
|
|
|
if (req.url === '/redirect-cross-site') {
|
|
|
|
res.setHeader('Location', `${crossSiteUrl}/redirected`);
|
|
|
|
res.statusCode = 302;
|
|
|
|
res.end();
|
|
|
|
} else if (req.url === '/redirected') {
|
|
|
|
res.end('<html><script>window.localStorage</script></html>');
|
2020-10-07 23:11:19 +00:00
|
|
|
} else if (req.url === '/first-window-open') {
|
|
|
|
res.end(`<html><script>window.open('${serverUrl}/second-window-open', 'first child');</script></html>`);
|
|
|
|
} else if (req.url === '/second-window-open') {
|
|
|
|
res.end('<html><script>window.open(\'wrong://url\', \'second child\');</script></html>');
|
2019-09-30 22:00:47 +00:00
|
|
|
} else {
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(respond, 0);
|
|
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
|
|
|
crossSiteUrl = `http://localhost:${(server.address() as AddressInfo).port}`;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('does not emit current-render-view-deleted when speculative RVHs are deleted', async () => {
|
2019-09-30 22:00:47 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
let currentRenderViewDeletedEmitted = false;
|
|
|
|
const renderViewDeletedHandler = () => {
|
|
|
|
currentRenderViewDeletedEmitted = true;
|
|
|
|
};
|
|
|
|
w.webContents.on('current-render-view-deleted' as any, renderViewDeletedHandler);
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
w.webContents.removeListener('current-render-view-deleted' as any, renderViewDeletedHandler);
|
|
|
|
w.close();
|
|
|
|
});
|
2020-06-30 22:10:36 +00:00
|
|
|
const destroyed = emittedOnce(w.webContents, 'destroyed');
|
2019-09-30 22:00:47 +00:00
|
|
|
w.loadURL(`${serverUrl}/redirect-cross-site`);
|
2020-06-30 22:10:36 +00:00
|
|
|
await destroyed;
|
|
|
|
expect(currentRenderViewDeletedEmitted).to.be.false('current-render-view-deleted was emitted');
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
|
2022-01-06 17:28:03 +00:00
|
|
|
it('does not emit current-render-view-deleted when speculative RVHs are deleted', async () => {
|
|
|
|
const parentWindow = new BrowserWindow({ show: false });
|
2020-10-07 23:11:19 +00:00
|
|
|
let currentRenderViewDeletedEmitted = false;
|
2020-12-01 01:06:25 +00:00
|
|
|
let childWindow: BrowserWindow | null = null;
|
2020-10-07 23:11:19 +00:00
|
|
|
const destroyed = emittedOnce(parentWindow.webContents, 'destroyed');
|
|
|
|
const renderViewDeletedHandler = () => {
|
|
|
|
currentRenderViewDeletedEmitted = true;
|
|
|
|
};
|
2021-01-22 19:25:47 +00:00
|
|
|
const childWindowCreated = new Promise<void>((resolve) => {
|
2020-12-01 01:06:25 +00:00
|
|
|
app.once('browser-window-created', (event, window) => {
|
|
|
|
childWindow = window;
|
|
|
|
window.webContents.on('current-render-view-deleted' as any, renderViewDeletedHandler);
|
|
|
|
resolve();
|
|
|
|
});
|
2020-10-07 23:11:19 +00:00
|
|
|
});
|
|
|
|
parentWindow.loadURL(`${serverUrl}/first-window-open`);
|
2020-12-01 01:06:25 +00:00
|
|
|
await childWindowCreated;
|
|
|
|
childWindow!.webContents.removeListener('current-render-view-deleted' as any, renderViewDeletedHandler);
|
|
|
|
parentWindow.close();
|
2020-10-07 23:11:19 +00:00
|
|
|
await destroyed;
|
|
|
|
expect(currentRenderViewDeletedEmitted).to.be.false('child window was destroyed');
|
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('emits current-render-view-deleted if the current RVHs are deleted', async () => {
|
2019-09-30 22:00:47 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
let currentRenderViewDeletedEmitted = false;
|
|
|
|
w.webContents.on('current-render-view-deleted' as any, () => {
|
|
|
|
currentRenderViewDeletedEmitted = true;
|
|
|
|
});
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
w.close();
|
|
|
|
});
|
2020-06-30 22:10:36 +00:00
|
|
|
const destroyed = emittedOnce(w.webContents, 'destroyed');
|
2019-09-30 22:00:47 +00:00
|
|
|
w.loadURL(`${serverUrl}/redirect-cross-site`);
|
2020-06-30 22:10:36 +00:00
|
|
|
await destroyed;
|
|
|
|
expect(currentRenderViewDeletedEmitted).to.be.true('current-render-view-deleted wasn\'t emitted');
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('emits render-view-deleted if any RVHs are deleted', async () => {
|
2019-09-30 22:00:47 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
let rvhDeletedCount = 0;
|
|
|
|
w.webContents.on('render-view-deleted' as any, () => {
|
|
|
|
rvhDeletedCount++;
|
|
|
|
});
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
w.close();
|
|
|
|
});
|
2020-06-30 22:10:36 +00:00
|
|
|
const destroyed = emittedOnce(w.webContents, 'destroyed');
|
2019-09-30 22:00:47 +00:00
|
|
|
w.loadURL(`${serverUrl}/redirect-cross-site`);
|
2020-06-30 22:10:36 +00:00
|
|
|
await destroyed;
|
|
|
|
const expectedRenderViewDeletedEventCount = 1;
|
|
|
|
expect(rvhDeletedCount).to.equal(expectedRenderViewDeletedEventCount, 'render-view-deleted wasn\'t emitted the expected nr. of times');
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setIgnoreMenuShortcuts(ignore)', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('does not throw', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
expect(() => {
|
|
|
|
w.webContents.setIgnoreMenuShortcuts(true);
|
|
|
|
w.webContents.setIgnoreMenuShortcuts(false);
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-02 21:50:24 +00:00
|
|
|
const crashPrefs = [
|
|
|
|
{
|
|
|
|
nodeIntegration: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const nicePrefs = (o: any) => {
|
|
|
|
let s = '';
|
|
|
|
for (const key of Object.keys(o)) {
|
|
|
|
s += `${key}=${o[key]}, `;
|
|
|
|
}
|
|
|
|
return `(${s.slice(0, s.length - 2)})`;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const prefs of crashPrefs) {
|
|
|
|
describe(`crash with webPreferences ${nicePrefs(prefs)}`, () => {
|
|
|
|
let w: BrowserWindow;
|
|
|
|
beforeEach(async () => {
|
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('isCrashed() is false by default', () => {
|
|
|
|
expect(w.webContents.isCrashed()).to.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forcefullyCrashRenderer() crashes the process with reason=killed||crashed', async () => {
|
|
|
|
expect(w.webContents.isCrashed()).to.equal(false);
|
|
|
|
const crashEvent = emittedOnce(w.webContents, 'render-process-gone');
|
|
|
|
w.webContents.forcefullyCrashRenderer();
|
|
|
|
const [, details] = await crashEvent;
|
|
|
|
expect(details.reason === 'killed' || details.reason === 'crashed').to.equal(true, 'reason should be killed || crashed');
|
|
|
|
expect(w.webContents.isCrashed()).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('a crashed process is recoverable with reload()', async () => {
|
|
|
|
expect(w.webContents.isCrashed()).to.equal(false);
|
|
|
|
w.webContents.forcefullyCrashRenderer();
|
|
|
|
w.webContents.reload();
|
|
|
|
expect(w.webContents.isCrashed()).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
// Destroying webContents in its event listener is going to crash when
|
|
|
|
// Electron is built in Debug mode.
|
|
|
|
describe('destroy()', () => {
|
|
|
|
let server: http.Server;
|
|
|
|
let serverUrl: string;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
switch (request.url) {
|
|
|
|
case '/net-error':
|
|
|
|
response.destroy();
|
|
|
|
break;
|
|
|
|
case '/200':
|
|
|
|
response.end();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
done('unsupported endpoint');
|
|
|
|
}
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
serverUrl = 'http://127.0.0.1:' + (server.address() as AddressInfo).port;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
const events = [
|
|
|
|
{ name: 'did-start-loading', url: '/200' },
|
|
|
|
{ name: 'dom-ready', url: '/200' },
|
|
|
|
{ name: 'did-stop-loading', url: '/200' },
|
|
|
|
{ name: 'did-finish-load', url: '/200' },
|
|
|
|
// FIXME: Multiple Emit calls inside an observer assume that object
|
|
|
|
// will be alive till end of the observer. Synchronous `destroy` api
|
|
|
|
// violates this contract and crashes.
|
|
|
|
{ name: 'did-frame-finish-load', url: '/200' },
|
|
|
|
{ name: 'did-fail-load', url: '/net-error' }
|
|
|
|
];
|
|
|
|
for (const e of events) {
|
2020-01-09 19:50:30 +00:00
|
|
|
it(`should not crash when invoked synchronously inside ${e.name} handler`, async function () {
|
|
|
|
// This test is flaky on Windows CI and we don't know why, but the
|
|
|
|
// purpose of this test is to make sure Electron does not crash so it
|
|
|
|
// is fine to retry this test for a few times.
|
|
|
|
this.retries(3);
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
const contents = (webContents as any).create() as WebContents;
|
|
|
|
const originalEmit = contents.emit.bind(contents);
|
2019-10-06 20:00:38 +00:00
|
|
|
contents.emit = (...args) => { return originalEmit(...args); };
|
2022-12-14 21:07:38 +00:00
|
|
|
contents.once(e.name as any, () => contents.destroy());
|
2019-09-30 22:00:47 +00:00
|
|
|
const destroyed = emittedOnce(contents, 'destroyed');
|
|
|
|
contents.loadURL(serverUrl + e.url);
|
|
|
|
await destroyed;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-16 22:21:40 +00:00
|
|
|
describe('did-change-theme-color event', () => {
|
2019-09-30 22:00:47 +00:00
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('is triggered with correct theme color', (done) => {
|
|
|
|
const w = new BrowserWindow({ show: true });
|
|
|
|
let count = 0;
|
|
|
|
w.webContents.on('did-change-theme-color', (e, color) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
if (count === 0) {
|
|
|
|
count += 1;
|
|
|
|
expect(color).to.equal('#FFEEDD');
|
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
} else if (count === 1) {
|
|
|
|
expect(color).to.be.null();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
2019-09-30 22:00:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'theme-color.html'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('console-message event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('is triggered with correct log message', (done) => {
|
|
|
|
const w = new BrowserWindow({ show: true });
|
|
|
|
w.webContents.on('console-message', (e, level, message) => {
|
|
|
|
// Don't just assert as Chromium might emit other logs that we should ignore.
|
|
|
|
if (message === 'a') {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'a.html'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ipc-message event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('emits when the renderer process sends an asynchronous message', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: true, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-09-30 22:00:47 +00:00
|
|
|
await w.webContents.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript(`
|
|
|
|
require('electron').ipcRenderer.send('message', 'Hello World!')
|
|
|
|
`);
|
|
|
|
|
|
|
|
const [, channel, message] = await emittedOnce(w.webContents, 'ipc-message');
|
|
|
|
expect(channel).to.equal('message');
|
|
|
|
expect(message).to.equal('Hello World!');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ipc-message-sync event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('emits when the renderer process sends a synchronous message', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: true, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-09-30 22:00:47 +00:00
|
|
|
await w.webContents.loadURL('about:blank');
|
|
|
|
const promise: Promise<[string, string]> = new Promise(resolve => {
|
|
|
|
w.webContents.once('ipc-message-sync', (event, channel, arg) => {
|
|
|
|
event.returnValue = 'foobar' as any;
|
|
|
|
resolve([channel, arg]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const result = await w.webContents.executeJavaScript(`
|
|
|
|
require('electron').ipcRenderer.sendSync('message', 'Hello World!')
|
|
|
|
`);
|
|
|
|
|
|
|
|
const [channel, message] = await promise;
|
|
|
|
expect(channel).to.equal('message');
|
|
|
|
expect(message).to.equal('Hello World!');
|
|
|
|
expect(result).to.equal('foobar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('referrer', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('propagates referrer information to new target=_blank windows', (done) => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/should_have_referrer') {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
expect(req.headers.referer).to.equal(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`);
|
|
|
|
return done();
|
|
|
|
} catch (e) {
|
|
|
|
return done(e);
|
|
|
|
} finally {
|
|
|
|
server.close();
|
|
|
|
}
|
2019-09-30 22:00:47 +00:00
|
|
|
}
|
|
|
|
res.end('<a id="a" href="/should_have_referrer" target="_blank">link</a>');
|
|
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port + '/';
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2022-05-23 14:39:50 +00:00
|
|
|
w.webContents.setWindowOpenHandler(details => {
|
|
|
|
expect(details.referrer.url).to.equal(url);
|
|
|
|
expect(details.referrer.policy).to.equal('strict-origin-when-cross-origin');
|
|
|
|
return { action: 'allow' };
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
w.webContents.executeJavaScript('a.click()');
|
|
|
|
});
|
|
|
|
w.loadURL(url);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-01 15:11:11 +00:00
|
|
|
it('propagates referrer information to windows opened with window.open', (done) => {
|
2019-09-30 22:00:47 +00:00
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/should_have_referrer') {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
expect(req.headers.referer).to.equal(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`);
|
|
|
|
return done();
|
|
|
|
} catch (e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
2019-09-30 22:00:47 +00:00
|
|
|
}
|
|
|
|
res.end('');
|
|
|
|
});
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port + '/';
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2022-05-23 14:39:50 +00:00
|
|
|
w.webContents.setWindowOpenHandler(details => {
|
|
|
|
expect(details.referrer.url).to.equal(url);
|
2022-11-01 15:11:11 +00:00
|
|
|
expect(details.referrer.policy).to.equal('strict-origin-when-cross-origin');
|
2022-05-23 14:39:50 +00:00
|
|
|
return { action: 'allow' };
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
w.webContents.executeJavaScript('window.open(location.href + "should_have_referrer")');
|
|
|
|
});
|
|
|
|
w.loadURL(url);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('webframe messages in sandboxed contents', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('responds to executeJavaScript', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const result = await w.webContents.executeJavaScript('37 + 5');
|
|
|
|
expect(result).to.equal(42);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('preload-error event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
const generateSpecs = (description: string, sandbox: boolean) => {
|
|
|
|
describe(description, () => {
|
|
|
|
it('is triggered when unhandled exception is thrown', async () => {
|
|
|
|
const preload = path.join(fixturesPath, 'module', 'preload-error-exception.js');
|
|
|
|
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox,
|
|
|
|
preload
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'preload-error');
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
|
|
|
|
const [, preloadPath, error] = await promise;
|
|
|
|
expect(preloadPath).to.equal(preload);
|
|
|
|
expect(error.message).to.equal('Hello World!');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is triggered on syntax errors', async () => {
|
|
|
|
const preload = path.join(fixturesPath, 'module', 'preload-error-syntax.js');
|
|
|
|
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox,
|
|
|
|
preload
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'preload-error');
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
|
|
|
|
const [, preloadPath, error] = await promise;
|
|
|
|
expect(preloadPath).to.equal(preload);
|
|
|
|
expect(error.message).to.equal('foobar is not defined');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is triggered when preload script loading fails', async () => {
|
|
|
|
const preload = path.join(fixturesPath, 'module', 'preload-invalid.js');
|
|
|
|
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox,
|
|
|
|
preload
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'preload-error');
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
|
|
|
|
const [, preloadPath, error] = await promise;
|
|
|
|
expect(preloadPath).to.equal(preload);
|
|
|
|
expect(error.message).to.contain('preload-invalid.js');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
generateSpecs('without sandbox', false);
|
|
|
|
generateSpecs('with sandbox', true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('takeHeapSnapshot()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('works with sandboxed renderers', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
|
|
|
|
const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot');
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(filePath);
|
|
|
|
} catch (e) {
|
|
|
|
// ignore error
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await w.webContents.takeHeapSnapshot(filePath);
|
|
|
|
const stats = fs.statSync(filePath);
|
|
|
|
expect(stats.size).not.to.be.equal(0);
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with invalid file path', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
|
|
|
|
const promise = w.webContents.takeHeapSnapshot('');
|
|
|
|
return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setBackgroundThrottling()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('does not crash when allowing', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.webContents.setBackgroundThrottling(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not crash when called via BrowserWindow', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
|
|
|
|
(w as any).setBackgroundThrottling(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not crash when disallowing', () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { backgroundThrottling: true } });
|
|
|
|
|
|
|
|
w.webContents.setBackgroundThrottling(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-14 13:11:45 +00:00
|
|
|
describe('getBackgroundThrottling()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('works via getter', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
|
|
|
|
w.webContents.setBackgroundThrottling(false);
|
|
|
|
expect(w.webContents.getBackgroundThrottling()).to.equal(false);
|
|
|
|
|
|
|
|
w.webContents.setBackgroundThrottling(true);
|
|
|
|
expect(w.webContents.getBackgroundThrottling()).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works via property', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
|
|
|
|
w.webContents.backgroundThrottling = false;
|
|
|
|
expect(w.webContents.backgroundThrottling).to.equal(false);
|
|
|
|
|
|
|
|
w.webContents.backgroundThrottling = true;
|
|
|
|
expect(w.webContents.backgroundThrottling).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works via BrowserWindow', () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
|
|
|
|
(w as any).setBackgroundThrottling(false);
|
|
|
|
expect((w as any).getBackgroundThrottling()).to.equal(false);
|
|
|
|
|
|
|
|
(w as any).setBackgroundThrottling(true);
|
|
|
|
expect((w as any).getBackgroundThrottling()).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
ifdescribe(features.isPrintingEnabled())('getPrinters()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('can get printer list', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const printers = w.webContents.getPrinters();
|
|
|
|
expect(printers).to.be.an('array');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-25 18:16:58 +00:00
|
|
|
ifdescribe(features.isPrintingEnabled())('getPrintersAsync()', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('can get printer list', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const printers = await w.webContents.getPrintersAsync();
|
|
|
|
expect(printers).to.be.an('array');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
ifdescribe(features.isPrintingEnabled())('printToPDF()', () => {
|
2020-01-28 20:47:24 +00:00
|
|
|
let w: BrowserWindow;
|
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
});
|
2020-01-28 20:47:24 +00:00
|
|
|
});
|
|
|
|
|
2019-09-30 22:00:47 +00:00
|
|
|
afterEach(closeAllWindows);
|
2020-01-28 20:47:24 +00:00
|
|
|
|
|
|
|
it('rejects on incorrectly typed parameters', async () => {
|
|
|
|
const badTypes = {
|
|
|
|
landscape: [],
|
2022-05-31 06:21:25 +00:00
|
|
|
displayHeaderFooter: '123',
|
2020-01-28 20:47:24 +00:00
|
|
|
printBackground: 2,
|
2022-05-31 06:21:25 +00:00
|
|
|
scale: 'not-a-number',
|
|
|
|
pageSize: 'IAmAPageSize',
|
|
|
|
margins: 'terrible',
|
|
|
|
pageRanges: { oops: 'im-not-the-right-key' },
|
|
|
|
headerTemplate: [1, 2, 3],
|
|
|
|
footerTemplate: [4, 5, 6],
|
|
|
|
preferCSSPageSize: 'no'
|
2020-01-28 20:47:24 +00:00
|
|
|
};
|
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
await w.loadURL('data:text/html,<h1>Hello, World!</h1>');
|
|
|
|
|
2020-01-28 20:47:24 +00:00
|
|
|
// These will hard crash in Chromium unless we type-check
|
|
|
|
for (const [key, value] of Object.entries(badTypes)) {
|
|
|
|
const param = { [key]: value };
|
|
|
|
await expect(w.webContents.printToPDF(param)).to.eventually.be.rejected();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-14 01:13:34 +00:00
|
|
|
it('does not crash when called multiple times in parallel', async () => {
|
2022-10-12 14:15:49 +00:00
|
|
|
await w.loadURL('data:text/html,<h1>Hello, World!</h1>');
|
|
|
|
|
2019-10-29 02:36:29 +00:00
|
|
|
const promises = [];
|
2020-07-14 01:13:34 +00:00
|
|
|
for (let i = 0; i < 3; i++) {
|
2019-10-29 02:36:29 +00:00
|
|
|
promises.push(w.webContents.printToPDF({}));
|
|
|
|
}
|
2020-07-14 01:13:34 +00:00
|
|
|
|
2019-10-29 02:36:29 +00:00
|
|
|
const results = await Promise.all(promises);
|
|
|
|
for (const data of results) {
|
|
|
|
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty();
|
|
|
|
}
|
|
|
|
});
|
2020-07-14 01:13:34 +00:00
|
|
|
|
|
|
|
it('does not crash when called multiple times in sequence', async () => {
|
2022-10-12 14:15:49 +00:00
|
|
|
await w.loadURL('data:text/html,<h1>Hello, World!</h1>');
|
|
|
|
|
2020-07-14 01:13:34 +00:00
|
|
|
const results = [];
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
const result = await w.webContents.printToPDF({});
|
|
|
|
results.push(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const data of results) {
|
|
|
|
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty();
|
|
|
|
}
|
|
|
|
});
|
2021-01-21 23:36:52 +00:00
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
it('can print a PDF with default settings', async () => {
|
|
|
|
await w.loadURL('data:text/html,<h1>Hello, World!</h1>');
|
2021-01-21 23:36:52 +00:00
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
const data = await w.webContents.printToPDF({});
|
|
|
|
expect(data).to.be.an.instanceof(Buffer).that.is.not.empty();
|
|
|
|
});
|
2021-01-21 23:36:52 +00:00
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
it('with custom page sizes', async () => {
|
|
|
|
const paperFormats: Record<string, ElectronInternal.PageSize> = {
|
|
|
|
letter: { width: 8.5, height: 11 },
|
|
|
|
legal: { width: 8.5, height: 14 },
|
|
|
|
tabloid: { width: 11, height: 17 },
|
|
|
|
ledger: { width: 17, height: 11 },
|
|
|
|
a0: { width: 33.1, height: 46.8 },
|
|
|
|
a1: { width: 23.4, height: 33.1 },
|
|
|
|
a2: { width: 16.54, height: 23.4 },
|
|
|
|
a3: { width: 11.7, height: 16.54 },
|
|
|
|
a4: { width: 8.27, height: 11.7 },
|
|
|
|
a5: { width: 5.83, height: 8.27 },
|
|
|
|
a6: { width: 4.13, height: 5.83 }
|
|
|
|
};
|
|
|
|
|
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'print-to-pdf-small.html'));
|
|
|
|
|
|
|
|
for (const format of Object.keys(paperFormats)) {
|
|
|
|
const data = await w.webContents.printToPDF({ pageSize: format });
|
2021-01-21 23:36:52 +00:00
|
|
|
|
|
|
|
const doc = await pdfjs.getDocument(data).promise;
|
2022-10-12 14:15:49 +00:00
|
|
|
const page = await doc.getPage(1);
|
|
|
|
|
|
|
|
// page.view is [top, left, width, height].
|
|
|
|
const width = page.view[2] / 72;
|
|
|
|
const height = page.view[3] / 72;
|
|
|
|
|
|
|
|
const approxEq = (a: number, b: number, epsilon = 0.01) => Math.abs(a - b) <= epsilon;
|
|
|
|
|
|
|
|
expect(approxEq(width, paperFormats[format].width)).to.be.true();
|
|
|
|
expect(approxEq(height, paperFormats[format].height)).to.be.true();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with custom header and footer', async () => {
|
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'print-to-pdf-small.html'));
|
|
|
|
|
|
|
|
const data = await w.webContents.printToPDF({
|
|
|
|
displayHeaderFooter: true,
|
|
|
|
headerTemplate: '<div>I\'m a PDF header</div>',
|
|
|
|
footerTemplate: '<div>I\'m a PDF footer</div>'
|
|
|
|
});
|
2021-01-21 23:36:52 +00:00
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
const doc = await pdfjs.getDocument(data).promise;
|
|
|
|
const page = await doc.getPage(1);
|
2021-01-21 23:36:52 +00:00
|
|
|
|
2022-10-12 14:15:49 +00:00
|
|
|
const { items } = await page.getTextContent();
|
|
|
|
|
|
|
|
// Check that generated PDF contains a header.
|
|
|
|
const containsText = (text: RegExp) => items.some(({ str }: { str: string }) => str.match(text));
|
|
|
|
|
|
|
|
expect(containsText(/I'm a PDF header/)).to.be.true();
|
|
|
|
expect(containsText(/I'm a PDF footer/)).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('in landscape mode', async () => {
|
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'print-to-pdf-small.html'));
|
|
|
|
|
|
|
|
const data = await w.webContents.printToPDF({ landscape: true });
|
|
|
|
const doc = await pdfjs.getDocument(data).promise;
|
|
|
|
const page = await doc.getPage(1);
|
|
|
|
|
|
|
|
// page.view is [top, left, width, height].
|
|
|
|
const width = page.view[2];
|
|
|
|
const height = page.view[3];
|
|
|
|
|
|
|
|
expect(width).to.be.greaterThan(height);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with custom page ranges', async () => {
|
|
|
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'print-to-pdf-large.html'));
|
|
|
|
|
|
|
|
const data = await w.webContents.printToPDF({
|
|
|
|
pageRanges: '1-3',
|
|
|
|
landscape: true
|
2021-01-21 23:36:52 +00:00
|
|
|
});
|
2022-10-12 14:15:49 +00:00
|
|
|
|
|
|
|
const doc = await pdfjs.getDocument(data).promise;
|
|
|
|
|
|
|
|
// Check that correct # of pages are rendered.
|
|
|
|
expect(doc.numPages).to.equal(3);
|
2021-01-21 23:36:52 +00:00
|
|
|
});
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('PictureInPicture video', () => {
|
|
|
|
afterEach(closeAllWindows);
|
2021-01-07 22:17:53 +00:00
|
|
|
it('works as expected', async function () {
|
2023-02-03 11:43:42 +00:00
|
|
|
const w = new BrowserWindow({ webPreferences: { sandbox: true } });
|
|
|
|
|
|
|
|
// TODO(codebytere): figure out why this workaround is needed and remove.
|
|
|
|
// It is not germane to the actual test.
|
|
|
|
await w.loadFile(path.join(fixturesPath, 'blank.html'));
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
await w.loadFile(path.join(fixturesPath, 'api', 'picture-in-picture.html'));
|
2021-01-07 22:17:53 +00:00
|
|
|
|
2023-02-03 11:43:42 +00:00
|
|
|
await w.webContents.executeJavaScript('document.createElement(\'video\').canPlayType(\'video/webm; codecs="vp8.0"\')', true);
|
2021-01-07 22:17:53 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
const result = await w.webContents.executeJavaScript(
|
|
|
|
`runTest(${features.isPictureInPictureEnabled()})`, true);
|
|
|
|
expect(result).to.be.true();
|
2019-09-30 22:00:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-02 12:38:27 +00:00
|
|
|
describe('Shared Workers', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('can get multiple shared workers', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-10-02 12:38:27 +00:00
|
|
|
|
|
|
|
const ready = emittedOnce(ipcMain, 'ready');
|
|
|
|
w.loadFile(path.join(fixturesPath, 'api', 'shared-worker', 'shared-worker.html'));
|
|
|
|
await ready;
|
|
|
|
|
|
|
|
const sharedWorkers = w.webContents.getAllSharedWorkers();
|
|
|
|
|
|
|
|
expect(sharedWorkers).to.have.lengthOf(2);
|
|
|
|
expect(sharedWorkers[0].url).to.contain('shared-worker');
|
|
|
|
expect(sharedWorkers[1].url).to.contain('shared-worker');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can inspect a specific shared worker', async () => {
|
2021-03-01 21:52:29 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
|
2019-10-02 12:38:27 +00:00
|
|
|
|
|
|
|
const ready = emittedOnce(ipcMain, 'ready');
|
|
|
|
w.loadFile(path.join(fixturesPath, 'api', 'shared-worker', 'shared-worker.html'));
|
|
|
|
await ready;
|
|
|
|
|
|
|
|
const sharedWorkers = w.webContents.getAllSharedWorkers();
|
|
|
|
|
|
|
|
const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened');
|
|
|
|
w.webContents.inspectSharedWorkerById(sharedWorkers[0].id);
|
|
|
|
await devtoolsOpened;
|
|
|
|
|
|
|
|
const devtoolsClosed = emittedOnce(w.webContents, 'devtools-closed');
|
|
|
|
w.webContents.closeDevTools();
|
|
|
|
await devtoolsClosed;
|
|
|
|
});
|
|
|
|
});
|
2019-11-11 17:47:01 +00:00
|
|
|
|
|
|
|
describe('login event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
let server: http.Server;
|
|
|
|
let serverUrl: string;
|
|
|
|
let serverPort: number;
|
|
|
|
let proxyServer: http.Server;
|
|
|
|
let proxyServerPort: number;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
if (request.url === '/no-auth') {
|
|
|
|
return response.end('ok');
|
|
|
|
}
|
|
|
|
if (request.headers.authorization) {
|
|
|
|
response.writeHead(200, { 'Content-type': 'text/plain' });
|
|
|
|
return response.end(request.headers.authorization);
|
|
|
|
}
|
|
|
|
response
|
|
|
|
.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' })
|
2019-11-14 18:01:18 +00:00
|
|
|
.end('401');
|
2019-11-11 17:47:01 +00:00
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
serverPort = (server.address() as AddressInfo).port;
|
|
|
|
serverUrl = `http://127.0.0.1:${serverPort}`;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
proxyServer = http.createServer((request, response) => {
|
|
|
|
if (request.headers['proxy-authorization']) {
|
|
|
|
response.writeHead(200, { 'Content-type': 'text/plain' });
|
|
|
|
return response.end(request.headers['proxy-authorization']);
|
|
|
|
}
|
|
|
|
response
|
|
|
|
.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Foo"' })
|
|
|
|
.end();
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
proxyServerPort = (proxyServer.address() as AddressInfo).port;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-14 18:01:18 +00:00
|
|
|
afterEach(async () => {
|
2020-02-21 18:40:45 +00:00
|
|
|
await session.defaultSession.clearAuthCache();
|
2019-11-14 18:01:18 +00:00
|
|
|
});
|
|
|
|
|
2019-11-11 17:47:01 +00:00
|
|
|
after(() => {
|
|
|
|
server.close();
|
|
|
|
proxyServer.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is emitted when navigating', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
let eventRequest: any;
|
|
|
|
let eventAuthInfo: any;
|
|
|
|
w.webContents.on('login', (event, request, authInfo, cb) => {
|
|
|
|
eventRequest = request;
|
|
|
|
eventAuthInfo = authInfo;
|
|
|
|
event.preventDefault();
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
await w.loadURL(serverUrl);
|
2020-03-20 15:12:18 +00:00
|
|
|
const body = await w.webContents.executeJavaScript('document.documentElement.textContent');
|
2019-11-11 17:47:01 +00:00
|
|
|
expect(body).to.equal(`Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}`);
|
|
|
|
expect(eventRequest.url).to.equal(serverUrl + '/');
|
|
|
|
expect(eventAuthInfo.isProxy).to.be.false();
|
|
|
|
expect(eventAuthInfo.scheme).to.equal('basic');
|
|
|
|
expect(eventAuthInfo.host).to.equal('127.0.0.1');
|
|
|
|
expect(eventAuthInfo.port).to.equal(serverPort);
|
|
|
|
expect(eventAuthInfo.realm).to.equal('Foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is emitted when a proxy requests authorization', async () => {
|
|
|
|
const customSession = session.fromPartition(`${Math.random()}`);
|
|
|
|
await customSession.setProxy({ proxyRules: `127.0.0.1:${proxyServerPort}`, proxyBypassRules: '<-loopback>' });
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } });
|
|
|
|
let eventRequest: any;
|
|
|
|
let eventAuthInfo: any;
|
|
|
|
w.webContents.on('login', (event, request, authInfo, cb) => {
|
|
|
|
eventRequest = request;
|
|
|
|
eventAuthInfo = authInfo;
|
|
|
|
event.preventDefault();
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
await w.loadURL(`${serverUrl}/no-auth`);
|
2020-03-20 15:12:18 +00:00
|
|
|
const body = await w.webContents.executeJavaScript('document.documentElement.textContent');
|
2019-11-11 17:47:01 +00:00
|
|
|
expect(body).to.equal(`Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}`);
|
|
|
|
expect(eventRequest.url).to.equal(`${serverUrl}/no-auth`);
|
|
|
|
expect(eventAuthInfo.isProxy).to.be.true();
|
|
|
|
expect(eventAuthInfo.scheme).to.equal('basic');
|
|
|
|
expect(eventAuthInfo.host).to.equal('127.0.0.1');
|
|
|
|
expect(eventAuthInfo.port).to.equal(proxyServerPort);
|
|
|
|
expect(eventAuthInfo.realm).to.equal('Foo');
|
|
|
|
});
|
2019-11-14 18:01:18 +00:00
|
|
|
|
|
|
|
it('cancels authentication when callback is called with no arguments', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.webContents.on('login', (event, request, authInfo, cb) => {
|
|
|
|
event.preventDefault();
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
await w.loadURL(serverUrl);
|
2020-03-20 15:12:18 +00:00
|
|
|
const body = await w.webContents.executeJavaScript('document.documentElement.textContent');
|
2019-11-14 18:01:18 +00:00
|
|
|
expect(body).to.equal('401');
|
|
|
|
});
|
2019-11-11 17:47:01 +00:00
|
|
|
});
|
2020-04-13 15:37:41 +00:00
|
|
|
|
2021-06-30 17:53:10 +00:00
|
|
|
describe('page-title-updated event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('is emitted with a full title for pages with no navigation', async () => {
|
2022-01-06 17:28:03 +00:00
|
|
|
const bw = new BrowserWindow({ show: false });
|
2021-06-30 17:53:10 +00:00
|
|
|
await bw.loadURL('about:blank');
|
|
|
|
bw.webContents.executeJavaScript('child = window.open("", "", "show=no"); null');
|
|
|
|
const [, child] = await emittedOnce(app, 'web-contents-created');
|
|
|
|
bw.webContents.executeJavaScript('child.document.title = "new title"');
|
|
|
|
const [, title] = await emittedOnce(child, 'page-title-updated');
|
|
|
|
expect(title).to.equal('new title');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-17 12:19:17 +00:00
|
|
|
describe('crashed event', () => {
|
|
|
|
it('does not crash main process when destroying WebContents in it', (done) => {
|
|
|
|
const contents = (webContents as any).create({ nodeIntegration: true });
|
|
|
|
contents.once('crashed', () => {
|
|
|
|
contents.destroy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
contents.loadURL('about:blank').then(() => contents.forcefullyCrashRenderer());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-21 23:52:49 +00:00
|
|
|
describe('context-menu event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('emits when right-clicked in page', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadFile(path.join(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
|
|
|
|
const promise = emittedOnce(w.webContents, 'context-menu');
|
|
|
|
|
|
|
|
// Simulate right-click to create context-menu event.
|
|
|
|
const opts = { x: 0, y: 0, button: 'right' as any };
|
|
|
|
w.webContents.sendInputEvent({ ...opts, type: 'mouseDown' });
|
|
|
|
w.webContents.sendInputEvent({ ...opts, type: 'mouseUp' });
|
|
|
|
|
|
|
|
const [, params] = await promise;
|
|
|
|
|
|
|
|
expect(params.pageURL).to.equal(w.webContents.getURL());
|
|
|
|
expect(params.frame).to.be.an('object');
|
|
|
|
expect(params.x).to.be.a('number');
|
|
|
|
expect(params.y).to.be.a('number');
|
|
|
|
});
|
|
|
|
});
|
2022-09-13 17:49:34 +00:00
|
|
|
|
2022-09-16 23:22:59 +00:00
|
|
|
describe('close() method', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('closes when close() is called', async () => {
|
|
|
|
const w = (webContents as any).create() as WebContents;
|
|
|
|
const destroyed = emittedOnce(w, 'destroyed');
|
|
|
|
w.close();
|
|
|
|
await destroyed;
|
|
|
|
expect(w.isDestroyed()).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('closes when close() is called after loading a page', async () => {
|
|
|
|
const w = (webContents as any).create() as WebContents;
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const destroyed = emittedOnce(w, 'destroyed');
|
|
|
|
w.close();
|
|
|
|
await destroyed;
|
|
|
|
expect(w.isDestroyed()).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be GCed before loading a page', async () => {
|
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
|
|
|
let registry: FinalizationRegistry<unknown> | null = null;
|
|
|
|
const cleanedUp = new Promise<number>(resolve => {
|
|
|
|
registry = new FinalizationRegistry(resolve as any);
|
|
|
|
});
|
|
|
|
(() => {
|
|
|
|
const w = (webContents as any).create() as WebContents;
|
|
|
|
registry!.register(w, 42);
|
|
|
|
})();
|
|
|
|
const i = setInterval(() => v8Util.requestGarbageCollectionForTesting(), 100);
|
|
|
|
defer(() => clearInterval(i));
|
|
|
|
expect(await cleanedUp).to.equal(42);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('causes its parent browserwindow to be closed', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
const closed = emittedOnce(w, 'closed');
|
|
|
|
w.webContents.close();
|
|
|
|
await closed;
|
|
|
|
expect(w.isDestroyed()).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ignores beforeunload if waitForBeforeUnload not specified', async () => {
|
|
|
|
const w = (webContents as any).create() as WebContents;
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
await w.executeJavaScript('window.onbeforeunload = () => "hello"; null');
|
|
|
|
w.on('will-prevent-unload', () => { throw new Error('unexpected will-prevent-unload'); });
|
|
|
|
const destroyed = emittedOnce(w, 'destroyed');
|
|
|
|
w.close();
|
|
|
|
await destroyed;
|
|
|
|
expect(w.isDestroyed()).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('runs beforeunload if waitForBeforeUnload is specified', async () => {
|
|
|
|
const w = (webContents as any).create() as WebContents;
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
await w.executeJavaScript('window.onbeforeunload = () => "hello"; null');
|
|
|
|
const willPreventUnload = emittedOnce(w, 'will-prevent-unload');
|
|
|
|
w.close({ waitForBeforeUnload: true });
|
|
|
|
await willPreventUnload;
|
|
|
|
expect(w.isDestroyed()).to.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overriding beforeunload prevention results in webcontents close', async () => {
|
|
|
|
const w = (webContents as any).create() as WebContents;
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
await w.executeJavaScript('window.onbeforeunload = () => "hello"; null');
|
|
|
|
w.once('will-prevent-unload', e => e.preventDefault());
|
|
|
|
const destroyed = emittedOnce(w, 'destroyed');
|
|
|
|
w.close({ waitForBeforeUnload: true });
|
|
|
|
await destroyed;
|
|
|
|
expect(w.isDestroyed()).to.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-13 17:49:34 +00:00
|
|
|
describe('content-bounds-updated event', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
it('emits when moveTo is called', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript('window.moveTo(100, 100)', true);
|
|
|
|
const [, rect] = await emittedOnce(w.webContents, 'content-bounds-updated');
|
|
|
|
const { width, height } = w.getBounds();
|
|
|
|
expect(rect).to.deep.equal({
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
});
|
|
|
|
await new Promise(setImmediate);
|
|
|
|
expect(w.getBounds().x).to.equal(100);
|
|
|
|
expect(w.getBounds().y).to.equal(100);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits when resizeTo is called', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.executeJavaScript('window.resizeTo(100, 100)', true);
|
|
|
|
const [, rect] = await emittedOnce(w.webContents, 'content-bounds-updated');
|
|
|
|
const { x, y } = w.getBounds();
|
|
|
|
expect(rect).to.deep.equal({
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
});
|
|
|
|
await new Promise(setImmediate);
|
|
|
|
expect({
|
|
|
|
width: w.getBounds().width,
|
|
|
|
height: w.getBounds().height
|
|
|
|
}).to.deep.equal(process.platform === 'win32' ? {
|
|
|
|
// The width is reported as being larger on Windows? I'm not sure why
|
|
|
|
// this is.
|
|
|
|
width: 136,
|
|
|
|
height: 100
|
|
|
|
} : {
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not change window bounds if cancelled', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
const { width, height } = w.getBounds();
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
w.webContents.once('content-bounds-updated', e => e.preventDefault());
|
|
|
|
await w.webContents.executeJavaScript('window.resizeTo(100, 100)', true);
|
|
|
|
await new Promise(setImmediate);
|
|
|
|
expect(w.getBounds().width).to.equal(width);
|
|
|
|
expect(w.getBounds().height).to.equal(height);
|
|
|
|
});
|
|
|
|
});
|
2019-04-02 01:28:11 +00:00
|
|
|
});
|