electron/spec/api-remote-spec.js

490 lines
17 KiB
JavaScript
Raw Normal View History

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')
const path = require('path')
const {closeWindow} = require('./window-helpers')
2016-01-12 02:40:23 +00:00
2017-12-01 20:57:41 +00:00
const {remote} = require('electron')
2016-01-12 02:40:23 +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-12-01 21:01:03 +00:00
describe('remote module', () => {
const fixtures = path.join(__dirname, 'fixtures')
2016-03-25 20:03:49 +00:00
let w = null
afterEach(() => closeWindow(w).then(() => { w = null }))
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)
})
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)
})
it('should work when object has no prototype', () => {
const a = remote.require(path.join(fixtures, 'module', 'no-prototype.js'))
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)
assert.equal(a.bar, 1234)
assert.equal(a.anonymous.constructor.name, '')
assert.equal(a.getConstructorName(Object.create(null)), '')
2016-10-13 01:47:43 +00:00
assert.equal(a.getConstructorName(new (class {})()), '')
})
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'))
})
it('should work with function properties', () => {
let 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')
2016-08-16 17:37:51 +00:00
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', () => {
const 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')
const foo = new a.Foo()
assert.equal(foo.baz(), 123)
})
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)
})
it('handles circular references in arrays and objects', () => {
const a = remote.require(path.join(fixtures, 'module', 'circular.js'))
let arrayA = ['foo']
const arrayB = [arrayA, 'bar']
arrayA.push(arrayB)
assert.deepEqual(a.returnArgs(arrayA, arrayB), [
['foo', [null, 'bar']],
[['foo', null], 'bar']
])
let objectA = {foo: 'bar'}
const 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]}
])
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'}}
])
arrayA = []
arrayA.push(arrayA)
assert.deepEqual(a.returnArgs(arrayA), [
[null]
])
2016-07-11 17:30:18 +00:00
objectA = {}
objectA.foo = objectA
2016-07-11 17:49:23 +00:00
objectA.bar = 'baz'
assert.deepEqual(a.returnArgs(objectA), [
2016-07-11 17:49:23 +00:00
{foo: null, bar: 'baz'}
])
objectA = {}
objectA.foo = {bar: objectA}
2016-07-11 17:49:23 +00:00
objectA.bar = 'baz'
assert.deepEqual(a.returnArgs(objectA), [
2016-07-11 17:49:23 +00:00
{foo: {bar: null}, bar: 'baz'}
])
})
2016-03-25 20:03:49 +00:00
})
describe('remote.createFunctionWithReturnValue', () => {
it('should be called in browser synchronously', () => {
const buf = Buffer.from('test')
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')
})
})
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')
})
it('returns toString() of original function via toString()', () => {
const {readText} = remote.clipboard
assert(readText.toString().startsWith('function'))
const {functionWithToStringProperty} = remote.require(path.join(fixtures, 'module', 'to-string-non-function.js'))
assert.equal(functionWithToStringProperty.toString, 'hello')
})
2017-02-24 18:43:39 +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)
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)
assert.equal(property.getFunctionProperty(), 'foo-browser')
property.func.property = 'bar'
assert.equal(property.getFunctionProperty(), 'bar-browser')
property.func.property = 'foo' // revert back
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
})
it('rethrows errors getting/setting properties', () => {
const foo = remote.require(path.join(fixtures, 'module', 'error-properties.js'))
assert.throws(() => {
2017-11-23 22:22:43 +00:00
// eslint-disable-next-line
foo.bar
}, /getting error/)
assert.throws(() => {
foo.bar = 'test'
}, /setting error/)
})
it('can set a remote property with a remote object', () => {
const foo = remote.require(path.join(fixtures, 'module', 'remote-object-set.js'))
assert.doesNotThrow(() => {
foo.bar = remote.getCurrentWindow()
})
})
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')
})
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)
remoteFunctions.aFunction = () => { return 1234 }
2016-03-25 20:03:49 +00:00
assert.equal(remoteFunctions.aFunction(), 1234)
assert.equal(delete remoteFunctions.aFunction, true)
})
it('is referenced by its members', () => {
let stringify = remote.getGlobal('JSON').stringify
2016-06-29 16:37:10 +00:00
global.gc()
stringify({})
2016-06-29 16:37:10 +00:00
})
2016-03-25 20:03:49 +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
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)
})
it('keeps its constructor name for objects', () => {
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
})
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
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]))
})
it('supports instanceof ArrayBuffer', () => {
const buffer = new ArrayBuffer(8)
const view = new DataView(buffer)
view.setFloat64(0, Math.PI)
assert.deepEqual(printName.echo(buffer), buffer)
assert.equal(printName.print(buffer), 'ArrayBuffer')
})
it('supports instanceof Int8Array', () => {
const values = [1, 2, 3, 4]
assert.deepEqual(printName.typedArray('Int8Array', values), values)
const int8values = new Int8Array(values)
assert.deepEqual(printName.typedArray('Int8Array', int8values), int8values)
assert.equal(printName.print(int8values), 'Int8Array')
})
it('supports instanceof Uint8Array', () => {
2016-08-24 23:59:54 +00:00
const values = [1, 2, 3, 4]
assert.deepEqual(printName.typedArray('Uint8Array', values), values)
const uint8values = new Uint8Array(values)
assert.deepEqual(printName.typedArray('Uint8Array', uint8values), uint8values)
assert.equal(printName.print(uint8values), 'Uint8Array')
})
it('supports instanceof Uint8ClampedArray', () => {
const values = [1, 2, 3, 4]
assert.deepEqual(printName.typedArray('Uint8ClampedArray', values), values)
const uint8values = new Uint8ClampedArray(values)
assert.deepEqual(printName.typedArray('Uint8ClampedArray', uint8values), uint8values)
assert.equal(printName.print(uint8values), 'Uint8ClampedArray')
})
it('supports instanceof Int16Array', () => {
const values = [0x1234, 0x2345, 0x3456, 0x4567]
assert.deepEqual(printName.typedArray('Int16Array', values), values)
const int16values = new Int16Array(values)
assert.deepEqual(printName.typedArray('Int16Array', int16values), int16values)
assert.equal(printName.print(int16values), 'Int16Array')
})
it('supports instanceof Uint16Array', () => {
const values = [0x1234, 0x2345, 0x3456, 0x4567]
assert.deepEqual(printName.typedArray('Uint16Array', values), values)
const uint16values = new Uint16Array(values)
assert.deepEqual(printName.typedArray('Uint16Array', uint16values), uint16values)
assert.equal(printName.print(uint16values), 'Uint16Array')
})
it('supports instanceof Int32Array', () => {
const values = [0x12345678, 0x23456789]
assert.deepEqual(printName.typedArray('Int32Array', values), values)
const int32values = new Int32Array(values)
assert.deepEqual(printName.typedArray('Int32Array', int32values), int32values)
assert.equal(printName.print(int32values), 'Int32Array')
})
it('supports instanceof Uint32Array', () => {
const values = [0x12345678, 0x23456789]
assert.deepEqual(printName.typedArray('Uint32Array', values), values)
const uint32values = new Uint32Array(values)
assert.deepEqual(printName.typedArray('Uint32Array', uint32values), uint32values)
assert.equal(printName.print(uint32values), 'Uint32Array')
})
it('supports instanceof Float32Array', () => {
const values = [0.5, 1.0, 1.5]
assert.deepEqual(printName.typedArray('Float32Array', values), values)
const float32values = new Float32Array()
assert.deepEqual(printName.typedArray('Float32Array', float32values), float32values)
assert.equal(printName.print(float32values), 'Float32Array')
})
it('supports instanceof Float64Array', () => {
const values = [0.5, 1.0, 1.5]
assert.deepEqual(printName.typedArray('Float64Array', values), values)
const float64values = new Float64Array([0.5, 1.0, 1.5])
assert.deepEqual(printName.typedArray('Float64Array', float64values), float64values)
assert.equal(printName.print(float64values), 'Float64Array')
2016-07-25 07:30:40 +00:00
})
2016-03-25 20:03:49 +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()
})
})
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) => {
assert.equal(error.message, 'rejected')
done()
})
})
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) => {
assert.equal(error.message, 'rejected')
done()
})
})
it('does not emit unhandled rejection events in the main process', (done) => {
remote.process.once('unhandledRejection', function (reason) {
done(reason)
})
const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
promise.reject().then(() => {
done(new Error('Promise was not rejected'))
}).catch((error) => {
assert.equal(error.message, 'rejected')
done()
})
})
// FIXME(alexeykuzmin): [Ch67] Enable the test back.
// It looks like the "unhandledrejection" handler is no longer called.
xit('emits unhandled rejection events in the renderer process', (done) => {
window.addEventListener('unhandledrejection', function (event) {
event.preventDefault()
assert.equal(event.reason.message, 'rejected')
done()
})
const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
promise.reject().then(() => {
done(new Error('Promise was not rejected'))
})
})
2016-03-25 20:03:49 +00:00
})
describe('remote webContents', () => {
it('can return same object with different getters', () => {
const contents1 = remote.getCurrentWindow().webContents
const contents2 = remote.getCurrentWebContents()
assert(contents1 === contents2)
2016-03-25 20:03:49 +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
it('can get methods', () => {
2016-03-25 20:03:49 +00:00
assert.equal(base.method(), 'method')
})
it('can get properties', () => {
2016-03-25 20:03:49 +00:00
assert.equal(base.readonly, 'readonly')
})
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'
})
it('has unenumerable methods', () => {
2016-03-25 20:03:49 +00:00
assert(!base.hasOwnProperty('method'))
assert(Object.getPrototypeOf(base).hasOwnProperty('method'))
})
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'))
})
it('is referenced by methods in prototype chain', () => {
let method = derived.method
derived = null
2016-06-29 16:37:10 +00:00
global.gc()
assert.equal(method(), 'method')
2016-06-29 16:37:10 +00:00
})
2016-03-25 20:03:49 +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', () => {
let err = new Error('error')
err.cause = new Error('cause')
err.prop = 'error prop'
try {
throwFunction(err)
} catch (error) {
assert.ok(error.from)
assert.deepEqual(error.cause, err)
}
})
})
2016-03-25 20:03:49 +00:00
})