Initial commit
[platform/core/api/component-manager.git] / unit_tests / src / test_component_context.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 <sys/types.h>
19 #include <unistd.h>
20 #include <aul.h>
21 #include <gtest/gtest.h>
22
23 #include <iostream>
24 #include <memory>
25
26 #include "mock_aul_comp_context.h"
27 #include "component_manager.h"
28
29 static int __fake_aul_comp_context_create(const char* comp_id,
30     aul_comp_context_h* handle) {
31   char* h = static_cast<char*>(malloc(sizeof(char)));
32   if (!h)
33     return AUL_R_ENOMEM;
34
35   *handle = static_cast<aul_comp_context_h>(h);
36
37   return AUL_R_OK;
38 }
39
40 static int __fake_aul_comp_context_destroy(aul_comp_context_h handle) {
41   if (!handle)
42     return AUL_R_EINVAL;
43
44   free(static_cast<void*>(handle));
45
46   return AUL_R_OK;
47 }
48
49 static int __fake_aul_comp_context_get_app_id(aul_comp_context_h handle,
50     const char** app_id) {
51   *app_id = "org.tizen.helloworld";
52   return AUL_R_OK;
53 }
54
55 static int __fake_aul_comp_context_get_instance_id(aul_comp_context_h handle,
56     const char** instance_id) {
57   *instance_id = "2019@TestComponent";
58   return AUL_R_OK;
59 }
60
61 static int __fake_aul_comp_context_get_comp_id(aul_comp_context_h handle,
62     const char** comp_id) {
63   *comp_id = "TestComponent";
64   return AUL_R_OK;
65 }
66
67 static int __fake_aul_comp_context_get_type(aul_comp_context_h handle,
68     const char** type) {
69   *type = "frame";
70   return AUL_R_OK;
71 }
72
73 static int __fake_aul_comp_context_get_pid(aul_comp_context_h handle,
74     pid_t* pid) {
75   *pid = getpid();
76   return AUL_R_OK;
77 }
78
79 static int __fake_aul_comp_context_get_status(aul_comp_context_h handle,
80     int* status) {
81   *status = STATUS_VISIBLE;
82   return AUL_R_OK;
83 }
84
85 static int __fake_aul_comp_context_is_sub_comp(aul_comp_context_h handle,
86     bool* is_sub_comp) {
87   *is_sub_comp = false;
88   return AUL_R_OK;
89 }
90
91 static int __fake_aul_comp_context_clone(aul_comp_context_h handle,
92     aul_comp_context_h *clone) {
93   char* h = static_cast<char*>(malloc(sizeof(char)));
94   if (!h)
95     return AUL_R_ENOMEM;
96
97   *clone = static_cast<aul_comp_context_h>(h);
98
99   return AUL_R_OK;
100 }
101
102 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
103     bool *running) {
104   *running = true;
105   return AUL_R_OK;
106 }
107
108 static int __fake_aul_comp_context_resume(aul_comp_context_h handle) {
109   return AUL_R_OK;
110 }
111
112 static int __fake_aul_comp_context_pause(aul_comp_context_h handle) {
113   return AUL_R_OK;
114 }
115
116 static int __fake_aul_comp_context_terminate_bg_comp(
117     aul_comp_context_h handle) {
118   return AUL_R_OK;
119 }
120
121 static int __fake_aul_comp_context_terminate(aul_comp_context_h handle) {
122   return AUL_R_OK;
123 }
124
125 class ComponentContextTest : public testing::Test {
126  public:
127   virtual void SetUp() {
128     aul_comp_context_create_fake.custom_fake =
129       __fake_aul_comp_context_create;
130     aul_comp_context_destroy_fake.custom_fake =
131       __fake_aul_comp_context_destroy;
132     aul_comp_context_get_app_id_fake.custom_fake =
133       __fake_aul_comp_context_get_app_id;
134     aul_comp_context_get_instance_id_fake.custom_fake =
135       __fake_aul_comp_context_get_instance_id;
136     aul_comp_context_get_comp_id_fake.custom_fake =
137       __fake_aul_comp_context_get_comp_id;
138     aul_comp_context_get_type_fake.custom_fake =
139       __fake_aul_comp_context_get_type;
140     aul_comp_context_get_pid_fake.custom_fake =
141       __fake_aul_comp_context_get_pid;
142     aul_comp_context_get_status_fake.custom_fake =
143       __fake_aul_comp_context_get_status;
144     aul_comp_context_is_sub_comp_fake.custom_fake =
145       __fake_aul_comp_context_is_sub_comp;
146     aul_comp_context_clone_fake.custom_fake =
147       __fake_aul_comp_context_clone;
148     aul_comp_context_is_running_fake.custom_fake =
149       __fake_aul_comp_context_is_running;
150     aul_comp_context_resume_fake.custom_fake =
151       __fake_aul_comp_context_resume;
152     aul_comp_context_pause_fake.custom_fake =
153       __fake_aul_comp_context_pause;
154     aul_comp_context_terminate_bg_comp_fake.custom_fake =
155       __fake_aul_comp_context_terminate_bg_comp;
156     aul_comp_context_terminate_fake.custom_fake =
157       __fake_aul_comp_context_terminate;
158
159     component_manager_get_component_context("TestComponent", &handle_);
160   }
161
162   virtual void TearDown() {
163     if (handle_ != nullptr) {
164       component_context_destroy(handle_);
165       handle_ = nullptr;
166     }
167   }
168
169   component_context_h GetHandle() const {
170     return handle_;
171   }
172
173   void SetHandle(component_context_h handle) {
174     handle_ = handle;
175   }
176
177  private:
178   component_context_h handle_ = nullptr;
179 };
180
181 TEST_F(ComponentContextTest, component_context_destroy_Positive) {
182   component_context_h handle = GetHandle();
183
184   int ret = component_context_destroy(handle);
185   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
186   SetHandle(nullptr);
187 }
188
189 TEST_F(ComponentContextTest, component_context_destroy_Negative) {
190   int ret = component_context_destroy(nullptr);
191   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
192 }
193
194 TEST_F(ComponentContextTest, component_context_get_app_id_Positive) {
195   component_context_h handle = GetHandle();
196   char* app_id = nullptr;
197
198   int ret = component_context_get_app_id(handle, &app_id);
199   auto p = std::unique_ptr<char, decltype(std::free)*>(app_id, std::free);
200   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
201   EXPECT_EQ(std::string(p.get()), "org.tizen.helloworld");
202 }
203
204 TEST_F(ComponentContextTest, component_context_get_app_id_Negative) {
205   int ret = component_context_get_app_id(nullptr, nullptr);
206   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
207 }
208
209 TEST_F(ComponentContextTest, component_context_get_comp_id_Positive) {
210   component_context_h handle = GetHandle();
211   char* comp_id = nullptr;
212
213   int ret = component_context_get_comp_id(handle, &comp_id);
214   auto p = std::unique_ptr<char, decltype(std::free)*>(comp_id, std::free);
215   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
216   EXPECT_EQ(std::string(p.get()), "TestComponent");
217 }
218
219 TEST_F(ComponentContextTest, component_context_get_comp_id_Negative) {
220   int ret = component_context_get_comp_id(nullptr, nullptr);
221   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
222 }
223
224 TEST_F(ComponentContextTest, component_context_get_instance_id_Positive) {
225   component_context_h handle = GetHandle();
226   char* instance_id = nullptr;
227
228   int ret = component_context_get_instance_id(handle, &instance_id);
229   auto p = std::unique_ptr<char, decltype(std::free)*>(instance_id, std::free);
230   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
231   EXPECT_EQ(std::string(p.get()), "2019@TestComponent");
232 }
233
234 TEST_F(ComponentContextTest, component_context_get_instance_id_Negative) {
235   int ret = component_context_get_instance_id(nullptr, nullptr);
236   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
237 }
238
239 TEST_F(ComponentContextTest, component_context_get_component_state_Positive) {
240   component_context_h handle = GetHandle();
241   component_state_e state = COMPONENT_STATE_UNDEFINED;
242
243   int ret = component_context_get_component_state(handle, &state);
244   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
245   EXPECT_EQ(state, COMPONENT_STATE_FOREGROUND);
246 }
247
248 TEST_F(ComponentContextTest, component_context_get_component_state_Negative) {
249   int ret = component_context_get_component_state(nullptr, nullptr);
250   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
251 }
252
253 TEST_F(ComponentContextTest, component_context_is_terminated_Positive) {
254   component_context_h handle = GetHandle();
255   bool terminated;
256
257   int ret = component_context_is_terminated(handle, &terminated);
258   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
259   EXPECT_EQ(terminated, false);
260 }
261
262 TEST_F(ComponentContextTest, component_context_is_terminated_Negative) {
263   int ret = component_context_is_terminated(nullptr, nullptr);
264   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
265 }
266
267 TEST_F(ComponentContextTest, component_context_is_sub_comp_Positive) {
268   component_context_h handle = GetHandle();
269   bool is_sub_comp;
270
271   int ret = component_context_is_sub_comp(handle, &is_sub_comp);
272   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
273   EXPECT_EQ(is_sub_comp, false);
274 }
275
276 TEST_F(ComponentContextTest, component_context_is_sub_comp_Negative) {
277   int ret = component_context_is_sub_comp(nullptr, nullptr);
278   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
279 }
280
281 TEST_F(ComponentContextTest, component_context_clone_Positive) {
282   component_context_h handle = GetHandle();
283   component_context_h clone = nullptr;
284
285   int ret = component_context_clone(&clone, handle);
286   auto p = std::unique_ptr<std::remove_pointer<component_context_h>::type,
287        decltype(component_context_destroy)*>(clone, component_context_destroy);
288   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
289   EXPECT_NE(p, nullptr);
290   EXPECT_NE(p.get(), nullptr);
291 }
292
293 TEST_F(ComponentContextTest, component_context_clone_Negative) {
294   int ret = component_context_clone(nullptr, nullptr);
295   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
296 }