forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-browser-view-spec.ts
452 lines (382 loc) · 13.9 KB
/
api-browser-view-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { expect } from 'chai';
import * as path from 'node:path';
import { BrowserView, BrowserWindow, screen, webContents } from 'electron/main';
import { closeWindow } from './lib/window-helpers';
import { defer, ifit, startRemoteControlApp } from './lib/spec-helpers';
import { areColorsSimilar, captureScreen, getPixelColor } from './lib/screen-helpers';
import { once } from 'node:events';
describe('BrowserView module', () => {
const fixtures = path.resolve(__dirname, 'fixtures');
let w: BrowserWindow;
let view: BrowserView;
beforeEach(() => {
expect(webContents.getAllWebContents()).to.have.length(0);
w = new BrowserWindow({
show: false,
width: 400,
height: 400,
webPreferences: {
backgroundThrottling: false
}
});
});
afterEach(async () => {
const p = once(w.webContents, 'destroyed');
await closeWindow(w);
w = null as any;
await p;
if (view && view.webContents) {
const p = once(view.webContents, 'destroyed');
view.webContents.destroy();
view = null as any;
await p;
}
expect(webContents.getAllWebContents()).to.have.length(0);
});
it('sets the correct class name on the prototype', () => {
expect(BrowserView.prototype.constructor.name).to.equal('BrowserView');
});
it('can be created with an existing webContents', async () => {
const wc = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
await wc.loadURL('about:blank');
view = new BrowserView({ webContents: wc } as any);
expect(view.webContents.getURL()).to.equal('about:blank');
});
describe('BrowserView.setBackgroundColor()', () => {
it('does not throw for valid args', () => {
view = new BrowserView();
view.setBackgroundColor('#000');
});
it('throws for invalid args', () => {
view = new BrowserView();
expect(() => {
view.setBackgroundColor(null as any);
}).to.throw(/conversion failure/);
});
// Linux and arm64 platforms (WOA and macOS) do not return any capture sources
ifit(process.platform === 'darwin' && process.arch === 'x64')('sets the background color to transparent if none is set', async () => {
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
ifit(process.platform === 'darwin' && process.arch === 'x64')('successfully applies the background color', async () => {
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();
});
});
describe('BrowserView.setAutoResize()', () => {
it('does not throw for valid args', () => {
view = new BrowserView();
view.setAutoResize({});
view.setAutoResize({ width: true, height: false });
});
it('throws for invalid args', () => {
view = new BrowserView();
expect(() => {
view.setAutoResize(null as any);
}).to.throw(/conversion failure/);
});
});
describe('BrowserView.setBounds()', () => {
it('does not throw for valid args', () => {
view = new BrowserView();
view.setBounds({ x: 0, y: 0, width: 1, height: 1 });
});
it('throws for invalid args', () => {
view = new BrowserView();
expect(() => {
view.setBounds(null as any);
}).to.throw(/conversion failure/);
expect(() => {
view.setBounds({} as any);
}).to.throw(/conversion failure/);
});
});
describe('BrowserView.getBounds()', () => {
it('returns the current bounds', () => {
view = new BrowserView();
const bounds = { x: 10, y: 20, width: 30, height: 40 };
view.setBounds(bounds);
expect(view.getBounds()).to.deep.equal(bounds);
});
});
describe('BrowserWindow.setBrowserView()', () => {
it('does not throw for valid args', () => {
view = new BrowserView();
w.setBrowserView(view);
});
it('does not throw if called multiple times with same view', () => {
view = new BrowserView();
w.setBrowserView(view);
w.setBrowserView(view);
w.setBrowserView(view);
});
});
describe('BrowserWindow.getBrowserView()', () => {
it('returns the set view', () => {
view = new BrowserView();
w.setBrowserView(view);
const view2 = w.getBrowserView();
expect(view2!.webContents.id).to.equal(view.webContents.id);
});
it('returns null if none is set', () => {
const view = w.getBrowserView();
expect(view).to.be.null('view');
});
});
describe('BrowserWindow.addBrowserView()', () => {
it('does not throw for valid args', () => {
const view1 = new BrowserView();
defer(() => view1.webContents.destroy());
w.addBrowserView(view1);
defer(() => w.removeBrowserView(view1));
const view2 = new BrowserView();
defer(() => view2.webContents.destroy());
w.addBrowserView(view2);
defer(() => w.removeBrowserView(view2));
});
it('does not throw if called multiple times with same view', () => {
view = new BrowserView();
w.addBrowserView(view);
w.addBrowserView(view);
w.addBrowserView(view);
});
it('does not crash if the BrowserView webContents are destroyed prior to window addition', () => {
expect(() => {
const view1 = new BrowserView();
view1.webContents.destroy();
w.addBrowserView(view1);
}).to.not.throw();
});
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();
});
it('can handle BrowserView reparenting', async () => {
view = new BrowserView();
w.addBrowserView(view);
view.webContents.loadURL('about:blank');
await once(view.webContents, 'did-finish-load');
const w2 = new BrowserWindow({ show: false });
w2.addBrowserView(view);
w.close();
view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
await once(view.webContents, 'did-finish-load');
// Clean up - the afterEach hook assumes the webContents on w is still alive.
w = new BrowserWindow({ show: false });
w2.close();
w2.destroy();
});
});
describe('BrowserWindow.removeBrowserView()', () => {
it('does not throw if called multiple times with same view', () => {
expect(() => {
view = new BrowserView();
w.addBrowserView(view);
w.removeBrowserView(view);
w.removeBrowserView(view);
}).to.not.throw();
});
it('can be called on a BrowserView with a destroyed webContents', (done) => {
view = new BrowserView();
w.addBrowserView(view);
view.webContents.on('destroyed', () => {
w.removeBrowserView(view);
done();
});
view.webContents.loadURL('data:text/html,hello there').then(() => {
view.webContents.close();
});
});
});
describe('BrowserWindow.getBrowserViews()', () => {
it('returns same views as was added', () => {
const view1 = new BrowserView();
defer(() => view1.webContents.destroy());
w.addBrowserView(view1);
defer(() => w.removeBrowserView(view1));
const view2 = new BrowserView();
defer(() => view2.webContents.destroy());
w.addBrowserView(view2);
defer(() => w.removeBrowserView(view2));
const views = w.getBrowserViews();
expect(views).to.have.lengthOf(2);
expect(views[0].webContents.id).to.equal(view1.webContents.id);
expect(views[1].webContents.id).to.equal(view2.webContents.id);
});
it('persists ordering by z-index', () => {
const view1 = new BrowserView();
defer(() => view1.webContents.destroy());
w.addBrowserView(view1);
defer(() => w.removeBrowserView(view1));
const view2 = new BrowserView();
defer(() => view2.webContents.destroy());
w.addBrowserView(view2);
defer(() => w.removeBrowserView(view2));
w.setTopBrowserView(view1);
const views = w.getBrowserViews();
expect(views).to.have.lengthOf(2);
expect(views[0].webContents.id).to.equal(view2.webContents.id);
expect(views[1].webContents.id).to.equal(view1.webContents.id);
});
});
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();
});
});
describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
it('points to owning window', () => {
view = new BrowserView();
expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
w.setBrowserView(view);
expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
w.setBrowserView(null);
expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
});
});
describe('shutdown behavior', () => {
it('does not crash on exit', async () => {
const rc = await startRemoteControlApp();
await rc.remotely(() => {
const { BrowserView, app } = require('electron');
// eslint-disable-next-line no-new
new BrowserView({});
setTimeout(() => {
app.quit();
});
});
const [code] = await once(rc.process, 'exit');
expect(code).to.equal(0);
});
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();
});
});
const [code] = await once(rc.process, 'exit');
expect(code).to.equal(0);
});
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');
});
});
describe('window.open()', () => {
it('works in BrowserView', (done) => {
view = new BrowserView();
w.setBrowserView(view);
view.webContents.setWindowOpenHandler(({ url, frameName }) => {
expect(url).to.equal('http://host/');
expect(frameName).to.equal('host');
done();
return { action: 'deny' };
});
view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
});
});
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);
});
});
});