-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrowser-window.test.ts
More file actions
679 lines (617 loc) · 20.7 KB
/
Copy pathbrowser-window.test.ts
File metadata and controls
679 lines (617 loc) · 20.7 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { InvalidArgumentError } from '../../../../src/common/errors';
import { resetBootstrapForTesting } from '../../../../src/main/bootstrap';
import { setNativeAppForTesting } from '../../../../src/main/native-app';
import type {
NativeApplication,
NativeWebContents,
NativeWindow,
NativeWindowOptions,
Rect,
WindowEventType,
} from '../../../../src/main/platform/native';
import {
BrowserWindow,
resetWindowRegistryForTesting,
} from '../../../../src/main/api/browser-window';
import { app } from '../../../../src/main/api/app';
import { session } from '../../../../src/main/api/session';
import { resetWebContentsIdsForTesting } from '../../../../src/main/api/web-contents';
import { appExitCodes, installSafeAppExit } from '../../../helpers/safe-app-exit';
type FakeWindow = NativeWindow & {
fireClosed: () => void;
/** Fire a non-preventable window event up through the seam. */
fireEvent: (type: WindowEventType) => void;
/**
* Simulate a native close attempt: run the registered close callback. Returns
* whether the close was vetoed (callback returned true). When not vetoed, the
* fake fires `onClosed` like a real backend would.
*/
fireCloseRequest: () => boolean;
/** Number of times the (idempotent) teardown ran. */
teardownCount: () => number;
/** The User-Agent applied to this window's web contents, if any. */
appliedUserAgent: () => string | undefined;
/** The last opacity pushed to the native window, if any. */
appliedOpacity: () => number | undefined;
/** The last resizable flag pushed to the native window, if any. */
appliedResizable: () => boolean | undefined;
/** The last minimum size pushed to the native window, if any. */
appliedMinSize: () => [number, number] | undefined;
/** Whether `center` was called on the native window. */
wasCentered: () => boolean;
};
const makeFakeWindow = (options: NativeWindowOptions): FakeWindow => {
let title = options.title;
let visible = options.show;
let bounds: Rect = { x: 0, y: 0, width: options.width, height: options.height };
let maximized = false;
let minimized = false;
let fullscreen = false;
let focused = false;
let onClosed: (() => void) | undefined;
let onClose: (() => boolean) | undefined;
let teardowns = 0;
let tornDown = false;
const eventCallbacks = new Map<WindowEventType, () => void>();
const teardown = (): void => {
if (tornDown) {
return;
}
tornDown = true;
teardowns += 1;
};
let appliedUserAgent: string | undefined;
let appliedOpacity: number | undefined;
let appliedResizable: boolean | undefined;
let appliedMinSize: [number, number] | undefined;
let centered = false;
const webContents: NativeWebContents = {
loadURL: () => undefined,
loadHTML: () => undefined,
getURL: () => 'about:blank',
getTitle: () => '',
reload: () => undefined,
reloadIgnoringCache: () => undefined,
stop: () => undefined,
goBack: () => undefined,
goForward: () => undefined,
canGoBack: () => false,
canGoForward: () => false,
executeJavaScript: () => Promise.resolve(undefined),
printToPDF: () => Promise.resolve(new Uint8Array(0)),
capturePage: () => Promise.resolve(new Uint8Array(0)),
openDevTools: () => undefined,
closeDevTools: () => undefined,
setZoomFactor: () => undefined,
setUserAgent: (ua) => {
appliedUserAgent = ua;
},
sendEnvelopeToRenderer: () => undefined,
onRendererEnvelope: () => undefined,
onNavigation: () => undefined,
setWindowOpenHandler: () => undefined,
};
return {
webContents,
setTitle: (t) => {
title = t;
},
getTitle: () => title,
setSize: (w, h) => {
bounds = { ...bounds, width: w, height: h };
},
setPosition: (x, y) => {
bounds = { ...bounds, x, y };
},
setBounds: (b) => {
bounds = { ...b };
},
getBounds: () => bounds,
setResizable: (r) => {
appliedResizable = r;
},
setOpacity: (o) => {
appliedOpacity = o;
},
setMinimumSize: (w, h) => {
appliedMinSize = [w, h];
},
center: () => {
centered = true;
},
show: () => {
visible = true;
},
hide: () => {
visible = false;
},
isVisible: () => visible,
focus: () => {
visible = true;
focused = true;
},
minimize: () => {
minimized = true;
},
maximize: () => {
maximized = true;
},
unmaximize: () => {
maximized = false;
},
isMaximized: () => maximized,
isMinimized: () => minimized,
restore: () => {
minimized = false;
},
isFocused: () => focused,
setFullScreen: (flag) => {
fullscreen = flag;
},
isFullScreen: () => fullscreen,
setAlwaysOnTop: () => undefined,
close: () => {
// A real backend routes programmatic close through the same delegate path:
// consult the veto, and only on a non-veto run teardown + fire closed.
if (onClose?.() === true) {
return;
}
teardown();
onClosed?.();
},
destroy: () => {
teardown();
onClosed?.();
},
onClosed: (cb) => {
onClosed = cb;
},
onClose: (cb) => {
onClose = cb;
},
onWindowEvent: (type, cb) => {
eventCallbacks.set(type, cb);
},
popupMenu: () => undefined,
closePopupMenu: () => undefined,
fireClosed: () => onClosed?.(),
fireEvent: (type) => eventCallbacks.get(type)?.(),
fireCloseRequest: () => {
if (onClose?.() === true) {
return true;
}
teardown();
onClosed?.();
return false;
},
teardownCount: () => teardowns,
appliedUserAgent: () => appliedUserAgent,
appliedOpacity: () => appliedOpacity,
appliedResizable: () => appliedResizable,
appliedMinSize: () => appliedMinSize,
wasCentered: () => centered,
};
};
const makeFakeApp = (): {
native: NativeApplication;
created: NativeWindowOptions[];
windows: FakeWindow[];
} => {
const created: NativeWindowOptions[] = [];
const windows: FakeWindow[] = [];
const native: NativeApplication = {
start: () => undefined,
onReady: (cb) => cb(),
quit: () => undefined,
createWindow: (options) => {
created.push(options);
const window = makeFakeWindow(options);
windows.push(window);
return window;
},
};
return { native, created, windows };
};
let created: NativeWindowOptions[];
let windows: FakeWindow[];
beforeEach(() => {
resetWindowRegistryForTesting();
resetWebContentsIdsForTesting();
resetBootstrapForTesting();
installSafeAppExit();
const fake = makeFakeApp();
created = fake.created;
windows = fake.windows;
setNativeAppForTesting(fake.native);
});
afterEach(() => {
setNativeAppForTesting(undefined);
app.resetForTesting();
session.defaultSession.resetForTesting();
});
describe('BrowserWindow default-session user agent', () => {
test('applies the default session user agent to a new window', () => {
session.defaultSession.setUserAgent('Bunmaska/UA (test)');
new BrowserWindow();
expect(windows[0]?.appliedUserAgent()).toBe('Bunmaska/UA (test)');
});
test('does not override the user agent when the session has none', () => {
new BrowserWindow();
expect(windows[0]?.appliedUserAgent()).toBeUndefined();
});
test('applies app.userAgentFallback when the session has no override', () => {
app.userAgentFallback = 'Fallback/1.0';
new BrowserWindow();
expect(windows[0]?.appliedUserAgent()).toBe('Fallback/1.0');
});
test('a session user agent takes precedence over app.userAgentFallback', () => {
app.userAgentFallback = 'Fallback/1.0';
session.defaultSession.setUserAgent('Session/2.0');
new BrowserWindow();
expect(windows[0]?.appliedUserAgent()).toBe('Session/2.0');
});
});
describe('BrowserWindow runtime setters', () => {
test('setResizable toggles the native window and isResizable cache', () => {
const win = new BrowserWindow();
expect(win.isResizable()).toBe(true);
win.setResizable(false);
expect(windows[0]?.appliedResizable()).toBe(false);
expect(win.isResizable()).toBe(false);
});
test('isResizable reflects the constructor option', () => {
expect(new BrowserWindow({ resizable: false }).isResizable()).toBe(false);
});
test('setOpacity clamps to [0,1], pushes native, and getOpacity tracks it', () => {
const win = new BrowserWindow();
expect(win.getOpacity()).toBe(1);
win.setOpacity(0.5);
expect(windows[0]?.appliedOpacity()).toBe(0.5);
expect(win.getOpacity()).toBe(0.5);
win.setOpacity(2);
expect(win.getOpacity()).toBe(1);
win.setOpacity(-1);
expect(win.getOpacity()).toBe(0);
});
test('setMinimumSize pushes native and getMinimumSize tracks it', () => {
const win = new BrowserWindow();
expect(win.getMinimumSize()).toEqual([0, 0]);
win.setMinimumSize(400, 300);
expect(windows[0]?.appliedMinSize()).toEqual([400, 300]);
expect(win.getMinimumSize()).toEqual([400, 300]);
});
test('getSize derives from the native bounds', () => {
const win = new BrowserWindow({ width: 1024, height: 768 });
expect(win.getSize()).toEqual([1024, 768]);
win.setSize(640, 480);
expect(win.getSize()).toEqual([640, 480]);
});
test('center delegates to the native window', () => {
const win = new BrowserWindow();
win.center();
expect(windows[0]?.wasCentered()).toBe(true);
});
});
describe('BrowserWindow construction', () => {
test('applies default options when none are given', () => {
new BrowserWindow();
expect(created[0]).toEqual({ width: 800, height: 600, title: 'Bunmaska', show: true });
});
test('passes through provided options', () => {
new BrowserWindow({ width: 1024, height: 768, title: 'My App', show: false });
expect(created[0]).toEqual({ width: 1024, height: 768, title: 'My App', show: false });
});
test('forwards resizable, frame, and fullscreen when provided', () => {
new BrowserWindow({ resizable: false, frame: false, fullscreen: true });
expect(created[0]).toMatchObject({ resizable: false, frame: false, fullscreen: true });
});
test('omits resizable/frame/fullscreen when not provided', () => {
new BrowserWindow();
expect(created[0]).not.toHaveProperty('resizable');
expect(created[0]).not.toHaveProperty('frame');
expect(created[0]).not.toHaveProperty('fullscreen');
});
test('assigns incrementing ids', () => {
const a = new BrowserWindow();
const b = new BrowserWindow();
expect(b.id).toBe(a.id + 1);
});
test('exposes a WebContents instance', () => {
const win = new BrowserWindow();
expect(win.webContents).toBeDefined();
expect(typeof win.webContents.loadURL).toBe('function');
});
test('is a Node EventEmitter', () => {
expect(typeof new BrowserWindow().on).toBe('function');
});
test('leaves preloadScript undefined when webPreferences is omitted', () => {
new BrowserWindow();
expect(created[0]?.preloadScript).toBeUndefined();
});
});
describe('BrowserWindow webPreferences.preload', () => {
let dir: string;
beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'bunmaska-preload-'));
});
afterEach(() => {
rmSync(dir, { recursive: true, force: true });
});
test('reads the preload file and passes its contents as preloadScript', () => {
const source = 'window.__bunmaskaPreloadRan = true;\n';
const preloadPath = join(dir, 'preload.js');
writeFileSync(preloadPath, source);
new BrowserWindow({ webPreferences: { preload: preloadPath } });
expect(created[0]?.preloadScript).toBe(source);
});
test('resolves a relative preload path against the current working directory', () => {
const source = 'void 0;\n';
const preloadPath = join(dir, 'relative-preload.js');
writeFileSync(preloadPath, source);
const cwd = process.cwd();
process.chdir(dir);
try {
new BrowserWindow({ webPreferences: { preload: 'relative-preload.js' } });
} finally {
process.chdir(cwd);
}
expect(created[0]?.preloadScript).toBe(source);
});
test('throws InvalidArgumentError naming the path when the preload is missing', () => {
const missing = join(dir, 'does-not-exist.js');
expect(() => new BrowserWindow({ webPreferences: { preload: missing } })).toThrow(
InvalidArgumentError,
);
expect(() => new BrowserWindow({ webPreferences: { preload: missing } })).toThrow(missing);
});
});
describe('BrowserWindow registry', () => {
test('getAllWindows returns open windows in creation order', () => {
const a = new BrowserWindow();
const b = new BrowserWindow();
expect(BrowserWindow.getAllWindows().map((w) => w.id)).toEqual([a.id, b.id]);
});
test('fromId returns the matching window', () => {
const a = new BrowserWindow();
expect(BrowserWindow.fromId(a.id)).toBe(a);
});
test('fromId returns undefined for an unknown id', () => {
expect(BrowserWindow.fromId(9999)).toBeUndefined();
});
});
describe('BrowserWindow lifecycle', () => {
test('title getter reflects setter', () => {
const win = new BrowserWindow({ title: 'initial' });
win.setTitle('updated');
expect(win.getTitle()).toBe('updated');
});
test('setSize updates bounds', () => {
const win = new BrowserWindow();
win.setSize(300, 200);
expect(win.getBounds()).toEqual({ x: 0, y: 0, width: 300, height: 200 });
});
test('setPosition moves the window and getPosition reads it back', () => {
const win = new BrowserWindow();
win.setPosition(120, 80);
expect(win.getPosition()).toEqual([120, 80]);
expect(win.getBounds()).toMatchObject({ x: 120, y: 80 });
});
test('setBounds sets position and size together', () => {
const win = new BrowserWindow();
win.setBounds({ x: 50, y: 60, width: 800, height: 600 });
expect(win.getBounds()).toEqual({ x: 50, y: 60, width: 800, height: 600 });
expect(win.getPosition()).toEqual([50, 60]);
expect(win.getSize()).toEqual([800, 600]);
});
test('close emits closed, removes from registry, and marks destroyed', () => {
const win = new BrowserWindow();
let closedEvents = 0;
win.on('closed', () => {
closedEvents += 1;
});
win.close();
expect(closedEvents).toBe(1);
expect(win.isDestroyed()).toBe(true);
expect(BrowserWindow.fromId(win.id)).toBeUndefined();
});
});
describe('BrowserWindow lifecycle events', () => {
const simpleEvents: WindowEventType[] = [
'focus',
'blur',
'show',
'hide',
'resize',
'maximize',
'unmaximize',
'minimize',
'restore',
'ready-to-show',
];
for (const type of simpleEvents) {
test(`re-emits the native '${type}' event`, () => {
const win = new BrowserWindow();
let fired = 0;
win.on(type, () => {
fired += 1;
});
windows[0]?.fireEvent(type);
expect(fired).toBe(1);
});
}
test('a native close request with no listeners proceeds to closed', () => {
const win = new BrowserWindow();
let closed = 0;
win.on('closed', () => {
closed += 1;
});
const vetoed = windows[0]?.fireCloseRequest();
expect(vetoed).toBe(false);
expect(closed).toBe(1);
expect(win.isDestroyed()).toBe(true);
});
test("emits a preventable 'close' before 'closed' with an Electron-style event", () => {
const win = new BrowserWindow();
const order: string[] = [];
let eventArg: { preventDefault: () => void; defaultPrevented: boolean } | undefined;
win.on('close', (event) => {
order.push('close');
eventArg = event;
});
win.on('closed', () => order.push('closed'));
windows[0]?.fireCloseRequest();
expect(order).toEqual(['close', 'closed']);
expect(typeof eventArg?.preventDefault).toBe('function');
expect(eventArg?.defaultPrevented).toBe(false);
});
test("a 'close' listener calling preventDefault vetoes the close", () => {
const win = new BrowserWindow();
let closed = 0;
win.on('close', (event) => {
event.preventDefault();
});
win.on('closed', () => {
closed += 1;
});
const vetoed = windows[0]?.fireCloseRequest();
expect(vetoed).toBe(true);
expect(closed).toBe(0);
expect(win.isDestroyed()).toBe(false);
expect(BrowserWindow.fromId(win.id)).toBe(win);
});
test('a non-prevented close after a prevented one still closes', () => {
const win = new BrowserWindow();
let prevent = true;
win.on('close', (event) => {
if (prevent) {
event.preventDefault();
}
});
expect(windows[0]?.fireCloseRequest()).toBe(true);
expect(win.isDestroyed()).toBe(false);
prevent = false;
expect(windows[0]?.fireCloseRequest()).toBe(false);
expect(win.isDestroyed()).toBe(true);
});
test('programmatic close() consults close listeners (preventable)', () => {
const win = new BrowserWindow();
win.on('close', (event) => {
event.preventDefault();
});
win.close();
expect(win.isDestroyed()).toBe(false);
});
test('teardown runs exactly once across repeated close attempts (idempotent)', () => {
const win = new BrowserWindow();
windows[0]?.fireCloseRequest();
win.close();
windows[0]?.fireCloseRequest();
expect(windows[0]?.teardownCount()).toBe(1);
});
});
describe('App-level window events', () => {
test('constructing a window emits browser-window-created with the window', () => {
let received: BrowserWindow | undefined;
app.on('browser-window-created', (_event: unknown, win: BrowserWindow) => {
received = win;
});
const win = new BrowserWindow();
expect(received).toBe(win);
});
test('constructing a window emits web-contents-created with the web contents', () => {
let received: unknown;
app.on('web-contents-created', (_event: unknown, contents: unknown) => {
received = contents;
});
const win = new BrowserWindow();
expect(received).toBe(win.webContents);
});
test('native focus re-emits app browser-window-focus with the window', () => {
const win = new BrowserWindow();
let focused: BrowserWindow | undefined;
app.on('browser-window-focus', (_event: unknown, w: BrowserWindow) => {
focused = w;
});
windows[0]?.fireEvent('focus');
expect(focused).toBe(win);
});
test('native blur re-emits app browser-window-blur with the window', () => {
const win = new BrowserWindow();
let blurred: BrowserWindow | undefined;
app.on('browser-window-blur', (_event: unknown, w: BrowserWindow) => {
blurred = w;
});
windows[0]?.fireEvent('blur');
expect(blurred).toBe(win);
});
test('closing the last window emits app window-all-closed', () => {
const win = new BrowserWindow();
let fired = 0;
app.on('window-all-closed', () => {
fired += 1;
});
win.close();
expect(fired).toBe(1);
});
test('window-all-closed fires only once the final window closes', () => {
const a = new BrowserWindow();
const b = new BrowserWindow();
let fired = 0;
app.on('window-all-closed', () => {
fired += 1;
});
a.close();
expect(fired).toBe(0);
b.close();
expect(fired).toBe(1);
});
test('closing the last window with no listener quits the app', () => {
const win = new BrowserWindow();
win.close();
expect(appExitCodes()).toEqual([0]);
});
test('a window-all-closed listener suppresses the default quit', () => {
const win = new BrowserWindow();
app.on('window-all-closed', () => undefined);
win.close();
expect(appExitCodes()).toEqual([]);
});
});
describe('BrowserWindow window controls', () => {
test('restore clears the minimized state', () => {
const win = new BrowserWindow();
win.minimize();
expect(win.isMinimized()).toBe(true);
win.restore();
expect(win.isMinimized()).toBe(false);
});
test('isFocused reflects the native state', () => {
const win = new BrowserWindow();
expect(win.isFocused()).toBe(false);
win.focus();
expect(win.isFocused()).toBe(true);
});
test('setFullScreen toggles isFullScreen', () => {
const win = new BrowserWindow();
expect(win.isFullScreen()).toBe(false);
win.setFullScreen(true);
expect(win.isFullScreen()).toBe(true);
win.setFullScreen(false);
expect(win.isFullScreen()).toBe(false);
});
test('setAlwaysOnTop does not throw', () => {
const win = new BrowserWindow();
expect(() => win.setAlwaysOnTop(true)).not.toThrow();
});
test('destroy force-closes even when a close listener vetoes', () => {
const win = new BrowserWindow();
win.on('close', (event) => event.preventDefault());
win.destroy();
expect(win.isDestroyed()).toBe(true);
expect(BrowserWindow.fromId(win.id)).toBeUndefined();
});
});