2 * Copyright 2020 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
17 #include <gtest/gtest.h>
19 #include "client_manager.h"
23 class CDummyApplicationManager : public IApplicationManager
26 CDummyApplicationManager() {};
27 virtual ~CDummyApplicationManager() {};
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>{}; }
36 class StorageWithNoClient : public testing::Test
39 StorageWithNoClient() {
41 virtual ~StorageWithNoClient() {
43 void SetUp() override {
44 client_manager.set_application_manager(&application_manager);
46 void TearDown() override {
48 CClientManager client_manager;
49 CDummyApplicationManager application_manager;
52 TEST_F(StorageWithNoClient, HasOneClientAfterCreate) {
53 const std::string arbitrary_client_appid{"Client1"};
54 const int arbitrary_client_pid{1};
56 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
58 int client_num = client_manager.get_client_num();
60 ASSERT_EQ(client_num, 1);
63 TEST_F(StorageWithNoClient, ValidityReturnsTrueForCreatedClient) {
64 const std::string arbitrary_client_appid{"Client1"};
65 const int arbitrary_client_pid{1};
67 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
69 bool validity = client_manager.check_client_validity_by_appid(arbitrary_client_appid);
71 ASSERT_TRUE(validity);
74 TEST_F(StorageWithNoClient, ValidityReturnsFalseForNotCreatedClient) {
75 const std::string arbitrary_client_appid{"Client1"};
76 const int arbitrary_client_pid{1};
78 const std::string noexist_client_appid{"Client987654321"};
80 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
82 bool validity = client_manager.check_client_validity_by_appid(noexist_client_appid);
84 ASSERT_FALSE(validity);
87 class StorageWithOneClient : public testing::Test {
89 StorageWithOneClient() {
91 virtual ~StorageWithOneClient() {
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);
97 void TearDown() override {
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;
105 TEST_F(StorageWithOneClient, ReturnsExistingPIDByIndex) {
106 const std::string arbitrary_client_appid{"Client2"};
107 const int arbitrary_client_pid{2};
109 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
111 int pid = client_manager.find_client_pid_by_index(0);
113 ASSERT_EQ(pid, preloaded_client_pid_1);
116 TEST_F(StorageWithOneClient, ReturnsCreatedPIDByIndex) {
117 const std::string arbitrary_client_appid{"Client2"};
118 const int arbitrary_client_pid{2};
120 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
122 int pid = client_manager.find_client_pid_by_index(1);
124 ASSERT_EQ(pid, arbitrary_client_pid);
127 TEST_F(StorageWithOneClient, PreservesExistingItemNotRequestedForRemoval) {
128 const std::string arbitrary_client_appid{"Client2"};
129 const int arbitrary_client_pid{2};
131 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
132 client_manager.destroy_client_by_pid(arbitrary_client_pid);
134 int pid = client_manager.find_client_pid_by_index(0);
136 ASSERT_EQ(pid, preloaded_client_pid_1);
139 TEST_F(StorageWithOneClient, PreservesCreatedItemNotRequestedForRemoval) {
140 const std::string arbitrary_client_appid{"Client2"};
141 const int arbitrary_client_pid{2};
143 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
144 client_manager.destroy_client_by_pid(preloaded_client_pid_1);
146 int pid = client_manager.find_client_pid_by_index(0);
148 ASSERT_EQ(pid, arbitrary_client_pid);
151 TEST_F(StorageWithOneClient, ReturnsCorrectExistingPIDByAppID) {
152 const std::string arbitrary_client_appid{"Client2"};
153 const int arbitrary_client_pid{2};
155 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
157 int pid = client_manager.find_client_pid_by_appid(preloaded_client_appid_1);
159 ASSERT_EQ(pid, preloaded_client_pid_1);
162 TEST_F(StorageWithOneClient, ReturnsCorrectCreatedPIDByAppID) {
163 const std::string arbitrary_client_appid{"Client2"};
164 const int arbitrary_client_pid{2};
166 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
168 int pid = client_manager.find_client_pid_by_appid(arbitrary_client_appid);
170 ASSERT_EQ(pid, arbitrary_client_pid);
173 TEST_F(StorageWithOneClient, ReturnsCorrectExistingAppIDByPID) {
174 const std::string arbitrary_client_appid{"Client2"};
175 const int arbitrary_client_pid{2};
177 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
179 std::string appid = client_manager.find_client_appid_by_pid(preloaded_client_pid_1);
181 ASSERT_EQ(appid, preloaded_client_appid_1);
184 TEST_F(StorageWithOneClient, ReturnsCorrectCreatedPIDAppIDByPID) {
185 const std::string arbitrary_client_appid{"Client2"};
186 const int arbitrary_client_pid{2};
188 client_manager.create_client(arbitrary_client_pid, arbitrary_client_appid);
190 std::string appid = client_manager.find_client_appid_by_pid(arbitrary_client_pid);
192 ASSERT_EQ(appid, arbitrary_client_appid);
195 int main(int argc, char** argv) {
196 testing::InitGoogleTest(&argc, argv);
197 int ret = RUN_ALL_TESTS();