Replace int with pid_t where applicable
[platform/core/uifw/multi-assistant-service.git] / tests / utc / client-manager / test_client_manager.cpp
1 /*
2  * Copyright 2020 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <gtest/gtest.h>
18
19 #include "client_manager.h"
20
21 #include <string>
22
23 class CDummyApplicationManager : public IApplicationManager
24 {
25 public:
26         CDummyApplicationManager() {};
27         virtual ~CDummyApplicationManager() {};
28
29         bool is_application_running(pid_t pid) { return true; }
30         bool is_application_running(const std::string& appid) { return true; }
31         bool bring_app_to_foreground(const std::string& appid) { return true; }
32         bool launch_app_async(const std::string& appid, bool background) { return true; }
33         boost::optional<std::string> get_appid_by_pid(pid_t pid) { return boost::optional<std::string>{}; }
34 };
35
36 class StorageWithNoClient : public testing::Test
37 {
38 public:
39         StorageWithNoClient() {
40         }
41         virtual ~StorageWithNoClient() {
42         }
43         void SetUp() override {
44                 client_manager.set_application_manager(&application_manager);
45         }
46         void TearDown() override {
47         }
48         CClientManager client_manager;
49         CDummyApplicationManager application_manager;
50 };
51
52 TEST_F(StorageWithNoClient, HasOneClientAfterCreate) {
53         const std::string arbitrary_client_appid{"Client1"};
54         const int arbitrary_client_pid{1};
55
56         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
57
58         int client_num = client_manager.get_client_num();
59
60         ASSERT_EQ(client_num, 1);
61 }
62
63 TEST_F(StorageWithNoClient, ValidityReturnsTrueForCreatedClient) {
64         const std::string arbitrary_client_appid{"Client1"};
65         const int arbitrary_client_pid{1};
66
67         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
68
69         bool validity = client_manager.check_client_validity_by_appid(arbitrary_client_appid);
70
71         ASSERT_TRUE(validity);
72 }
73
74 TEST_F(StorageWithNoClient, ValidityReturnsFalseForNotCreatedClient) {
75         const std::string arbitrary_client_appid{"Client1"};
76         const int arbitrary_client_pid{1};
77
78         const std::string noexist_client_appid{"Client987654321"};
79
80         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
81
82         bool validity = client_manager.check_client_validity_by_appid(noexist_client_appid);
83
84         ASSERT_FALSE(validity);
85 }
86
87 class StorageWithOneClient : public testing::Test {
88 public:
89         StorageWithOneClient() {
90         }
91         virtual ~StorageWithOneClient() {
92         }
93         void SetUp() override {
94                 client_manager.set_application_manager(&application_manager);
95                 client_manager.create_client(preloaded_client_pid_1, preloaded_client_appid_1);
96         }
97         void TearDown() override {
98         }
99         const std::string preloaded_client_appid_1{"Client1"};
100         const int preloaded_client_pid_1{1};
101         CClientManager client_manager;
102         CDummyApplicationManager application_manager;
103 };
104
105 TEST_F(StorageWithOneClient, ReturnsExistingPIDByIndex) {
106         const std::string arbitrary_client_appid{"Client2"};
107         const int arbitrary_client_pid{2};
108
109         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
110
111         int pid = client_manager.find_client_pid_by_index(0);
112
113         ASSERT_EQ(pid, preloaded_client_pid_1);
114 }
115
116 TEST_F(StorageWithOneClient, ReturnsCreatedPIDByIndex) {
117         const std::string arbitrary_client_appid{"Client2"};
118         const int arbitrary_client_pid{2};
119
120         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
121
122         int pid = client_manager.find_client_pid_by_index(1);
123
124         ASSERT_EQ(pid, arbitrary_client_pid);
125 }
126
127 TEST_F(StorageWithOneClient, PreservesExistingItemNotRequestedForRemoval) {
128         const std::string arbitrary_client_appid{"Client2"};
129         const int arbitrary_client_pid{2};
130
131         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
132         client_manager.destroy_client_by_pid(arbitrary_client_pid);
133
134         int pid = client_manager.find_client_pid_by_index(0);
135
136         ASSERT_EQ(pid, preloaded_client_pid_1);
137 }
138
139 TEST_F(StorageWithOneClient, PreservesCreatedItemNotRequestedForRemoval) {
140         const std::string arbitrary_client_appid{"Client2"};
141         const int arbitrary_client_pid{2};
142
143         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
144         client_manager.destroy_client_by_pid(preloaded_client_pid_1);
145
146         int pid = client_manager.find_client_pid_by_index(0);
147
148         ASSERT_EQ(pid, arbitrary_client_pid);
149 }
150
151 TEST_F(StorageWithOneClient, ReturnsCorrectExistingPIDByAppID) {
152         const std::string arbitrary_client_appid{"Client2"};
153         const int arbitrary_client_pid{2};
154
155         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
156
157         int pid = client_manager.find_client_pid_by_appid(preloaded_client_appid_1);
158
159         ASSERT_EQ(pid, preloaded_client_pid_1);
160 }
161
162 TEST_F(StorageWithOneClient, ReturnsCorrectCreatedPIDByAppID) {
163         const std::string arbitrary_client_appid{"Client2"};
164         const int arbitrary_client_pid{2};
165
166         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
167
168         int pid = client_manager.find_client_pid_by_appid(arbitrary_client_appid);
169
170         ASSERT_EQ(pid, arbitrary_client_pid);
171 }
172
173 TEST_F(StorageWithOneClient, ReturnsCorrectExistingAppIDByPID) {
174         const std::string arbitrary_client_appid{"Client2"};
175         const int arbitrary_client_pid{2};
176
177         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
178
179         std::string appid = client_manager.find_client_appid_by_pid(preloaded_client_pid_1);
180
181         ASSERT_EQ(appid, preloaded_client_appid_1);
182 }
183
184 TEST_F(StorageWithOneClient, ReturnsCorrectCreatedPIDAppIDByPID) {
185         const std::string arbitrary_client_appid{"Client2"};
186         const int arbitrary_client_pid{2};
187
188         client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
189
190         std::string appid = client_manager.find_client_appid_by_pid(arbitrary_client_pid);
191
192         ASSERT_EQ(appid, arbitrary_client_appid);
193 }
194
195 int main(int argc, char** argv) {
196         testing::InitGoogleTest(&argc, argv);
197         int ret = RUN_ALL_TESTS();
198         return ret;
199 }