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 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)));
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);
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)));
80 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
86 class Mocks : public ::testing::NiceMock<AulMock> {};
88 class ComponentManagerTest : public TestFixture {
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;
101 virtual void TearDown() {
102 if (handle_ != nullptr) {
103 component_context_destroy(handle_);
108 component_context_h GetHandle() const {
112 void SetHandle(component_context_h handle) {
117 bool component_context_cb_touched_ = false;
118 bool component_info_cb_touched_ = false;
121 component_context_h handle_ = nullptr;
124 TEST_F(ComponentManagerTest,
125 component_manager_foreach_component_context_Positive) {
127 EXPECT_CALL(GetMock<AulMock>(),
128 aul_comp_context_foreach_comp_context(_, _)).
129 WillOnce(Invoke(__fake_aul_comp_context_foreach_comp_context));
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;
137 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
138 EXPECT_EQ(component_context_cb_touched_, true);
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);
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));
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);
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);
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);
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);
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));
189 EXPECT_CALL(GetMock<AulMock>(),
190 aul_comp_context_is_running(_, _)).
191 WillOnce(Invoke(__fake_aul_comp_context_is_running));
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);
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);
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);
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);
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);
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);