• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_SYSTEM_TEST_ABILITY_UTIL_H
17 #define OHOS_SYSTEM_TEST_ABILITY_UTIL_H
18 #include "event.h"
19 #include "stoperator.h"
20 #include "ability_manager_service.h"
21 #include "ability_manager_errors.h"
22 #include "app_mgr_service.h"
23 #include "bundle_installer_interface.h"
24 #include "bundle_mgr_interface.h"
25 #include "hilog_wrapper.h"
26 #include "module_test_dump_util.h"
27 #include "sa_mgr_client.h"
28 #include "system_ability_definition.h"
29 #include "common_event.h"
30 #include "common_event_manager.h"
31 #include "status_receiver_host.h"
32 #include "iservice_registry.h"
33 #include <memory>
34 #include <mutex>
35 #include <cstdio>
36 #include <thread>
37 #include <chrono>
38 
39 namespace OHOS {
40 namespace STABUtil {
41 namespace {
42 using vector_str = std::vector<std::string>;
43 using MAP_STR_STR = std::map<std::string, std::string>;
44 }  // namespace
45 class STAbilityUtil {
46 public:
47     static std::shared_ptr<STAbilityUtil> GetInstance();
48     static void DestroyInstance();
49     ~STAbilityUtil() = default;
50 
51     /**
52      *
53      * @param  {string} eventName                  : Sent Event Name
54      * @param  {int} code                  : Sent Code
55      * @param  {string} data                  : Sent Data
56      * @Introduction: Sent Event
57      */
58     static bool PublishEvent(const std::string &eventName, const int &code, const std::string &data);
59 
60     /**
61      *
62      * @param  {vector<string>} hapNames                  : container containing multiple hap names
63      * @Introduction: use bm install multiple hap
64      */
65     static void InstallHaps(vector_str &hapNames);
66 
67     /**
68      *
69      * @param  {vector<string>} bundleNames                  : container containing multiple bundle names
70      * @Introduction: use bm install multiple bundle
71      */
72     static void UninstallBundle(vector_str &bundleNames);
73 
74     /**
75      *
76      * @param  {string} serviceName                  : process name
77      * @Introduction: kill process
78      */
79     static void KillService(const std::string &serviceName);
80 
81     /**
82      *
83      * @param  {string} serviceName                  : executable file name
84      * @param  {time_t} delay                  : Waiting time for executable to start(milliseconds)
85      * @Introduction: start executable file
86      */
87     static void StartService(const std::string &serviceName, const time_t &delay = 0);
88 
89     /**
90      *
91      * @param  {vector<string>} bundleNames                  : Container Containing Multiple Bundle Names
92      * @Introduction: start executable file
93      */
94     static void KillBundleProcess(vector_str &bundleNames);
95 
96     /**
97      *
98      * @Introduction: Get Ability manager Service.
99      */
100     static sptr<AAFwk::IAbilityManager> GetAbilityManagerService();
101 
102     /**
103      *
104      * @Introduction: Get App manager Service.
105      */
106     static sptr<AppExecFwk::IAppMgr> GetAppMgrService();
107 
108     /**
109      *
110      * @param  {Want} want                  : The want of the ability to start.
111      * @param  {sptr<AAFwk::IAbilityManager>} abilityMs                  : Ability Manager Service ptr
112      * @param  {time_t} delay                  : Waiting time for ability to start (milliseconds)
113      * @Introduction: start ability
114      */
115     static ErrCode StartAbility(
116         const AAFwk::Want &want, sptr<AAFwk::IAbilityManager> &abilityMs, const time_t &delay = 0);
117 
118     /**
119      *
120      * @param  {string} eventName                  : Sent Event Name
121      * @param  {int} code                  : Sent Code
122      * @param  {string} data                  : Sent Data
123      * @Introduction: Sent Event to terminate app
124      */
125     static bool StopAbility(const std::string &eventName, const int &code, const std::string &data);
126     /**
127      *
128      * @param  {string} deviceId                  : Device ID
129      * @param  {string} abilityName                  : Ability Name
130      * @param  {string} bundleName                  : Bundle Name
131      * @param  {MAP_STR_STR} params                  : Params (SetParam)
132      * @Introduction: Great Want
133      */
134     static AAFwk::Want MakeWant(
135         std::string deviceId, std::string abilityName, std::string bundleName, MAP_STR_STR params = {});
136 
137     /**
138      *
139      * @param  {string} deviceId                  : Device ID
140      * @param  {string} abilityName                  : Ability Name
141      * @param  {string} bundleName                  : Bundle Name
142      * @param  {vector_str} params                  : Params (SetParam)
143      * @Introduction: Great Want
144      */
145     static AAFwk::Want MakeWant(
146         std::string deviceId, std::string abilityName, std::string bundleName, vector_str params = {});
147 
148     /**
149      *
150      * @param  {int64_t} id                  : Ability Record ID
151      * @param  {sptr<AAFwk::IAbilityManager>} abilityMs                  : Ability Manager Service ptr
152      * @Introduction: Get Top AbilityRecord ID
153      */
154     static ErrCode GetTopAbilityRecordId(int64_t &id, sptr<AAFwk::IAbilityManager> &abilityMs);
155 
156     /**
157      *
158      * @param  {shared_ptr<RunningProcessInfo>} runningProcessInfo                  : Process Info
159      * @param  {sptr<AppExecFwk::IAppMgr>} appMs                  : App Manager ptr
160      * @param  {time_t} delay                  : Waiting time for ability to Get Process Info (milliseconds)
161      * @Introduction: Get Top AbilityRecord ID
162      */
163     static ErrCode GetRunningProcessInfo(std::vector<AppExecFwk::RunningProcessInfo> &runningProcessInfo,
164         sptr<AppExecFwk::IAppMgr> &appMs, const time_t &delay = 0);
165 
166     /**
167      *
168      * @param  {string} appName                  : app Name
169      * @param  {sptr<AppExecFwk::IAppMgr>} appMs                  : App Manager ptr
170      * @param  {time_t} delay                  : Waiting time for ability to KillApplication (milliseconds)
171      * @Introduction: Get Top AbilityRecord ID
172      */
173     static ErrCode KillApplication(
174         const std::string &appName, sptr<AppExecFwk::IAppMgr> &appMs, const time_t &delay = 0);
175 
176     /**
177      *
178      * @param  {string} processName                  : processName
179      * @param  {sptr<AppExecFwk::IAppMgr>} appMs                  : App Manager ptr
180      * @param  {time_t} delay                  : Waiting time for ability to GetAppProcessInfo (milliseconds)
181      * @Introduction: Get Top AbilityRecord ID
182      */
183     static AppExecFwk::RunningProcessInfo GetAppProcessInfoByName(
184         const std::string &processName, sptr<AppExecFwk::IAppMgr> &appMs, const time_t &delay = 0);
185 
186     /**
187      *
188      * @param  {Event} event                   : Event Class Object
189      * @param  {string} eventName                  : The name of the event to wait for.
190      * @param  {int} code                  : The code of the event to wait for.
191      * @param  {int} timeout                  : Time of wait (seconds)
192      * @Introduction: Judge whether the event is received in the event queue, if not, wait
193      */
194     static int WaitCompleted(
195         STtools::Event &event, const std::string &eventName, const int code, const int timeout = 15);
196 
197     /**
198      *
199      * @param  {Event} event                   : Event Class Object
200      * @param  {string} eventName                  : The name of the event to wait for.
201      * @param  {int} code                  : The code of the event to wait for.
202      * @Introduction: Compare the received events, if not, join the event queue.
203      */
204     static void Completed(STtools::Event &event, const std::string &eventName, const int code);
205 
206     /**
207      *
208      * @param  {Event} event                   : Event Class Object
209      * @param  {string} eventName                  : The name of the event to wait for.
210      * @param  {int} code                  : The code of the event to wait for.
211      * @param  {string} data                  : The data of the event to wait for.
212      * @Introduction: Compare the received events, if not, join the event queue.
213      */
214     static void Completed(STtools::Event &event, const std::string &eventName, const int code, const std::string &data);
215 
216     /**
217      *
218      * @param  {Event} event                   : Event Class Object
219      * @Introduction: Clean wait event.
220      */
221     static void CleanMsg(STtools::Event &event);
222 
223     /**
224      *
225      * @param  {Event} event                   : Event Class Object
226      * @param  {string} eventName                  : The name of the event to wait for.
227      * @param  {int} code                  : The code of the event to wait for.
228      * @Introduction: get the event data.
229      */
230     static std::string GetData(STtools::Event &event, const std::string &eventName, const int code);
231 
232     /**
233      *
234      * @param  {StOperator} ParentOperator                   : StOperator Class Object
235      * @Introduction:  Serialization StOperator Class Object To Vector
236      */
237     static std::vector<std::string> SerializationStOperatorToVector(STtools::StOperator &ParentOperator);
238 
239     /**
240      *
241      * @param  {StOperator} ParentOperator                   : StOperator Class Object
242      * @param  {vector<string>} vectorOperator                   : Data Source Resolved Into StOperator Class Object
243      * @Introduction: Deserialization StOperator Class Object From Vector
244      */
245     static void DeserializationStOperatorFromVector(
246         STtools::StOperator &ParentOperator, std::vector<std::string> &vectorOperator);
247 
248     /**
249      *
250      * @param  {Want} want                  : The want of the ability to start.
251      * @param  {sptr<IAbilityConnection>} connect                  : Callback
252      * @param  {time_t} delay                  : Waiting time for ability to start (milliseconds)
253      * @Introduction: connect ability
254      */
255     static ErrCode ConnectAbility(const AAFwk::Want &want, const sptr<AAFwk::IAbilityConnection> &connect,
256         const sptr<IRemoteObject> &callerToken, unsigned int usec = 0);
257 
258     /**
259      *
260      * @param  {Want} want                  : The want of the ability to start.
261      * @param  {sptr<IAbilityConnection>} connect                  : Callback
262      * @param  {time_t} delay                  : Waiting time for ability to start (milliseconds)
263      * @Introduction: disconnect ability
264      */
265     static ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &connect, unsigned int usec = 0);
266 
267     /**
268      *
269      * @param  {Want} want                  : The want of the ability to start.
270      * @param  {time_t} delay                  : Waiting time for ability to start (milliseconds)
271      * @Introduction: stop service ability
272      */
273     static ErrCode StopServiceAbility(const AAFwk::Want &want, unsigned int usec = 0);
274 
275     /**
276      *
277      * @param  {string} bundleFilePath                  : Hap File Name
278      * @param  {AppExecFwk::InstallFlag} installFlag                  : install flag
279      * @Introduction: use bms install
280      */
281     static void Install(
282         const std::string &bundleFilePath, const AppExecFwk::InstallFlag installFlag = AppExecFwk::InstallFlag::NORMAL);
283 
284     /**
285      *
286      * @param  {string} bundleName                  : bundleName
287      * @Introduction: use bm uninstall bundleName
288      */
289     static void Uninstall(const std::string &bundleName);
290 
291 private:
292     /**
293      *
294      * @Introduction: get bundleManger object
295      */
296     static sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy();
297 
298     /**
299      *
300      * @Introduction: get bundleInstaller object
301      */
302     static sptr<AppExecFwk::IBundleInstaller> GetInstallerProxy();
303     /**
304      *
305      * @param  {vector<string>} vectorOperator                   : StOperator Class Object Info Save in vectorOperator
306      * @param  {StOperator} ParentOperator                   : StOperator Class Object
307      * @Introduction: push StOperator Class Object In Vector
308      */
309     static void PushOperatorInVector(std::vector<std::string> &vectorOperator, STtools::StOperator &ParentOperator);
310 
311     /**
312      *
313      * @param  {StOperator} ParentOperator                   : StOperator Class Object
314      * @param  {vector<string>} vectorOperator                   : Data Source Resolved Into StOperator Class Object
315      * @Introduction: Pull StOperator Class Object From Vector
316      */
317     STAbilityUtil() = default;
318     static void PullOperatorFromVector(STtools::StOperator &ParentOperator, std::vector<std::string> &vectorOperator);
319     static std::shared_ptr<STAbilityUtil> instance_;
320     static std::mutex mutex_;
321 };
322 
323 class InstallToolStatusReceiver : public AppExecFwk::StatusReceiverHost {
324 public:
325     InstallToolStatusReceiver();
326     virtual ~InstallToolStatusReceiver() override;
327     virtual void OnStatusNotify(const int progress) override;
328     virtual void OnFinished(const int32_t resultCode, const std::string &resultMsg) override;
329     static int TestWaitCompleted(STtools::Event &event, const std::string eventName, const int code,
330         const int timeout = 10);
331     static void TestCompleted(STtools::Event &event, const std::string &eventName, const int code);
332     STtools::Event event_ = STtools::Event();
333 
334 private:
335     int iProgress_ = 0;
336     DISALLOW_COPY_AND_MOVE(InstallToolStatusReceiver);
337 };
338 }  // namespace STABUtil
339 }  // namespace OHOS
340 #endif  // OHOS_SYSTEM_TEST_ABILITY_UTIL_H
341