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#include "find_widget.h"
16using namespace std;
17
18namespace OHOS::uitest {
19    std::vector<std::string> FindWidget(UiDriver &driver, float x, float y)
20    {
21        ApiCallErr err(NO_ERROR);
22        std::vector<std::unique_ptr<Widget>> rec;
23        WidgetSelector selector{};
24        selector.SetWantMulti(true);
25        driver.FindWidgets(selector, rec, err, true);
26        if (err.code_ != NO_ERROR) {
27            return {};
28        }
29        int maxDep = 0;
30        int maxIndex = -1;
31        for (size_t index = 0; index < rec.size(); index++) {
32            const auto &rect = rec[index]->GetBounds();
33            if (!(x <= rect.right_ && x >= rect.left_ && y <= rect.bottom_ && y >= rect.top_)) {
34                continue;
35            }
36            int curDep = rec[index]->GetHierarchy().length();
37            if (curDep > maxDep) {
38                maxDep = curDep;
39                maxIndex = index;
40            }
41        }
42        if (maxIndex > -1) {
43            return std::move(rec[maxIndex])->GetAttrVec();
44        } else {
45            return {};
46        }
47    }
48} // namespace OHOS::uitest