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