Initial commit
[platform/core/api/component-manager.git] / unit_tests / src / test_component_info.cc
1 /*
2  * Copyright (c) 2019 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 <aul.h>
19 #include <gtest/gtest.h>
20
21 #include <iostream>
22 #include <memory>
23
24 #include "mock_aul_comp_info.h"
25 #include "component_manager.h"
26
27 static const char ICON_PATH[] =
28   "/opt/usr/home/owner/apps_rw/org.tizen.helloworld/shared/res/helloworld.png";
29
30 static int __fake_aul_comp_info_create(const char* compid,
31     aul_comp_info_h* handle) {
32   char* h = static_cast<char*>(malloc(sizeof(char)));
33   if (!h)
34     return AUL_R_ENOMEM;
35
36   *handle = static_cast<aul_comp_info_h>(h);
37
38   return AUL_R_OK;
39 }
40
41 static int __fake_aul_comp_info_destroy(
42     aul_comp_info_h handle) {
43   if (!handle)
44     return AUL_R_EINVAL;
45
46   free(static_cast<void*>(handle));
47
48   return AUL_R_OK;
49 }
50
51 static int __fake_aul_comp_info_get_app_id(aul_comp_info_h handle,
52     const char** appid) {
53   *appid = "org.tizen.helloworld";
54   return AUL_R_OK;
55 }
56
57 static int __fake_aul_comp_info_get_comp_id(aul_comp_info_h handle,
58     const char** compid) {
59   *compid = "TestComponent";
60   return AUL_R_OK;
61 }
62
63 static int __fake_aul_comp_info_get_type(aul_comp_info_h handle,
64     const char** type) {
65   *type = "frame";
66   return AUL_R_OK;
67 }
68
69 static int __fake_aul_comp_info_is_icon_display(aul_comp_info_h handle,
70     bool* nodisplay) {
71   *nodisplay = true;
72   return AUL_R_OK;
73 }
74
75 static int __fake_aul_comp_info_is_taskmanage(
76     aul_comp_info_h handle, bool* taskmanage) {
77   *taskmanage = true;
78   return AUL_R_OK;
79 }
80
81 static int __fake_aul_comp_info_get_icon(aul_comp_info_h handle,
82     const char** icon) {
83   *icon = ICON_PATH;
84   return AUL_R_OK;
85 }
86
87 static int __fake_aul_comp_info_get_label(aul_comp_info_h handle,
88     const char** label) {
89   *label = "TestComponent";
90   return AUL_R_OK;
91 }
92
93 static int __fake_aul_comp_info_get_localed_label(const char* compid,
94     const char* locale, char** label) {
95   *label = strdup("Kor_TestComponent");
96   return AUL_R_OK;
97 }
98
99 static int __fake_aul_comp_info_clone(
100     aul_comp_info_h handle, aul_comp_info_h* clone) {
101   char* h = static_cast<char *>(malloc(sizeof(char)));
102   if (!h)
103     return AUL_R_ERROR;
104
105   *clone = static_cast<aul_comp_info_h>(h);
106
107   return AUL_R_OK;
108 }
109
110 class ComponentInfoTest : public testing::Test {
111  public:
112   virtual void SetUp() {
113     aul_comp_info_create_fake.custom_fake =
114         __fake_aul_comp_info_create;
115     aul_comp_info_destroy_fake.custom_fake =
116         __fake_aul_comp_info_destroy;
117     aul_comp_info_get_app_id_fake.custom_fake =
118         __fake_aul_comp_info_get_app_id;
119     aul_comp_info_get_comp_id_fake.custom_fake =
120         __fake_aul_comp_info_get_comp_id;
121     aul_comp_info_get_type_fake.custom_fake =
122         __fake_aul_comp_info_get_type;
123     aul_comp_info_is_icon_display_fake.custom_fake =
124         __fake_aul_comp_info_is_icon_display;
125     aul_comp_info_is_taskmanage_fake.custom_fake =
126         __fake_aul_comp_info_is_taskmanage;
127     aul_comp_info_get_icon_fake.custom_fake =
128         __fake_aul_comp_info_get_icon;
129     aul_comp_info_get_label_fake.custom_fake =
130         __fake_aul_comp_info_get_label;
131     aul_comp_info_get_localed_label_fake.custom_fake =
132         __fake_aul_comp_info_get_localed_label;
133     aul_comp_info_clone_fake.custom_fake =
134         __fake_aul_comp_info_clone;
135
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   int ret = component_info_create("TestComponent", &handle);
161   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
162
163   // Post: Destroys the handle
164   component_info_destroy(handle);
165 }
166
167 TEST_F(ComponentInfoTest, component_info_create_Negative) {
168   int ret = component_info_create(nullptr, nullptr);
169   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
170 }
171
172 TEST_F(ComponentInfoTest, component_info_destroy_Positive) {
173   component_info_h handle = GetHandle();
174
175   int ret = component_info_destroy(handle);
176   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
177   SetHandle(nullptr);
178 }
179
180 TEST_F(ComponentInfoTest, component_info_destroy_Negative) {
181   int ret = component_info_destroy(nullptr);
182   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
183 }
184
185 TEST_F(ComponentInfoTest, component_info_get_app_id_Positive) {
186   component_info_h handle = GetHandle();
187
188   char* value = nullptr;
189   int ret = component_info_get_app_id(handle, &value);
190   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
191   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
192   EXPECT_EQ(std::string(p.get()), "org.tizen.helloworld");
193 }
194
195 TEST_F(ComponentInfoTest, component_info_get_app_id_Negative) {
196   int ret = component_info_get_app_id(nullptr, nullptr);
197   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
198 }
199
200 TEST_F(ComponentInfoTest, component_info_get_comp_id_Positive) {
201   component_info_h handle = GetHandle();
202
203   char* value = nullptr;
204   int ret = component_info_get_comp_id(handle, &value);
205   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
206   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
207   EXPECT_EQ(std::string(p.get()), "TestComponent");
208 }
209
210 TEST_F(ComponentInfoTest, component_info_get_comp_id_Negative) {
211   int ret = component_info_get_comp_id(nullptr, nullptr);
212   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
213 }
214
215 TEST_F(ComponentInfoTest, component_info_get_component_type_Positive) {
216   component_info_h handle = GetHandle();
217
218   component_info_component_type_e type;
219   int ret = component_info_get_component_type(handle, &type);
220   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
221   EXPECT_EQ(type, COMPONENT_INFO_COMPONENT_TYPE_FRAME);
222 }
223
224 TEST_F(ComponentInfoTest, component_info_get_component_type_Negative) {
225   int ret = component_info_get_component_type(nullptr, nullptr);
226   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
227 }
228
229 TEST_F(ComponentInfoTest, component_info_is_icon_display_Positive) {
230   component_info_h handle = GetHandle();
231
232   bool icon_display = false;
233   int ret = component_info_is_icon_display(handle, &icon_display);
234   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
235   EXPECT_EQ(icon_display, true);
236 }
237
238 TEST_F(ComponentInfoTest, component_info_is_icon_display_Negative) {
239   int ret = component_info_is_icon_display(nullptr, nullptr);
240   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
241 }
242
243 TEST_F(ComponentInfoTest, component_info_is_taskmanage_Positive) {
244   component_info_h handle = GetHandle();
245
246   bool taskmanage = false;
247   int ret = component_info_is_taskmanage(handle, &taskmanage);
248   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
249   EXPECT_EQ(taskmanage, true);
250 }
251
252 TEST_F(ComponentInfoTest, component_info_is_taskmanage_Negative) {
253   int ret = component_info_is_taskmanage(nullptr, nullptr);
254   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
255 }
256
257 TEST_F(ComponentInfoTest, component_info_get_icon_Positive) {
258   component_info_h handle = GetHandle();
259
260   char* value = nullptr;
261   int ret = component_info_get_icon(handle, &value);
262   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
263   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
264   EXPECT_EQ(std::string(p.get()), ICON_PATH);
265 }
266
267 TEST_F(ComponentInfoTest, component_info_get_icon_Negative) {
268   int ret = component_info_get_icon(nullptr, nullptr);
269   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
270 }
271
272 TEST_F(ComponentInfoTest, component_info_get_label_Positive) {
273   component_info_h handle = GetHandle();
274
275   char* value = nullptr;
276   int ret = component_info_get_label(handle, &value);
277   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
278   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
279   EXPECT_EQ(std::string(p.get()), "TestComponent");
280 }
281
282 TEST_F(ComponentInfoTest, component_info_get_label_Nagative) {
283   int ret = component_info_get_label(nullptr, nullptr);
284   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
285 }
286
287 TEST_F(ComponentInfoTest, component_info_get_localed_label_Positive) {
288   component_info_h handle = GetHandle();
289
290   char* value = nullptr;
291   int ret = component_info_get_localed_label(handle, "ko-kr", &value);
292   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
293   auto p = std::unique_ptr<char, decltype(std::free)*>(value, std::free);
294   EXPECT_EQ(std::string(p.get()), "Kor_TestComponent");
295 }
296
297 TEST_F(ComponentInfoTest, component_info_get_localed_label_Negative) {
298   int ret = component_info_get_localed_label(nullptr, nullptr, nullptr);
299   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
300 }
301
302 TEST_F(ComponentInfoTest, component_info_clone_Positive) {
303   component_info_h handle = GetHandle();
304
305   component_info_h clone = nullptr;
306   int ret = component_info_clone(&clone, handle);
307   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
308   component_info_destroy(clone);
309 }
310
311 TEST_F(ComponentInfoTest, component_info_clone_Negative) {
312   int ret = component_info_clone(nullptr, nullptr);
313   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
314 }