Proxy unit tests (#40)

* add unit test for proxy

* more unit tests for proxy
This commit is contained in:
Lynn
2017-03-23 16:19:59 -07:00
committed by GitHub
parent 2b0537945b
commit 2ded38c542
7 changed files with 571 additions and 23 deletions

View File

@@ -15,12 +15,18 @@ const apiCmds = apiEnums.cmds;
const apiName = apiEnums.apiName;
const apiProxyCmds = apiEnums.proxyCmds
// can be overridden for testing
let checkValidWindow = true;
/**
* Ensure events comes from a window that we have created.
* @param {EventEmitter} event node emitter event to be tested
* @return {Boolean} returns true if exists otherwise false
*/
function isValidWindow(event) {
if (!checkValidWindow) {
return true;
}
var result = false;
if (event && event.sender) {
// validate that event sender is from window we created
@@ -87,7 +93,7 @@ electron.ipcMain.on(apiName, (event, arg) => {
const Notify = require('./notify/notifyImpl.js');
// holds all project classes that can be created.
const api = {
let api = {
Notify: Notify
}
@@ -127,12 +133,14 @@ electron.ipcMain.on(apiProxyCmds.createObject, function(event, args) {
let objId = uniqueId();
liveObjs[objId] = obj;
obj.addEventListener('destroy', function() {
var liveObj = liveObjs[objId];
if (liveObj) {
delete liveObjs[objId];
}
});
if (typeof obj.addEventListener === 'function') {
obj.addEventListener('destroy', function() {
var liveObj = liveObjs[objId];
if (liveObj) {
delete liveObjs[objId];
}
});
}
setResult(objId);
} else {
@@ -206,7 +214,6 @@ electron.ipcMain.on(apiProxyCmds.invokeMethod, function(event, args) {
result = classType[args.methodName](...funcArgs);
} else {
let obj = liveObjs[args.objId];
if (!args.methodName || !obj[args.methodName]) {
event.sender.send(apiProxyCmds.invokeResult, {
error: 'no such method',
@@ -423,3 +430,15 @@ electron.ipcMain.on(apiProxyCmds.removeEvent, function(event, args) {
obj.removeEventListener(args.eventName, callbackFunc);
}
});
function addNewInterface(name, interfaceClass) {
api[name] = interfaceClass;
}
// expose these methods primarily for testing...
module.exports = {
addNewInterface: addNewInterface,
shouldCheckValidWindow: function(shouldCheck) {
checkValidWindow = shouldCheck;
}
}

View File

@@ -50,10 +50,6 @@ class Notify {
this.destroy();
}
destroy() {
this.emitter.removeAllListeners();
}
static get permission() {
return 'granted';
}
@@ -73,6 +69,13 @@ class Notify {
//
// private stuff below here
//
destroy() {
this.emitter.removeAllListeners();
// allow live instance to be destroyed
this.emitter.emit('destroy');
}
}
module.exports = Notify;

View File

@@ -32,12 +32,6 @@ class Notify {
*/
close() {}
/**
* call to clean up ref held by main process to notification.
* note: calling close will also invoke destroy.
*/
destroy() {}
/**
* This returns a promise and is always 'granted'
* @return {promise} promise fullfilled with 'granted'

View File

@@ -62,7 +62,6 @@ let constructorHandler = {
set: instanceSetHandler
}
// work like to incorporate something like https://github.com/EvolveLabs/electron-weak
// here to tell when object is destroyed so we can ipc main process to
// loss ref to liveObj.
@@ -76,7 +75,7 @@ let constructorHandler = {
function instanceGetHandler(target, name) {
// all methods and getters we support should be on the prototype
let prototype = Reflect.getPrototypeOf(target);
let prototype = Object.getPrototypeOf(target);
let desc = Object.getOwnPropertyDescriptor(prototype, name);
// does this have a "getter"
@@ -127,8 +126,8 @@ function addEventHandler(target) {
function removeEventHandler(target) {
return function(eventName, callback) {
if (target._callbacks && target._callback.has(callback)) {
let callbackObj = target._callback.get(callback);
if (target._callbacks && target._callbacks.has(callback)) {
let callbackObj = target._callbacks.get(callback);
let args = {
eventName: eventName,
@@ -233,7 +232,7 @@ function getHandler(target, property, isStatic) {
}
function instanceSetHandler(target, property, value) {
let prototype = Reflect.getPrototypeOf(target);
let prototype = Object.getPrototypeOf(target);
let desc = Object.getOwnPropertyDescriptor(prototype, property);
if (desc && desc.set) {