Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use webContents.create() in type-safe way #37308

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/api-browser-view-spec.ts
Expand Up @@ -41,7 +41,7 @@ describe('BrowserView module', () => {
});

it('can be created with an existing webContents', async () => {
const wc = (webContents as any).create({ sandbox: true });
const wc = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
await wc.loadURL('about:blank');

view = new BrowserView({ webContents: wc } as any);
Expand Down
2 changes: 1 addition & 1 deletion spec/api-browser-window-spec.ts
Expand Up @@ -2184,7 +2184,7 @@ describe('BrowserWindow module', () => {
});

it('returns null for webContents without a BrowserWindow', () => {
const contents = (webContents as any).create({});
const contents = (webContents as typeof ElectronInternal.WebContents).create();
try {
expect(BrowserWindow.fromWebContents(contents)).to.be.null('BrowserWindow.fromWebContents(contents)');
} finally {
Expand Down
2 changes: 1 addition & 1 deletion spec/api-ipc-renderer-spec.ts
Expand Up @@ -135,7 +135,7 @@ describe('ipcRenderer module', () => {
const payload = 'Hello World!';

before(async () => {
contents = (webContents as any).create({
contents = (webContents as typeof ElectronInternal.WebContents).create({
preload: path.join(fixtures, 'module', 'preload-ipc-ping-pong.js'),
...webPreferences
});
Expand Down
13 changes: 10 additions & 3 deletions spec/api-protocol-spec.ts
Expand Up @@ -73,7 +73,7 @@ function defer (): Promise<any> & {resolve: Function, reject: Function} {
describe('protocol module', () => {
let contents: WebContents = null as unknown as WebContents;
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
before(() => { contents = (webContents as any).create({ sandbox: true }); });
before(() => { contents = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true }); });
after(() => contents.destroy());

async function ajax (url: string, options = {}) {
Expand Down Expand Up @@ -952,7 +952,10 @@ describe('protocol module', () => {
callback('');
});

const newContents: WebContents = (webContents as any).create({ nodeIntegration: true, contextIsolation: false });
const newContents = (webContents as typeof ElectronInternal.WebContents).create({
nodeIntegration: true,
contextIsolation: false
});
const consoleMessages: string[] = [];
newContents.on('console-message', (e, level, message) => consoleMessages.push(message));
try {
Expand Down Expand Up @@ -1053,7 +1056,11 @@ describe('protocol module', () => {
await registerStreamProtocol(standardScheme, protocolHandler);
await registerStreamProtocol('stream', protocolHandler);

const newContents: WebContents = (webContents as any).create({ nodeIntegration: true, contextIsolation: false });
const newContents = (webContents as typeof ElectronInternal.WebContents).create({
nodeIntegration: true,
contextIsolation: false
});

try {
newContents.loadURL(testingScheme + '://fake-host');
const [, response] = await emittedOnce(ipcMain, 'result');
Expand Down
2 changes: 1 addition & 1 deletion spec/api-service-workers-spec.ts
Expand Up @@ -39,7 +39,7 @@ describe('session.serviceWorkers', () => {
});
});

w = (webContents as any).create({ session: ses });
w = (webContents as typeof ElectronInternal.WebContents).create({ session: ses });
});

afterEach(async () => {
Expand Down
20 changes: 10 additions & 10 deletions spec/api-web-contents-spec.ts
Expand Up @@ -3,7 +3,7 @@ import { AddressInfo } from 'net';
import * as path from 'path';
import * as fs from 'fs';
import * as http from 'http';
import { BrowserWindow, ipcMain, webContents, session, WebContents, app, BrowserView } from 'electron/main';
import { BrowserWindow, ipcMain, webContents, session, app, BrowserView } from 'electron/main';
import { emittedOnce } from './lib/events-helpers';
import { closeAllWindows } from './lib/window-helpers';
import { ifdescribe, delay, defer, waitUntil } from './lib/spec-helpers';
Expand Down Expand Up @@ -48,11 +48,11 @@ describe('webContents module', () => {

describe('fromFrame()', () => {
it('returns WebContents for mainFrame', () => {
const contents = (webContents as any).create() as WebContents;
const contents = (webContents as typeof ElectronInternal.WebContents).create();
expect(webContents.fromFrame(contents.mainFrame)).to.equal(contents);
});
it('returns undefined for disposed frame', async () => {
const contents = (webContents as any).create() as WebContents;
const contents = (webContents as typeof ElectronInternal.WebContents).create();
const { mainFrame } = contents;
contents.destroy();
await waitUntil(() => typeof webContents.fromFrame(mainFrame) === 'undefined');
Expand Down Expand Up @@ -1549,7 +1549,7 @@ describe('webContents module', () => {
// is fine to retry this test for a few times.
this.retries(3);

const contents = (webContents as any).create() as WebContents;
const contents = (webContents as typeof ElectronInternal.WebContents).create();
const originalEmit = contents.emit.bind(contents);
contents.emit = (...args) => { return originalEmit(...args); };
contents.once(e.name as any, () => contents.destroy());
Expand Down Expand Up @@ -2261,15 +2261,15 @@ describe('webContents module', () => {
afterEach(closeAllWindows);

it('closes when close() is called', async () => {
const w = (webContents as any).create() as WebContents;
const w = (webContents as typeof ElectronInternal.WebContents).create();
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;
const w = (webContents as typeof ElectronInternal.WebContents).create();
await w.loadURL('about:blank');
const destroyed = emittedOnce(w, 'destroyed');
w.close();
Expand All @@ -2284,7 +2284,7 @@ describe('webContents module', () => {
registry = new FinalizationRegistry(resolve as any);
});
(() => {
const w = (webContents as any).create() as WebContents;
const w = (webContents as typeof ElectronInternal.WebContents).create();
registry!.register(w, 42);
})();
const i = setInterval(() => v8Util.requestGarbageCollectionForTesting(), 100);
Expand All @@ -2302,7 +2302,7 @@ describe('webContents module', () => {
});

it('ignores beforeunload if waitForBeforeUnload not specified', async () => {
const w = (webContents as any).create() as WebContents;
const w = (webContents as typeof ElectronInternal.WebContents).create();
await w.loadURL('about:blank');
await w.executeJavaScript('window.onbeforeunload = () => "hello"; null');
w.on('will-prevent-unload', () => { throw new Error('unexpected will-prevent-unload'); });
Expand All @@ -2313,7 +2313,7 @@ describe('webContents module', () => {
});

it('runs beforeunload if waitForBeforeUnload is specified', async () => {
const w = (webContents as any).create() as WebContents;
const w = (webContents as typeof ElectronInternal.WebContents).create();
await w.loadURL('about:blank');
await w.executeJavaScript('window.onbeforeunload = () => "hello"; null');
const willPreventUnload = emittedOnce(w, 'will-prevent-unload');
Expand All @@ -2323,7 +2323,7 @@ describe('webContents module', () => {
});

it('overriding beforeunload prevention results in webcontents close', async () => {
const w = (webContents as any).create() as WebContents;
const w = (webContents as typeof ElectronInternal.WebContents).create();
await w.loadURL('about:blank');
await w.executeJavaScript('window.onbeforeunload = () => "hello"; null');
w.once('will-prevent-unload', e => e.preventDefault());
Expand Down
4 changes: 2 additions & 2 deletions spec/api-web-request-spec.ts
Expand Up @@ -52,7 +52,7 @@ describe('webRequest module', () => {
let contents: WebContents = null as unknown as WebContents;
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
before(async () => {
contents = (webContents as any).create({ sandbox: true });
contents = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
await contents.loadFile(path.join(fixturesPath, 'pages', 'fetch.html'));
});
after(() => contents.destroy());
Expand Down Expand Up @@ -529,7 +529,7 @@ describe('webRequest module', () => {
}
});

const contents = (webContents as any).create({
const contents = (webContents as typeof ElectronInternal.WebContents).create({
session: ses,
nodeIntegration: true,
webSecurity: false,
Expand Down
8 changes: 4 additions & 4 deletions spec/chromium-spec.ts
Expand Up @@ -1472,7 +1472,7 @@ describe('chromium features', () => {
});

beforeEach(() => {
contents = (webContents as any).create({
contents = (webContents as typeof ElectronInternal.WebContents).create({
nodeIntegration: true,
contextIsolation: false
});
Expand Down Expand Up @@ -1603,7 +1603,7 @@ describe('chromium features', () => {
});

it('default value allows websql', async () => {
contents = (webContents as any).create({
contents = (webContents as typeof ElectronInternal.WebContents).create({
session: sqlSession,
nodeIntegration: true,
contextIsolation: false
Expand All @@ -1614,7 +1614,7 @@ describe('chromium features', () => {
});

it('when set to false can disallow websql', async () => {
contents = (webContents as any).create({
contents = (webContents as typeof ElectronInternal.WebContents).create({
session: sqlSession,
nodeIntegration: true,
enableWebSQL: false,
Expand All @@ -1626,7 +1626,7 @@ describe('chromium features', () => {
});

it('when set to false does not disable indexedDB', async () => {
contents = (webContents as any).create({
contents = (webContents as typeof ElectronInternal.WebContents).create({
session: sqlSession,
nodeIntegration: true,
enableWebSQL: false,
Expand Down
@@ -1,6 +1,6 @@
const { app, webContents } = require('electron');
app.whenReady().then(function () {
webContents.create({});
webContents.create();

app.quit();
});
4 changes: 2 additions & 2 deletions spec/node-spec.ts
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';
import * as util from 'util';
import { emittedOnce } from './lib/events-helpers';
import { getRemoteContext, ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
import { webContents, WebContents } from 'electron/main';
import { webContents } from 'electron/main';
import { EventEmitter } from 'stream';

const features = process._linkedBinding('electron_common_features');
Expand Down Expand Up @@ -780,7 +780,7 @@ describe('node feature', () => {
// NOTE: temporary debug logging to try to catch flake.
child.stderr.on('data', (m) => console.log(m.toString()));
child.stdout.on('data', (m) => console.log(m.toString()));
const w = (webContents as any).create({}) as WebContents;
const w = (webContents as typeof ElectronInternal.WebContents).create();
w.loadURL('about:blank')
.then(() => w.executeJavaScript(`new Promise(resolve => {
const connection = new WebSocket(${JSON.stringify(match[1])})
Expand Down
2 changes: 1 addition & 1 deletion typings/internal-electron.d.ts
Expand Up @@ -282,7 +282,7 @@ declare namespace ElectronInternal {
}

class WebContents extends Electron.WebContents {
static create(opts: Electron.WebPreferences): Electron.WebContents;
static create(opts?: Electron.WebPreferences): Electron.WebContents;
}
}

Expand Down