From: Michal Michalski Date: Tue, 8 Oct 2019 09:53:19 +0000 (+0200) Subject: [mediacontroller][ut] Custom events tests. X-Git-Tag: submit/tizen/20191009.054725~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F63%2F215463%2F3;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [mediacontroller][ut] Custom events tests. [ACR] http://suprem.sec.samsung.net/jira/browse/TWDAPI-209 Tests for custom events feature of media controller. [Verification] none. Signed-off-by: Michal Michalski Change-Id: I5069f64fc9ff6514ebe830eec30c1777b99bdffe --- diff --git a/src/mediacontroller/js/ut/test_custom_event.js b/src/mediacontroller/js/ut/test_custom_event.js new file mode 100644 index 00000000..997267c9 --- /dev/null +++ b/src/mediacontroller/js/ut/test_custom_event.js @@ -0,0 +1,175 @@ +mocha.setup('bdd'); + +var server = tizen.mediacontroller.createServer(); +var client = tizen.mediacontroller.getClient(); +server.updatePlaybackState('PLAY'); + +describe('MediaControllerServer::getAllClientsInfo', function() { + it('should return client info for this application', function(done) { + var clientsInfo = server.getAllClientsInfo(); + chai.expect(clientsInfo.length).to.equal(1); + var appId = tizen.application.getCurrentApplication().appInfo.id; + chai.expect(clientsInfo[0].name).to.equal(appId); + done(); + }); +}); + +describe('MediaControllerClientInfo::name', function() { + it('should be readonly attribute', function(done) { + var clientsInfo = server.getAllClientsInfo(); + var old = clientsInfo[0].name; + clientsInfo[0].name = 'other'; + chai.expect(clientsInfo[0].name).to.equal(old); + done(); + }); +}); + +describe('MediaControllerClient::setCustomEventListener', function() { + it('should throw TypeMismatchError if argument is not a function', function(done) { + try { + client.setCustomEventListener('not-a-function'); + done(new Error('Expected TypeMismatchError.')); + } catch (error) { + chai.expect(error.name).to.equal('TypeMismatchError'); + done(); + } + }); + + it('should override previously set listener if called multiple times', function(done) { + function firstListener(serverName, event, data) { + done(new Error('first listener should have been overriden')); + } + function secondListener(serverName, event, data) { + done(); + } + function callback(r) { + /* ignore */ + } + + client.setCustomEventListener(firstListener); + client.setCustomEventListener(secondListener); + server.getAllClientsInfo()[0].sendEvent('test', {}, callback); + client.unsetCustomEventListener(); + }); +}); + +describe('MediaControllerClient::unsetCustomEventListener', function() { + it('should have no effect if the listener is not set', function(done) { + function listener(server, event, data) { + /* ignore */ + } + client.unsetCustomEventListener(); + client.setCustomEventListener(listener); + client.unsetCustomEventListener(); + client.unsetCustomEventListener(); + done(); + }); + + it('should disable event listener', function(done) { + var eventCounter = 0; + function listener(server, event, data) { + if (eventCounter++ > 0) { + done('Too many events received.'); + } + } + function callback(reply) { + /* ignore */ + } + + var cinfo = server.getAllClientsInfo()[0]; + client.setCustomEventListener(listener); + cinfo.sendEvent('Event1', null, callback); + client.unsetCustomEventListener(); + cinfo.sendEvent('Event2', null, callback); + + setTimeout(function() { + done(); + }, 5000); + }); +}); + +describe('MediaControllerClientInfo::sendEvent', function() { + var cinfo = null; + before(function() { + cinfo = server.getAllClientsInfo()[0]; + }); + + it('should throw TypeMismatchError if any argument has invalid type', function(done) { + try { + cinfo.sendEvent('TestEvent', null, 'not-a-function'); + done(new Error('Expected TypeMismatchError')); + } catch (error) { + chai.expect(error.name).to.equal('TypeMismatchError'); + } + try { + cinfo.sendEvent('TestEvent', 'not-an-object-or-null', function(reply) {}); + done(new Error('Expected TypeMismatchError')); + } catch (error) { + chai.expect(error.name).to.equal('TypeMismatchError'); + } + done(); + }); + + it('should trigger client listener', function(done) { + function listener(serverName, event, data) { + var currentAppId = tizen.application.getCurrentApplication().appInfo.appId; + chai.expect(serverName).to.equal(currentAppId); + chai.expect(event).to.equal('TestEvent'); + chai.expect(data).to.equal(null); + done(); + } + function callback(reply) { + /* ignore */ + } + client.setCustomEventListener(listener); + cinfo.sendEvent('TestEvent', null, callback); + }); + + it('should receive reply via callback', function(done) { + function listener(server, event, data) { + return new tizen.mediacontroller.RequestReply(null, 123); + } + + function callback(reply) { + chai.expect(reply.code).to.equal(123); + chai.expect(reply.data).to.equal(null); + done(); + } + + client.setCustomEventListener(listener); + cinfo.sendEvent('TestEvent', null, callback); + }); + + it('should pass data bundle', function(done) { + function listener(server, event, data) { + chai.expect(event).to.equal('TestEvent'); + chai.expect(data.get('test')).to.equal('data'); + return new tizen.mediacontroller.RequestReply( + { TestReply: 'ReplyData' }, + 123 + ); + } + + function callback(reply) { + chai.expect(reply.data.get('TestReply')).to.equal('ReplyData'); + done(); + } + + client.setCustomEventListener(listener); + cinfo.sendEvent('TestEvent', new tizen.Bundle({ test: 'data' }), callback); + }); +}); + +mocha.checkLeaks(); +mocha + .run() + .on('pass', function(test) { + console.log(test.title + ' OK'); + }) + .on('fail', function(test, err) { + console.error(test.title + ' FAILED'); + console.error(err); + }) + .on('end', function() { + console.log('All done'); + });