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 "test_flow.h"
17
18 #include "component_manager.h"
19 #include "report.h"
20
21 namespace OHOS {
22 namespace WuKong {
23 namespace {
24 bool g_isPermissionBundle = false;
25 }
TestFlow(WuKongShellCommand & shellcommand)26 TestFlow::TestFlow(WuKongShellCommand &shellcommand)
27 : shellcommand_(shellcommand),
28 isFinished_(false),
29 semStop_(SEMPHORE_STOP_NAME, 1)
30 {
31 }
32
CheckVaildityCmd()33 ErrCode TestFlow::CheckVaildityCmd()
34 {
35 OHOS::ErrCode result = OHOS::ERR_OK;
36
37 // get command option and arguments from child class.
38 std::string shortOpts = "";
39 auto longOpts = GetOptionArguments(shortOpts);
40 if (longOpts == nullptr) {
41 return OHOS::ERR_INVALID_VALUE;
42 }
43
44 // get shell command argumnents from shellcommand.
45 int argc_ = shellcommand_.GetArgc();
46 char **argv_ = shellcommand_.GetArgv();
47 int counter = 0;
48 while (true) {
49 int option = -1;
50 counter++;
51 option = getopt_long(argc_, argv_, shortOpts.c_str(), longOpts, nullptr);
52 // process error
53 if (optind < 0 || optind > argc_) {
54 return OHOS::ERR_INVALID_VALUE;
55 }
56 // process error
57 if (option == -1) {
58 if (counter == 1 && strcmp(argv_[optind], argv_[1]) == 0) {
59 ERROR_LOG(" you must specify an option at least.");
60 result = OHOS::ERR_INVALID_VALUE;
61 }
62 break;
63 }
64 // process error
65 if (option == '?') {
66 result = HandleUnknownOption(optopt);
67 break;
68 }
69 // process correct
70 result = HandleNormalOption(option);
71 if (result != OHOS::ERR_OK) {
72 break;
73 }
74 }
75 if (result == OHOS::ERR_OK) {
76 AppManager::GetInstance()->SetAbilityController();
77 result = EnvInit();
78 if (result != OHOS::ERR_OK) {
79 return result;
80 }
81 result = WuKongUtil::GetInstance()->GetAllAppInfo();
82 }
83 return result;
84 }
85
Run()86 ErrCode TestFlow::Run()
87 {
88 TRACK_LOG_STD();
89 OHOS::ErrCode result = OHOS::ERR_OK;
90 int count = 0;
91 // init report
92
93 // Open the stop semaphore, check stop.
94 bool res = semStop_.Open();
95 if (!res) {
96 ERROR_LOG("Open stop semaphore failed.");
97 return OHOS::ERR_INVALID_VALUE;
98 }
99 // if the semaphore is 1, wait it.
100 if (semStop_.GetValue() == 1) {
101 semStop_.Wait();
102 }
103
104 // run test step, check test status, and control test step.
105 while (!isFinished_) {
106 if (g_isPermissionBundle == true) {
107 result = ComponentManager::GetInstance()->PermoissionInput();
108 if (result == OHOS::ERR_OK) {
109 g_isPermissionBundle = false;
110 }
111 DEBUG_LOG_STR("PermoissionInput Result: (%d)", result);
112 } else {
113 result = RunStep();
114 if (!isFinished_) {
115 DEBUG_LOG_STR("Step: (%d) Result: (%d)", ++count, result);
116 }
117 }
118 if (semStop_.GetValue() == 1) {
119 TEST_RUN_LOG("Finished: (Stop)");
120 isFinished_ = true;
121 }
122 }
123
124 TEST_RUN_LOG("all test Finished");
125 // recover stop semaphore.
126 if (semStop_.GetValue() == 0) {
127 semStop_.Post();
128 }
129 semStop_.Close();
130
131 // save report
132 OHOS::WuKong::Report::GetInstance()->Finish();
133 TRACK_LOG_END();
134 return result;
135 }
136
Stop(OHOS::ErrCode code)137 void TestFlow::Stop(OHOS::ErrCode code)
138 {
139 isFinished_ = true;
140 }
OnStatusUpdated(ComponentStatus status)141 void TestFlow::OnStatusUpdated(ComponentStatus status)
142 {
143 DEBUG_LOG_STR("Component Status: (%d)", status);
144 }
145
OnScreenUpdated()146 void TestFlow::OnScreenUpdated()
147 {
148 TRACK_LOG_STD();
149 TRACK_LOG_END();
150 }
151
OnPermissionScreenShown()152 void TestFlow::OnPermissionScreenShown()
153 {
154 TRACK_LOG_STD();
155 TRACK_LOG_END();
156 g_isPermissionBundle = true;
157 }
158 } // namespace WuKong
159 } // namespace OHOS
160