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 "mock_aul_comp_context.h"
27 #include "component_manager.h"
29 static int __fake_aul_comp_context_create(const char* comp_id,
30 aul_comp_context_h* handle) {
31 char* h = static_cast<char*>(malloc(sizeof(char)));
35 *handle = static_cast<aul_comp_context_h>(h);
40 static int __fake_aul_comp_context_destroy(aul_comp_context_h handle) {
44 free(static_cast<void*>(handle));
49 static int __fake_aul_comp_context_get_app_id(aul_comp_context_h handle,
50 const char** app_id) {
51 *app_id = "org.tizen.helloworld";
55 static int __fake_aul_comp_context_get_instance_id(aul_comp_context_h handle,
56 const char** instance_id) {
57 *instance_id = "2019@TestComponent";
61 static int __fake_aul_comp_context_get_comp_id(aul_comp_context_h handle,
62 const char** comp_id) {
63 *comp_id = "TestComponent";
67 static int __fake_aul_comp_context_get_type(aul_comp_context_h handle,
73 static int __fake_aul_comp_context_get_pid(aul_comp_context_h handle,
79 static int __fake_aul_comp_context_get_status(aul_comp_context_h handle,
81 *status = STATUS_VISIBLE;
85 static int __fake_aul_comp_context_is_sub_comp(aul_comp_context_h handle,
91 static int __fake_aul_comp_context_clone(aul_comp_context_h handle,
92 aul_comp_context_h *clone) {
93 char* h = static_cast<char*>(malloc(sizeof(char)));
97 *clone = static_cast<aul_comp_context_h>(h);
102 static int __fake_aul_comp_context_is_running(aul_comp_context_h handle,
108 static int __fake_aul_comp_context_resume(aul_comp_context_h handle) {
112 static int __fake_aul_comp_context_pause(aul_comp_context_h handle) {
116 static int __fake_aul_comp_context_terminate_bg_comp(
117 aul_comp_context_h handle) {
121 static int __fake_aul_comp_context_terminate(aul_comp_context_h handle) {
125 class ComponentContextTest : public testing::Test {
127 virtual void SetUp() {
128 aul_comp_context_create_fake.custom_fake =
129 __fake_aul_comp_context_create;
130 aul_comp_context_destroy_fake.custom_fake =
131 __fake_aul_comp_context_destroy;
132 aul_comp_context_get_app_id_fake.custom_fake =
133 __fake_aul_comp_context_get_app_id;
134 aul_comp_context_get_instance_id_fake.custom_fake =
135 __fake_aul_comp_context_get_instance_id;
136 aul_comp_context_get_comp_id_fake.custom_fake =
137 __fake_aul_comp_context_get_comp_id;
138 aul_comp_context_get_type_fake.custom_fake =
139 __fake_aul_comp_context_get_type;
140 aul_comp_context_get_pid_fake.custom_fake =
141 __fake_aul_comp_context_get_pid;
142 aul_comp_context_get_status_fake.custom_fake =
143 __fake_aul_comp_context_get_status;
144 aul_comp_context_is_sub_comp_fake.custom_fake =
145 __fake_aul_comp_context_is_sub_comp;
146 aul_comp_context_clone_fake.custom_fake =
147 __fake_aul_comp_context_clone;
148 aul_comp_context_is_running_fake.custom_fake =
149 __fake_aul_comp_context_is_running;
150 aul_comp_context_resume_fake.custom_fake =
151 __fake_aul_comp_context_resume;
152 aul_comp_context_pause_fake.custom_fake =
153 __fake_aul_comp_context_pause;
154 aul_comp_context_terminate_bg_comp_fake.custom_fake =
155 __fake_aul_comp_context_terminate_bg_comp;
156 aul_comp_context_terminate_fake.custom_fake =
157 __fake_aul_comp_context_terminate;
159 component_manager_get_component_context("TestComponent", &handle_);
162 virtual void TearDown() {
163 if (handle_ != nullptr) {
164 component_context_destroy(handle_);
169 component_context_h GetHandle() const {
173 void SetHandle(component_context_h handle) {
178 component_context_h handle_ = nullptr;
181 TEST_F(ComponentContextTest, component_context_destroy_Positive) {
182 component_context_h handle = GetHandle();
184 int ret = component_context_destroy(handle);
185 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
189 TEST_F(ComponentContextTest, component_context_destroy_Negative) {
190 int ret = component_context_destroy(nullptr);
191 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
194 TEST_F(ComponentContextTest, component_context_get_app_id_Positive) {
195 component_context_h handle = GetHandle();
196 char* app_id = nullptr;
198 int ret = component_context_get_app_id(handle, &app_id);
199 auto p = std::unique_ptr<char, decltype(std::free)*>(app_id, std::free);
200 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
201 EXPECT_EQ(std::string(p.get()), "org.tizen.helloworld");
204 TEST_F(ComponentContextTest, component_context_get_app_id_Negative) {
205 int ret = component_context_get_app_id(nullptr, nullptr);
206 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
209 TEST_F(ComponentContextTest, component_context_get_comp_id_Positive) {
210 component_context_h handle = GetHandle();
211 char* comp_id = nullptr;
213 int ret = component_context_get_comp_id(handle, &comp_id);
214 auto p = std::unique_ptr<char, decltype(std::free)*>(comp_id, std::free);
215 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
216 EXPECT_EQ(std::string(p.get()), "TestComponent");
219 TEST_F(ComponentContextTest, component_context_get_comp_id_Negative) {
220 int ret = component_context_get_comp_id(nullptr, nullptr);
221 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
224 TEST_F(ComponentContextTest, component_context_get_instance_id_Positive) {
225 component_context_h handle = GetHandle();
226 char* instance_id = nullptr;
228 int ret = component_context_get_instance_id(handle, &instance_id);
229 auto p = std::unique_ptr<char, decltype(std::free)*>(instance_id, std::free);
230 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
231 EXPECT_EQ(std::string(p.get()), "2019@TestComponent");
234 TEST_F(ComponentContextTest, component_context_get_instance_id_Negative) {
235 int ret = component_context_get_instance_id(nullptr, nullptr);
236 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
239 TEST_F(ComponentContextTest, component_context_get_component_state_Positive) {
240 component_context_h handle = GetHandle();
241 component_state_e state = COMPONENT_STATE_UNDEFINED;
243 int ret = component_context_get_component_state(handle, &state);
244 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
245 EXPECT_EQ(state, COMPONENT_STATE_FOREGROUND);
248 TEST_F(ComponentContextTest, component_context_get_component_state_Negative) {
249 int ret = component_context_get_component_state(nullptr, nullptr);
250 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
253 TEST_F(ComponentContextTest, component_context_is_terminated_Positive) {
254 component_context_h handle = GetHandle();
257 int ret = component_context_is_terminated(handle, &terminated);
258 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
259 EXPECT_EQ(terminated, false);
262 TEST_F(ComponentContextTest, component_context_is_terminated_Negative) {
263 int ret = component_context_is_terminated(nullptr, nullptr);
264 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
267 TEST_F(ComponentContextTest, component_context_is_sub_comp_Positive) {
268 component_context_h handle = GetHandle();
271 int ret = component_context_is_sub_comp(handle, &is_sub_comp);
272 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
273 EXPECT_EQ(is_sub_comp, false);
276 TEST_F(ComponentContextTest, component_context_is_sub_comp_Negative) {
277 int ret = component_context_is_sub_comp(nullptr, nullptr);
278 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
281 TEST_F(ComponentContextTest, component_context_clone_Positive) {
282 component_context_h handle = GetHandle();
283 component_context_h clone = nullptr;
285 int ret = component_context_clone(&clone, handle);
286 auto p = std::unique_ptr<std::remove_pointer<component_context_h>::type,
287 decltype(component_context_destroy)*>(clone, component_context_destroy);
288 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
289 EXPECT_NE(p, nullptr);
290 EXPECT_NE(p.get(), nullptr);
293 TEST_F(ComponentContextTest, component_context_clone_Negative) {
294 int ret = component_context_clone(nullptr, nullptr);
295 EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);