/
guest-window-manager-spec.ts
207 lines (180 loc) · 7.02 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
import { BrowserWindow } from 'electron';
import { writeFileSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { expect, assert } from 'chai';
import { closeAllWindows } from './window-helpers';
const { emittedOnce } = require('./events-helpers');
function genSnapshot (browserWindow: BrowserWindow, features: string) {
return new Promise((resolve) => {
browserWindow.webContents.on('new-window', (...args: any[]) => {
resolve([features, ...args]);
});
browserWindow.webContents.executeJavaScript(`window.open('about:blank', 'frame name', '${features}') && true`);
});
}
describe('new-window event', () => {
const testConfig = {
native: {
snapshotFileName: 'native-window-open.snapshot.txt',
browserWindowOptions: {
show: false,
width: 200,
title: 'cool',
backgroundColor: 'blue',
focusable: false,
webPreferences: {
nativeWindowOpen: true,
sandbox: true
}
}
},
proxy: {
snapshotFileName: 'proxy-window-open.snapshot.txt',
browserWindowOptions: {
show: false
}
}
};
for (const testName of Object.keys(testConfig) as (keyof typeof testConfig)[]) {
const { snapshotFileName, browserWindowOptions } = testConfig[testName];
describe(`for ${testName} window opening`, () => {
const snapshotFile = resolve(__dirname, 'fixtures', 'snapshots', snapshotFileName);
let browserWindow: BrowserWindow;
let existingSnapshots: any[];
before(() => {
existingSnapshots = parseSnapshots(readFileSync(snapshotFile, { encoding: 'utf8' }));
});
beforeEach((done) => {
browserWindow = new BrowserWindow(browserWindowOptions);
browserWindow.loadURL('about:blank');
browserWindow.on('ready-to-show', () => { done(); });
});
afterEach(closeAllWindows);
const newSnapshots: any[] = [];
[
'top=5,left=10,resizable=no',
'zoomFactor=2,resizable=0,x=0,y=10',
'backgroundColor=gray,webPreferences=0,x=100,y=100',
'x=50,y=20,title=sup',
'show=false,top=1,left=1'
].forEach((features, index) => {
/**
* ATTN: If this test is failing, you likely just need to change
* `shouldOverwriteSnapshot` to true and then evaluate the snapshot diff
* to see if the change is harmless.
*/
it(`matches snapshot for ${features}`, async () => {
const newSnapshot = await genSnapshot(browserWindow, features);
newSnapshots.push(newSnapshot);
// TODO: The output when these fail could be friendlier.
expect(stringifySnapshots(newSnapshot)).to.equal(stringifySnapshots(existingSnapshots[index]));
});
});
after(() => {
const shouldOverwriteSnapshot = false;
if (shouldOverwriteSnapshot) writeFileSync(snapshotFile, stringifySnapshots(newSnapshots, true));
});
});
}
});
describe('webContents.setWindowOpenHandler', () => {
const testConfig = {
native: {
browserWindowOptions: {
show: false,
webPreferences: {
nativeWindowOpen: true
}
}
},
proxy: {
browserWindowOptions: {
show: false,
webPreferences: {
nativeWindowOpen: false
}
}
}
};
for (const testName of Object.keys(testConfig) as (keyof typeof testConfig)[]) {
let browserWindow: BrowserWindow;
const { browserWindowOptions } = testConfig[testName];
describe(testName, () => {
beforeEach((done) => {
browserWindow = new BrowserWindow(browserWindowOptions);
browserWindow.loadURL('about:blank');
browserWindow.on('ready-to-show', () => { browserWindow.show(); done(); });
});
afterEach(closeAllWindows);
it('does not fire window creation events if an override returns action: deny', (done) => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
browserWindow.webContents.on('new-window', () => {
assert.fail('new-window should not to be called with an overridden window.open');
});
browserWindow.webContents.on('did-create-window', () => {
assert.fail('did-create-window should not to be called with an overridden window.open');
});
browserWindow.webContents.executeJavaScript("window.open('about:blank') && true");
setTimeout(() => {
done();
}, 500);
});
it('fires handler with correct params', (done) => {
const testFrameName = 'test-frame-name';
const testFeatures = 'top=10&left=10&something-unknown';
const testUrl = 'app://does-not-exist/';
browserWindow.webContents.setWindowOpenHandler(({ url, frameName, features }) => {
expect(url).to.equal(testUrl);
expect(frameName).to.equal(testFrameName);
expect(features).to.equal(testFeatures);
done();
return { action: 'deny' };
});
browserWindow.webContents.executeJavaScript(`window.open('${testUrl}', '${testFrameName}', '${testFeatures}') && true`);
});
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') && true");
});
await Promise.all([
emittedOnce(browserWindow.webContents, 'did-create-window'),
emittedOnce(browserWindow.webContents, 'new-window')
]);
});
it('can change webPreferences of child windows', (done) => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { defaultFontSize: 30 } } }));
browserWindow.webContents.on('did-create-window', async (childWindow) => {
await childWindow.webContents.executeJavaScript("document.write('hello')");
const size = await childWindow.webContents.executeJavaScript("getComputedStyle(document.querySelector('body')).fontSize");
expect(size).to.equal('30px');
done();
});
browserWindow.webContents.executeJavaScript("window.open('about:blank') && true");
});
});
}
});
function stringifySnapshots (snapshots: any, pretty = false) {
return JSON.stringify(snapshots, (key, value) => {
if (['sender', 'webContents'].includes(key)) {
return '[WebContents]';
}
if (key === 'openerId' && typeof value === 'number') {
return 'placeholder-opener-id';
}
if (key === 'processId' && typeof value === 'number') {
return 'placeholder-process-id';
}
if (key === 'returnValue') {
return 'placeholder-guest-contents-id';
}
return value;
}, pretty ? 2 : undefined);
}
function parseSnapshots (snapshotsJson: string) {
return JSON.parse(snapshotsJson, (key, value) => {
if (key === 'openerId' && value === 'placeholder-opener-id') return 1;
return value;
});
}