feat: Allow WebContentsView to accept webContents object. (#42086)

feat: Allow WebContentsView to accept webContents object
This commit is contained in:
Krzysztof Halwa 2024-05-30 21:45:35 +02:00 committed by GitHub
parent d92a80dfe3
commit 85df2a86dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 69 additions and 2 deletions

View file

@ -1,8 +1,10 @@
import { closeAllWindows } from './lib/window-helpers';
import { expect } from 'chai';
import { BaseWindow, View, WebContentsView } from 'electron/main';
import { BaseWindow, View, WebContentsView, webContents } from 'electron/main';
import { once } from 'node:events';
import { defer } from './lib/spec-helpers';
import { BrowserWindow } from 'electron';
describe('WebContentsView', () => {
afterEach(closeAllWindows);
@ -17,6 +19,48 @@ describe('WebContentsView', () => {
new WebContentsView({});
});
it('accepts existing webContents object', async () => {
const currentWebContentsCount = webContents.getAllWebContents().length;
const wc = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
defer(() => wc.destroy());
await wc.loadURL('about:blank');
const webContentsView = new WebContentsView({
webContents: wc
});
expect(webContentsView.webContents).to.eq(wc);
expect(webContents.getAllWebContents().length).to.equal(currentWebContentsCount + 1, 'expected only single webcontents to be created');
});
it('should throw error when created with already attached webContents to BrowserWindow', () => {
const browserWindow = new BrowserWindow();
defer(() => browserWindow.webContents.destroy());
const webContentsView = new WebContentsView();
defer(() => webContentsView.webContents.destroy());
browserWindow.contentView.addChildView(webContentsView);
defer(() => browserWindow.contentView.removeChildView(webContentsView));
expect(() => new WebContentsView({
webContents: webContentsView.webContents
})).to.throw('options.webContents is already attached to a window');
});
it('should throw error when created with already attached webContents to other WebContentsView', () => {
const browserWindow = new BrowserWindow();
const webContentsView = new WebContentsView();
defer(() => webContentsView.webContents.destroy());
webContentsView.webContents.loadURL('about:blank');
expect(() => new WebContentsView({
webContents: browserWindow.webContents
})).to.throw('options.webContents is already attached to a window');
});
it('can be used as content view', () => {
const w = new BaseWindow({ show: false });
w.setContentView(new WebContentsView());