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