Using gmock for unit test
[platform/core/api/component-manager.git] / test / unit_tests / test_component_context.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 class Mocks : public ::testing::NiceMock<AulMock> {};
52
53 static int __fake_aul_comp_context_create(const char* comp_id,
54     aul_comp_context_h* handle) {
55   aul_comp_context_h h = static_cast<aul_comp_context_h>(
56       calloc(1, sizeof(struct aul_comp_context_s)));
57   if (!h)
58     return AUL_R_ENOMEM;
59
60   *handle = h;
61   return AUL_R_OK;
62 }
63
64 static int __fake_aul_comp_context_get_app_id(aul_comp_context_h handle,
65     const char** app_id) {
66   *app_id = "org.tizen.helloworld";
67   return AUL_R_OK;
68 }
69
70 static int __fake_aul_comp_context_get_comp_id(aul_comp_context_h handle,
71     const char** comp_id) {
72   *comp_id = "TestComponent";
73   return AUL_R_OK;
74 }
75
76 static int __fake_aul_comp_context_get_instance_id(aul_comp_context_h handle,
77     const char** instance_id) {
78   *instance_id = "2019@TestComponent";
79   return AUL_R_OK;
80 }
81
82 static int __fake_aul_comp_context_get_status(aul_comp_context_h handle,
83     int* status) {
84   *status = COMP_STATUS_RESUMED;
85   return AUL_R_OK;
86 }
87
88 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
89     bool *running) {
90   *running = true;
91   return AUL_R_OK;
92 }
93
94 static int __fake_aul_comp_context_is_sub_comp(aul_comp_context_h handle,
95     bool* is_sub_comp) {
96   *is_sub_comp = false;
97   return AUL_R_OK;
98 }
99
100 static int __fake_aul_comp_context_clone(aul_comp_context_h handle,
101     aul_comp_context_h *clone) {
102   aul_comp_context_h h = static_cast<aul_comp_context_h>(
103       calloc(1, sizeof(struct aul_comp_context_s)));
104   if (!h)
105     return AUL_R_ENOMEM;
106
107   *clone = h;
108   return AUL_R_OK;
109 }
110
111 class ComponentContextTest : public TestFixture {
112  public:
113   ComponentContextTest() : TestFixture(std::make_unique<Mocks>()) {}
114   virtual ~ComponentContextTest() {}
115   virtual void SetUp() {
116     EXPECT_CALL(GetMock<AulMock>(),
117       aul_comp_context_create(_, _)).
118           WillOnce(Invoke(__fake_aul_comp_context_create));
119     component_manager_get_component_context("TestComponent", &handle_);
120   }
121
122   virtual void TearDown() {
123     if (handle_ != nullptr) {
124       component_context_destroy(handle_);
125       handle_ = nullptr;
126     }
127   }
128
129   component_context_h GetHandle() const {
130     return handle_;
131   }
132
133   void SetHandle(component_context_h handle) {
134     handle_ = handle;
135   }
136
137  private:
138   component_context_h handle_ = nullptr;
139 };
140
141 TEST_F(ComponentContextTest, component_context_destroy_Positive) {
142   component_context_h handle = GetHandle();
143   int ret = component_context_destroy(handle);
144   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
145   SetHandle(nullptr);
146 }
147
148 TEST_F(ComponentContextTest, component_context_destroy_Negative) {
149   int ret = component_context_destroy(nullptr);
150   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
151 }
152
153 TEST_F(ComponentContextTest, component_context_get_app_id_Positive) {
154   EXPECT_CALL(GetMock<AulMock>(),
155       aul_comp_context_get_app_id(_, _)).
156           WillOnce(Invoke(__fake_aul_comp_context_get_app_id));
157   component_context_h handle = GetHandle();
158   char* app_id = nullptr;
159   int ret = component_context_get_app_id(handle, &app_id);
160   auto p = std::unique_ptr<char, decltype(std::free)*>(app_id, std::free);
161   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
162   EXPECT_EQ(std::string(p.get()), "org.tizen.helloworld");
163 }
164
165 TEST_F(ComponentContextTest, component_context_get_app_id_Negative) {
166   int ret = component_context_get_app_id(nullptr, nullptr);
167   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
168 }
169
170 TEST_F(ComponentContextTest, component_context_get_component_id_Positive) {
171   component_context_h handle = GetHandle();
172
173   EXPECT_CALL(GetMock<AulMock>(),
174       aul_comp_context_get_comp_id(_, _)).
175           WillOnce(Invoke(__fake_aul_comp_context_get_comp_id));
176   char* component_id = nullptr;
177   int ret = component_context_get_component_id(handle, &component_id);
178   auto p = std::unique_ptr<char, decltype(std::free)*>(component_id, std::free);
179   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
180   EXPECT_EQ(std::string(p.get()), "TestComponent");
181 }
182
183 TEST_F(ComponentContextTest, component_context_get_component_id_Negative) {
184   int ret = component_context_get_component_id(nullptr, nullptr);
185   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
186 }
187
188 TEST_F(ComponentContextTest, component_context_get_instance_id_Positive) {
189   component_context_h handle = GetHandle();
190   EXPECT_CALL(GetMock<AulMock>(),
191       aul_comp_context_get_instance_id(_, _)).
192           WillOnce(Invoke(__fake_aul_comp_context_get_instance_id));
193
194   char* instance_id = nullptr;
195   int ret = component_context_get_instance_id(handle, &instance_id);
196   auto p = std::unique_ptr<char, decltype(std::free)*>(instance_id, std::free);
197   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
198   EXPECT_EQ(std::string(p.get()), "2019@TestComponent");
199 }
200
201 TEST_F(ComponentContextTest, component_context_get_instance_id_Negative) {
202   int ret = component_context_get_instance_id(nullptr, nullptr);
203   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
204 }
205
206 TEST_F(ComponentContextTest, component_context_get_component_state_Positive) {
207   component_context_h handle = GetHandle();
208   EXPECT_CALL(GetMock<AulMock>(),
209       aul_comp_context_get_status(_, _)).
210           WillOnce(Invoke(__fake_aul_comp_context_get_status));
211
212   component_state_e state = COMPONENT_STATE_INITIALIZED;
213   int ret = component_context_get_component_state(handle, &state);
214   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
215   EXPECT_EQ(state, COMPONENT_STATE_RESUMED);
216 }
217
218 TEST_F(ComponentContextTest, component_context_get_component_state_Negative) {
219   int ret = component_context_get_component_state(nullptr, nullptr);
220   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
221 }
222
223 TEST_F(ComponentContextTest, component_context_is_terminated_Positive) {
224   component_context_h handle = GetHandle();
225   EXPECT_CALL(GetMock<AulMock>(),
226       aul_comp_context_is_running(_, _)).
227           WillOnce(Invoke(__fake_aul_comp_context_is_running));
228
229   bool terminated;
230   int ret = component_context_is_terminated(handle, &terminated);
231   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
232   EXPECT_EQ(terminated, false);
233 }
234
235 TEST_F(ComponentContextTest, component_context_is_terminated_Negative) {
236   int ret = component_context_is_terminated(nullptr, nullptr);
237   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
238 }
239
240 TEST_F(ComponentContextTest, component_context_is_subcomponent_Positive) {
241   component_context_h handle = GetHandle();
242   EXPECT_CALL(GetMock<AulMock>(),
243       aul_comp_context_is_sub_comp(_, _)).
244           WillOnce(Invoke(__fake_aul_comp_context_is_sub_comp));
245
246   bool is_subcomponent;
247   int ret = component_context_is_subcomponent(handle, &is_subcomponent);
248   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
249   EXPECT_EQ(is_subcomponent, false);
250 }
251
252 TEST_F(ComponentContextTest, component_context_is_subcomponent_Negative) {
253   int ret = component_context_is_subcomponent(nullptr, nullptr);
254   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
255 }
256
257 TEST_F(ComponentContextTest, component_context_clone_Positive) {
258   component_context_h handle = GetHandle();
259   component_context_h clone = nullptr;
260   EXPECT_CALL(GetMock<AulMock>(),
261       aul_comp_context_clone(_, _)).
262           WillOnce(Invoke(__fake_aul_comp_context_clone));
263
264   int ret = component_context_clone(&clone, handle);
265   auto p = std::unique_ptr<std::remove_pointer<component_context_h>::type,
266        decltype(component_context_destroy)*>(clone, component_context_destroy);
267   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
268   EXPECT_NE(p, nullptr);
269   EXPECT_NE(p.get(), nullptr);
270 }
271
272 TEST_F(ComponentContextTest, component_context_clone_Negative) {
273   int ret = component_context_clone(nullptr, nullptr);
274   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
275 }