1 /*
2 * Copyright (c) 2023 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 <optional>
17
18 #include "gtest/gtest.h"
19
20 #include "base/utils/time_util.h"
21 #include "core/event/touch_event.h"
22 #define private public
23 #define protected public
24
25 #include "test/mock/core/pipeline/mock_pipeline_context.h"
26 #include "test/mock/core/render/mock_render_context.h"
27
28 #include "base/memory/ace_type.h"
29 #include "base/memory/referenced.h"
30 #include "core/components_ng/base/frame_node.h"
31 #include "core/components_ng/gestures/tap_gesture.h"
32 #include "core/components_ng/manager/post_event/post_event_manager.h"
33 #include "core/components_ng/pattern/pattern.h"
34
35 using namespace testing;
36 using namespace testing::ext;
37
38 namespace OHOS::Ace::NG {
39 namespace {
40 const std::string ROOT_TAG("root");
41 } // namespace
42
43 class PostEventManagerTestNg : public testing::Test {
44 public:
45 static void SetUpTestSuite();
46 static void TearDownTestSuite();
47 RefPtr<PostEventManager> postEventManager_;
48 RefPtr<FrameNode> root_;
49 void Init();
50 };
51
SetUpTestSuite()52 void PostEventManagerTestNg::SetUpTestSuite()
53 {
54 MockPipelineContext::SetUp();
55 }
56
TearDownTestSuite()57 void PostEventManagerTestNg::TearDownTestSuite()
58 {
59 MockPipelineContext::TearDown();
60 }
61
Init()62 void PostEventManagerTestNg::Init()
63 {
64 postEventManager_ = AceType::MakeRefPtr<PostEventManager>();
65 ASSERT_NE(postEventManager_, nullptr);
66 root_ = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
67 root_->SetExclusiveEventForChild(true);
68 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
69 root_->renderContext_ = mockRenderContext;
70 auto localPoint = PointF(10, 10);
71 mockRenderContext->rect_ = RectF(0, 0, 100, 100);
72 root_->SetActive(true);
73 }
74
75 /**
76 * @tc.name: PostEventManagerTest001
77 * @tc.desc: test post event.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest001, TestSize.Level1)
81 {
82 /**
83 * @tc.steps: step1. construct a FrameNode and set gesture.
84 */
85 Init();
86 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
87 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
88 auto gesture = AceType::MakeRefPtr<TapGesture>();
89 gestureEventHub->AddGesture(gesture);
90 DimensionRect responseRect(Dimension(100), Dimension(100), DimensionOffset());
91 std::vector<DimensionRect> responseRegion;
92 responseRegion.emplace_back(responseRect);
93 gestureEventHub->SetResponseRegion(responseRegion);
94 auto paintRect = root_->renderContext_->GetPaintRectWithoutTransform();
95 root_->GetResponseRegionList(paintRect, 1);
96 EXPECT_FALSE(gestureEventHub->GetResponseRegion().empty());
97
98 /**
99 * @tc.steps: step2. call PostEvent func and check return value.
100 */
101 TouchEvent touchEvent;
102 touchEvent.type = TouchType::DOWN;
103 touchEvent.x = 10;
104 touchEvent.y = 10;
105 auto result = postEventManager_->PostEvent(root_, touchEvent);
106 EXPECT_FALSE(result);
107
108 /**
109 * @tc.steps: step3. call PostEvent func with same event and check return value.
110 */
111 result = postEventManager_->PostEvent(root_, touchEvent);
112 EXPECT_EQ(result, false);
113
114 /**
115 * @tc.steps: step4. call PostEvent func with touch up event and check return value.
116 */
117 TouchEvent touchMoveEvent;
118 touchMoveEvent.type = TouchType::MOVE;
119 touchMoveEvent.x = 15;
120 touchMoveEvent.y = 15;
121 auto currentTime = GetSysTimestamp();
122 std::chrono::nanoseconds nanoseconds(currentTime);
123 TimeStamp time(nanoseconds);
124 touchMoveEvent.time = time;
125 result = postEventManager_->PostEvent(root_, touchMoveEvent);
126 EXPECT_FALSE(result);
127
128 /**
129 * @tc.steps: step5. call PostEvent func with touch up event and check return value.
130 */
131 TouchEvent touchUpEvent;
132 touchUpEvent.type = TouchType::UP;
133 touchUpEvent.x = 15;
134 touchUpEvent.y = 15;
135 currentTime = GetSysTimestamp();
136 std::chrono::nanoseconds nanosecondsUp(currentTime);
137 TimeStamp timeUp(nanosecondsUp);
138 touchUpEvent.time = timeUp;
139 result = postEventManager_->PostEvent(root_, touchUpEvent);
140 EXPECT_FALSE(result);
141 }
142
143 /**
144 * @tc.name: PostEventManagerTest002
145 * @tc.desc: test post touch down type event twice.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest002, TestSize.Level1)
149 {
150 /**
151 * @tc.steps: step1. construct a FrameNode and set gesture.
152 */
153 Init();
154 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
155 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
156 auto gesture = AceType::MakeRefPtr<TapGesture>();
157 gestureEventHub->AddGesture(gesture);
158 DimensionRect responseRect(Dimension(100), Dimension(100), DimensionOffset());
159 std::vector<DimensionRect> responseRegion;
160 responseRegion.emplace_back(responseRect);
161 gestureEventHub->SetResponseRegion(responseRegion);
162 auto paintRect = root_->renderContext_->GetPaintRectWithoutTransform();
163 root_->GetResponseRegionList(paintRect, 1);
164 EXPECT_FALSE(gestureEventHub->GetResponseRegion().empty());
165
166 /**
167 * @tc.steps: step2. call PostEvent func and check return value.
168 */
169 TouchEvent touchEvent;
170 touchEvent.type = TouchType::DOWN;
171 touchEvent.x = 10;
172 touchEvent.y = 10;
173 auto result = postEventManager_->PostEvent(root_, touchEvent);
174 EXPECT_FALSE(result);
175
176 /**
177 * @tc.steps: step3. call PostEvent func with another down event check whether cancel event will be sent or not.
178 */
179 TouchEvent touchMoveEvent;
180 touchMoveEvent.type = TouchType::DOWN;
181 touchMoveEvent.x = 15;
182 touchMoveEvent.y = 15;
183 auto currentTime = GetSysTimestamp();
184 std::chrono::nanoseconds nanoseconds(currentTime);
185 TimeStamp time(nanoseconds);
186 touchMoveEvent.time = time;
187 result = postEventManager_->PostEvent(root_, touchMoveEvent);
188 EXPECT_FALSE(result);
189 }
190
191 /**
192 * @tc.name: PostEventManagerTest003
193 * @tc.desc: test post event when touch test result is null.
194 * @tc.type: FUNC
195 */
196 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest003, TestSize.Level1)
197 {
198 /**
199 * @tc.steps: step1. construct a FrameNode and set gesture.
200 */
201 Init();
202 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
203 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
204 auto gesture = AceType::MakeRefPtr<TapGesture>();
205 gestureEventHub->AddGesture(gesture);
206
207 /**
208 * @tc.steps: step2. call PostEvent func when touch test result is null.
209 */
210 TouchEvent touchEvent;
211 touchEvent.type = TouchType::DOWN;
212 touchEvent.x = 10;
213 touchEvent.y = 10;
214 auto result = postEventManager_->PostEvent(root_, touchEvent);
215 EXPECT_EQ(result, false);
216 }
217
218 /**
219 * @tc.name: PostEventManagerTest004
220 * @tc.desc: test post touch event event but has no down event.
221 * @tc.type: FUNC
222 */
223 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest004, TestSize.Level1)
224 {
225 /**
226 * @tc.steps: step1. construct a FrameNode and set gesture.
227 */
228 Init();
229 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
230 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
231 auto gesture = AceType::MakeRefPtr<TapGesture>();
232 gestureEventHub->AddGesture(gesture);
233 DimensionRect responseRect(Dimension(100), Dimension(100), DimensionOffset());
234 std::vector<DimensionRect> responseRegion;
235 responseRegion.emplace_back(responseRect);
236 gestureEventHub->SetResponseRegion(responseRegion);
237 auto paintRect = root_->renderContext_->GetPaintRectWithoutTransform();
238 root_->GetResponseRegionList(paintRect, 1);
239 EXPECT_FALSE(gestureEventHub->GetResponseRegion().empty());
240
241 /**
242 * @tc.steps: step2. call PostEvent func and check return value.
243 */
244 TouchEvent touchEvent;
245 touchEvent.type = TouchType::MOVE;
246 touchEvent.x = 10;
247 touchEvent.y = 10;
248 auto result = postEventManager_->PostEvent(root_, touchEvent);
249 EXPECT_EQ(result, false);
250 }
251
252 /**
253 * @tc.name: PostEventManagerTest005
254 * @tc.desc: test multi fingers post event.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest005, TestSize.Level1)
258 {
259 /**
260 * @tc.steps: step1. construct a FrameNode and set gesture.
261 */
262 Init();
263 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
264 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
265 auto gesture = AceType::MakeRefPtr<TapGesture>();
266 gestureEventHub->AddGesture(gesture);
267 DimensionRect responseRect(Dimension(100), Dimension(100), DimensionOffset());
268 std::vector<DimensionRect> responseRegion;
269 responseRegion.emplace_back(responseRect);
270 gestureEventHub->SetResponseRegion(responseRegion);
271 auto paintRect = root_->renderContext_->GetPaintRectWithoutTransform();
272 root_->GetResponseRegionList(paintRect, 1);
273 EXPECT_FALSE(gestureEventHub->GetResponseRegion().empty());
274
275 /**
276 * @tc.steps: step2. call finger 0 PostEvent func and check return value.
277 */
278 TouchEvent touchEvent;
279 touchEvent.type = TouchType::DOWN;
280 touchEvent.x = 10;
281 touchEvent.y = 10;
282 auto result = postEventManager_->PostEvent(root_, touchEvent);
283 EXPECT_FALSE(result);
284
285 /**
286 * @tc.steps: step3. call finger 1 PostEvent func and check return value.
287 */
288 TouchEvent anotherTouchEvent;
289 anotherTouchEvent.type = TouchType::DOWN;
290 anotherTouchEvent.id = 1;
291 anotherTouchEvent.x = 20;
292 anotherTouchEvent.y = 20;
293 result = postEventManager_->PostEvent(root_, anotherTouchEvent);
294 EXPECT_FALSE(result);
295
296 /**
297 * @tc.steps: step3. call PostEvent func with touch up event and check return value.
298 */
299 TouchEvent touchMoveEvent;
300 touchMoveEvent.type = TouchType::MOVE;
301 touchMoveEvent.x = 15;
302 touchMoveEvent.y = 15;
303 auto currentTime = GetSysTimestamp();
304 std::chrono::nanoseconds nanoseconds(currentTime);
305 TimeStamp time(nanoseconds);
306 touchMoveEvent.time = time;
307 result = postEventManager_->PostEvent(root_, touchMoveEvent);
308 EXPECT_FALSE(result);
309
310 /**
311 * @tc.steps: step4. call PostEvent func with touch up event and check return value.
312 */
313 TouchEvent touchUpEvent;
314 touchUpEvent.type = TouchType::UP;
315 touchUpEvent.x = 15;
316 touchUpEvent.y = 15;
317 currentTime = GetSysTimestamp();
318 std::chrono::nanoseconds nanosecondsUp(currentTime);
319 TimeStamp timeUp(nanosecondsUp);
320 touchUpEvent.time = timeUp;
321 result = postEventManager_->PostEvent(root_, touchUpEvent);
322 EXPECT_FALSE(result);
323 }
324
325 /**
326 * @tc.name: PostEventManagerTest006
327 * @tc.desc: test multi fingers post event.
328 * @tc.type: FUNC
329 */
330 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest006, TestSize.Level1)
331 {
332 Init();
333 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
334 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
335 auto gesture = AceType::MakeRefPtr<TapGesture>();
336 gestureEventHub->AddGesture(gesture);
337
338 TouchEvent touchUpEvent;
339 touchUpEvent.type = TouchType::HOVER_ENTER;
340 touchUpEvent.x = 15;
341 touchUpEvent.y = 15;
342 auto currentTime = GetSysTimestamp();
343 std::chrono::nanoseconds nanosecondsUp(currentTime);
344 TimeStamp timeUp(nanosecondsUp);
345 touchUpEvent.time = timeUp;
346 postEventManager_->PostEvent(root_, touchUpEvent);
347 }
348
349 /**
350 * @tc.name: PostEventManagerTest007
351 * @tc.desc: test multi fingers post event.
352 * @tc.type: FUNC
353 */
354 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest007, TestSize.Level1)
355 {
356 Init();
357 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
358 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
359 auto gesture = AceType::MakeRefPtr<TapGesture>();
360 gestureEventHub->AddGesture(gesture);
361
362 TouchEvent touchUpEvent;
363 touchUpEvent.type = TouchType::DOWN;
364 touchUpEvent.x = 15;
365 touchUpEvent.y = 15;
366 touchUpEvent.id = 2;
367 auto currentTime = GetSysTimestamp();
368 std::chrono::nanoseconds nanosecondsUp(currentTime);
369 TimeStamp timeUp(nanosecondsUp);
370 touchUpEvent.time = timeUp;
371 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
372 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
373 PostEventAction eventAction;
374 eventAction.targetNode = UInode;
375 eventAction.touchEvent = touchUpEvent;
376 postEventManager_->postEventAction_.push_back(eventAction);
377 auto result = postEventManager_->PostDownEvent(UInode, touchUpEvent);
378 EXPECT_EQ(result, false);
379 }
380
381 /**
382 * @tc.name: PostEventManagerTest008
383 * @tc.desc: test lastEventMap.
384 * @tc.type: FUNC
385 */
386 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest008, TestSize.Level1)
387 {
388 Init();
389 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
390 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
391 auto gesture = AceType::MakeRefPtr<TapGesture>();
392 gestureEventHub->AddGesture(gesture);
393 TouchEvent touchUpEvent;
394 touchUpEvent.type = TouchType::DOWN;
395 touchUpEvent.x = 15;
396 touchUpEvent.y = 15;
397 touchUpEvent.id = 2;
398 auto currentTime = GetSysTimestamp();
399 std::chrono::nanoseconds nanosecondsUp(currentTime);
400 TimeStamp timeUp(nanosecondsUp);
401 touchUpEvent.time = timeUp;
402 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
403 PostEventAction eventAction;
404 eventAction.targetNode = frameNode;
405 eventAction.touchEvent = touchUpEvent;
406 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
407 postEventManager_->postEventAction_.push_back(eventAction);
408 auto result = postEventManager_->PostDownEvent(root_, touchUpEvent);
409 EXPECT_EQ(result, false);
410 }
411
412 /**
413 * @tc.name: PostEventManagerTest009
414 * @tc.desc: test lastEventMap.
415 * @tc.type: FUNC
416 */
417 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest009, TestSize.Level1)
418 {
419 Init();
420 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
421 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
422 auto gesture = AceType::MakeRefPtr<TapGesture>();
423 gestureEventHub->AddGesture(gesture);
424 TouchEvent touchUpEvent;
425 touchUpEvent.type = TouchType::UP;
426 touchUpEvent.x = 15;
427 touchUpEvent.y = 15;
428 touchUpEvent.id = 2;
429 auto currentTime = GetSysTimestamp();
430 std::chrono::nanoseconds nanosecondsUp(currentTime);
431 TimeStamp timeUp(nanosecondsUp);
432 touchUpEvent.time = timeUp;
433 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
434 PostEventAction eventAction;
435 eventAction.targetNode = frameNode;
436 eventAction.touchEvent = touchUpEvent;
437 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
438 postEventManager_->postEventAction_.push_back(eventAction);
439 auto result = postEventManager_->PostUpEvent(root_, touchUpEvent);
440 EXPECT_EQ(result, false);
441 }
442
443 /**
444 * @tc.name: PostEventManagerTest010
445 * @tc.desc: test lastEventMap.
446 * @tc.type: FUNC
447 */
448 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest0010, TestSize.Level1)
449 {
450 Init();
451 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
452 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
453 auto gesture = AceType::MakeRefPtr<TapGesture>();
454 gestureEventHub->AddGesture(gesture);
455
456 TouchEvent touchUpEvent;
457 touchUpEvent.type = TouchType::DOWN;
458 touchUpEvent.x = 15;
459 touchUpEvent.y = 15;
460 touchUpEvent.id = 2;
461 auto currentTime = GetSysTimestamp();
462 std::chrono::nanoseconds nanosecondsUp(currentTime);
463 TimeStamp timeUp(nanosecondsUp);
464 touchUpEvent.time = timeUp;
465 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
466 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
467 PostEventAction eventAction;
468 eventAction.targetNode = UInode;
469 eventAction.touchEvent = touchUpEvent;
470 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
471 postEventManager_->postEventAction_.push_back(eventAction);
472 auto result = postEventManager_->PostDownEvent(UInode, touchUpEvent);
473 EXPECT_EQ(result, false);
474 }
475
476 /**
477 * @tc.name: PostEventManagerTest011
478 * @tc.desc: test lastEventMap.
479 * @tc.type: FUNC
480 */
481 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest0011, TestSize.Level1)
482 {
483 Init();
484 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
485 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
486 auto gesture = AceType::MakeRefPtr<TapGesture>();
487 gestureEventHub->AddGesture(gesture);
488
489 TouchEvent touchUpEvent;
490 touchUpEvent.type = TouchType::UP;
491 touchUpEvent.x = 15;
492 touchUpEvent.y = 15;
493 touchUpEvent.id = 2;
494 auto currentTime = GetSysTimestamp();
495 std::chrono::nanoseconds nanosecondsUp(currentTime);
496 TimeStamp timeUp(nanosecondsUp);
497 touchUpEvent.time = timeUp;
498 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
499 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
500 PostEventAction eventAction;
501 eventAction.targetNode = UInode;
502 eventAction.touchEvent = touchUpEvent;
503 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
504 postEventManager_->postEventAction_.push_back(eventAction);
505 auto result = postEventManager_->PostDownEvent(UInode, touchUpEvent);
506 EXPECT_EQ(result, false);
507 }
508
509 /**
510 * @tc.name: PostEventManagerTest012
511 * @tc.desc: test lastEventMap.
512 * @tc.type: FUNC
513 */
514 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest0012, TestSize.Level1)
515 {
516 Init();
517 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
518 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
519 auto gesture = AceType::MakeRefPtr<TapGesture>();
520 gestureEventHub->AddGesture(gesture);
521
522 TouchEvent touchUpEvent;
523 touchUpEvent.type = TouchType::DOWN;
524 touchUpEvent.x = 15;
525 touchUpEvent.y = 15;
526 touchUpEvent.id = 2;
527 auto currentTime = GetSysTimestamp();
528 std::chrono::nanoseconds nanosecondsUp(currentTime);
529 TimeStamp timeUp(nanosecondsUp);
530 touchUpEvent.time = timeUp;
531 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
532 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
533 PostEventAction eventAction;
534 eventAction.targetNode = UInode;
535 eventAction.touchEvent = touchUpEvent;
536 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
537 postEventManager_->postEventAction_.push_back(eventAction);
538 auto result = postEventManager_->PostDownEvent(UInode, touchUpEvent);
539 EXPECT_EQ(result, false);
540 }
541
542 /**
543 * @tc.name: PostEventManagerTest013
544 * @tc.desc: test lastEventMap.
545 * @tc.type: FUNC
546 */
547 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest0013, TestSize.Level1)
548 {
549 Init();
550 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
551 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
552 auto gesture = AceType::MakeRefPtr<TapGesture>();
553 gestureEventHub->AddGesture(gesture);
554
555 TouchEvent touchUpEvent;
556 touchUpEvent.type = TouchType::DOWN;
557 touchUpEvent.x = 15;
558 touchUpEvent.y = 15;
559 touchUpEvent.id = 2;
560 auto currentTime = GetSysTimestamp();
561 std::chrono::nanoseconds nanosecondsUp(currentTime);
562 TimeStamp timeUp(nanosecondsUp);
563 touchUpEvent.time = timeUp;
564 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
565 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
566 PostEventAction eventAction;
567 eventAction.targetNode = UInode;
568 eventAction.touchEvent = touchUpEvent;
569 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
570 postEventManager_->postEventAction_.push_back(eventAction);
571 auto result = postEventManager_->PostMoveEvent(UInode, touchUpEvent);
572 EXPECT_EQ(result, true);
573 }
574
575 /**
576 * @tc.name: PostEventManagerTest014
577 * @tc.desc: test lastEventMap.
578 * @tc.type: FUNC
579 */
580 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest014, TestSize.Level1)
581 {
582 Init();
583 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
584 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
585 auto gesture = AceType::MakeRefPtr<TapGesture>();
586 gestureEventHub->AddGesture(gesture);
587
588 TouchEvent touchUpEvent;
589 touchUpEvent.type = TouchType::DOWN;
590 touchUpEvent.x = 15;
591 touchUpEvent.y = 15;
592 touchUpEvent.id = 2;
593 auto currentTime = GetSysTimestamp();
594 std::chrono::nanoseconds nanosecondsUp(currentTime);
595 TimeStamp timeUp(nanosecondsUp);
596 touchUpEvent.time = timeUp;
597 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
598 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
599 PostEventAction eventAction;
600 eventAction.targetNode = UInode;
601 eventAction.touchEvent = touchUpEvent;
602 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
603 postEventManager_->postEventAction_.push_back(eventAction);
604 auto result = postEventManager_->PostUpEvent(UInode, touchUpEvent);
605 EXPECT_EQ(result, true);
606 }
607
608 /**
609 * @tc.name: PostEventManagerTest015
610 * @tc.desc: test lastEventMap.
611 * @tc.type: FUNC
612 */
613 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest015, TestSize.Level1)
614 {
615 Init();
616 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
617 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
618 auto gesture = AceType::MakeRefPtr<TapGesture>();
619 gestureEventHub->AddGesture(gesture);
620
621 TouchEvent touchUpEvent;
622 touchUpEvent.type = TouchType::DOWN;
623 touchUpEvent.x = 15;
624 touchUpEvent.y = 15;
625 touchUpEvent.id = 2;
626 auto currentTime = GetSysTimestamp();
627 std::chrono::nanoseconds nanosecondsUp(currentTime);
628 TimeStamp timeUp(nanosecondsUp);
629 touchUpEvent.time = timeUp;
630 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
631 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
632 PostEventAction eventAction;
633 eventAction.targetNode = UInode;
634 eventAction.touchEvent = touchUpEvent;
635 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
636 postEventManager_->postEventAction_.push_back(eventAction);
637 postEventManager_->CheckAndClearPostEventAction(UInode, touchUpEvent.id);
638 SUCCEED();
639 postEventManager_->CheckAndClearPostEventAction(UInode, (touchUpEvent.id + 1));
640 SUCCEED();
641 postEventManager_->CheckAndClearPostEventAction(UInode, touchUpEvent.id);
642 SUCCEED();
643 }
644
645 /**
646 * @tc.name: PostEventManagerTest016
647 * @tc.desc: test lastEventMap.
648 * @tc.type: FUNC
649 */
650 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest016, TestSize.Level1)
651 {
652 Init();
653 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
654 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
655 auto gesture = AceType::MakeRefPtr<TapGesture>();
656 gestureEventHub->AddGesture(gesture);
657
658 TouchEvent touchUpEvent;
659 touchUpEvent.type = TouchType::UP;
660 touchUpEvent.x = 15;
661 touchUpEvent.y = 15;
662 touchUpEvent.id = 2;
663 auto currentTime = GetSysTimestamp();
664 std::chrono::nanoseconds nanosecondsUp(currentTime);
665 TimeStamp timeUp(nanosecondsUp);
666 touchUpEvent.time = timeUp;
667 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
668 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
669 PostEventAction eventAction;
670 eventAction.targetNode = UInode;
671 eventAction.touchEvent = touchUpEvent;
672 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
673 postEventManager_->postEventAction_.push_back(eventAction);
674 postEventManager_->CheckAndClearPostEventAction(UInode, touchUpEvent.id);
675 SUCCEED();
676 }
677
678 /**
679 * @tc.name: PostEventManagerTest017
680 * @tc.desc: test lastEventMap.
681 * @tc.type: FUNC
682 */
683 HWTEST_F(PostEventManagerTestNg, PostEventManagerTest017, TestSize.Level1)
684 {
685 Init();
686 auto gestureEventHub = root_->GetOrCreateGestureEventHub();
687 gestureEventHub->SetHitTestMode(HitTestMode::HTMBLOCK);
688 auto gesture = AceType::MakeRefPtr<TapGesture>();
689 gestureEventHub->AddGesture(gesture);
690
691 TouchEvent touchUpEvent;
692 touchUpEvent.type = TouchType::CANCEL;
693 touchUpEvent.x = 15;
694 touchUpEvent.y = 15;
695 touchUpEvent.id = 2;
696 auto currentTime = GetSysTimestamp();
697 std::chrono::nanoseconds nanosecondsUp(currentTime);
698 TimeStamp timeUp(nanosecondsUp);
699 touchUpEvent.time = timeUp;
700 auto frameNode = AceType::MakeRefPtr<FrameNode>(ROOT_TAG, -1, AceType::MakeRefPtr<Pattern>(), true);
701 auto UInode = AceType::DynamicCast<NG::UINode>(frameNode);
702 PostEventAction eventAction;
703 eventAction.targetNode = UInode;
704 eventAction.touchEvent = touchUpEvent;
705 postEventManager_->lastEventMap_.insert(std::make_pair(touchUpEvent.id, eventAction));
706 postEventManager_->postEventAction_.push_back(eventAction);
707 postEventManager_->CheckAndClearPostEventAction(UInode, touchUpEvent.id);
708 SUCCEED();
709 }
710
711 /**
712 * @tc.name: HandlePostEventTest001
713 * @tc.desc: test HandlePostEvent func.
714 * @tc.type: FUNC
715 */
716 HWTEST_F(PostEventManagerTestNg, HandlePostEventTest001, TestSize.Level1)
717 {
718 /**
719 * @tc.steps: step1. construct a FrameNode and set gesture.
720 */
721 Init();
722
723 /**
724 * @tc.steps: step2. mock user touch event.
725 */
726 auto buttonNode = FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, 1,
__anone84395a40202() 727 []() { return AceType::MakeRefPtr<Pattern>(); });
728 TouchEvent touchEvent;
729
730 const std::vector<Ace::TouchType> touchTypeArray = { Ace::TouchType::DOWN, Ace::TouchType::UP };
731 int32_t touchStateCount = 4;
732 for (int32_t i = 0; i < touchStateCount; ++i) {
733 int32_t index = i % touchTypeArray.size();
734 touchEvent.type = touchTypeArray[index];
735 touchEvent.id = touchTypeArray.size() > 0 ? i / touchTypeArray.size() : i;
736 postEventManager_->HandlePostEvent(buttonNode, touchEvent);
737 }
738 EXPECT_TRUE(postEventManager_->lastEventMap_.empty());
739 }
740
741 /**
742 * @tc.name: PostDownEventTest001
743 * @tc.desc: test PostDownEvent func.
744 * @tc.type: FUNC
745 */
746 HWTEST_F(PostEventManagerTestNg, PostDownEventTest001, TestSize.Level1)
747 {
748 /**
749 * @tc.steps: step1. construct a FrameNode and set gesture.
750 */
751 Init();
752
753 /**
754 * @tc.steps: step2. Simulate when the user touchDown and then handles the out-of-hand
755 * action event through the PostDownEvent function.
756 */
757 int32_t nodeId = 1;
758 auto buttonNode = FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG, nodeId,
__anone84395a40302() 759 []() { return AceType::MakeRefPtr<Pattern>(); });
760 TouchEvent touchEvent;
761 touchEvent.type = Ace::TouchType::DOWN;
762 touchEvent.id = nodeId;
763 postEventManager_->HandlePostEvent(buttonNode, touchEvent);
764
765 postEventManager_->PostDownEvent(buttonNode, touchEvent);
766 EXPECT_TRUE(postEventManager_->postEventAction_.empty());
767 EXPECT_TRUE(postEventManager_->lastEventMap_.empty());
768 }
769 } // namespace OHOS::Ace::NG
770