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