Using gmock for unit test
[platform/core/api/component-manager.git] / test / unit_tests / test_component_info.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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 <stdlib.h>
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
22
23 #include <iostream>
24 #include <memory>
25
26 #include "component_manager.h"
27 #include "aul_mock.h"
28 #include "test_fixture.h"
29
30 using ::testing::_;
31 using ::testing::DoAll;
32 using ::testing::Return;
33 using ::testing::SetArgPointee;
34 using ::testing::StrEq;
35 using ::testing::Invoke;
36
37 struct aul_comp_context_s {
38   char *comp_id;
39   char *instance_id;
40   char *app_id;
41   char *type;
42   pid_t pid;
43   int status;
44   bool is_sub_comp;
45 };
46
47 struct aul_comp_info_s {
48   char *value[8];
49 };
50
51 class Mocks : public ::testing::NiceMock<AulMock> {};
52
53
54 static const char ICON_PATH[] =
55   "/opt/usr/home/owner/apps_rw/org.tizen.helloworld/shared/res/helloworld.png";
56
57 static int __fake_aul_comp_info_create(const char* comp_id,
58     aul_comp_info_h* handle) {
59   aul_comp_info_h h = static_cast<aul_comp_info_h>(
60       calloc(1, sizeof(struct aul_comp_info_s)));
61   if (!h)
62     return AUL_R_ENOMEM;
63
64   *handle = h;
65   return AUL_R_OK;
66 }
67
68 static int __fake_aul_comp_info_get_app_id(aul_comp_info_h handle,
69     const char** appid) {
70   *appid = "org.tizen.helloworld";
71   return AUL_R_OK;
72 }
73
74 static int __fake_aul_comp_info_get_comp_id(aul_comp_info_h handle,
75     const char** compid) {
76   *compid = "TestComponent";
77   return AUL_R_OK;
78 }
79
80 static int __fake_aul_comp_info_get_type(aul_comp_info_h handle,
81     const char** type) {
82   *type = "frame";
83   return AUL_R_OK;
84 }
85
86 static int __fake_aul_comp_info_is_icon_display(aul_comp_info_h handle,
87     bool* nodisplay) {
88   *nodisplay = true;
89   return AUL_R_OK;
90 }
91
92 static int __fake_aul_comp_info_is_taskmanage(
93     aul_comp_info_h handle, bool* taskmanage) {
94   *taskmanage = true;
95   return AUL_R_OK;
96 }
97
98 static int __fake_aul_comp_info_get_icon(aul_comp_info_h handle,
99     const char** icon) {
100   *icon = ICON_PATH;
101   return AUL_R_OK;
102 }
103
104 static int __fake_aul_comp_info_get_label(aul_comp_info_h handle,
105     const char** label) {
106   *label = "TestComponent";
107   return AUL_R_OK;
108 }
109
110 static int __fake_aul_comp_info_get_localed_label(aul_comp_info_h handle,
111     const char* locale, const char** label) {
112   *label = strdup("Kor_TestComponent");
113   return AUL_R_OK;
114 }
115
116 static int __fake_aul_comp_info_clone(
117     aul_comp_info_h handle, aul_comp_info_h* clone) {
118   aul_comp_info_h h = static_cast<aul_comp_info_h>(
119       calloc(1, sizeof(struct aul_comp_info_s)));
120   if (!h)
121     return AUL_R_ENOMEM;
122
123   *clone = h;
124
125   return AUL_R_OK;
126 }
127
128 class ComponentInfoTest : public TestFixture {
129  public:
130   ComponentInfoTest() : TestFixture(std::make_unique<Mocks>()) {}
131   virtual ~ComponentInfoTest() {}
132   virtual void SetUp() {
133     EXPECT_CALL(GetMock<AulMock>(),
134       aul_comp_info_create(_, _)).
135           WillOnce(Invoke(__fake_aul_comp_info_create));
136     component_info_create("TestComponent", &handle_);
137   }
138
139   virtual void TearDown() {
140     if (handle_ != nullptr) {
141       component_info_destroy(handle_);
142       handle_ = nullptr;
143     }
144   }
145
146   component_info_h GetHandle() const {
147     return handle_;
148   }
149
150   void SetHandle(component_info_h handle) {
151     handle_ = handle;
152   }
153
154  private:
155   component_info_h handle_ = nullptr;
156 };
157
158 TEST_F(ComponentInfoTest, component_info_create_Positive) {
159   component_info_h handle = nullptr;
160
161   EXPECT_CALL(GetMock<AulMock>(),
162       aul_comp_info_create(_, _)).
163           WillOnce(Invoke(__fake_aul_comp_info_create));
164   int ret = component_info_create("TestComponent", &handle);
165   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
166
167   // Post: Destroys the handle
168   component_info_destroy(handle);
169 }
170
171 TEST_F(ComponentInfoTest, component_info_create_Negative) {
172   int ret = component_info_create(nullptr, nullptr);
173   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
174 }
175
176 TEST_F(ComponentInfoTest, component_info_destroy_Positive) {
177   component_info_h handle = GetHandle();
178
179   int ret = component_info_destroy(handle);
180   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
181   SetHandle(nullptr);
182 }
183
184 TEST_F(ComponentInfoTest, component_info_destroy_Negative) {
185   int ret = component_info_destroy(nullptr);
186   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
187 }
188
189 TEST_F(ComponentInfoTest, component_info_get_app_id_Positive) {
190   component_info_h handle = GetHandle();
191
192   EXPECT_CALL(GetMock<AulMock>(),
193       aul_comp_info_get_app_id(_, _)).
194           WillOnce(Invoke(__fake_aul_comp_info_get_app_id));
195   char* value = nullptr;
196   int ret = component_info_get_app_id(handle, &value);
197   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
198   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
199   EXPECT_EQ(std::string(p.get()), "org.tizen.helloworld");
200 }
201
202 TEST_F(ComponentInfoTest, component_info_get_app_id_Negative) {
203   int ret = component_info_get_app_id(nullptr, nullptr);
204   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
205 }
206
207 TEST_F(ComponentInfoTest, component_info_get_component_id_Positive) {
208   component_info_h handle = GetHandle();
209
210   EXPECT_CALL(GetMock<AulMock>(),
211       aul_comp_info_get_comp_id(_, _)).
212           WillOnce(Invoke(__fake_aul_comp_info_get_comp_id));
213   char* value = nullptr;
214   int ret = component_info_get_component_id(handle, &value);
215   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
216   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
217   EXPECT_EQ(std::string(p.get()), "TestComponent");
218 }
219
220 TEST_F(ComponentInfoTest, component_info_get_component_id_Negative) {
221   int ret = component_info_get_component_id(nullptr, nullptr);
222   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
223 }
224
225 TEST_F(ComponentInfoTest, component_info_get_component_type_Positive) {
226   component_info_h handle = GetHandle();
227
228   EXPECT_CALL(GetMock<AulMock>(),
229       aul_comp_info_get_type(_, _)).
230           WillOnce(Invoke(__fake_aul_comp_info_get_type));
231   component_info_component_type_e type;
232   int ret = component_info_get_component_type(handle, &type);
233   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
234   EXPECT_EQ(type, COMPONENT_INFO_COMPONENT_TYPE_FRAME);
235 }
236
237 TEST_F(ComponentInfoTest, component_info_get_component_type_Negative) {
238   int ret = component_info_get_component_type(nullptr, nullptr);
239   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
240 }
241
242 TEST_F(ComponentInfoTest, component_info_is_icon_display_Positive) {
243   component_info_h handle = GetHandle();
244
245   EXPECT_CALL(GetMock<AulMock>(),
246       aul_comp_info_is_icon_display(_, _)).
247           WillOnce(Invoke(__fake_aul_comp_info_is_icon_display));
248   bool icon_display = false;
249   int ret = component_info_is_icon_display(handle, &icon_display);
250   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
251   EXPECT_EQ(icon_display, true);
252 }
253
254 TEST_F(ComponentInfoTest, component_info_is_icon_display_Negative) {
255   int ret = component_info_is_icon_display(nullptr, nullptr);
256   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
257 }
258
259 TEST_F(ComponentInfoTest, component_info_is_managed_by_task_manager_Positive) {
260   component_info_h handle = GetHandle();
261
262   EXPECT_CALL(GetMock<AulMock>(),
263       aul_comp_info_is_taskmanage(_, _)).
264           WillOnce(Invoke(__fake_aul_comp_info_is_taskmanage));
265   bool managed = false;
266   int ret = component_info_is_managed_by_task_manager(handle, &managed);
267   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
268   EXPECT_EQ(managed, true);
269 }
270
271 TEST_F(ComponentInfoTest, component_info_is_managed_by_task_manager_Negative) {
272   int ret = component_info_is_managed_by_task_manager(nullptr, nullptr);
273   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
274 }
275
276 TEST_F(ComponentInfoTest, component_info_get_icon_Positive) {
277   component_info_h handle = GetHandle();
278
279   EXPECT_CALL(GetMock<AulMock>(),
280       aul_comp_info_get_icon(_, _)).
281           WillOnce(Invoke(__fake_aul_comp_info_get_icon));
282   char* value = nullptr;
283   int ret = component_info_get_icon(handle, &value);
284   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
285   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
286   EXPECT_EQ(std::string(p.get()), ICON_PATH);
287 }
288
289 TEST_F(ComponentInfoTest, component_info_get_icon_Negative) {
290   int ret = component_info_get_icon(nullptr, nullptr);
291   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
292 }
293
294 TEST_F(ComponentInfoTest, component_info_get_label_Positive) {
295   component_info_h handle = GetHandle();
296
297   EXPECT_CALL(GetMock<AulMock>(),
298       aul_comp_info_get_label(_, _)).
299           WillOnce(Invoke(__fake_aul_comp_info_get_label));
300   char* value = nullptr;
301   int ret = component_info_get_label(handle, &value);
302   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
303   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
304   EXPECT_EQ(std::string(p.get()), "TestComponent");
305 }
306
307 TEST_F(ComponentInfoTest, component_info_get_label_Nagative) {
308   int ret = component_info_get_label(nullptr, nullptr);
309   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
310 }
311
312 TEST_F(ComponentInfoTest, component_info_get_localized_label_Positive) {
313   component_info_h handle = GetHandle();
314
315   EXPECT_CALL(GetMock<AulMock>(),
316       aul_comp_info_get_localed_label(_, _, _)).
317           WillOnce(Invoke(__fake_aul_comp_info_get_localed_label));
318   char* value = nullptr;
319   int ret = component_info_get_localized_label(handle, "ko-kr", &value);
320   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
321   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
322   EXPECT_EQ(std::string(p.get()), "Kor_TestComponent");
323 }
324
325 TEST_F(ComponentInfoTest, component_info_get_localized_label_Negative) {
326   int ret = component_info_get_localized_label(nullptr, nullptr, nullptr);
327   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
328 }
329
330 TEST_F(ComponentInfoTest, component_info_clone_Positive) {
331   component_info_h handle = GetHandle();
332
333   EXPECT_CALL(GetMock<AulMock>(),
334       aul_comp_info_clone(_, _)).
335           WillOnce(Invoke(__fake_aul_comp_info_clone));
336   component_info_h clone = nullptr;
337   int ret = component_info_clone(&clone, handle);
338   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
339   component_info_destroy(clone);
340 }
341
342 TEST_F(ComponentInfoTest, component_info_clone_Negative) {
343   int ret = component_info_clone(nullptr, nullptr);
344   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
345 }