1 /* 2 * Copyright (c) 2020-2021 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_TEST_INPUT_EVENT_H 17 #define UI_TEST_INPUT_EVENT_H 18 19 #include "components/root_view.h" 20 #include "components/ui_label.h" 21 #include "components/ui_label_button.h" 22 #include "components/ui_scroll_view.h" 23 #include "dock/input_device.h" 24 #include "ui_test.h" 25 26 namespace OHOS { 27 class TestKeyInputListener : public RootView::OnKeyActListener { 28 public: TestKeyInputListener(UILabel * label)29 explicit TestKeyInputListener(UILabel* label) : label_(label) {} ~TestKeyInputListener()30 virtual ~TestKeyInputListener() {} OnKeyAct(UIView & view,const KeyEvent & event)31 bool OnKeyAct(UIView& view, const KeyEvent& event) override 32 { 33 if (label_ == nullptr) { 34 return true; 35 } 36 switch (event.GetState()) { 37 case InputDevice::STATE_PRESS: 38 label_->SetText("key pressed!"); 39 break; 40 case InputDevice::STATE_RELEASE: 41 label_->SetText("key released!"); 42 break; 43 default: 44 label_->SetText(""); 45 break; 46 } 47 label_->Invalidate(); 48 return true; 49 } 50 51 private: 52 UILabel* label_; 53 }; 54 55 class TestOnClickListener : public UIView::OnClickListener { 56 public: TestOnClickListener(UILabel * label,char * sentence,bool isConsume)57 explicit TestOnClickListener(UILabel* label, char* sentence, bool isConsume) 58 : label_(label), sentence_(sentence), isConsume_(isConsume) 59 { 60 } ~TestOnClickListener()61 virtual ~TestOnClickListener() {} OnClick(UIView & view,const ClickEvent & event)62 virtual bool OnClick(UIView& view, const ClickEvent& event) 63 { 64 if (label_ != nullptr) { 65 label_->SetText(sentence_); 66 label_->Invalidate(); 67 } 68 return isConsume_; 69 } 70 71 private: 72 UILabel* label_; 73 char* sentence_; 74 bool isConsume_; 75 }; 76 77 class TestOnLongPressListener : public UIView::OnLongPressListener { 78 public: TestOnLongPressListener(UILabel * label,char * sentence,bool isConsume)79 explicit TestOnLongPressListener(UILabel* label, char* sentence, bool isConsume) 80 : label_(label), sentence_(sentence), isConsume_(isConsume) 81 { 82 } ~TestOnLongPressListener()83 virtual ~TestOnLongPressListener() {} OnLongPress(UIView & view,const LongPressEvent & event)84 virtual bool OnLongPress(UIView& view, const LongPressEvent& event) 85 { 86 if (label_ != nullptr) { 87 label_->SetText(sentence_); 88 label_->Invalidate(); 89 } 90 return isConsume_; 91 } 92 93 private: 94 UILabel* label_; 95 char* sentence_; 96 bool isConsume_; 97 }; 98 99 class TestOnTouchListener : public UIView::OnTouchListener { 100 public: TestOnTouchListener(UILabel * label,char * strPress,char * strRelease,char * strCancel,bool isConsume)101 explicit TestOnTouchListener(UILabel* label, char* strPress, char* strRelease, char* strCancel, bool isConsume) 102 : label_(label), strPress_(strPress), strRelease_(strRelease), strCancel_(strCancel), isConsume_(isConsume) 103 { 104 } ~TestOnTouchListener()105 virtual ~TestOnTouchListener() {} OnPress(UIView & view,const PressEvent & event)106 virtual bool OnPress(UIView& view, const PressEvent& event) 107 { 108 if (label_ != nullptr) { 109 label_->SetText(strPress_); 110 label_->Invalidate(); 111 } 112 return isConsume_; 113 } 114 OnRelease(UIView & view,const ReleaseEvent & event)115 virtual bool OnRelease(UIView& view, const ReleaseEvent& event) 116 { 117 if (label_ != nullptr) { 118 label_->SetText(strRelease_); 119 label_->Invalidate(); 120 } 121 return isConsume_; 122 } 123 OnCancel(UIView & view,const CancelEvent & event)124 virtual bool OnCancel(UIView& view, const CancelEvent& event) 125 { 126 if (label_ != nullptr) { 127 label_->SetText(strCancel_); 128 label_->Invalidate(); 129 } 130 return isConsume_; 131 } 132 133 private: 134 UILabel* label_; 135 char* strPress_; 136 char* strRelease_; 137 char* strCancel_; 138 bool isConsume_; 139 }; 140 141 class TestOnDragListener : public UIView::OnDragListener { 142 public: TestOnDragListener(UILabel * label,char * strDragStart,char * strDrag,char * strDragEnd,bool isConsume)143 explicit TestOnDragListener(UILabel* label, char* strDragStart, char* strDrag, char* strDragEnd, bool isConsume) 144 : label_(label), strDragStart_(strDragStart), strDrag_(strDrag), strDragEnd_(strDragEnd), isConsume_(isConsume) 145 { 146 } ~TestOnDragListener()147 virtual ~TestOnDragListener() {} OnDragStart(UIView & view,const DragEvent & event)148 virtual bool OnDragStart(UIView& view, const DragEvent& event) 149 { 150 if (label_ != nullptr) { 151 label_->SetText(strDragStart_); 152 label_->Invalidate(); 153 } 154 return isConsume_; 155 } 156 OnDrag(UIView & view,const DragEvent & event)157 virtual bool OnDrag(UIView& view, const DragEvent& event) 158 { 159 if (label_ != nullptr) { 160 label_->SetText(strDrag_); 161 label_->Invalidate(); 162 } 163 return isConsume_; 164 } 165 OnDragEnd(UIView & view,const DragEvent & event)166 virtual bool OnDragEnd(UIView& view, const DragEvent& event) 167 { 168 if (label_ != nullptr) { 169 label_->SetText(strDragEnd_); 170 label_->Invalidate(); 171 } 172 return isConsume_; 173 } 174 175 private: 176 UILabel* label_; 177 char* strDragStart_; 178 char* strDrag_; 179 char* strDragEnd_; 180 bool isConsume_; 181 }; 182 183 class UITestInputEvent : public UITest { 184 public: UITestInputEvent()185 UITestInputEvent() {} ~UITestInputEvent()186 ~UITestInputEvent() {} 187 void SetUp() override; 188 void TearDown() override; 189 const UIView* GetTestView() override; 190 191 /** 192 * @brief Test if dispatch press\release\longpress\cancel event act normal when target is touchable 193 */ 194 void UIKitPointerInputTestDispatchSimpleEvent001(); 195 /** 196 * @brief Test if dispatch press\release\longpress\cancel\drag event act normal when target is untouchable 197 */ 198 void UIKitPointerInputTestDispatchSimpleEvent002(); 199 /** 200 * @brief Test if dispatch drag event act normal when target is touchable and draggable and drag parent instead. 201 */ 202 void UIKitPointerInputTestDispatchDragEvent001(); 203 /** 204 * @brief Test if dispatch drag event act normal when target is touchable and draggable and not drag parent instead. 205 */ 206 void UIKitPointerInputTestDispatchDragEvent002(); 207 /** 208 * @brief Test if dispatch drag event act normal when target is untouchable but draggable. 209 */ 210 void UIKitPointerInputTestDispatchDragEvent003(); 211 /** 212 * @brief Test if dispatch drag event act normal when target is untouchable but draggable. 213 */ 214 void UIKitPointerInputTestDispatchKeyEvent001(); 215 /** 216 * @brief Test if dispatch drag event act normal when target is untouchable but draggable. 217 */ 218 void UIKitPointerInputTestDispatchInVisibleEvent001(); 219 /** 220 * @brief Test click, release or longClick event bubble act normal when both of parent and child is triggered. 221 */ 222 void UIKitPointerInputTestDispatchBubble001(); 223 /** 224 * @brief Test click, release or longClick event bubble act normal when child is triggered but and parent not. 225 */ 226 void UIKitPointerInputTestDispatchBubble002(); 227 /** 228 * @brief Test click, release or longClick event bubble act normal when child is triggered but and parent not. 229 */ 230 void UIKitPointerInputTestDispatchBubble003(); 231 /** 232 * @brief Test click, release or longClick event bubble act normal when parent is triggered but and child not. 233 */ 234 void UIKitPointerInputTestDispatchBubble004(); 235 /** 236 * @brief Test drag event bubble act normal when both of parent and child is triggered. 237 */ 238 void UIKitPointerInputTestDispatchBubble005(); 239 /** 240 * @brief Test drag event bubble act normal when child is triggered but and parent not. 241 */ 242 void UIKitPointerInputTestDispatchBubble006(); 243 /** 244 * @brief Test drag event bubble act normal when child is triggered but and parent not. 245 */ 246 void UIKitPointerInputTestDispatchBubble007(); 247 /** 248 * @brief Test drag event bubble act normal when both of parent and child is not triggered. 249 */ 250 void UIKitPointerInputTestDispatchBubble008(); 251 /** 252 * @brief Test drag event bubble act normal when parent is triggered but and child not. 253 */ 254 void UIKitPointerInputTestDispatchBubble009(); 255 /** 256 * @brief Test drag event bubble act normal when parent is triggered but and child not. 257 */ 258 void UIKitPointerInputTestDispatchBubble010(); 259 260 private: 261 UIScrollView* container_ = nullptr; 262 TestKeyInputListener* keyListener_ = nullptr; 263 void InnerTest(const char* title, bool touchable, bool draggable, bool dragParent); 264 void InnerBubbleTest(const char* title, bool touchable, bool draggable, bool hasListener, bool isBubble); 265 void InnerBubbleDragTest(const char* title, 266 bool childDraggable, 267 bool parentDraggable, 268 bool hasListener, 269 bool isBubble); 270 }; 271 } // namespace OHOS 272 #endif // UI_TEST_INPUT_EVENT_H 273