2016-03-25 23:03:49 +03:00
|
|
|
'use strict'
|
2016-02-22 07:13:26 +03:00
|
|
|
|
2016-03-25 23:03:49 +03:00
|
|
|
const assert = require('assert')
|
|
|
|
const path = require('path')
|
2016-08-03 22:47:53 +03:00
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-01-12 05:40:23 +03:00
|
|
|
|
2016-05-18 16:14:51 +03:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
|
|
|
const {ipcMain, webContents, BrowserWindow} = remote
|
2016-01-12 05:40:23 +03:00
|
|
|
|
2016-03-25 23:03:49 +03:00
|
|
|
const comparePaths = function (path1, path2) {
|
2016-01-12 05:40:23 +03:00
|
|
|
if (process.platform === 'win32') {
|
2016-03-25 23:03:49 +03:00
|
|
|
path1 = path1.toLowerCase()
|
|
|
|
path2 = path2.toLowerCase()
|
2016-01-12 05:40:23 +03:00
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(path1, path2)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('ipc module', function () {
|
|
|
|
var fixtures = path.join(__dirname, 'fixtures')
|
|
|
|
|
2016-08-03 22:47:53 +03:00
|
|
|
var w = null
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
return closeWindow(w).then(function () { w = null })
|
|
|
|
})
|
|
|
|
|
2016-03-25 23:03:49 +03:00
|
|
|
describe('remote.require', function () {
|
|
|
|
it('should returns same object for the same module', function () {
|
|
|
|
var dialog1 = remote.require('electron')
|
|
|
|
var dialog2 = remote.require('electron')
|
|
|
|
assert.equal(dialog1, dialog2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should work when object contains id property', function () {
|
|
|
|
var a = remote.require(path.join(fixtures, 'module', 'id.js'))
|
|
|
|
assert.equal(a.id, 1127)
|
|
|
|
})
|
|
|
|
|
2016-05-30 09:20:53 +03:00
|
|
|
it('should work when object has no prototype', function () {
|
2016-05-27 20:41:10 +03:00
|
|
|
var a = remote.require(path.join(fixtures, 'module', 'no-prototype.js'))
|
2016-07-06 23:06:48 +03:00
|
|
|
assert.equal(a.foo.constructor.name, '')
|
2016-05-27 20:52:56 +03:00
|
|
|
assert.equal(a.foo.bar, 'baz')
|
|
|
|
assert.equal(a.foo.baz, false)
|
2016-05-27 20:41:10 +03:00
|
|
|
assert.equal(a.bar, 1234)
|
2016-07-06 23:11:25 +03:00
|
|
|
assert.equal(a.anonymous.constructor.name, '')
|
2016-07-06 23:06:48 +03:00
|
|
|
assert.equal(a.getConstructorName(Object.create(null)), '')
|
2016-07-07 01:45:35 +03:00
|
|
|
assert.equal(a.getConstructorName(new (class {})), '')
|
2016-05-27 20:41:10 +03:00
|
|
|
})
|
|
|
|
|
2016-03-25 23:03:49 +03:00
|
|
|
it('should search module from the user app', function () {
|
|
|
|
comparePaths(path.normalize(remote.process.mainModule.filename), path.resolve(__dirname, 'static', 'main.js'))
|
|
|
|
comparePaths(path.normalize(remote.process.mainModule.paths[0]), path.resolve(__dirname, 'static', 'node_modules'))
|
|
|
|
})
|
2016-07-11 19:52:34 +03:00
|
|
|
|
|
|
|
it('handles circular references in arrays and objects', function () {
|
|
|
|
var a = remote.require(path.join(fixtures, 'module', 'circular.js'))
|
2016-07-11 20:27:43 +03:00
|
|
|
|
|
|
|
var arrayA = ['foo']
|
|
|
|
var arrayB = [arrayA, 'bar']
|
|
|
|
arrayA.push(arrayB)
|
|
|
|
assert.deepEqual(a.returnArgs(arrayA, arrayB), [
|
|
|
|
['foo', [null, 'bar']],
|
|
|
|
[['foo', null], 'bar']
|
|
|
|
])
|
|
|
|
|
|
|
|
var objectA = {foo: 'bar'}
|
|
|
|
var objectB = {baz: objectA}
|
|
|
|
objectA.objectB = objectB
|
|
|
|
assert.deepEqual(a.returnArgs(objectA, objectB), [
|
|
|
|
{foo: 'bar', objectB: {baz: null}},
|
|
|
|
{baz: {foo: 'bar', objectB: null}}
|
|
|
|
])
|
|
|
|
|
|
|
|
arrayA = [1, 2, 3]
|
|
|
|
assert.deepEqual(a.returnArgs({foo: arrayA}, {bar: arrayA}), [
|
|
|
|
{foo: [1, 2, 3]},
|
|
|
|
{bar: [1, 2, 3]}
|
|
|
|
])
|
|
|
|
|
2016-07-11 20:49:23 +03:00
|
|
|
objectA = {foo: 'bar'}
|
|
|
|
assert.deepEqual(a.returnArgs({foo: objectA}, {bar: objectA}), [
|
|
|
|
{foo: {foo: 'bar'}},
|
|
|
|
{bar: {foo: 'bar'}}
|
|
|
|
])
|
|
|
|
|
2016-07-11 20:27:43 +03:00
|
|
|
arrayA = []
|
|
|
|
arrayA.push(arrayA)
|
|
|
|
assert.deepEqual(a.returnArgs(arrayA), [
|
|
|
|
[null]
|
|
|
|
])
|
|
|
|
|
2016-07-11 20:30:18 +03:00
|
|
|
objectA = {}
|
2016-07-11 20:27:43 +03:00
|
|
|
objectA.foo = objectA
|
2016-07-11 20:49:23 +03:00
|
|
|
objectA.bar = 'baz'
|
2016-07-11 20:27:43 +03:00
|
|
|
assert.deepEqual(a.returnArgs(objectA), [
|
2016-07-11 20:49:23 +03:00
|
|
|
{foo: null, bar: 'baz'}
|
2016-07-11 19:52:34 +03:00
|
|
|
])
|
|
|
|
|
2016-07-11 20:27:43 +03:00
|
|
|
objectA = {}
|
|
|
|
objectA.foo = {bar: objectA}
|
2016-07-11 20:49:23 +03:00
|
|
|
objectA.bar = 'baz'
|
2016-07-11 20:27:43 +03:00
|
|
|
assert.deepEqual(a.returnArgs(objectA), [
|
2016-07-11 20:49:23 +03:00
|
|
|
{foo: {bar: null}, bar: 'baz'}
|
2016-07-11 19:52:34 +03:00
|
|
|
])
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote.createFunctionWithReturnValue', function () {
|
|
|
|
it('should be called in browser synchronously', function () {
|
|
|
|
var buf = new Buffer('test')
|
|
|
|
var call = remote.require(path.join(fixtures, 'module', 'call.js'))
|
|
|
|
var result = call.call(remote.createFunctionWithReturnValue(buf))
|
|
|
|
assert.equal(result.constructor.name, 'Buffer')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote object in renderer', function () {
|
|
|
|
it('can change its properties', function () {
|
|
|
|
var property = remote.require(path.join(fixtures, 'module', 'property.js'))
|
|
|
|
assert.equal(property.property, 1127)
|
|
|
|
property.property = 1007
|
|
|
|
assert.equal(property.property, 1007)
|
|
|
|
var property2 = remote.require(path.join(fixtures, 'module', 'property.js'))
|
|
|
|
assert.equal(property2.property, 1007)
|
|
|
|
property.property = 1127
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can construct an object from its member', function () {
|
|
|
|
var call = remote.require(path.join(fixtures, 'module', 'call.js'))
|
2016-03-31 00:06:50 +03:00
|
|
|
var obj = new call.constructor()
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(obj.test, 'test')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can reassign and delete its member functions', function () {
|
|
|
|
var remoteFunctions = remote.require(path.join(fixtures, 'module', 'function.js'))
|
|
|
|
assert.equal(remoteFunctions.aFunction(), 1127)
|
|
|
|
|
2016-03-29 02:31:06 +03:00
|
|
|
remoteFunctions.aFunction = function () { return 1234 }
|
2016-03-25 23:03:49 +03:00
|
|
|
assert.equal(remoteFunctions.aFunction(), 1234)
|
|
|
|
|
|
|
|
assert.equal(delete remoteFunctions.aFunction, true)
|
|
|
|
})
|
2016-04-01 08:50:33 +03:00
|
|
|
|
|
|
|
it('is referenced by its members', function () {
|
|
|
|
let stringify = remote.getGlobal('JSON').stringify
|
2016-06-29 19:37:10 +03:00
|
|
|
global.gc()
|
2016-04-01 08:50:33 +03:00
|
|
|
stringify({})
|
2016-06-29 19:37:10 +03:00
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote value in browser', function () {
|
2016-07-25 10:30:40 +03:00
|
|
|
const print = path.join(fixtures, 'module', 'print_name.js')
|
|
|
|
const printName = remote.require(print)
|
2016-03-25 23:03:49 +03:00
|
|
|
|
|
|
|
it('keeps its constructor name for objects', function () {
|
2016-07-25 10:30:40 +03:00
|
|
|
const buf = new Buffer('test')
|
2016-06-29 19:37:10 +03:00
|
|
|
assert.equal(printName.print(buf), 'Buffer')
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports instanceof Date', function () {
|
2016-07-25 10:30:40 +03:00
|
|
|
const now = new Date()
|
2016-06-29 19:37:10 +03:00
|
|
|
assert.equal(printName.print(now), 'Date')
|
|
|
|
assert.deepEqual(printName.echo(now), now)
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
2016-07-25 10:30:40 +03:00
|
|
|
|
|
|
|
it('supports TypedArray', function () {
|
|
|
|
const values = [1, 2, 3, 4]
|
|
|
|
const typedArray = printName.typedArray(values)
|
2016-07-25 10:41:20 +03:00
|
|
|
assert.deepEqual(values, typedArray)
|
2016-07-25 10:30:40 +03:00
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote promise', function () {
|
|
|
|
it('can be used as promise in each side', function (done) {
|
|
|
|
var promise = remote.require(path.join(fixtures, 'module', 'promise.js'))
|
|
|
|
promise.twicePromise(Promise.resolve(1234)).then(function (value) {
|
|
|
|
assert.equal(value, 2468)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-05-24 01:06:46 +03:00
|
|
|
|
|
|
|
it('handles rejections via catch(onRejected)', function (done) {
|
|
|
|
var promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
|
|
|
|
promise.reject(Promise.resolve(1234)).catch(function (error) {
|
|
|
|
assert.equal(error.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles rejections via then(onFulfilled, onRejected)', function (done) {
|
|
|
|
var promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
|
|
|
|
promise.reject(Promise.resolve(1234)).then(function () {}, function (error) {
|
|
|
|
assert.equal(error.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-06-21 03:54:15 +03:00
|
|
|
|
|
|
|
it('does not emit unhandled rejection events in the main process', function (done) {
|
|
|
|
remote.process.once('unhandledRejection', function (reason) {
|
|
|
|
done(reason)
|
|
|
|
})
|
|
|
|
|
|
|
|
var promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
|
|
|
|
promise.reject().then(function () {
|
|
|
|
done(new Error('Promise was not rejected'))
|
|
|
|
}).catch(function (error) {
|
|
|
|
assert.equal(error.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('emits unhandled rejection events in the renderer process', function (done) {
|
|
|
|
window.addEventListener('unhandledrejection', function (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
assert.equal(event.reason.message, 'rejected')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
var promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
|
|
|
|
promise.reject().then(function () {
|
|
|
|
done(new Error('Promise was not rejected'))
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote webContents', function () {
|
|
|
|
it('can return same object with different getters', function () {
|
|
|
|
var contents1 = remote.getCurrentWindow().webContents
|
|
|
|
var contents2 = remote.getCurrentWebContents()
|
2016-03-29 02:31:06 +03:00
|
|
|
assert(contents1 === contents2)
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('remote class', function () {
|
|
|
|
let cl = remote.require(path.join(fixtures, 'module', 'class.js'))
|
|
|
|
let base = cl.base
|
|
|
|
let derived = cl.derived
|
|
|
|
|
|
|
|
it('can get methods', function () {
|
|
|
|
assert.equal(base.method(), 'method')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can get properties', function () {
|
|
|
|
assert.equal(base.readonly, 'readonly')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can change properties', function () {
|
|
|
|
assert.equal(base.value, 'old')
|
|
|
|
base.value = 'new'
|
|
|
|
assert.equal(base.value, 'new')
|
|
|
|
base.value = 'old'
|
|
|
|
})
|
|
|
|
|
|
|
|
it('has unenumerable methods', function () {
|
|
|
|
assert(!base.hasOwnProperty('method'))
|
|
|
|
assert(Object.getPrototypeOf(base).hasOwnProperty('method'))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('keeps prototype chain in derived class', function () {
|
|
|
|
assert.equal(derived.method(), 'method')
|
|
|
|
assert.equal(derived.readonly, 'readonly')
|
|
|
|
assert(!derived.hasOwnProperty('method'))
|
|
|
|
let proto = Object.getPrototypeOf(derived)
|
|
|
|
assert(!proto.hasOwnProperty('method'))
|
|
|
|
assert(Object.getPrototypeOf(proto).hasOwnProperty('method'))
|
|
|
|
})
|
2016-04-01 09:35:34 +03:00
|
|
|
|
|
|
|
it('is referenced by methods in prototype chain', function () {
|
|
|
|
let method = derived.method
|
|
|
|
derived = null
|
2016-06-29 19:37:10 +03:00
|
|
|
global.gc()
|
2016-04-01 09:35:34 +03:00
|
|
|
assert.equal(method(), 'method')
|
2016-06-29 19:37:10 +03:00
|
|
|
})
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('ipc.sender.send', function () {
|
|
|
|
it('should work when sending an object containing id property', function (done) {
|
2016-02-17 04:09:41 +03:00
|
|
|
var obj = {
|
2016-01-12 05:40:23 +03:00
|
|
|
id: 1,
|
|
|
|
name: 'ly'
|
2016-03-25 23:03:49 +03:00
|
|
|
}
|
|
|
|
ipcRenderer.once('message', function (event, message) {
|
|
|
|
assert.deepEqual(message, obj)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('message', obj)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can send instance of Date', function (done) {
|
|
|
|
const currentDate = new Date()
|
|
|
|
ipcRenderer.once('message', function (event, value) {
|
|
|
|
assert.equal(value, currentDate.toISOString())
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
ipcRenderer.send('message', currentDate)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('ipc.sendSync', function () {
|
2016-05-09 14:08:50 +03:00
|
|
|
afterEach(function () {
|
|
|
|
ipcMain.removeAllListeners('send-sync-message')
|
|
|
|
})
|
|
|
|
|
2016-03-25 23:03:49 +03:00
|
|
|
it('can be replied by setting event.returnValue', function () {
|
|
|
|
var msg = ipcRenderer.sendSync('echo', 'test')
|
|
|
|
assert.equal(msg, 'test')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not crash when reply is not sent and browser is destroyed', function (done) {
|
|
|
|
this.timeout(10000)
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2016-08-03 22:47:53 +03:00
|
|
|
w = new BrowserWindow({
|
2016-01-12 05:40:23 +03:00
|
|
|
show: false
|
2016-03-25 23:03:49 +03: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-06 00:22:59 +03:00
|
|
|
|
|
|
|
it('does not crash when reply is sent by multiple listeners', function (done) {
|
2016-08-03 22:47:53 +03:00
|
|
|
w = new BrowserWindow({
|
2016-05-06 00:22:59 +03: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 23:03:49 +03:00
|
|
|
})
|
|
|
|
|
2016-05-18 16:14:51 +03:00
|
|
|
describe('ipcRenderer.sendTo', function () {
|
|
|
|
let contents = null
|
|
|
|
beforeEach(function () {
|
|
|
|
contents = webContents.create({})
|
|
|
|
})
|
|
|
|
afterEach(function () {
|
|
|
|
ipcRenderer.removeAllListeners('pong')
|
|
|
|
contents.destroy()
|
|
|
|
contents = null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sends message to WebContents', function (done) {
|
|
|
|
const webContentsId = remote.getCurrentWebContents().id
|
|
|
|
ipcRenderer.once('pong', function (event, id) {
|
|
|
|
assert.equal(webContentsId, id)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
contents.once('did-finish-load', function () {
|
|
|
|
ipcRenderer.sendTo(contents.id, 'ping', webContentsId)
|
|
|
|
})
|
|
|
|
contents.loadURL('file://' + path.join(fixtures, 'pages', 'ping-pong.html'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-03-25 23:03:49 +03:00
|
|
|
describe('remote listeners', function () {
|
|
|
|
it('can be added and removed correctly', function () {
|
2016-01-12 05:40:23 +03:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false
|
2016-03-25 23:03:49 +03:00
|
|
|
})
|
|
|
|
var listener = function () {}
|
|
|
|
w.on('test', listener)
|
|
|
|
assert.equal(w.listenerCount('test'), 1)
|
|
|
|
w.removeListener('test', listener)
|
|
|
|
assert.equal(w.listenerCount('test'), 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|