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