• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FRONTEND_API_DEFINES_H
17 #define FRONTEND_API_DEFINES_H
18 
19 #include <initializer_list>
20 #include <string_view>
21 #include <map>
22 #include "json.hpp"
23 
24 namespace OHOS::uitest {
25     enum ErrCode : uint32_t {
26         /**Old ErrorCode*/
27         NO_ERROR = 0,
28         /**Internal error, not expected to happen.*/
29         INTERNAL_ERROR = 1,
30         /**Widget that is expected to be exist lost.*/
31         WIDGET_LOST = 2,
32         /**Window that is expected to be exist lost.*/
33         WINDOW_LOST = 3,
34         /**The user assertion failure.*/
35         ASSERTION_FAILURE = 4,
36         USAGE_ERROR = 5,
37         /**New ErrorCode*/
38         /**Initialize failed.*/
39         ERR_INITIALIZE_FAILED = 17000001,
40         /**API does not allow calling concurrently.*/
41         ERR_API_USAGE = 17000002,
42         /**Component existence assertion failed.*/
43         ERR_ASSERTION_FAILED = 17000003,
44         /**Component lost/UiWindow lost.*/
45         ERR_COMPONENT_LOST = 17000004,
46         /**This operation is not supported.*/
47         ERR_OPERATION_UNSUPPORTED = 17000005,
48         /**Internal error.*/
49         ERR_INTERNAL = 17000006,
50         /**Invalid input parameter.*/
51         ERR_INVALID_INPUT = 401,
52         /**The specified SystemCapability name was not found.*/
53         ERR_NO_SYSTEM_CAPABILITY = 801,
54     };
55 
56     const std::map<ErrCode, std::string> ErrDescMap = {
57         /**Correspondence between error codes and descriptions*/
58         {NO_ERROR, "No Error"},
59         {ERR_INITIALIZE_FAILED, "Initialize failed."},
60         {ERR_API_USAGE, "API does not allow calling concurrently."},
61         {ERR_ASSERTION_FAILED, "Component existence assertion failed."},
62         {ERR_COMPONENT_LOST, "Component lost/UiWindow lost."},
63         {ERR_OPERATION_UNSUPPORTED, "This operation is not supported."},
64         {ERR_INTERNAL, "Internal error."},
65         {ERR_NO_SYSTEM_CAPABILITY, "The specified SystemCapability name was not found."},
66         {ERR_INVALID_INPUT, "Invalid input parameter."},
67     };
68 
69     /**API invocation error detail wrapper.*/
70     struct ApiCallErr {
71     public:
72         ErrCode code_;
73         std::string message_ = "";
74 
75         ApiCallErr() = delete;
76 
ApiCallErrApiCallErr77         explicit ApiCallErr(ErrCode ec)
78         {
79             code_ = ec;
80             message_ = ErrDescMap.find(ec)->second;
81         }
82 
ApiCallErrApiCallErr83         ApiCallErr(ErrCode ec, std::string_view msg)
84         {
85             code_ = ec;
86             message_ = std::string(msg);
87         }
88     };
89 
90     /**Structure wraps the api-call data.*/
91     struct ApiCallInfo {
92         std::string apiId_;
93         std::string callerObjRef_;
94         nlohmann::json paramList_ = nlohmann::json::array();
95         int32_t fdParamIndex_ = -1; // support fd as param
96     };
97 
98     /**Structure wraps the api-call reply.*/
99     struct ApiReplyInfo {
100         nlohmann::json resultValue_ = nullptr;
101         ApiCallErr exception_ = ApiCallErr(NO_ERROR);
102     };
103 
104     /** Specifications of a frontend enumerator value.*/
105     struct FrontendEnumValueDef {
106         std::string_view name_;
107         std::string_view valueJson_;
108     };
109 
110     /** Specifications of a frontend enumerator.*/
111     struct FrontendEnumeratorDef {
112         std::string_view name_;
113         const FrontendEnumValueDef *values_;
114         size_t valueCount_;
115     };
116 
117     /** Specifications of a frontend json data property.*/
118     struct FrontEndJsonPropDef {
119         std::string_view name_;
120         std::string_view type_;
121         bool required_;
122     };
123     /** Specifications of a frontend json object.*/
124     struct FrontEndJsonDef {
125         std::string_view name_;
126         const FrontEndJsonPropDef *props_;
127         size_t propCount_;
128     };
129 
130     /** Specifications of a frontend class method.*/
131     struct FrontendMethodDef {
132         std::string_view name_;
133         std::string_view signature_;
134         bool static_;
135         bool fast_;
136     };
137 
138     /** Specifications of a frontend class.*/
139     struct FrontEndClassDef {
140         std::string_view name_;
141         const FrontendMethodDef *methods_;
142         size_t methodCount_;
143         bool bindUiDriver_;
144     };
145 
146     /** MatchPattern enumerator definition.*/
147     constexpr FrontendEnumValueDef PATTERN_VALUES[] = {
148         {"EQUALS", "0"},
149         {"CONTAINS", "1"},
150         {"STARTS_WITH", "2"},
151         {"ENDS_WITH", "3"},
152     };
153     constexpr FrontendEnumeratorDef MATCH_PATTERN_DEF = {
154         "MatchPattern",
155         PATTERN_VALUES,
156         sizeof(PATTERN_VALUES) / sizeof(FrontendEnumValueDef),
157     };
158 
159     /** Window ResizeDirection enumerator definition.*/
160     constexpr FrontendEnumValueDef RESIZE_DIRECTION_VALUES[] = {
161         {"LEFT", "0"},    {"RIGHT", "1"},     {"UP", "2"},       {"DOWN", "3"},
162         {"LEFT_UP", "4"}, {"LEFT_DOWN", "5"}, {"RIGHT_UP", "6"}, {"RIGHT_DOWN", "7"},
163     };
164     constexpr FrontendEnumeratorDef RESIZE_DIRECTION_DEF = {
165         "ResizeDirection",
166         RESIZE_DIRECTION_VALUES,
167         sizeof(RESIZE_DIRECTION_VALUES) / sizeof(FrontendEnumValueDef),
168     };
169 
170     /** WindowMode enumerator definition.*/
171     constexpr FrontendEnumValueDef WINDOW_MODE_VALUES[] = {
172         {"FULLSCREEN", "0"},
173         {"PRIMARY", "1"},
174         {"SECONDARY", "2"},
175         {"FLOATING", "3"},
176     };
177     constexpr FrontendEnumeratorDef WINDOW_MODE_DEF = {
178         "WindowMode",
179         WINDOW_MODE_VALUES,
180         sizeof(WINDOW_MODE_VALUES) / sizeof(FrontendEnumValueDef),
181     };
182 
183     /** Describes the rotation of the device display.*/
184     constexpr FrontendEnumValueDef DISPLAY_ROTATION_VALUES[] = {
185         {"ROTATION_0", "0"},
186         {"ROTATION_90", "1"},
187         {"ROTATION_180", "2"},
188         {"ROTATION_270", "3"},
189     };
190     constexpr FrontendEnumeratorDef DISPLAY_ROTATION_DEF = {
191         "DisplayRotation",
192         DISPLAY_ROTATION_VALUES,
193         sizeof(DISPLAY_ROTATION_VALUES) / sizeof(FrontendEnumValueDef),
194     };
195 
196     /** Rect jsonObject definition.*/
197     constexpr FrontEndJsonPropDef RECT_PROPERTIES[] = {
198         {"left", "int", true},
199         {"top", "int", true},
200         {"right", "int", true},
201         {"bottom", "int", true},
202     };
203     constexpr FrontEndJsonDef RECT_DEF = {
204         "Rect",
205         RECT_PROPERTIES,
206         sizeof(RECT_PROPERTIES) / sizeof(FrontEndJsonPropDef),
207     };
208 
209     /** Point jsonObject definition.*/
210     constexpr FrontEndJsonPropDef POINT_PROPERTIES[] = {
211         {"x", "int", true},
212         {"y", "int", true},
213     };
214     constexpr FrontEndJsonDef POINT_DEF = {
215         "Point",
216         POINT_PROPERTIES,
217         sizeof(POINT_PROPERTIES) / sizeof(FrontEndJsonPropDef),
218     };
219 
220     /** WindowFilter jsonObject definition.*/
221     constexpr FrontEndJsonPropDef WINDOW_FILTER_PROPERTIES[] = {
222         {"bundleName", "string", false},
223         {"title", "string", false},
224         {"focused", "bool", false},
225         {"actived", "bool", false},
226     };
227     constexpr FrontEndJsonDef WINDOW_FILTER_DEF = {
228         "WindowFilter",
229         WINDOW_FILTER_PROPERTIES,
230         sizeof(WINDOW_FILTER_PROPERTIES) / sizeof(FrontEndJsonPropDef),
231     };
232 
233     /** By class definition. deprecated since api 9*/
234     constexpr FrontendMethodDef BY_METHODS[] = {
235         {"By.id", "(int):By", false, true},
236         {"By.text", "(string,int?):By", false, true}, //  MatchPattern enum as int value
237         {"By.key", "(string):By", false, true},
238         {"By.type", "(string):By", false, true},
239         {"By.enabled", "(bool?):By", false, true}, // default bool arg: true
240         {"By.focused", "(bool?):By", false, true},
241         {"By.selected", "(bool?):By", false, true},
242         {"By.clickable", "(bool?):By", false, true},
243         {"By.scrollable", "(bool?):By", false, true},
244         {"By.isBefore", "(By):By", false, true},
245         {"By.isAfter", "(By):By", false, true},
246     };
247     constexpr std::string_view REF_SEED_BY = "On#seed";
248     constexpr FrontEndClassDef BY_DEF = {
249         "By",
250         BY_METHODS,
251         sizeof(BY_METHODS) / sizeof(FrontendMethodDef),
252     };
253 
254     /** UiDriver class definition.*/
255     constexpr FrontendMethodDef UI_DRIVER_METHODS[] = {
256         {"UiDriver.create", "():UiDriver", true, true},
257         {"UiDriver.delayMs", "(int):void", false, false},
258         {"UiDriver.findComponent", "(By):UiComponent", false, false},
259         {"UiDriver.findComponents", "(By):[UiComponent]", false, false},
260         {"UiDriver.screenCap", "(string):bool", false, false},
261         {"UiDriver.assertComponentExist", "(By):void", false, false},
262         {"UiDriver.pressBack", "():void", false, false},
263         {"UiDriver.triggerKey", "(int):void", false, false},
264         {"UiDriver.swipe", "(int,int,int,int,int?):void", false, false},
265         {"UiDriver.click", "(int,int):void", false, false},
266         {"UiDriver.longClick", "(int,int):void", false, false},
267         {"UiDriver.doubleClick", "(int,int):void", false, false},
268     };
269     constexpr FrontEndClassDef UI_DRIVER_DEF = {
270         "UiDriver",
271         UI_DRIVER_METHODS,
272         sizeof(UI_DRIVER_METHODS) / sizeof(FrontendMethodDef),
273     };
274 
275     /** UiComponent class definition.*/
276     constexpr FrontendMethodDef UI_COMPONENT_METHODS[] = {
277         {"UiComponent.getId", "():int", false, false},
278         {"UiComponent.getText", "():string", false, false},
279         {"UiComponent.getKey", "():string", false, false},
280         {"UiComponent.getType", "():string", false, false},
281         {"UiComponent.isEnabled", "():bool", false, false},
282         {"UiComponent.isFocused", "():bool", false, false},
283         {"UiComponent.isSelected", "():bool", false, false},
284         {"UiComponent.isClickable", "():bool", false, false},
285         {"UiComponent.isScrollable", "():bool", false, false},
286         {"UiComponent.click", "():void", false, false},
287         {"UiComponent.longClick", "():void", false, false},
288         {"UiComponent.doubleClick", "():void", false, false},
289         {"UiComponent.inputText", "(string):void", false, false},
290         {"UiComponent.scrollSearch", "(By):UiComponent", false, false},
291     };
292     constexpr FrontEndClassDef UI_COMPONENT_DEF = {
293         "UiComponent",
294         UI_COMPONENT_METHODS,
295         sizeof(UI_COMPONENT_METHODS) / sizeof(FrontendMethodDef),
296     };
297 
298     /** On class definition(since api 9, outdates By class).*/
299     constexpr FrontendMethodDef ON_METHODS[] = {
300         {"On.text", "(string,int?):On", false, true}, //  MatchPattern enum as int value
301         {"On.id", "(string):On", false, true},
302         {"On.type", "(string):On", false, true},
303         {"On.enabled", "(bool?):On", false, true}, // default bool arg: true
304         {"On.focused", "(bool?):On", false, true},
305         {"On.selected", "(bool?):On", false, true},
306         {"On.clickable", "(bool?):On", false, true},
307         {"On.longClickable", "(bool?):On", false, true},
308         {"On.scrollable", "(bool?):On", false, true},
309         {"On.checkable", "(bool?):On", false, true},
310         {"On.checked", "(bool?):On", false, true},
311         {"On.isBefore", "(On):On", false, true},
312         {"On.isAfter", "(On):On", false, true},
313     };
314 
315     constexpr std::string_view REF_SEED_ON = "On#seed";
316     constexpr FrontEndClassDef ON_DEF = {
317         "On",
318         ON_METHODS,
319         sizeof(ON_METHODS) / sizeof(FrontendMethodDef),
320     };
321 
322     /** Driver class definition. (since api 9, outdates UiDriver)*/
323     constexpr FrontendMethodDef DRIVER_METHODS[] = {
324         {"Driver.create", "():Driver", true, true},
325         {"Driver.delayMs", "(int):void", false, false},
326         {"Driver.findComponent", "(On):Component", false, false},
327         {"Driver.findWindow", "(WindowFilter):UiWindow", false, false},
328         {"Driver.findComponents", "(On):[Component]", false, false},
329         {"Driver.waitForComponent", "(On,int):Component", false, false},
330         {"Driver.screenCap", "(string):bool", false, false},
331         {"Driver.assertComponentExist", "(On):void", false, false},
332         {"Driver.pressBack", "():void", false, false},
333         {"Driver.triggerKey", "(int):void", false, false},
334         {"Driver.triggerCombineKeys", "(int,int,int?):void", false, false},
335         {"Driver.click", "(int,int):void", false, false},
336         {"Driver.longClick", "(int,int):void", false, false},
337         {"Driver.doubleClick", "(int,int):void", false, false},
338         {"Driver.swipe", "(int,int,int,int,int?):void", false, false},
339         {"Driver.drag", "(int,int,int,int,int?):void", false, false},
340         {"Driver.setDisplayRotation", "(int):void", false, false},  // DisplayRotation enum as int value
341         {"Driver.getDisplayRotation", "():int", false, false},  // DisplayRotation enum as int value
342         {"Driver.setDisplayRotationEnabled", "(bool):void", false, false},
343         {"Driver.getDisplaySize", "():Point", false, false},
344         {"Driver.getDisplayDensity", "():Point", false, false},
345         {"Driver.wakeUpDisplay", "():void", false, false},
346         {"Driver.pressHome", "():void", false, false},
347         {"Driver.waitForIdle", "(int,int):bool", false, false},
348         {"Driver.fling", "(Point,Point,int,int):void", false, false},
349         {"Driver.injectMultiPointerAction", "(PointerMatrix, int?):bool", false, false},
350     };
351     constexpr FrontEndClassDef DRIVER_DEF = {
352         "Driver",
353         DRIVER_METHODS,
354         sizeof(DRIVER_METHODS) / sizeof(FrontendMethodDef),
355     };
356 
357     /** Component class definition.(since api 9, outdates UiComponent)*/
358     constexpr FrontendMethodDef COMPONENT_METHODS[] = {
359         {"Component.getText", "():string", false, false},
360         {"Component.getId", "():string", false, false},
361         {"Component.getType", "():string", false, false},
362         {"Component.isEnabled", "():bool", false, false},
363         {"Component.isFocused", "():bool", false, false},
364         {"Component.isSelected", "():bool", false, false},
365         {"Component.isClickable", "():bool", false, false},
366         {"Component.isLongClickable", "():bool", false, false},
367         {"Component.isScrollable", "():bool", false, false},
368         {"Component.isCheckable", "():bool", false, false},
369         {"Component.isChecked", "():bool", false, false},
370         {"Component.getBounds", "():Rect", false, false},
371         {"Component.getBoundsCenter", "():Point", false, false},
372         {"Component.click", "():void", false, false},
373         {"Component.longClick", "():void", false, false},
374         {"Component.doubleClick", "():void", false, false},
375         {"Component.scrollToTop", "(int?):void", false, false},
376         {"Component.scrollToBottom", "(int?):void", false, false},
377         {"Component.inputText", "(string):void", false, false},
378         {"Component.clearText", "():void", false, false},
379         {"Component.scrollSearch", "(On):Component", false, false},
380         {"Component.dragTo", "(Component):void", false, false},
381         {"Component.pinchOut", "(float):void", false, false},
382         {"Component.pinchIn", "(float):void", false, false},
383     };
384     constexpr FrontEndClassDef COMPONENT_DEF = {
385         "Component",
386         COMPONENT_METHODS,
387         sizeof(COMPONENT_METHODS) / sizeof(FrontendMethodDef),
388     };
389 
390     /** UiWindow class definition.*/
391     constexpr FrontendMethodDef UI_WINDOW_METHODS[] = {
392         {"UiWindow.getBundleName", "():string", false, false},
393         {"UiWindow.getBounds", "():Rect", false, false},
394         {"UiWindow.getTitle", "():string", false, false},
395         {"UiWindow.getWindowMode", "():int", false, false}, // WindowMode enum as int value
396         {"UiWindow.isFocused", "():bool", false, false},
397         {"UiWindow.isActived", "():bool", false, false},
398         {"UiWindow.focus", "():void", false, false},
399         {"UiWindow.moveTo", "(int,int):void", false, false},
400         {"UiWindow.resize", "(int,int,int):void", false, false}, // ResizeDirection enum as int value
401         {"UiWindow.split", "():void", false, false},
402         {"UiWindow.maximize", "():void", false, false},
403         {"UiWindow.resume", "():void", false, false},
404         {"UiWindow.minimize", "():void", false, false},
405         {"UiWindow.close", "():void", false, false},
406     };
407     constexpr FrontEndClassDef UI_WINDOW_DEF = {
408         "UiWindow",
409         UI_WINDOW_METHODS,
410         sizeof(UI_WINDOW_METHODS) / sizeof(FrontendMethodDef),
411     };
412 
413     /** PointerMatrix class definition.*/
414     constexpr FrontendMethodDef POINTER_MATRIX_METHODS[] = {
415         {"PointerMatrix.create", "(int,int):PointerMatrix", true, true},
416         {"PointerMatrix.setPoint", "(int,int,Point):void", false, true},
417     };
418     constexpr FrontEndClassDef POINTER_MATRIX_DEF = {
419         "PointerMatrix",
420         POINTER_MATRIX_METHODS,
421         sizeof(POINTER_MATRIX_METHODS) / sizeof(FrontendMethodDef),
422     };
423 
424     /** List all the frontend data-type definitions.*/
425     const auto FRONTEND_CLASS_DEFS = {&BY_DEF, &UI_DRIVER_DEF, &UI_COMPONENT_DEF, &ON_DEF,
426                                       &DRIVER_DEF, &COMPONENT_DEF, &UI_WINDOW_DEF, &POINTER_MATRIX_DEF};
427     const auto FRONTEND_ENUMERATOR_DEFS = {&MATCH_PATTERN_DEF, &WINDOW_MODE_DEF, &RESIZE_DIRECTION_DEF,
428                                            &DISPLAY_ROTATION_DEF};
429     const auto FRONTEND_JSON_DEFS = {&RECT_DEF, &POINT_DEF, &WINDOW_FILTER_DEF};
430     /** The allowed in/out data type scope of frontend apis.*/
431     const std::initializer_list<std::string_view> DATA_TYPE_SCOPE = {
432         "int",
433         "float",
434         "bool",
435         "string",
436         RECT_DEF.name_,
437         POINT_DEF.name_,
438         WINDOW_FILTER_DEF.name_,
439         BY_DEF.name_,
440         UI_DRIVER_DEF.name_,
441         UI_COMPONENT_DEF.name_,
442         ON_DEF.name_,
443         DRIVER_DEF.name_,
444         COMPONENT_DEF.name_,
445         UI_WINDOW_DEF.name_,
446         POINTER_MATRIX_DEF.name_,
447     };
448 } // namespace OHOS::uitest
449 
450 #endif