2016-03-25 20:03:49 +00:00
|
|
|
'use strict'
|
2016-02-22 04:13:26 +00:00
|
|
|
|
2018-10-18 03:36:45 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
2019-05-20 17:04:18 +00:00
|
|
|
const { resolveGetters } = require('./expect-helpers')
|
2019-09-18 16:52:06 +00:00
|
|
|
const { ifdescribe } = require('./spec-helpers')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-10-18 03:36:45 +00:00
|
|
|
const { remote, ipcRenderer } = require('electron')
|
2018-10-31 15:26:57 +00:00
|
|
|
const { ipcMain, BrowserWindow } = remote
|
2018-10-18 03:36:45 +00:00
|
|
|
const { expect } = chai
|
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
const features = process.electronBinding('features')
|
|
|
|
|
2018-10-18 03:36:45 +00:00
|
|
|
chai.use(dirtyChai)
|
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
|
|
|
}
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(path1).to.equal(path2)
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-01-08 22:27:56 +00:00
|
|
|
describe('remote.require', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
it('should returns same object for the same module', () => {
|
|
|
|
const dialog1 = remote.require('electron')
|
|
|
|
const dialog2 = remote.require('electron')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(dialog1).to.equal(dialog2)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.id).to.equal(1127)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.foo.constructor.name).to.equal('')
|
|
|
|
expect(a.foo.bar).to.equal('baz')
|
|
|
|
expect(a.foo.baz).to.equal(false)
|
|
|
|
expect(a.bar).to.equal(1234)
|
|
|
|
expect(a.anonymous.constructor.name).to.equal('')
|
|
|
|
expect(a.getConstructorName(Object.create(null))).to.equal('')
|
|
|
|
expect(a.getConstructorName(new (class {})())).to.equal('')
|
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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a).to.be.a('function')
|
|
|
|
expect(a.bar).to.equal('baz')
|
2016-08-16 16:24:38 +00:00
|
|
|
|
|
|
|
a = remote.require(path.join(fixtures, 'module', 'function-with-properties.js'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a).to.be.an('object')
|
|
|
|
expect(a.foo()).to.equal('hello')
|
|
|
|
expect(a.foo.bar).to.equal('baz')
|
|
|
|
expect(a.foo.nested.prop).to.equal('yes')
|
|
|
|
expect(a.foo.method1()).to.equal('world')
|
|
|
|
expect(a.foo.method1.prop1()).to.equal(123)
|
2016-08-17 20:58:48 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.foo).to.have.a.property('bar')
|
|
|
|
expect(a.foo).to.have.a.property('nested')
|
|
|
|
expect(a.foo).to.have.a.property('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()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.bar()).to.equal(true)
|
|
|
|
expect(a.bar.baz).to.be.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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.Foo).to.be.a('function')
|
|
|
|
expect(a.Foo.foo()).to.equal(3)
|
|
|
|
expect(a.Foo.bar).to.equal('baz')
|
2016-08-16 17:32:32 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const foo = new a.Foo()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(foo.baz()).to.equal(123)
|
2016-08-16 17:32:32 +00:00
|
|
|
})
|
|
|
|
|
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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a((a, b, c, d, f) => {})).to.equal(5)
|
|
|
|
expect(a((a) => {})).to.equal(1)
|
|
|
|
expect(a((...args) => {})).to.equal(0)
|
2017-02-07 23:24:49 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs(arrayA, arrayB)).to.deep.equal([
|
2016-07-11 17:27:43 +00:00
|
|
|
['foo', [null, 'bar']],
|
|
|
|
[['foo', null], 'bar']
|
|
|
|
])
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
let objectA = { foo: 'bar' }
|
|
|
|
const objectB = { baz: objectA }
|
2016-07-11 17:27:43 +00:00
|
|
|
objectA.objectB = objectB
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs(objectA, objectB)).to.deep.equal([
|
2018-09-13 16:10:51 +00:00
|
|
|
{ foo: 'bar', objectB: { baz: null } },
|
|
|
|
{ baz: { foo: 'bar', objectB: null } }
|
2016-07-11 17:27:43 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
arrayA = [1, 2, 3]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs({ foo: arrayA }, { bar: arrayA })).to.deep.equal([
|
2018-09-13 16:10:51 +00:00
|
|
|
{ foo: [1, 2, 3] },
|
|
|
|
{ bar: [1, 2, 3] }
|
2016-07-11 17:27:43 +00:00
|
|
|
])
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
objectA = { foo: 'bar' }
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs({ foo: objectA }, { bar: objectA })).to.deep.equal([
|
2018-09-13 16:10:51 +00:00
|
|
|
{ foo: { foo: 'bar' } },
|
|
|
|
{ bar: { foo: 'bar' } }
|
2016-07-11 17:49:23 +00:00
|
|
|
])
|
|
|
|
|
2016-07-11 17:27:43 +00:00
|
|
|
arrayA = []
|
|
|
|
arrayA.push(arrayA)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs(arrayA)).to.deep.equal([
|
2016-07-11 17:27:43 +00:00
|
|
|
[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'
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs(objectA)).to.deep.equal([
|
2018-09-13 16:10:51 +00:00
|
|
|
{ foo: null, bar: 'baz' }
|
2016-07-11 16:52:34 +00:00
|
|
|
])
|
|
|
|
|
2016-07-11 17:27:43 +00:00
|
|
|
objectA = {}
|
2018-09-13 16:10:51 +00:00
|
|
|
objectA.foo = { bar: objectA }
|
2016-07-11 17:49:23 +00:00
|
|
|
objectA.bar = 'baz'
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(a.returnArgs(objectA)).to.deep.equal([
|
2018-09-13 16:10:51 +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))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(result).to.be.an.instanceOf(Buffer)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('remote modules', () => {
|
|
|
|
it('includes browser process modules as properties', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(remote.app.getPath).to.be.a('function')
|
|
|
|
expect(remote.webContents.getFocusedWebContents).to.be.a('function')
|
|
|
|
expect(remote.clipboard.readText).to.be.a('function')
|
2017-02-24 18:43:39 +00:00
|
|
|
})
|
2017-03-17 15:21:37 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('returns toString() of original function via toString()', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { readText } = remote.clipboard
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(readText.toString().startsWith('function')).to.be.true()
|
2017-03-17 17:28:23 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { functionWithToStringProperty } = remote.require(path.join(fixtures, 'module', 'to-string-non-function.js'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(functionWithToStringProperty.toString).to.equal('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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property).to.have.a.property('property').that.is.equal(1127)
|
2017-04-03 21:26:39 +00:00
|
|
|
|
|
|
|
property.property = null
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property).to.have.a.property('property').that.is.null()
|
2017-04-03 21:26:39 +00:00
|
|
|
property.property = undefined
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property).to.have.a.property('property').that.is.undefined()
|
2016-03-25 20:03:49 +00:00
|
|
|
property.property = 1007
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property).to.have.a.property('property').that.is.equal(1007)
|
2017-04-03 21:26:39 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property.getFunctionProperty()).to.equal('foo-browser')
|
2016-08-17 21:21:50 +00:00
|
|
|
property.func.property = 'bar'
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property.getFunctionProperty()).to.equal('bar-browser')
|
2018-09-13 16:10:51 +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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(property2.property).to.equal(1007)
|
2016-03-25 20:03:49 +00:00
|
|
|
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'))
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2017-11-23 22:22:43 +00:00
|
|
|
// eslint-disable-next-line
|
2017-04-03 16:09:37 +00:00
|
|
|
foo.bar
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw('getting error')
|
2017-04-03 16:09:37 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2017-04-03 16:09:37 +00:00
|
|
|
foo.bar = 'test'
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw('setting error')
|
2017-04-03 16:09:37 +00:00
|
|
|
})
|
|
|
|
|
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'))
|
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2017-04-03 21:17:16 +00:00
|
|
|
foo.bar = remote.getCurrentWindow()
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.not.throw()
|
2017-04-03 21:17:16 +00:00
|
|
|
})
|
|
|
|
|
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()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(obj.test).to.equal('test')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
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'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(remoteFunctions.aFunction()).to.equal(1127)
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
remoteFunctions.aFunction = () => { return 1234 }
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(remoteFunctions.aFunction()).to.equal(1234)
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(delete remoteFunctions.aFunction).to.equal(true)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-01 05:50:33 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('is referenced by its members', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const 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
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
it('preserves NaN', () => {
|
|
|
|
expect(printName.getNaN()).to.be.NaN()
|
|
|
|
expect(printName.echo(NaN)).to.be.NaN()
|
2018-06-13 07:38:31 +00:00
|
|
|
})
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
it('preserves Infinity', () => {
|
|
|
|
expect(printName.getInfinity()).to.equal(Infinity)
|
|
|
|
expect(printName.echo(Infinity)).to.equal(Infinity)
|
2018-06-13 07:38:31 +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')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.print(buf)).to.equal('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()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.print(now)).to.equal('Date')
|
|
|
|
expect(printName.echo(now)).to.deep.equal(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')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(buffer.equals(printName.echo(buffer))).to.be.true()
|
2016-08-24 22:01:52 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const objectWithBuffer = { a: 'foo', b: Buffer.from('bar') }
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(objectWithBuffer.b.equals(printName.echo(objectWithBuffer).b)).to.be.true()
|
2016-08-24 22:01:52 +00:00
|
|
|
|
2016-08-24 23:41:59 +00:00
|
|
|
const arrayWithBuffer = [1, 2, Buffer.from('baz')]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(arrayWithBuffer[2].equals(printName.echo(arrayWithBuffer)[2])).to.be.true()
|
2016-08-24 22:01:52 +00:00
|
|
|
})
|
|
|
|
|
2018-05-24 12:05:46 +00:00
|
|
|
it('supports instanceof ArrayBuffer', () => {
|
|
|
|
const buffer = new ArrayBuffer(8)
|
|
|
|
const view = new DataView(buffer)
|
|
|
|
|
|
|
|
view.setFloat64(0, Math.PI)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.echo(buffer)).to.deep.equal(buffer)
|
|
|
|
expect(printName.print(buffer)).to.equal('ArrayBuffer')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Int8Array', () => {
|
|
|
|
const values = [1, 2, 3, 4]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Int8Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const int8values = new Int8Array(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Int8Array', int8values)).to.deep.equal(int8values)
|
|
|
|
expect(printName.print(int8values)).to.equal('Int8Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint8Array', () => {
|
2016-08-24 23:59:54 +00:00
|
|
|
const values = [1, 2, 3, 4]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Uint8Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint8values = new Uint8Array(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Uint8Array', uint8values)).to.deep.equal(uint8values)
|
|
|
|
expect(printName.print(uint8values)).to.equal('Uint8Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint8ClampedArray', () => {
|
|
|
|
const values = [1, 2, 3, 4]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Uint8ClampedArray', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint8values = new Uint8ClampedArray(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Uint8ClampedArray', uint8values)).to.deep.equal(uint8values)
|
|
|
|
expect(printName.print(uint8values)).to.equal('Uint8ClampedArray')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Int16Array', () => {
|
|
|
|
const values = [0x1234, 0x2345, 0x3456, 0x4567]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Int16Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const int16values = new Int16Array(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Int16Array', int16values)).to.deep.equal(int16values)
|
|
|
|
expect(printName.print(int16values)).to.equal('Int16Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint16Array', () => {
|
|
|
|
const values = [0x1234, 0x2345, 0x3456, 0x4567]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Uint16Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint16values = new Uint16Array(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Uint16Array', uint16values)).to.deep.equal(uint16values)
|
|
|
|
expect(printName.print(uint16values)).to.equal('Uint16Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Int32Array', () => {
|
|
|
|
const values = [0x12345678, 0x23456789]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Int32Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const int32values = new Int32Array(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Int32Array', int32values)).to.deep.equal(int32values)
|
|
|
|
expect(printName.print(int32values)).to.equal('Int32Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint32Array', () => {
|
|
|
|
const values = [0x12345678, 0x23456789]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Uint32Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint32values = new Uint32Array(values)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Uint32Array', uint32values)).to.deep.equal(uint32values)
|
|
|
|
expect(printName.print(uint32values)).to.equal('Uint32Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Float32Array', () => {
|
|
|
|
const values = [0.5, 1.0, 1.5]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Float32Array', values)]).to.deep.equal(values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const float32values = new Float32Array()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Float32Array', float32values)).to.deep.equal(float32values)
|
|
|
|
expect(printName.print(float32values)).to.equal('Float32Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Float64Array', () => {
|
|
|
|
const values = [0.5, 1.0, 1.5]
|
2019-05-20 17:04:18 +00:00
|
|
|
expect([...printName.typedArray('Float64Array', values)]).to.deep.equal(values)
|
2016-08-24 23:46:56 +00:00
|
|
|
|
2018-05-24 12:05:46 +00:00
|
|
|
const float64values = new Float64Array([0.5, 1.0, 1.5])
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(printName.typedArray('Float64Array', float64values)).to.deep.equal(float64values)
|
|
|
|
expect(printName.print(float64values)).to.equal('Float64Array')
|
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) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(value).to.equal(2468)
|
2016-03-25 20:03:49 +00:00
|
|
|
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) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error.message).to.equal('rejected')
|
2016-05-23 22:06:46 +00:00
|
|
|
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) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error.message).to.equal('rejected')
|
2016-05-23 22:06:46 +00:00
|
|
|
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) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error.message).to.equal('rejected')
|
2016-06-21 00:54:15 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-09-13 06:32:49 +00:00
|
|
|
it('emits unhandled rejection events in the renderer process', (done) => {
|
2018-12-06 20:16:19 +00:00
|
|
|
window.addEventListener('unhandledrejection', function handler (event) {
|
2016-06-21 00:54:15 +00:00
|
|
|
event.preventDefault()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(event.reason.message).to.equal('rejected')
|
2018-12-06 20:16:19 +00:00
|
|
|
window.removeEventListener('unhandledrejection', handler)
|
2016-06-21 00:54:15 +00:00
|
|
|
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()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(contents1).to.equal(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', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(base.method()).to.equal('method')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can get properties', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(base.readonly).to.equal('readonly')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can change properties', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(base.value).to.equal('old')
|
2016-03-25 20:03:49 +00:00
|
|
|
base.value = 'new'
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(base.value).to.equal('new')
|
2016-03-25 20:03:49 +00:00
|
|
|
base.value = 'old'
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('has unenumerable methods', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(base).to.not.have.own.property('method')
|
|
|
|
expect(Object.getPrototypeOf(base)).to.have.own.property('method')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('keeps prototype chain in derived class', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(derived.method()).to.equal('method')
|
|
|
|
expect(derived.readonly).to.equal('readonly')
|
|
|
|
expect(derived).to.not.have.own.property('method')
|
2018-10-02 01:56:31 +00:00
|
|
|
const proto = Object.getPrototypeOf(derived)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(proto).to.not.have.own.property('method')
|
|
|
|
expect(Object.getPrototypeOf(proto)).to.have.own.property('method')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-01 06:35:34 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('is referenced by methods in prototype chain', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const method = derived.method
|
2016-04-01 06:35:34 +00:00
|
|
|
derived = null
|
2016-06-29 16:37:10 +00:00
|
|
|
global.gc()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(method()).to.equal('method')
|
2016-06-29 16:37:10 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-04-24 12:40:19 +00:00
|
|
|
|
|
|
|
describe('remote exception', () => {
|
|
|
|
const throwFunction = remote.require(path.join(fixtures, 'module', 'exception.js'))
|
|
|
|
|
|
|
|
it('throws errors from the main process', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2018-04-24 12:40:19 +00:00
|
|
|
throwFunction()
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw()
|
2018-04-24 12:40:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throws custom errors from the main process', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const err = new Error('error')
|
2018-04-24 12:40:19 +00:00
|
|
|
err.cause = new Error('cause')
|
|
|
|
err.prop = 'error prop'
|
|
|
|
try {
|
|
|
|
throwFunction(err)
|
|
|
|
} catch (error) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(error.from).to.equal('browser')
|
|
|
|
expect(error.cause).to.deep.equal(...resolveGetters(err))
|
2018-04-24 12:40:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-10-31 15:26:57 +00:00
|
|
|
|
|
|
|
describe('remote function in renderer', () => {
|
2019-05-28 21:07:48 +00:00
|
|
|
let w = null
|
|
|
|
|
|
|
|
afterEach(() => closeWindow(w).then(() => { w = null }))
|
2018-10-31 15:26:57 +00:00
|
|
|
afterEach(() => {
|
|
|
|
ipcMain.removeAllListeners('done')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('works when created in preload script', (done) => {
|
|
|
|
ipcMain.once('done', () => w.close())
|
|
|
|
const preload = path.join(fixtures, 'module', 'preload-remote-function.js')
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2019-01-07 19:19:27 +00:00
|
|
|
preload
|
2018-10-31 15:26:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
w.once('closed', () => done())
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
|
|
|
})
|
2019-09-19 20:35:05 +00:00
|
|
|
|
|
|
|
describe('remote listeners', () => {
|
|
|
|
let w = null
|
|
|
|
afterEach(() => closeWindow(w).then(() => { w = null }))
|
|
|
|
|
|
|
|
it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
expect(results).to.deep.equal({
|
|
|
|
warningMessage: expectedMessage,
|
|
|
|
listenerCountBefore: 2,
|
|
|
|
listenerCountAfter: 1
|
|
|
|
})
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.webContents.reload()
|
|
|
|
})
|
|
|
|
w.loadFile(path.join(fixtures, 'api', 'remote-event-handler.html'))
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|