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 "nlohmann/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 54 virtual void GetUiWindows(std::map<int32_t, vector<Window>> &out, int32_t targetDisplay = -1){}; 55 GetWidgetsInWindow(const Window & winInfo,unique_ptr<ElementNodeIterator> & elementIterator,AamsWorkMode mode)56 virtual bool GetWidgetsInWindow(const Window &winInfo, unique_ptr<ElementNodeIterator> &elementIterator, 57 AamsWorkMode mode) 58 { 59 return false; 60 }; 61 WaitForUiSteady(uint32_t idleThresholdMs,uint32_t timeoutSec)62 virtual bool WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec) const 63 { 64 return false; 65 }; 66 InjectTouchEventSequence(const PointerMatrix & events)67 virtual void InjectTouchEventSequence(const PointerMatrix& events) const {}; 68 InjectKeyEventSequence(const std::vector<KeyEvent> & events)69 virtual void InjectKeyEventSequence(const std::vector<KeyEvent>& events) const {}; 70 InjectMouseEventSequence(const vector<MouseEvent> & events)71 virtual void InjectMouseEventSequence(const vector<MouseEvent>& events) const {}; 72 IsTouchPadExist()73 virtual bool IsTouchPadExist() const 74 { 75 return false; 76 }; 77 InjectTouchPadEventSequence(const vector<TouchPadEvent> & events)78 virtual void InjectTouchPadEventSequence(const vector<TouchPadEvent>& events) const {}; 79 PutTextToClipboard(std::string_view text)80 virtual void PutTextToClipboard(std::string_view text) const {}; 81 TakeScreenCap(int32_t fd,std::stringstream & errReceiver,int32_t displayId,Rect rect)82 virtual bool TakeScreenCap(int32_t fd, std::stringstream &errReceiver, int32_t displayId, Rect rect) const 83 { 84 return false; 85 }; 86 GetCharKeyCode(char ch,int32_t & code,int32_t & ctrlCode)87 virtual bool GetCharKeyCode(char ch, int32_t& code, int32_t& ctrlCode) const 88 { 89 return false; 90 }; 91 SetDisplayRotation(DisplayRotation rotation)92 virtual void SetDisplayRotation(DisplayRotation rotation) const {}; 93 GetDisplayRotation(int32_t displayId)94 virtual DisplayRotation GetDisplayRotation(int32_t displayId) const 95 { 96 return ROTATION_0; 97 }; 98 SetDisplayRotationEnabled(bool enabled)99 virtual void SetDisplayRotationEnabled(bool enabled) const {}; 100 GetDisplaySize(int32_t displayId)101 virtual Point GetDisplaySize(int32_t displayId) const 102 { 103 return Point(0, 0); 104 }; 105 GetDisplayDensity(int32_t displayId)106 virtual Point GetDisplayDensity(int32_t displayId) const 107 { 108 return Point(0, 0); 109 }; 110 IsScreenOn()111 virtual bool IsScreenOn() const 112 { 113 return true; 114 }; 115 116 /** 117 * Tells if this controller is effective for current UI. 118 * */ 119 virtual bool IsWorkable() const = 0; 120 RegisterUiEventListener(std::shared_ptr<UiEventListener> listener)121 virtual void RegisterUiEventListener(std::shared_ptr<UiEventListener> listener) const {}; 122 GetHidumperInfo(std::string windowId,char ** buf,size_t & len)123 virtual void GetHidumperInfo(std::string windowId, char **buf, size_t &len) {}; 124 }; 125 } 126 127 #endif