[tizen] Add copy ctor to tizen.Bundle. 45/211145/3
authorMichal Michalski <m.michalski2@partner.samsung.com>
Tue, 30 Jul 2019 10:08:57 +0000 (12:08 +0200)
committerPiotr Kosko <p.kosko@samsung.com>
Wed, 31 Jul 2019 04:53:58 +0000 (04:53 +0000)
This will simplify the implementation of API methods which
need to accept both plain json object and tizen.Bundle.
In example: function sendCommand(..., data) { var d = new tizen.Bundle(data); }
instead of manually having to check if data is a tizen.Bundle or object.

[Verification] Added new test case for this constructor, all existing tests passes.

Signed-off-by: Michal Michalski <m.michalski2@partner.samsung.com>
Change-Id: I4c880dbf8889c88bb20a4029b5cbb8fa2941eb1d

src/tizen/js/ut/bundle_ut.js
src/tizen/tizen_api.js

index f9c66d8..4c5f9f2 100644 (file)
@@ -7,6 +7,19 @@ function TestDefaultCtor() {
     assertEqual(0, counter, 'bundle should be empty');
 }
 
+function TestCopyCtor() {
+    var src = new tizen.Bundle({
+        key1: 'value',
+        key2: ['value1', 'value2'],
+    });
+
+    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");
+}
+
 function TestJsonCtor() {
     var json = {
         key1: 'value',
@@ -237,6 +250,7 @@ function TestToString() {
 var testcases = [
     TestDefaultCtor,
     TestJsonCtor,
+    TestCopyCtor,
     TestSetter,
     TestGetters,
     TestTypeOf,
@@ -244,3 +258,4 @@ var testcases = [
     TestToJson,
     TestToString
 ];
+
index 5dfb159..44b911b 100644 (file)
@@ -507,19 +507,29 @@ function getValueType(value) {
     }
 }
 
-exports.Bundle = function(json) {
+function Bundle(arg) {
     xwalk.utils.validator.isConstructorCall(this, exports.Bundle);
     this.data = {};
+    var json = {};
 
-    if (xwalk.utils.type.isObject(json)) {
-        forEachOwnProperty(
-            json,
-            function(key, value) {
-                this.set(key, value);
-            }.bind(this)
-        );
+    // copy constructor
+    if (arg instanceof tizen.Bundle) {
+      // perform deep copy of the other bundle.
+      json = JSON.parse(arg.toString());
+    }
+    // json to bundle conversion
+    else if (xwalk.utils.type.isObject(json)) {
+      json = arg;
     }
+
+    forEachOwnProperty(
+        json,
+        function(key, value) {
+            this.set(key, value);
+        }.bind(this)
+    );
 };
+exports.Bundle = Bundle;
 exports.Bundle.prototype.constructor = exports.Bundle;
 
 exports.Bundle.prototype.set = function(key, value) {