• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 TEST_WUKONG_UTIL_H
17 #define TEST_WUKONG_UTIL_H
18 
19 #include <dirent.h>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 
24 #include "bundle_mgr_proxy.h"
25 #include "errors.h"
26 #include "semaphore_ex.h"
27 #include "singleton.h"
28 
29 namespace OHOS {
30 namespace WuKong {
31 class WuKongUtil : public DelayedSingleton<WuKongUtil> {
32 public:
33     /**
34      * @brief Get the All App Info object
35      * @return Return ERR_OK on success, others on failure.
36      */
37     ErrCode GetAllAppInfo();
38 
39     /**
40      * @brief to find  the location of the key bundle in the bundle list.
41      * @param bundleList the bundlelist of test
42      * @param key the key subscript in the bundle list.
43      * @return the location of the key bundle in the bundlelist.
44      */
45     uint32_t FindElement(std::vector<std::string> &bundleList, std::string key);
46 
47     /**
48      * @brief to find if the key bundle is in the bundle list.
49      * @param bundleList the bundlelist of test
50      * @param key the key subscript in the bundle list.
51      * @return if the key bundle in the bundlelist.
52      */
53     bool ContainsElement(std::vector<std::string> &bundleList, std::string key);
54 
55     /**
56      * @brief Set the Allow List.
57      * @param optarg command string
58      * @return Return ERR_OK on success, others on failure.
59      */
60     ErrCode SetAllowList(const std::string &optarg);
61 
62     /**
63      * @brief Set the Block List.
64      * @param optarg command string.
65      * @return Return ERR_OK on success, others on failure.
66      */
67     ErrCode SetBlockList(const std::string &optarg);
68 
69     /**
70      * @brief Set the BlockPage List.
71      * @param optarg command string.
72      * @return Return ERR_OK on success, others on failure.
73      */
74     ErrCode SetBlockPageList(const std::string &optarg);
75 
76     /**
77      * @brief Set the BlockAbility List.
78      * @param optarg command string.
79      * @return Return ERR_OK on success, others on failure.
80      */
81     ErrCode SetBlockAbilityList(const std::string &optarg);
82 
83     /**
84      * @brief Check if AbilityName is valid
85      * @return Return ERR_OK argument is valid, others is invalid.
86      */
87     ErrCode CheckAbilityNameList();
88 
89     /**
90      * @brief Get the Allow Ability List of the test.
91      * @param bundleNameArgs the Allow Ability list.
92      */
93     void GetAllowAbilityList(std::vector<std::string> &allowAbilityList);
94 
95     /**
96      * @brief Check the list of allowed abilities to cannibalize
97      * @return Return ERR_OK is ok, others have not ok.
98      */
99     ErrCode CheckAbilityArgumentList(std::vector<std::string> &allowAbilityList);
100 
101     /**
102      * @brief Set Allow Ability List.
103      * @param optarg the Ability list of the test.
104      */
105     ErrCode SetAllowAbilityList(const std::string &optarg);
106 
107     /**
108      * @brief Get the Block Ability List of the test.
109      * @param blockAbilityList the block Ability List.
110      */
111     void GetBlockAbilityList(std::vector<std::string> &blockAbilityList);
112 
113     /**
114      * @brief Get the Allow List of the test.
115      * @param bundleNameArgs the Allow list.
116      */
117     void GetAllowList(std::vector<std::string> &Allowlist);
118 
119     /**
120      * @brief Get the block List of the test.
121      * @param blockList the block list.
122      */
123     void GetBlockList(std::vector<std::string> &blockList);
124 
125     /**
126      * @brief Get the block page List of the test.
127      * @param blockList the block page list.
128      */
129     void GetBlockPageList(std::vector<std::string> &blockPageList);
130 
131     /**
132      * @brief get the bundlelist and ability list.
133      * @param bundleList the bundle list of the test.
134      * @param abilityList the ability list of the test.
135      */
136     void SetAllAppInfo(std::vector<std::string> &bundleList, std::vector<std::string> &abilityList);
137 
138     /**
139      * @brief Get the Bundle List of the test.
140      * @param bundlelist
141      * @param abilitylist
142      */
143     void GetBundleList(std::vector<std::string> &bundlelist, std::vector<std::string> &abilitylist);
144 
145     /**
146      * @brief Get the list of the bundle list except for blockList.
147      * @param validbundlelist  the list except for blockList
148      */
149     void GetValidBundleList(std::vector<std::string> &validbundlelist);
150 
151     /**
152      * @brief get the size of screen.
153      * @param width the width of screen.
154      * @param height the height of screen.
155      * @return Return ERR_OK on success, others on failure.
156      */
157     ErrCode GetScreenSize(int32_t &width, int32_t &height);
158 
159     /**
160      * @brief get the icon path of app.
161      * @param iconpath icon path of app.
162      */
163     void GetIconPath(std::string &iconpath);
164 
165     /**
166      * @brief get start run time string for report, log etc.
167      * @return start run time string.
168      */
GetStartRunTime()169     std::string GetStartRunTime()
170     {
171         return startRunTime_;
172     }
173 
174     /**
175      * @brief get screenshot.
176      * @return Return ERR_OK on success, others on failure.
177      */
178     ErrCode WukongScreenCap(std::string &screenStorePath, bool gCommandUitest);
179 
180     /**
181      * @brief get all abilities by bundleName string.
182      * @param bundleName bundle name.
183      * @param abilities all abilities find by bundle name.
184      */
185     void GetAllAbilitiesByBundleName(std::string bundleName, std::vector<std::string> &abilities);
186 
187     /**
188      * @brief Check if param is valid
189      * @param argumentlist argument list.
190      * @param isAddToList should add check item to bundleList.
191      * @return Return ERR_OK argument is valid, others is invalid.
192      */
193     ErrCode CheckArgumentList(std::vector<std::string> &arguments, bool isAddToList);
194 
195     /**
196      * @brief get current test wukong base dir.
197      * @return dir string
198      */
199     std::string GetCurrentTestDir();
200 
201     /*
202      * @brief copy file from soruce to dest
203      * @param targetFile
204      * @param sourceFile
205      * @param destFile
206      * @return bool
207      */
208     bool CopyFile(std::string &targetFile, std::string &sourceDir, std::string &destDir);
209 
210     /*
211      * @brief deleted screenshots
212      * @param targetDir
213      * @return bool
214      */
215     bool DeleteFile(std::string targetDir);
216 
217     /*
218      * @brief set the whitelist list
219      * @param tempAllowList
220      */
221     void SetTempAllowList(std::vector<std::string> tempAllowList);
222 
223     /*
224      * @brief get the whitelist list
225      * @return tmpAllowList
226      */
227     std::vector<std::string> GetTempAllowList();
228 
229     /*
230      * @brief set orderFlag
231      * @param orderFlag
232      */
233     void SetOrderFlag(bool orderFlag);
234 
235     /*
236      * @brief get orderFlag
237      * @return bool
238      */
239     bool GetOrderFlag();
240 
241     /**
242      * @brief get all ability infos
243      * @return Return ERR_OK if get sth, others not.
244      */
245     ErrCode GetAllAbilities();
246 
247     /*
248      * @brief set the component id block list
249      * @param compIdBlockList
250      */
251     void SetCompIdBlockList(const std::string &optarg);
252 
253     /*
254      * @brief get the component id block list
255      * @return compIdBlockList
256      */
257     std::vector<std::string> GetCompIdBlockList();
258 
259     /*
260      * @brief set the component Type block list
261      * @param compTypeBlockList
262      */
263     void SetCompTypeBlockList(const std::string &optarg);
264 
265     /*
266      * @brief get the component Type block list
267      * @return compTypeBlockList
268      */
269     std::vector<std::string> GetCompTypeBlockList();
270 
271     /*
272      * @brief cmd calls
273      * @param cmd
274      * @return cmd result
275      */
276     std::string runProcess(std::string cmd);
277 
278     /*
279      * @brief set IsFirstStartAppFlag
280      * @param isFirstStartApp
281      */
282     void SetIsFirstStartAppFlag(bool isFirstStartApp);
283 
284     /*
285      * @brief get isFirstStartApp
286      * @return bool
287      */
288     bool GetIsFirstStartAppFlag();
289 
290     DECLARE_DELAYED_SINGLETON(WuKongUtil);
291 
292 private:
293     /**
294      * @brief delete repeat argument of argument list.
295      * @param argumentlist argument list.
296      */
297     void DelRepeatArguments(std::vector<std::string> &argumentlist);
298 
299     /**
300      * @brief Check if there is a conflict between the block and Allow lists
301      * @return Return ERR_OK no conflict, others have conflict.
302      */
303     ErrCode CheckBundleNameList();
304 
305     /**
306      * @brief get bundle manager proxy for get abilitys.
307      * @return return bundleManager instance.
308      */
309     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgrProxy() const;
310 
311     std::string iconPath_;
312     std::vector<std::string> bundleList_;
313     std::vector<std::string> abilityList_;
314     std::vector<std::string> allowAbilityList_;
315     std::vector<std::string> validBundleList_;
316     std::vector<std::string> validAbilityList_;
317     std::vector<std::string> allowList_;
318     std::vector<std::string> blockList_ = {"com.ohos.devicemanagerui", "com.ohos.screenlock",
319                                            "com.ohos.permissionmanager"};
320     std::vector<std::string> blockPageList_ = {"pages/system"};
321     std::vector<std::string> blockAbilityList_;
322     std::vector<std::string> unLaunchedBundleList_;
323     std::vector<std::string> unLaunchedAbilityList_;
324     std::vector<std::string> allBundleList_;
325     std::vector<std::string> allAbilityList_;
326     int32_t screenWidth_ = -1;
327     int32_t screenHeight_ = -1;
328 
329     std::string startRunTime_;
330     std::string curDir_;
331 
332     bool orderFlag_ = false;
333     bool isFirstStartApp_ = true;
334     std::vector<std::string> tempAllowList_;
335     std::vector<std::string> compIdBlockList_ = {};
336     std::vector<std::string> compTypeBlockList_ = {"Divider"};
337 };
338 
339 class WukongSemaphore {
340 public:
WukongSemaphore(const std::string & name,size_t size)341     WukongSemaphore(const std::string &name, size_t size) : name_(name)
342     {
343         maxCount_ = 0;
344     }
~WukongSemaphore()345     ~WukongSemaphore()
346     {
347     }
348 
Create()349     bool Create()
350     {
351         return true;
352     }
Unlink()353     bool Unlink()
354     {
355         return true;
356     }
357 
Open()358     bool Open()
359     {
360         return true;
361     }
Close()362     bool Close()
363     {
364         return true;
365     }
366 
Wait()367     bool Wait()
368     {
369         return true;
370     }
TryWait()371     bool TryWait()
372     {
373         return true;
374     }
TimedWait(const struct timespec & ts)375     bool TimedWait(const struct timespec &ts)
376     {
377         return true;
378     }
Post()379     bool Post()
380     {
381         return true;
382     }
383 
GetValue()384     int GetValue() const
385     {
386         return maxCount_;
387     }
388 
389 private:
390     std::string name_;
391     int maxCount_;
392 };
393 
394 typedef WukongSemaphore NamedSemaphore;
395 }  // namespace WuKong
396 }  // namespace OHOS
397 #endif  // TEST_WUKONG_UTIL_H
398