Using gmock for unit test
[platform/core/api/component-manager.git] / test / unit_tests / test_component_manager_extension.cc
1 /*
2  * Copyright (c) 2020 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 <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 #include <bundle_cpp.h>
21 #include <bundle_internal.h>
22
23 #include <iostream>
24 #include <memory>
25
26 #include "component_manager_extension.h"
27 #include "component_manager.h"
28 #include "aul_mock.h"
29 #include "test_fixture.h"
30
31 using ::testing::_;
32 using ::testing::DoAll;
33 using ::testing::Return;
34 using ::testing::SetArgPointee;
35 using ::testing::StrEq;
36 using ::testing::Invoke;
37
38 struct aul_comp_context_s {
39   char *comp_id;
40   char *instance_id;
41   char *app_id;
42   char *type;
43   pid_t pid;
44   int status;
45   bool is_sub_comp;
46 };
47
48 struct aul_comp_info_s {
49   char *value[8];
50 };
51
52 class Mocks : public ::testing::NiceMock<AulMock> {};
53
54 static int __fake_aul_comp_context_create(const char* comp_id,
55     aul_comp_context_h* handle) {
56   aul_comp_context_h h = static_cast<aul_comp_context_h>(
57       calloc(1, sizeof(struct aul_comp_context_s)));
58   if (!h)
59     return AUL_R_ENOMEM;
60
61   *handle = h;
62   return AUL_R_OK;
63 }
64
65 class ComponentManagerExtensionTest : public TestFixture {
66  public:
67   ComponentManagerExtensionTest() : TestFixture(std::make_unique<Mocks>()) {}
68   virtual ~ComponentManagerExtensionTest() {}
69   virtual void SetUp() {
70     EXPECT_CALL(GetMock<AulMock>(),
71       aul_comp_context_create(_, _)).
72           WillOnce(Invoke(__fake_aul_comp_context_create));
73     component_manager_get_component_context("TestComponent", &handle_);
74   }
75
76   virtual void TearDown() {
77     if (handle_ != nullptr) {
78       component_context_destroy(handle_);
79       handle_ = nullptr;
80     }
81   }
82
83   component_context_h GetHandle() const {
84     return handle_;
85   }
86
87   void SetHandle(component_context_h handle) {
88     handle_ = handle;
89   }
90
91  private:
92   component_context_h handle_ = nullptr;
93 };
94
95 TEST_F(ComponentManagerExtensionTest,
96     component_manager_pause_component_Positive) {
97   component_context_h handle = GetHandle();
98   EXPECT_CALL(GetMock<AulMock>(),
99       aul_comp_context_pause(_)).
100           WillOnce(Return(AUL_R_OK));
101   int ret = component_manager_pause_component(handle);
102   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
103 }
104
105 TEST_F(ComponentManagerExtensionTest,
106     component_manager_pause_component_Negative) {
107   int ret = component_manager_pause_component(nullptr);
108   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
109 }
110
111 TEST_F(ComponentManagerExtensionTest,
112     component_manager_terminate_component_Positive) {
113   component_context_h handle = GetHandle();
114   EXPECT_CALL(GetMock<AulMock>(),
115       aul_comp_context_terminate(_)).
116           WillOnce(Return(AUL_R_OK));
117   int ret = component_manager_terminate_component(handle);
118   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_NONE);
119 }
120
121 TEST_F(ComponentManagerExtensionTest,
122     component_manager_terminate_component_Negative) {
123   int ret = component_manager_terminate_component(nullptr);
124   EXPECT_EQ(ret, COMPONENT_MANAGER_ERROR_INVALID_PARAMETER);
125 }