2 * Copyright (c) 2019 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 <sys/types.h>
21 #include <gtest/gtest.h>
26 #include "include/component_manager.h"
28 #include "mock/mock_aul_comp_context.h"
29 #include "mock/mock_aul_comp_types.h"
31 static int __fake_aul_comp_context_create(const char* comp_id,
32 aul_comp_context_h* handle) {
33 char* h = static_cast<char*>(malloc(sizeof(char)));
37 *handle = static_cast<aul_comp_context_h>(h);
42 static int __fake_aul_comp_context_destroy(aul_comp_context_h handle) {
46 free(static_cast<void*>(handle));
51 static int __fake_aul_comp_context_get_app_id(aul_comp_context_h handle,
52 const char** app_id) {
53 *app_id = "org.tizen.helloworld";
57 static int __fake_aul_comp_context_get_instance_id(aul_comp_context_h handle,
58 const char** instance_id) {
59 *instance_id = "2019@TestComponent";
63 static int __fake_aul_comp_context_get_comp_id(aul_comp_context_h handle,
64 const char** comp_id) {
65 *comp_id = "TestComponent";
69 static int __fake_aul_comp_context_get_type(aul_comp_context_h handle,
75 static int __fake_aul_comp_context_get_pid(aul_comp_context_h handle,
81 static int __fake_aul_comp_context_get_status(aul_comp_context_h handle,
83 *status = COMP_STATUS_RESUMED;
87 static int __fake_aul_comp_context_is_sub_comp(aul_comp_context_h handle,
93 static int __fake_aul_comp_context_clone(aul_comp_context_h handle,
94 aul_comp_context_h *clone) {
95 char* h = static_cast<char*>(malloc(sizeof(char)));
99 *clone = static_cast<aul_comp_context_h>(h);
104 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
110 static int __fake_aul_comp_context_resume(aul_comp_context_h handle) {
114 static int __fake_aul_comp_context_pause(aul_comp_context_h handle) {
118 static int __fake_aul_comp_context_terminate_bg_comp(
119 aul_comp_context_h handle) {
123 static int __fake_aul_comp_context_terminate(aul_comp_context_h handle) {
127 class ComponentContextTest : public testing::Test {
129 virtual void SetUp() {
130 aul_comp_context_create_fake.custom_fake =
131 __fake_aul_comp_context_create;
132 aul_comp_context_destroy_fake.custom_fake =
133 __fake_aul_comp_context_destroy;
134 aul_comp_context_get_app_id_fake.custom_fake =
135 __fake_aul_comp_context_get_app_id;
136 aul_comp_context_get_instance_id_fake.custom_fake =
137 __fake_aul_comp_context_get_instance_id;
138 aul_comp_context_get_comp_id_fake.custom_fake =
139 __fake_aul_comp_context_get_comp_id;
140 aul_comp_context_get_type_fake.custom_fake =
141 __fake_aul_comp_context_get_type;
142 aul_comp_context_get_pid_fake.custom_fake =
143 __fake_aul_comp_context_get_pid;
144 aul_comp_context_get_status_fake.custom_fake =
145 __fake_aul_comp_context_get_status;
146 aul_comp_context_is_sub_comp_fake.custom_fake =
147 __fake_aul_comp_context_is_sub_comp;
148 aul_comp_context_clone_fake.custom_fake =
149 __fake_aul_comp_context_clone;
150 aul_comp_context_is_running_fake.custom_fake =
151 __fake_aul_comp_context_is_running;
152 aul_comp_context_resume_fake.custom_fake =
153 __fake_aul_comp_context_resume;
154 aul_comp_context_pause_fake.custom_fake =
155 __fake_aul_comp_context_pause;
156 aul_comp_context_terminate_bg_comp_fake.custom_fake =
157 __fake_aul_comp_context_terminate_bg_comp;
158 aul_comp_context_terminate_fake.custom_fake =
159 __fake_aul_comp_context_terminate;
161 component_manager_get_component_context("TestComponent", &handle_);
164 virtual void TearDown() {
165 if (handle_ != nullptr) {
166 component_context_destroy(handle_);
171 component_context_h GetHandle() const {
175 void SetHandle(component_context_h handle) {
180 component_context_h handle_ = nullptr;
183 TEST_F(ComponentContextTest, component_context_destroy_Positive) {
184 component_context_h handle = GetHandle();
186 int ret = component_context_destroy(handle);
187 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
191 TEST_F(ComponentContextTest, component_context_destroy_Negative) {
192 int ret = component_context_destroy(nullptr);
193 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
196 TEST_F(ComponentContextTest, component_context_get_app_id_Positive) {
197 component_context_h handle = GetHandle();
198 char* app_id = nullptr;
200 int ret = component_context_get_app_id(handle, &app_id);
201 auto p = std::unique_ptr<char, decltype(std::free)*>(app_id, std::free);
202 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
203 EXPECT_EQ(std::string(p.get()), "org.tizen.helloworld");
206 TEST_F(ComponentContextTest, component_context_get_app_id_Negative) {
207 int ret = component_context_get_app_id(nullptr, nullptr);
208 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
211 TEST_F(ComponentContextTest, component_context_get_component_id_Positive) {
212 component_context_h handle = GetHandle();
213 char* component_id = nullptr;
215 int ret = component_context_get_component_id(handle, &component_id);
216 auto p = std::unique_ptr<char, decltype(std::free)*>(component_id, std::free);
217 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
218 EXPECT_EQ(std::string(p.get()), "TestComponent");
221 TEST_F(ComponentContextTest, component_context_get_component_id_Negative) {
222 int ret = component_context_get_component_id(nullptr, nullptr);
223 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
226 TEST_F(ComponentContextTest, component_context_get_instance_id_Positive) {
227 component_context_h handle = GetHandle();
228 char* instance_id = nullptr;
230 int ret = component_context_get_instance_id(handle, &instance_id);
231 auto p = std::unique_ptr<char, decltype(std::free)*>(instance_id, std::free);
232 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
233 EXPECT_EQ(std::string(p.get()), "2019@TestComponent");
236 TEST_F(ComponentContextTest, component_context_get_instance_id_Negative) {
237 int ret = component_context_get_instance_id(nullptr, nullptr);
238 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
241 TEST_F(ComponentContextTest, component_context_get_component_state_Positive) {
242 component_context_h handle = GetHandle();
243 component_state_e state = COMPONENT_STATE_INITIALIZED;
245 int ret = component_context_get_component_state(handle, &state);
246 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
247 EXPECT_EQ(state, COMPONENT_STATE_RESUMED);
250 TEST_F(ComponentContextTest, component_context_get_component_state_Negative) {
251 int ret = component_context_get_component_state(nullptr, nullptr);
252 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
255 TEST_F(ComponentContextTest, component_context_is_terminated_Positive) {
256 component_context_h handle = GetHandle();
259 int ret = component_context_is_terminated(handle, &terminated);
260 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
261 EXPECT_EQ(terminated, false);
264 TEST_F(ComponentContextTest, component_context_is_terminated_Negative) {
265 int ret = component_context_is_terminated(nullptr, nullptr);
266 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
269 TEST_F(ComponentContextTest, component_context_is_subcomponent_Positive) {
270 component_context_h handle = GetHandle();
271 bool is_subcomponent;
273 int ret = component_context_is_subcomponent(handle, &is_subcomponent);
274 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
275 EXPECT_EQ(is_subcomponent, false);
278 TEST_F(ComponentContextTest, component_context_is_subcomponent_Negative) {
279 int ret = component_context_is_subcomponent(nullptr, nullptr);
280 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
283 TEST_F(ComponentContextTest, component_context_clone_Positive) {
284 component_context_h handle = GetHandle();
285 component_context_h clone = nullptr;
287 int ret = component_context_clone(&clone, handle);
288 auto p = std::unique_ptr<std::remove_pointer<component_context_h>::type,
289 decltype(component_context_destroy)*>(clone, component_context_destroy);
290 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
291 EXPECT_NE(p, nullptr);
292 EXPECT_NE(p.get(), nullptr);
295 TEST_F(ComponentContextTest, component_context_clone_Negative) {
296 int ret = component_context_clone(nullptr, nullptr);
297 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);