forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromium-spec.js
569 lines (513 loc) · 18.6 KB
/
chromium-spec.js
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
const { expect } = require('chai');
const fs = require('fs');
const http = require('http');
const path = require('path');
const ws = require('ws');
const url = require('url');
const ChildProcess = require('child_process');
const { ipcRenderer } = require('electron');
const { emittedOnce } = require('./events-helpers');
const { resolveGetters } = require('./expect-helpers');
const features = process.electronBinding('features');
/* Most of the APIs here don't use standard callbacks */
/* eslint-disable standard/no-callback-literal */
describe('chromium feature', () => {
const fixtures = path.resolve(__dirname, 'fixtures');
let listener = null;
afterEach(() => {
if (listener != null) {
window.removeEventListener('message', listener);
}
listener = null;
});
describe('heap snapshot', () => {
it('does not crash', function () {
process.electronBinding('v8_util').takeHeapSnapshot();
});
});
describe('navigator.webkitGetUserMedia', () => {
it('calls its callbacks', (done) => {
navigator.webkitGetUserMedia({
audio: true,
video: false
}, () => done(),
() => done());
});
});
describe('navigator.language', () => {
it('should not be empty', () => {
expect(navigator.language).to.not.equal('');
});
});
describe('navigator.geolocation', () => {
before(function () {
if (!features.isFakeLocationProviderEnabled()) {
return this.skip();
}
});
it('returns position when permission is granted', (done) => {
navigator.geolocation.getCurrentPosition((position) => {
expect(position).to.have.a.property('coords');
expect(position).to.have.a.property('timestamp');
done();
}, (error) => {
done(error);
});
});
});
describe('window.open', () => {
it('accepts "nodeIntegration" as feature', (done) => {
let b = null;
listener = (event) => {
expect(event.data.isProcessGlobalUndefined).to.be.true();
b.close();
done();
};
window.addEventListener('message', listener);
b = window.open(`file://${fixtures}/pages/window-opener-node.html`, '', 'nodeIntegration=no,show=no');
});
it('inherit options of parent window', (done) => {
let b = null;
listener = (event) => {
const width = outerWidth;
const height = outerHeight;
expect(event.data).to.equal(`size: ${width} ${height}`);
b.close();
done();
};
window.addEventListener('message', listener);
b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no');
});
it('disables node integration when it is disabled on the parent window', (done) => {
let b = null;
listener = (event) => {
expect(event.data.isProcessGlobalUndefined).to.be.true();
b.close();
done();
};
window.addEventListener('message', listener);
const windowUrl = require('url').format({
pathname: `${fixtures}/pages/window-opener-no-node-integration.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-node.html`
},
slashes: true
});
b = window.open(windowUrl, '', 'nodeIntegration=no,show=no');
});
it('disables the <webview> tag when it is disabled on the parent window', (done) => {
let b = null;
listener = (event) => {
expect(event.data.isWebViewGlobalUndefined).to.be.true();
b.close();
done();
};
window.addEventListener('message', listener);
const windowUrl = require('url').format({
pathname: `${fixtures}/pages/window-opener-no-webview-tag.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-webview.html`
},
slashes: true
});
b = window.open(windowUrl, '', 'webviewTag=no,nodeIntegration=yes,show=no');
});
it('does not override child options', (done) => {
let b = null;
const size = {
width: 350,
height: 450
};
listener = (event) => {
expect(event.data).to.equal(`size: ${size.width} ${size.height}`);
b.close();
done();
};
window.addEventListener('message', listener);
b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no,width=' + size.width + ',height=' + size.height);
});
it('throws an exception when the arguments cannot be converted to strings', () => {
expect(() => {
window.open('', { toString: null });
}).to.throw('Cannot convert object to primitive value');
expect(() => {
window.open('', '', { toString: 3 });
}).to.throw('Cannot convert object to primitive value');
});
it('does not throw an exception when the features include webPreferences', () => {
let b = null;
expect(() => {
b = window.open('', '', 'webPreferences=');
}).to.not.throw();
b.close();
});
});
describe('window.opener', () => {
it('is not null for window opened by window.open', (done) => {
let b = null;
listener = (event) => {
expect(event.data).to.equal('object');
b.close();
done();
};
window.addEventListener('message', listener);
b = window.open(`file://${fixtures}/pages/window-opener.html`, '', 'show=no');
});
});
describe('window.postMessage', () => {
it('throws an exception when the targetOrigin cannot be converted to a string', () => {
const b = window.open('');
expect(() => {
b.postMessage('test', { toString: null });
}).to.throw('Cannot convert object to primitive value');
b.close();
});
});
describe('window.opener.postMessage', () => {
it('sets source and origin correctly', (done) => {
let b = null;
listener = (event) => {
window.removeEventListener('message', listener);
expect(event.source).to.deep.equal(b);
b.close();
expect(event.origin).to.equal('file://');
done();
};
window.addEventListener('message', listener);
b = window.open(`file://${fixtures}/pages/window-opener-postMessage.html`, '', 'show=no');
});
it('supports windows opened from a <webview>', (done) => {
const webview = new WebView();
webview.addEventListener('console-message', (e) => {
webview.remove();
expect(e.message).to.equal('message');
done();
});
webview.allowpopups = true;
webview.src = url.format({
pathname: `${fixtures}/pages/webview-opener-postMessage.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-postMessage.html`
},
slashes: true
});
document.body.appendChild(webview);
});
describe('targetOrigin argument', () => {
let serverURL;
let server;
beforeEach((done) => {
server = http.createServer((req, res) => {
res.writeHead(200);
const filePath = path.join(fixtures, 'pages', 'window-opener-targetOrigin.html');
res.end(fs.readFileSync(filePath, 'utf8'));
});
server.listen(0, '127.0.0.1', () => {
serverURL = `http://127.0.0.1:${server.address().port}`;
done();
});
});
afterEach(() => {
server.close();
});
it('delivers messages that match the origin', (done) => {
let b = null;
listener = (event) => {
window.removeEventListener('message', listener);
b.close();
expect(event.data).to.equal('deliver');
done();
};
window.addEventListener('message', listener);
b = window.open(serverURL, '', 'show=no');
});
});
});
describe('webgl', () => {
before(function () {
if (process.platform === 'win32') {
this.skip();
}
});
it('can be get as context in canvas', () => {
if (process.platform === 'linux') {
// FIXME(alexeykuzmin): Skip the test.
// this.skip()
return;
}
const webgl = document.createElement('canvas').getContext('webgl');
expect(webgl).to.not.be.null();
});
});
describe('web workers', () => {
it('Worker can work', (done) => {
const worker = new Worker('../fixtures/workers/worker.js');
const message = 'ping';
worker.onmessage = (event) => {
expect(event.data).to.equal(message);
worker.terminate();
done();
};
worker.postMessage(message);
});
it('Worker has no node integration by default', (done) => {
const worker = new Worker('../fixtures/workers/worker_node.js');
worker.onmessage = (event) => {
expect(event.data).to.equal('undefined undefined undefined undefined');
worker.terminate();
done();
};
});
it('Worker has node integration with nodeIntegrationInWorker', (done) => {
const webview = new WebView();
webview.addEventListener('ipc-message', (e) => {
expect(e.channel).to.equal('object function object function');
webview.remove();
done();
});
webview.src = `file://${fixtures}/pages/worker.html`;
webview.setAttribute('webpreferences', 'nodeIntegration, nodeIntegrationInWorker');
document.body.appendChild(webview);
});
// FIXME: disabled during chromium update due to crash in content::WorkerScriptFetchInitiator::CreateScriptLoaderOnIO
xdescribe('SharedWorker', () => {
it('can work', (done) => {
const worker = new SharedWorker('../fixtures/workers/shared_worker.js');
const message = 'ping';
worker.port.onmessage = (event) => {
expect(event.data).to.equal(message);
done();
};
worker.port.postMessage(message);
});
it('has no node integration by default', (done) => {
const worker = new SharedWorker('../fixtures/workers/shared_worker_node.js');
worker.port.onmessage = (event) => {
expect(event.data).to.equal('undefined undefined undefined undefined');
done();
};
});
it('has node integration with nodeIntegrationInWorker', (done) => {
const webview = new WebView();
webview.addEventListener('console-message', (e) => {
console.log(e);
});
webview.addEventListener('ipc-message', (e) => {
expect(e.channel).to.equal('object function object function');
webview.remove();
done();
});
webview.src = `file://${fixtures}/pages/shared_worker.html`;
webview.setAttribute('webpreferences', 'nodeIntegration, nodeIntegrationInWorker');
document.body.appendChild(webview);
});
});
});
describe('iframe', () => {
let iframe = null;
beforeEach(() => {
iframe = document.createElement('iframe');
});
afterEach(() => {
document.body.removeChild(iframe);
});
it('does not have node integration', (done) => {
iframe.src = `file://${fixtures}/pages/set-global.html`;
document.body.appendChild(iframe);
iframe.onload = () => {
expect(iframe.contentWindow.test).to.equal('undefined undefined undefined');
done();
};
});
});
describe('storage', () => {
describe('DOM storage quota increase', () => {
['localStorage', 'sessionStorage'].forEach((storageName) => {
const storage = window[storageName];
it(`allows saving at least 40MiB in ${storageName}`, (done) => {
// Although JavaScript strings use UTF-16, the underlying
// storage provider may encode strings differently, muddling the
// translation between character and byte counts. However,
// a string of 40 * 2^20 characters will require at least 40MiB
// and presumably no more than 80MiB, a size guaranteed to
// to exceed the original 10MiB quota yet stay within the
// new 100MiB quota.
// Note that both the key name and value affect the total size.
const testKeyName = '_electronDOMStorageQuotaIncreasedTest';
const length = 40 * Math.pow(2, 20) - testKeyName.length;
storage.setItem(testKeyName, 'X'.repeat(length));
// Wait at least one turn of the event loop to help avoid false positives
// Although not entirely necessary, the previous version of this test case
// failed to detect a real problem (perhaps related to DOM storage data caching)
// wherein calling `getItem` immediately after `setItem` would appear to work
// but then later (e.g. next tick) it would not.
setTimeout(() => {
expect(storage.getItem(testKeyName)).to.have.lengthOf(length);
storage.removeItem(testKeyName);
done();
}, 1);
});
it(`throws when attempting to use more than 128MiB in ${storageName}`, () => {
expect(() => {
const testKeyName = '_electronDOMStorageQuotaStillEnforcedTest';
const length = 128 * Math.pow(2, 20) - testKeyName.length;
try {
storage.setItem(testKeyName, 'X'.repeat(length));
} finally {
storage.removeItem(testKeyName);
}
}).to.throw();
});
});
});
it('requesting persitent quota works', (done) => {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024, (grantedBytes) => {
expect(grantedBytes).to.equal(1048576);
done();
});
});
});
describe('websockets', () => {
let wss = null;
let server = null;
const WebSocketServer = ws.Server;
afterEach(() => {
wss.close();
server.close();
});
it('has user agent', (done) => {
server = http.createServer();
server.listen(0, '127.0.0.1', () => {
const port = server.address().port;
wss = new WebSocketServer({ server: server });
wss.on('error', done);
wss.on('connection', (ws, upgradeReq) => {
if (upgradeReq.headers['user-agent']) {
done();
} else {
done('user agent is empty');
}
});
const socket = new WebSocket(`ws://127.0.0.1:${port}`);
});
});
});
describe('Promise', () => {
it('resolves correctly in Node.js calls', (done) => {
class XElement extends HTMLElement {}
customElements.define('x-element', XElement);
setImmediate(() => {
let called = false;
Promise.resolve().then(() => {
done(called ? undefined : new Error('wrong sequence'));
});
document.createElement('x-element');
called = true;
});
});
it('resolves correctly in Electron calls', (done) => {
class YElement extends HTMLElement {}
customElements.define('y-element', YElement);
ipcRenderer.invoke('ping').then(() => {
let called = false;
Promise.resolve().then(() => {
done(called ? undefined : new Error('wrong sequence'));
});
document.createElement('y-element');
called = true;
});
});
});
describe('fetch', () => {
it('does not crash', (done) => {
const server = http.createServer((req, res) => {
res.end('test');
server.close();
});
server.listen(0, '127.0.0.1', () => {
const port = server.address().port;
fetch(`http://127.0.0.1:${port}`).then((res) => res.body.getReader())
.then((reader) => {
reader.read().then((r) => {
reader.cancel();
done();
});
}).catch((e) => done(e));
});
});
});
describe('window.alert(message, title)', () => {
it('throws an exception when the arguments cannot be converted to strings', () => {
expect(() => {
window.alert({ toString: null });
}).to.throw('Cannot convert object to primitive value');
});
});
describe('window.confirm(message, title)', () => {
it('throws an exception when the arguments cannot be converted to strings', () => {
expect(() => {
window.confirm({ toString: null }, 'title');
}).to.throw('Cannot convert object to primitive value');
});
});
describe('window.history', () => {
describe('window.history.go(offset)', () => {
it('throws an exception when the argumnet cannot be converted to a string', () => {
expect(() => {
window.history.go({ toString: null });
}).to.throw('Cannot convert object to primitive value');
});
});
});
// TODO(nornagon): this is broken on CI, it triggers:
// [FATAL:speech_synthesis.mojom-shared.h(237)] The outgoing message will
// trigger VALIDATION_ERROR_UNEXPECTED_NULL_POINTER at the receiving side
// (null text in SpeechSynthesisUtterance struct).
describe.skip('SpeechSynthesis', () => {
before(function () {
if (!features.isTtsEnabled()) {
this.skip();
}
});
it('should emit lifecycle events', async () => {
const sentence = `long sentence which will take at least a few seconds to
utter so that it's possible to pause and resume before the end`;
const utter = new SpeechSynthesisUtterance(sentence);
// Create a dummy utterence so that speech synthesis state
// is initialized for later calls.
speechSynthesis.speak(new SpeechSynthesisUtterance());
speechSynthesis.cancel();
speechSynthesis.speak(utter);
// paused state after speak()
expect(speechSynthesis.paused).to.be.false();
await new Promise((resolve) => { utter.onstart = resolve; });
// paused state after start event
expect(speechSynthesis.paused).to.be.false();
speechSynthesis.pause();
// paused state changes async, right before the pause event
expect(speechSynthesis.paused).to.be.false();
await new Promise((resolve) => { utter.onpause = resolve; });
expect(speechSynthesis.paused).to.be.true();
speechSynthesis.resume();
await new Promise((resolve) => { utter.onresume = resolve; });
// paused state after resume event
expect(speechSynthesis.paused).to.be.false();
await new Promise((resolve) => { utter.onend = resolve; });
});
});
});
describe('console functions', () => {
it('should exist', () => {
expect(console.log, 'log').to.be.a('function');
expect(console.error, 'error').to.be.a('function');
expect(console.warn, 'warn').to.be.a('function');
expect(console.info, 'info').to.be.a('function');
expect(console.debug, 'debug').to.be.a('function');
expect(console.trace, 'trace').to.be.a('function');
expect(console.time, 'time').to.be.a('function');
expect(console.timeEnd, 'timeEnd').to.be.a('function');
});
});