Bump version to 0.2.30
[platform/core/uifw/multi-assistant-service.git] / tests / utc / service-main / test_service_main.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 "service_main.h"
20
21 #include <string>
22 #include <unistd.h>
23 #include <stdlib.h>
24
25 class CDummyApplicationManager : public IApplicationManager
26 {
27 public:
28         CDummyApplicationManager() {};
29         ~CDummyApplicationManager() {};
30
31         bool is_application_running(pid_t pid) override { return false; }
32         bool is_application_running(const std::string& appid) override { return false; }
33         bool bring_app_to_foreground(const std::string& appid) override
34         {
35                 brought_to_foreground_appid = appid;
36                 return true;
37         }
38         bool launch_app_async(const std::string& appid, bool background) override
39         {
40                 launched_appid = appid;
41                 launched_option_background = background;
42                 return true;
43         }
44         boost::optional<std::string> get_appid_by_pid(pid_t pid) override { return boost::optional<std::string>{}; }
45 public:
46         boost::optional<std::string> launched_appid;
47         boost::optional<bool> launched_option_background;
48         boost::optional<std::string> brought_to_foreground_appid;;
49 };
50
51 class CDummyPreferenceManager : public IPreferenceManager
52 {
53 public:
54         CDummyPreferenceManager() {}
55         ~CDummyPreferenceManager() {}
56
57         boost::optional<std::string> get_string(const std::string& key) override
58         {
59                 boost::optional<std::string> ret;
60                 const auto& iter = key_value_string.find(key);
61                 if (iter != key_value_string.end()) {
62                         ret = iter->second;
63                 }
64                 return ret;
65         }
66         boost::optional<int> get_int(const std::string& key) override
67         {
68                 boost::optional<int> ret;
69                 const auto& iter = key_value_int.find(key);
70                 if (iter != key_value_int.end()) {
71                         ret = iter->second;
72                 }
73                 return ret;
74         }
75         boost::optional<bool> get_bool(const std::string& key) override
76         {
77                 boost::optional<bool> ret;
78                 const auto& iter = key_value_bool.find(key);
79                 if (iter != key_value_bool.end()) {
80                         ret = iter->second;
81                 }
82                 return ret;
83         }
84
85         bool register_changed_callback(
86                         const std::string& key, preference_changed_cb calback, void* user_data) override { return true; }
87         bool unregister_changed_callback(
88                         const std::string& key, preference_changed_cb callback) override { return true; }
89 public:
90         std::map<std::string, std::string> key_value_string;
91         std::map<std::string, int> key_value_int;
92         std::map<std::string, bool> key_value_bool;
93 };
94
95 class DefaultFixure : public testing::Test
96 {
97 public:
98         DefaultFixure() {
99         }
100         virtual ~DefaultFixure() {
101         }
102         void SetUp() override {
103         }
104         void TearDown() override {
105         }
106         CDummyApplicationManager application_manager;
107         CDummyPreferenceManager preference_manager;
108
109         CServiceMain service{application_manager, preference_manager};
110 };
111
112 TEST_F(DefaultFixure, BringsDefaultAssistantToFrontOnAssistantActivatedPreprocessingStateEvent) {
113         std::string assistant_appid{"Arbitrary Assistant"};
114         ma_assistant_info_s info;
115         info.app_id = assistant_appid.c_str();
116         service.add_assistant_info(&info);
117         service.mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
118
119         bool brought = false;
120         if (application_manager.brought_to_foreground_appid) {
121                 brought = true;
122         }
123
124         ASSERT_TRUE(brought);
125 }
126
127 int main(int argc, char** argv) {
128         const char* seconds = getenv("TEST_SLEEP_SECONDS");
129         if (seconds) {
130                 sleep(atoi(seconds));
131         }
132         testing::InitGoogleTest(&argc, argv);
133         int ret = RUN_ALL_TESTS();
134         return ret;
135 }