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 "widget_image.h" 17 #include "ui_model.h" 18 19 namespace OHOS::uitest { 20 using namespace std; 21 using namespace nlohmann; 22 GetAttribute(string_view attrName,string_view defaultValue) const23 string WidgetImage::GetAttribute(string_view attrName, string_view defaultValue) const 24 { 25 auto item = attributes_.find(string(attrName)); 26 if (item == attributes_.end()) { 27 return string(defaultValue); 28 } 29 return item->second; 30 } 31 SetAttributes(const map<string,string> & source)32 void WidgetImage::SetAttributes(const map<string, string> &source) 33 { 34 for (auto&[attr, value]:source) { 35 this->attributes_[attr] = value; 36 } 37 } 38 SetSelectionDesc(string_view desc)39 void WidgetImage::SetSelectionDesc(string_view desc) 40 { 41 this->selectionCriteria_ = desc; 42 } 43 GetHierarchy() const44 string WidgetImage::GetHierarchy() const 45 { 46 return attributes_.find(string(ATTR_HIERARCHY))->second; 47 } 48 GetHashCode() const49 string WidgetImage::GetHashCode() const 50 { 51 auto entry = attributes_.find(string(ATTR_HASHCODE)); 52 return entry == attributes_.end() ? "" : entry->second; 53 } 54 GetSelectionDesc() const55 string WidgetImage::GetSelectionDesc() const 56 { 57 return selectionCriteria_; 58 } 59 Compare(const WidgetImage & other) const60 bool WidgetImage::Compare(const WidgetImage &other) const 61 { 62 auto hashcode = this->GetHashCode(); 63 if (!hashcode.empty() && this->GetHashCode() == other.GetHashCode()) { 64 return true; 65 } 66 for (auto&[attr, value]:attributes_) { 67 if (attr == ATTR_HASHCODE) { 68 continue; 69 } 70 auto entry = other.attributes_.find(attr); 71 if (entry == other.attributes_.end() || value != entry->second) { 72 return false; 73 } 74 } 75 return true; 76 } 77 WriteIntoParcel(json & data) const78 void WidgetImage::WriteIntoParcel(json &data) const 79 { 80 data["selection_criteria"] = selectionCriteria_; 81 for (auto&[attr, value]:attributes_) { 82 data[attr] = value; 83 } 84 } 85 ReadFromParcel(const json & data)86 void WidgetImage::ReadFromParcel(const json &data) 87 { 88 for (auto&[key, value] : data.items()) { 89 if (key == "selection_criteria") { 90 selectionCriteria_ = value; 91 } else { 92 attributes_[key] = value; 93 } 94 } 95 } 96 } 97 98