• 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 #include "find_widget.h"
16 using namespace std;
17 namespace OHOS::uitest {
FindWidget(UiDriver & driver,float x,float y)18     const Widget FindWidget(UiDriver &driver, float x, float y)
19     {
20         ApiCallErr err(NO_ERROR);
21         std::map<float, Widget> recv;
22         auto matcher = WidgetMatcherByCoord(x, y);
23         auto visitor = WidgetCollector(matcher, recv, Point(x, y));
24         driver.DfsTraverseTree(visitor);
25         return visitor.GetMaxDepWidget();
26     }
Describe() const27     std::string WidgetMatcherByCoord::Describe() const
28     {
29         return "Match widget by coordinates point";
30     }
Matches(const Widget & widget) const31     bool WidgetMatcherByCoord::Matches(const Widget &widget) const
32     {
33         if (widget.IsVisible()) {
34             Rect rect = widget.GetBounds();
35             if (x_ <= rect.right_ && x_ >= rect.left_ && y_ <= rect.bottom_ && y_ >= rect.top_) {
36                 return true;
37             }
38         }
39         return false;
40     }
41 
GetDept(const Widget & widget) const42     int32_t WidgetCollector::GetDept(const Widget &widget) const
43     {
44         return widget.GetHierarchy().length();
45     }
46 
Visit(const Widget & widget)47     void WidgetCollector::Visit(const Widget &widget)
48     {
49         if (matcher_.Matches(widget)) {
50             int32_t dept = GetDept(widget);
51             if (receiver_.size() == 0) {
52                 maxDep = dept;
53             } else {
54                 maxDep = max(maxDep, dept);
55             }
56             receiver_.insert(std::make_pair(dept, widget));
57         }
58     }
59 }