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