--- /dev/null
+mocha.setup('bdd');
+
+describe('sendSearchRequest', function() {
+ var client = null, server = null, sinfo = null, listenerId = null;
+
+ before(function() {
+ server = tizen.mediacontroller.createServer();
+ client = tizen.mediacontroller.getClient();
+ server.updatePlaybackState('PLAY');
+ sinfo = client.getLatestServerInfo();
+ });
+
+ afterEach(function() {
+ server.unsetSearchRequestListener(listenerId);
+ });
+
+ it('should trigger search request listener and reply callback', function (done) {
+ function requestCallback(client, request) {
+ chai.expect(request.length).to.equal(2);
+ var keywords = [request[0].keyword, request[1].keyword];
+ chai.expect(keywords).to.have.members(['rock', 'pop']);
+ return new tizen.mediacontroller.RequestReply({key: ['tizen1', 'tizen2']}, 123);
+ }
+ server.setSearchRequestListener(requestCallback);
+
+ var request = [
+ new tizen.mediacontroller.SearchFilter('MUSIC', 'GENRE', 'rock', null),
+ new tizen.mediacontroller.SearchFilter('MUSIC', 'GENRE', 'pop', null)
+ ];
+
+ sinfo.sendSearchRequest(request, function(reply) {
+ chai.expect(reply.code).to.equal(123);
+ chai.expect(reply.data.get('key')).to.deep.equal(['tizen1', 'tizen2']);
+ done();
+ });
+ });
+
+ it('should raise InvalidValuesErr if too many filters in the request', function (done) {
+ var request = [];
+ for (var i = 0; i < 20 + 1; ++i) {
+ request.push(
+ new tizen.mediacontroller.SearchFilter('MUSIC', 'GENRE', 'genre' + i)
+ );
+ }
+
+ try {
+ sinfo.sendSearchRequest(request, function(reply) {
+ done(new Error("search request reply received unexpectedly"));
+ });
+ done(new Error("sendSearchRequest should fail but did not"));
+ } catch (error) {
+ chai.expect(error.name).to.equal('InvalidValuesError');
+ done();
+ }
+ });
+
+ it('should throw type mismatch error for invalid request type', function (done) {
+ try {
+ sinfo.sendSearchRequest('not-array', function(reply) {
+ done(new Error("search request reply received unexpectedly"));
+ });
+ done(new Error("sendSearchRequest should fail but did not"));
+ } catch (error) {
+ chai.expect(error.name).to.equal('TypeMismatchError');
+ done();
+ }
+ });
+
+ it('set/unsetSearchRequestListener() methods should not throw an error', function (done) {
+ try {
+ server.unsetSearchRequestListener();
+ server.setSearchRequestListener(function(client, request) {});
+ server.unsetSearchRequestListener();
+ server.setSearchRequestListener(function(client, request) {});
+ server.unsetSearchRequestListener();
+ done();
+ } catch (error) {
+ done(new Error(error));
+ }
+ });
+
+ it('search filter objects should be properly constructed', function (done) {
+ var f1 = new tizen.mediacontroller.SearchFilter('MUSIC');
+ chai.expect(f1.contentType).to.equal('MUSIC');
+ chai.expect(f1.category).to.equal('NO_CATEGORY');
+ chai.expect(f1.keyword).to.equal(null);
+ chai.expect(f1.extraData).to.equal(null);
+
+ var f3 = new tizen.mediacontroller.SearchFilter('MUSIC', 'TITLE', 'tizen');
+ chai.expect(f3.contentType).to.equal('MUSIC');
+ chai.expect(f3.category).to.equal('TITLE');
+ chai.expect(f3.keyword).to.equal('tizen');
+ chai.expect(f3.extraData).to.equal(null);
+
+ var f4 = new tizen.mediacontroller.SearchFilter('MUSIC', 'TITLE', 'tizen', {});
+ chai.expect(f4.contentType).to.equal('MUSIC');
+ chai.expect(f4.category).to.equal('TITLE');
+ chai.expect(f4.keyword).to.equal('tizen');
+ chai.expect(f4.extraData).to.satisfy((x) => x instanceof tizen.Bundle);
+
+ var f5 = new tizen.mediacontroller.SearchFilter('MUSIC', 'TITLE', 'tizen', {"key": "value"});
+ chai.expect(f5.contentType).to.equal('MUSIC');
+ chai.expect(f5.category).to.equal('TITLE');
+ chai.expect(f5.keyword).to.equal('tizen');
+ chai.expect(f5.extraData).to.satisfy((x) => x instanceof tizen.Bundle);
+ chai.expect(f5.extraData.get("key")).to.equal('value');
+
+ done();
+ });
+
+ it('search filter object setters should work properly', function (done) {
+ var f1 = new tizen.mediacontroller.SearchFilter('MUSIC');
+ chai.expect(f1.contentType).to.equal('MUSIC');
+
+ f1.contentType = 'VIDEO';
+ chai.expect(f1.contentType).to.equal('VIDEO');
+
+ try {
+ f1.contentType = 'INVALID VALUE';
+ done(new Error("invalid content type value should cause error but did not"));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal("TypeMismatchError");
+ }
+
+ try {
+ f1.contentType = 10;
+ done(new Error("invalid content type value should cause error but did not"));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal('TypeMismatchError');
+ }
+
+ var f2 = new tizen.mediacontroller.SearchFilter('MUSIC');
+ chai.expect(f2.category).to.equal('NO_CATEGORY');
+ try {
+ f2.category = null;
+ done(new Error('search category is not nullable'));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal('TypeMismatchError');
+ }
+
+ try {
+ f2.category = 'INVALID VALUE';
+ done(new Error('invalid category value should cause an error but did not'));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal('TypeMismatchError');
+ }
+
+ var f3 = new tizen.mediacontroller.SearchFilter('MUSIC');
+ chai.expect(f3.keyword).to.be.null;
+
+ f3.keyword = 'hello';
+ chai.expect(f3.keyword).to.equal('hello');
+
+ f3.keyword = null;
+ chai.expect(f3.keyword).to.be.null;
+
+ f3.keyword = '';
+ chai.expect(f3.keyword).to.equal('');
+
+ var f4 = new tizen.mediacontroller.SearchFilter('MUSIC');
+ chai.expect(f4.extraData).to.be.null;
+
+ f4.extraData = new tizen.Bundle({key: "value"});
+ chai.expect(f4.extraData.get('key')).to.equal('value');
+
+ f4.extraData = null;
+ chai.expect(f4.extraData).to.be.null;
+
+ f4.extraData = {key: "value"};
+ chai.expect(f4.extraData.get('key')).to.equal('value');
+
+
+ f4.extraData = "invalid value";
+ chai.expect(f4.extraData.data).to.be.empty;
+
+ var f5 = new tizen.mediacontroller.SearchFilter('MUSIC', 'NO_CATEGORY', null);
+ chai.expect(f5.keyword).to.be.null;
+
+ try {
+ f5.category = "TITLE";
+ done(new Error("exception should be thrown if keyword == null and category != null"));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal("InvalidValuesError");
+ }
+
+ try {
+ var f6 = new tizen.mediacontroller.SearchFilter("MUSIC", "TITLE", null);
+ done(new Error("exception should be thrown if keyword == null and category != null"));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal("InvalidValuesError");
+ }
+
+ var f6 = new tizen.mediacontroller.SearchFilter("MUSIC", "TITLE", "tizen");
+ try {
+ f6.keyword = null;
+ done(new Error("exception should be thrown if keyword == null and category != null"));
+ }
+ catch (error) {
+ chai.expect(error.name).to.equal("InvalidValuesError");
+ }
+
+ done();
+ });
+
+});
+
+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');
+ });
+
+
+
-function TestDefaultCtor() {
- var bundle = new tizen.Bundle();
- var counter = 0;
- bundle.forEach(function(key, value, type) {
- counter++;
- });
- assertEqual(0, counter, 'bundle should be empty');
-}
-
-function TestCopyCtor() {
- var src = new tizen.Bundle({
- key1: 'value',
- key2: ['value1', 'value2'],
- });
+mocha.setup('bdd');
- var dest = new tizen.Bundle(src);
-
- assertEqual(Object.keys(src.data).length, Object.keys(dest.data).length, "number of entries is different");
- assertEqual(src.data.key1, dest.data.key1, "key1 entry has different value");
- assertArrayEqual(src.data.key2, dest.data.key2, "key2 entry has different value");
-}
+describe('default constructor', function () {
+ it('should initialize empty bundle object', function (done) {
+ var bundle = new tizen.Bundle();
+ chai.expect(bundle.data).to.be.empty;
+ done();
+ });
+});
-function TestJsonCtor() {
+describe('json constructor', function () {
+ it('should properly initialize entries of all types', function (done) {
var json = {
key1: 'value',
key2: ['value1', 'value2'],
key6: [[1, 2, 3], [4, 5, 6]],
key7: [],
key8: null,
- key9: {}
+ key9: {},
+ key10: [1,2,300]
};
var bundle = new tizen.Bundle(json);
+ chai.expect(bundle.data.key1).to.equal('value');
+ chai.expect(bundle.data.key2).to.deep.equal(json.key2);
+ chai.expect(bundle.data.key3).to.deep.equal(json.key3);
+ chai.expect(bundle.data.key4).to.deep.equal(json.key4);
+ chai.expect(bundle.data.key5).to.deep.equal(json.key3);
+ chai.expect(bundle.data.key6).to.deep.equal(json.key4);
+ chai.expect(bundle.data.key7).to.be.empty;
+ chai.expect(bundle.data.key8).to.equal('null');
+ chai.expect(bundle.data.key9).to.equal('[object Object]');
+ chai.expect(bundle.data.key10).to.equal('1,2,300');
+ done();
+ });
+});
+
+describe('copy constructor', function () {
+ it('should create deep copy of bundle data', function (done) {
+ var src = new tizen.Bundle({
+ key1: 'value',
+ key2: ['value1', 'value2'],
+ });
+ var dest = new tizen.Bundle(src);
+ chai.expect(dest).to.deep.equal(src);
+ done();
+ });
+});
- assertEqual(bundle.data.key1, 'value', 'invalid value of key1');
- assertArrayEqual(bundle.data.key2, ['value1', 'value2'], 'invalid value of key2');
- assertArrayEqual(
- bundle.data.key3,
- new Uint8Array([1, 2, 3]),
- 'invalid value of key3'
- );
- assertEqual(bundle.data.key4.length, 2, 'not enough elements in key4');
- assertArrayEqual(
- bundle.data.key4[0],
- new Uint8Array([1, 2, 3]),
- 'invalud value of key4[0]'
- );
- assertArrayEqual(
- bundle.data.key4[1],
- new Uint8Array([4, 5, 6]),
- 'invalud value of key4[1]'
- );
- assertArrayEqual(
- bundle.data.key5,
- new Uint8Array([1, 2, 3]),
- 'invalid value of key5'
- );
- assertEqual(bundle.data.key6.length, 2, 'invalid length of key6');
- assertArrayEqual(
- bundle.data.key6[0],
- new Uint8Array([1, 2, 3]),
- 'invalud value of key6[0]'
- );
- assertArrayEqual(
- bundle.data.key6[1],
- new Uint8Array([4, 5, 6]),
- 'invalud value of key6[1]'
- );
- assertArrayEqual(bundle.data.key7, [], 'invalid value of key7');
- assertEqual(bundle.data.key8, 'null', 'invalid value of key8');
- assertEqual(bundle.data.key9, '[object Object]', 'invalid value of key9');
-}
-
-function TestSetter() {
+describe('setter and getter', function () {
+ it('should set string entry', function (done) {
var bundle = new tizen.Bundle();
bundle.set('string', 'hello, world');
- assertEqual(bundle.data['string'], 'hello, world', 'invalid string value');
+ chai.expect(bundle.get('string')).to.equal('hello, world');
+ chai.expect(bundle.typeOf('string')).to.equal('STRING');
+ done();
+ });
+ it('should set string array entry', function (done) {
+ var bundle = new tizen.Bundle();
bundle.set('stringArray', ['hello', 'world']);
- assertArrayEqual(
- bundle.data['stringArray'],
- ['hello', 'world'],
- 'invalid string array value'
- );
-
- bundle.set('bytes', new Uint8Array([1, 2, 3, 4]));
- assertArrayEqual(bundle.data['bytes'], [1, 2, 3, 4], 'invalid bytes value');
-
- bundle.set('bytesArray', [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]);
- assertEqual(bundle.data['bytesArray'].length, 2, 'wrong number of byte streams');
- assertArrayEqual(bundle.data['bytesArray'][0], [1, 2, 3], 'wrong byte stream');
- assertArrayEqual(bundle.data['bytesArray'][1], [4, 5, 6], 'wrong byte stream');
-
- bundle.set('other', function() {});
- assertEqual(
- bundle.typeOf('other') == tizen.BundleValueType.STRING,
- true,
- 'other type should be converted to string'
- );
- assertEqual(bundle.data['other'], 'function () {}', 'invalid value for other type');
-
- bundle.set('someKey', undefined);
- assertEqual(bundle.data['someKey'], 'undefined');
-}
+ chai.expect(bundle.get('stringArray')).to.deep.equal(['hello', 'world']);
+ chai.expect(bundle.typeOf('stringArray')).to.equal('STRING_ARRAY');
+ done();
+ });
-function TestGetters() {
- var json = {
- key1: 'value',
- key2: ['value1', 'value2'],
- key3: new Uint8Array([1, 2, 3]),
- key4: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]
- };
+ it('should set byte stream entry', function (done) {
+ var bundle = new tizen.Bundle();
+ bundle.set('bytes', new Uint8Array([1,2,3]));
+ chai.expect(bundle.get('bytes')).to.deep.equal(new Uint8Array([1,2,3]));
+ chai.expect(bundle.typeOf('bytes')).to.equal('BYTES');
+ done();
+ });
- var bundle = new tizen.Bundle(json);
+ it('should set byte streams array entry', function (done) {
+ var bundle = new tizen.Bundle();
+ bundle.set('bytesArray', [
+ new Uint8Array([1,2,3]),
+ new Uint8Array([4,5,6])
+ ]);
+ chai.expect(bundle.get('bytesArray')).to.deep.equal([
+ new Uint8Array([1,2,3]),
+ new Uint8Array([4,5,6])
+ ]);
+ chai.expect(bundle.typeOf('bytesArray')).to.equal('BYTES_ARRAY');
+ done();
+ });
+
+ it('should convert unsupported types to strings', function (done) {
+ var bundle = new tizen.Bundle();
- var strVal = bundle.get('key1');
- assertEqual(strVal, 'value', 'string getter returned invalid value');
+ bundle.set('other1', function () {});
+ bundle.set('other2', undefined);
- var strArrVal = bundle.get('key2');
- assertArrayEqual(
- strArrVal,
- ['value1', 'value2'],
- 'string array getter returned invalid value'
- );
+ chai.expect(bundle.typeOf('other1')).to.equal('STRING');
+ chai.expect(bundle.get('other1')).to.equal('function () {}');
- var bytesVal = bundle.get('key3');
- assertArrayEqual(bytesVal, [1, 2, 3], 'bytes getter returned invalid value');
+ chai.expect(bundle.typeOf('other2')).to.equal('STRING');
+ chai.expect(bundle.get('other2')).to.equal('undefined');
- var bytesArrVal = bundle.get('key4');
- assertEqual(bytesArrVal.length, 2, 'wrong number of byte streams');
- assertArrayEqual(bytesArrVal[0], [1, 2, 3], 'byte stream invalid');
- assertArrayEqual(bytesArrVal[1], [4, 5, 6], 'byte stream invalid');
+ done();
+ });
+ it('should throw NotFoundErr if key does not exist - get()', function (done) {
+ var bundle = new tizen.Bundle();
try {
- var notVal = bundle.get('not-a-key');
- assertEqual(true, false, 'expected exception was not thrown');
- } catch (err) {
- assertEqual(err.code, WebAPIException.NOT_FOUND_ERR, 'expected NOT_FOUND_ERR');
+ bundle.get("not-a-key");
+ done(new Error("bundle.get() should fail but it did not"));
}
-}
-
-function TestTypeOf() {
- var json = {
- key1: 'value',
- key2: ['value1', 'value2'],
- key3: new Uint8Array([1, 2, 3]),
- key4: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])],
- key5: [],
- key6: [1, 2, 3],
- key7: [[1, 2, 3], [4, 5, 6]]
- };
-
- var bundle = new tizen.Bundle(json);
- assertEqual(
- bundle.typeOf('key1'),
- tizen.BundleValueType.STRING,
- 'expected STRING type'
- );
- assertEqual(
- bundle.typeOf('key2'),
- tizen.BundleValueType.STRING_ARRAY,
- 'expected STRING_ARRAY type'
- );
- assertEqual(
- bundle.typeOf('key3'),
- tizen.BundleValueType.BYTES,
- 'expected BYTES type'
- );
- assertEqual(
- bundle.typeOf('key4'),
- tizen.BundleValueType.BYTES_ARRAY,
- 'expected BYTES_ARRAY'
- );
- assertEqual(
- bundle.typeOf('key5'),
- tizen.BundleValueType.STRING_ARRAY,
- 'empty array should be treated like STRING_ARRAY'
- );
- assertEqual(
- bundle.typeOf('key6'),
- tizen.BundleValueType.BYTES,
- 'expected BYTES type'
- );
- assertEqual(
- bundle.typeOf('key7'),
- tizen.BundleValueType.BYTES_ARRAY,
- 'expected BYTES_ARRAY type'
- );
+ catch (error) {
+ chai.expect(error.code).to.equal(WebAPIException.NOT_FOUND_ERR);
+ done();
+ }
+ });
+ it('should throw NotFoundErr if key does not exist - typeOf()', function (done) {
+ var bundle = new tizen.Bundle();
try {
- type = bundle.typeOf('notKey');
- assertEqual(false, true, 'exception was not thrown');
- } catch (err) {
- assertEqual(
- err.code,
- WebAPIException.NOT_FOUND_ERR,
- 'expected NOT_FOUND_ERR exception'
- );
+ bundle.get("not-a-key");
+ done(new Error("bundle.get() should fail but it did not"));
}
-}
+ catch (error) {
+ chai.expect(error.code).to.equal(WebAPIException.NOT_FOUND_ERR);
+ done();
+ }
+ });
+});
-function TestForEach() {
+describe('forEach', function () {
+ it('should trigger callback for each entry', function (done) {
var json = {
key1: 'value',
key2: ['value1', 'value2'],
key3: new Uint8Array([1, 2, 3]),
key4: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]
};
-
var bundle = new tizen.Bundle(json);
- var assertions = {
- key1: {
- active: true,
- check: function(key, value, type) {
- assertEqual(value, 'value', 'invalid value for key ' + key);
- assertEqual(type, tizen.BundleValueType.STRING, 'should be STRING');
- }
- },
- key2: {
- active: true,
- check: function(key, value, type) {
- assertArrayEqual(
- value,
- ['value1', 'value2'],
- 'invalid value for key ' + key
- );
- assertEqual(
- type,
- tizen.BundleValueType.STRING_ARRAY,
- 'should be STRING_ARRAY'
- );
- }
- },
- key3: {
- active: true,
- check: function(key, value, type) {
- assertArrayEqual(value, [1, 2, 3], 'invalid byte stream');
- assertEqual(type, tizen.BundleValueType.BYTES, 'should be BYTES');
- }
- },
- key4: {
- active: true,
- check: function(key, value, type) {
- assertEqual(value.length, 2, 'invalid number of byte streams');
- assertArrayEqual(value[0], [1, 2, 3], 'invalid byte stream');
- assertArrayEqual(value[1], [4, 5, 6], 'invalid byte stream');
- assertEqual(type, tizen.BundleValueType.BYTES_ARRAY);
- }
- }
- };
-
- bundle.forEach(function(key, value, type) {
- assertEqual(assertions[key].active, true, 'inactive assertion triggered');
- assertions[key].check(key, value, type);
- assertions[key].active = false;
+ var keys = Object.keys(json);
+ bundle.forEach(function (key, value, type) {
+ var index = keys.indexOf(key);
+ chai.expect(index).to.be.gte(0);
+ keys.splice(index, 1);
});
+ chai.expect(keys).to.be.empty;
+ done();
+ });
+});
- for (var id in assertions) {
- if (assertions.hasOwnProperty(id)) {
- assertEqual(false, assertions[id].active, 'unused assertion with id ' + id);
- }
- }
-}
-
-function TestToJson() {
+describe('toJSON', function () {
+ it('should properly convert bundle to json', function (done) {
var json = {
key1: 'value',
key2: ['value1', 'value2'],
key3: new Uint8Array([1, 2, 3]),
key4: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]
};
-
var result = new tizen.Bundle(json).toJSON();
- assertEqual(json.key1, result.key1, 'key1 differs');
- assertArrayEqual(json.key2, result.key2, 'key2 differs');
- assertEqual(true, Array.isArray(result.key3), 'key3 not an array');
- assertEqual(result.key4.length, json.key4.length, 'key4 length differs');
- assertEqual(true, Array.isArray(result.key4[0]), 'key4[0] element not an array');
- assertEqual(true, Array.isArray(result.key4[1]), 'key4[1] element not an array');
- assertArrayEqual(json.key4[0], result.key4[0], 'key4[0] differs');
- assertArrayEqual(json.key4[1], result.key4[1], 'key4[1] differs');
-}
-
-function TestToString() {
+ chai.expect(result.key1).to.equal(json.key1);
+ chai.expect(result.key2).to.deep.equal(json.key2);
+ chai.expect(result.key3).to.deep.equal([1,2,3]);
+ chai.expect(result.key4).to.deep.equal([[1,2,3], [4,5,6]]);
+ done();
+ });
+});
+
+describe('toString', function () {
+ it('should properly convert bundle to string', function (done) {
var json = {
key1: 'value',
key2: ['value1', 'value2'],
var expected =
'{"key1":"value","key2":["value1","value2"],"key3":[1,2,3],' +
'"key4":[[1,2,3],[4,5,6]],"key5":[1,2,3],"key6":[[1,2,3],[4,5,6]]}';
-
- assertEqual(bundle.toString(), expected, 'string forms differ');
-}
-
-var testcases = [
- TestDefaultCtor,
- TestJsonCtor,
- TestCopyCtor,
- TestSetter,
- TestGetters,
- TestTypeOf,
- TestForEach,
- TestToJson,
- TestToString
-];
+ chai.expect(bundle.toString()).to.equal(expected);
+ done();
+ });
+});
+
+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');
+ });