electron/spec/api-ipc-spec.js

211 lines
6.7 KiB
JavaScript
Raw Normal View History

2016-02-22 04:13:26 +00:00
'use strict';
2016-02-17 00:46:49 +00:00
const assert = require('assert');
const path = require('path');
2016-01-12 02:40:23 +00:00
2016-02-17 00:46:49 +00:00
const ipcRenderer = require('electron').ipcRenderer;
const remote = require('electron').remote;
2016-01-12 02:40:23 +00:00
2016-02-17 00:46:49 +00:00
const ipcMain = remote.require('electron').ipcMain;
const BrowserWindow = remote.require('electron').BrowserWindow;
2016-01-12 02:40:23 +00:00
2016-02-17 00:46:49 +00:00
const comparePaths = function(path1, path2) {
2016-01-12 02:40:23 +00:00
if (process.platform === 'win32') {
path1 = path1.toLowerCase();
path2 = path2.toLowerCase();
}
2016-02-17 01:39:11 +00:00
assert.equal(path1, path2);
2016-01-12 02:40:23 +00:00
};
describe('ipc module', function() {
var fixtures = path.join(__dirname, 'fixtures');
2016-01-12 02:40:23 +00:00
describe('remote.require', function() {
it('should returns same object for the same module', function() {
2016-02-17 17:27:25 +00:00
var dialog1 = remote.require('electron');
var dialog2 = remote.require('electron');
2016-02-17 01:39:11 +00:00
assert.equal(dialog1, dialog2);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('should work when object contains id property', function() {
2016-02-17 01:39:11 +00:00
var a = remote.require(path.join(fixtures, 'module', 'id.js'));
assert.equal(a.id, 1127);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('should search module from the user app', function() {
2016-01-12 02:40:23 +00:00
comparePaths(path.normalize(remote.process.mainModule.filename), path.resolve(__dirname, 'static', 'main.js'));
2016-02-17 01:39:11 +00:00
comparePaths(path.normalize(remote.process.mainModule.paths[0]), path.resolve(__dirname, 'static', 'node_modules'));
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('remote.createFunctionWithReturnValue', function() {
2016-02-17 01:39:11 +00:00
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));
2016-02-17 01:39:11 +00:00
assert.equal(result.constructor.name, 'Buffer');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('remote object in renderer', function() {
it('can change its properties', function() {
var property = remote.require(path.join(fixtures, 'module', 'property.js'));
2016-01-12 02:40:23 +00:00
assert.equal(property.property, 1127);
property.property = 1007;
assert.equal(property.property, 1007);
var property2 = remote.require(path.join(fixtures, 'module', 'property.js'));
2016-01-12 02:40:23 +00:00
assert.equal(property2.property, 1007);
2016-02-17 01:39:11 +00:00
property.property = 1127;
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('can construct an object from its member', function() {
2016-02-17 17:27:25 +00:00
var call = remote.require(path.join(fixtures, 'module', 'call.js'));
var obj = new call.constructor;
2016-02-17 01:39:11 +00:00
assert.equal(obj.test, 'test');
2016-01-12 02:40:23 +00:00
});
2016-03-04 23:56:18 +00:00
it('can reassign and delete its member functions', function() {
var remoteFunctions = remote.require(path.join(fixtures, 'module', 'function.js'));
assert.equal(remoteFunctions.aFunction(), 1127);
remoteFunctions.aFunction = function () { return 1234; };
assert.equal(remoteFunctions.aFunction(), 1234);
assert.equal(delete remoteFunctions.aFunction, true);
});
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
describe('remote value in browser', function() {
var print = path.join(fixtures, 'module', 'print_name.js');
2016-01-12 02:40:23 +00:00
it('keeps its constructor name for objects', function() {
var buf = new Buffer('test');
var print_name = remote.require(print);
2016-02-17 01:39:11 +00:00
assert.equal(print_name.print(buf), 'Buffer');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('supports instanceof Date', function() {
var now = new Date();
var print_name = remote.require(print);
2016-01-12 02:40:23 +00:00
assert.equal(print_name.print(now), 'Date');
2016-02-17 01:39:11 +00:00
assert.deepEqual(print_name.echo(now), now);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('remote promise', function() {
2016-02-17 01:39:11 +00:00
it('can be used as promise in each side', function(done) {
var promise = remote.require(path.join(fixtures, 'module', 'promise.js'));
2016-02-17 01:39:11 +00:00
promise.twicePromise(Promise.resolve(1234)).then(function(value) {
2016-01-19 22:49:40 +00:00
assert.equal(value, 2468);
2016-02-17 01:39:11 +00:00
done();
2016-01-19 22:49:40 +00:00
});
2016-01-12 02:40:23 +00:00
});
});
describe('remote webContents', function() {
it('can return same object with different getters', function() {
var contents1 = remote.getCurrentWindow().webContents;
var contents2 = remote.getCurrentWebContents();
assert(contents1 == contents2);
});
});
2016-02-22 04:13:26 +00: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-01-12 02:40:23 +00:00
describe('ipc.sender.send', function() {
2016-02-17 01:39:11 +00:00
it('should work when sending an object containing id property', function(done) {
var obj = {
2016-01-12 02:40:23 +00:00
id: 1,
name: 'ly'
};
ipcRenderer.once('message', function(event, message) {
assert.deepEqual(message, obj);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
ipcRenderer.send('message', obj);
2016-01-12 02:40:23 +00:00
});
2016-02-17 07:36:18 +00:00
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);
});
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
describe('ipc.sendSync', function() {
it('can be replied by setting event.returnValue', function() {
var msg = ipcRenderer.sendSync('echo', 'test');
2016-02-17 01:39:11 +00:00
assert.equal(msg, 'test');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('does not crash when reply is not sent and browser is destroyed', function(done) {
2016-01-12 02:40:23 +00:00
this.timeout(10000);
var w = new BrowserWindow({
2016-01-12 02:40:23 +00:00
show: false
});
ipcMain.once('send-sync-message', function(event) {
event.returnValue = null;
w.destroy();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
w.loadURL('file://' + path.join(fixtures, 'api', 'send-sync-message.html'));
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
describe('remote listeners', function() {
var w = null;
2016-01-12 02:40:23 +00:00
afterEach(function() {
2016-02-17 01:39:11 +00:00
w.destroy();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('can be added and removed correctly', function() {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
});
var listener = function() {};
2016-01-12 02:40:23 +00:00
w.on('test', listener);
assert.equal(w.listenerCount('test'), 1);
w.removeListener('test', listener);
2016-02-17 01:39:11 +00:00
assert.equal(w.listenerCount('test'), 0);
2016-01-12 02:40:23 +00:00
});
});
});