Release version 1.1.2
[platform/core/api/component-manager.git] / unit_tests / src / test_component_manager.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_context.h"
27 #include "mock/mock_aul_comp_info.h"
28
29 static int __fake_aul_comp_info_foreach_comp_info(aul_comp_info_cb callback,
30     void* user_data) {
31   aul_comp_info_h handle = nullptr;
32   callback(handle, user_data);
33   return AUL_R_OK;
34 }
35
36 static int __fake_aul_comp_info_create(const char* comp_id,
37     aul_comp_info_h* handle) {
38   char* h = static_cast<char*>(malloc(sizeof(char)));
39   if (!h)
40     return AUL_R_ENOMEM;
41
42   *handle = static_cast<aul_comp_info_h>(h);
43   return AUL_R_OK;
44 }
45
46 static int __fake_aul_comp_info_destroy(aul_comp_info_h handle) {
47   if (!handle)
48     return AUL_R_EINVAL;
49
50   std::free(static_cast<void*>(handle));
51   return AUL_R_OK;
52 }
53
54 static int __fake_aul_comp_context_foreach_comp_context(
55     aul_comp_context_cb callback, void* user_data) {
56   aul_comp_context_h handle = nullptr;
57   callback(handle, user_data);
58   return AUL_R_OK;
59 }
60
61 static int __fake_aul_comp_context_create(const char* comp_id,
62     aul_comp_context_h* handle) {
63   char* h = static_cast<char*>(malloc(sizeof(char)));
64   if (!h)
65     return AUL_R_ENOMEM;
66
67   *handle = static_cast<aul_comp_context_h>(h);
68   return AUL_R_OK;
69 }
70
71 static int __fake_aul_comp_context_destroy(aul_comp_context_h handle) {
72   if (!handle)
73     return AUL_R_EINVAL;
74
75   std::free(static_cast<void*>(handle));
76   return AUL_R_OK;
77 }
78
79 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
80     bool* running) {
81   *running = true;
82   return AUL_R_OK;
83 }
84
85 static int __fake_aul_comp_context_resume(aul_comp_context_h handle) {
86   return AUL_R_OK;
87 }
88
89 static int __fake_aul_comp_context_terminate_bg_comp(
90     aul_comp_context_h handle) {
91   return AUL_R_OK;
92 }
93
94 class ComponentManagerTest : public testing::Test {
95  public:
96   virtual void SetUp() {
97     aul_comp_info_foreach_comp_info_fake.custom_fake =
98       __fake_aul_comp_info_foreach_comp_info;
99     aul_comp_info_create_fake.custom_fake =
100       __fake_aul_comp_info_create;
101     aul_comp_info_destroy_fake.custom_fake =
102       __fake_aul_comp_info_destroy;
103     aul_comp_context_foreach_comp_context_fake.custom_fake =
104       __fake_aul_comp_context_foreach_comp_context;
105     aul_comp_context_create_fake.custom_fake =
106       __fake_aul_comp_context_create;
107     aul_comp_context_destroy_fake.custom_fake =
108       __fake_aul_comp_context_destroy;
109     aul_comp_context_is_running_fake.custom_fake =
110       __fake_aul_comp_context_is_running;
111     aul_comp_context_resume_fake.custom_fake =
112       __fake_aul_comp_context_resume;
113     aul_comp_context_terminate_bg_comp_fake.custom_fake =
114       __fake_aul_comp_context_terminate_bg_comp;
115
116     component_manager_get_component_context("TestComponent", &handle_);
117     component_context_cb_touched_ = false;
118     component_info_cb_touched_ = false;
119   }
120
121   virtual void TearDown() {
122     if (handle_ != nullptr) {
123       component_context_destroy(handle_);
124       handle_ = nullptr;
125     }
126   }
127
128   component_context_h GetHandle() const {
129     return handle_;
130   }
131
132   void SetHandle(component_context_h handle) {
133     handle_ = handle;
134   }
135
136  public:
137   bool component_context_cb_touched_ = false;
138   bool component_info_cb_touched_ = false;
139
140  private:
141   component_context_h handle_ = nullptr;
142 };
143
144 TEST_F(ComponentManagerTest,
145     component_manager_foreach_component_context_Positive) {
146   int ret = component_manager_foreach_component_context(
147       [](component_context_h handle, void* user_data) -> bool {
148         ComponentManagerTest* p = static_cast<ComponentManagerTest*>(user_data);
149         p->component_context_cb_touched_ = true;
150         return true;
151       }, this);
152   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
153   EXPECT_EQ(component_context_cb_touched_, true);
154 }
155
156 TEST_F(ComponentManagerTest,
157     comopnent_manager_foreach_component_context_Negative) {
158   int ret = component_manager_foreach_component_context(nullptr, nullptr);
159   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
160 }
161
162 TEST_F(ComponentManagerTest,
163     component_manager_foreach_component_info_Positive) {
164   int ret = component_manager_foreach_component_info(
165       [](component_info_h handle, void* user_data) -> bool {
166         ComponentManagerTest* p = static_cast<ComponentManagerTest*>(user_data);
167         p->component_info_cb_touched_ = true;
168         return true;
169       }, this);
170   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
171   EXPECT_EQ(component_info_cb_touched_, true);
172 }
173
174 TEST_F(ComponentManagerTest,
175     component_manager_foreach_component_info_Negative) {
176   int ret = component_manager_foreach_component_info(nullptr, nullptr);
177   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
178 }
179
180 TEST_F(ComponentManagerTest, component_manager_get_component_context_Positive) {
181   component_context_h handle = nullptr;
182   int ret = component_manager_get_component_context("TestComponent", &handle);
183   auto p = std::unique_ptr<std::remove_pointer<component_context_h>::type,
184        decltype(component_context_destroy)*>(handle, component_context_destroy);
185   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
186   EXPECT_NE(p, nullptr);
187   EXPECT_NE(p.get(), nullptr);
188 }
189
190 TEST_F(ComponentManagerTest, component_manager_get_component_context_Negative) {
191   int ret = component_manager_get_component_context(nullptr, nullptr);
192   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
193 }
194
195 TEST_F(ComponentManagerTest, component_manager_get_component_info_Positive) {
196   component_info_h handle = nullptr;
197   int ret = component_manager_get_component_info("TestComponent", &handle);
198   auto p = std::unique_ptr<std::remove_pointer<component_info_h>::type,
199        decltype(component_info_destroy)*>(handle, component_info_destroy);
200   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
201   EXPECT_NE(p, nullptr);
202   EXPECT_NE(p.get(), nullptr);
203 }
204
205 TEST_F(ComponentManagerTest, component_manager_get_component_info_Negative) {
206   int ret = component_manager_get_component_info(nullptr, nullptr);
207   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
208 }
209
210 TEST_F(ComponentManagerTest, component_manager_is_running_Positive) {
211   bool running = false;
212   int ret = component_manager_is_running("TestComponent", &running);
213   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
214   EXPECT_EQ(running, true);
215 }
216
217 TEST_F(ComponentManagerTest, component_manager_is_running_Negative) {
218   int ret = component_manager_is_running(nullptr, nullptr);
219   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
220 }
221
222 TEST_F(ComponentManagerTest, component_manager_resume_component_Positive) {
223   component_context_h handle = GetHandle();
224
225   int ret = component_manager_resume_component(handle);
226   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
227 }
228
229 TEST_F(ComponentManagerTest, component_manager_resume_component_Negative) {
230   int ret = component_manager_resume_component(nullptr);
231   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
232 }
233
234 TEST_F(ComponentManagerTest,
235     component_manager_terminate_bg_component_Positive) {
236   component_context_h handle = GetHandle();
237
238   int ret = component_manager_terminate_bg_component(handle);
239   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
240 }
241
242 TEST_F(ComponentManagerTest,
243     component_manager_terminate_bg_component_Negative) {
244   int ret = component_manager_terminate_bg_component(nullptr);
245   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
246 }