forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguest-window-manager-spec.ts
373 lines (316 loc) · 14.6 KB
/
guest-window-manager-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
import { BrowserWindow, screen } from 'electron';
import { expect, assert } from 'chai';
import { HexColors, ScreenCapture, hasCapturableScreen } from './lib/screen-helpers';
import { ifit, listen } from './lib/spec-helpers';
import { closeAllWindows } from './lib/window-helpers';
import { once } from 'node:events';
import * as http from 'node:http';
describe('webContents.setWindowOpenHandler', () => {
describe('native window', () => {
let browserWindow: BrowserWindow;
beforeEach(async () => {
browserWindow = new BrowserWindow({ show: false });
await browserWindow.loadURL('about:blank');
});
afterEach(closeAllWindows);
it('does not fire window creation events if the handler callback throws an error', (done) => {
const error = new Error('oh no');
const listeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
process.on('uncaughtException', (thrown) => {
try {
expect(thrown).to.equal(error);
done();
} catch (e) {
done(e);
} finally {
process.removeAllListeners('uncaughtException');
for (const listener of listeners) {
process.on('uncaughtException', listener);
}
}
});
browserWindow.webContents.on('did-create-window', () => {
assert.fail('did-create-window should not be called with an overridden window.open');
});
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
browserWindow.webContents.setWindowOpenHandler(() => {
throw error;
});
});
it('does not fire window creation events if the handler callback returns a bad result', async () => {
const bad = new Promise((resolve) => {
browserWindow.webContents.setWindowOpenHandler(() => {
setTimeout(resolve);
return [1, 2, 3] as any;
});
});
browserWindow.webContents.on('did-create-window', () => {
assert.fail('did-create-window should not be called with an overridden window.open');
});
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
await bad;
});
it('does not fire window creation events if an override returns action: deny', async () => {
const denied = new Promise((resolve) => {
browserWindow.webContents.setWindowOpenHandler(() => {
setTimeout(resolve);
return { action: 'deny' };
});
});
browserWindow.webContents.on('did-create-window', () => {
assert.fail('did-create-window should not be called with an overridden window.open');
});
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
await denied;
});
it('is called when clicking on a target=_blank link', async () => {
const denied = new Promise((resolve) => {
browserWindow.webContents.setWindowOpenHandler(() => {
setTimeout(resolve);
return { action: 'deny' };
});
});
browserWindow.webContents.on('did-create-window', () => {
assert.fail('did-create-window should not be called with an overridden window.open');
});
await browserWindow.webContents.loadURL('data:text/html,<a target="_blank" href="http://example.com" style="display: block; width: 100%; height: 100%; position: fixed; left: 0; top: 0;">link</a>');
browserWindow.webContents.sendInputEvent({ type: 'mouseDown', x: 10, y: 10, button: 'left', clickCount: 1 });
browserWindow.webContents.sendInputEvent({ type: 'mouseUp', x: 10, y: 10, button: 'left', clickCount: 1 });
await denied;
});
it('is called when shift-clicking on a link', async () => {
const denied = new Promise((resolve) => {
browserWindow.webContents.setWindowOpenHandler(() => {
setTimeout(resolve);
return { action: 'deny' };
});
});
browserWindow.webContents.on('did-create-window', () => {
assert.fail('did-create-window should not be called with an overridden window.open');
});
await browserWindow.webContents.loadURL('data:text/html,<a href="http://example.com" style="display: block; width: 100%; height: 100%; position: fixed; left: 0; top: 0;">link</a>');
browserWindow.webContents.sendInputEvent({ type: 'mouseDown', x: 10, y: 10, button: 'left', clickCount: 1, modifiers: ['shift'] });
browserWindow.webContents.sendInputEvent({ type: 'mouseUp', x: 10, y: 10, button: 'left', clickCount: 1, modifiers: ['shift'] });
await denied;
});
it('fires handler with correct params', async () => {
const testFrameName = 'test-frame-name';
const testFeatures = 'top=10&left=10&something-unknown&show=no';
const testUrl = 'app://does-not-exist/';
const details = await new Promise<Electron.HandlerDetails>(resolve => {
browserWindow.webContents.setWindowOpenHandler((details) => {
setTimeout(() => resolve(details));
return { action: 'deny' };
});
browserWindow.webContents.executeJavaScript(`window.open('${testUrl}', '${testFrameName}', '${testFeatures}') && true`);
});
const { url, frameName, features, disposition, referrer } = details;
expect(url).to.equal(testUrl);
expect(frameName).to.equal(testFrameName);
expect(features).to.equal(testFeatures);
expect(disposition).to.equal('new-window');
expect(referrer).to.deep.equal({
policy: 'strict-origin-when-cross-origin',
url: ''
});
});
it('includes post body', async () => {
const details = await new Promise<Electron.HandlerDetails>(resolve => {
browserWindow.webContents.setWindowOpenHandler((details) => {
setTimeout(() => resolve(details));
return { action: 'deny' };
});
browserWindow.webContents.loadURL(`data:text/html,${encodeURIComponent(`
<form action="http://example.com" target="_blank" method="POST" id="form">
<input name="key" value="value"></input>
</form>
<script>form.submit()</script>
`)}`);
});
const { url, frameName, features, disposition, referrer, postBody } = details;
expect(url).to.equal('http://example.com/');
expect(frameName).to.equal('');
expect(features).to.deep.equal('');
expect(disposition).to.equal('foreground-tab');
expect(referrer).to.deep.equal({
policy: 'strict-origin-when-cross-origin',
url: ''
});
expect(postBody).to.deep.equal({
contentType: 'application/x-www-form-urlencoded',
data: [{
type: 'rawData',
bytes: Buffer.from('key=value')
}]
});
});
it('does fire window creation events if an override returns action: allow', async () => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow' }));
setImmediate(() => {
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
});
await once(browserWindow.webContents, 'did-create-window');
});
it('can change webPreferences of child windows', async () => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { defaultFontSize: 30 } } }));
const didCreateWindow = once(browserWindow.webContents, 'did-create-window') as Promise<[BrowserWindow, Electron.DidCreateWindowDetails]>;
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
const [childWindow] = await didCreateWindow;
await childWindow.webContents.executeJavaScript("document.write('hello')");
const size = await childWindow.webContents.executeJavaScript("getComputedStyle(document.querySelector('body')).fontSize");
expect(size).to.equal('30px');
});
it('does not hang parent window when denying window.open', async () => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
browserWindow.webContents.executeJavaScript("window.open('https://127.0.0.1')");
expect(await browserWindow.webContents.executeJavaScript('42')).to.equal(42);
});
ifit(hasCapturableScreen())('should not make child window background transparent', async () => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow' }));
const didCreateWindow = once(browserWindow.webContents, 'did-create-window');
browserWindow.webContents.executeJavaScript("window.open('about:blank') && true");
const [childWindow] = await didCreateWindow;
const display = screen.getPrimaryDisplay();
childWindow.setBounds(display.bounds);
await childWindow.webContents.executeJavaScript("const meta = document.createElement('meta'); meta.name = 'color-scheme'; meta.content = 'dark'; document.head.appendChild(meta); true;");
const screenCapture = new ScreenCapture(display);
// color-scheme is set to dark so background should not be white
await screenCapture.expectColorAtCenterDoesNotMatch(HexColors.WHITE);
});
});
describe('custom window', () => {
let browserWindow: BrowserWindow;
let server: http.Server;
let url: string;
before(async () => {
server = http.createServer((request, response) => {
switch (request.url) {
case '/index':
response.statusCode = 200;
response.end('<title>Index page</title>');
break;
case '/child':
response.statusCode = 200;
response.end('<title>Child page</title>');
break;
default:
throw new Error(`Unsupported endpoint: ${request.url}`);
}
});
url = (await listen(server)).url;
});
after(() => {
server.close();
});
beforeEach(async () => {
browserWindow = new BrowserWindow({ show: false });
await browserWindow.loadURL(`${url}/index`);
});
afterEach(closeAllWindows);
it('throws error when created window uses invalid webcontents', async () => {
const listeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
const uncaughtExceptionEmitted = new Promise<void>((resolve, reject) => {
process.on('uncaughtException', (thrown) => {
try {
expect(thrown.message).to.equal('Invalid webContents. Created window should be connected to webContents passed with options object.');
resolve();
} catch (e) {
reject(e);
} finally {
process.removeAllListeners('uncaughtException');
listeners.forEach((listener) => process.on('uncaughtException', listener));
}
});
});
browserWindow.webContents.setWindowOpenHandler(() => {
return {
action: 'allow',
createWindow: () => {
const childWindow = new BrowserWindow({ title: 'New window' });
return childWindow.webContents;
}
};
});
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
await uncaughtExceptionEmitted;
});
it('spawns browser window when createWindow is provided', async () => {
const browserWindowTitle = 'Child browser window';
const childWindow = await new Promise<Electron.BrowserWindow>(resolve => {
browserWindow.webContents.setWindowOpenHandler(() => {
return {
action: 'allow',
createWindow: (options) => {
const childWindow = new BrowserWindow({ ...options, title: browserWindowTitle });
resolve(childWindow);
return childWindow.webContents;
}
};
});
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
});
expect(childWindow.title).to.equal(browserWindowTitle);
});
it('spawns browser window with overriden options', async () => {
const childWindow = await new Promise<Electron.BrowserWindow>(resolve => {
browserWindow.webContents.setWindowOpenHandler(() => {
return {
action: 'allow',
overrideBrowserWindowOptions: {
width: 640,
height: 480
},
createWindow: (options) => {
expect(options.width).to.equal(640);
expect(options.height).to.equal(480);
const childWindow = new BrowserWindow(options);
resolve(childWindow);
return childWindow.webContents;
}
};
});
browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
});
const size = childWindow.getSize();
expect(size[0]).to.equal(640);
expect(size[1]).to.equal(480);
});
it('spawns browser window with access to opener property', async () => {
const childWindow = await new Promise<Electron.BrowserWindow>(resolve => {
browserWindow.webContents.setWindowOpenHandler(() => {
return {
action: 'allow',
createWindow: (options) => {
const childWindow = new BrowserWindow(options);
resolve(childWindow);
return childWindow.webContents;
}
};
});
browserWindow.webContents.executeJavaScript(`window.open('${url}/child', '', 'show=no') && true`);
});
await once(childWindow.webContents, 'ready-to-show');
const childWindowOpenerTitle = await childWindow.webContents.executeJavaScript('window.opener.document.title');
expect(childWindowOpenerTitle).to.equal(browserWindow.title);
});
it('spawns browser window without access to opener property because of noopener attribute ', async () => {
const childWindow = await new Promise<Electron.BrowserWindow>(resolve => {
browserWindow.webContents.setWindowOpenHandler(() => {
return {
action: 'allow',
createWindow: (options) => {
const childWindow = new BrowserWindow(options);
resolve(childWindow);
return childWindow.webContents;
}
};
});
browserWindow.webContents.executeJavaScript(`window.open('${url}/child', '', 'noopener,show=no') && true`);
});
await once(childWindow.webContents, 'ready-to-show');
await expect(childWindow.webContents.executeJavaScript('window.opener.document.title')).to.be.rejectedWith('Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.');
});
});
});