2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
26 #include "component_manager.h"
28 #include "test_fixture.h"
31 using ::testing::DoAll;
32 using ::testing::Return;
33 using ::testing::SetArgPointee;
34 using ::testing::StrEq;
35 using ::testing::Invoke;
37 struct aul_comp_context_s {
47 struct aul_comp_info_s {
51 class Mocks : public ::testing::NiceMock<AulMock> {};
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)));
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";
70 static int __fake_aul_comp_context_get_comp_id(aul_comp_context_h handle,
71 const char** comp_id) {
72 *comp_id = "TestComponent";
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";
82 static int __fake_aul_comp_context_get_status(aul_comp_context_h handle,
84 *status = COMP_STATUS_RESUMED;
88 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
94 static int __fake_aul_comp_context_is_sub_comp(aul_comp_context_h handle,
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)));
111 class ComponentContextTest : public TestFixture {
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_);
122 virtual void TearDown() {
123 if (handle_ != nullptr) {
124 component_context_destroy(handle_);
129 component_context_h GetHandle() const {
133 void SetHandle(component_context_h handle) {
138 component_context_h handle_ = nullptr;
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);
148 TEST_F(ComponentContextTest, component_context_destroy_Negative) {
149 int ret = component_context_destroy(nullptr);
150 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
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");
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);
170 TEST_F(ComponentContextTest, component_context_get_component_id_Positive) {
171 component_context_h handle = GetHandle();
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");
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);
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));
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");
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);
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));
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);
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);
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));
230 int ret = component_context_is_terminated(handle, &terminated);
231 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
232 EXPECT_EQ(terminated, false);
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);
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));
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);
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);
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));
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);
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);