Release version 0.6.0
[platform/core/base/bundle.git] / tests / bundle_unittests / src / test_bundle_cpp.cc
1 /*
2  * Copyright (c) 2019 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include <iostream>
20 #include <stdexcept>
21
22 #include "include/bundle_cpp.h"
23
24 using ::testing::AtLeast;
25 using namespace tizen_base;
26 using namespace std;
27
28 TEST(Bundle, CtorDtor) {
29   Bundle bundle;
30 }
31
32 TEST(Bundle, CopyCtor) {
33   Bundle bundle;
34   bundle.Add("TestKey", "TestVal");
35
36   Bundle b2(bundle);
37   EXPECT_EQ(b2.GetString("TestKey"), "TestVal");
38 }
39
40 TEST(Bundle, MoveCtor) {
41   Bundle bundle;
42   bundle.Add("TestKey", "TestVal");
43
44   Bundle b2(std::move(bundle));
45   EXPECT_EQ(b2.GetString("TestKey"), "TestVal");
46 }
47
48 TEST(Bundle, AddStringGetString) {
49   Bundle bundle;
50   bundle.Add("TestKey", "TestVal");
51
52   EXPECT_EQ(bundle.GetString("TestKey"), "TestVal");
53 }
54
55 TEST(Bundle, AddByteGetByte) {
56   Bundle bundle;
57   std::vector<unsigned char> v = { 0, 1, 2, 3};
58   bundle.Add("TestKey", v);
59   auto v2 = bundle.GetByte("TestKey");
60
61   EXPECT_EQ(v2.size(), 4);
62   EXPECT_EQ(v2[0], 0);
63   EXPECT_EQ(v2[1], 1);
64   EXPECT_EQ(v2[2], 2);
65   EXPECT_EQ(v2[3], 3);
66 }
67
68 TEST(Bundle, AddStringArrayGetStringArray) {
69   Bundle bundle;
70   bundle.Add("TestKey", { "TestVal1", "TestVal2", "TestVal3" });
71
72   auto v = bundle.GetStringArray("TestKey");
73
74   EXPECT_EQ(v.size(), 3);
75   EXPECT_EQ(v[0], "TestVal1");
76   EXPECT_EQ(v[1], "TestVal2");
77   EXPECT_EQ(v[2], "TestVal3");
78 }
79
80 TEST(Bundle, ToRaw) {
81   Bundle bundle;
82   bundle.Add("TestKey", "TestVal");
83
84   auto base64_r = bundle.ToRaw();
85   Bundle b2(std::move(base64_r));
86   EXPECT_EQ(bundle.GetString("TestKey"), "TestVal");
87
88   auto r = bundle.ToRaw(false);
89   Bundle b3(std::move(r), false);
90   EXPECT_EQ(bundle.GetString("TestKey"), "TestVal");
91 }
92
93 TEST(Bundle, GetCount) {
94   Bundle bundle;
95   bundle.Add("TestKey1", "TestVal1");
96   bundle.Add("TestKey2", "TestVal2");
97
98   EXPECT_EQ(bundle.GetCount(), 2);
99 }
100
101 TEST(Bundle, Delete) {
102   Bundle bundle;
103   int r = bundle.Add("TestKey1", "TestVal1");
104   EXPECT_EQ(r, 0);
105
106   r = bundle.Delete("TestKey1");
107   EXPECT_EQ(r, 0);
108
109   EXPECT_EQ(bundle.GetString("TestKey1"), "");
110 }
111
112 TEST(Bundle, GetKeys) {
113   Bundle bundle;
114   bundle.Add("TestKey1", "TestVal1");
115   bundle.Add("TestKey2", "TestVal2");
116   bundle.Add("TestKey3", "TestVal3");
117
118   auto v = bundle.GetKeys();
119
120   EXPECT_EQ(bundle.GetCount(), 3);
121
122   for (auto& i : v) {
123     EXPECT_EQ(i.GetType(), BUNDLE_TYPE_STR);
124   }
125 }
126
127 TEST(Bundle, GetKeysCopy) {
128   Bundle bundle;
129   bundle.Add("TestKey1", "TestVal1");
130   bundle.Add("TestKey2", "TestVal2");
131   bundle.Add("TestKey3", "TestVal3");
132
133   auto v = bundle.GetKeys();
134
135   EXPECT_EQ(bundle.GetCount(), 3);
136
137   for (auto& i : v) {
138     Bundle::KeyInfo copied = i;
139     EXPECT_EQ(copied.GetType(), BUNDLE_TYPE_STR);
140     EXPECT_EQ(copied.GetName(), i.GetName());
141   }
142 }
143
144 TEST(Bundle, GetKeysMove) {
145   Bundle bundle;
146   bundle.Add("TestKey1", "TestVal1");
147   bundle.Add("TestKey2", "TestVal2");
148   bundle.Add("TestKey3", "TestVal3");
149
150   auto v = bundle.GetKeys();
151
152   EXPECT_EQ(bundle.GetCount(), 3);
153
154   for (auto& i : v) {
155     string name = i.GetName();
156     Bundle::KeyInfo copied = move(i);
157     EXPECT_EQ(copied.GetType(), BUNDLE_TYPE_STR);
158     EXPECT_EQ(copied.GetName(), name);
159   }
160 }
161
162 TEST(Bundle, IsEmpty) {
163   Bundle bundle;
164   EXPECT_TRUE(bundle.IsEmpty());
165   bundle.Add("TestKey1", "TestVal");
166   EXPECT_FALSE(bundle.IsEmpty());
167 }
168
169 TEST(Bundle, Export) {
170   Bundle bundle;
171   bundle.Add("TestKey1", "TestVal1");
172   std::vector<std::string> argv = bundle.Export();
173   EXPECT_NE(argv.size(), 0);
174 }