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         auto first_event_attr = firstTouchEventInfo.attributes;
38         if (!first_event_attr.empty()) {
39             data["W1_ID"] = first_event_attr[UiAttr::ID];
40             data["W1_Type"] = first_event_attr[UiAttr::TYPE];
41             data["W1_Text"] = first_event_attr[UiAttr::TEXT];
42             data["W1_BOUNDS"] = first_event_attr[UiAttr::BOUNDS];
43             data["W1_HIER"] = first_event_attr[UiAttr::HIERARCHY];
44         }
45         data["X2_POSI"] = lastTouchEventInfo.x != 0 ? std::to_string(lastTouchEventInfo.x) : "";
46         data["Y2_POSI"] = lastTouchEventInfo.y != 0 ? std::to_string(lastTouchEventInfo.y) : "";
47         auto last_event_attr = lastTouchEventInfo.attributes;
48         if (!last_event_attr.empty()) {
49             data["W2_ID"] = last_event_attr[UiAttr::ID];
50             data["W2_Type"] = last_event_attr[UiAttr::TYPE];
51             data["W2_Text"] = last_event_attr[UiAttr::TEXT];
52             data["W2_BOUNDS"] = last_event_attr[UiAttr::BOUNDS];
53             data["W2_HIER"] = last_event_attr[UiAttr::HIERARCHY];
54         }
55         data["LENGTH"] = std::to_string(stepLength);
56         data["MAX_VEL"] = std::to_string(MAX_VELOCITY);
57         data["VELO"] = std::to_string(velocity);
58         data["direction.X"] = std::to_string(direction.GetX());
59         data["direction.Y"] = std::to_string(direction.GetY());
60         return data;
61     }
62 
WriteWindowData(std::string actionType)63     std::string FingerInfo::WriteWindowData(std::string actionType)
64     {
65         std::stringstream sout;
66         auto first_event_attr = firstTouchEventInfo.attributes;
67         auto last_event_attr = lastTouchEventInfo.attributes;
68         if (actionType == "click" || actionType == "longClick" || actionType == "doubleClick") {
69             sout << actionType << ": " ;
70             if (first_event_attr.empty()) {
71                 sout <<" at Point(x:" << firstTouchEventInfo.x << ", y:" << firstTouchEventInfo.y << ") "<< "; ";
72             } else if (first_event_attr[UiAttr::ID] != "" || first_event_attr[UiAttr::TEXT] != "") {
73                 sout << " at Widget( id: " << first_event_attr[UiAttr::ID] << ", "
74                      << "text: " << first_event_attr[UiAttr::TEXT] << ", "
75                      << "type: " << first_event_attr[UiAttr::TYPE] << ") "<< "; ";
76             } else {
77                 sout <<" at Point(x:" << firstTouchEventInfo.x << ", y:" << firstTouchEventInfo.y << ") "<< "; ";
78             }
79         }
80         // fling swipe drag
81         if (first_event_attr.empty()) {
82             sout << "from Point(x:" << firstTouchEventInfo.x << ", y:" << firstTouchEventInfo.y << ") ";
83         } else if (first_event_attr[UiAttr::ID] != "" || first_event_attr[UiAttr::TEXT] != "") {
84             sout << "from Widget(id: " << first_event_attr[UiAttr::ID] << ", "
85                     << "type: " << first_event_attr[UiAttr::TYPE] << ", "
86                     << "text: " << first_event_attr[UiAttr::TEXT] << ") " << "; ";
87         } else {
88             sout << "from Point(x:" << firstTouchEventInfo.x << ", y:" << firstTouchEventInfo.y << ") ";
89         }
90         if (last_event_attr.empty()) {
91             sout << " to Point(x:" << lastTouchEventInfo.x << ", y:" << lastTouchEventInfo.y << ") " << "; ";
92         } else if (last_event_attr[UiAttr::ID] != "" || last_event_attr[UiAttr::TEXT] != "") {
93             sout << "to Widget(id: " << last_event_attr[UiAttr::ID] << ", "
94                     << "type: " << last_event_attr[UiAttr::TYPE] << ", "
95                     << "text: " << last_event_attr[UiAttr::TEXT] << ") " << "; ";
96         } else {
97             sout << " to Point(x:" << lastTouchEventInfo.x << ", y:" << lastTouchEventInfo.y << ") " << "; ";
98         }
99         if (actionType == "fling" || actionType == "swipe") {
100             sout << "Off-hand speed:" << velocity << ", " << "Step length:" << stepLength << ";";
101         }
102         return sout.str();
103     }
104 
WriteData()105     nlohmann::json PointerInfo::WriteData()
106     {
107         auto data = nlohmann::json();
108         data["EVENT_TYPE"] = EVENT_TYPE;
109         data["OP_TYPE"] = OP_TYPE[touchOpt];
110         data["fingerNumber"] = std::to_string(fingers.size());
111         data["duration"] = duration;
112 
113         data["BUNDLE"] = bundleName;
114         data["ABILITY"] = abilityName;
115         data["direction.X"] = std::to_string(avgDirection.GetX());
116         data["direction.Y"] = std::to_string(avgDirection.GetY());
117         data["LENGTH"] = std::to_string(avgStepLength);
118         data["VELO"] = std::to_string(avgVelocity);
119         data["CENTER_X"] = centerSet.px_ == 0.0 ? "" : std::to_string(centerSet.px_);
120         data["CENTER_Y"] = centerSet.py_ == 0.0 ? "" : std::to_string(centerSet.py_);
121 
122         auto fingerjson = nlohmann::json();
123         for (auto& finger :fingers) {
124             fingerjson.push_back(finger.WriteData());
125         }
126         data["fingerList"] = fingerjson;
127         return data;
128     }
129 
WriteWindowData()130     std::string PointerInfo::WriteWindowData()
131     {
132         std::stringstream sout;
133         std::string actionType = OP_TYPE[touchOpt];
134         sout << actionType << " , "
135              << "fingerNumber:" << std::to_string(fingers.size()) << " , " ;
136         int i = 1;
137         for (auto& finger :fingers) {
138             sout << std::endl;
139             sout << "\t" << "finger" << i << ":" << finger.WriteWindowData(actionType) ;
140             i++;
141         }
142         return sout.str();
143     }
144 }