[mediacontroller][ut] Custom events tests. 63/215463/3
authorMichal Michalski <m.michalski2@partner.samsung.com>
Tue, 8 Oct 2019 09:53:19 +0000 (11:53 +0200)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 8 Oct 2019 13:45:53 +0000 (13:45 +0000)
[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 <m.michalski2@partner.samsung.com>
Change-Id: I5069f64fc9ff6514ebe830eec30c1777b99bdffe

src/mediacontroller/js/ut/test_custom_event.js [new file with mode: 0644]

diff --git a/src/mediacontroller/js/ut/test_custom_event.js b/src/mediacontroller/js/ut/test_custom_event.js
new file mode 100644 (file)
index 0000000..997267c
--- /dev/null
@@ -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');
+    });