forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-ipc-spec.js
609 lines (512 loc) · 20.2 KB
/
api-ipc-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
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
'use strict'
const assert = require('assert')
const http = require('http')
const path = require('path')
const {closeWindow} = require('./window-helpers')
const {ipcRenderer, remote} = require('electron')
const {ipcMain, webContents, BrowserWindow} = remote
const comparePaths = function (path1, path2) {
if (process.platform === 'win32') {
path1 = path1.toLowerCase()
path2 = path2.toLowerCase()
}
assert.equal(path1, path2)
}
describe('ipc module', function () {
var fixtures = path.join(__dirname, 'fixtures')
var w = null
afterEach(function () {
return closeWindow(w).then(function () { w = null })
})
describe('remote.require', function () {
it('should returns same object for the same module', function () {
var dialog1 = remote.require('electron')
var dialog2 = remote.require('electron')
assert.equal(dialog1, dialog2)
})
it('should work when object contains id property', function () {
var a = remote.require(path.join(fixtures, 'module', 'id.js'))
assert.equal(a.id, 1127)
})
it('should work when object has no prototype', function () {
var a = remote.require(path.join(fixtures, 'module', 'no-prototype.js'))
assert.equal(a.foo.constructor.name, '')
assert.equal(a.foo.bar, 'baz')
assert.equal(a.foo.baz, false)
assert.equal(a.bar, 1234)
assert.equal(a.anonymous.constructor.name, '')
assert.equal(a.getConstructorName(Object.create(null)), '')
assert.equal(a.getConstructorName(new (class {})()), '')
})
it('should search module from the user app', function () {
comparePaths(path.normalize(remote.process.mainModule.filename), path.resolve(__dirname, 'static', 'main.js'))
comparePaths(path.normalize(remote.process.mainModule.paths[0]), path.resolve(__dirname, 'static', 'node_modules'))
})
it('should work with function properties', function () {
var a = remote.require(path.join(fixtures, 'module', 'export-function-with-properties.js'))
assert.equal(typeof a, 'function')
assert.equal(a.bar, 'baz')
a = remote.require(path.join(fixtures, 'module', 'function-with-properties.js'))
assert.equal(typeof a, 'object')
assert.equal(a.foo(), 'hello')
assert.equal(a.foo.bar, 'baz')
assert.equal(a.foo.nested.prop, 'yes')
assert.equal(a.foo.method1(), 'world')
assert.equal(a.foo.method1.prop1(), 123)
assert.ok(Object.keys(a.foo).includes('bar'))
assert.ok(Object.keys(a.foo).includes('nested'))
assert.ok(Object.keys(a.foo).includes('method1'))
a = remote.require(path.join(fixtures, 'module', 'function-with-missing-properties.js')).setup()
assert.equal(a.bar(), true)
assert.equal(a.bar.baz, undefined)
})
it('should work with static class members', function () {
var a = remote.require(path.join(fixtures, 'module', 'remote-static.js'))
assert.equal(typeof a.Foo, 'function')
assert.equal(a.Foo.foo(), 3)
assert.equal(a.Foo.bar, 'baz')
var foo = new a.Foo()
assert.equal(foo.baz(), 123)
})
it('includes the length of functions specified as arguments', function () {
var a = remote.require(path.join(fixtures, 'module', 'function-with-args.js'))
assert.equal(a(function (a, b, c, d, f) {}), 5)
assert.equal(a((a) => {}), 1)
assert.equal(a((...args) => {}), 0)
})
it('handles circular references in arrays and objects', function () {
var a = remote.require(path.join(fixtures, 'module', 'circular.js'))
var arrayA = ['foo']
var arrayB = [arrayA, 'bar']
arrayA.push(arrayB)
assert.deepEqual(a.returnArgs(arrayA, arrayB), [
['foo', [null, 'bar']],
[['foo', null], 'bar']
])
var objectA = {foo: 'bar'}
var objectB = {baz: objectA}
objectA.objectB = objectB
assert.deepEqual(a.returnArgs(objectA, objectB), [
{foo: 'bar', objectB: {baz: null}},
{baz: {foo: 'bar', objectB: null}}
])
arrayA = [1, 2, 3]
assert.deepEqual(a.returnArgs({foo: arrayA}, {bar: arrayA}), [
{foo: [1, 2, 3]},
{bar: [1, 2, 3]}
])
objectA = {foo: 'bar'}
assert.deepEqual(a.returnArgs({foo: objectA}, {bar: objectA}), [
{foo: {foo: 'bar'}},
{bar: {foo: 'bar'}}
])
arrayA = []
arrayA.push(arrayA)
assert.deepEqual(a.returnArgs(arrayA), [
[null]
])
objectA = {}
objectA.foo = objectA
objectA.bar = 'baz'
assert.deepEqual(a.returnArgs(objectA), [
{foo: null, bar: 'baz'}
])
objectA = {}
objectA.foo = {bar: objectA}
objectA.bar = 'baz'
assert.deepEqual(a.returnArgs(objectA), [
{foo: {bar: null}, bar: 'baz'}
])
})
})
describe('remote.createFunctionWithReturnValue', function () {
it('should be called in browser synchronously', function () {
var buf = new Buffer('test')
var call = remote.require(path.join(fixtures, 'module', 'call.js'))
var result = call.call(remote.createFunctionWithReturnValue(buf))
assert.equal(result.constructor.name, 'Buffer')
})
})
describe('remote modules', function () {
it('includes browser process modules as properties', function () {
assert.equal(typeof remote.app.getPath, 'function')
assert.equal(typeof remote.webContents.getFocusedWebContents, 'function')
assert.equal(typeof remote.clipboard.readText, 'function')
assert.equal(typeof remote.shell.openExternal, 'function')
})
it('returns toString() of original function via toString()', function () {
const {readText} = remote.clipboard
assert(readText.toString().startsWith('function'))
var {functionWithToStringProperty} = remote.require(path.join(fixtures, 'module', 'to-string-non-function.js'))
assert.equal(functionWithToStringProperty.toString, 'hello')
})
})
describe('remote object in renderer', function () {
it('can change its properties', function () {
var property = remote.require(path.join(fixtures, 'module', 'property.js'))
assert.equal(property.property, 1127)
property.property = null
assert.equal(property.property, null)
property.property = undefined
assert.equal(property.property, undefined)
property.property = 1007
assert.equal(property.property, 1007)
assert.equal(property.getFunctionProperty(), 'foo-browser')
property.func.property = 'bar'
assert.equal(property.getFunctionProperty(), 'bar-browser')
property.func.property = 'foo' // revert back
var property2 = remote.require(path.join(fixtures, 'module', 'property.js'))
assert.equal(property2.property, 1007)
property.property = 1127
})
it('rethrows errors getting/setting properties', function () {
const foo = remote.require(path.join(fixtures, 'module', 'error-properties.js'))
assert.throws(function () {
foo.bar
}, /getting error/)
assert.throws(function () {
foo.bar = 'test'
}, /setting error/)
})
it('can set a remote property with a remote object', function () {
const foo = remote.require(path.join(fixtures, 'module', 'remote-object-set.js'))
assert.doesNotThrow(function () {
foo.bar = remote.getCurrentWindow()
})
})
it('can construct an object from its member', function () {
var call = remote.require(path.join(fixtures, 'module', 'call.js'))
var obj = new call.constructor()
assert.equal(obj.test, 'test')
})
it('can reassign and delete its member functions', function () {
var remoteFunctions = remote.require(path.join(fixtures, 'module', 'function.js'))
assert.equal(remoteFunctions.aFunction(), 1127)
remoteFunctions.aFunction = function () { return 1234 }
assert.equal(remoteFunctions.aFunction(), 1234)
assert.equal(delete remoteFunctions.aFunction, true)
})
it('is referenced by its members', function () {
let stringify = remote.getGlobal('JSON').stringify
global.gc()
stringify({})
})
})
describe('remote value in browser', function () {
const print = path.join(fixtures, 'module', 'print_name.js')
const printName = remote.require(print)
it('keeps its constructor name for objects', function () {
const buf = new Buffer('test')
assert.equal(printName.print(buf), 'Buffer')
})
it('supports instanceof Date', function () {
const now = new Date()
assert.equal(printName.print(now), 'Date')
assert.deepEqual(printName.echo(now), now)
})
it('supports instanceof Buffer', function () {
const buffer = Buffer.from('test')
assert.ok(buffer.equals(printName.echo(buffer)))
const objectWithBuffer = {a: 'foo', b: Buffer.from('bar')}
assert.ok(objectWithBuffer.b.equals(printName.echo(objectWithBuffer).b))
const arrayWithBuffer = [1, 2, Buffer.from('baz')]
assert.ok(arrayWithBuffer[2].equals(printName.echo(arrayWithBuffer)[2]))
})
it('supports TypedArray', function () {
const values = [1, 2, 3, 4]
assert.deepEqual(printName.typedArray(values), values)
const int16values = new Int16Array([1, 2, 3, 4])
assert.deepEqual(printName.typedArray(int16values), int16values)
})
})
describe('remote promise', function () {
it('can be used as promise in each side', function (done) {
var promise = remote.require(path.join(fixtures, 'module', 'promise.js'))
promise.twicePromise(Promise.resolve(1234)).then(function (value) {
assert.equal(value, 2468)
done()
})
})
it('handles rejections via catch(onRejected)', function (done) {
var promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
promise.reject(Promise.resolve(1234)).catch(function (error) {
assert.equal(error.message, 'rejected')
done()
})
})
it('handles rejections via then(onFulfilled, onRejected)', function (done) {
var promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
promise.reject(Promise.resolve(1234)).then(function () {}, function (error) {
assert.equal(error.message, 'rejected')
done()
})
})
it('does not emit unhandled rejection events in the main process', function (done) {
remote.process.once('unhandledRejection', function (reason) {
done(reason)
})
var promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
promise.reject().then(function () {
done(new Error('Promise was not rejected'))
}).catch(function (error) {
assert.equal(error.message, 'rejected')
done()
})
})
it('emits unhandled rejection events in the renderer process', function (done) {
window.addEventListener('unhandledrejection', function (event) {
event.preventDefault()
assert.equal(event.reason.message, 'rejected')
done()
})
var promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
promise.reject().then(function () {
done(new Error('Promise was not rejected'))
})
})
})
describe('remote webContents', function () {
it('can return same object with different getters', function () {
var contents1 = remote.getCurrentWindow().webContents
var contents2 = remote.getCurrentWebContents()
assert(contents1 === contents2)
})
})
describe('remote class', function () {
let cl = remote.require(path.join(fixtures, 'module', 'class.js'))
let base = cl.base
let derived = cl.derived
it('can get methods', function () {
assert.equal(base.method(), 'method')
})
it('can get properties', function () {
assert.equal(base.readonly, 'readonly')
})
it('can change properties', function () {
assert.equal(base.value, 'old')
base.value = 'new'
assert.equal(base.value, 'new')
base.value = 'old'
})
it('has unenumerable methods', function () {
assert(!base.hasOwnProperty('method'))
assert(Object.getPrototypeOf(base).hasOwnProperty('method'))
})
it('keeps prototype chain in derived class', function () {
assert.equal(derived.method(), 'method')
assert.equal(derived.readonly, 'readonly')
assert(!derived.hasOwnProperty('method'))
let proto = Object.getPrototypeOf(derived)
assert(!proto.hasOwnProperty('method'))
assert(Object.getPrototypeOf(proto).hasOwnProperty('method'))
})
it('is referenced by methods in prototype chain', function () {
let method = derived.method
derived = null
global.gc()
assert.equal(method(), 'method')
})
})
describe('ipc.sender.send', function () {
it('should work when sending an object containing id property', function (done) {
var obj = {
id: 1,
name: 'ly'
}
ipcRenderer.once('message', function (event, message) {
assert.deepEqual(message, obj)
done()
})
ipcRenderer.send('message', obj)
})
it('can send instances of Date', function (done) {
const currentDate = new Date()
ipcRenderer.once('message', function (event, value) {
assert.equal(value, currentDate.toISOString())
done()
})
ipcRenderer.send('message', currentDate)
})
it('can send instances of Buffer', function (done) {
const buffer = Buffer.from('hello')
ipcRenderer.once('message', function (event, message) {
assert.ok(buffer.equals(message))
done()
})
ipcRenderer.send('message', buffer)
})
it('can send objects with DOM class prototypes', function (done) {
ipcRenderer.once('message', function (event, value) {
assert.equal(value.protocol, 'file:')
assert.equal(value.hostname, '')
done()
})
ipcRenderer.send('message', document.location)
})
it('can send Electron API objects', function (done) {
const webContents = remote.getCurrentWebContents()
ipcRenderer.once('message', function (event, value) {
assert.deepEqual(value.browserWindowOptions, webContents.browserWindowOptions)
done()
})
ipcRenderer.send('message', webContents)
})
it('does not crash on external objects (regression)', function (done) {
const request = http.request({port: 5000, hostname: '127.0.0.1', method: 'GET', path: '/'})
const stream = request.agent.sockets['127.0.0.1:5000:'][0]._handle._externalStream
request.on('error', function () {})
ipcRenderer.once('message', function (event, requestValue, externalStreamValue) {
assert.equal(requestValue.method, 'GET')
assert.equal(requestValue.path, '/')
assert.equal(externalStreamValue, null)
done()
})
ipcRenderer.send('message', request, stream)
})
it('can send objects that both reference the same object', function (done) {
const child = {hello: 'world'}
const foo = {name: 'foo', child: child}
const bar = {name: 'bar', child: child}
const array = [foo, bar]
ipcRenderer.once('message', function (event, arrayValue, fooValue, barValue, childValue) {
assert.deepEqual(arrayValue, array)
assert.deepEqual(fooValue, foo)
assert.deepEqual(barValue, bar)
assert.deepEqual(childValue, child)
done()
})
ipcRenderer.send('message', array, foo, bar, child)
})
it('inserts null for cyclic references', function (done) {
const array = [5]
array.push(array)
const child = {hello: 'world'}
child.child = child
ipcRenderer.once('message', function (event, arrayValue, childValue) {
assert.equal(arrayValue[0], 5)
assert.equal(arrayValue[1], null)
assert.equal(childValue.hello, 'world')
assert.equal(childValue.child, null)
done()
})
ipcRenderer.send('message', array, child)
})
})
describe('ipc.sendSync', function () {
afterEach(function () {
ipcMain.removeAllListeners('send-sync-message')
})
it('can be replied by setting event.returnValue', function () {
var msg = ipcRenderer.sendSync('echo', 'test')
assert.equal(msg, 'test')
})
it('does not crash when reply is not sent and browser is destroyed', function (done) {
w = new BrowserWindow({
show: false
})
ipcMain.once('send-sync-message', function (event) {
event.returnValue = null
done()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
})
it('does not crash when reply is sent by multiple listeners', function (done) {
w = new BrowserWindow({
show: false
})
ipcMain.on('send-sync-message', function (event) {
event.returnValue = null
})
ipcMain.on('send-sync-message', function (event) {
event.returnValue = null
done()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
})
})
describe('ipcRenderer.sendTo', function () {
let contents = null
beforeEach(function () {
contents = webContents.create({})
})
afterEach(function () {
ipcRenderer.removeAllListeners('pong')
contents.destroy()
contents = null
})
it('sends message to WebContents', function (done) {
const webContentsId = remote.getCurrentWebContents().id
ipcRenderer.once('pong', function (event, id) {
assert.equal(webContentsId, id)
done()
})
contents.once('did-finish-load', function () {
ipcRenderer.sendTo(contents.id, 'ping', webContentsId)
})
contents.loadURL('file://' + path.join(fixtures, 'pages', 'ping-pong.html'))
})
})
describe('remote listeners', function () {
it('can be added and removed correctly', function () {
w = new BrowserWindow({
show: false
})
var listener = function () {}
w.on('test', listener)
assert.equal(w.listenerCount('test'), 1)
w.removeListener('test', listener)
assert.equal(w.listenerCount('test'), 0)
})
it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => {
w = new BrowserWindow({
show: false
})
w.webContents.once('did-finish-load', () => {
w.webContents.once('did-finish-load', () => {
const expectedMessage = [
'Attempting to call a function in a renderer window that has been closed or released.',
'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler'
].join('\n')
const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.deepEqual(results, {
warningMessage: expectedMessage,
listenerCountBefore: 2,
listenerCountAfter: 1
})
done()
})
w.webContents.reload()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'remote-event-handler.html'))
})
})
it('throws an error when removing all the listeners', () => {
ipcMain.on('test-event', () => {})
assert.equal(ipcMain.listenerCount('test-event'), 1)
ipcRenderer.on('test-event', () => {})
assert.equal(ipcRenderer.listenerCount('test-event'), 1)
assert.throws(() => {
ipcMain.removeAllListeners()
}, /Removing all listeners from ipcMain will make Electron internals stop working/)
assert.throws(() => {
ipcRenderer.removeAllListeners()
}, /Removing all listeners from ipcRenderer will make Electron internals stop working/)
ipcMain.removeAllListeners('test-event')
assert.equal(ipcMain.listenerCount('test-event'), 0)
ipcRenderer.removeAllListeners('test-event')
assert.equal(ipcRenderer.listenerCount('test-event'), 0)
})
describe('remote objects registry', function () {
it('does not dereference until the render view is deleted (regression)', function (done) {
w = new BrowserWindow({
show: false
})
ipcMain.once('error-message', (event, message) => {
assert(message.startsWith('Cannot call function \'getURL\' on missing remote object'), message)
done()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'render-view-deleted.html'))
})
})
})