• 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 Set the Allow List.
49      * @param optarg command string
50      * @return Return ERR_OK on success, others on failure.
51      */
52     ErrCode SetAllowList(const std::string &optarg);
53 
54     /**
55      * @brief Set the Block List.
56      * @param optarg command string.
57      * @return Return ERR_OK on success, others on failure.
58      */
59     ErrCode SetBlockList(const std::string &optarg);
60 
61     /**
62      * @brief Set the BlockPage List.
63      * @param optarg command string.
64      * @return Return ERR_OK on success, others on failure.
65      */
66     ErrCode SetBlockPageList(const std::string &optarg);
67 
68     /**
69      * @brief Get the Allow List of the test.
70      * @param bundleNameArgs the Allow list.
71      */
72     void GetAllowList(std::vector<std::string> &Allowlist);
73 
74     /**
75      * @brief Get the block List of the test.
76      * @param blockList the block list.
77      */
78     void GetBlockList(std::vector<std::string> &blockList);
79 
80     /**
81      * @brief Get the block page List of the test.
82      * @param blockList the block page list.
83      */
84     void GetBlockPageList(std::vector<std::string> &blockPageList);
85 
86     /**
87      * @brief get the bundlelist and ability list.
88      * @param bundleList the bundle list of the test.
89      * @param abilityList the ability list of the test.
90      */
91     void SetAllAppInfo(std::vector<std::string> &bundleList, std::vector<std::string> &abilityList);
92 
93     /**
94      * @brief Get the Bundle List of the test.
95      * @param bundlelist
96      * @param abilitylist
97      */
98     void GetBundleList(std::vector<std::string> &bundlelist, std::vector<std::string> &abilitylist);
99 
100     /**
101      * @brief Get the list of the bundle list except for blockList.
102      * @param validbundlelist  the list except for blockList
103      */
104     void GetValidBundleList(std::vector<std::string> &validbundlelist);
105 
106     /**
107      * @brief get the size of screen.
108      * @param width the width of screen.
109      * @param height the height of screen.
110      * @return Return ERR_OK on success, others on failure.
111      */
112     ErrCode GetScreenSize(int32_t &width, int32_t &height);
113 
114     /**
115      * @brief get the icon path of app.
116      * @param iconpath icon path of app.
117      */
118     void GetIconPath(std::string &iconpath);
119 
120     /**
121      * @brief get start run time string for report, log etc.
122      * @return start run time string.
123      */
GetStartRunTime()124     std::string GetStartRunTime()
125     {
126         return startRunTime_;
127     }
128 
129     /**
130      * @brief get screenshot.
131      * @return Return ERR_OK on success, others on failure.
132      */
133     ErrCode WukongScreenCap(std::string &screenStorePath, bool gCommandUitest);
134 
135     /**
136      * @brief get all abilities by bundleName string.
137      * @param bundleName bundle name.
138      * @param abilities all abilities find by bundle name.
139      */
140     void GetAllAbilitiesByBundleName(std::string bundleName, std::vector<std::string> &abilities);
141 
142     /**
143      * @brief Check if param is valid
144      * @param argumentlist argument list.
145      * @param isAddToList should add check item to bundleList.
146      * @return Return ERR_OK argument is valid, others is invalid.
147      */
148     ErrCode CheckArgumentList(std::vector<std::string> &arguments, bool isAddToList);
149 
150     /**
151      * @brief get current test wukong base dir.
152      * @return dir string
153      */
154     std::string GetCurrentTestDir();
155 
156     /*
157      * @brief copy file from soruce to dest
158      * @param targetFile
159      * @param sourceFile
160      * @param destFile
161      * @return bool
162      */
163     bool CopyFile(std::string &targetFile, std::string &sourceDir, std::string &destDir);
164 
165     /*
166      * @brief deleted screenshots
167      * @param targetDir
168      * @return bool
169      */
170     bool DeleteFile(std::string targetDir);
171 
172     /*
173      * @brief set the whitelist list
174      * @param tempAllowList
175      */
176     void SetTempAllowList(std::vector<std::string> tempAllowList);
177 
178     /*
179      * @brief get the whitelist list
180      * @return tmpAllowList
181      */
182     std::vector<std::string> GetTempAllowList();
183 
184     /*
185      * @brief set orderFlag
186      * @param orderFlag
187      */
188     void SetOrderFlag(bool orderFlag);
189 
190     /*
191      * @brief get orderFlag
192      * @return bool
193      */
194     bool GetOrderFlag();
195 
196     /**
197      * @brief get all ability infos
198      * @return Return ERR_OK if get sth, others not.
199      */
200     ErrCode GetAllAbilities();
201 
202     DECLARE_DELAYED_SINGLETON(WuKongUtil);
203 
204 private:
205     /**
206      * @brief delete repeat argument of argument list.
207      * @param argumentlist argument list.
208      */
209     void DelRepeatArguments(std::vector<std::string> &argumentlist);
210 
211     /**
212      * @brief Check if there is a conflict between the block and Allow lists
213      * @return Return ERR_OK no conflict, others have conflict.
214      */
215     ErrCode CheckBundleNameList();
216 
217     /**
218      * @brief get bundle manager proxy for get abilitys.
219      * @return return bundleManager instance.
220      */
221     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgrProxy() const;
222 
223     std::string iconPath_;
224     std::vector<std::string> bundleList_;
225     std::vector<std::string> abilityList_;
226     std::vector<std::string> validBundleList_;
227     std::vector<std::string> validAbilityList_;
228     std::vector<std::string> allowList_;
229     std::vector<std::string> blockList_ = {"com.ohos.devicemanagerui", "com.ohos.screenlock",
230                                            "com.ohos.permissionmanager"};
231     std::vector<std::string> blockPageList_ = {"pages/system"};
232     std::vector<std::string> unLaunchedBundleList_;
233     std::vector<std::string> unLaunchedAbilityList_;
234     int32_t screenWidth_ = -1;
235     int32_t screenHeight_ = -1;
236 
237     std::string startRunTime_;
238     std::string curDir_;
239 
240     bool orderFlag_ = false;
241     std::vector<std::string> tempAllowList_;
242 };
243 
244 class WukongSemaphore {
245 public:
WukongSemaphore(const std::string & name,size_t size)246     WukongSemaphore(const std::string &name, size_t size) : name_(name)
247     {
248         maxCount_ = 0;
249     }
~WukongSemaphore()250     ~WukongSemaphore()
251     {
252     }
253 
Create()254     bool Create()
255     {
256         return true;
257     }
Unlink()258     bool Unlink()
259     {
260         return true;
261     }
262 
Open()263     bool Open()
264     {
265         return true;
266     }
Close()267     bool Close()
268     {
269         return true;
270     }
271 
Wait()272     bool Wait()
273     {
274         return true;
275     }
TryWait()276     bool TryWait()
277     {
278         return true;
279     }
TimedWait(const struct timespec & ts)280     bool TimedWait(const struct timespec &ts)
281     {
282         return true;
283     }
Post()284     bool Post()
285     {
286         return true;
287     }
288 
GetValue()289     int GetValue() const
290     {
291         return maxCount_;
292     }
293 
294 private:
295     std::string name_;
296     int maxCount_;
297 };
298 
299 typedef WukongSemaphore NamedSemaphore;
300 }  // namespace WuKong
301 }  // namespace OHOS
302 #endif  // TEST_WUKONG_UTIL_H
303