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 "pointer_info.h" 17 18 namespace OHOS::uitest { 19 const std::string PointerInfo::EVENT_TYPE = "pointer"; 20 std::map <int32_t, std::string> PointerInfo::OP_TYPE = { 21 {TouchOpt::OP_CLICK, "click"}, 22 {TouchOpt::OP_LONG_CLICK, "longClick"}, 23 {TouchOpt::OP_DOUBLE_CLICK, "doubleClick"}, 24 {TouchOpt::OP_SWIPE, "swipe"}, 25 {TouchOpt::OP_DRAG, "drag"}, 26 {TouchOpt::OP_FLING, "fling"}, 27 {TouchOpt::OP_PINCH, "pinch"}, 28 {TouchOpt::OP_HOME, "home"}, 29 {TouchOpt::OP_RECENT, "recent"}, 30 {TouchOpt::OP_RETURN, "back"}, 31 }; WriteData()32 nlohmann::json FingerInfo::WriteData() 33 { 34 auto data = nlohmann::json(); 35 data["X_POSI"] = std::to_string(firstTouchEventInfo.x); 36 data["Y_POSI"] = std::to_string(firstTouchEventInfo.y); 37 data["W1_ID"] = firstTouchEventInfo.attributes.count("id")? 38 firstTouchEventInfo.attributes.find("id")->second : ""; 39 data["W1_Type"] = firstTouchEventInfo.attributes.count("type") ? 40 firstTouchEventInfo.attributes.find("type")->second : ""; 41 data["W1_Text"] = firstTouchEventInfo.attributes.count("text") ? 42 firstTouchEventInfo.attributes.find("text")->second : ""; 43 data["W1_BOUNDS"] = firstTouchEventInfo.attributes.count("bounds") ? 44 firstTouchEventInfo.attributes.find("bounds")->second : ""; 45 data["W1_HIER"] = firstTouchEventInfo.attributes.count("hierarchy") ? 46 firstTouchEventInfo.attributes.find("hierarchy")->second : ""; 47 48 data["X2_POSI"] = lastTouchEventInfo.x != 0 ? std::to_string(lastTouchEventInfo.x) : ""; 49 data["Y2_POSI"] = lastTouchEventInfo.y != 0 ? std::to_string(lastTouchEventInfo.y) : ""; 50 data["W2_ID"] = lastTouchEventInfo.attributes.count("id")? 51 lastTouchEventInfo.attributes.find("id")->second : ""; 52 data["W2_Type"] = lastTouchEventInfo.attributes.count("type") ? 53 lastTouchEventInfo.attributes.find("type")->second : ""; 54 data["W2_Text"] = lastTouchEventInfo.attributes.count("text") ? 55 lastTouchEventInfo.attributes.find("text")->second : ""; 56 data["W2_BOUNDS"] = lastTouchEventInfo.attributes.count("bounds") ? 57 lastTouchEventInfo.attributes.find("bounds")->second : ""; 58 data["W2_HIER"] = lastTouchEventInfo.attributes.count("hierarchy") ? 59 lastTouchEventInfo.attributes.find("hierarchy")->second : ""; 60 61 data["LENGTH"] = std::to_string(stepLength); 62 data["MAX_VEL"] = std::to_string(MAX_VELOCITY); 63 data["VELO"] = std::to_string(velocity); 64 data["direction.X"] = std::to_string(direction.GetX()); 65 data["direction.Y"] = std::to_string(direction.GetY()); 66 return data; 67 } 68 WriteWindowData(std::string actionType)69 std::string FingerInfo::WriteWindowData(std::string actionType) 70 { 71 std::stringstream sout; 72 if (actionType == "fling" || actionType == "swipe" || actionType == "drag") { 73 if (firstTouchEventInfo.attributes.find("id")->second != "" || 74 firstTouchEventInfo.attributes.find("text")->second != "") { 75 sout << "from Widget(id: " << firstTouchEventInfo.attributes.find("id")->second << ", " 76 << "type: " << firstTouchEventInfo.attributes.find("type")->second << ", " 77 << "text: " << firstTouchEventInfo.attributes.find("text")->second << ") " << "; "; 78 } else { 79 sout << "from Point(x:" << firstTouchEventInfo.x << ", y:" << firstTouchEventInfo.y << ") "; 80 } 81 if (lastTouchEventInfo.attributes.find("id")->second != "" || 82 lastTouchEventInfo.attributes.find("text")->second != "") { 83 sout << "to Widget(id: " << lastTouchEventInfo.attributes.find("id")->second << ", " 84 << "type: " << lastTouchEventInfo.attributes.find("type")->second << ", " 85 << "text: " << lastTouchEventInfo.attributes.find("text")->second << ") " << "; "; 86 } else { 87 sout << " to Point(x:" << lastTouchEventInfo.x << ", y:" << lastTouchEventInfo.y << ") " << "; "; 88 } 89 if (actionType == "fling" || actionType == "swipe") { 90 sout << "Off-hand speed:" << velocity << ", " 91 << "Step length:" << stepLength << ";"; 92 } 93 } else if (actionType == "click" || actionType == "longClick" || actionType == "doubleClick") { 94 sout << actionType << ": " ; 95 if (firstTouchEventInfo.attributes.find("id")->second != "" || 96 firstTouchEventInfo.attributes.find("text")->second != "") { 97 sout << " at Widget( id: " << firstTouchEventInfo.attributes.find("id")->second << ", " 98 << "text: " << firstTouchEventInfo.attributes.find("text")->second << ", " 99 << "type: " << firstTouchEventInfo.attributes.find("type")->second<< ") "<< "; "; 100 } else { 101 sout <<" at Point(x:" << firstTouchEventInfo.x << ", y:" << firstTouchEventInfo.y << ") "<< "; "; 102 } 103 } 104 return sout.str(); 105 } 106 WriteData()107 nlohmann::json PointerInfo::WriteData() 108 { 109 auto data = nlohmann::json(); 110 data["EVENT_TYPE"] = EVENT_TYPE; 111 data["OP_TYPE"] = OP_TYPE[touchOpt]; 112 data["fingerNumber"] = std::to_string(fingers.size()); 113 data["duration"] = duration; 114 115 data["BUNDLE"] = bundleName; 116 data["ABILITY"] = abilityName; 117 data["direction.X"] = std::to_string(avgDirection.GetX()); 118 data["direction.Y"] = std::to_string(avgDirection.GetY()); 119 data["LENGTH"] = std::to_string(avgStepLength); 120 data["VELO"] = std::to_string(avgVelocity); 121 data["CENTER_X"] = centerSet.px_==0.0 && centerSet.px_ == 0.0 ? "" :std::to_string(centerSet.px_); 122 data["CENTER_Y"] = centerSet.px_==0.0 && centerSet.px_ == 0.0 ? "" :std::to_string(centerSet.px_); 123 124 auto fingerjson = nlohmann::json(); 125 for (auto& finger :fingers) { 126 fingerjson.push_back(finger.WriteData()); 127 } 128 data["fingerList"] = fingerjson; 129 return data; 130 } 131 WriteWindowData()132 std::string PointerInfo::WriteWindowData() 133 { 134 std::stringstream sout; 135 std::string actionType = OP_TYPE[touchOpt]; 136 sout << actionType << " , " 137 << "fingerNumber:" << std::to_string(fingers.size()) << " , " ; 138 int i = 1; 139 for (auto& finger :fingers) { 140 sout << std::endl; 141 sout << "\t" << "finger" << i << ":" << finger.WriteWindowData(actionType) ; 142 i++; 143 } 144 return sout.str(); 145 } 146 }