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')
|
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')
|
2018-09-14 06:56:55 +00:00
|
|
|
const { resolveGetters } = require('./assert-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
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(path1, path2)
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:01:03 +00:00
|
|
|
describe('remote module', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
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
|
|
|
|
2019-01-08 22:27:56 +00:00
|
|
|
describe('remote.getGlobal filtering', () => {
|
2018-10-18 03:36:45 +00:00
|
|
|
it('can return custom values', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-global', { test: 'Hello World!' })
|
|
|
|
expect(remote.getGlobal('test')).to.be.equal('Hello World!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when no returnValue set', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-global')
|
2019-01-08 22:27:56 +00:00
|
|
|
expect(() => remote.getGlobal('test')).to.throw(`Blocked remote.getGlobal('test')`)
|
2018-10-18 03:36:45 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-01-08 22:27:56 +00:00
|
|
|
describe('remote.getBuiltin filtering', () => {
|
|
|
|
it('can return custom values', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-builtin', { test: 'Hello World!' })
|
|
|
|
expect(remote.getBuiltin('test')).to.be.equal('Hello World!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when no returnValue set', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-builtin')
|
|
|
|
expect(() => remote.getBuiltin('test')).to.throw(`Blocked remote.getBuiltin('test')`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote.require filtering', () => {
|
2018-10-18 03:36:45 +00:00
|
|
|
it('can return custom values', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-require', { test: 'Hello World!' })
|
|
|
|
expect(remote.require('test')).to.be.equal('Hello World!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when no returnValue set', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-require')
|
2019-01-08 22:27:56 +00:00
|
|
|
expect(() => remote.require('test')).to.throw(`Blocked remote.require('test')`)
|
2018-10-18 03:36:45 +00:00
|
|
|
})
|
2019-01-08 22:27:56 +00:00
|
|
|
})
|
2018-10-18 03:36:45 +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')
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(dialog1, 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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(a.id, 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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(a.foo.constructor.name, '')
|
|
|
|
assert.strictEqual(a.foo.bar, 'baz')
|
|
|
|
assert.strictEqual(a.foo.baz, false)
|
|
|
|
assert.strictEqual(a.bar, 1234)
|
|
|
|
assert.strictEqual(a.anonymous.constructor.name, '')
|
|
|
|
assert.strictEqual(a.getConstructorName(Object.create(null)), '')
|
|
|
|
assert.strictEqual(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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(typeof a, 'function')
|
|
|
|
assert.strictEqual(a.bar, 'baz')
|
2016-08-16 16:24:38 +00:00
|
|
|
|
|
|
|
a = remote.require(path.join(fixtures, 'module', 'function-with-properties.js'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(typeof a, 'object')
|
|
|
|
assert.strictEqual(a.foo(), 'hello')
|
|
|
|
assert.strictEqual(a.foo.bar, 'baz')
|
|
|
|
assert.strictEqual(a.foo.nested.prop, 'yes')
|
|
|
|
assert.strictEqual(a.foo.method1(), 'world')
|
|
|
|
assert.strictEqual(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()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(a.bar(), true)
|
|
|
|
assert.strictEqual(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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(typeof a.Foo, 'function')
|
|
|
|
assert.strictEqual(a.Foo.foo(), 3)
|
|
|
|
assert.strictEqual(a.Foo.bar, 'baz')
|
2016-08-16 17:32:32 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const foo = new a.Foo()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(foo.baz(), 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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(a((a, b, c, d, f) => {}), 5)
|
|
|
|
assert.strictEqual(a((a) => {}), 1)
|
|
|
|
assert.strictEqual(a((...args) => {}), 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)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(a.returnArgs(arrayA, arrayB), [
|
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
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(a.returnArgs(objectA, objectB), [
|
|
|
|
{ foo: 'bar', objectB: { baz: null } },
|
|
|
|
{ baz: { foo: 'bar', objectB: null } }
|
2016-07-11 17:27:43 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
arrayA = [1, 2, 3]
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(a.returnArgs({ foo: arrayA }, { bar: arrayA }), [
|
|
|
|
{ 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' }
|
|
|
|
assert.deepStrictEqual(a.returnArgs({ foo: objectA }, { bar: objectA }), [
|
|
|
|
{ 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)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(a.returnArgs(arrayA), [
|
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'
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(a.returnArgs(objectA), [
|
|
|
|
{ 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'
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(a.returnArgs(objectA), [
|
|
|
|
{ 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))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result.constructor.name, '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', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(typeof remote.app.getPath, 'function')
|
|
|
|
assert.strictEqual(typeof remote.webContents.getFocusedWebContents, 'function')
|
|
|
|
assert.strictEqual(typeof remote.clipboard.readText, 'function')
|
|
|
|
assert.strictEqual(typeof remote.shell.openExternal, '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
|
2017-03-17 15:21:37 +00:00
|
|
|
assert(readText.toString().startsWith('function'))
|
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'))
|
|
|
|
assert.strictEqual(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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property.property, 1127)
|
2017-04-03 21:26:39 +00:00
|
|
|
|
|
|
|
property.property = null
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property.property, null)
|
2017-04-03 21:26:39 +00:00
|
|
|
property.property = undefined
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property.property, undefined)
|
2016-03-25 20:03:49 +00:00
|
|
|
property.property = 1007
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property.property, 1007)
|
2017-04-03 21:26:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property.getFunctionProperty(), 'foo-browser')
|
2016-08-17 21:21:50 +00:00
|
|
|
property.func.property = 'bar'
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property.getFunctionProperty(), 'bar-browser')
|
|
|
|
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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(property2.property, 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'))
|
|
|
|
|
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()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(obj.test, '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'))
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(remoteFunctions.aFunction(), 1127)
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
remoteFunctions.aFunction = () => { return 1234 }
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(remoteFunctions.aFunction(), 1234)
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(delete remoteFunctions.aFunction, 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
|
|
|
|
2018-06-13 07:38:31 +00:00
|
|
|
it('converts NaN to undefined', () => {
|
|
|
|
assert.strictEqual(printName.getNaN(), undefined)
|
|
|
|
assert.strictEqual(printName.echo(NaN), undefined)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('converts Infinity to undefined', () => {
|
|
|
|
assert.strictEqual(printName.getInfinity(), undefined)
|
|
|
|
assert.strictEqual(printName.echo(Infinity), undefined)
|
|
|
|
})
|
|
|
|
|
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')
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(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()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(printName.print(now), 'Date')
|
|
|
|
assert.deepStrictEqual(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)))
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const objectWithBuffer = { a: 'foo', b: Buffer.from('bar') }
|
2016-08-24 22:01:52 +00:00
|
|
|
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]))
|
|
|
|
})
|
|
|
|
|
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)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.echo(buffer), buffer)
|
|
|
|
assert.strictEqual(printName.print(buffer), 'ArrayBuffer')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Int8Array', () => {
|
|
|
|
const values = [1, 2, 3, 4]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Int8Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const int8values = new Int8Array(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Int8Array', int8values), int8values)
|
|
|
|
assert.strictEqual(printName.print(int8values), '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]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Uint8Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint8values = new Uint8Array(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Uint8Array', uint8values), uint8values)
|
|
|
|
assert.strictEqual(printName.print(uint8values), 'Uint8Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint8ClampedArray', () => {
|
|
|
|
const values = [1, 2, 3, 4]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Uint8ClampedArray', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint8values = new Uint8ClampedArray(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Uint8ClampedArray', uint8values), uint8values)
|
|
|
|
assert.strictEqual(printName.print(uint8values), 'Uint8ClampedArray')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Int16Array', () => {
|
|
|
|
const values = [0x1234, 0x2345, 0x3456, 0x4567]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Int16Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const int16values = new Int16Array(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Int16Array', int16values), int16values)
|
|
|
|
assert.strictEqual(printName.print(int16values), 'Int16Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint16Array', () => {
|
|
|
|
const values = [0x1234, 0x2345, 0x3456, 0x4567]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Uint16Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint16values = new Uint16Array(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Uint16Array', uint16values), uint16values)
|
|
|
|
assert.strictEqual(printName.print(uint16values), 'Uint16Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Int32Array', () => {
|
|
|
|
const values = [0x12345678, 0x23456789]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Int32Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const int32values = new Int32Array(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Int32Array', int32values), int32values)
|
|
|
|
assert.strictEqual(printName.print(int32values), 'Int32Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Uint32Array', () => {
|
|
|
|
const values = [0x12345678, 0x23456789]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Uint32Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const uint32values = new Uint32Array(values)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Uint32Array', uint32values), uint32values)
|
|
|
|
assert.strictEqual(printName.print(uint32values), 'Uint32Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Float32Array', () => {
|
|
|
|
const values = [0.5, 1.0, 1.5]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Float32Array', values)], values)
|
2018-05-24 12:05:46 +00:00
|
|
|
|
|
|
|
const float32values = new Float32Array()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Float32Array', float32values), float32values)
|
|
|
|
assert.strictEqual(printName.print(float32values), 'Float32Array')
|
2018-05-24 12:05:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Float64Array', () => {
|
|
|
|
const values = [0.5, 1.0, 1.5]
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual([...printName.typedArray('Float64Array', values)], 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])
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.deepStrictEqual(printName.typedArray('Float64Array', float64values), float64values)
|
|
|
|
assert.strictEqual(printName.print(float64values), '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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(value, 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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error.message, '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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error.message, '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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(error.message, '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()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(event.reason.message, '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()
|
2016-03-28 23:31:06 +00:00
|
|
|
assert(contents1 === contents2)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-01-08 22:27:56 +00:00
|
|
|
describe('remote.getCurrentWindow filtering', () => {
|
|
|
|
it('can return custom value', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-current-window', 'Hello World!')
|
|
|
|
expect(remote.getCurrentWindow()).to.be.equal('Hello World!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when no returnValue set', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-current-window')
|
|
|
|
expect(() => remote.getCurrentWindow()).to.throw('Blocked remote.getCurrentWindow()')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote.getCurrentWebContents filtering', () => {
|
|
|
|
it('can return custom value', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-current-web-contents', 'Hello World!')
|
|
|
|
expect(remote.getCurrentWebContents()).to.be.equal('Hello World!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when no returnValue set', () => {
|
|
|
|
ipcRenderer.send('handle-next-remote-get-current-web-contents')
|
|
|
|
expect(() => remote.getCurrentWebContents()).to.throw('Blocked remote.getCurrentWebContents()')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(base.method(), 'method')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can get properties', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(base.readonly, 'readonly')
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can change properties', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(base.value, 'old')
|
2016-03-25 20:03:49 +00:00
|
|
|
base.value = 'new'
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(base.value, 'new')
|
2016-03-25 20:03:49 +00:00
|
|
|
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', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(derived.method(), 'method')
|
|
|
|
assert.strictEqual(derived.readonly, 'readonly')
|
2016-03-25 20:03:49 +00:00
|
|
|
assert(!derived.hasOwnProperty('method'))
|
2018-10-02 01:56:31 +00:00
|
|
|
const proto = Object.getPrototypeOf(derived)
|
2016-03-25 20:03:49 +00:00
|
|
|
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', () => {
|
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()
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(method(), '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', () => {
|
|
|
|
assert.throws(() => {
|
|
|
|
throwFunction()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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) {
|
|
|
|
assert.ok(error.from)
|
2018-09-14 06:56:55 +00:00
|
|
|
assert.deepStrictEqual(error.cause, ...resolveGetters(err))
|
2018-04-24 12:40:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2018-10-31 15:26:57 +00:00
|
|
|
|
|
|
|
describe('remote function in renderer', () => {
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|