diff --git a/tests/www/js/tests_auto.js b/tests/www/js/tests_auto.js index c922beb..27c06be 100644 --- a/tests/www/js/tests_auto.js +++ b/tests/www/js/tests_auto.js @@ -19,12 +19,12 @@ window['cordova-plugin-chromecast-tests'].runner(function () { var utils = window['cordova-plugin-chromecast-tests'].utils; describe('cordova-plugin-chromecast', function () { - this.timeout(20000); + this.timeout(10000); this.slow(8000); this.bail(true); var appId = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID; - var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4'; + // var videoUrl = 'https://archive.org/download/CosmosLaundromatFirstCycle/Cosmos%20Laundromat%20-%20First%20Cycle%20%281080p%29.mp4'; // callOrder constants that are re-used frequently var success = 'success'; @@ -135,7 +135,6 @@ window['cordova-plugin-chromecast-tests'].runner(function () { }); describe('chrome.cast.cordova functions and session.leave', function () { - var scanStopped = false; var _route; var _session; it('should have definitions', function () { @@ -145,12 +144,41 @@ window['cordova-plugin-chromecast-tests'].runner(function () { assert.exists(chrome.cast.cordova.selectRoute); assert.exists(chrome.cast.cordova.Route); }); + it('startRouteScan 2nd call should result in error for first', function (done) { + var secondStarted = false; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (secondStarted) { + return; + } + secondStarted = true; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + }, function (err) { + // The only acceptable way for this scan to stop + assert.instanceOf(err, chrome.cast.Error); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }, function (err) { + assert.instanceOf(err, chrome.cast.Error); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Started a new route scan before stopping previous one.'); + done(); + }); + }); + it('stopRouteScan 2nd call should succeed', function (done) { + chrome.cast.cordova.stopRouteScan(function () { + chrome.cast.cordova.stopRouteScan(function () { + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); it('startRouteScan should find valid routes', function (done) { _route = undefined; chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { - if (scanStopped) { - assert.fail('Should not have gotten route update after scan was stopped'); - } if (_route) { return; // we have already found a valid route } @@ -170,24 +198,44 @@ window['cordova-plugin-chromecast-tests'].runner(function () { done(); } }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.instanceOf(err, chrome.cast.Error); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); }); }); - it('stopRouteScan should succeed', function (done) { - chrome.cast.cordova.stopRouteScan(function () { - scanStopped = true; + it('stopRouteScan should succeed and trigger cancel error in startRouteScan', function (done) { + var scanStopped = false; + var called = utils.callOrder([ + { id: update, repeats: false }, + { id: success, repeats: false } + ], function () { done(); + }); + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanStopped) { + assert.fail('Should not have gotten route update after scan was stopped'); + } + chrome.cast.cordova.stopRouteScan(function () { + scanStopped = true; + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); }, function (err) { - assert.fail(err.code + ': ' + err.description); + assert.instanceOf(err, chrome.cast.Error); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + called(update); }); }); it('selectRoute should receive a TIMEOUT error if route does not exist', function (done) { this.timeout(20000); - chrome.cast.cordova.selectRoute('definitely does not exist route id', function (session) { + var routeId = 'non-existant-route-id'; + chrome.cast.cordova.selectRoute(routeId, function (session) { assert.fail('should not have hit the success callback'); }, function (err) { assert.instanceOf(err, chrome.cast.Error); assert.equal(err.code, chrome.cast.ErrorCode.TIMEOUT); + assert.match(err.description, new RegExp('^Failed to join route \\(' + routeId + '\\) after 15s and [0-9]* tries\\.$')); done(); }); }); @@ -206,6 +254,7 @@ window['cordova-plugin-chromecast-tests'].runner(function () { }, function (err) { assert.instanceOf(err, chrome.cast.Error); assert.equal(err.code, chrome.cast.ErrorCode.SESSION_ERROR); + assert.equal(err.description, 'Leave or stop current session before attempting to join new session.'); done(); }); }); @@ -230,6 +279,131 @@ window['cordova-plugin-chromecast-tests'].runner(function () { assert.fail(err.code + ': ' + err.description); }); }); + it('initialize should not receive a session after session.leave', function (done) { + var apiConfig = new chrome.cast.ApiConfig(new chrome.cast.SessionRequest(appId), function sessionListener (session) { + assert.fail('should not receive a session (we did sessionLeave so we shouldnt be able to auto rejoin rejoin)'); + }); + chrome.cast.initialize(apiConfig, function () { + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.leave should give an error if session already left', function (done) { + _session.leave(function () { + assert.fail('session.leave - Should not call success'); + }, function (err) { + assert.instanceOf(err, chrome.cast.Error); + assert.equal(err.code, chrome.cast.Error.INVALID_PARAMETER); + assert.equal(err.description, 'No active session'); + done(); + }); + }); + }); + + describe('chrome.cast session functions', function () { + var session; + before(function (done) { + // need to have a valid session to run these tests + session = null; + var scanState = 'running'; + var foundRoute = null; + chrome.cast.cordova.startRouteScan(function routeUpdate (routes) { + if (scanState === 'stopped') { + assert.fail('Should not have gotten route update after scan was stopped'); + } + var route; + for (var i = 0; i < routes.length; i++) { + route = routes[i]; + assert.instanceOf(route, chrome.cast.cordova.Route); + assert.isString(route.id); + assert.isString(route.name); + assert.isBoolean(route.isNearbyDevice); + assert.isBoolean(route.isCastGroup); + if (!route.isNearbyDevice && !route.isCastGroup) { + foundRoute = route; + } + } + if (foundRoute && scanState === 'running') { + scanState = 'stopping'; + chrome.cast.cordova.stopRouteScan(function () { + scanState = 'stopped'; + chrome.cast.cordova.selectRoute(foundRoute.id, function (sess) { + utils.testSessionProperties(sess); + session = sess; + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + } + }, function (err) { + assert.instanceOf(err, chrome.cast.Error); + assert.equal(err.code, chrome.cast.ErrorCode.CANCEL); + assert.equal(err.description, 'Scan stopped.'); + }); + }); + it('session.setReceiverVolumeLevel should mute the volume', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: false } + ], done); + + // Do the opposite mute state as current + var muted = !session.receiver.volume.muted; + + session.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(session.receiver, chrome.cast.Receiver); + assert.instanceOf(session.receiver.volume, chrome.cast.Volume); + if (session.receiver.volume.muted === muted) { + session.removeUpdateListener(listener); + called(update); + } + }); + + session.setReceiverMuted(muted, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + it('session.setReceiverVolumeLevel should set the volume level', function (done) { + var called = utils.waitForAllCalls([ + { id: success, repeats: false }, + { id: update, repeats: false } + ], done); + + // Make sure the request volume is significantly different + var requestedVolume = Math.abs(session.receiver.volume.level - 0.5); + + session.addUpdateListener(function listener (isAlive) { + assert.isTrue(isAlive); + assert.instanceOf(session.receiver, chrome.cast.Receiver); + assert.instanceOf(session.receiver.volume, chrome.cast.Volume); + // Check that the receiver volume is approximate match + if (session.receiver.volume.level > requestedVolume - 0.1 && + session.receiver.volume.level < requestedVolume + 0.1) { + session.removeUpdateListener(listener); + called(update); + } + }); + + session.setReceiverVolumeLevel(requestedVolume, function () { + called(success); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); + after(function (done) { + session.stop(function () { + done(); + }, function (err) { + assert.fail(err.code + ': ' + err.description); + }); + }); }); });