2019-11-07 03:15:55 +03:00
|
|
|
import { expect } from 'chai';
|
2019-07-24 18:44:24 +03:00
|
|
|
import * as path from 'path';
|
2022-03-28 19:47:08 +03:00
|
|
|
import { BrowserView, BrowserWindow, screen, webContents } from 'electron/main';
|
2023-01-26 00:01:25 +03:00
|
|
|
import { closeWindow } from './lib/window-helpers';
|
|
|
|
import { defer, ifit, startRemoteControlApp } from './lib/spec-helpers';
|
|
|
|
import { areColorsSimilar, captureScreen, getPixelColor } from './lib/screen-helpers';
|
2023-02-24 02:53:53 +03:00
|
|
|
import { once } from 'events';
|
2019-08-06 20:27:33 +03:00
|
|
|
|
2017-10-27 03:11:12 +03:00
|
|
|
describe('BrowserView module', () => {
|
2022-08-16 22:23:13 +03:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures');
|
2018-11-27 04:39:03 +03:00
|
|
|
|
2019-07-24 18:44:24 +03:00
|
|
|
let w: BrowserWindow;
|
|
|
|
let view: BrowserView;
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
beforeEach(() => {
|
2020-07-09 18:48:39 +03:00
|
|
|
expect(webContents.getAllWebContents()).to.have.length(0);
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-24 18:44:24 +03:00
|
|
|
afterEach(async () => {
|
2023-02-24 02:53:53 +03:00
|
|
|
const p = once(w.webContents, 'destroyed');
|
2019-10-04 03:30:44 +03:00
|
|
|
await closeWindow(w);
|
2020-07-09 18:48:39 +03:00
|
|
|
w = null as any;
|
|
|
|
await p;
|
2019-10-04 03:30:44 +03:00
|
|
|
|
2022-09-01 03:40:02 +03:00
|
|
|
if (view && view.webContents) {
|
2023-02-24 02:53:53 +03:00
|
|
|
const p = once(view.webContents, 'destroyed');
|
2022-12-15 00:07:38 +03:00
|
|
|
view.webContents.destroy();
|
2020-07-09 18:48:39 +03:00
|
|
|
view = null as any;
|
|
|
|
await p;
|
2017-04-13 00:52:07 +03:00
|
|
|
}
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
|
2020-07-09 18:48:39 +03:00
|
|
|
expect(webContents.getAllWebContents()).to.have.length(0);
|
2018-03-15 10:15:56 +03:00
|
|
|
});
|
|
|
|
|
2020-12-16 02:52:43 +03:00
|
|
|
it('can be created with an existing webContents', async () => {
|
2023-02-16 17:41:41 +03:00
|
|
|
const wc = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
|
2020-12-16 02:52:43 +03:00
|
|
|
await wc.loadURL('about:blank');
|
|
|
|
|
|
|
|
view = new BrowserView({ webContents: wc } as any);
|
|
|
|
|
|
|
|
expect(view.webContents.getURL()).to.equal('about:blank');
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
describe('BrowserView.setBackgroundColor()', () => {
|
|
|
|
it('does not throw for valid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
view.setBackgroundColor('#000');
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
it('throws for invalid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
2018-06-18 01:56:04 +03:00
|
|
|
expect(() => {
|
2019-07-24 18:44:24 +03:00
|
|
|
view.setBackgroundColor(null as any);
|
2018-06-18 01:56:04 +03:00
|
|
|
}).to.throw(/conversion failure/);
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
});
|
2022-03-28 19:47:08 +03:00
|
|
|
|
|
|
|
// Linux and arm64 platforms (WOA and macOS) do not return any capture sources
|
2022-11-21 18:24:26 +03:00
|
|
|
ifit(process.platform === 'darwin' && process.arch === 'x64')('sets the background color to transparent if none is set', async () => {
|
2022-03-28 19:47:08 +03:00
|
|
|
const display = screen.getPrimaryDisplay();
|
|
|
|
const WINDOW_BACKGROUND_COLOR = '#55ccbb';
|
|
|
|
|
|
|
|
w.show();
|
|
|
|
w.setBounds(display.bounds);
|
|
|
|
w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
|
|
|
|
view = new BrowserView();
|
|
|
|
view.setBounds(display.bounds);
|
|
|
|
w.setBrowserView(view);
|
|
|
|
await view.webContents.loadURL('data:text/html,hello there');
|
|
|
|
|
|
|
|
const screenCapture = await captureScreen();
|
|
|
|
const centerColor = getPixelColor(screenCapture, {
|
|
|
|
x: display.size.width / 2,
|
|
|
|
y: display.size.height / 2
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(areColorsSimilar(centerColor, WINDOW_BACKGROUND_COLOR)).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Linux and arm64 platforms (WOA and macOS) do not return any capture sources
|
2022-11-21 18:24:26 +03:00
|
|
|
ifit(process.platform === 'darwin' && process.arch === 'x64')('successfully applies the background color', async () => {
|
2022-03-28 19:47:08 +03:00
|
|
|
const WINDOW_BACKGROUND_COLOR = '#55ccbb';
|
|
|
|
const VIEW_BACKGROUND_COLOR = '#ff00ff';
|
|
|
|
const display = screen.getPrimaryDisplay();
|
|
|
|
|
|
|
|
w.show();
|
|
|
|
w.setBounds(display.bounds);
|
|
|
|
w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
|
|
|
|
await w.loadURL('about:blank');
|
|
|
|
|
|
|
|
view = new BrowserView();
|
|
|
|
view.setBounds(display.bounds);
|
|
|
|
w.setBrowserView(view);
|
|
|
|
w.setBackgroundColor(VIEW_BACKGROUND_COLOR);
|
|
|
|
await view.webContents.loadURL('data:text/html,hello there');
|
|
|
|
|
|
|
|
const screenCapture = await captureScreen();
|
|
|
|
const centerColor = getPixelColor(screenCapture, {
|
|
|
|
x: display.size.width / 2,
|
|
|
|
y: display.size.height / 2
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(areColorsSimilar(centerColor, VIEW_BACKGROUND_COLOR)).to.be.true();
|
|
|
|
});
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
describe('BrowserView.setAutoResize()', () => {
|
|
|
|
it('does not throw for valid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
2017-04-12 14:40:31 +03:00
|
|
|
view.setAutoResize({});
|
|
|
|
view.setAutoResize({ width: true, height: false });
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
it('throws for invalid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
2018-06-18 01:56:04 +03:00
|
|
|
expect(() => {
|
2019-07-24 18:44:24 +03:00
|
|
|
view.setAutoResize(null as any);
|
2018-06-18 01:56:04 +03:00
|
|
|
}).to.throw(/conversion failure/);
|
2017-04-12 14:40:31 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
describe('BrowserView.setBounds()', () => {
|
|
|
|
it('does not throw for valid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
view.setBounds({ x: 0, y: 0, width: 1, height: 1 });
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
it('throws for invalid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
2018-06-18 01:56:04 +03:00
|
|
|
expect(() => {
|
2019-07-24 18:44:24 +03:00
|
|
|
view.setBounds(null as any);
|
2018-06-18 01:56:04 +03:00
|
|
|
}).to.throw(/conversion failure/);
|
|
|
|
expect(() => {
|
2019-07-24 18:44:24 +03:00
|
|
|
view.setBounds({} as any);
|
2018-06-18 01:56:04 +03:00
|
|
|
}).to.throw(/conversion failure/);
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-30 05:43:05 +03:00
|
|
|
describe('BrowserView.getBounds()', () => {
|
2022-08-29 18:53:03 +03:00
|
|
|
it('returns correct bounds on a framed window', () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
const bounds = { x: 10, y: 20, width: 30, height: 40 };
|
|
|
|
view.setBounds(bounds);
|
|
|
|
expect(view.getBounds()).to.deep.equal(bounds);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns correct bounds on a frameless window', () => {
|
2019-07-30 05:43:05 +03:00
|
|
|
view = new BrowserView();
|
|
|
|
const bounds = { x: 10, y: 20, width: 30, height: 40 };
|
|
|
|
view.setBounds(bounds);
|
|
|
|
expect(view.getBounds()).to.deep.equal(bounds);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
describe('BrowserWindow.setBrowserView()', () => {
|
|
|
|
it('does not throw for valid args', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
w.setBrowserView(view);
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
it('does not throw if called multiple times with same view', () => {
|
2017-04-13 00:52:07 +03:00
|
|
|
view = new BrowserView();
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
w.setBrowserView(view);
|
|
|
|
w.setBrowserView(view);
|
|
|
|
w.setBrowserView(view);
|
|
|
|
});
|
|
|
|
});
|
2017-06-22 02:21:28 +03:00
|
|
|
|
2017-10-27 21:44:41 +03:00
|
|
|
describe('BrowserWindow.getBrowserView()', () => {
|
|
|
|
it('returns the set view', () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
w.setBrowserView(view);
|
2018-06-18 01:56:04 +03:00
|
|
|
|
2018-10-02 04:56:31 +03:00
|
|
|
const view2 = w.getBrowserView();
|
2019-07-24 18:44:24 +03:00
|
|
|
expect(view2!.webContents.id).to.equal(view.webContents.id);
|
2017-10-27 21:44:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if none is set', () => {
|
2018-10-02 04:56:31 +03:00
|
|
|
const view = w.getBrowserView();
|
2019-07-24 18:44:24 +03:00
|
|
|
expect(view).to.be.null('view');
|
2017-10-27 21:44:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-22 04:49:26 +03:00
|
|
|
describe('BrowserWindow.addBrowserView()', () => {
|
|
|
|
it('does not throw for valid args', () => {
|
2019-07-24 18:44:24 +03:00
|
|
|
const view1 = new BrowserView();
|
2022-12-15 00:07:38 +03:00
|
|
|
defer(() => view1.webContents.destroy());
|
2018-12-22 04:49:26 +03:00
|
|
|
w.addBrowserView(view1);
|
2020-07-09 18:48:39 +03:00
|
|
|
defer(() => w.removeBrowserView(view1));
|
2019-07-24 18:44:24 +03:00
|
|
|
const view2 = new BrowserView();
|
2022-12-15 00:07:38 +03:00
|
|
|
defer(() => view2.webContents.destroy());
|
2018-12-22 04:49:26 +03:00
|
|
|
w.addBrowserView(view2);
|
2020-07-09 18:48:39 +03:00
|
|
|
defer(() => w.removeBrowserView(view2));
|
2018-12-22 04:49:26 +03:00
|
|
|
});
|
2020-08-26 06:04:13 +03:00
|
|
|
|
2018-12-22 04:49:26 +03:00
|
|
|
it('does not throw if called multiple times with same view', () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
w.addBrowserView(view);
|
|
|
|
w.addBrowserView(view);
|
|
|
|
w.addBrowserView(view);
|
|
|
|
});
|
2020-08-26 06:04:13 +03:00
|
|
|
|
2021-11-15 10:24:22 +03:00
|
|
|
it('does not crash if the BrowserView webContents are destroyed prior to window addition', () => {
|
2020-08-26 06:04:13 +03:00
|
|
|
expect(() => {
|
|
|
|
const view1 = new BrowserView();
|
2022-12-15 00:07:38 +03:00
|
|
|
view1.webContents.destroy();
|
2020-08-26 06:04:13 +03:00
|
|
|
w.addBrowserView(view1);
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
2021-01-05 03:34:22 +03:00
|
|
|
|
2021-11-15 10:24:22 +03:00
|
|
|
it('does not crash if the webContents is destroyed after a URL is loaded', () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
expect(async () => {
|
|
|
|
view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
|
|
|
|
await view.webContents.loadURL('data:text/html,hello there');
|
|
|
|
view.webContents.destroy();
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
|
2021-01-05 03:34:22 +03:00
|
|
|
it('can handle BrowserView reparenting', async () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
|
|
|
|
w.addBrowserView(view);
|
|
|
|
view.webContents.loadURL('about:blank');
|
2023-02-24 02:53:53 +03:00
|
|
|
await once(view.webContents, 'did-finish-load');
|
2021-01-05 03:34:22 +03:00
|
|
|
|
|
|
|
const w2 = new BrowserWindow({ show: false });
|
|
|
|
w2.addBrowserView(view);
|
|
|
|
|
|
|
|
w.close();
|
|
|
|
|
|
|
|
view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
|
2023-02-24 02:53:53 +03:00
|
|
|
await once(view.webContents, 'did-finish-load');
|
2021-01-05 03:34:22 +03:00
|
|
|
|
|
|
|
// Clean up - the afterEach hook assumes the webContents on w is still alive.
|
|
|
|
w = new BrowserWindow({ show: false });
|
|
|
|
w2.close();
|
|
|
|
w2.destroy();
|
|
|
|
});
|
2018-12-22 04:49:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('BrowserWindow.removeBrowserView()', () => {
|
|
|
|
it('does not throw if called multiple times with same view', () => {
|
2020-08-26 06:04:13 +03:00
|
|
|
expect(() => {
|
|
|
|
view = new BrowserView();
|
|
|
|
w.addBrowserView(view);
|
|
|
|
w.removeBrowserView(view);
|
|
|
|
w.removeBrowserView(view);
|
|
|
|
}).to.not.throw();
|
2018-12-22 04:49:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('BrowserWindow.getBrowserViews()', () => {
|
|
|
|
it('returns same views as was added', () => {
|
2019-07-24 18:44:24 +03:00
|
|
|
const view1 = new BrowserView();
|
2022-12-15 00:07:38 +03:00
|
|
|
defer(() => view1.webContents.destroy());
|
2018-12-22 04:49:26 +03:00
|
|
|
w.addBrowserView(view1);
|
2020-07-09 18:48:39 +03:00
|
|
|
defer(() => w.removeBrowserView(view1));
|
2019-07-24 18:44:24 +03:00
|
|
|
const view2 = new BrowserView();
|
2022-12-15 00:07:38 +03:00
|
|
|
defer(() => view2.webContents.destroy());
|
2018-12-22 04:49:26 +03:00
|
|
|
w.addBrowserView(view2);
|
2020-07-09 18:48:39 +03:00
|
|
|
defer(() => w.removeBrowserView(view2));
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2018-12-22 04:49:26 +03:00
|
|
|
const views = w.getBrowserViews();
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(views).to.have.lengthOf(2);
|
2018-12-22 04:49:26 +03:00
|
|
|
expect(views[0].webContents.id).to.equal(view1.webContents.id);
|
|
|
|
expect(views[1].webContents.id).to.equal(view2.webContents.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-10 10:23:35 +03:00
|
|
|
describe('BrowserWindow.setTopBrowserView()', () => {
|
|
|
|
it('should throw an error when a BrowserView is not attached to the window', () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
expect(() => {
|
|
|
|
w.setTopBrowserView(view);
|
|
|
|
}).to.throw(/is not attached/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when a BrowserView is attached to some other window', () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
|
|
|
|
const win2 = new BrowserWindow();
|
|
|
|
|
|
|
|
w.addBrowserView(view);
|
|
|
|
view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
|
|
|
|
win2.addBrowserView(view);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
w.setTopBrowserView(view);
|
|
|
|
}).to.throw(/is not attached/);
|
|
|
|
|
|
|
|
win2.close();
|
|
|
|
win2.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-27 03:05:15 +03:00
|
|
|
describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
|
|
|
|
it('points to owning window', () => {
|
2017-06-22 02:21:28 +03:00
|
|
|
view = new BrowserView();
|
2019-07-24 18:44:24 +03:00
|
|
|
expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
|
2018-06-18 01:56:04 +03:00
|
|
|
|
2017-06-22 02:21:28 +03:00
|
|
|
w.setBrowserView(view);
|
2018-06-18 01:56:04 +03:00
|
|
|
expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
|
|
|
|
|
2017-06-22 02:21:28 +03:00
|
|
|
w.setBrowserView(null);
|
2019-07-24 18:44:24 +03:00
|
|
|
expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
|
2017-06-22 02:21:28 +03:00
|
|
|
});
|
|
|
|
});
|
2017-07-24 06:32:30 +03:00
|
|
|
|
2020-07-09 18:48:39 +03:00
|
|
|
describe('shutdown behavior', () => {
|
|
|
|
it('does not crash on exit', async () => {
|
|
|
|
const rc = await startRemoteControlApp();
|
|
|
|
await rc.remotely(() => {
|
|
|
|
const { BrowserView, app } = require('electron');
|
|
|
|
new BrowserView({}) // eslint-disable-line
|
|
|
|
setTimeout(() => {
|
|
|
|
app.quit();
|
|
|
|
});
|
|
|
|
});
|
2023-02-24 02:53:53 +03:00
|
|
|
const [code] = await once(rc.process, 'exit');
|
2020-07-09 18:48:39 +03:00
|
|
|
expect(code).to.equal(0);
|
2017-11-23 04:06:14 +03:00
|
|
|
});
|
2018-11-08 18:57:28 +03:00
|
|
|
|
2020-07-09 18:48:39 +03:00
|
|
|
it('does not crash on exit if added to a browser window', async () => {
|
|
|
|
const rc = await startRemoteControlApp();
|
|
|
|
await rc.remotely(() => {
|
|
|
|
const { app, BrowserView, BrowserWindow } = require('electron');
|
|
|
|
const bv = new BrowserView();
|
|
|
|
bv.webContents.loadURL('about:blank');
|
|
|
|
const bw = new BrowserWindow({ show: false });
|
|
|
|
bw.addBrowserView(bv);
|
|
|
|
setTimeout(() => {
|
|
|
|
app.quit();
|
|
|
|
});
|
|
|
|
});
|
2023-02-24 02:53:53 +03:00
|
|
|
const [code] = await once(rc.process, 'exit');
|
2018-11-08 18:57:28 +03:00
|
|
|
expect(code).to.equal(0);
|
|
|
|
});
|
2023-03-01 13:35:06 +03:00
|
|
|
|
|
|
|
it('emits the destroyed event when webContents.close() is called', async () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
w.setBrowserView(view);
|
|
|
|
await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
|
|
|
|
|
|
|
|
view.webContents.close();
|
|
|
|
await once(view.webContents, 'destroyed');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits the destroyed event when window.close() is called', async () => {
|
|
|
|
view = new BrowserView();
|
|
|
|
w.setBrowserView(view);
|
|
|
|
await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
|
|
|
|
|
|
|
|
view.webContents.executeJavaScript('window.close()');
|
|
|
|
await once(view.webContents, 'destroyed');
|
|
|
|
});
|
2018-11-08 18:57:28 +03:00
|
|
|
});
|
2018-11-27 04:39:03 +03:00
|
|
|
|
|
|
|
describe('window.open()', () => {
|
2020-11-10 20:06:03 +03:00
|
|
|
it('works in BrowserView', (done) => {
|
2018-11-27 04:39:03 +03:00
|
|
|
view = new BrowserView();
|
|
|
|
w.setBrowserView(view);
|
2020-11-10 20:06:03 +03:00
|
|
|
view.webContents.setWindowOpenHandler(({ url, frameName }) => {
|
|
|
|
expect(url).to.equal('http://host/');
|
|
|
|
expect(frameName).to.equal('host');
|
|
|
|
done();
|
|
|
|
return { action: 'deny' };
|
|
|
|
});
|
2018-11-27 04:39:03 +03:00
|
|
|
view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
|
|
|
|
});
|
|
|
|
});
|
2022-03-22 02:38:03 +03:00
|
|
|
|
|
|
|
describe('BrowserView.capturePage(rect)', () => {
|
|
|
|
it('returns a Promise with a Buffer', async () => {
|
|
|
|
view = new BrowserView({
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.addBrowserView(view);
|
|
|
|
view.setBounds({
|
|
|
|
...w.getBounds(),
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
});
|
|
|
|
const image = await view.webContents.capturePage({
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(image.isEmpty()).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
xit('resolves after the window is hidden and capturer count is non-zero', async () => {
|
|
|
|
view = new BrowserView({
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.setBrowserView(view);
|
|
|
|
view.setBounds({
|
|
|
|
...w.getBounds(),
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
});
|
|
|
|
await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
|
|
|
|
|
|
|
|
const image = await view.webContents.capturePage();
|
|
|
|
expect(image.isEmpty()).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
Implement initial, experimental BrowserView API
Right now, `<webview>` is the only way to embed additional content in a
`BrowserWindow`. Unfortunately `<webview>` suffers from a [number of
problems](https://github.com/electron/electron/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20label%3Awebview%20).
To make matters worse, many of these are upstream Chromium bugs instead
of Electron-specific bugs.
For us at [Figma](https://www.figma.com), the main issue is very slow
performance.
Despite the upstream improvements to `<webview>` through the OOPIF work, it is
probable that there will continue to be `<webview>`-specific bugs in the
future.
Therefore, this introduces a `<webview>` alternative to called `BrowserView`,
which...
- is a thin wrapper around `api::WebContents` (so bugs in `BrowserView` will
likely also be bugs in `BrowserWindow` web contents)
- is instantiated in the main process like `BrowserWindow` (and unlike
`<webview>`, which lives in the DOM of a `BrowserWindow` web contents)
- needs to be added to a `BrowserWindow` to display something on the screen
This implements the most basic API. The API is expected to evolve and change in
the near future and has consequently been marked as experimental. Please do not
use this API in production unless you are prepared to deal with breaking
changes.
In the future, we will want to change the API to support multiple
`BrowserView`s per window. We will also want to consider z-ordering
auto-resizing, and possibly even nested views.
2017-04-11 20:47:30 +03:00
|
|
|
});
|