• 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 UI_DRIVER_H
17 #define UI_DRIVER_H
18 
19 #include <string>
20 #include "ui_controller.h"
21 #include "ui_model.h"
22 #include "widget_selector.h"
23 #include "widget_image.h"
24 
25 namespace OHOS::uitest {
26 
27     class UiDriver : public ExternApi<TypeId::DRIVER> {
28     public:
29         /**Create UiDriver for the target device.*/
30         explicit UiDriver(std::string_view device);
31 
~UiDriver()32         ~UiDriver() {}
33 
34         /**Perform given operation on the given widget.*/
35         void PerformWidgetOperate(const WidgetImage &image, WidgetOp type, ApiCallErr &error);
36 
37         /**Trigger the given key action. */
38         void TriggerKey(const KeyAction &action, ApiCallErr &error);
39 
40         /**Inject the given text to the given id-specified widget.*/
41         void InputText(const WidgetImage &image, std::string_view text, ApiCallErr &error);
42 
43         /**Find widgets with the given selector. Results are arranged in the receiver in <b>DFS</b> order.
44          * @returns the widget images.
45          **/
46         void FindWidgets(const WidgetSelector &select, std::vector<std::unique_ptr<WidgetImage>> &rev, ApiCallErr &err);
47 
48         /**Scroll on the given subject widget to find the target widget matching the selector.*/
49         std::unique_ptr<WidgetImage> ScrollSearch(const WidgetImage &img, const WidgetSelector &selector,
50                                                   ApiCallErr &err, int32_t deadZoneSize);
51 
52         /**Drag widget-A to widget-B.*/
53         void DragWidgetToAnother(const WidgetImage &imgFrom, const WidgetImage &imgTo, ApiCallErr &err);
54 
55         /**Wait for the matching widget appear in the given timeout.*/
56         std::unique_ptr<WidgetImage> WaitForWidget(const WidgetSelector &select, uint32_t maxMs, ApiCallErr &err);
57 
58         /**Update the attributes of the WidgetImage from current UI.*/
59         void UpdateWidgetImage(WidgetImage &image, ApiCallErr &error);
60 
61         /**Perform generic-action on raw points.*/
62         void PerformGenericClick(PointerOp type, const Point &point, ApiCallErr &err);
63 
64         /**Perform generic-swipe on raw points.*/
65         void PerformGenericSwipe(PointerOp type, const Point &fromPoint, const Point &toPoint, ApiCallErr &err);
66 
67         /**Delay current thread for given duration.*/
68         static void DelayMs(uint32_t ms);
69 
70         /**Take screen capture, save to given file path as PNG.*/
71         void TakeScreenCap(std::string_view savePath, ApiCallErr &err);
72 
73         void WriteIntoParcel(nlohmann::json &data) const override;
74 
75         void ReadFromParcel(const nlohmann::json &data) override;
76 
77     private:
78         /**Update UI controller and UI objects.*/
79         void UpdateUi(bool updateUiTree, ApiCallErr &error);
80 
81         /**Retrieve widget represented by the given WidgetImage from updated UI.*/
82         const Widget *RetrieveWidget(const WidgetImage &img, ApiCallErr &err, bool updateUi = true);
83 
84         /**Find scroll widget on current UI. <b>(without updating UI objects)</b>*/
85         const Widget *FindScrollWidget(const WidgetImage &img) const;
86 
87         /**For multi-device UI operation, specifies the target device name.*/
88         std::string deviceName_;
89         /**The UI manipulation options.*/
90         UiDriveOptions options_;
91         // objects that are needed to be updated before each interaction and used in the interaction
92         std::unique_ptr<WidgetTree> widgetTree_ = nullptr;
93         const UiController *uiController_ = nullptr;
94     };
95 }
96 
97 #endif