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 auto selector = WidgetSelector(); 22 vector<std::unique_ptr<Widget>> rev; 23 driver.FindWidgets(selector, rev, err, true); 24 auto tree = driver.GetWidgetTree(); 25 std::map<float, Widget> recv; 26 auto matcher = WidgetMatcherByCoord(x, y); 27 auto visitor = WidgetCollector(matcher, recv); 28 tree->DfsTraverse(visitor); 29 return visitor.GetMaxDepWidget(); 30 } Describe() const31 std::string WidgetMatcherByCoord::Describe() const 32 { 33 return "Match widget by coordinates point"; 34 } Matches(const Widget & widget) const35 bool WidgetMatcherByCoord::Matches(const Widget &widget) const 36 { 37 Rect rect = widget.GetBounds(); 38 if (x_ <= rect.right_ && x_ >= rect.left_ && y_ <= rect.bottom_ && y_ >= rect.top_) { 39 return true; 40 } else { 41 return false; 42 } 43 } 44 GetDept(const Widget & widget) const45 int32_t WidgetCollector::GetDept(const Widget &widget) const 46 { 47 return widget.GetHierarchy().length(); 48 } 49 Visit(const Widget & widget)50 void WidgetCollector::Visit(const Widget &widget) 51 { 52 if (matcher_.Matches(widget)) { 53 int32_t dept = GetDept(widget); 54 if (receiver_.size() == 0) { 55 maxDep = dept; 56 } else { 57 maxDep = max(maxDep, dept); 58 } 59 receiver_.insert(std::make_pair(dept, widget)); 60 } 61 } 62 }