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