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 TestView : public UIView {
28 public:
TestView()29     TestView() {}
~TestView()30     virtual ~TestView() {}
OnLongPressEvent(const LongPressEvent & event)31     bool OnLongPressEvent(const LongPressEvent& event) override
32     {
33         if (label_ != nullptr) {
34             label_->SetText("long press!");
35             label_->Invalidate();
36         }
37         return UIView::OnLongPressEvent(event);
38     }
39 
OnDragEvent(const DragEvent & event)40     bool OnDragEvent(const DragEvent& event) override
41     {
42         if (label_ != nullptr) {
43             label_->SetText("drag!");
44             label_->Invalidate();
45         }
46         return UIView::OnDragEvent(event);
47     }
48 
SetSentence(const char * sentence)49     void SetSentence(const char* sentence)
50     {
51         sentence_ = sentence;
52     }
53 
OnClickEvent(const ClickEvent & event)54     bool OnClickEvent(const ClickEvent& event) override
55     {
56         if (label_ != nullptr) {
57             label_->SetText(sentence_);
58             label_->Invalidate();
59         }
60         return UIView::OnClickEvent(event);
61     }
62 
OnPressEvent(const PressEvent & event)63     bool OnPressEvent(const PressEvent& event) override
64     {
65         if (label_ != nullptr) {
66             label_->SetText("press!");
67             label_->Invalidate();
68         }
69         return UIView::OnPressEvent(event);
70     }
71 
OnReleaseEvent(const ReleaseEvent & event)72     bool OnReleaseEvent(const ReleaseEvent& event) override
73     {
74         if (label_ != nullptr) {
75             label_->SetText("release!");
76             label_->Invalidate();
77         }
78         return UIView::OnReleaseEvent(event);
79     }
80 
OnCancelEvent(const CancelEvent & event)81     bool OnCancelEvent(const CancelEvent& event) override
82     {
83         if (label_ != nullptr) {
84             label_->SetText("cancel!");
85             label_->Invalidate();
86         }
87         return UIView::OnCancelEvent(event);
88     }
89 
SetLabel(UILabel * label)90     void SetLabel(UILabel* label)
91     {
92         label_ = label;
93     }
94 
SetLabel2(UILabel * label)95     void SetLabel2(UILabel* label)
96     {
97         label2_ = label;
98     }
99 
100 private:
101     UILabel* label_ = nullptr;
102     UILabel* label2_ = nullptr;
103     const char* sentence_ = "click";
104 };
105 
106 class TestUIScrollView : public UIScrollView {
107 public:
TestUIScrollView()108     TestUIScrollView() {}
~TestUIScrollView()109     virtual ~TestUIScrollView() {}
OnLongPressEvent(const LongPressEvent & event)110     bool OnLongPressEvent(const LongPressEvent& event) override
111     {
112         if (label_ != nullptr) {
113             label_->SetText("long press!");
114             label_->Invalidate();
115         }
116         return UIView::OnLongPressEvent(event);
117     }
118 
OnDragEvent(const DragEvent & event)119     bool OnDragEvent(const DragEvent& event) override
120     {
121         if (label_ != nullptr) {
122             label_->SetText("drag!");
123             label_->Invalidate();
124         }
125         return UIScrollView::OnDragEvent(event);
126     }
127 
OnDragStartEvent(const DragEvent & event)128     bool OnDragStartEvent(const DragEvent& event) override
129     {
130         if (label_ != nullptr) {
131             label_->SetText("drag start!");
132             label_->Invalidate();
133         }
134         return UIScrollView::OnDragStartEvent(event);
135     }
136 
OnDragEndEvent(const DragEvent & event)137     bool OnDragEndEvent(const DragEvent& event) override
138     {
139         if (label_ != nullptr) {
140             label_->SetText("drag end!");
141             label_->Invalidate();
142         }
143         return UIScrollView::OnDragEndEvent(event);
144     }
145 
SetSentence(const char * sentence)146     void SetSentence(const char* sentence)
147     {
148         sentence_ = sentence;
149     }
150 
OnClickEvent(const ClickEvent & event)151     bool OnClickEvent(const ClickEvent& event) override
152     {
153         if (label_ != nullptr) {
154             label_->SetText(sentence_);
155             label_->Invalidate();
156         }
157         return UIView::OnClickEvent(event);
158     }
159 
OnPressEvent(const PressEvent & event)160     bool OnPressEvent(const PressEvent& event) override
161     {
162         if (label_ != nullptr) {
163             label_->SetText("press!");
164             label_->Invalidate();
165         }
166         return UIView::OnPressEvent(event);
167     }
168 
OnReleaseEvent(const ReleaseEvent & event)169     bool OnReleaseEvent(const ReleaseEvent& event) override
170     {
171         if (label_ != nullptr) {
172             label_->SetText("release!");
173             label_->Invalidate();
174         }
175         return UIView::OnReleaseEvent(event);
176     }
177 
OnCancelEvent(const CancelEvent & event)178     bool OnCancelEvent(const CancelEvent& event) override
179     {
180         if (label_ != nullptr) {
181             label_->SetText("cancel!");
182             label_->Invalidate();
183         }
184         return UIView::OnCancelEvent(event);
185     }
186 
SetLabel(UILabel * label)187     void SetLabel(UILabel* label)
188     {
189         label_ = label;
190     }
191 
192 private:
193     UILabel* label_ = nullptr;
194     const char* sentence_ = "click";
195 };
196 
197 class TestKeyInputListener : public RootView::OnKeyActListener {
198 public:
TestKeyInputListener(UILabel * label)199     explicit TestKeyInputListener(UILabel* label) : label_(label) {}
~TestKeyInputListener()200     virtual ~TestKeyInputListener() {}
OnKeyAct(UIView & view,const KeyEvent & event)201     bool OnKeyAct(UIView& view, const KeyEvent& event) override
202     {
203         if (label_ == nullptr) {
204             return true;
205         }
206         switch (event.GetState()) {
207             case InputDevice::STATE_PRESS:
208                 label_->SetText("key pressed!");
209                 break;
210             case InputDevice::STATE_RELEASE:
211                 label_->SetText("key released!");
212                 break;
213             default:
214                 label_->SetText("");
215                 break;
216         }
217         label_->Invalidate();
218         return true;
219     }
220 
221 private:
222     UILabel* label_;
223 };
224 
225 class TestOnClickListener : public UIView::OnClickListener {
226 public:
TestOnClickListener(UILabel * label,char * sentence,bool isConsume)227     explicit TestOnClickListener(UILabel* label, char* sentence, bool isConsume)
228         : label_(label), sentence_(sentence), isConsume_(isConsume)
229     {
230     }
~TestOnClickListener()231     virtual ~TestOnClickListener() {}
OnClick(UIView & view,const ClickEvent & event)232     virtual bool OnClick(UIView& view, const ClickEvent& event)
233     {
234         if (label_ != nullptr) {
235             label_->SetText(sentence_);
236             label_->Invalidate();
237         }
238         return isConsume_;
239     }
240 
241 private:
242     UILabel* label_;
243     char* sentence_;
244     bool isConsume_;
245 };
246 
247 class TestOnLongPressListener : public UIView::OnLongPressListener {
248 public:
TestOnLongPressListener(UILabel * label,char * sentence,bool isConsume)249     explicit TestOnLongPressListener(UILabel* label, char* sentence, bool isConsume)
250         : label_(label), sentence_(sentence), isConsume_(isConsume)
251     {
252     }
~TestOnLongPressListener()253     virtual ~TestOnLongPressListener() {}
OnLongPress(UIView & view,const LongPressEvent & event)254     virtual bool OnLongPress(UIView& view, const LongPressEvent& event)
255     {
256         if (label_ != nullptr) {
257             label_->SetText(sentence_);
258             label_->Invalidate();
259         }
260         return isConsume_;
261     }
262 
263 private:
264     UILabel* label_;
265     char* sentence_;
266     bool isConsume_;
267 };
268 
269 class TestOnTouchListener : public UIView::OnTouchListener {
270 public:
TestOnTouchListener(UILabel * label,char * strPress,char * strRelease,char * strCancel,bool isConsume)271     explicit TestOnTouchListener(UILabel* label, char* strPress, char* strRelease, char* strCancel, bool isConsume)
272         : label_(label), strPress_(strPress), strRelease_(strRelease), strCancel_(strCancel), isConsume_(isConsume)
273     {
274     }
~TestOnTouchListener()275     virtual ~TestOnTouchListener() {}
OnPress(UIView & view,const PressEvent & event)276     virtual bool OnPress(UIView& view, const PressEvent& event)
277     {
278         if (label_ != nullptr) {
279             label_->SetText(strPress_);
280             label_->Invalidate();
281         }
282         return isConsume_;
283     }
284 
OnRelease(UIView & view,const ReleaseEvent & event)285     virtual bool OnRelease(UIView& view, const ReleaseEvent& event)
286     {
287         if (label_ != nullptr) {
288             label_->SetText(strRelease_);
289             label_->Invalidate();
290         }
291         return isConsume_;
292     }
293 
OnCancel(UIView & view,const CancelEvent & event)294     virtual bool OnCancel(UIView& view, const CancelEvent& event)
295     {
296         if (label_ != nullptr) {
297             label_->SetText(strCancel_);
298             label_->Invalidate();
299         }
300         return isConsume_;
301     }
302 
303 private:
304     UILabel* label_;
305     char* strPress_;
306     char* strRelease_;
307     char* strCancel_;
308     bool isConsume_;
309 };
310 
311 class TestOnDragListener : public UIView::OnDragListener {
312 public:
TestOnDragListener(UILabel * label,char * strDragStart,char * strDrag,char * strDragEnd,bool isConsume)313     explicit TestOnDragListener(UILabel* label, char* strDragStart, char* strDrag, char* strDragEnd, bool isConsume)
314         : label_(label), strDragStart_(strDragStart), strDrag_(strDrag), strDragEnd_(strDragEnd), isConsume_(isConsume)
315     {
316     }
~TestOnDragListener()317     virtual ~TestOnDragListener() {}
OnDragStart(UIView & view,const DragEvent & event)318     virtual bool OnDragStart(UIView& view, const DragEvent& event)
319     {
320         if (label_ != nullptr) {
321             label_->SetText(strDragStart_);
322             label_->Invalidate();
323         }
324         return isConsume_;
325     }
326 
OnDrag(UIView & view,const DragEvent & event)327     virtual bool OnDrag(UIView& view, const DragEvent& event)
328     {
329         if (label_ != nullptr) {
330             label_->SetText(strDrag_);
331             label_->Invalidate();
332         }
333         return isConsume_;
334     }
335 
OnDragEnd(UIView & view,const DragEvent & event)336     virtual bool OnDragEnd(UIView& view, const DragEvent& event)
337     {
338         if (label_ != nullptr) {
339             label_->SetText(strDragEnd_);
340             label_->Invalidate();
341         }
342         return isConsume_;
343     }
344 
345 private:
346     UILabel* label_;
347     char* strDragStart_;
348     char* strDrag_;
349     char* strDragEnd_;
350     bool isConsume_;
351 };
352 
353 class UITestInputEvent : public UITest {
354 public:
UITestInputEvent()355     UITestInputEvent() {}
~UITestInputEvent()356     ~UITestInputEvent() {}
357     void SetUp() override;
358     void TearDown() override;
359     const UIView* GetTestView() override;
360 
361     /**
362      * @brief Test if dispatch press\release\longpress\cancel event act normal when target is touchable
363      */
364     void UIKitPointerInputTestDispatchSimpleEvent001();
365     /**
366      * @brief Test if dispatch press\release\longpress\cancel\drag event act normal when target is untouchable
367      */
368     void UIKitPointerInputTestDispatchSimpleEvent002();
369     /**
370      * @brief Test if dispatch drag event act normal when target is touchable and draggable and drag parent instead.
371      */
372     void UIKitPointerInputTestDispatchDragEvent001();
373     /**
374      * @brief Test if dispatch drag event act normal when target is touchable and draggable and not drag parent instead.
375      */
376     void UIKitPointerInputTestDispatchDragEvent002();
377     /**
378      * @brief Test if dispatch drag event act normal when target is untouchable but draggable.
379      */
380     void UIKitPointerInputTestDispatchDragEvent003();
381     /**
382      * @brief Test if dispatch drag event act normal when target is untouchable but draggable.
383      */
384     void UIKitPointerInputTestDispatchKeyEvent001();
385     /**
386      * @brief Test if dispatch drag event act normal when target is untouchable but draggable.
387      */
388     void UIKitPointerInputTestDispatchInVisibleEvent001();
389     /**
390      * @brief Test click, release or longClick event bubble act normal when both of parent and child is triggered.
391      */
392     void UIKitPointerInputTestDispatchBubble001();
393     /**
394      * @brief Test click, release or longClick event bubble act normal when child is triggered but and parent not.
395      */
396     void UIKitPointerInputTestDispatchBubble002();
397     /**
398      * @brief Test click, release or longClick event bubble act normal when child is triggered but and parent not.
399      */
400     void UIKitPointerInputTestDispatchBubble003();
401     /**
402      * @brief Test click, release or longClick event bubble act normal when parent is triggered but and child not.
403      */
404     void UIKitPointerInputTestDispatchBubble004();
405     /**
406      * @brief Test drag event bubble act normal when both of parent and child is triggered.
407      */
408     void UIKitPointerInputTestDispatchBubble005();
409     /**
410      * @brief Test drag event bubble act normal when child is triggered but and parent not.
411      */
412     void UIKitPointerInputTestDispatchBubble006();
413     /**
414      * @brief Test drag event bubble act normal when child is triggered but and parent not.
415      */
416     void UIKitPointerInputTestDispatchBubble007();
417     /**
418      * @brief Test drag event bubble act normal when both of parent and child is not triggered.
419      */
420     void UIKitPointerInputTestDispatchBubble008();
421     /**
422      * @brief Test drag event bubble act normal when parent is triggered but and child not.
423      */
424     void UIKitPointerInputTestDispatchBubble009();
425     /**
426      * @brief Test drag event bubble act normal when parent is triggered but and child not.
427      */
428     void UIKitPointerInputTestDispatchBubble010();
429 
430 private:
431     UIScrollView* container_ = nullptr;
432     TestKeyInputListener* keyListener_ = nullptr;
433     void InnerTest(const char* title, bool touchable, bool draggable, bool dragParent);
434     void InnerBubbleTest(const char* title, bool touchable, bool draggable, bool hasListener, bool isBubble);
435     void InnerBubbleDragTest(const char* title,
436                              bool childDraggable,
437                              bool parentDraggable,
438                              bool hasListener,
439                              bool isBubble);
440     void SetViewAndContainerListeners(bool isBubble, OHOS::UIScrollView* parentContainer,
441                                       TestView* testView, UILabel* label1, UILabel* label2);
442     void SetScrollsListeners(bool isBubble, OHOS::UIScrollView* parentScroll,
443                              OHOS::UIScrollView* childScroll, UILabel* label1, UILabel* label2);
444 };
445 } // namespace OHOS
446 #endif // UI_TEST_INPUT_EVENT_H
447