1 /* 2 * Copyright (c) 2023 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 "json.hpp" 17 #include "ui_model.h" 18 19 namespace OHOS::uitest { 20 using namespace std; 21 using namespace nlohmann; 22 GetMiddleStr(string_view str,size_t & index,string_view startStr,string_view endStr)23 static string_view GetMiddleStr(string_view str, size_t &index, string_view startStr, string_view endStr) 24 { 25 size_t ori = index; 26 auto begin = str.find(startStr, index); 27 if (begin != string::npos) { 28 index = begin + startStr.size(); 29 auto end = str.find(endStr, index); 30 if (end != string::npos) { 31 string_view result = str.substr(index, end - index); 32 index = end; 33 return result; 34 } 35 } 36 index = ori; 37 return ""; 38 } 39 DFSMarshalWidget(std::vector<Widget> & allWidget,int index,nlohmann::json & dom,const std::map<std::string,int> & widgetChildCountMap,std::map<std::string,int> & visitWidgetMap)40 static void DFSMarshalWidget(std::vector<Widget> &allWidget, int index, nlohmann::json &dom, 41 const std::map<std::string, int> &widgetChildCountMap, std::map<std::string, int> &visitWidgetMap) 42 { 43 auto attrData = json(); 44 allWidget.at(index).WrapperWidgetToJson(attrData); 45 auto childrenData = json::array(); 46 int childIndex = 0; 47 int childCount = 0; 48 int childVisit = 0; 49 auto hierarchy = allWidget.at(index).GetHierarchy(); 50 if (widgetChildCountMap.find(hierarchy) != widgetChildCountMap.cend()) { 51 childCount = widgetChildCountMap.at(hierarchy); 52 } 53 while (childVisit < childCount) { 54 auto tempChildHierarchy = WidgetHierarchyBuilder::GetChildHierarchy(hierarchy, childIndex); 55 ++childIndex; 56 if (visitWidgetMap.find(tempChildHierarchy) == visitWidgetMap.cend()) { 57 continue; 58 } 59 auto childWidIndex = visitWidgetMap.at(tempChildHierarchy); 60 if (!allWidget.at(childWidIndex).IsVisible()) { 61 ++childVisit; 62 continue; 63 } 64 auto childData = json(); 65 DFSMarshalWidget(allWidget, childWidIndex, childData, widgetChildCountMap, visitWidgetMap); 66 childrenData.emplace_back(childData); 67 ++childVisit; 68 } 69 dom["attributes"] = attrData; 70 dom["children"] = childrenData; 71 } 72 AddExtraAttrs(nlohmann::json & root,const map<int32_t,string_view> & elementTrees,size_t index)73 void DumpHandler::AddExtraAttrs(nlohmann::json &root, const map<int32_t, string_view> &elementTrees, size_t index) 74 { 75 auto windowIdValue = root["attributes"]["hostWindowId"].dump(); 76 auto windowId = atoi(windowIdValue.substr(1, windowIdValue.size() - 2).c_str()); 77 auto find = elementTrees.find(windowId); 78 auto elementTree = (find != elementTrees.end()) ? find->second : ""; 79 string_view nodeEndStr = "|->"; 80 auto accessibilityIdInfo = root["attributes"]["accessibilityId"].dump(); 81 auto accessibilityId = accessibilityIdInfo.substr(1, accessibilityIdInfo.size() - 2); 82 string_view accessibilityIdStr = "AccessibilityId: " + accessibilityId; 83 string_view nodeAttrStr = GetMiddleStr(elementTree, index, accessibilityIdStr, nodeEndStr); 84 auto extraAttrs = json(); 85 size_t nodeAttrTraverlIndex = 0; 86 auto backgroundColor = GetMiddleStr(nodeAttrStr, nodeAttrTraverlIndex, "BackgroundColor: ", "\n"); 87 auto content = GetMiddleStr(nodeAttrStr, nodeAttrTraverlIndex, "Content: ", "\n"); 88 auto fontColor = GetMiddleStr(nodeAttrStr, nodeAttrTraverlIndex, "FontColor: ", "\n"); 89 auto fontSize = GetMiddleStr(nodeAttrStr, nodeAttrTraverlIndex, "FontSize: ", "\n"); 90 if (!backgroundColor.empty()) { 91 extraAttrs["BackgroundColor"] = backgroundColor; 92 } 93 if (!content.empty()) { 94 extraAttrs["Content"] = move(content); 95 } 96 if (!fontColor.empty()) { 97 extraAttrs["FontColor"] = move(fontColor); 98 } 99 if (!fontSize.empty()) { 100 extraAttrs["FontSize"] = move(fontSize); 101 } 102 if (!extraAttrs.empty()) { 103 root["extraAttrs"] = extraAttrs; 104 } 105 auto &childrenData = root["children"]; 106 auto childCount = childrenData.size(); 107 for (size_t idx = 0; idx < childCount; idx++) { 108 auto &child = childrenData.at(idx); 109 AddExtraAttrs(child, elementTrees, index); 110 } 111 } 112 DumpWindowInfoToJson(vector<Widget> & allWidget,nlohmann::json & root)113 void DumpHandler::DumpWindowInfoToJson(vector<Widget> &allWidget, nlohmann::json &root) 114 { 115 std::map<std::string, int> visitWidgetMap; 116 std::map<std::string, int> widgetCountMap; 117 for (int i = 0; i < allWidget.size(); ++i) { 118 const Widget &wid = allWidget.at(i); 119 std::string hie = wid.GetHierarchy(); 120 visitWidgetMap.emplace(hie, i); 121 std::string parentHie = WidgetHierarchyBuilder::GetParentWidgetHierarchy(hie); 122 if (widgetCountMap.find(parentHie) == widgetCountMap.cend()) { 123 widgetCountMap[parentHie] = 1; 124 } else { 125 widgetCountMap[parentHie] = widgetCountMap[parentHie] + 1; 126 } 127 } 128 DFSMarshalWidget(allWidget, 0, root, widgetCountMap, visitWidgetMap); 129 } 130 } // namespace OHOS::uitest