1 /* 2 * Copyright (c) 2021-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 16 #include <algorithm> 17 #include "ui_model.h" 18 19 namespace OHOS::uitest { 20 using namespace std; 21 using namespace nlohmann; 22 SetBounds(const Rect & bounds)23 void Widget::SetBounds(const Rect &bounds) 24 { 25 bounds_ = bounds; 26 } 27 ToStr() const28 string Widget::ToStr() const 29 { 30 stringstream os; 31 os << "Widget{"; 32 for (int i = 0; i < UiAttr::MAX; i++) { 33 os << ATTR_NAMES[i] << "='" << attributeVec_[i] << "',"; 34 } 35 os << "}"; 36 return os.str(); 37 } 38 Clone(string_view hierarchy) const39 unique_ptr<Widget> Widget::Clone(string_view hierarchy) const 40 { 41 auto clone = make_unique<Widget>(hierarchy); 42 clone->bounds_ = this->bounds_; 43 clone->attributeVec_ = attributeVec_; 44 return clone; 45 } 46 GetAttrVec() const47 std::vector<std::string> Widget::GetAttrVec() const 48 { 49 std::vector<std::string> retVec = attributeVec_; 50 if (attributeVec_[UiAttr::BOUNDS].empty()) { 51 stringstream boundStream; 52 boundStream << "[" << bounds_.left_ << "," << bounds_.top_ << "][" << bounds_.right_ << "," 53 << bounds_.bottom_ << "]"; 54 retVec[UiAttr::BOUNDS] = boundStream.str(); 55 } 56 return retVec; 57 } 58 SetAttr(UiAttr attrId,string value)59 void Widget::SetAttr(UiAttr attrId, string value) 60 { 61 if (attrId >= UiAttr::MAX) { 62 LOG_E("Error attrId %{public}d, check it", attrId); 63 return; 64 } 65 attributeVec_[attrId] = value; 66 } 67 GetAttr(UiAttr attrId) const68 std::string Widget::GetAttr(UiAttr attrId) const 69 { 70 if (attrId >= UiAttr::MAX) { 71 return "none"; 72 } 73 if (attrId == UiAttr::BOUNDS && attributeVec_[UiAttr::BOUNDS].empty()) { 74 stringstream boundStream; 75 boundStream << "[" << bounds_.left_ << "," << bounds_.top_ << "][" << bounds_.right_ << "," 76 << bounds_.bottom_ << "]"; 77 return boundStream.str(); 78 } 79 return attributeVec_[attrId]; 80 } 81 MatchAttr(const WidgetMatchModel & matchModel) const82 bool Widget::MatchAttr(const WidgetMatchModel& matchModel) const 83 { 84 UiAttr attr = matchModel.attrName; 85 std::string_view value = matchModel.attrValue; 86 ValueMatchPattern pattern = matchModel.pattern; 87 std::string_view attrValue = attributeVec_[attr]; 88 switch (pattern) { 89 case ValueMatchPattern::EQ: 90 return attrValue == value; 91 case ValueMatchPattern::CONTAINS: 92 return attrValue.find(value) != std::string_view::npos; 93 case ValueMatchPattern::STARTS_WITH: 94 return attrValue.rfind(value) != std::string_view::npos; 95 case ValueMatchPattern::ENDS_WITH: 96 if (attrValue.length() < value.length()) { 97 return false; 98 } 99 return attrValue.substr(attrValue.length() - value.length()) == value; 100 default: 101 break; 102 } 103 return false; 104 } 105 SetHierarchy(const std::string & hierarch)106 void Widget::SetHierarchy(const std::string &hierarch) 107 { 108 hierarchy_ = hierarch; 109 attributeVec_[UiAttr::HIERARCHY] = hierarch; 110 } 111 WrapperWidgetToJson(nlohmann::json & out)112 void Widget::WrapperWidgetToJson(nlohmann::json &out) 113 { 114 for (int i = 0; i < UiAttr::HIERARCHY; ++i) { 115 if (i == UiAttr::BOUNDS && attributeVec_[UiAttr::BOUNDS].empty()) { 116 stringstream boundStream; 117 boundStream << "[" << bounds_.left_ << "," << bounds_.top_ << "][" << bounds_.right_ << "," 118 << bounds_.bottom_ << "]"; 119 attributeVec_[i] = boundStream.str(); 120 } 121 out[ATTR_NAMES[i].data()] = attributeVec_[i]; 122 } 123 out[ATTR_NAMES[UiAttr::VISIBLE].data()] = attributeVec_[UiAttr::VISIBLE]; 124 out[ATTR_NAMES[UiAttr::HASHCODE].data()] = attributeVec_[UiAttr::HASHCODE]; 125 out[ATTR_NAMES[UiAttr::HINT].data()] = attributeVec_[UiAttr::HINT]; 126 } 127 Build(string_view parentWidgetHierarchy,uint32_t childIndex)128 string WidgetHierarchyBuilder::Build(string_view parentWidgetHierarchy, uint32_t childIndex) 129 { 130 return string(parentWidgetHierarchy) + string(HIERARCHY_SEPARATOR) + to_string(childIndex); 131 } 132 GetParentWidgetHierarchy(string_view hierarchy)133 string WidgetHierarchyBuilder::GetParentWidgetHierarchy(string_view hierarchy) 134 { 135 if (hierarchy == ROOT_HIERARCHY) { 136 // no parent for root widget 137 return ""; 138 } 139 140 auto findRoot = hierarchy.find(ROOT_HIERARCHY); 141 if (findRoot != 0) { 142 // invalid hierarchy string 143 return ""; 144 } 145 auto findLastSeparator = hierarchy.find_last_of(HIERARCHY_SEPARATOR); 146 if (findLastSeparator <= 0 || findLastSeparator == string::npos) { 147 return ""; 148 } 149 return string(hierarchy).substr(0, findLastSeparator); 150 } 151 GetChildHierarchy(string_view hierarchy,uint32_t childIndex)152 string WidgetHierarchyBuilder::GetChildHierarchy(string_view hierarchy, uint32_t childIndex) 153 { 154 if (hierarchy.find(ROOT_HIERARCHY) != 0) { 155 // invalid hierarchy string 156 return ""; 157 } 158 return string(hierarchy) + string(HIERARCHY_SEPARATOR) + to_string(childIndex); 159 } 160 } // namespace OHOS::uitest 161