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 28 namespace OHOS::uitest { 29 enum Priority : uint8_t { 30 HIGH = 3, MEDIUM = 2, LOW = 1 31 }; 32 33 enum DisplayRotation : uint32_t { 34 ROTATION_0, 35 ROTATION_90, 36 ROTATION_180, 37 ROTATION_270 38 }; 39 40 class UiController; 41 // Prototype of function that provides UiControllers, used to install controllers on demand. 42 using UiControllerProvider = std::function<void(std::list<std::unique_ptr<UiController>> &)>; 43 44 class UiController { 45 public: 46 explicit UiController(std::string_view name); 47 48 virtual ~UiController() = default; 49 GetUiHierarchy(std::vector<std::pair<Window,nlohmann::json>> & out)50 virtual void GetUiHierarchy(std::vector<std::pair<Window, nlohmann::json>>& out) {}; 51 WaitForUiSteady(uint32_t idleThresholdMs,uint32_t timeoutSec)52 virtual bool WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec) const 53 { 54 return false; 55 }; 56 InjectTouchEventSequence(const PointerMatrix & events)57 virtual void InjectTouchEventSequence(const PointerMatrix& events) const {}; 58 InjectKeyEventSequence(const std::vector<KeyEvent> & events)59 virtual void InjectKeyEventSequence(const std::vector<KeyEvent>& events) const {}; 60 PutTextToClipboard(std::string_view text)61 virtual void PutTextToClipboard(std::string_view text) const {}; 62 TakeScreenCap(std::string_view savePath,std::stringstream & errReceiver)63 virtual bool TakeScreenCap(std::string_view savePath, std::stringstream &errReceiver) const 64 { 65 return false; 66 }; 67 GetCharKeyCode(char ch,int32_t & code,int32_t & ctrlCode)68 virtual bool GetCharKeyCode(char ch, int32_t& code, int32_t& ctrlCode) const 69 { 70 return false; 71 }; 72 SetDisplayRotation(DisplayRotation rotation)73 virtual void SetDisplayRotation(DisplayRotation rotation) const {}; 74 GetDisplayRotation()75 virtual DisplayRotation GetDisplayRotation() const 76 { 77 return ROTATION_0; 78 }; 79 SetDisplayRotationEnabled(bool enabled)80 virtual void SetDisplayRotationEnabled(bool enabled) const {}; 81 GetDisplaySize()82 virtual Point GetDisplaySize() const 83 { 84 return Point(0, 0); 85 }; 86 GetDisplayDensity()87 virtual Point GetDisplayDensity() const 88 { 89 return Point(0, 0); 90 }; 91 IsScreenOn()92 virtual bool IsScreenOn() const 93 { 94 return true; 95 }; 96 97 /** 98 * Tells if this controller is effective for current UI. 99 * */ 100 virtual bool IsWorkable() const = 0; 101 GetName()102 std::string GetName() const 103 { 104 return name_; 105 } 106 SetPriority(Priority val)107 void SetPriority(Priority val) 108 { 109 this->priority_ = val; 110 } 111 112 static void RegisterControllerProvider(UiControllerProvider func); 113 114 static void RegisterController(std::unique_ptr<UiController> controller, Priority priority); 115 116 static void RemoveController(std::string_view name); 117 118 static void RemoveAllControllers(); 119 120 /**Install UiControllers with registered controllerProvider.*/ 121 static void InstallFromProvider(); 122 123 /**The the currently active UiController, returns null if none is available.*/ 124 static UiController *GetController(); 125 126 private: 127 const std::string name_; 128 Priority priority_ = Priority::MEDIUM; 129 static std::mutex controllerAccessMutex_; 130 static std::list<std::unique_ptr<UiController>> controllers_; 131 static UiControllerProvider controllerProvider_; 132 133 static bool Comparator(const std::unique_ptr<UiController> &c1, const std::unique_ptr<UiController> &c2); 134 }; 135 } 136 137 #endif