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 #ifndef UI_ACTION_H 16 #define UI_ACTION_H 17 18 #include <vector> 19 #include "ui_model.h" 20 21 namespace OHOS::uitest { 22 // frequently used keys. 23 constexpr int32_t KEYCODE_NONE = 0; 24 constexpr int32_t KEYCODE_BACK = 2; 25 constexpr int32_t KEYCODE_CTRL = 2072; 26 constexpr int32_t KEYCODE_V = 2038; 27 constexpr int32_t KEYCODE_POWER = 18; 28 constexpr int32_t KEYCODE_HOME = 1; 29 30 /**Enumerates all the supported coordinate-based touch operations.*/ 31 enum TouchOp : uint8_t { CLICK, LONG_CLICK, DOUBLE_CLICK_P, SWIPE, DRAG, FLING }; 32 33 /**Enumerates the supported Key actions.*/ 34 enum UiKey : uint8_t { BACK, GENERIC }; 35 36 enum ActionStage : uint8_t { 37 DOWN = 0, MOVE = 1, UP = 2 38 }; 39 40 struct TouchEvent { 41 ActionStage stage_; 42 Point point_; 43 uint32_t downTimeOffsetMs_; 44 uint32_t holdMs_; 45 uint32_t flags_; 46 }; 47 48 struct KeyEvent { 49 ActionStage stage_; 50 int32_t code_; 51 uint32_t holdMs_; 52 }; 53 54 class PointerMatrix : public BackendClass { 55 public: 56 PointerMatrix(); 57 58 PointerMatrix(uint32_t fingersNum, uint32_t stepsNum); 59 60 PointerMatrix(PointerMatrix&& other); 61 62 PointerMatrix& operator=(PointerMatrix&& other); 63 64 ~PointerMatrix() override; 65 GetFrontendClassDef()66 const FrontEndClassDef &GetFrontendClassDef() const override 67 { 68 return POINTER_MATRIX_DEF; 69 } 70 71 void PushAction(const TouchEvent& ptr); 72 73 bool Empty() const; 74 75 TouchEvent& At(uint32_t fingerIndex, uint32_t stepIndex) const; 76 77 uint32_t GetCapacity() const; 78 79 uint32_t GetSize() const; 80 81 uint32_t GetSteps() const; 82 83 uint32_t GetFingers() const; 84 private: 85 std::unique_ptr<TouchEvent[]> data_ = nullptr; 86 uint32_t capacity_ = 0; 87 uint32_t stepNum_ = 0; 88 uint32_t fingerNum_ = 0; 89 uint32_t size_ = 0; 90 }; 91 92 /** 93 * Options of the UI operations, initialized with system default values. 94 **/ 95 class UiOpArgs { 96 public: 97 const uint32_t maxSwipeVelocityPps_ = 40000; 98 const uint32_t minSwipeVelocityPps_ = 200; 99 const uint32_t defaultSwipeVelocityPps_ = 600; 100 const uint32_t maxMultiTouchFingers = 10; 101 const uint32_t maxMultiTouchSteps = 1000; 102 uint32_t clickHoldMs_ = 100; 103 uint32_t longClickHoldMs_ = 1500; 104 uint32_t doubleClickIntervalMs_ = 200; 105 uint32_t keyHoldMs_ = 100; 106 uint32_t swipeVelocityPps_ = defaultSwipeVelocityPps_; 107 uint32_t uiSteadyThresholdMs_ = 1000; 108 uint32_t waitUiSteadyMaxMs_ = 3000; 109 uint32_t waitWidgetMaxMs_ = 5000; 110 int32_t scrollWidgetDeadZone_ = 20; 111 uint16_t swipeStepsCounts_ = 50; 112 }; 113 114 class TouchAction { 115 public: 116 /**Compute the touch event sequence that are needed to implement this action. 117 * @param recv: the event seqence receiver. 118 * @param options the ui operation agruments. 119 * */ 120 virtual void Decompose(PointerMatrix &recv, const UiOpArgs &options) const = 0; 121 }; 122 123 /** 124 * Base type of all raw pointer click actions. 125 **/ 126 class GenericClick : public TouchAction { 127 public: GenericClick(TouchOp type,const Point & point)128 GenericClick(TouchOp type, const Point &point) : type_(type), point_(point) {}; 129 130 void Decompose(PointerMatrix &recv, const UiOpArgs &options) const override; 131 132 ~GenericClick() = default; 133 134 private: 135 const TouchOp type_; 136 const Point point_; 137 }; 138 139 /** 140 * Base type of all raw pointer swipe actions. 141 **/ 142 class GenericSwipe : public TouchAction { 143 public: GenericSwipe(TouchOp type,const Point & from,const Point & to)144 explicit GenericSwipe(TouchOp type, const Point &from, const Point &to) : type_(type), from_(from), to_(to) {}; 145 146 void Decompose(PointerMatrix &recv, const UiOpArgs &options) const override; 147 148 ~GenericSwipe() = default; 149 150 private: 151 const TouchOp type_; 152 const Point from_; 153 const Point to_; 154 }; 155 156 /** 157 * Base type of all raw pointer pinch actions. 158 **/ 159 class GenericPinch : public TouchAction { 160 public: GenericPinch(const Rect & rect,float_t scale)161 explicit GenericPinch(const Rect &rect, float_t scale) : rect_(rect), scale_(scale) {}; 162 163 void Decompose(PointerMatrix &recv, const UiOpArgs &options) const override; 164 165 ~GenericPinch() = default; 166 167 private: 168 const Rect rect_; 169 const float_t scale_; 170 }; 171 172 /** 173 * Base type of multi pointer actions. 174 **/ 175 class MultiPointerAction : public TouchAction { 176 public: MultiPointerAction(const PointerMatrix & pointer)177 explicit MultiPointerAction(const PointerMatrix &pointer) : pointers_(pointer) {}; 178 179 void Decompose(PointerMatrix &recv, const UiOpArgs &options) const override; 180 181 ~MultiPointerAction() = default; 182 183 private: 184 const PointerMatrix& pointers_; 185 }; 186 187 /** 188 * Base type of all key actions. 189 * */ 190 class KeyAction { 191 public: 192 /**Compute the key event sequence that are needed to implement this action.*/ 193 virtual void ComputeEvents(std::vector<KeyEvent> &recv, const UiOpArgs &options) const = 0; 194 195 virtual ~KeyAction() = default; 196 }; 197 198 /**Base type of named single-key actions with at most 1 ctrl key.*/ 199 template<uint32_t kCode, uint32_t kCtrlCode = KEYCODE_NONE> 200 class NamedPlainKey : public KeyAction { 201 public: 202 explicit NamedPlainKey() = default; 203 ComputeEvents(std::vector<KeyEvent> & recv,const UiOpArgs & opt)204 void ComputeEvents(std::vector<KeyEvent> &recv, const UiOpArgs &opt) const override 205 { 206 if (kCtrlCode != KEYCODE_NONE) { 207 recv.push_back(KeyEvent {ActionStage::DOWN, kCtrlCode, 0}); 208 } 209 recv.push_back(KeyEvent {ActionStage::DOWN, kCode, opt.keyHoldMs_}); 210 recv.push_back(KeyEvent {ActionStage::UP, kCode, 0}); 211 if (kCtrlCode != KEYCODE_NONE) { 212 recv.push_back(KeyEvent {ActionStage::UP, kCtrlCode, 0}); 213 } 214 } 215 }; 216 217 /**Generic key actions without name and ctrl key.*/ 218 class AnonymousSingleKey final : public KeyAction { 219 public: AnonymousSingleKey(int32_t code)220 explicit AnonymousSingleKey(int32_t code) : code_(code) {}; 221 ComputeEvents(std::vector<KeyEvent> & recv,const UiOpArgs & opt)222 void ComputeEvents(std::vector<KeyEvent> &recv, const UiOpArgs &opt) const override 223 { 224 recv.push_back(KeyEvent {ActionStage::DOWN, code_, opt.keyHoldMs_}); 225 recv.push_back(KeyEvent {ActionStage::UP, code_, 0}); 226 } 227 228 private: 229 const int32_t code_; 230 }; 231 232 /**Generic Combinedkeys actions.*/ 233 class CombinedKeys final : public KeyAction { 234 public: CombinedKeys(int32_t codeZero,int32_t codeOne,int32_t codeTwo)235 CombinedKeys(int32_t codeZero, int32_t codeOne, int32_t codeTwo) 236 : codeZero_(codeZero), codeOne_(codeOne), codeTwo_(codeTwo) {}; 237 ComputeEvents(std::vector<KeyEvent> & recv,const UiOpArgs & opt)238 void ComputeEvents(std::vector<KeyEvent> &recv, const UiOpArgs &opt) const override 239 { 240 recv.push_back(KeyEvent {ActionStage::DOWN, codeZero_, 0}); 241 recv.push_back(KeyEvent {ActionStage::DOWN, codeOne_, 0}); 242 if (codeTwo_ != KEYCODE_NONE) { 243 recv.push_back(KeyEvent {ActionStage::DOWN, codeTwo_, opt.keyHoldMs_}); 244 } else { 245 recv.at(INDEX_ONE).holdMs_ = opt.keyHoldMs_; 246 } 247 if (codeTwo_ != KEYCODE_NONE) { 248 recv.push_back(KeyEvent {ActionStage::UP, codeTwo_, 0}); 249 } 250 recv.push_back(KeyEvent {ActionStage::UP, codeOne_, 0}); 251 recv.push_back(KeyEvent {ActionStage::UP, codeZero_, 0}); 252 } 253 254 private: 255 const int32_t codeZero_; 256 const int32_t codeOne_; 257 const int32_t codeTwo_; 258 }; 259 260 using Back = NamedPlainKey<KEYCODE_BACK>; 261 using Power = NamedPlainKey<KEYCODE_POWER>; 262 using Home = NamedPlainKey<KEYCODE_HOME>; 263 using Paste = NamedPlainKey<KEYCODE_V, KEYCODE_CTRL>; 264 } 265 266 #endif