Release version 1.1.2
[platform/core/api/component-manager.git] / test / unit_tests / test_component_manager.cc
1 /*
2  * Copyright (c) 2020 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 <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
22
23 #include <iostream>
24 #include <memory>
25
26 #include "component_manager.h"
27 #include "aul_mock.h"
28 #include "test_fixture.h"
29
30 using ::testing::_;
31 using ::testing::DoAll;
32 using ::testing::Return;
33 using ::testing::SetArgPointee;
34 using ::testing::StrEq;
35 using ::testing::Invoke;
36
37 struct aul_comp_context_s {
38   char *comp_id;
39   char *instance_id;
40   char *app_id;
41   char *type;
42   pid_t pid;
43   int status;
44   bool is_sub_comp;
45 };
46
47 struct aul_comp_info_s {
48   char *value[8];
49 };
50
51 static int __fake_aul_comp_info_create(const char* comp_id,
52     aul_comp_info_h* handle) {
53   aul_comp_info_h h = static_cast<aul_comp_info_h>(
54       calloc(1, sizeof(struct aul_comp_info_s)));
55   if (!h)
56     return AUL_R_ENOMEM;
57
58   *handle = h;
59   return AUL_R_OK;
60 }
61
62 static int __fake_aul_comp_context_foreach_comp_context(
63     aul_comp_context_cb callback, void* user_data) {
64   aul_comp_context_h handle = nullptr;
65   callback(handle, user_data);
66   return AUL_R_OK;
67 }
68
69 static int __fake_aul_comp_context_create(const char* comp_id,
70     aul_comp_context_h* handle) {
71   aul_comp_context_h h = static_cast<aul_comp_context_h>(
72       calloc(1, sizeof(struct aul_comp_context_s)));
73   if (!h)
74     return AUL_R_ENOMEM;
75
76   *handle = h;
77   return AUL_R_OK;
78 }
79
80 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
81     bool* running) {
82   *running = true;
83   return AUL_R_OK;
84 }
85
86 class Mocks : public ::testing::NiceMock<AulMock> {};
87
88 class ComponentManagerTest : public TestFixture {
89  public:
90   ComponentManagerTest() : TestFixture(std::make_unique<Mocks>()) {}
91   virtual ~ComponentManagerTest() {}
92   virtual void SetUp() {
93     EXPECT_CALL(GetMock<AulMock>(),
94       aul_comp_context_create(_, _)).
95           WillOnce(Invoke(__fake_aul_comp_context_create));
96     component_manager_get_component_context("TestComponent", &handle_);
97     component_context_cb_touched_ = false;
98     component_info_cb_touched_ = false;
99   }
100
101   virtual void TearDown() {
102     if (handle_ != nullptr) {
103       component_context_destroy(handle_);
104       handle_ = nullptr;
105     }
106   }
107
108   component_context_h GetHandle() const {
109     return handle_;
110   }
111
112   void SetHandle(component_context_h handle) {
113     handle_ = handle;
114   }
115
116  public:
117   bool component_context_cb_touched_ = false;
118   bool component_info_cb_touched_ = false;
119
120  private:
121   component_context_h handle_ = nullptr;
122 };
123
124 TEST_F(ComponentManagerTest,
125     component_manager_foreach_component_context_Positive) {
126
127   EXPECT_CALL(GetMock<AulMock>(),
128       aul_comp_context_foreach_comp_context(_, _)).
129           WillOnce(Invoke(__fake_aul_comp_context_foreach_comp_context));
130
131   int ret = component_manager_foreach_component_context(
132       [](component_context_h handle, void* user_data) -> bool {
133         ComponentManagerTest* p = static_cast<ComponentManagerTest*>(user_data);
134         p->component_context_cb_touched_ = true;
135         return true;
136       }, this);
137   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
138   EXPECT_EQ(component_context_cb_touched_, true);
139 }
140
141 TEST_F(ComponentManagerTest,
142     comopnent_manager_foreach_component_context_Negative) {
143   int ret = component_manager_foreach_component_context(nullptr, nullptr);
144   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
145 }
146
147 TEST_F(ComponentManagerTest, component_manager_get_component_context_Positive) {
148   component_context_h handle = nullptr;
149   EXPECT_CALL(GetMock<AulMock>(),
150       aul_comp_context_create(_, _)).
151           WillOnce(Invoke(__fake_aul_comp_context_create));
152
153   int ret = component_manager_get_component_context("TestComponent", &handle);
154   auto p = std::unique_ptr<std::remove_pointer<component_context_h>::type,
155        decltype(component_context_destroy)*>(handle, component_context_destroy);
156   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
157   EXPECT_NE(p, nullptr);
158   EXPECT_NE(p.get(), nullptr);
159 }
160
161 TEST_F(ComponentManagerTest, component_manager_get_component_context_Negative) {
162   int ret = component_manager_get_component_context(nullptr, nullptr);
163   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
164 }
165
166 TEST_F(ComponentManagerTest, component_manager_get_component_info_Positive) {
167   EXPECT_CALL(GetMock<AulMock>(),
168       aul_comp_info_create(_, _)).
169           WillOnce(Invoke(__fake_aul_comp_info_create));
170   component_info_h handle = nullptr;
171   int ret = component_manager_get_component_info("TestComponent", &handle);
172   auto p = std::unique_ptr<std::remove_pointer<component_info_h>::type,
173        decltype(component_info_destroy)*>(handle, component_info_destroy);
174   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
175   EXPECT_NE(p, nullptr);
176   EXPECT_NE(p.get(), nullptr);
177 }
178
179 TEST_F(ComponentManagerTest, component_manager_get_component_info_Negative) {
180   int ret = component_manager_get_component_info(nullptr, nullptr);
181   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
182 }
183
184 TEST_F(ComponentManagerTest, component_manager_is_running_Positive) {
185   EXPECT_CALL(GetMock<AulMock>(),
186       aul_comp_context_create(_, _)).
187           WillOnce(Invoke(__fake_aul_comp_context_create));
188
189   EXPECT_CALL(GetMock<AulMock>(),
190       aul_comp_context_is_running(_, _)).
191           WillOnce(Invoke(__fake_aul_comp_context_is_running));
192
193   bool running = false;
194   int ret = component_manager_is_running("TestComponent", &running);
195   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
196   EXPECT_EQ(running, true);
197 }
198
199 TEST_F(ComponentManagerTest, component_manager_is_running_Negative) {
200   int ret = component_manager_is_running(nullptr, nullptr);
201   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
202 }
203
204 TEST_F(ComponentManagerTest, component_manager_resume_component_Positive) {
205   EXPECT_CALL(GetMock<AulMock>(),
206       aul_comp_context_resume(_)).
207           WillOnce(Return(AUL_R_OK));
208   component_context_h handle = GetHandle();
209   int ret = component_manager_resume_component(handle);
210   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
211 }
212
213 TEST_F(ComponentManagerTest, component_manager_resume_component_Negative) {
214   int ret = component_manager_resume_component(nullptr);
215   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
216 }
217
218 TEST_F(ComponentManagerTest,
219     component_manager_terminate_bg_component_Positive) {
220   EXPECT_CALL(GetMock<AulMock>(),
221       aul_comp_context_terminate_bg_comp(_)).
222           WillOnce(Return(AUL_R_OK));
223   component_context_h handle = GetHandle();
224   int ret = component_manager_terminate_bg_component(handle);
225   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
226 }
227
228 TEST_F(ComponentManagerTest,
229     component_manager_terminate_bg_component_Negative) {
230   int ret = component_manager_terminate_bg_component(nullptr);
231   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
232 }