2016-03-25 20:03:49 +00:00
|
|
|
'use strict'
|
2016-02-22 04:13:26 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2016-08-26 16:36:25 +00:00
|
|
|
const http = require('http')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
2016-08-03 19:47:53 +00:00
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-05-18 13:14:51 +00:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
|
|
|
const {ipcMain, webContents, BrowserWindow} = remote
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const comparePaths = (path1, path2) => {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.platform === 'win32') {
|
2016-03-25 20:03:49 +00:00
|
|
|
path1 = path1.toLowerCase()
|
|
|
|
path2 = path2.toLowerCase()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(path1, path2)
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ipc module', () => {
|
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
let w = null
|
2016-08-03 19:47:53 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => closeWindow(w).then(() => { w = null }))
|
2016-08-03 19:47:53 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote.require', () => {
|
|
|
|
it('should returns same object for the same module', () => {
|
|
|
|
const dialog1 = remote.require('electron')
|
|
|
|
const dialog2 = remote.require('electron')
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(dialog1, dialog2)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should work when object contains id property', () => {
|
|
|
|
const a = remote.require(path.join(fixtures, 'module', 'id.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(a.id, 1127)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should work when object has no prototype', () => {
|
|
|
|
const a = remote.require(path.join(fixtures, 'module', 'no-prototype.js'))
|
2016-07-06 20:06:48 +00:00
|
|
|
assert.equal(a.foo.constructor.name, '')
|
2016-05-27 17:52:56 +00:00
|
|
|
assert.equal(a.foo.bar, 'baz')
|
|
|
|
assert.equal(a.foo.baz, false)
|
2016-05-27 17:41:10 +00:00
|
|
|
assert.equal(a.bar, 1234)
|
2016-07-06 20:11:25 +00:00
|
|
|
assert.equal(a.anonymous.constructor.name, '')
|
2016-07-06 20:06:48 +00:00
|
|
|
assert.equal(a.getConstructorName(Object.create(null)), '')
|
2016-10-13 01:47:43 +00:00
|
|
|
assert.equal(a.getConstructorName(new (class {})()), '')
|
2016-05-27 17:41:10 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should search module from the user app', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
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'))
|
|
|
|
})
|
2016-07-11 16:52:34 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should work with function properties', () => {
|
|
|
|
let a = remote.require(path.join(fixtures, 'module', 'export-function-with-properties.js'))
|
2016-08-16 16:24:38 +00:00
|
|
|
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')
|
2016-08-16 20:54:21 +00:00
|
|
|
assert.equal(a.foo(), 'hello')
|
2016-08-16 16:24:38 +00:00
|
|
|
assert.equal(a.foo.bar, 'baz')
|
2016-08-16 17:37:51 +00:00
|
|
|
assert.equal(a.foo.nested.prop, 'yes')
|
2016-08-16 20:54:21 +00:00
|
|
|
assert.equal(a.foo.method1(), 'world')
|
|
|
|
assert.equal(a.foo.method1.prop1(), 123)
|
2016-08-17 20:58:48 +00:00
|
|
|
|
|
|
|
assert.ok(Object.keys(a.foo).includes('bar'))
|
|
|
|
assert.ok(Object.keys(a.foo).includes('nested'))
|
|
|
|
assert.ok(Object.keys(a.foo).includes('method1'))
|
2016-09-14 17:43:55 +00:00
|
|
|
|
2016-09-14 18:16:44 +00:00
|
|
|
a = remote.require(path.join(fixtures, 'module', 'function-with-missing-properties.js')).setup()
|
2016-09-14 17:43:55 +00:00
|
|
|
assert.equal(a.bar(), true)
|
2016-09-14 20:21:44 +00:00
|
|
|
assert.equal(a.bar.baz, undefined)
|
2016-08-16 16:24:38 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should work with static class members', () => {
|
|
|
|
const a = remote.require(path.join(fixtures, 'module', 'remote-static.js'))
|
2016-08-16 17:32:32 +00:00
|
|
|
assert.equal(typeof a.Foo, 'function')
|
|
|
|
assert.equal(a.Foo.foo(), 3)
|
|
|
|
assert.equal(a.Foo.bar, 'baz')
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const foo = new a.Foo()
|
2016-08-16 17:32:32 +00:00
|
|
|
assert.equal(foo.baz(), 123)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('includes the length of functions specified as arguments', () => {
|
|
|
|
const a = remote.require(path.join(fixtures, 'module', 'function-with-args.js'))
|
|
|
|
assert.equal(a((a, b, c, d, f) => {}), 5)
|
2017-02-07 23:24:49 +00:00
|
|
|
assert.equal(a((a) => {}), 1)
|
|
|
|
assert.equal(a((...args) => {}), 0)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('handles circular references in arrays and objects', () => {
|
|
|
|
const a = remote.require(path.join(fixtures, 'module', 'circular.js'))
|
2016-07-11 17:27:43 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
let arrayA = ['foo']
|
|
|
|
const arrayB = [arrayA, 'bar']
|
2016-07-11 17:27:43 +00:00
|
|
|
arrayA.push(arrayB)
|
|
|
|
assert.deepEqual(a.returnArgs(arrayA, arrayB), [
|
|
|
|
['foo', [null, 'bar']],
|
|
|
|
[['foo', null], 'bar']
|
|
|
|
])
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
let objectA = {foo: 'bar'}
|
|
|
|
const objectB = {baz: objectA}
|
2016-07-11 17:27:43 +00:00
|
|
|
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]}
|
|
|
|
])
|
|
|
|
|
2016-07-11 17:49:23 +00:00
|
|
|
objectA = {foo: 'bar'}
|
|
|
|
assert.deepEqual(a.returnArgs({foo: objectA}, {bar: objectA}), [
|
|
|
|
{foo: {foo: 'bar'}},
|
|
|
|
{bar: {foo: 'bar'}}
|
|
|
|
])
|
|
|
|
|
2016-07-11 17:27:43 +00:00
|
|
|
arrayA = []
|
|
|
|
arrayA.push(arrayA)
|
|
|
|
assert.deepEqual(a.returnArgs(arrayA), [
|
|
|
|
[null]
|
|
|
|
])
|
|
|
|
|
2016-07-11 17:30:18 +00:00
|
|
|
objectA = {}
|
2016-07-11 17:27:43 +00:00
|
|
|
objectA.foo = objectA
|
2016-07-11 17:49:23 +00:00
|
|
|
objectA.bar = 'baz'
|
2016-07-11 17:27:43 +00:00
|
|
|
assert.deepEqual(a.returnArgs(objectA), [
|
2016-07-11 17:49:23 +00:00
|
|
|
{foo: null, bar: 'baz'}
|
2016-07-11 16:52:34 +00:00
|
|
|
])
|
|
|
|
|
2016-07-11 17:27:43 +00:00
|
|
|
objectA = {}
|
|
|
|
objectA.foo = {bar: objectA}
|
2016-07-11 17:49:23 +00:00
|
|
|
objectA.bar = 'baz'
|
2016-07-11 17:27:43 +00:00
|
|
|
assert.deepEqual(a.returnArgs(objectA), [
|
2016-07-11 17:49:23 +00:00
|
|
|
{foo: {bar: null}, bar: 'baz'}
|
2016-07-11 16:52:34 +00:00
|
|
|
])
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote.createFunctionWithReturnValue', () => {
|
|
|
|
it('should be called in browser synchronously', () => {
|
2017-11-23 22:22:31 +00:00
|
|
|
const buf = Buffer.from('test')
|
2017-10-27 20:45:58 +00:00
|
|
|
const call = remote.require(path.join(fixtures, 'module', 'call.js'))
|
|
|
|
const result = call.call(remote.createFunctionWithReturnValue(buf))
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(result.constructor.name, 'Buffer')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote modules', () => {
|
|
|
|
it('includes browser process modules as properties', () => {
|
2017-02-24 18:43:39 +00:00
|
|
|
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')
|
|
|
|
})
|
2017-03-17 15:21:37 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('returns toString() of original function via toString()', () => {
|
2017-03-17 17:28:23 +00:00
|
|
|
const {readText} = remote.clipboard
|
2017-03-17 15:21:37 +00:00
|
|
|
assert(readText.toString().startsWith('function'))
|
2017-03-17 17:28:23 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const {functionWithToStringProperty} = remote.require(path.join(fixtures, 'module', 'to-string-non-function.js'))
|
2017-03-17 17:28:23 +00:00
|
|
|
assert.equal(functionWithToStringProperty.toString, 'hello')
|
2017-03-17 15:21:37 +00:00
|
|
|
})
|
2017-02-24 18:43:39 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote object in renderer', () => {
|
|
|
|
it('can change its properties', () => {
|
|
|
|
const property = remote.require(path.join(fixtures, 'module', 'property.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(property.property, 1127)
|
2017-04-03 21:26:39 +00:00
|
|
|
|
|
|
|
property.property = null
|
|
|
|
assert.equal(property.property, null)
|
|
|
|
property.property = undefined
|
|
|
|
assert.equal(property.property, undefined)
|
2016-03-25 20:03:49 +00:00
|
|
|
property.property = 1007
|
|
|
|
assert.equal(property.property, 1007)
|
2017-04-03 21:26:39 +00:00
|
|
|
|
2016-08-17 21:21:50 +00:00
|
|
|
assert.equal(property.getFunctionProperty(), 'foo-browser')
|
|
|
|
property.func.property = 'bar'
|
|
|
|
assert.equal(property.getFunctionProperty(), 'bar-browser')
|
2016-09-08 05:42:59 +00:00
|
|
|
property.func.property = 'foo' // revert back
|
2016-08-17 21:21:50 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const property2 = remote.require(path.join(fixtures, 'module', 'property.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(property2.property, 1007)
|
|
|
|
property.property = 1127
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('rethrows errors getting/setting properties', () => {
|
2017-04-03 16:09:37 +00:00
|
|
|
const foo = remote.require(path.join(fixtures, 'module', 'error-properties.js'))
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
assert.throws(() => {
|
2017-11-23 22:22:43 +00:00
|
|
|
// eslint-disable-next-line
|
2017-04-03 16:09:37 +00:00
|
|
|
foo.bar
|
|
|
|
}, /getting error/)
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
assert.throws(() => {
|
2017-04-03 16:09:37 +00:00
|
|
|
foo.bar = 'test'
|
|
|
|
}, /setting error/)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can set a remote property with a remote object', () => {
|
2017-04-03 21:17:16 +00:00
|
|
|
const foo = remote.require(path.join(fixtures, 'module', 'remote-object-set.js'))
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
assert.doesNotThrow(() => {
|
2017-04-03 21:17:16 +00:00
|
|
|
foo.bar = remote.getCurrentWindow()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can construct an object from its member', () => {
|
|
|
|
const call = remote.require(path.join(fixtures, 'module', 'call.js'))
|
|
|
|
const obj = new call.constructor()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(obj.test, 'test')
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can reassign and delete its member functions', () => {
|
|
|
|
const remoteFunctions = remote.require(path.join(fixtures, 'module', 'function.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(remoteFunctions.aFunction(), 1127)
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
remoteFunctions.aFunction = () => { return 1234 }
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(remoteFunctions.aFunction(), 1234)
|
|
|
|
|
|
|
|
assert.equal(delete remoteFunctions.aFunction, true)
|
|
|
|
})
|
2016-04-01 05:50:33 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('is referenced by its members', () => {
|
2016-04-01 05:50:33 +00:00
|
|
|
let stringify = remote.getGlobal('JSON').stringify
|
2016-06-29 16:37:10 +00:00
|
|
|
global.gc()
|
2016-04-01 05:50:33 +00:00
|
|
|
stringify({})
|
2016-06-29 16:37:10 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote value in browser', () => {
|
2016-07-25 07:30:40 +00:00
|
|
|
const print = path.join(fixtures, 'module', 'print_name.js')
|
|
|
|
const printName = remote.require(print)
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('keeps its constructor name for objects', () => {
|
2017-11-23 22:22:31 +00:00
|
|
|
const buf = Buffer.from('test')
|
2016-06-29 16:37:10 +00:00
|
|
|
assert.equal(printName.print(buf), 'Buffer')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('supports instanceof Date', () => {
|
2016-07-25 07:30:40 +00:00
|
|
|
const now = new Date()
|
2016-06-29 16:37:10 +00:00
|
|
|
assert.equal(printName.print(now), 'Date')
|
|
|
|
assert.deepEqual(printName.echo(now), now)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-07-25 07:30:40 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('supports instanceof Buffer', () => {
|
2016-08-24 22:01:52 +00:00
|
|
|
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))
|
|
|
|
|
2016-08-24 23:41:59 +00:00
|
|
|
const arrayWithBuffer = [1, 2, Buffer.from('baz')]
|
2016-08-24 22:01:52 +00:00
|
|
|
assert.ok(arrayWithBuffer[2].equals(printName.echo(arrayWithBuffer)[2]))
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('supports TypedArray', () => {
|
2016-08-24 23:59:54 +00:00
|
|
|
const values = [1, 2, 3, 4]
|
2016-08-24 23:46:56 +00:00
|
|
|
assert.deepEqual(printName.typedArray(values), values)
|
|
|
|
|
2016-08-24 23:59:54 +00:00
|
|
|
const int16values = new Int16Array([1, 2, 3, 4])
|
|
|
|
assert.deepEqual(printName.typedArray(int16values), int16values)
|
2016-07-25 07:30:40 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote promise', () => {
|
|
|
|
it('can be used as promise in each side', (done) => {
|
|
|
|
const promise = remote.require(path.join(fixtures, 'module', 'promise.js'))
|
|
|
|
promise.twicePromise(Promise.resolve(1234)).then((value) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(value, 2468)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-05-23 22:06:46 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('handles rejections via catch(onRejected)', (done) => {
|
|
|
|
const promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
|
|
|
|
promise.reject(Promise.resolve(1234)).catch((error) => {
|
2016-05-23 22:06:46 +00:00
|
|
|
assert.equal(error.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('handles rejections via then(onFulfilled, onRejected)', (done) => {
|
|
|
|
const promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
|
|
|
|
promise.reject(Promise.resolve(1234)).then(() => {}, (error) => {
|
2016-05-23 22:06:46 +00:00
|
|
|
assert.equal(error.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-06-21 00:54:15 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not emit unhandled rejection events in the main process', (done) => {
|
2016-06-21 00:54:15 +00:00
|
|
|
remote.process.once('unhandledRejection', function (reason) {
|
|
|
|
done(reason)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
|
|
|
|
promise.reject().then(() => {
|
2016-06-21 00:54:15 +00:00
|
|
|
done(new Error('Promise was not rejected'))
|
2017-10-27 20:45:58 +00:00
|
|
|
}).catch((error) => {
|
2016-06-21 00:54:15 +00:00
|
|
|
assert.equal(error.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('emits unhandled rejection events in the renderer process', (done) => {
|
2016-06-21 00:54:15 +00:00
|
|
|
window.addEventListener('unhandledrejection', function (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
assert.equal(event.reason.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
|
|
|
|
promise.reject().then(() => {
|
2016-06-21 00:54:15 +00:00
|
|
|
done(new Error('Promise was not rejected'))
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote webContents', () => {
|
|
|
|
it('can return same object with different getters', () => {
|
|
|
|
const contents1 = remote.getCurrentWindow().webContents
|
|
|
|
const contents2 = remote.getCurrentWebContents()
|
2016-03-28 23:31:06 +00:00
|
|
|
assert(contents1 === contents2)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote class', () => {
|
|
|
|
const cl = remote.require(path.join(fixtures, 'module', 'class.js'))
|
|
|
|
const base = cl.base
|
2016-03-25 20:03:49 +00:00
|
|
|
let derived = cl.derived
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can get methods', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(base.method(), 'method')
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can get properties', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(base.readonly, 'readonly')
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can change properties', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(base.value, 'old')
|
|
|
|
base.value = 'new'
|
|
|
|
assert.equal(base.value, 'new')
|
|
|
|
base.value = 'old'
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('has unenumerable methods', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert(!base.hasOwnProperty('method'))
|
|
|
|
assert(Object.getPrototypeOf(base).hasOwnProperty('method'))
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('keeps prototype chain in derived class', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
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'))
|
|
|
|
})
|
2016-04-01 06:35:34 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('is referenced by methods in prototype chain', () => {
|
2016-04-01 06:35:34 +00:00
|
|
|
let method = derived.method
|
|
|
|
derived = null
|
2016-06-29 16:37:10 +00:00
|
|
|
global.gc()
|
2016-04-01 06:35:34 +00:00
|
|
|
assert.equal(method(), 'method')
|
2016-06-29 16:37:10 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ipc.sender.send', () => {
|
|
|
|
it('should work when sending an object containing id property', (done) => {
|
|
|
|
const obj = {
|
2016-01-12 02:40:23 +00:00
|
|
|
id: 1,
|
|
|
|
name: 'ly'
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
|
|
|
ipcRenderer.once('message', function (event, message) {
|
|
|
|
assert.deepEqual(message, obj)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('message', obj)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send instances of Date', (done) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
const currentDate = new Date()
|
|
|
|
ipcRenderer.once('message', function (event, value) {
|
|
|
|
assert.equal(value, currentDate.toISOString())
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('message', currentDate)
|
|
|
|
})
|
2016-08-08 16:35:30 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send instances of Buffer', (done) => {
|
2016-08-24 23:59:54 +00:00
|
|
|
const buffer = Buffer.from('hello')
|
2016-08-24 21:49:25 +00:00
|
|
|
ipcRenderer.once('message', function (event, message) {
|
|
|
|
assert.ok(buffer.equals(message))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('message', buffer)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send objects with DOM class prototypes', (done) => {
|
2016-08-08 16:35:30 +00:00
|
|
|
ipcRenderer.once('message', function (event, value) {
|
|
|
|
assert.equal(value.protocol, 'file:')
|
|
|
|
assert.equal(value.hostname, '')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('message', document.location)
|
|
|
|
})
|
2016-08-25 16:10:37 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send Electron API objects', (done) => {
|
2016-08-29 17:21:30 +00:00
|
|
|
const webContents = remote.getCurrentWebContents()
|
|
|
|
ipcRenderer.once('message', function (event, value) {
|
|
|
|
assert.deepEqual(value.browserWindowOptions, webContents.browserWindowOptions)
|
|
|
|
done()
|
2016-08-29 17:35:07 +00:00
|
|
|
})
|
2016-08-29 17:21:30 +00:00
|
|
|
ipcRenderer.send('message', webContents)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not crash on external objects (regression)', (done) => {
|
2016-08-26 16:36:25 +00:00
|
|
|
const request = http.request({port: 5000, hostname: '127.0.0.1', method: 'GET', path: '/'})
|
2016-08-30 16:27:12 +00:00
|
|
|
const stream = request.agent.sockets['127.0.0.1:5000:'][0]._handle._externalStream
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('error', () => {})
|
2016-08-30 16:27:12 +00:00
|
|
|
ipcRenderer.once('message', function (event, requestValue, externalStreamValue) {
|
|
|
|
assert.equal(requestValue.method, 'GET')
|
|
|
|
assert.equal(requestValue.path, '/')
|
|
|
|
assert.equal(externalStreamValue, null)
|
2016-08-26 16:36:25 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-08-30 16:27:12 +00:00
|
|
|
|
|
|
|
ipcRenderer.send('message', request, stream)
|
2016-08-26 16:36:25 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can send objects that both reference the same object', (done) => {
|
2016-08-25 16:10:37 +00:00
|
|
|
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)
|
|
|
|
})
|
2016-08-29 17:25:05 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('inserts null for cyclic references', (done) => {
|
2016-08-29 17:25:05 +00:00
|
|
|
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)
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ipc.sendSync', () => {
|
|
|
|
afterEach(() => {
|
2016-05-09 11:08:50 +00:00
|
|
|
ipcMain.removeAllListeners('send-sync-message')
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can be replied by setting event.returnValue', () => {
|
|
|
|
const msg = ipcRenderer.sendSync('echo', 'test')
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(msg, 'test')
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not crash when reply is not sent and browser is destroyed', (done) => {
|
2016-08-03 19:47:53 +00:00
|
|
|
w = new BrowserWindow({
|
2016-01-12 02:40:23 +00:00
|
|
|
show: false
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
ipcMain.once('send-sync-message', function (event) {
|
|
|
|
event.returnValue = null
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'))
|
|
|
|
})
|
2016-05-05 21:22:59 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('does not crash when reply is sent by multiple listeners', (done) => {
|
2016-08-03 19:47:53 +00:00
|
|
|
w = new BrowserWindow({
|
2016-05-05 21:22:59 +00:00
|
|
|
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'))
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ipcRenderer.sendTo', () => {
|
2016-05-18 13:14:51 +00:00
|
|
|
let contents = null
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach(() => {
|
2016-05-18 13:14:51 +00:00
|
|
|
contents = webContents.create({})
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => {
|
2016-05-18 13:14:51 +00:00
|
|
|
ipcRenderer.removeAllListeners('pong')
|
|
|
|
contents.destroy()
|
|
|
|
contents = null
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('sends message to WebContents', (done) => {
|
2016-05-18 13:14:51 +00:00
|
|
|
const webContentsId = remote.getCurrentWebContents().id
|
|
|
|
ipcRenderer.once('pong', function (event, id) {
|
|
|
|
assert.equal(webContentsId, id)
|
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
contents.once('did-finish-load', () => {
|
2016-05-18 13:14:51 +00:00
|
|
|
ipcRenderer.sendTo(contents.id, 'ping', webContentsId)
|
|
|
|
})
|
|
|
|
contents.loadURL('file://' + path.join(fixtures, 'pages', 'ping-pong.html'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote listeners', () => {
|
|
|
|
it('can be added and removed correctly', () => {
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
const listener = () => {}
|
2016-03-25 20:03:49 +00:00
|
|
|
w.on('test', listener)
|
|
|
|
assert.equal(w.listenerCount('test'), 1)
|
|
|
|
w.removeListener('test', listener)
|
|
|
|
assert.equal(w.listenerCount('test'), 0)
|
|
|
|
})
|
2017-01-12 23:02:24 +00:00
|
|
|
|
2017-01-18 20:44:26 +00:00
|
|
|
it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => {
|
2017-01-12 23:02:24 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false
|
|
|
|
})
|
2017-01-13 00:31:02 +00:00
|
|
|
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')
|
2017-01-24 23:05:34 +00:00
|
|
|
const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
|
|
|
|
assert.deepEqual(results, {
|
|
|
|
warningMessage: expectedMessage,
|
|
|
|
listenerCountBefore: 2,
|
|
|
|
listenerCountAfter: 1
|
|
|
|
})
|
2017-01-13 00:31:02 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.reload()
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'remote-event-handler.html'))
|
2017-01-12 23:02:24 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-12-01 22:41:34 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2016-12-01 01:29:50 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote objects registry', () => {
|
|
|
|
it('does not dereference until the render view is deleted (regression)', (done) => {
|
2016-12-01 01:29:50 +00:00
|
|
|
w = new BrowserWindow({
|
2016-12-06 21:25:46 +00:00
|
|
|
show: false
|
2016-12-01 01:29:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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'))
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|