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 "ui_input.h" 17 18 namespace OHOS::uitest { 19 using namespace std; 20 std::string opt; 21 ApiCallErr exception_ = ApiCallErr(NO_ERROR); 22 CheckSwipeVelocityPps(UiOpArgs & args)23 void CheckSwipeVelocityPps(UiOpArgs& args) 24 { 25 if (args.swipeVelocityPps_ < args.minSwipeVelocityPps_ || args.swipeVelocityPps_ > args.maxSwipeVelocityPps_) { 26 std::cout << "The swipe velocity out of range, the default value will be used. \n" << std::endl; 27 args.swipeVelocityPps_ = args.defaultSwipeVelocityPps_; 28 } 29 } CheckParams(int32_t argc,size_t want)30 bool CheckParams(int32_t argc, size_t want) 31 { 32 if ((size_t)argc < want) { 33 std::cout << "Missing parameter. \n" << std::endl; 34 PrintInputMessage(); 35 return false; 36 } 37 return true; 38 } PrintInputMessage()39 void PrintInputMessage() 40 { 41 const std::string usage = 42 "USAGE : \n" 43 "help print uiInput usage\n" 44 "dircFling [velocity stepLength] direction ranges from 0,1,2,3 (left, right, up, down)\n" 45 "click/doubleClick/longClick <x> <y> click on the target coordinates\n" 46 "swipe/drag <from_x> <from_y> <to_x> <to_y> [velocity] velocity ranges from 200 to 40000, default 600\n" 47 "fling <from_x> <from_y> <to_x> <to_y> [velocity] velocity ranges from 200 to 40000, default 600\n" 48 "keyEvent <keyID/Back/Home/Power> inject keyEvent\n" 49 "keyEvent <keyID_0> <keyID_1> [keyID_2] keyID_2 default to None \n" 50 "inputText <x> <y> <text> inputText at the target coordinate point\n" 51 "text <text> input text at the location where is already focused\n"; 52 std::cout << usage << std::endl; 53 } ParameterRedundancy()54 bool ParameterRedundancy() 55 { 56 std::cout << " Too many parameters. \n" << std::endl; 57 PrintInputMessage(); 58 return EXIT_FAILURE; 59 } CreateFlingPoint(Point & to,Point & from,Point screenSize,Direction direction)60 static bool CreateFlingPoint(Point &to, Point &from, Point screenSize, Direction direction) 61 { 62 to = Point(screenSize.px_ / INDEX_TWO, screenSize.py_ / INDEX_TWO); 63 switch (direction) { 64 case TO_LEFT: 65 from.px_ = to.px_ - screenSize.px_ / INDEX_FOUR; 66 from.py_ = to.py_; 67 return true; 68 case TO_RIGHT: 69 from.px_ = to.px_ + screenSize.px_ / INDEX_FOUR; 70 from.py_ = to.py_; 71 return true; 72 case TO_UP: 73 from.px_ = to.px_; 74 from.py_ = to.py_ - screenSize.py_ / INDEX_FOUR; 75 return true; 76 case TO_DOWN: 77 from.px_ = to.px_; 78 from.py_ = to.py_ + screenSize.py_ / INDEX_FOUR; 79 return true; 80 default: 81 std::cout << "Illegal argument: " + std::to_string(direction) << std::endl; 82 PrintInputMessage(); 83 return false; 84 } 85 } GetPoints(Point & to,Point & from,int32_t argc,char * argv[])86 bool GetPoints(Point &to, Point &from, int32_t argc, char *argv[]) 87 { 88 auto from_x = atoi(argv[THREE]); 89 auto from_y = atoi(argv[FOUR]); 90 auto to_x = atoi(argv[FIVE]); 91 auto to_y = atoi(argv[SIX]); 92 if (from_x <= 0 || from_y <= 0 || to_x <= 0 || to_y <= 0) { 93 std::cout << "Please confirm that the coordinate values are correct. \n" << std::endl; 94 PrintInputMessage(); 95 return false; 96 } 97 from = Point(from_x, from_y); 98 to = Point(to_x, to_y); 99 return true; 100 } GetPoint(Point & point,int32_t argc,char * argv[])101 bool GetPoint(Point &point, int32_t argc, char *argv[]) 102 { 103 auto from_x = atoi(argv[THREE]); 104 auto from_y = atoi(argv[FOUR]); 105 if (from_x <= 0 || from_y <= 0) { 106 std::cout << "Please confirm that the coordinate values are correct. \n" << std::endl; 107 PrintInputMessage(); 108 return false; 109 } 110 point = Point(from_x, from_y); 111 return true; 112 } CheckStepLength(UiOpArgs & uiOpArgs,Point to,Point from,uint32_t stepLength)113 bool CheckStepLength(UiOpArgs &uiOpArgs, Point to, Point from, uint32_t stepLength) 114 { 115 const int32_t distanceX = to.px_ - from.px_; 116 const int32_t distanceY = to.py_ - from.py_; 117 const uint32_t distance = sqrt(distanceX * distanceX + distanceY * distanceY); 118 if (stepLength <= 0 || stepLength > distance) { 119 std::cout << "The stepLen is out of range" << std::endl; 120 return EXIT_FAILURE; 121 } else { 122 uiOpArgs.swipeStepsCounts_ = distance / stepLength; 123 return true; 124 } 125 } FlingActionInput(int32_t argc,char * argv[],UiDriver & driver,UiOpArgs uiOpArgs)126 int32_t FlingActionInput(int32_t argc, char *argv[], UiDriver &driver, UiOpArgs uiOpArgs) 127 { 128 TouchOp op = TouchOp::SWIPE; 129 Point screenSize; 130 Direction direction; 131 Point from; 132 Point to; 133 if (opt == "dircFling" && CheckParams(argc, INDEX_FOUR)) { 134 direction = (Direction)atoi(argv[THREE]); 135 screenSize = driver.GetDisplaySize(exception_); 136 if (!CreateFlingPoint(to, from, screenSize, direction)) { 137 return EXIT_FAILURE; 138 } 139 if ((size_t)argc >= INDEX_FIVE) { 140 uiOpArgs.swipeVelocityPps_ = atoi(argv[FOUR]); 141 } 142 if ((size_t)argc == INDEX_SIX) { 143 uiOpArgs.swipeStepsCounts_ = atoi(argv[FIVE]); 144 } else if ((size_t)argc > INDEX_SIX) { 145 return ParameterRedundancy(); 146 } 147 } else if (opt == "fling" && CheckParams(argc, INDEX_SEVEN)) { 148 if (!GetPoints(to, from, argc, argv)) { 149 return EXIT_FAILURE; 150 } 151 if ((size_t)argc == INDEX_EIGHT) { 152 uiOpArgs.swipeVelocityPps_ = (uint32_t)atoi(argv[SEVEN]); 153 } else if ((size_t)argc == INDEX_NINE) { 154 auto stepLength = (uint32_t)atoi(argv[EIGHT]); 155 if (!CheckStepLength(uiOpArgs, to, from, stepLength)) { 156 return EXIT_FAILURE; 157 } 158 } else if ((size_t)argc > INDEX_NINE) { 159 return ParameterRedundancy(); 160 } 161 } else { 162 return EXIT_FAILURE; 163 } 164 CheckSwipeVelocityPps(uiOpArgs); 165 auto touch = GenericSwipe(op, from, to); 166 driver.PerformTouch(touch, uiOpArgs, exception_); 167 std::cout << exception_.message_ << std::endl; 168 return EXIT_SUCCESS; 169 } SwipeActionInput(int32_t argc,char * argv[],UiDriver & driver,UiOpArgs uiOpArgs)170 int32_t SwipeActionInput(int32_t argc, char *argv[], UiDriver &driver, UiOpArgs uiOpArgs) 171 { 172 opt = argv[TWO]; 173 TouchOp op = TouchOp::SWIPE; 174 if (opt == "drag") { 175 op = TouchOp::DRAG; 176 } 177 Point from; 178 Point to; 179 if (CheckParams(argc, INDEX_SEVEN)) { 180 if (!GetPoints(to, from, argc, argv)) { 181 return EXIT_FAILURE; 182 } 183 if ((size_t)argc == INDEX_EIGHT) { 184 uiOpArgs.swipeVelocityPps_ = (uint32_t)atoi(argv[SEVEN]); 185 } else if ((size_t)argc > INDEX_EIGHT) { 186 return ParameterRedundancy(); 187 } 188 } else { 189 return EXIT_FAILURE; 190 } 191 CheckSwipeVelocityPps(uiOpArgs); 192 auto touch = GenericSwipe(op, from, to); 193 driver.PerformTouch(touch, uiOpArgs, exception_); 194 std::cout << exception_.message_ << std::endl; 195 return EXIT_SUCCESS; 196 } KeyEventActionInput(int32_t argc,char * argv[],UiDriver & driver,UiOpArgs uiOpArgs)197 int32_t KeyEventActionInput(int32_t argc, char *argv[], UiDriver &driver, UiOpArgs uiOpArgs) 198 { 199 std::string key = argv[THREE]; 200 if (key == "Home" && (size_t)argc == INDEX_FOUR) { 201 driver.TriggerKey(Home(), uiOpArgs, exception_); 202 } else if (key == "Back" && (size_t)argc == INDEX_FOUR) { 203 driver.TriggerKey(Back(), uiOpArgs, exception_); 204 } else if (key == "Power" && (size_t)argc == INDEX_FOUR) { 205 driver.TriggerKey(Power(), uiOpArgs, exception_); 206 } else if (atoi(argv[THREE]) != 0) { 207 int32_t codeZero_ = atoi(argv[THREE]); 208 int32_t codeOne_; 209 int32_t codeTwo_; 210 if ((size_t)argc == INDEX_FOUR) { 211 auto keyAction_ = AnonymousSingleKey(codeZero_); 212 driver.TriggerKey(keyAction_, uiOpArgs, exception_); 213 } else if ((size_t)argc == INDEX_FIVE || (size_t)argc == INDEX_SIX) { 214 codeOne_ = atoi(argv[FOUR]); 215 if ((size_t)argc == INDEX_SIX) { 216 codeTwo_ = atoi(argv[FIVE]); 217 } else { 218 codeTwo_ = KEYCODE_NONE; 219 } 220 auto keyAction_ = CombinedKeys(codeZero_, codeOne_, codeTwo_); 221 driver.TriggerKey(keyAction_, uiOpArgs, exception_); 222 } else { 223 return ParameterRedundancy(); 224 } 225 } else { 226 std::cout << "Invalid parameters. \n" << std::endl; 227 PrintInputMessage(); 228 return EXIT_FAILURE; 229 } 230 std::cout << exception_.message_ << std::endl; 231 return EXIT_SUCCESS; 232 } TextActionInput(int32_t argc,char * argv[],UiDriver & driver,UiOpArgs uiOpArgs)233 int32_t TextActionInput(int32_t argc, char *argv[], UiDriver &driver, UiOpArgs uiOpArgs) 234 { 235 if ((size_t)argc != INDEX_FOUR) { 236 std::cout << "The number of parameters is incorrect. \n" << std::endl; 237 PrintInputMessage(); 238 return EXIT_FAILURE; 239 } 240 auto text = argv[THREE]; 241 driver.InputText(text, exception_); 242 std::cout << exception_.message_ << std::endl; 243 return EXIT_SUCCESS; 244 } TextActionInputWithPoint(int32_t argc,char * argv[],UiDriver & driver,UiOpArgs uiOpArgs)245 int32_t TextActionInputWithPoint(int32_t argc, char *argv[], UiDriver &driver, UiOpArgs uiOpArgs) 246 { 247 if ((size_t)argc != INDEX_SIX) { 248 std::cout << "The number of parameters is incorrect. \n" << std::endl; 249 PrintInputMessage(); 250 return EXIT_FAILURE; 251 } 252 Point point; 253 if (!GetPoint(point, argc, argv)) { 254 return EXIT_FAILURE; 255 } 256 auto text = argv[FIVE]; 257 auto touch = GenericClick(TouchOp::CLICK, point); 258 driver.PerformTouch(touch, uiOpArgs, exception_); 259 static constexpr uint32_t focusTimeMs = 500; 260 driver.DelayMs(focusTimeMs); 261 driver.InputText(text, exception_); 262 std::cout << exception_.message_ << std::endl; 263 return EXIT_SUCCESS; 264 } ClickActionInput(int32_t argc,char * argv[],UiDriver & driver,UiOpArgs uiOpArgs)265 int32_t ClickActionInput(int32_t argc, char *argv[], UiDriver &driver, UiOpArgs uiOpArgs) 266 { 267 if ((size_t)argc != INDEX_FIVE) { 268 std::cout << "The number of parameters is incorrect. \n" << std::endl; 269 PrintInputMessage(); 270 return EXIT_FAILURE; 271 } 272 TouchOp op = TouchOp::CLICK; 273 if (opt == "doubleClick") { 274 op = TouchOp::DOUBLE_CLICK_P; 275 } else if (opt == "longClick") { 276 op = TouchOp::LONG_CLICK; 277 } 278 Point point; 279 if (!GetPoint(point, argc, argv)) { 280 return EXIT_FAILURE; 281 } 282 auto touch = GenericClick(op, point); 283 driver.PerformTouch(touch, uiOpArgs, exception_); 284 std::cout << exception_.message_ << std::endl; 285 return EXIT_SUCCESS; 286 } UiActionInput(int32_t argc,char * argv[])287 int32_t UiActionInput(int32_t argc, char *argv[]) 288 { 289 // 1 uitest 2 uiInput 3 TouchOp 4 5 from_point 6 7 to_point 8 velocity 290 auto driver = UiDriver(); 291 UiOpArgs uiOpArgs; 292 opt = argv[TWO]; 293 if (!CheckParams(argc, INDEX_FOUR)) { 294 return EXIT_FAILURE; 295 } 296 if (opt == "keyEvent") { 297 return KeyEventActionInput(argc, argv, driver, uiOpArgs); 298 } else if (opt == "fling" || opt == "dircFling") { 299 return FlingActionInput(argc, argv, driver, uiOpArgs); 300 } else if (opt == "swipe" || opt == "drag") { 301 return SwipeActionInput(argc, argv, driver, uiOpArgs); 302 } else if (opt == "click" || opt == "longClick" || opt == "doubleClick") { 303 return ClickActionInput(argc, argv, driver, uiOpArgs); 304 } else if (opt == "inputText") { 305 return TextActionInputWithPoint(argc, argv, driver, uiOpArgs); 306 } else if (opt == "text") { 307 return TextActionInput(argc, argv, driver, uiOpArgs); 308 } else { 309 std::cout << "Invalid parameters. \n" << std::endl; 310 PrintInputMessage(); 311 return EXIT_FAILURE; 312 } 313 } 314 } // namespace OHOS::uitest 315