f7680ef912ff7caf908912a0d5638ce971278d70
[platform/core/api/component-manager.git] / unit_tests / src / test_component_manager_extension.cc
1 /*
2  * Copyright (c) 2019 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 <aul.h>
19 #include <gtest/gtest.h>
20
21 #include <iostream>
22 #include <memory>
23
24 #include "include/component_manager_extension.h"
25
26 #include "mock/mock_aul_comp_context.h"
27
28 static int __fake_aul_comp_context_create(const char* comp_id,
29     aul_comp_context_h* handle) {
30   char* h = static_cast<char*>(malloc(sizeof(char)));
31   if (!h)
32     return AUL_R_ENOMEM;
33
34   *handle = static_cast<aul_comp_context_h>(h);
35   return AUL_R_OK;
36 }
37
38 static int __fake_aul_comp_context_destroy(aul_comp_context_h handle) {
39   if (!handle)
40     return AUL_R_EINVAL;
41
42   std::free(static_cast<void*>(handle));
43   return AUL_R_OK;
44 }
45
46 static int __fake_aul_comp_context_pause(aul_comp_context_h handle) {
47   return AUL_R_OK;
48 }
49
50 static int __fake_aul_comp_context_terminate(aul_comp_context_h handle) {
51   return AUL_R_OK;
52 }
53
54 class ComponentManagerExtensionTest : public testing::Test {
55  public:
56   virtual void SetUp() {
57     aul_comp_context_create_fake.custom_fake =
58       __fake_aul_comp_context_create;
59     aul_comp_context_destroy_fake.custom_fake =
60       __fake_aul_comp_context_destroy;
61     aul_comp_context_pause_fake.custom_fake =
62       __fake_aul_comp_context_pause;
63     aul_comp_context_terminate_fake.custom_fake =
64       __fake_aul_comp_context_terminate;
65
66     component_manager_get_component_context("TestComponent", &handle_);
67   }
68
69   virtual void TearDown() {
70     if (handle_ != nullptr) {
71       component_context_destroy(handle_);
72       handle_ = nullptr;
73     }
74   }
75
76   component_context_h GetHandle() const {
77     return handle_;
78   }
79
80   void SetHandle(component_context_h handle) {
81     handle_ = handle;
82   }
83
84  private:
85   component_context_h handle_ = nullptr;
86 };
87
88 TEST_F(ComponentManagerExtensionTest,
89     component_manager_pause_component_Positive) {
90   component_context_h handle = GetHandle();
91
92   int ret = component_manager_pause_component(handle);
93   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
94 }
95
96 TEST_F(ComponentManagerExtensionTest,
97     component_manager_pause_component_Negative) {
98   int ret = component_manager_pause_component(nullptr);
99   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
100 }
101
102 TEST_F(ComponentManagerExtensionTest,
103     component_manager_terminate_component_Positive) {
104   component_context_h handle = GetHandle();
105
106   int ret = component_manager_terminate_component(handle);
107   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
108 }
109
110 TEST_F(ComponentManagerExtensionTest,
111     component_manager_terminate_component_Negative) {
112   int ret = component_manager_terminate_component(nullptr);
113   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
114 }