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_CONTROLLER_H
17 #define UI_CONTROLLER_H
18 
19 #include <list>
20 #include <string>
21 #include <sstream>
22 #include <memory>
23 #include <mutex>
24 #include <functional>
25 #include "json.hpp"
26 #include "ui_action.h"
27 #include "select_strategy.h"
28 
29 namespace OHOS::uitest {
30     struct UiEventSourceInfo {
31         std::string bundleName;
32         std::string text;
33         std::string type;
34     };
35 
36     class UiEventListener {
37     public:
38         UiEventListener() = default;
39         virtual ~UiEventListener() = default;
OnEvent(const std::string &event, const UiEventSourceInfo &source)40         virtual void OnEvent(const std::string &event, const UiEventSourceInfo &source) {};
41     };
42 
43     class UiController {
44     public:
UiController()45         UiController() {};
46 
47         virtual ~UiController() = default;
48 
Initialize(ApiCallErr &error)49         virtual bool Initialize(ApiCallErr &error)
50         {
51             return true;
52         };
53 
GetUiWindows(std::vector<Window> &out)54         virtual void GetUiWindows(std::vector<Window> &out){};
55 
GetWidgetsInWindow(const Window &winInfo, unique_ptr<ElementNodeIterator> &elementIterator)56         virtual bool GetWidgetsInWindow(const Window &winInfo, unique_ptr<ElementNodeIterator> &elementIterator)
57         {
58             return false;
59         };
60 
WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec) const61         virtual bool WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec) const
62         {
63             return false;
64         };
65 
InjectTouchEventSequence(const PointerMatrix& events) const66         virtual void InjectTouchEventSequence(const PointerMatrix& events) const {};
67 
InjectKeyEventSequence(const std::vector<KeyEvent>& events) const68         virtual void InjectKeyEventSequence(const std::vector<KeyEvent>& events) const {};
69 
InjectMouseEventSequence(const vector<MouseEvent>& events) const70         virtual void InjectMouseEventSequence(const vector<MouseEvent>& events) const {};
71 
PutTextToClipboard(std::string_view text) const72         virtual void PutTextToClipboard(std::string_view text) const {};
73 
TakeScreenCap(int32_t fd, std::stringstream &errReceiver, Rect rect) const74         virtual bool TakeScreenCap(int32_t fd, std::stringstream &errReceiver, Rect rect) const
75         {
76             return false;
77         };
78 
GetCharKeyCode(char ch, int32_t& code, int32_t& ctrlCode) const79         virtual bool GetCharKeyCode(char ch, int32_t& code, int32_t& ctrlCode) const
80         {
81             return false;
82         };
83 
SetDisplayRotation(DisplayRotation rotation) const84         virtual void SetDisplayRotation(DisplayRotation rotation) const {};
85 
GetDisplayRotation() const86         virtual DisplayRotation GetDisplayRotation() const
87         {
88             return ROTATION_0;
89         };
90 
SetDisplayRotationEnabled(bool enabled) const91         virtual void SetDisplayRotationEnabled(bool enabled) const {};
92 
GetDisplaySize() const93         virtual Point GetDisplaySize() const
94         {
95             return Point(0, 0);
96         };
97 
GetDisplayDensity() const98         virtual Point GetDisplayDensity() const
99         {
100             return Point(0, 0);
101         };
102 
IsScreenOn() const103         virtual bool IsScreenOn() const
104         {
105             return true;
106         };
107 
108         /**
109          * Tells if this controller is effective for current UI.
110          * */
111         virtual bool IsWorkable() const = 0;
112 
RegisterUiEventListener(std::shared_ptr<UiEventListener> listener) const113         virtual void RegisterUiEventListener(std::shared_ptr<UiEventListener> listener) const {};
114 
GetHidumperInfo(std::string windowId, char **buf, size_t &len)115         virtual void GetHidumperInfo(std::string windowId, char **buf, size_t &len) {};
116     };
117 }
118 
119 #endif