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 #include "ui_test_input_event.h"
17 
18 #include "common/screen.h"
19 #include "components/root_view.h"
20 
21 namespace OHOS {
22 namespace {
23 const int16_t ITEM_H = 50;
24 const int16_t TEXT_H = 29;
25 const int16_t TEXT_W = 250;
26 const int16_t TEST_VIEW_H = 40;
27 const int16_t TEST_VIEW_W = 40;
28 const int16_t GAP = 5;
29 const int16_t TEST_VIEW_GAP = 80;
30 } // namespace
31 
SetUp()32 void UITestInputEvent::SetUp()
33 {
34     if (container_ == nullptr) {
35         container_ = new UIScrollView();
36         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
37         container_->SetHorizontalScrollState(false);
38     }
39     positionX_ = 48; // 48: position x
40     positionY_ = 0;
41 }
42 
DeleteChildrenAndListener(UIView * view)43 void DeleteChildrenAndListener(UIView* view)
44 {
45     if (view == nullptr) {
46         return;
47     }
48     while (view != nullptr) {
49         UIView* tempView = view;
50         view = view->GetNextSibling();
51         if (tempView->IsViewGroup()) {
52             DeleteChildrenAndListener(static_cast<UIViewGroup*>(tempView)->GetChildrenHead());
53         }
54         if (tempView->GetParent()) {
55             static_cast<UIViewGroup*>(tempView->GetParent())->Remove(tempView);
56         }
57 
58         if (tempView->GetOnClickListener()) {
59             delete tempView->GetOnClickListener();
60         }
61         if (tempView->GetOnDragListener()) {
62             delete tempView->GetOnDragListener();
63         }
64         if (tempView->GetOnLongPressListener()) {
65             delete tempView->GetOnLongPressListener();
66         }
67         if (tempView->GetTouchListener()) {
68             delete tempView->GetTouchListener();
69         }
70 
71         delete tempView;
72     }
73 }
74 
TearDown()75 void UITestInputEvent::TearDown()
76 {
77     DeleteChildrenAndListener(container_);
78     container_ = nullptr;
79     RootView::GetInstance()->ClearOnKeyActListener();
80     if (keyListener_ != nullptr) {
81         delete keyListener_;
82         keyListener_ = nullptr;
83     }
84 }
85 
GetTestView()86 const UIView* UITestInputEvent::GetTestView()
87 {
88     UIKitPointerInputTestDispatchSimpleEvent001();
89     UIKitPointerInputTestDispatchSimpleEvent002();
90     UIKitPointerInputTestDispatchDragEvent001();
91     UIKitPointerInputTestDispatchDragEvent002();
92     UIKitPointerInputTestDispatchDragEvent003();
93     UIKitPointerInputTestDispatchKeyEvent001();
94     UIKitPointerInputTestDispatchInVisibleEvent001();
95     UIKitPointerInputTestDispatchBubble001();
96     UIKitPointerInputTestDispatchBubble002();
97     UIKitPointerInputTestDispatchBubble003();
98     UIKitPointerInputTestDispatchBubble004();
99     UIKitPointerInputTestDispatchBubble005();
100     UIKitPointerInputTestDispatchBubble006();
101     UIKitPointerInputTestDispatchBubble007();
102     UIKitPointerInputTestDispatchBubble008();
103     UIKitPointerInputTestDispatchBubble009();
104     UIKitPointerInputTestDispatchBubble010();
105     return container_;
106 }
107 
UIKitPointerInputTestDispatchSimpleEvent001()108 void UITestInputEvent::UIKitPointerInputTestDispatchSimpleEvent001()
109 {
110     InnerTest("可点击对象事件测试 ", true, false, false);
111 }
112 
UIKitPointerInputTestDispatchSimpleEvent002()113 void UITestInputEvent::UIKitPointerInputTestDispatchSimpleEvent002()
114 {
115     InnerTest("不可点击对象事件测试 ", false, false, false);
116 }
117 
UIKitPointerInputTestDispatchDragEvent001()118 void UITestInputEvent::UIKitPointerInputTestDispatchDragEvent001()
119 {
120     InnerTest("可点击可拖拽dragparent测试 ", true, true, true);
121 }
122 
UIKitPointerInputTestDispatchDragEvent002()123 void UITestInputEvent::UIKitPointerInputTestDispatchDragEvent002()
124 {
125     InnerTest("可点击可拖拽非dragparent测试 ", true, true, false);
126 }
127 
UIKitPointerInputTestDispatchDragEvent003()128 void UITestInputEvent::UIKitPointerInputTestDispatchDragEvent003()
129 {
130     InnerTest("不可点击可拖拽测试 ", false, true, false);
131 }
132 
UIKitPointerInputTestDispatchBubble001()133 void UITestInputEvent::UIKitPointerInputTestDispatchBubble001()
134 {
135     positionY_ = 0;
136     InnerBubbleTest("可点击有监听事件不消费冒泡测试 ", true, true, true, false);
137 }
138 
UIKitPointerInputTestDispatchBubble002()139 void UITestInputEvent::UIKitPointerInputTestDispatchBubble002()
140 {
141     InnerBubbleTest("可点击有监听事件消费冒泡测试 ", true, true, true, true);
142 }
143 
UIKitPointerInputTestDispatchBubble003()144 void UITestInputEvent::UIKitPointerInputTestDispatchBubble003()
145 {
146     InnerBubbleTest("可点击无监听事件不消费冒泡测试 ", true, true, false, false);
147 }
148 
UIKitPointerInputTestDispatchBubble004()149 void UITestInputEvent::UIKitPointerInputTestDispatchBubble004()
150 {
151     InnerBubbleTest("不可点击有监听事件消费冒泡测试 ", false, false, true, true);
152 }
153 
UIKitPointerInputTestDispatchBubble005()154 void UITestInputEvent::UIKitPointerInputTestDispatchBubble005()
155 {
156     InnerBubbleDragTest("子父可拖拽有监听事件不消费冒泡测试 ", true, true, true, false);
157 }
158 
UIKitPointerInputTestDispatchBubble006()159 void UITestInputEvent::UIKitPointerInputTestDispatchBubble006()
160 {
161     InnerBubbleDragTest("子父可拖拽有监听事件消费冒泡测试 ", true, true, true, true);
162 }
163 
UIKitPointerInputTestDispatchBubble007()164 void UITestInputEvent::UIKitPointerInputTestDispatchBubble007()
165 {
166     InnerBubbleDragTest("子父可拖拽无监听事件消费冒泡测试 ", true, true, false, true);
167 }
168 
UIKitPointerInputTestDispatchBubble008()169 void UITestInputEvent::UIKitPointerInputTestDispatchBubble008()
170 {
171     InnerBubbleDragTest("子父不可拖拽有监听事件消费冒泡测试 ", false, false, true, true);
172 }
173 
UIKitPointerInputTestDispatchBubble009()174 void UITestInputEvent::UIKitPointerInputTestDispatchBubble009()
175 {
176     InnerBubbleDragTest("子不可拖拽父可拖拽有监听事件消费冒泡测试 ", false, true, true, true);
177 }
178 
UIKitPointerInputTestDispatchBubble010()179 void UITestInputEvent::UIKitPointerInputTestDispatchBubble010()
180 {
181     InnerBubbleDragTest("子不可拖拽父可拖拽有监听事件不消费冒泡测试 ", false, true, true, false);
182 }
183 
UIKitPointerInputTestDispatchKeyEvent001()184 void UITestInputEvent::UIKitPointerInputTestDispatchKeyEvent001()
185 {
186     if (container_ != nullptr) {
187         UIViewGroup* uiViewGroup = new UIViewGroup();
188         // 2: half of screen width;
189         uiViewGroup->SetPosition(
190             TEXT_DISTANCE_TO_LEFT_SIDE, positionY_,
191             (Screen::GetInstance().GetWidth() / 2 - TEXT_DISTANCE_TO_LEFT_SIDE), // 2: half of screen width;
192             128);                                                                // 128: height
193         container_->Add(uiViewGroup);
194 
195         UILabel* label = new UILabel();
196         uiViewGroup->Add(label);
197         // 2: half of screen width;
198         label->SetPosition(0, 0, Screen::GetInstance().GetWidth() / 2, TEXT_H);
199         label->SetText("物理按键事件测试 ");
200         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
201         positionY_ += (TEXT_H + GAP);
202         UILabel* label1 = new UILabel();
203         uiViewGroup->Add(label1);
204         label1->SetPosition(0, TEXT_H + GAP, TEXT_W, TEXT_H);
205         label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
206         if (keyListener_ == nullptr) {
207             keyListener_ = new TestKeyInputListener(label1);
208         }
209         RootView::GetInstance()->SetOnKeyActListener(keyListener_);
210         positionY_ += ITEM_H;
211     }
212 }
213 
UIKitPointerInputTestDispatchInVisibleEvent001()214 void UITestInputEvent::UIKitPointerInputTestDispatchInVisibleEvent001()
215 {
216     if (container_ != nullptr) {
217         UIViewGroup* uiViewGroup = new UIViewGroup();
218         // 2: half of screen width; 36: decrease x-coordinate; 90: y-coordinate
219         uiViewGroup->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, 480, 128); // 480: width; 128: height
220         container_->Add(uiViewGroup);
221         UILabel* label = new UILabel();
222         uiViewGroup->Add(label);
223         // 2: half of screen width;
224         label->SetPosition(0, 0, Screen::GetInstance().GetWidth() / 2, TEXT_H);
225         label->SetText("不可见对象事件传递测试 ");
226         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
227         positionY_ = 0;
228         positionY_ += (TEXT_H + GAP * 2); // 2: double GAP
229         UIViewGroup* group1 = new UIViewGroup();
230         uiViewGroup->Add(group1);
231         // 20: increase width 20: increase  height
232         group1->SetPosition(positionX_, positionY_, TEST_VIEW_W + 20, TEST_VIEW_H + 20);
233         group1->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
234         group1->SetVisible(false);
235         TestView* testView = new TestView();
236         group1->Add(testView);
237         testView->SetPosition(5, 5, TEST_VIEW_W, TEST_VIEW_H); // 5: position x 5:position y
238         testView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
239         testView->SetTouchable(true);
240         testView->SetSentence("Click From test 0!");
241         UIViewGroup* group2 = new UIViewGroup();
242         uiViewGroup->Add(group2);
243         // 20: increase width 20: increase  height
244         group2->SetPosition(0, positionY_, TEST_VIEW_W + 20, TEST_VIEW_H + 20);
245         group2->SetStyle(STYLE_BACKGROUND_COLOR, Color::White().full);
246         TestView* testView1 = new TestView();
247         group2->Add(testView1);
248         testView1->SetPosition(5, 5, TEST_VIEW_W, TEST_VIEW_H); // 5: position x 5:position y
249         testView1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
250         testView1->SetTouchable(true);
251         testView1->SetSentence("Click From test 1!");
252         UILabel* label1 = new UILabel();
253         uiViewGroup->Add(label1);
254         // 50: increase width; 2:double GAP
255         label1->SetPosition(positionX_ + 50, positionY_ + 2 * GAP, TEXT_W, TEXT_H);
256         label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
257         testView->SetLabel(label1);
258         testView1->SetLabel(label1);
259         positionY_ += ITEM_H;
260     }
261 }
262 
InnerTest(const char * title,bool touchable,bool draggable,bool dragParent)263 void UITestInputEvent::InnerTest(const char* title, bool touchable, bool draggable, bool dragParent)
264 {
265     if (container_ != nullptr) {
266         UILabel* label = new UILabel();
267         container_->Add(label);
268         // 2: half Screen width
269         label->SetPosition(positionX_, positionY_, Screen::GetInstance().GetWidth() / 2, TEXT_H);
270         label->SetText(title);
271         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
272         positionY_ += (TEXT_H + GAP);
273         auto testView = new TestView();
274         container_->Add(testView);
275         testView->SetPosition(positionX_, positionY_, TEST_VIEW_W, TEST_VIEW_H);
276         testView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
277         testView->SetTouchable(touchable);
278         testView->SetDraggable(draggable);
279         testView->SetDragParentInstead(dragParent);
280         UILabel* label1 = new UILabel();
281         container_->Add(label1);
282         label1->SetPosition(positionX_ + TEST_VIEW_GAP, positionY_ + 2 * GAP, TEXT_W, TEXT_H); // 2: double GAP
283         label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
284         testView->SetLabel(label1);
285         positionY_ += ITEM_H;
286     }
287 }
288 
InnerBubbleTest(const char * title,bool touchable,bool draggable,bool hasListener,bool isBubble)289 void UITestInputEvent::InnerBubbleTest(const char* title,
290                                        bool touchable,
291                                        bool draggable,
292                                        bool hasListener,
293                                        bool isBubble)
294 {
295     if (container_ != nullptr) {
296         UILabel* label = new UILabel();
297         container_->Add(label);
298         label->SetPosition((Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE), // 2: half screen width
299                            positionY_,
300                            (Screen::GetInstance().GetWidth() / 2 - TEXT_DISTANCE_TO_LEFT_SIDE), // 2: half screen width
301                            TEXT_H);
302         label->SetText(title);
303         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
304         positionY_ += (TEXT_H + GAP);
305 
306         OHOS::UIScrollView* parentContainer = new UIScrollView();
307         // 2: half screen width
308         parentContainer->SetPosition(Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE, positionY_,
309                                      Screen::GetInstance().GetWidth() - TEXT_DISTANCE_TO_LEFT_SIDE, ITEM_H);
310         container_->Add(parentContainer);
311         TestView* testView = new TestView();
312         parentContainer->Add(testView);
313         testView->SetPosition(0, 0, TEST_VIEW_W, TEST_VIEW_H);
314         testView->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
315         testView->SetTouchable(touchable);
316         testView->SetDraggable(draggable);
317         testView->SetDragParentInstead(true);
318         UILabel* label1 = new UILabel();
319         parentContainer->Add(label1);
320         label1->SetPosition(positionX_ + TEST_VIEW_GAP, 2 * GAP, TEXT_W, TEXT_H); // 2: tow gap
321         label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
322         testView->SetLabel(label1);
323 
324         UILabel* label2 = new UILabel();
325         parentContainer->Add(label2);
326         label2->SetPosition(positionX_ + 2 * TEST_VIEW_GAP, 2 * GAP, TEXT_W, TEXT_H); // 2: tow gap
327         label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
328         testView->SetLabel2(label2);
329 
330         if (hasListener) {
331             SetViewAndContainerListeners(isBubble, parentContainer, testView, label1, label2);
332         }
333 
334         positionY_ += ITEM_H;
335     }
336 }
337 
SetViewAndContainerListeners(bool isBubble,OHOS::UIScrollView * parentContainer,TestView * testView,UILabel * label1,UILabel * label2)338 void UITestInputEvent::SetViewAndContainerListeners(bool isBubble, OHOS::UIScrollView* parentContainer,
339                                                     TestView* testView, UILabel* label1, UILabel* label2)
340 {
341     UIView::OnClickListener* clickListenerParent =
342         new TestOnClickListener(label2, const_cast<char*>("l-parent click"), isBubble);
343     UIView::OnClickListener* clickListenerChild =
344         new TestOnClickListener(label1, const_cast<char*>("l-click"), isBubble);
345     testView->SetOnClickListener(clickListenerChild);
346     parentContainer->SetOnClickListener(clickListenerParent);
347 
348     UIView::OnLongPressListener* longTouchListenerParent =
349         new TestOnLongPressListener(label2, const_cast<char*>("l-parent long press"), isBubble);
350     UIView::OnLongPressListener* longTouchListenerChild =
351         new TestOnLongPressListener(label1, const_cast<char*>("l-long press"), isBubble);
352     testView->SetOnLongPressListener(longTouchListenerChild);
353     parentContainer->SetOnLongPressListener(longTouchListenerParent);
354 
355     UIView::OnTouchListener* touchListenerParent = new TestOnTouchListener(
356         label2, const_cast<char*>("l-parent press"), const_cast<char*>("l-parent release"),
357         const_cast<char*>("l-parent cancel"), isBubble);
358     UIView::OnTouchListener* touchListenerChild =
359         new TestOnTouchListener(label1, const_cast<char*>("l-press"), const_cast<char*>("l-release"),
360                                 const_cast<char*>("l-cancel"), isBubble);
361     testView->SetOnTouchListener(touchListenerChild);
362     parentContainer->SetOnTouchListener(touchListenerParent);
363 }
364 
InnerBubbleDragTest(const char * title,bool childDraggable,bool parentDraggable,bool hasListener,bool isBubble)365 void UITestInputEvent::InnerBubbleDragTest(const char* title,
366                                            bool childDraggable,
367                                            bool parentDraggable,
368                                            bool hasListener,
369                                            bool isBubble)
370 {
371     int32_t itemH1 = ITEM_H * 2; // 2 times of ITEM_H
372     int32_t itemH2 = itemH1 + ITEM_H;
373     int32_t itemH3 = itemH2 + ITEM_H;
374     int32_t halfScreenWith = Screen::GetInstance().GetWidth() / 2; // 2: half screen width
375     if (container_ != nullptr) {
376         int32_t offset = 30;                                         // 40 pixel offset
377         UILabel* label = new UILabel();
378         container_->Add(label);
379         // 2: half screen width
380         label->SetPosition(halfScreenWith + TEXT_DISTANCE_TO_LEFT_SIDE, positionY_,
381                            halfScreenWith - TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_H);
382         label->SetText(title);
383         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
384         positionY_ += (TEXT_H + GAP);
385 
386         OHOS::TestUIScrollView* parentScroll = new TestUIScrollView();
387         parentScroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
388         parentScroll->SetPosition(halfScreenWith + TEXT_DISTANCE_TO_LEFT_SIDE, positionY_, itemH1, itemH1);
389         parentScroll->SetThrowDrag(parentDraggable);
390         parentScroll->SetDraggable(parentDraggable);
391         container_->Add(parentScroll);
392 
393         OHOS::TestUIScrollView* childScroll = new TestUIScrollView();
394         childScroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
395         childScroll->SetPosition(offset, offset, itemH2, itemH2);
396         childScroll->SetThrowDrag(childDraggable);
397         childScroll->SetDraggable(childDraggable);
398         parentScroll->Add(childScroll);
399 
400         UILabelButton* button1 = new UILabelButton();
401         button1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full);
402         button1->SetText("button1");
403         button1->SetPosition(offset, offset, itemH3 * 2, itemH3); // 2: tow width
404         childScroll->Add(button1);
405 
406         UILabel* label1 = new UILabel();
407         container_->Add(label1);
408         label1->SetPosition(itemH3 + offset + halfScreenWith, positionY_ + 2 * GAP, TEXT_W, TEXT_H); // 2: tow gap
409         label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
410         childScroll->SetLabel(label1);
411 
412         UILabel* label2 = new UILabel();
413         container_->Add(label2);
414         label2->SetPosition(itemH3 + offset + halfScreenWith, positionY_ + 6 * GAP, TEXT_W, TEXT_H); // 6: tow gap
415         label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
416         parentScroll->SetLabel(label2);
417 
418         if (hasListener) {
419             SetScrollsListeners(isBubble, parentScroll, childScroll, label1, label2);
420         }
421         positionY_ += itemH1;
422     }
423 }
424 
SetScrollsListeners(bool isBubble,OHOS::UIScrollView * parentScroll,OHOS::UIScrollView * childScroll,UILabel * label1,UILabel * label2)425 void UITestInputEvent::SetScrollsListeners(bool isBubble, OHOS::UIScrollView* parentScroll,
426                                            OHOS::UIScrollView* childScroll, UILabel* label1, UILabel* label2)
427 {
428     UIView::OnClickListener* clickListenerParent =
429         new TestOnClickListener(label2, const_cast<char*>("l-parent click"), isBubble);
430     UIView::OnClickListener* clickListenerChild =
431         new TestOnClickListener(label1, const_cast<char*>("l-click"), isBubble);
432     childScroll->SetOnClickListener(clickListenerChild);
433     parentScroll->SetOnClickListener(clickListenerParent);
434 
435     UIView::OnLongPressListener* longTouchListenerParent =
436         new TestOnLongPressListener(label2, const_cast<char*>("l-parent long press"), isBubble);
437     UIView::OnLongPressListener* longTouchListenerChild =
438         new TestOnLongPressListener(label1, const_cast<char*>("l-long press"), isBubble);
439     childScroll->SetOnLongPressListener(longTouchListenerChild);
440     parentScroll->SetOnLongPressListener(longTouchListenerParent);
441 
442     UIView::OnTouchListener* touchListenerParent = new TestOnTouchListener(
443         label2, const_cast<char*>("l-parent press"), const_cast<char*>("l-parent release"),
444         const_cast<char*>("l-parent cancel"), isBubble);
445     UIView::OnTouchListener* touchListenerChild =
446         new TestOnTouchListener(label1, const_cast<char*>("l-press"), const_cast<char*>("l-release"),
447                                 const_cast<char*>("l-cancel"), isBubble);
448     childScroll->SetOnTouchListener(touchListenerChild);
449     parentScroll->SetOnTouchListener(touchListenerParent);
450 
451     UIView::OnDragListener* dragListenerParent = new TestOnDragListener(
452         label2, const_cast<char*>("l-dragStart parent"), const_cast<char*>("l-drag parent"),
453         const_cast<char*>("l-dragEnd parent"), isBubble);
454     UIView::OnDragListener* dragListenerChild =
455         new TestOnDragListener(label1, const_cast<char*>("l-dragStart"), const_cast<char*>("l-drag"),
456                                 const_cast<char*>("l-dragEnd"), isBubble);
457     childScroll->SetOnDragListener(dragListenerChild);
458     parentScroll->SetOnDragListener(dragListenerParent);
459 }
460 } // namespace OHOS
461