• 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 #include "random_test_flow.h"
17 
18 #include <string>
19 
20 #include "report.h"
21 #include "wukong_define.h"
22 #include "ability_manager_client.h"
23 #include "component_manager.h"
24 #include "accessibility_ui_test_ability.h"
25 
26 namespace OHOS {
27 namespace WuKong {
28 namespace {
29 const std::string RANDOM_TEST_HELP_MSG =
30     "usage: wukong exec [<arguments>]\n"
31     "These are wukong exec arguments list:\n"
32     "   -h, --help                 random test help\n"
33     "   -a, --appswitch            appswitch event percent\n"
34     "   -b, --bundle               the bundle name of allowlist\n"
35     "   -p, --prohibit             the bundle name of blocklist\n"
36     "   -d, --page                 block page list\n"
37     "   -t, --touch                touch event percent\n"
38     "   -c, --count                test count\n"
39     "   -i, --interval             interval\n"
40     "   -s, --seed                 random seed\n"
41     "   -m, --mouse                mouse event percent\n"
42     "   -k, --keyboard             keyboard event percent\n"
43     "   -H, --hardkey              hardkey event percent\n"
44     "   -S, --swap                 swap event percent\n"
45     "   -T, --time                 test time\n"
46     "   -C, --component            component event percent\n"
47     "   -r, --rotate               rotate event percent\n"
48     "   -e, --allow ability        the ability name of allowlist\n"
49     "   -E, --block ability        the ability name of blocklist\n"
50     "   -Y, --blockCompId          the id list of block component\n"
51     "   -y, --blockCompType        the type list of block component\n"
52     "   -I, --screenshot           get screenshot(only in random input)\n";
53 
54 const std::string SHORT_OPTIONS = "a:b:c:d:e:E:hIi:k:p:s:t:T:H:m:S:C:r:Y:y:";
55 const struct option LONG_OPTIONS[] = {
56     {"help", no_argument, nullptr, 'h'},             // help
57     {"seed", required_argument, nullptr, 's'},       // test seed
58     {"time", required_argument, nullptr, 'T'},       // test time
59     {"count", required_argument, nullptr, 'c'},      // test count
60     {"interval", required_argument, nullptr, 'i'},   // test interval
61     {"bundle", required_argument, nullptr, 'b'},     // test haps
62     {"appswitch", required_argument, nullptr, 'a'},  // switch app percent
63     {"keyboard", required_argument, nullptr, 'k'},   // keyboard percent
64     {"mouse", required_argument, nullptr, 'm'},      // mouse percent
65     {"touch", required_argument, nullptr, 't'},      // touch percent
66     {"swap", required_argument, nullptr, 'S'},       // swap percent
67     {"hardkey", required_argument, nullptr, 'H'},    // hardkey percent
68     {"prohibit", required_argument, nullptr, 'p'},   // prohibit
69     {"component", required_argument, nullptr, 'C'},  // prohibit
70     {"rotate", required_argument, nullptr, 'r'},     // rotate percent
71     {"page", required_argument, nullptr, 'd'},       // block page
72     {"allow ability", required_argument, nullptr, 'e'},
73     {"block ability", required_argument, nullptr, 'E'},
74     {"blockCompId", required_argument, nullptr, 'Y'},
75     {"blockCompType", required_argument, nullptr, 'y'},
76     {"screenshot", no_argument, nullptr, 'I'},
77 };
78 
79 /**
80  * WuKong default input action percent.
81  */
82 const vector<int> DEFAULT_INPUT_PERCENT = {
83     10,  // INPUTTYPE_TOUCHINPUT,      input touch event
84     3,   // INPUTTYPE_SWAPINPUT,       input swap event
85     1,   // INPUTTYPE_MOUSEINPUT,      input mouse event
86     2,   // INPUTTYPE_KEYBOARDINPUT,   input keyboard event
87     70,  // INPUTTYPE_ELEMENTINPUT,    input element event
88     10,  // INPUTTYPE_APPSWITCHINPUT,  input appswitch event
89     2,   // INPUTTYPE_HARDKEYINPUT,    input hardkey event
90     2    // INPUTTYPE_ROTATE,          input rotate event
91 };
92 
93 const map<int, InputType> OPTION_INPUT_PERCENT = {
94     {'a', INPUTTYPE_APPSWITCHINPUT},  // input appswitch event
95     {'C', INPUTTYPE_ELEMENTINPUT},    // input element event
96     {'k', INPUTTYPE_KEYBOARDINPUT},   // input keyboard event
97     {'S', INPUTTYPE_SWAPINPUT},       // input swap event
98     {'m', INPUTTYPE_MOUSEINPUT},      // input mouse event
99     {'t', INPUTTYPE_TOUCHINPUT},      // input touch event
100     {'H', INPUTTYPE_HARDKEYINPUT},    // input hardkey event
101     {'r', INPUTTYPE_ROTATEINPUT}      // input rotate event
102 };
103 
104 /**
105  * @brief Wukong block page
106  */
107 std::vector<std::string> systemPaths;
108 
109 const int ONE_HUNDRED_PERCENT = 100;
110 // one minute (ms)
111 const int ONE_MINUTE = 60000;
112 // rotate
113 const int ROTATE = 114;
114 bool g_commandSEEDENABLE = false;
115 bool g_commandHELPENABLE = false;
116 bool g_commandTIMEENABLE = false;
117 bool g_commandCOUNTENABLE = false;
118 bool g_isAppStarted = false;
119 bool g_commandALLOWABILITYENABLE = false;
120 bool g_commandBLOCKABILITYENABLE = false;
121 bool g_commandALLOWBUNDLEENABLE = false;
122 bool g_commandSCREENSHOTENABLE = false;
123 // default false
124 bool g_commandUITEST = false;
125 }  // namespace
126 using namespace std;
127 
RandomTestFlow(WuKongShellCommand & shellcommand)128 RandomTestFlow::RandomTestFlow(WuKongShellCommand &shellcommand)
129     : TestFlow(shellcommand),
130       inputPercent_(INPUTTYPE_INVALIDINPUT, 0)
131 {
132 }
133 
~RandomTestFlow()134 RandomTestFlow::~RandomTestFlow()
135 {
136     if (timer_ != nullptr) {
137         timer_->Shutdown();
138         timer_->Unregister(timerId_);
139         timer_ = nullptr;
140     }
141 }
142 
InitEventPercent()143 ErrCode RandomTestFlow::InitEventPercent()
144 {
145     int sumPercent = 0;
146     int sumLastDefaultPercent = ONE_HUNDRED_PERCENT;
147     vector<int> lastDefaultPercent = DEFAULT_INPUT_PERCENT;
148     for (auto input : inputPercent_) {
149         TRACK_LOG_STR("input: (%02d)", input);
150     }
151     for (int type = 0; type < INPUTTYPE_INVALIDINPUT; type++) {
152         // add type to count input list for random algorithm.
153         for (int index = 0; index < inputPercent_[type]; index++) {
154             eventList_.push_back(type);
155         }
156         // check argument percent, and set last default percent.
157         if (inputPercent_[type] > 0) {
158             sumLastDefaultPercent -= lastDefaultPercent[type];
159             lastDefaultPercent[type] = 0;
160         }
161         sumPercent += inputPercent_[type];
162     }
163     TRACK_LOG_STR("sumPercent: %d", sumPercent);
164     // check the sum percent more than 100%, and exit wukong.
165     if (sumPercent > ONE_HUNDRED_PERCENT) {
166         shellcommand_.ResultReceiverAppend("all event percentage more than 1, please reset params.\n");
167         shellcommand_.ResultReceiverAppend(RANDOM_TEST_HELP_MSG);
168         return OHOS::ERR_INVALID_VALUE;
169     }
170 
171     // sum the last default percent for calculate last percent.
172     int lastPercent = ONE_HUNDRED_PERCENT - sumPercent;
173     int lastInputPercent = 0;
174     for (int type = 0; type < INPUTTYPE_INVALIDINPUT; type++) {
175         if (lastDefaultPercent[type] <= 0 || lastDefaultPercent[type] > ONE_HUNDRED_PERCENT ||
176             sumLastDefaultPercent <= 0) {
177             continue;
178         }
179         lastInputPercent = (int)(lastPercent * ((float)(lastDefaultPercent[type]) / sumLastDefaultPercent));
180         // add type to count input list for random algorithm.
181         for (int index = 0; index < lastInputPercent; index++) {
182             eventList_.push_back(type);
183         }
184         sumPercent += lastInputPercent;
185     }
186 
187     // if the sumPercent less than 100%, add INPUTTYPE_TOUCHINPUT to random algorithm.
188     for (int index = 0; index < ONE_HUNDRED_PERCENT - sumPercent; index++) {
189         eventList_.push_back(INPUTTYPE_TOUCHINPUT);
190     }
191 
192     return OHOS::ERR_OK;
193 }
194 
EnvInit()195 ErrCode RandomTestFlow::EnvInit()
196 {
197     // init event list percent.
198     ErrCode result = InitEventPercent();
199     if (result != OHOS::ERR_OK) {
200         return result;
201     }
202 
203     // init srand and print seed information.
204     if (g_commandSEEDENABLE) {
205         srand(seedArgs_);
206     } else {
207         time_t tempSeed = time(nullptr);
208         srand((unsigned int)tempSeed);
209         seedArgs_ = (int)time(nullptr);
210     }
211     Report::GetInstance()->SetSeed(std::to_string(seedArgs_));
212     TEST_RUN_LOG(("Seed: " + std::to_string(seedArgs_)).c_str());
213 
214     // shuffle the event list.
215     RandomShuffle();
216 
217     // if time test flow, register timer.
218     if (g_commandTIMEENABLE) {
219         RegisterTimer();
220     }
221     return result;
222 }
223 
SetInputPercent(const int option)224 ErrCode RandomTestFlow::SetInputPercent(const int option)
225 {
226     InputType inputType = INPUTTYPE_INVALIDINPUT;
227     auto it = OPTION_INPUT_PERCENT.find(option);
228     if (it == OPTION_INPUT_PERCENT.end()) {
229         return OHOS::ERR_INVALID_VALUE;
230     }
231 
232     inputType = it->second;
233     float percent = 0.0;
234     try {
235         percent = std::stof(optarg);
236         if ((it->first) == ROTATE && percent == 1) {
237             g_isAppStarted = true;
238         }
239     } catch (const std::exception &e) {
240         // try the option argument string convert float.
241         shellcommand_.ResultReceiverAppend("error: option '");
242         shellcommand_.ResultReceiverAppend(string((char *)(&option)));
243         shellcommand_.ResultReceiverAppend("' requires a value.\n");
244         shellcommand_.ResultReceiverAppend(RANDOM_TEST_HELP_MSG);
245         return OHOS::ERR_INVALID_VALUE;
246     }
247     // check valid of the option argument
248     if (percent > 1 || percent < 0) {
249         shellcommand_.ResultReceiverAppend("the input percent more than 1 (100%).\n");
250         shellcommand_.ResultReceiverAppend(RANDOM_TEST_HELP_MSG);
251         return OHOS::ERR_INVALID_VALUE;
252     }
253 
254     // convert float to int (0 ~ 100)
255     inputPercent_[inputType] = (int)(percent * ONE_HUNDRED_PERCENT);
256     return OHOS::ERR_OK;
257 }
258 
SetBlackWhiteSheet(const int option)259 ErrCode RandomTestFlow::SetBlackWhiteSheet(const int option)
260 {
261     ErrCode result = OHOS::ERR_OK;
262     if (option == 'b') {
263         result = WuKongUtil::GetInstance()->SetAllowList(optarg);
264         g_commandALLOWBUNDLEENABLE = true;
265     } else if (option == 'p') {
266         result = WuKongUtil::GetInstance()->SetBlockList(optarg);
267     } else if (option == 'e') {
268         result = WuKongUtil::GetInstance()->SetAllowAbilityList(optarg);
269         if (result != OHOS::ERR_INVALID_VALUE) {
270             result = CheckArgument(option);
271         }
272     } else if (option == 'E') {
273         result = WuKongUtil::GetInstance()->SetBlockAbilityList(optarg);
274         if (result != OHOS::ERR_INVALID_VALUE) {
275             result = CheckArgument(option);
276         }
277     } else if (option == 'd') {
278         result = WuKongUtil::GetInstance()->SetBlockPageList(optarg);
279     } else if (option == 'Y') {
280         WuKongUtil::GetInstance()->SetCompIdBlockList(optarg);
281     } else if (option == 'y') {
282         WuKongUtil::GetInstance()->SetCompTypeBlockList(optarg);
283     }
284     return OHOS::ERR_OK;
285 }
286 
SetRunningParam(const int option)287 ErrCode RandomTestFlow::SetRunningParam(const int option)
288 {
289     ErrCode result = OHOS::ERR_OK;
290     if (option == 'c' || option == 'T') {
291         result = CheckArgument(option);
292     } else if (option == 'i') {
293         intervalArgs_ = std::stoi(optarg);
294         TEST_RUN_LOG(("Interval: " + std::to_string(intervalArgs_)).c_str());
295     } else if (option == 's') {
296         seedArgs_ = std::stoi(optarg);
297         g_commandSEEDENABLE = true;
298     }
299     return OHOS::ERR_OK;
300 }
301 
SetRunningIndicator(const int option)302 ErrCode RandomTestFlow::SetRunningIndicator(const int option)
303 {
304     ErrCode result = OHOS::ERR_OK;
305     if (option == 'h') {
306         shellcommand_.ResultReceiverAppend(RANDOM_TEST_HELP_MSG);
307         result = OHOS::ERR_NO_INIT;
308         g_commandHELPENABLE = true;
309     } else if (option == 'I') {
310         g_commandSCREENSHOTENABLE = true;
311     }
312     return OHOS::ERR_OK;
313 }
314 
InputScene(std::shared_ptr<InputAction> inputaction,bool inputFlag)315 ErrCode RandomTestFlow::InputScene(std::shared_ptr<InputAction> inputaction, bool inputFlag)
316 {
317     ErrCode result = OHOS::ERR_OK;
318     if (inputFlag) {
319         result = inputaction->RandomInput();
320     } else {
321         ComponentManager::GetInstance()->BackToPrePage();
322     }
323     return result;
324 }
325 
SetBlockPage(const std::vector<std::string> systemPaths)326 bool RandomTestFlow::SetBlockPage(const std::vector<std::string> systemPaths)
327 {
328     auto root = std::make_shared<OHOS::Accessibility::AccessibilityElementInfo>();
329     auto accPtr = OHOS::Accessibility::AccessibilityUITestAbility::GetInstance();
330     // Get root AccessibilityElementInfo from Accessibility
331     accPtr->GetRoot(*(root.get()));
332     std::string path = root->GetPagePath();
333     bool inputFlag = true;
334     TRACK_LOG_STR("Componentpage path: (%s)", path.c_str());
335     for (string systemPath : systemPaths) {
336         if (path.find(systemPath) != string::npos) {
337             INFO_LOG_STR("Block the current page and return. Block page : (%s)", path.c_str());
338             inputFlag = false;
339             break;
340         }
341     }
342     return inputFlag;
343 }
344 
RunStep()345 ErrCode RandomTestFlow::RunStep()
346 {
347     ErrCode result;
348     // control the count test flow
349     if (g_commandCOUNTENABLE == true) {
350         totalCount_--;
351         if (totalCount_ < 0) {
352             isFinished_ = true;
353             return OHOS::ERR_OK;
354         }
355     }
356     if (g_commandSCREENSHOTENABLE) {
357         std::string screenStorePath;
358         result = WuKongUtil::GetInstance()->WukongScreenCap(screenStorePath, g_commandUITEST);
359         if (result == OHOS::ERR_OK) {
360             Report::GetInstance()->RecordScreenPath(screenStorePath);
361         }
362     }
363     bool inputFlag = SetBlockPage(systemPaths);
364     std::shared_ptr<InputAction> inputaction = nullptr;
365     if (!g_isAppStarted) {
366         inputaction = InputFactory::GetInputAction(INPUTTYPE_APPSWITCHINPUT);
367         if (inputaction == nullptr) {
368             ERROR_LOG("inputaction is nullptr");
369             return OHOS::ERR_INVALID_VALUE;
370         }
371         result = InputScene(inputaction, inputFlag);
372         if (result != OHOS::ERR_OK) {
373             ERROR_LOG("launch app failed and exit");
374             return result;
375         }
376         inputaction = nullptr;
377         g_isAppStarted = true;
378         usleep(intervalArgs_ * oneSecond_);
379     }
380     if (g_commandBLOCKABILITYENABLE == true) {
381         inputFlag = CheckBlockAbility();
382     }
383     // input event, get event index form event list by random algorithm.
384     int eventindex = rand() % ONE_HUNDRED_PERCENT;
385     InputType eventTypeId = (InputType)(eventList_.at(eventindex));
386     inputaction = InputFactory::GetInputAction(eventTypeId);
387     if (inputaction == nullptr) {
388         ERROR_LOG("inputaction is nullptr");
389         return OHOS::ERR_INVALID_VALUE;
390     }
391 
392     if (ProtectRightAbility(inputaction, eventTypeId) == OHOS::ERR_INVALID_VALUE) {
393         return OHOS::ERR_INVALID_VALUE;
394     }
395     result = InputScene(inputaction, inputFlag);
396     usleep(intervalArgs_ * oneSecond_);
397     return result;
398 }
399 
ProtectRightAbility(std::shared_ptr<InputAction> & inputaction,InputType & eventTypeId)400 ErrCode RandomTestFlow::ProtectRightAbility(std::shared_ptr<InputAction> &inputaction, InputType &eventTypeId)
401 {
402     std::vector<std::string> allowList;
403     WuKongUtil::GetInstance()->GetAllowList(allowList);
404     if (allowList.size() > 0 && g_commandALLOWABILITYENABLE == false) {
405         std::string bundleName = "com.ohos.launcher";
406         auto elementName = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility();
407         if (elementName.GetBundleName() == bundleName) {
408             if (eventTypeId == INPUTTYPE_TOUCHINPUT || eventTypeId == INPUTTYPE_ELEMENTINPUT) {
409                 return OHOS::ERR_INVALID_VALUE;
410             }
411         }
412         // allowList 数量大于0 并且 elementName.GetBundleName() 不在allowList里面,重新拉起一个应用
413         auto curBundleName = elementName.GetBundleName();
414         auto it = find(allowList.begin(), allowList.end(), curBundleName);
415         if (it == allowList.end()) {
416             inputaction = InputFactory::GetInputAction(INPUTTYPE_APPSWITCHINPUT);
417             if (inputaction == nullptr) {
418                 ERROR_LOG("inputaction is nullptr");
419                 return OHOS::ERR_INVALID_VALUE;
420             }
421         }
422     } else if (allowList.size() > 0 && g_commandALLOWABILITYENABLE == true) {
423         std::vector<std::string> abilityList;
424         WuKongUtil::GetInstance()->GetAllowAbilityList(abilityList);
425         auto elementName = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility();
426         auto curBundleName = elementName.GetBundleName();
427         auto curAbilityName = elementName.GetAbilityName();
428         TRACK_LOG_STR("curAbilityName : %s", curAbilityName.c_str());
429         auto bundleIndex = find(allowList.begin(), allowList.end(), curBundleName);
430         auto abilityIndex = find(abilityList.begin(), abilityList.end(), curAbilityName);
431         if (bundleIndex == allowList.end() || abilityIndex == abilityList.end()) {
432             int index = rand() % abilityList.size();
433             // start ability through bundle infomation
434             ErrCode result = AppManager::GetInstance()->StartAbilityByBundleInfo(abilityList[index], allowList[0]);
435             // print the result of start event
436             if (result ==OHOS::ERR_OK) {
437                 INFO_LOG_STR("Ability Name : ("%s") startup successful", abilityList[index].c_str());
438             } else {
439                 ERROR_LOG_STR("Ability Name : ("%s") startup failed", abilityList[index].c_str());
440             }
441             TRACK_LOG_STR("ability index : %d", index);
442         }
443         return OHOS::ERR_OK;
444     }
445     return OHOS::ERR_OK;
446 }
447 
HandleNormalOption(const int option)448 ErrCode RandomTestFlow::HandleNormalOption(const int option)
449 {
450     ErrCode result = OHOS::ERR_OK;
451     if (option == 't' || option == 'm' || option == 'S' || option == 'k' || option == 'a' ||
452         option == 'r' || option == 'C' || option == 'H') {
453         result = SetInputPercent(option);
454     } else {
455         result = SetBlackWhiteSheet(option);
456         if (result != OHOS::ERR_OK) {
457             return result;
458         }
459         result = SetRunningParam(option);
460         if (result != OHOS::ERR_OK) {
461             return result;
462         }
463         result = SetRunningIndicator(option);
464     }
465     WuKongUtil::GetInstance()->GetBlockPageList(systemPaths);
466     WuKongUtil::GetInstance()->SetOrderFlag(false);
467     return result;
468 }
469 
CheckArgument(const int option)470 ErrCode RandomTestFlow::CheckArgument(const int option)
471 {
472     ErrCode result = OHOS::ERR_OK;
473     switch (option) {
474         case 'c': {
475             // check if the '-c' and 'T' is exist at the same time
476             if (g_commandTIMEENABLE == false) {
477                 g_commandCOUNTENABLE = true;
478                 countArgs_ = std::stoi(optarg);
479                 TEST_RUN_LOG(("Count: " + std::to_string(countArgs_)).c_str());
480                 totalCount_ = countArgs_;
481             } else {
482                 DEBUG_LOG(PARAM_COUNT_TIME_ERROR);
483                 shellcommand_.ResultReceiverAppend(std::string(PARAM_COUNT_TIME_ERROR) + "\n");
484                 result = OHOS::ERR_INVALID_VALUE;
485             }
486             break;
487         }
488         case 'T': {
489             // check if the '-c' and 'T' is exist at the same time
490             if (g_commandCOUNTENABLE == false) {
491                 totalTime_ = std::stof(optarg);
492                 TEST_RUN_LOG(("Time: " + std::to_string(totalTime_)).c_str());
493                 g_commandTIMEENABLE = true;
494             } else {
495                 DEBUG_LOG(PARAM_TIME_COUNT_ERROR);
496                 shellcommand_.ResultReceiverAppend(std::string(PARAM_TIME_COUNT_ERROR) + "\n");
497                 result = OHOS::ERR_INVALID_VALUE;
498             }
499             break;
500         }
501         case 'e': {
502             result = CheckArgumentOptionOfe();
503             break;
504         }
505         case 'E': {
506             result = CheckArgumentOptionOfE();
507             break;
508         }
509         default: {
510             result = OHOS::ERR_INVALID_VALUE;
511             break;
512         }
513     }
514     return result;
515 }
516 
GetOptionArguments(std::string & shortOpts)517 const struct option *RandomTestFlow::GetOptionArguments(std::string &shortOpts)
518 {
519     shortOpts = SHORT_OPTIONS;
520     return LONG_OPTIONS;
521 }
522 
HandleUnknownOption(const char optopt)523 ErrCode RandomTestFlow::HandleUnknownOption(const char optopt)
524 {
525     ErrCode result = OHOS::ERR_OK;
526     switch (optopt) {
527         case 'a':
528         case 'b':
529         case 'c':
530         case 'd':
531         case 'e':
532         case 'i':
533         case 's':
534         case 't':
535         case 'r':
536         case 'S':
537         case 'p':
538         case 'k':
539         case 'H':
540         case 'T':
541         case 'm':
542         case 'C':
543         case 'E':
544         case 'Y':
545         case 'y':
546             // error: option 'x' requires a value.
547             shellcommand_.ResultReceiverAppend("error: option '-");
548             shellcommand_.ResultReceiverAppend(string(1, optopt));
549             shellcommand_.ResultReceiverAppend("' requires a value.\n");
550             result = OHOS::ERR_INVALID_VALUE;
551             break;
552         case 'h': {
553             result = OHOS::ERR_INVALID_VALUE;
554             break;
555         }
556         default: {
557             // 'wukong exec' with an unknown option: wukong exec -x
558             shellcommand_.ResultReceiverAppend(
559                 "'wukong exec' with an unknown option, please reference help information:\n");
560             result = OHOS::ERR_INVALID_VALUE;
561             break;
562         }
563     }
564     shellcommand_.ResultReceiverAppend(RANDOM_TEST_HELP_MSG);
565     return result;
566 }
567 
RandomShuffle()568 void RandomTestFlow::RandomShuffle()
569 {
570     for (uint32_t i = eventList_.size() - 1; i > 0; --i) {
571         std::swap(eventList_[i], eventList_[std::rand() % (i + 1)]);
572     }
573 }
574 
RegisterTimer()575 void RandomTestFlow::RegisterTimer()
576 {
577     if (timer_ == nullptr) {
578         timer_ = std::make_shared<Utils::Timer>("wukong");
579         timerId_ = timer_->Register(std::bind(&RandomTestFlow::TestTimeout, this), totalTime_ * ONE_MINUTE, true);
580         timer_->Setup();
581     }
582 }
583 
TestTimeout()584 void RandomTestFlow::TestTimeout()
585 {
586     g_commandTIMEENABLE = false;
587     isFinished_ = true;
588 }
589 
CheckArgumentOptionOfe()590 ErrCode RandomTestFlow::CheckArgumentOptionOfe()
591 {
592     if (g_commandALLOWABILITYENABLE == false) {
593         g_commandALLOWABILITYENABLE = true;
594         if (g_commandALLOWBUNDLEENABLE == true) {
595             return OHOS::ERR_OK;
596         } else {
597             ERROR_LOG("invalid param : When -e is configured, -b must be configured.");
598             ERROR_LOG("invalid param : please ensure that the -b is before the -e");
599             return OHOS::ERR_INVALID_VALUE;
600         }
601     } else {
602         ERROR_LOG("invalid param : please check params of '-e'.");
603         return OHOS::ERR_INVALID_VALUE;
604     }
605 }
606 
CheckArgumentOptionOfE()607 ErrCode RandomTestFlow::CheckArgumentOptionOfE()
608 {
609     if (g_commandBLOCKABILITYENABLE == false) {
610         g_commandBLOCKABILITYENABLE = true;
611         if (g_commandALLOWBUNDLEENABLE == true) {
612             return OHOS::ERR_OK;
613         } else {
614             ERROR_LOG("invalid param : When -E is configure, -b must be configured.");
615             ERROR_LOG("invalid param : Plese ensure that the -b is before the -E.");
616             return OHOS::ERR_INVALID_VALUE;
617         }
618     } else {
619         ERROR_LOG("invalid param : please check params of '-E'.");
620         return OHOS::ERR_INVALID_VALUE;
621     }
622 }
623 
CheckBlockAbility()624 bool RandomTestFlow::CheckBlockAbility()
625 {
626     bool inputFlag = true;
627     auto elementName = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility();
628     auto currentAbility = elementName.GetAbilityName();
629     std::vector<string> blockAbilityList;
630     WuKongUtil::GetInstance()->GetBlockAbilityList(blockAbilityList);
631     auto it = find(blockAbilityList.begin(), blockAbilityList.end(), currentAbility);
632     if (it != blockAbilityList.end()) {
633         INFO_LOG_STR("Block the current Ability and return. Block Ability : (%s)", currentAbility.c_str());
634         inputFlag = false;
635     }
636     return inputFlag;
637 }
638 }  // namespace WuKong
639 }  // namespace OHOS
640