1a69a01cdSopenharmony_ci/*
2a69a01cdSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3a69a01cdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a69a01cdSopenharmony_ci * you may not use this file except in compliance with the License.
5a69a01cdSopenharmony_ci * You may obtain a copy of the License at
6a69a01cdSopenharmony_ci *
7a69a01cdSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a69a01cdSopenharmony_ci *
9a69a01cdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a69a01cdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a69a01cdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a69a01cdSopenharmony_ci * See the License for the specific language governing permissions and
13a69a01cdSopenharmony_ci * limitations under the License.
14a69a01cdSopenharmony_ci */
15a69a01cdSopenharmony_ci
16a69a01cdSopenharmony_ci#include "component_input.h"
17a69a01cdSopenharmony_ci
18a69a01cdSopenharmony_ci#include "component_manager.h"
19a69a01cdSopenharmony_ci#include "input_factory.h"
20a69a01cdSopenharmony_ci#include "input_manager.h"
21a69a01cdSopenharmony_ci#include "input_msg_object.h"
22a69a01cdSopenharmony_ci#include "report.h"
23a69a01cdSopenharmony_ci#include "scene_delegate.h"
24a69a01cdSopenharmony_ci#include "focus_scene_delegate.h"
25a69a01cdSopenharmony_ci#include "tree_manager.h"
26a69a01cdSopenharmony_ci#include "wukong_define.h"
27a69a01cdSopenharmony_ci
28a69a01cdSopenharmony_cinamespace OHOS {
29a69a01cdSopenharmony_cinamespace WuKong {
30a69a01cdSopenharmony_cinamespace {
31a69a01cdSopenharmony_ciconst uint32_t PAGE_BACK_COUNT_MAX = 3;
32a69a01cdSopenharmony_ciconst uint32_t LANUCH_APP_COUNT_MAX = 5;
33a69a01cdSopenharmony_ci
34a69a01cdSopenharmony_civoid PrepareApp()
35a69a01cdSopenharmony_ci{
36a69a01cdSopenharmony_ci	// Wait for the App to be launched successfully
37a69a01cdSopenharmony_ci    bool isFirstStartApp = WuKongUtil::GetInstance()->GetIsFirstStartAppFlag();
38a69a01cdSopenharmony_ci    if (isFirstStartApp) {
39a69a01cdSopenharmony_ci        usleep(THREE_SECOND);
40a69a01cdSopenharmony_ci        WuKongUtil::GetInstance()->SetIsFirstStartAppFlag(false);
41a69a01cdSopenharmony_ci    }
42a69a01cdSopenharmony_ci}
43a69a01cdSopenharmony_ci
44a69a01cdSopenharmony_civoid HandleAllBundleStatus(const std::shared_ptr<ComponentParam>& componentPtr)
45a69a01cdSopenharmony_ci{
46a69a01cdSopenharmony_ci    componentPtr->isAllFinished_ = true;
47a69a01cdSopenharmony_ci	// confirm all bundle status.
48a69a01cdSopenharmony_ci    std::vector<bool> bundleFinishList = componentPtr->bundleFinish_;
49a69a01cdSopenharmony_ci    bool x = false;
50a69a01cdSopenharmony_ci    if (std::any_of(bundleFinishList.begin(), bundleFinishList.end(), [x](int y) { return x == y; })) {
51a69a01cdSopenharmony_ci        componentPtr->isAllFinished_ = false;
52a69a01cdSopenharmony_ci    }
53a69a01cdSopenharmony_ci}
54a69a01cdSopenharmony_ci
55a69a01cdSopenharmony_ciErrCode LauncherApp(const std::string& bundleName)
56a69a01cdSopenharmony_ci{
57a69a01cdSopenharmony_ci    auto appInput = InputFactory::GetInputAction(INPUTTYPE_APPSWITCHINPUT);
58a69a01cdSopenharmony_ci    if (appInput == nullptr) {
59a69a01cdSopenharmony_ci        ERROR_LOG("InputFactory::GetInputAction INPUTTYPE_APPSWITCHINPUT is null");
60a69a01cdSopenharmony_ci        return OHOS::ERR_INVALID_VALUE;
61a69a01cdSopenharmony_ci    }
62a69a01cdSopenharmony_ci
63a69a01cdSopenharmony_ci    // launch app by AppSwitchInput function.
64a69a01cdSopenharmony_ci    std::shared_ptr<AppSwitchParam> appSwitchParam = std::make_shared<AppSwitchParam>();
65a69a01cdSopenharmony_ci    appSwitchParam->bundlename_ = bundleName;
66a69a01cdSopenharmony_ci    std::shared_ptr<SpcialTestObject> sto = appSwitchParam;
67a69a01cdSopenharmony_ci    auto result = appInput->OrderInput(sto);
68a69a01cdSopenharmony_ci    if (result != OHOS::ERR_OK) {
69a69a01cdSopenharmony_ci        ERROR_LOG("AppSwitchInput OrderInput failed");
70a69a01cdSopenharmony_ci    }
71a69a01cdSopenharmony_ci    return result;
72a69a01cdSopenharmony_ci}
73a69a01cdSopenharmony_ci
74a69a01cdSopenharmony_ciuint32_t CheckLauncherApp(const std::shared_ptr<ComponentParam>& param)
75a69a01cdSopenharmony_ci{
76a69a01cdSopenharmony_ci    TRACK_LOG_STD();
77a69a01cdSopenharmony_ci    std::vector<std::string> tempAllowList;
78a69a01cdSopenharmony_ci    for (uint32_t i = 0; i < param->bundleName_.size(); i++) {
79a69a01cdSopenharmony_ci        // do not launch app when bundle is running.
80a69a01cdSopenharmony_ci        if (param->bundleRunning_[i] == true && param->bundleFinish_[i] == false) {
81a69a01cdSopenharmony_ci            return i;
82a69a01cdSopenharmony_ci        }
83a69a01cdSopenharmony_ci        // launch app when the bundle is stop and not finish.
84a69a01cdSopenharmony_ci        if (param->bundleRunning_[i] == false && param->bundleFinish_[i] == false) {
85a69a01cdSopenharmony_ci            tempAllowList.clear();
86a69a01cdSopenharmony_ci            tempAllowList.push_back(param->bundleName_[i]);
87a69a01cdSopenharmony_ci            WuKongUtil::GetInstance()->SetTempAllowList(tempAllowList);
88a69a01cdSopenharmony_ci            // launch app by AppSwitchInput function.
89a69a01cdSopenharmony_ci            if (LauncherApp(param->bundleName_[i]) != OHOS::ERR_OK) {
90a69a01cdSopenharmony_ci                return param->bundleName_.size();
91a69a01cdSopenharmony_ci            }
92a69a01cdSopenharmony_ci            // init bundleRunning status to stop.
93a69a01cdSopenharmony_ci            std::vector<bool> bundleRunning = param->bundleRunning_;
94a69a01cdSopenharmony_ci            std::fill(bundleRunning.begin(), bundleRunning.end(), false);
95a69a01cdSopenharmony_ci
96a69a01cdSopenharmony_ci            // set current launched bundle is running.
97a69a01cdSopenharmony_ci            param->bundleRunning_[i] = true;
98a69a01cdSopenharmony_ci            TRACK_LOG_STR("%s", param->toString().c_str());
99a69a01cdSopenharmony_ci            TEST_RUN_LOG(param->bundleName_[i].c_str());
100a69a01cdSopenharmony_ci            return i;
101a69a01cdSopenharmony_ci        }
102a69a01cdSopenharmony_ci    }
103a69a01cdSopenharmony_ci    // not found bundle can be run, and return failed.
104a69a01cdSopenharmony_ci    return param->bundleName_.size();
105a69a01cdSopenharmony_ci}
106a69a01cdSopenharmony_ci
107a69a01cdSopenharmony_cibool CheckAbliltyFinished(const std::shared_ptr<AbilityTree>& abilityNode)
108a69a01cdSopenharmony_ci{
109a69a01cdSopenharmony_ci    TRACK_LOG_STD();
110a69a01cdSopenharmony_ci    bool abilityRunFinished = false;
111a69a01cdSopenharmony_ci    if (abilityNode == nullptr) {
112a69a01cdSopenharmony_ci        ERROR_LOG("abilityNode is nullptr");
113a69a01cdSopenharmony_ci        return abilityRunFinished;
114a69a01cdSopenharmony_ci    }
115a69a01cdSopenharmony_ci    uint32_t allCount = abilityNode->GetAllComponentCount();
116a69a01cdSopenharmony_ci    uint32_t inputCount = abilityNode->GetInputCount();
117a69a01cdSopenharmony_ci    TRACK_LOG_STR("ability (%s) component count (%u), input count (%u)", abilityNode->GetBundleName().c_str(), allCount,
118a69a01cdSopenharmony_ci                  inputCount);
119a69a01cdSopenharmony_ci    if (inputCount >= allCount) {
120a69a01cdSopenharmony_ci        abilityRunFinished = true;
121a69a01cdSopenharmony_ci    }
122a69a01cdSopenharmony_ci    TRACK_LOG_END();
123a69a01cdSopenharmony_ci    return abilityRunFinished;
124a69a01cdSopenharmony_ci}
125a69a01cdSopenharmony_ci
126a69a01cdSopenharmony_cibool CheckBundleFinished(const std::shared_ptr<WuKongTree>& parent)
127a69a01cdSopenharmony_ci{
128a69a01cdSopenharmony_ci    if (!CheckAbliltyFinished(std::static_pointer_cast<AbilityTree>(parent))) {
129a69a01cdSopenharmony_ci        return false;
130a69a01cdSopenharmony_ci    }
131a69a01cdSopenharmony_ci    for (auto child : parent->GetChildren()) {
132a69a01cdSopenharmony_ci        if (!CheckBundleFinished(std::static_pointer_cast<AbilityTree>(child))) {
133a69a01cdSopenharmony_ci            return false;
134a69a01cdSopenharmony_ci        }
135a69a01cdSopenharmony_ci    }
136a69a01cdSopenharmony_ci    return true;
137a69a01cdSopenharmony_ci}
138a69a01cdSopenharmony_ci
139a69a01cdSopenharmony_cibool CheckInputFinished(const std::shared_ptr<ComponentParam>& param)
140a69a01cdSopenharmony_ci{
141a69a01cdSopenharmony_ci    TRACK_LOG_STD();
142a69a01cdSopenharmony_ci    bool isFinished = false;
143a69a01cdSopenharmony_ci    auto currentAbilityPtr = TreeManager::GetInstance()->GetCurrentAbility();
144a69a01cdSopenharmony_ci    if (currentAbilityPtr == nullptr) {
145a69a01cdSopenharmony_ci        ERROR_LOG("GetCurrentAbility abilityNode is nullptr");
146a69a01cdSopenharmony_ci        return isFinished;
147a69a01cdSopenharmony_ci    }
148a69a01cdSopenharmony_ci
149a69a01cdSopenharmony_ci    // check app input event finished and set param is finished.
150a69a01cdSopenharmony_ci    if (CheckBundleFinished(WuKongTree::GetRoot(currentAbilityPtr))) {
151a69a01cdSopenharmony_ci        for (uint32_t i = 0; i < param->bundleRunning_.size(); i++) {
152a69a01cdSopenharmony_ci            if (param->bundleRunning_[i] && param->bundleName_[i] == currentAbilityPtr->GetBundleName()) {
153a69a01cdSopenharmony_ci                param->bundleFinish_[i] = true;
154a69a01cdSopenharmony_ci                isFinished = true;
155a69a01cdSopenharmony_ci                break;
156a69a01cdSopenharmony_ci            }
157a69a01cdSopenharmony_ci        }
158a69a01cdSopenharmony_ci    }
159a69a01cdSopenharmony_ci
160a69a01cdSopenharmony_ci    TRACK_LOG_END();
161a69a01cdSopenharmony_ci    return isFinished;
162a69a01cdSopenharmony_ci}
163a69a01cdSopenharmony_ci
164a69a01cdSopenharmony_ciErrCode JudgeBackResult(const std::shared_ptr<ComponentParam>& param, uint32_t launchIndex)
165a69a01cdSopenharmony_ci{
166a69a01cdSopenharmony_ci    TRACK_LOG_STD();
167a69a01cdSopenharmony_ci    ErrCode result;
168a69a01cdSopenharmony_ci    param->pageBack_[launchIndex]++;
169a69a01cdSopenharmony_ci    TRACK_LOG_STR("back count: %d", param->pageBack_[launchIndex]);
170a69a01cdSopenharmony_ci    if (param->pageBack_[launchIndex] > PAGE_BACK_COUNT_MAX) {
171a69a01cdSopenharmony_ci        result = LauncherApp(param->bundleName_[launchIndex]);
172a69a01cdSopenharmony_ci        if (result != OHOS::ERR_OK) {
173a69a01cdSopenharmony_ci            return result;
174a69a01cdSopenharmony_ci        }
175a69a01cdSopenharmony_ci        param->pageBack_[launchIndex] = 0;
176a69a01cdSopenharmony_ci        param->lanuchCount_[launchIndex]++;
177a69a01cdSopenharmony_ci        TRACK_LOG_STR("lanuchCount_[%d] = %d", launchIndex, param->lanuchCount_[launchIndex]);
178a69a01cdSopenharmony_ci        if (param->lanuchCount_[launchIndex] > LANUCH_APP_COUNT_MAX) {
179a69a01cdSopenharmony_ci            param->bundleFinish_[launchIndex] = true;
180a69a01cdSopenharmony_ci            ERROR_LOG("Failed to launch the app five times in a row and exit");
181a69a01cdSopenharmony_ci            param->lanuchCount_[launchIndex] = 0;
182a69a01cdSopenharmony_ci            return OHOS::ERR_INVALID_VALUE;
183a69a01cdSopenharmony_ci        }
184a69a01cdSopenharmony_ci    } else {
185a69a01cdSopenharmony_ci        result = ComponentManager::GetInstance()->BackToPrePage();
186a69a01cdSopenharmony_ci    }
187a69a01cdSopenharmony_ci    TRACK_LOG_END();
188a69a01cdSopenharmony_ci    return result;
189a69a01cdSopenharmony_ci}
190a69a01cdSopenharmony_ci}  // namespace
191a69a01cdSopenharmony_ciComponentInput::ComponentInput() : InputAction()
192a69a01cdSopenharmony_ci{
193a69a01cdSopenharmony_ci}
194a69a01cdSopenharmony_ci
195a69a01cdSopenharmony_ciComponentInput::~ComponentInput()
196a69a01cdSopenharmony_ci{
197a69a01cdSopenharmony_ci}
198a69a01cdSopenharmony_ci
199a69a01cdSopenharmony_ciErrCode ComponentInput::OrderInput(const std::shared_ptr<SpcialTestObject>& specialTestObject)
200a69a01cdSopenharmony_ci{
201a69a01cdSopenharmony_ci    auto componentPtr = std::static_pointer_cast<ComponentParam>(specialTestObject);
202a69a01cdSopenharmony_ci    if (componentPtr == nullptr) {
203a69a01cdSopenharmony_ci        ERROR_LOG("specialTestObject param is null");
204a69a01cdSopenharmony_ci        return OHOS::ERR_INVALID_VALUE;
205a69a01cdSopenharmony_ci    }
206a69a01cdSopenharmony_ci    // launch app and check if app has been started
207a69a01cdSopenharmony_ci    uint32_t launchIndex = CheckLauncherApp(componentPtr);
208a69a01cdSopenharmony_ci    if (launchIndex >= componentPtr->bundleName_.size()) {
209a69a01cdSopenharmony_ci        ERROR_LOG("launcher app failed, and stop run test");
210a69a01cdSopenharmony_ci        componentPtr->isAllFinished_ = true;
211a69a01cdSopenharmony_ci        ERROR_LOG(componentPtr->toString().c_str());
212a69a01cdSopenharmony_ci        return OHOS::ERR_INVALID_VALUE;
213a69a01cdSopenharmony_ci    }
214a69a01cdSopenharmony_ci    auto treemanager = TreeManager::GetInstance();
215a69a01cdSopenharmony_ci    auto delegate = SceneDelegate::GetInstance();
216a69a01cdSopenharmony_ci    PrepareApp();
217a69a01cdSopenharmony_ci    // update component information
218a69a01cdSopenharmony_ci    ErrCode result = treemanager->UpdateComponentInfo();
219a69a01cdSopenharmony_ci    DEBUG_LOG_STR("update componentinfo result (%d)", result);
220a69a01cdSopenharmony_ci    if (result == OHOS::ERR_OK) {
221a69a01cdSopenharmony_ci        // choose scene and set valid components
222a69a01cdSopenharmony_ci        result = delegate->ChooseScene(false);
223a69a01cdSopenharmony_ci        if (result != OHOS::ERR_OK) {
224a69a01cdSopenharmony_ci            ERROR_LOG("choose scene failed");
225a69a01cdSopenharmony_ci            return result;
226a69a01cdSopenharmony_ci        }
227a69a01cdSopenharmony_ci        // judge if is neccessnary to back to previous page
228a69a01cdSopenharmony_ci        if (delegate->IsBackToPrePage()) {
229a69a01cdSopenharmony_ci            result = JudgeBackResult(componentPtr, launchIndex);
230a69a01cdSopenharmony_ci            if (result != OHOS::ERR_OK) {
231a69a01cdSopenharmony_ci                return result;
232a69a01cdSopenharmony_ci            }
233a69a01cdSopenharmony_ci        } else {
234a69a01cdSopenharmony_ci            // get the component from tree manager to input action
235a69a01cdSopenharmony_ci            auto elementInfo = treemanager->GetElementInfoByOrder();
236a69a01cdSopenharmony_ci            if (elementInfo == nullptr) {
237a69a01cdSopenharmony_ci                ERROR_LOG("elementinfo is nullptr");
238a69a01cdSopenharmony_ci                return OHOS::ERR_INVALID_VALUE;
239a69a01cdSopenharmony_ci            }
240a69a01cdSopenharmony_ci            // get the actions of component
241a69a01cdSopenharmony_ci            int actionType = JudgeComponentType(*(elementInfo.get()));
242a69a01cdSopenharmony_ci            if (actionType == Accessibility::ACCESSIBILITY_ACTION_INVALID) {
243a69a01cdSopenharmony_ci                actionType = OHOS::Accessibility::ACCESSIBILITY_ACTION_CLICK;
244a69a01cdSopenharmony_ci            }
245a69a01cdSopenharmony_ci            // input action to component
246a69a01cdSopenharmony_ci            result = ComponentManager::GetInstance()->ComponentEventInput(*(elementInfo.get()), actionType);
247a69a01cdSopenharmony_ci            if (result == OHOS::ERR_OK) {
248a69a01cdSopenharmony_ci                // record index of inputted component
249a69a01cdSopenharmony_ci                treemanager->SetInputcomponentIndex(actionType);
250a69a01cdSopenharmony_ci                componentPtr->pageBack_[launchIndex] = 0;
251a69a01cdSopenharmony_ci                componentPtr->lanuchCount_[launchIndex] = 0;
252a69a01cdSopenharmony_ci                std::shared_ptr<ComponmentInputMsg> componentInputMsg = std::make_shared<ComponmentInputMsg>();
253a69a01cdSopenharmony_ci                componentInputMsg->pageComponments = delegate->GetComponentTypeList();
254a69a01cdSopenharmony_ci                componentInputMsg->pageId_ = delegate->GetCurrentPageId();
255a69a01cdSopenharmony_ci                componentInputMsg->componmentType_ = elementInfo->GetComponentType();
256a69a01cdSopenharmony_ci                Report::GetInstance()->SyncInputInfo(componentInputMsg);
257a69a01cdSopenharmony_ci            }
258a69a01cdSopenharmony_ci        }
259a69a01cdSopenharmony_ci    }
260a69a01cdSopenharmony_ci    // check current bundle finished state.
261a69a01cdSopenharmony_ci    if (CheckInputFinished(componentPtr)) {
262a69a01cdSopenharmony_ci        HandleAllBundleStatus(componentPtr);
263a69a01cdSopenharmony_ci    }
264a69a01cdSopenharmony_ci    DEBUG_LOG_STR("component order input result (%d)", result);
265a69a01cdSopenharmony_ci    return result;
266a69a01cdSopenharmony_ci}
267a69a01cdSopenharmony_ci
268a69a01cdSopenharmony_ciErrCode ComponentInput::RandomInput()
269a69a01cdSopenharmony_ci{
270a69a01cdSopenharmony_ci    auto treemanager = TreeManager::GetInstance();
271a69a01cdSopenharmony_ci    ErrCode result = treemanager->UpdateComponentInfo();
272a69a01cdSopenharmony_ci    DEBUG_LOG_STR("update componentinfo result (%d)", result);
273a69a01cdSopenharmony_ci    if (result == OHOS::ERR_OK) {
274a69a01cdSopenharmony_ci        auto delegate = SceneDelegate::GetInstance();
275a69a01cdSopenharmony_ci        delegate->ChooseScene(true);
276a69a01cdSopenharmony_ci        auto componentInfos = treemanager->GetActiveElementInfos();
277a69a01cdSopenharmony_ci        auto wComponentInfos = treemanager->GetActiveComponentInfos();
278a69a01cdSopenharmony_ci        DEBUG_LOG_STR("component list size (%d)", componentInfos.size());
279a69a01cdSopenharmony_ci        DEBUG_LOG_STR("back: %d", delegate->IsBackToPrePage());
280a69a01cdSopenharmony_ci        if (delegate->IsBackToPrePage()) {
281a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch1");
282a69a01cdSopenharmony_ci            result = ComponentManager::GetInstance()->BackToPrePage();
283a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch1 end");
284a69a01cdSopenharmony_ci        } else if (componentInfos.size() > 0) {
285a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch2");
286a69a01cdSopenharmony_ci            uint32_t index = (uint32_t)(rand()) % componentInfos.size();
287a69a01cdSopenharmony_ci            DEBUG_LOG_STR("component input index (%d), NodeId (%d)", index,
288a69a01cdSopenharmony_ci                wComponentInfos[index]->GetNodeId());
289a69a01cdSopenharmony_ci            int actionType = JudgeComponentType(*(componentInfos[index].get()));
290a69a01cdSopenharmony_ci            if (actionType == Accessibility::ACCESSIBILITY_ACTION_INVALID) {
291a69a01cdSopenharmony_ci                actionType = OHOS::Accessibility::ACCESSIBILITY_ACTION_CLICK;
292a69a01cdSopenharmony_ci            }
293a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch2 before input");
294a69a01cdSopenharmony_ci            result = ComponentManager::GetInstance()->ComponentEventInput(*(componentInfos[index].get()), actionType);
295a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch2 after input");
296a69a01cdSopenharmony_ci            if (result == OHOS::ERR_OK) {
297a69a01cdSopenharmony_ci                treemanager->SetInputcomponentIndex(actionType, index);
298a69a01cdSopenharmony_ci                std::shared_ptr<ComponmentInputMsg> componentInputMsg = std::make_shared<ComponmentInputMsg>();
299a69a01cdSopenharmony_ci                DEBUG_LOG("componentInputBranch2 after componentInputMsg");
300a69a01cdSopenharmony_ci                componentInputMsg->pageComponments = delegate->GetComponentTypeList();
301a69a01cdSopenharmony_ci                componentInputMsg->pageId_ = delegate->GetCurrentPageId();
302a69a01cdSopenharmony_ci                componentInputMsg->componmentType_ = componentInfos[index]->GetComponentType();
303a69a01cdSopenharmony_ci                DEBUG_LOG("componentInputBranch2 before SyncInputInfo");
304a69a01cdSopenharmony_ci                Report::GetInstance()->SyncInputInfo(componentInputMsg);
305a69a01cdSopenharmony_ci                DEBUG_LOG("componentInputBranch2 after SyncInputInfo");
306a69a01cdSopenharmony_ci            }
307a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch2 end");
308a69a01cdSopenharmony_ci        } else {
309a69a01cdSopenharmony_ci            DEBUG_LOG("componentInputBranch3");
310a69a01cdSopenharmony_ci            result = OHOS::ERR_NO_INIT;
311a69a01cdSopenharmony_ci        }
312a69a01cdSopenharmony_ci    }
313a69a01cdSopenharmony_ci    DEBUG_LOG_STR("component random input result (%d)", result);
314a69a01cdSopenharmony_ci    return result;
315a69a01cdSopenharmony_ci}
316a69a01cdSopenharmony_ci
317a69a01cdSopenharmony_ciErrCode ComponentInput::FocusInput(bool shouldScreenCap)
318a69a01cdSopenharmony_ci{
319a69a01cdSopenharmony_ci    auto treemanager = TreeManager::GetInstance();
320a69a01cdSopenharmony_ci    ErrCode result = treemanager->UpdateComponentInfo();
321a69a01cdSopenharmony_ci    auto componentManager = ComponentManager::GetInstance();
322a69a01cdSopenharmony_ci    DEBUG_LOG_STR("update componentinfo result (%d)", result);
323a69a01cdSopenharmony_ci    if (result == OHOS::ERR_OK) {
324a69a01cdSopenharmony_ci        auto delegate = FocusSceneDelegate::GetInstance();
325a69a01cdSopenharmony_ci        delegate->ChooseScene(true);
326a69a01cdSopenharmony_ci        auto componentInfos = treemanager->GetActiveElementInfos();
327a69a01cdSopenharmony_ci        auto wComponentInfos = treemanager->GetActiveComponentInfos();
328a69a01cdSopenharmony_ci        DEBUG_LOG_STR("component list size (%d), back : (%d)", componentInfos.size(), delegate->IsBackToPrePage());
329a69a01cdSopenharmony_ci        if (delegate->IsBackToPrePage()) {
330a69a01cdSopenharmony_ci            result = componentManager->BackToPrePage();
331a69a01cdSopenharmony_ci        } else if (componentInfos.size() > 0) {
332a69a01cdSopenharmony_ci            uint32_t index = ChooseRightComponentIndex(componentInfos, wComponentInfos, shouldScreenCap);
333a69a01cdSopenharmony_ci            DEBUG_LOG_STR("after ChooseRightComponentIndex (%d), comp cnt (%d)index (%d)",
334a69a01cdSopenharmony_ci                          componentInfos.size(), wComponentInfos.size(), index);
335a69a01cdSopenharmony_ci            auto componentinfo = componentInfos[index];
336a69a01cdSopenharmony_ci            DEBUG_LOG_STR("component input index (%d), NodeId (%d)", index,
337a69a01cdSopenharmony_ci                          wComponentInfos[index]->GetNodeId());
338a69a01cdSopenharmony_ci            int actionType = JudgeComponentType(*(componentinfo.get()));
339a69a01cdSopenharmony_ci            if (actionType == Accessibility::ACCESSIBILITY_ACTION_INVALID) {
340a69a01cdSopenharmony_ci                actionType = OHOS::Accessibility::ACCESSIBILITY_ACTION_CLICK;
341a69a01cdSopenharmony_ci            }
342a69a01cdSopenharmony_ci
343a69a01cdSopenharmony_ci            result = componentManager->ComponentEventInput(*(componentinfo.get()), actionType);
344a69a01cdSopenharmony_ci            DEBUG_LOG_STR("after ChooseRightComponentIndex3 (%d), index (%d)", componentInfos.size(), index);
345a69a01cdSopenharmony_ci            DEBUG_LOG_STR("action type: %d, sequence input: %d", actionType, result);
346a69a01cdSopenharmony_ci            if (result == OHOS::ERR_OK) {
347a69a01cdSopenharmony_ci                treemanager->SetInputcomponentIndex(actionType, index);
348a69a01cdSopenharmony_ci                std::shared_ptr<ComponmentInputMsg> componentInputMsg = std::make_shared<ComponmentInputMsg>();
349a69a01cdSopenharmony_ci                componentInputMsg->pageComponments = delegate->GetComponentTypeList();
350a69a01cdSopenharmony_ci                componentInputMsg->componmentType_ = componentinfo->GetComponentType();
351a69a01cdSopenharmony_ci                componentInputMsg->pagePath_ = componentinfo->GetPagePath();
352a69a01cdSopenharmony_ci                componentInputMsg->startX_ = componentManager->GetStartX();
353a69a01cdSopenharmony_ci                componentInputMsg->startY_ = componentManager->GetStartY();
354a69a01cdSopenharmony_ci                componentInputMsg->endX_ = componentManager->GetEndX();
355a69a01cdSopenharmony_ci                componentInputMsg->endY_ = componentManager->GetEndY();
356a69a01cdSopenharmony_ci                componentInputMsg->content_ = componentinfo->GetContent();
357a69a01cdSopenharmony_ci                componentInputMsg->pssTotal_ = WuKongUtil::GetInstance()->GetBundlePssTotal();
358a69a01cdSopenharmony_ci                TreeManager::GetInstance()->SetComponentType(componentinfo->GetComponentType());
359a69a01cdSopenharmony_ci                Report::GetInstance()->SyncInputInfo(componentInputMsg);
360a69a01cdSopenharmony_ci            }
361a69a01cdSopenharmony_ci        } else {
362a69a01cdSopenharmony_ci            ERROR_LOG("component list is null");
363a69a01cdSopenharmony_ci            result = OHOS::ERR_NO_INIT;
364a69a01cdSopenharmony_ci        }
365a69a01cdSopenharmony_ci    }
366a69a01cdSopenharmony_ci    DEBUG_LOG_STR("component focus input result (%d)", result);
367a69a01cdSopenharmony_ci    return result;
368a69a01cdSopenharmony_ci}
369a69a01cdSopenharmony_ci
370a69a01cdSopenharmony_ciuint32_t ComponentInput::ChooseRightComponentIndex(
371a69a01cdSopenharmony_ci    std::vector<std::shared_ptr<OHOS::Accessibility::AccessibilityElementInfo>> &componentInfos,
372a69a01cdSopenharmony_ci    std::vector<std::shared_ptr<ComponentTree>> &wComponentInfos, bool shouldScreenCap)
373a69a01cdSopenharmony_ci{
374a69a01cdSopenharmony_ci    auto treemanager = TreeManager::GetInstance();
375a69a01cdSopenharmony_ci    uint32_t index;
376a69a01cdSopenharmony_ci    if (treemanager->HasDialog()) {
377a69a01cdSopenharmony_ci        DEBUG_LOG("Has dialog");
378a69a01cdSopenharmony_ci        std::vector<uint32_t> indexList;
379a69a01cdSopenharmony_ci        int count = wComponentInfos.size();
380a69a01cdSopenharmony_ci        for (int i = 0; i < count; i++) {
381a69a01cdSopenharmony_ci            auto it = wComponentInfos[i];
382a69a01cdSopenharmony_ci            if (it->IsTopComponent()) {
383a69a01cdSopenharmony_ci                indexList.push_back(i);
384a69a01cdSopenharmony_ci            }
385a69a01cdSopenharmony_ci        }
386a69a01cdSopenharmony_ci        uint32_t randIndex = (std::uint32_t) rand() % indexList.size();
387a69a01cdSopenharmony_ci        index = indexList[randIndex];
388a69a01cdSopenharmony_ci    } else {
389a69a01cdSopenharmony_ci        DEBUG_LOG("no dialog");
390a69a01cdSopenharmony_ci        uint32_t randIndex = treemanager->FindInputComponentIndex(shouldScreenCap);
391a69a01cdSopenharmony_ci        DEBUG_LOG_STR("ComponentInput::FocusInput START: randomIndex: %u", randIndex);
392a69a01cdSopenharmony_ci        index = randIndex % componentInfos.size();
393a69a01cdSopenharmony_ci    }
394a69a01cdSopenharmony_ci    return index;
395a69a01cdSopenharmony_ci}
396a69a01cdSopenharmony_ci
397a69a01cdSopenharmony_ciInputType ComponentInput::GetInputInfo()
398a69a01cdSopenharmony_ci{
399a69a01cdSopenharmony_ci    return INPUTTYPE_ELEMENTINPUT;
400a69a01cdSopenharmony_ci}
401a69a01cdSopenharmony_ci
402a69a01cdSopenharmony_ciint ComponentInput::JudgeComponentType(OHOS::Accessibility::AccessibilityElementInfo& elementInfo)
403a69a01cdSopenharmony_ci{
404a69a01cdSopenharmony_ci    int actionType;
405a69a01cdSopenharmony_ci    TRACK_LOG_STD();
406a69a01cdSopenharmony_ci    // get action list of component
407a69a01cdSopenharmony_ci    std::vector<OHOS::Accessibility::AccessibleAction> actionlist = elementInfo.GetActionList();
408a69a01cdSopenharmony_ci    if (actionlist.empty()) {
409a69a01cdSopenharmony_ci        std::string componentType = elementInfo.GetComponentType();
410a69a01cdSopenharmony_ci        TRACK_LOG_STR("component type: %s", componentType.c_str());
411a69a01cdSopenharmony_ci        if (componentType == "TextInput" || componentType == "TextArea" || componentType == "Text") {
412a69a01cdSopenharmony_ci            actionType = Accessibility::ACCESSIBILITY_ACTION_SET_TEXT;
413a69a01cdSopenharmony_ci        } else if (componentType == "GridContainer") {
414a69a01cdSopenharmony_ci            actionType = Accessibility::ACCESSIBILITY_ACTION_SCROLL_FORWARD;
415a69a01cdSopenharmony_ci        } else if (componentType == "Slider") {
416a69a01cdSopenharmony_ci            actionType = COMPONENT_LEFT_SWAP;
417a69a01cdSopenharmony_ci        } else {
418a69a01cdSopenharmony_ci            actionType = Accessibility::ACCESSIBILITY_ACTION_CLICK;
419a69a01cdSopenharmony_ci        }
420a69a01cdSopenharmony_ci    } else {
421a69a01cdSopenharmony_ci        TRACK_LOG_STR("action list size: %u", actionlist.size());
422a69a01cdSopenharmony_ci        auto it = actionlist[(uint32_t)(rand()) % actionlist.size()];
423a69a01cdSopenharmony_ci        actionType = (int)it.GetActionType();
424a69a01cdSopenharmony_ci    }
425a69a01cdSopenharmony_ci    TRACK_LOG_STR("action type: %d", actionType);
426a69a01cdSopenharmony_ci    return actionType;
427a69a01cdSopenharmony_ci}
428a69a01cdSopenharmony_ci}  // namespace WuKong
429a69a01cdSopenharmony_ci}  // namespace OHOS
430