• 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_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     class UiController;
34     // Prototype of function that provides UiControllers for given device, used to install controllers on demand.
35     using UiControllerProvider = std::function<void(std::string_view, std::list<std::unique_ptr<UiController>> &)>;
36 
37     class UiController {
38     public:
39         UiController(std::string_view name, std::string_view device);
40 
41         virtual ~UiController() = default;
42 
43         virtual void GetCurrentUiDom(nlohmann::json& out) const = 0;
44 
WaitForUiSteady(uint32_t idleThresholdMs,uint32_t timeoutSec)45         virtual void WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec) const {};
46 
InjectTouchEventSequence(const std::vector<TouchEvent> & events)47         virtual void InjectTouchEventSequence(const std::vector<TouchEvent>& events) const {};
48 
InjectKeyEventSequence(const std::vector<KeyEvent> & events)49         virtual void InjectKeyEventSequence(const std::vector<KeyEvent>& events) const {};
50 
PutTextToClipboard(std::string_view text)51         virtual void PutTextToClipboard(std::string_view text) const {};
52 
TakeScreenCap(std::string_view savePath,std::stringstream & errReceiver)53         virtual bool TakeScreenCap(std::string_view savePath, std::stringstream &errReceiver) const
54         {
55             return false;
56         };
57 
GetCharKeyCode(char ch,int32_t & code,int32_t & ctrlCode)58         virtual bool GetCharKeyCode(char ch, int32_t& code, int32_t& ctrlCode) const
59         {
60             return false;
61         };
62 
63         /**
64          * Tells if this controller is effective for current UI.
65          * */
66         virtual bool IsWorkable() const = 0;
67 
GetName()68         std::string GetName() const
69         {
70             return name_;
71         }
72 
SetPriority(Priority val)73         void SetPriority(Priority val)
74         {
75             this->priority_ = val;
76         }
77 
78         static void RegisterControllerProvider(UiControllerProvider func);
79 
80         static void RegisterController(std::unique_ptr<UiController> controller, Priority priority);
81 
82         static void RemoveController(std::string_view name);
83 
84         static void RemoveAllControllers();
85 
86         /**Install UiControllers for target device.*/
87         static void InstallForDevice(std::string_view device);
88 
89         /**The the currently active UiController instance for the target device, returns null if none is available.*/
90         static const UiController *GetController(std::string_view targetDevice);
91 
92     private:
93         const std::string name_;
94         const std::string targetDevice_;
95         Priority priority_ = Priority::MEDIUM;
96         static std::mutex controllerAccessMutex_;
97         static std::list<std::unique_ptr<UiController>> controllers_;
98         static UiControllerProvider controllerProvider_;
99         static std::set<std::string> controllerInstalledDevices_;
100 
101         static bool Comparator(const std::unique_ptr<UiController> &c1, const std::unique_ptr<UiController> &c2);
102     };
103 }
104 
105 #endif