Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
#1620 - WIP - Adding native spec and code support for Android multica…
Browse files Browse the repository at this point in the history
…st lock

There is a bug in this code that causes a test to fail in testThaliMobile because the wifiBasedNativeMock changes its version of thaliWifiInfrastructure via proxyquire to just be a skeleton that is missing the methods I added here. So I have to fix that.

testthaliWifiInfrastructure - The check for platform.isMobile was wrong since that is always true. We needed to check for real mobile only. I also added two tests that make sure that we do call multicast lock on Android and don't call multicast lock anywhere else.

wifiBasedNativeMock - Added two thunk calls to fake calling the Android lock methods. And linting.

promiseQueue - Linting

thaliMobileNative - Defined how the native implementation of the Android multicast lock methods are to work. Also removed the definition of toggleWifi. This is actually defined by JXcore, not us and anyway per #1601 we are supposed to be out of this business and leave it to JXcore. Also linting.

thaliMobileNativeWrapper - Adding support for calling the multicast lock native call on Android

thaliWifiInfrastructure - Added the logic to get and release multicast locks on the Android platform. And a lot of linting.
  • Loading branch information
yaronyg committed Dec 14, 2016
1 parent aaf35e7 commit 90d2727
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 82 deletions.
62 changes: 58 additions & 4 deletions test/www/jxcore/bv_tests/testThaliWifiInfrastructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,10 @@ test('does not get peer changes from self', function (t) {
knownOwnPeerIdentifiers.push(wifiInfrastructure.peer.peerIdentifier);
return Promise.delay(thaliConfig.SSDP_ADVERTISEMENT_INTERVAL * 2);
}).then(function () {
return wifiInfrastructure.startUpdateAdvertisingAndListening();
return wifiInfrastructure.startUpdateAdvertisingAndListening();
}).then(function () {
knownOwnPeerIdentifiers.push(wifiInfrastructure.peer.peerIdentifier);
return Promise.delay(thaliConfig.SSDP_ADVERTISEMENT_INTERVAL * 2);
knownOwnPeerIdentifiers.push(wifiInfrastructure.peer.peerIdentifier);
return Promise.delay(thaliConfig.SSDP_ADVERTISEMENT_INTERVAL * 2);
}).then(function () {
wifiInfrastructure.removeListener(
'wifiPeerAvailabilityChanged',
Expand All @@ -591,9 +591,63 @@ test('does not get peer changes from self', function (t) {
});
});

test('Make sure we turn on and off the Android multicast locks',
function () {
return !platform.isAndroid;
},
function (t) {
var lockSpy = sinon.spy(ThaliMobileNativeWrapper,
'lockAndroidWifiMulticast');
var unlockSpy = sinon.spy(ThaliMobileNativeWrapper,
'unlockAndroidWifiMulticast');
wifiInfrastructure.startListeningForAdvertisements()
.then(function () {
t.equals(lockSpy.callCount, 1, 'We have locked');
t.equals(unlockSpy.callCount, 0, 'We have not unlocked');
return wifiInfrastructure.stopListeningForAdvertisements();
})
.then(function () {
t.equals(lockSpy.callCount, 1, 'No new locks');
t.equals(unlockSpy.callCount, 1, 'We unlocked');
})
.catch(function (err) {
t.fail(err);
})
.then(function () {
t.end();
});
});

test('Make sure we do not use Android locks when we are not on Android',
function () {
return platform.isAndroid;
},
function (t) {
var lockSpy = sinon.spy(ThaliMobileNativeWrapper,
'lockAndroidWifiMulticast');
var unlockSpy = sinon.spy(ThaliMobileNativeWrapper,
'unlockAndroidWifiMulticast');
wifiInfrastructure.startListeningForAdvertisements()
.then(function () {
t.equals(lockSpy.callCount, 0, 'We have no lock');
t.equals(unlockSpy.callCount, 0, 'We have not unlocked');
return wifiInfrastructure.stopListeningForAdvertisements();
})
.then(function () {
t.equals(lockSpy.callCount, 0, 'Still no lock');
t.equals(unlockSpy.callCount, 0, 'Still not unlocked');
})
.catch(function (err) {
t.fail(err);
})
.then(function () {
t.end();
});
});

// From here onwards, tests only work on mocked up desktop
// environment where network changes can be simulated.
if (platform.isMobile) {
if (platform._isRealMobile) {
return;
}

Expand Down
61 changes: 44 additions & 17 deletions test/www/jxcore/lib/wifiBasedNativeMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ var startListeningForAdvertisementsIsActive = false;
* @param {module:thaliMobileNative~ThaliMobileCallback} callback
*/
MobileCallInstance.prototype.startListeningForAdvertisements =
function (callback) {// jscs:ignore disallowUnusedParams
function (callback) {// eslint-disable-line no-unused-vars
return CallOnce.check(this, '_startListeningForAdvertisements', arguments);
};

Expand All @@ -291,7 +291,7 @@ MobileCallInstance.prototype._startListeningForAdvertisements =
* @param {module:thaliMobileNative~ThaliMobileCallback} callBack
*/
MobileCallInstance.prototype.stopListeningForAdvertisements =
function (callBack) {// jscs:ignore disallowUnusedParams
function (callBack) {// eslint-disable-line no-unused-vars
return CallOnce.check(this, '_stopListeningForAdvertisements', arguments);
};

Expand Down Expand Up @@ -351,7 +351,7 @@ var startUpdateAdvertisingAndListeningIsActive = false;
* @param {module:thaliMobileNative~ThaliMobileCallback} callback
*/
MobileCallInstance.prototype.startUpdateAdvertisingAndListening =
function (portNumber, callback) {// jscs:ignore disallowUnusedParams
function (portNumber, callback) {// eslint-disable-line no-unused-vars
return CallOnce.check(this, '_startUpdateAdvertisingAndListening',
arguments);
};
Expand Down Expand Up @@ -410,7 +410,7 @@ function closeAndDeleteOutgoingProxyServer(peerIdentifier) {
* @param {module:thaliMobileNative~ThaliMobileCallback} callBack
*/
MobileCallInstance.prototype.stopAdvertisingAndListening =
function (callBack) {// jscs:ignore disallowUnusedParams
function (callBack) {// eslint-disable-line no-unused-vars
return CallOnce.check(this, '_stopAdvertisingAndListening', arguments);
};

Expand Down Expand Up @@ -479,7 +479,7 @@ MobileCallInstance.prototype._disconnect =
}

callback(null);
};
};

function returnPort(peerIdentifier, callback) {
callback(null, JSON.stringify({
Expand Down Expand Up @@ -566,17 +566,17 @@ MobileCallInstance.prototype._androidConnectLogic =
peerConnections[peerIdentifier] = net.connect(peerToConnect.portNumber,
function () {
setTimeout(function () {
if (!peerProxyServers[peerIdentifier]) {
var error = 'Unspecified Error with Radio infrastructure';
return callback(error);
}
peerConnections[peerIdentifier]
if (!peerProxyServers[peerIdentifier]) {
var error = 'Unspecified Error with Radio infrastructure';
return callback(error);
}
peerConnections[peerIdentifier]
.removeListener('error', failedPeerConnectionError);
peerConnections[peerIdentifier]
peerConnections[peerIdentifier]
.on('error', simplePeerConnectionError);
returnConnectResponse(peerIdentifier, callback,
returnConnectResponse(peerIdentifier, callback,
cleanProxyServer);
},
},
100);
});
peerConnections[peerIdentifier].on('end', function () {
Expand Down Expand Up @@ -803,6 +803,28 @@ MobileCallInstance.prototype.killConnections = function (callback) {
callback('Not Supported');
};

MobileCallInstance.prototype.lockAndroidWifiMulticast =
function (callback) {// eslint-disable-line no-unused-vars
return CallOnce.check(this, '_lockAndroidWifiMulticast', arguments);
};


MobileCallInstance.prototype._lockAndroidWifiMulticast =
function (callback) {
callback(null);
};

MobileCallInstance.prototype.unlockAndroidWifiMulticast =
function (callback) {// eslint-disable-line no-unused-vars
return CallOnce.check(this, '_unlockAndroidWifiMulticast', arguments);
};

MobileCallInstance.prototype._unlockAndroidWifiMulticast =
function (callback) {
callback(null);
};


var fakeDeviceName = uuid.v4();
/**
* Generates a UUID that will be used as the name of the current device.
Expand Down Expand Up @@ -856,6 +878,12 @@ MobileCallInstance.prototype.callNative = function () {
case 'killConnections': {
return this.killConnections(arguments[0]);
}
case 'lockAndroidWifiMulticast': {
return this.lockAndroidWifiMulticast(arguments[0]);
}
case 'unlockAndroidWifiMulticast': {
return this.unlockAndroidWifiMulticast(arguments[0]);
}
case 'GetDeviceName': {
return this.getDeviceName(arguments[0]);
}
Expand Down Expand Up @@ -1143,7 +1171,6 @@ function wifiPeerAvailabilityChanged(platform, thaliWifiInfrastructure) {
};
}

// jscs:disable jsDoc
/**
* To use this mock save the current global object Mobile (if it exists) and
* replace it with this object. In general this object won't exist on the
Expand All @@ -1156,7 +1183,6 @@ function wifiPeerAvailabilityChanged(platform, thaliWifiInfrastructure) {
* stack. We need it here so we can add a router to simulate the iOS case where
* we need to let the other peer know we want a connection.
*/
// jscs:enable jsDoc
function WifiBasedNativeMock(platformName, router) {
assert(platformName, 'platformName must be set');
if (!router) {
Expand All @@ -1181,14 +1207,15 @@ function WifiBasedNativeMock(platformName, router) {
mobileHandler.toggleBluetooth =
toggleBluetooth(platformName, thaliWifiInfrastructure);

mobileHandler.toggleWiFi =
mobileHandler.toggleWiFi =
toggleWiFi(platform, thaliWifiInfrastructure);

mobileHandler.firePeerAvailabilityChanged =
firePeerAvailabilityChanged(platformName, thaliWifiInfrastructure);

mobileHandler.fireIncomingConnectionToPortNumberFailed =
fireIncomingConnectionToPortNumberFailed(platformName, thaliWifiInfrastructure);
fireIncomingConnectionToPortNumberFailed(platformName,
thaliWifiInfrastructure);

mobileHandler.fireDiscoveryAdvertisingStateUpdateNonTCP =
fireDiscoveryAdvertisingStateUpdateNonTCP(platformName,
Expand Down
1 change: 1 addition & 0 deletions thali/NextGeneration/promiseQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function _finishPromise (localFn, globalResolveFn) {
* @private
* @param {module:promiseQueue~promiseFunction} fn
* @param {module:promiseQueue~unshiftOrPush} unshiftOrPushFn
* @return {Promise<?Error>}
*/
PromiseQueue.prototype._changeQueue = function (fn, unshiftOrPushFn) {
var self = this;
Expand Down
Loading

0 comments on commit 90d2727

Please sign in to comment.