1 /*
2 * Copyright (c) 2024 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 "swiper_test_ng.h"
17 #include "test/mock/core/pattern/mock_nestable_scroll_container.h"
18 #include "test/mock/core/render/mock_render_context.h"
19
20 #include "core/components/swiper/swiper_component.h"
21
22 namespace OHOS::Ace::NG {
23
24 namespace {
25 constexpr float DRAG_DELTA = 400.0f;
26 } // namespace
27
28 class SwiperEventTestNg : public SwiperTestNg {
29 public:
30 void HandleDrag(GestureEvent info);
31 void HandleDragStart(GestureEvent info);
32 void HandleDragUpdate(GestureEvent info);
33 void HandleDragEnd(GestureEvent info);
34 void HandleDragCancel();
35 void MockPaintRect(const RefPtr<FrameNode>& frameNode);
36 GestureEvent CreateDragInfo(bool moveDirection);
37 };
38
HandleDrag(GestureEvent info)39 void SwiperEventTestNg::HandleDrag(GestureEvent info)
40 {
41 HandleDragStart(info);
42 HandleDragUpdate(info);
43 HandleDragEnd(info);
44 FlushLayoutTask(frameNode_);
45 }
46
HandleDragStart(GestureEvent info)47 void SwiperEventTestNg::HandleDragStart(GestureEvent info)
48 {
49 pattern_->panEvent_->GetActionStartEventFunc()(info);
50 }
51
HandleDragUpdate(GestureEvent info)52 void SwiperEventTestNg::HandleDragUpdate(GestureEvent info)
53 {
54 pattern_->panEvent_->GetActionUpdateEventFunc()(info);
55 }
56
HandleDragEnd(GestureEvent info)57 void SwiperEventTestNg::HandleDragEnd(GestureEvent info)
58 {
59 pattern_->panEvent_->GetActionEndEventFunc()(info);
60 }
61
HandleDragCancel()62 void SwiperEventTestNg::HandleDragCancel()
63 {
64 pattern_->panEvent_->GetActionCancelEventFunc()();
65 }
66
MockPaintRect(const RefPtr<FrameNode> & frameNode)67 void SwiperEventTestNg::MockPaintRect(const RefPtr<FrameNode>& frameNode)
68 {
69 auto mockRenderContext = AceType::DynamicCast<MockRenderContext>(frameNode->renderContext_);
70 mockRenderContext->paintRect_ = RectF(0.f, 0.f, SWIPER_WIDTH, SWIPER_HEIGHT);
71 }
72
CreateDragInfo(bool moveDirection)73 GestureEvent SwiperEventTestNg::CreateDragInfo(bool moveDirection)
74 {
75 GestureEvent info;
76 info.SetInputEventType(InputEventType::AXIS);
77 info.SetSourceTool(SourceTool::TOUCHPAD);
78 info.SetGlobalLocation(Offset(100.f, 100.f));
79 info.SetMainDelta(moveDirection ? -DRAG_DELTA : DRAG_DELTA);
80 info.SetMainVelocity(moveDirection ? -2000.f : 2000.f);
81 return info;
82 }
83
84 /**
85 * @tc.name: HandleDrag001
86 * @tc.desc: HandleDrag with AXIS and MOUSE, will trigger ShowPrevious or ShowNext
87 * @tc.type: FUNC
88 */
89 HWTEST_F(SwiperEventTestNg, HandleDrag001, TestSize.Level1)
90 {
__anon8de6c5f20202(SwiperModelNG model) 91 CreateWithItem([](SwiperModelNG model) {});
92 GestureEvent info;
93 info.SetInputEventType(InputEventType::AXIS);
94 info.SetSourceTool(SourceTool::MOUSE);
95
96 /**
97 * @tc.steps: step1. Drag to right
98 * @tc.expected: Trigger ShowPrevious
99 */
100 info.SetMainDelta(10.f);
101 HandleDrag(info);
102 EXPECT_EQ(pattern_->GetCurrentShownIndex(), -1);
103
104 /**
105 * @tc.steps: step2. Drag to left
106 * @tc.expected: Trigger ShowNext
107 */
108 info.SetMainDelta(-10.f);
109 HandleDrag(info);
110 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 0);
111
112 /**
113 * @tc.steps: step3. Drag not move
114 * @tc.expected: CurrentIndex not changed
115 */
116 info.SetMainDelta(0.f);
117 HandleDrag(info);
118 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 0);
119 }
120
121 /**
122 * @tc.name: HandleDrag002
123 * @tc.desc: HandleDrag to cancel or drag outOfHotRegion, will not change CurrentIndex
124 * @tc.type: FUNC
125 */
126 HWTEST_F(SwiperEventTestNg, HandleDrag002, TestSize.Level1)
127 {
128 /**
129 * @tc.steps: step1. Set HotRegion and drag in it
130 */
__anon8de6c5f20302(SwiperModelNG model) 131 CreateWithItem([](SwiperModelNG model) {});
132 MockPaintRect(frameNode_);
133
134 /**
135 * @tc.steps: step2. HandleDragCancel
136 * @tc.expected: targetIndex_ not changed
137 */
138 GestureEvent info = CreateDragInfo(false);
139 HandleDragStart(info);
140 HandleDragUpdate(info);
141 HandleDragCancel();
142 EXPECT_EQ(pattern_->targetIndex_, 0);
143 }
144
145 /**
146 * @tc.name: HandleDrag003
147 * @tc.desc: HandleDrag left out of boundary, CurrentIndex changed because loop:true
148 * @tc.type: FUNC
149 */
150 HWTEST_F(SwiperEventTestNg, HandleDrag003, TestSize.Level1)
151 {
152 /**
153 * @tc.steps: step1. Set HotRegion and drag in it
154 */
__anon8de6c5f20402(SwiperModelNG model) 155 CreateWithItem([](SwiperModelNG model) {});
156 MockPaintRect(frameNode_);
157
158 /**
159 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH
160 * @tc.expected: Item(index:0) OffsetX is equal to dragDelta
161 */
162 GestureEvent info = CreateDragInfo(false);
163 HandleDragStart(info);
164 HandleDragUpdate(info);
165 FlushLayoutTask(frameNode_);
166 EXPECT_EQ(GetChildX(frameNode_, 0), DRAG_DELTA);
167
168 /**
169 * @tc.steps: step3. HandleDragUpdate abs(delta) > SWIPER_WIDTH
170 * @tc.expected: Item(index:0) OffsetX not more than SWIPER_WIDTH
171 */
172 HandleDragUpdate(info);
173 FlushLayoutTask(frameNode_);
174 EXPECT_EQ(GetChildX(frameNode_, 0), SWIPER_WIDTH);
175
176 /**
177 * @tc.steps: step4. HandleDragEnd
178 * @tc.expected: Change CurrentIndex by MainVelocity direction
179 */
180 HandleDragEnd(info);
181 FlushLayoutTask(frameNode_);
182 EXPECT_EQ(pattern_->GetCurrentShownIndex(), -1);
183 }
184
185 /**
186 * @tc.name: HandleDrag004
187 * @tc.desc: HandleDrag right out of boundary, CurrentIndex changed because loop:true
188 * @tc.type: FUNC
189 */
190 HWTEST_F(SwiperEventTestNg, HandleDrag004, TestSize.Level1)
191 {
192 /**
193 * @tc.steps: step1. Set HotRegion and drag in it
194 */
__anon8de6c5f20502(SwiperModelNG model) 195 CreateWithItem([](SwiperModelNG model) {
196 model.SetIndex(3);
197 });
198 MockPaintRect(frameNode_);
199
200 /**
201 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH
202 * @tc.expected: Item(index:3) OffsetX is equal to dragDelta
203 */
204 GestureEvent info = CreateDragInfo(true);
205 HandleDragStart(info);
206 HandleDragUpdate(info);
207 FlushLayoutTask(frameNode_);
208 EXPECT_EQ(GetChildX(frameNode_, 3), -DRAG_DELTA);
209
210 /**
211 * @tc.steps: step3. HandleDragUpdate abs(delta) > SWIPER_WIDTH
212 * @tc.expected: Item(index:3) OffsetX not more than SWIPER_WIDTH
213 */
214 HandleDragUpdate(info);
215 FlushLayoutTask(frameNode_);
216 EXPECT_EQ(GetChildX(frameNode_, 3), -SWIPER_WIDTH);
217
218 /**
219 * @tc.steps: step4. HandleDragEnd
220 * @tc.expected: Change CurrentIndex by MainVelocity direction
221 */
222 HandleDragEnd(info);
223 FlushLayoutTask(frameNode_);
224 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 4);
225 }
226
227 /**
228 * @tc.name: HandleDrag005
229 * @tc.desc: HandleDrag out of left boundary, but loop false
230 * @tc.type: FUNC
231 */
232 HWTEST_F(SwiperEventTestNg, HandleDrag005, TestSize.Level1)
233 {
234 /**
235 * @tc.steps: step1. Set loop false, set HotRegion and drag in it
236 */
__anon8de6c5f20602(SwiperModelNG model) 237 CreateWithItem([](SwiperModelNG model) {
238 model.SetLoop(false);
239 });
240 MockPaintRect(frameNode_);
241
242 /**
243 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH
244 * @tc.expected: Item(index:0) OffsetX < dragDelta because spring friction
245 */
246 GestureEvent info = CreateDragInfo(false);
247 HandleDragStart(info);
248 HandleDragUpdate(info);
249 FlushLayoutTask(frameNode_);
250 EXPECT_LT(GetChildX(frameNode_, 0), DRAG_DELTA);
251 EXPECT_GT(GetChildX(frameNode_, 0), 0.f);
252
253 /**
254 * @tc.steps: step3. HandleDragUpdate abs(delta) > SWIPER_WIDTH
255 * @tc.expected: Item(index:0) OffsetX < dragDelta because spring friction
256 */
257 float preDelta = GetChildX(frameNode_, 0);
258 HandleDragUpdate(info);
259 FlushLayoutTask(frameNode_);
260 EXPECT_LT(GetChildX(frameNode_, 0), DRAG_DELTA * 2);
261 EXPECT_GT(GetChildX(frameNode_, 0), preDelta);
262
263 /**
264 * @tc.steps: step4. HandleDragEnd
265 * @tc.expected: Change still 0
266 */
267 HandleDragEnd(info);
268 FlushLayoutTask(frameNode_);
269 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 0);
270 }
271
272 /**
273 * @tc.name: HandleDrag006
274 * @tc.desc: HandleDrag out of right boundary, but loop false
275 * @tc.type: FUNC
276 */
277 HWTEST_F(SwiperEventTestNg, HandleDrag006, TestSize.Level1)
278 {
279 /**
280 * @tc.steps: step1. Set loop false, set HotRegion and drag in it
281 */
__anon8de6c5f20702(SwiperModelNG model) 282 CreateWithItem([](SwiperModelNG model) {
283 model.SetLoop(false);
284 model.SetIndex(3);
285 });
286 MockPaintRect(frameNode_);
287
288 /**
289 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH
290 * @tc.expected: Item(index:0) OffsetX < dragDelta because spring friction
291 */
292 GestureEvent info = CreateDragInfo(true);
293 HandleDragStart(info);
294 HandleDragUpdate(info);
295 FlushLayoutTask(frameNode_);
296 EXPECT_LT(GetChildX(frameNode_, 3), 0.f);
297 EXPECT_GT(GetChildX(frameNode_, 3), -DRAG_DELTA);
298
299 /**
300 * @tc.steps: step3. HandleDragUpdate abs(delta) > SWIPER_WIDTH
301 * @tc.expected: Item(index:0) OffsetX < dragDelta because spring friction
302 */
303 float preDelta = GetChildX(frameNode_, 3);
304 HandleDragUpdate(info);
305 FlushLayoutTask(frameNode_);
306 EXPECT_LT(GetChildX(frameNode_, 3), preDelta);
307 EXPECT_GT(GetChildX(frameNode_, 3), -DRAG_DELTA * 2);
308
309 /**
310 * @tc.steps: step4. HandleDragEnd
311 * @tc.expected: Change still 3
312 */
313 HandleDragEnd(info);
314 FlushLayoutTask(frameNode_);
315 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 3);
316 }
317
318 /**
319 * @tc.name: HandleDrag007
320 * @tc.desc: HandleDrag left out of boundary, but loop false and EdgeEffect::FADE
321 * @tc.type: FUNC
322 */
323 HWTEST_F(SwiperEventTestNg, HandleDrag007, TestSize.Level1)
324 {
325 /**
326 * @tc.steps: step1. Set loop false and EdgeEffect::FADE, set HotRegion and drag in it
327 */
__anon8de6c5f20802(SwiperModelNG model) 328 CreateWithItem([](SwiperModelNG model) {
329 model.SetLoop(false);
330 model.SetEdgeEffect(EdgeEffect::FADE);
331 });
332 MockPaintRect(frameNode_);
333
334 /**
335 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH
336 * @tc.expected: fadeOffset_ is equal to dragDelta
337 */
338 GestureEvent info = CreateDragInfo(false);
339 HandleDragStart(info);
340 HandleDragUpdate(info);
341 FlushLayoutTask(frameNode_);
342 EXPECT_EQ(pattern_->fadeOffset_, DRAG_DELTA);
343
344 /**
345 * @tc.steps: step3. HandleDragEnd
346 */
347 HandleDragEnd(info);
348 FlushLayoutTask(frameNode_);
349 EXPECT_FALSE(pattern_->targetIndex_.has_value());
350 }
351
352 /**
353 * @tc.name: HandleDrag008
354 * @tc.desc: HandleDrag right out of boundary, but loop false and EdgeEffect::FADE
355 * @tc.type: FUNC
356 */
357 HWTEST_F(SwiperEventTestNg, HandleDrag008, TestSize.Level1)
358 {
359 /**
360 * @tc.steps: step1. Set loop false and EdgeEffect::FADE, set HotRegion and drag in it
361 */
__anon8de6c5f20902(SwiperModelNG model) 362 CreateWithItem([](SwiperModelNG model) {
363 model.SetLoop(false);
364 model.SetEdgeEffect(EdgeEffect::FADE);
365 model.SetIndex(3);
366 });
367 MockPaintRect(frameNode_);
368
369 /**
370 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH
371 * @tc.expected: fadeOffset_ is equal to dragDelta
372 */
373 GestureEvent info = CreateDragInfo(true);
374 HandleDragStart(info);
375 HandleDragUpdate(info);
376 FlushLayoutTask(frameNode_);
377 EXPECT_EQ(pattern_->fadeOffset_, -DRAG_DELTA);
378
379 /**
380 * @tc.steps: step3. HandleDragEnd
381 */
382 HandleDragEnd(info);
383 FlushLayoutTask(frameNode_);
384 EXPECT_FALSE(pattern_->targetIndex_.has_value());
385 }
386
387 /**
388 * @tc.name: HandleDrag009
389 * @tc.desc: HandleDrag with DisplayCount
390 * @tc.type: FUNC
391 */
392 HWTEST_F(SwiperEventTestNg, HandleDrag009, TestSize.Level1)
393 {
__anon8de6c5f20a02(SwiperModelNG model) 394 CreateWithItem([](SwiperModelNG model) {
395 model.SetDisplayCount(2);
396 model.SetSwipeByGroup(true);
397 });
398
399 /**
400 * @tc.steps: step1. Drag to right
401 * @tc.expected: Swipe to last page
402 */
403 GestureEvent info = CreateDragInfo(false);
404 HandleDrag(info);
405 EXPECT_EQ(pattern_->GetCurrentShownIndex(), -2);
406
407 /**
408 * @tc.steps: step2. Drag to left
409 * @tc.expected: Swipe to first page
410 */
411 info = CreateDragInfo(true);
412 HandleDrag(info);
413 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 0);
414 }
415
416 /**
417 * @tc.name: HandleDrag010
418 * @tc.desc: HandleDrag to left with different drag distances and velocities
419 * @tc.type: FUNC
420 */
421 HWTEST_F(SwiperEventTestNg, HandleDrag010, TestSize.Level1)
422 {
423 // HandleDragUpdate abs(delta) > SWIPER_WIDTH / 2 and velocity > threshold included in the HandleDrag003 case
424 /**
425 * @tc.steps: step1. HandleDragUpdate abs(delta) > SWIPER_WIDTH / 2 and velocity < threshold
426 * @tc.expected: Item index is equal to -1
427 */
__anon8de6c5f20b02(SwiperModelNG model) 428 CreateWithItem([](SwiperModelNG model) {});
429 MockPaintRect(frameNode_);
430 GestureEvent info = CreateDragInfo(false);
431 HandleDragStart(info);
432 HandleDragUpdate(info);
433 FlushLayoutTask(frameNode_);
434 info.SetMainVelocity(0);
435 HandleDragEnd(info);
436 FlushLayoutTask(frameNode_);
437 EXPECT_EQ(pattern_->GetCurrentShownIndex(), -1);
438
439 /**
440 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH / 2 and velocity > threshold
441 * @tc.expected: Item index is equal to -1
442 */
__anon8de6c5f20c02(SwiperModelNG model) 443 CreateWithItem([](SwiperModelNG model) {});
444 MockPaintRect(frameNode_);
445 info = CreateDragInfo(false);
446 info.SetMainDelta(DRAG_DELTA / 2);
447 HandleDragStart(info);
448 HandleDragUpdate(info);
449 FlushLayoutTask(frameNode_);
450 HandleDragEnd(info);
451 FlushLayoutTask(frameNode_);
452 EXPECT_EQ(pattern_->GetCurrentShownIndex(), -1);
453
454 /**
455 * @tc.steps: step3. HandleDragUpdate abs(delta) < SWIPER_WIDTH / 2 and velocity < threshold
456 * @tc.expected: Item index is equal to 0
457 */
__anon8de6c5f20d02(SwiperModelNG model) 458 CreateWithItem([](SwiperModelNG model) {});
459 MockPaintRect(frameNode_);
460 info = CreateDragInfo(false);
461 info.SetMainDelta(DRAG_DELTA / 2);
462 HandleDragStart(info);
463 HandleDragUpdate(info);
464 FlushLayoutTask(frameNode_);
465 info.SetMainVelocity(0);
466 HandleDragEnd(info);
467 FlushLayoutTask(frameNode_);
468 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 0);
469 }
470
471 /**
472 * @tc.name: HandleDrag011
473 * @tc.desc: HandleDrag to right with different drag distances and velocities
474 * @tc.type: FUNC
475 */
476 HWTEST_F(SwiperEventTestNg, HandleDrag011, TestSize.Level1)
477 {
478 // HandleDragUpdate abs(delta) > SWIPER_WIDTH / 2 and velocity > threshold included in the HandleDrag004 case
479 /**
480 * @tc.steps: step1. HandleDragUpdate abs(delta) > SWIPER_WIDTH / 2 and velocity < threshold
481 * @tc.expected: Item index is equal to 4
482 */
__anon8de6c5f20e02(SwiperModelNG model) 483 CreateWithItem([](SwiperModelNG model) {
484 model.SetIndex(3);
485 });
486 MockPaintRect(frameNode_);
487 GestureEvent info = CreateDragInfo(true);
488 HandleDragStart(info);
489 HandleDragUpdate(info);
490 FlushLayoutTask(frameNode_);
491 info.SetMainVelocity(0);
492 HandleDragEnd(info);
493 FlushLayoutTask(frameNode_);
494 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 4);
495
496 /**
497 * @tc.steps: step2. HandleDragUpdate abs(delta) < SWIPER_WIDTH / 2 and velocity > threshold
498 * @tc.expected: Item index is equal to 4
499 */
__anon8de6c5f20f02(SwiperModelNG model) 500 CreateWithItem([](SwiperModelNG model) {
501 model.SetIndex(3);
502 });
503 MockPaintRect(frameNode_);
504 info = CreateDragInfo(true);
505 info.SetMainDelta(-DRAG_DELTA / 2);
506 HandleDragStart(info);
507 HandleDragUpdate(info);
508 FlushLayoutTask(frameNode_);
509 HandleDragEnd(info);
510 FlushLayoutTask(frameNode_);
511 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 4);
512
513 /**
514 * @tc.steps: step3. HandleDragUpdate abs(delta) < SWIPER_WIDTH / 2 and velocity < threshold
515 * @tc.expected: Item index is equal to 3
516 */
__anon8de6c5f21002(SwiperModelNG model) 517 CreateWithItem([](SwiperModelNG model) {
518 model.SetIndex(3);
519 });
520 MockPaintRect(frameNode_);
521 info = CreateDragInfo(true);
522 info.SetMainDelta(-DRAG_DELTA / 2);
523 HandleDragStart(info);
524 HandleDragUpdate(info);
525 FlushLayoutTask(frameNode_);
526 info.SetMainVelocity(0);
527 HandleDragEnd(info);
528 FlushLayoutTask(frameNode_);
529 EXPECT_EQ(pattern_->GetCurrentShownIndex(), 3);
530 }
531
532 /**
533 * @tc.name: SwiperPatternHandleScroll001
534 * @tc.desc: test HandleScroll SELF_ONLY
535 * @tc.type: FUNC
536 */
537 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll001, TestSize.Level1)
538 {
__anon8de6c5f21102(SwiperModelNG model) 539 CreateWithItem([](SwiperModelNG model) {});
540 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(true);
541 auto res = pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::GESTURE);
542 EXPECT_EQ(res.remain, 0.0f);
543 EXPECT_FALSE(res.reachEdge);
544
545 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateDisableSwipe(true);
546 res = pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::GESTURE);
547 EXPECT_EQ(res.remain, 5.0f);
548 EXPECT_TRUE(res.reachEdge);
549 }
550
551 /**
552 * @tc.name: SwiperPatternHandleScroll002
553 * @tc.desc: test HandleScroll SELF_FIRST but scrolling within boundary
554 * @tc.type: FUNC
555 */
556 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll002, TestSize.Level1)
557 {
__anon8de6c5f21202(SwiperModelNG model) 558 CreateWithItem([](SwiperModelNG model) {});
559 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(true);
560 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
561 EXPECT_CALL(*mockScroll, HandleScroll).Times(0);
562 pattern_->parent_ = mockScroll;
563 NestedScrollOptions nestedOpt = {
564 .forward = NestedScrollMode::SELF_FIRST,
565 .backward = NestedScrollMode::SELF_FIRST,
566 };
567 pattern_->SetNestedScroll(nestedOpt);
568 auto res = pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::GESTURE);
569 EXPECT_EQ(res.remain, 0.0f);
570 }
571
572 /**
573 * @tc.name: SwiperPatternHandleScroll003
574 * @tc.desc: test HandleScroll SELF_FIRST while scrolling out of boundary and EdgeEffect doesn't consume extra offset
575 * @tc.type: FUNC
576 */
577 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll003, TestSize.Level1)
578 {
__anon8de6c5f21302(SwiperModelNG model) 579 CreateWithItem([](SwiperModelNG model) {});
580 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(false);
581 pattern_->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::NONE);
582 pattern_->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = -0.5 } });
583
584 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
585 EXPECT_CALL(*mockScroll, HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL, 0.f))
586 .Times(1)
587 .WillOnce(Return(ScrollResult { .remain = 5.0f, .reachEdge = true }));
588 EXPECT_CALL(*mockScroll, HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_OVER_SCROLL, 0.f))
589 .Times(1)
590 .WillOnce(Return(ScrollResult { .remain = 5.0f, .reachEdge = true }));
591 pattern_->parent_ = mockScroll;
592 NestedScrollOptions nestedOpt = {
593 .forward = NestedScrollMode::SELF_FIRST,
594 .backward = NestedScrollMode::SELF_FIRST,
595 };
596 pattern_->SetNestedScroll(nestedOpt);
597 auto res = pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::GESTURE, 0.f);
598 EXPECT_EQ(res.remain, 5.0f);
599 EXPECT_TRUE(res.reachEdge);
600 }
601
602 /**
603 * @tc.name: SwiperPatternHandleScroll004
604 * @tc.desc: test HandleScroll SELF_FIRST and scrolling out of boundary and EdgeEffect consumes extra offset
605 * @tc.type: FUNC
606 */
607 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll004, TestSize.Level1)
608 {
__anon8de6c5f21402(SwiperModelNG model) 609 CreateWithItem([](SwiperModelNG model) {});
610 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(false);
611 pattern_->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::SPRING);
612 pattern_->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = -0.5 } });
613
614 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
615 EXPECT_CALL(*mockScroll, HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL, 0.f))
616 .Times(1)
617 .WillOnce(Return(ScrollResult { .remain = 5.0f, .reachEdge = true }));
618 EXPECT_CALL(*mockScroll, HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_OVER_SCROLL, 0.f))
619 .Times(1)
620 .WillOnce(Return(ScrollResult { .remain = 5.0f, .reachEdge = true }));
621 pattern_->parent_ = mockScroll;
622 NestedScrollOptions nestedOpt = {
623 .forward = NestedScrollMode::SELF_FIRST,
624 .backward = NestedScrollMode::SELF_FIRST,
625 };
626 pattern_->SetNestedScroll(nestedOpt);
627
628 auto res = pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::GESTURE, 0.f);
629 EXPECT_EQ(res.remain, 0.0f);
630 }
631
632 /**
633 * @tc.name: SwiperPatternHandleScroll005
634 * @tc.desc: test HandleScroll called by CHILD_SCROLL when edge is reached. Should pass offset back to child.
635 * @tc.type: FUNC
636 */
637 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll005, TestSize.Level1)
638 {
639 /**
640 * @tc.steps: step1. Create swipernode.
641 */
642 auto swiperNode = FrameNode::CreateFrameNode("Swiper", 0, AceType::MakeRefPtr<SwiperPattern>());
643 auto swiperPattern = swiperNode->GetPattern<SwiperPattern>();
644 ASSERT_NE(swiperPattern, nullptr);
645 swiperPattern->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(false);
646 swiperPattern->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::SPRING);
647 swiperPattern->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = -0.5 } });
648 auto res = swiperPattern->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
649 EXPECT_EQ(res.remain, 4.5f);
650 // three level nesting
651 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
652 EXPECT_CALL(*mockScroll, HandleScroll(4.5f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL, 0.f))
653 .Times(1)
654 .WillOnce(Return(ScrollResult { .remain = 4.5f, .reachEdge = true }));
655 EXPECT_CALL(*mockScroll, HandleScroll(4.5f, SCROLL_FROM_UPDATE, NestedState::CHILD_OVER_SCROLL, 0.f))
656 .Times(1)
657 .WillOnce(Return(ScrollResult { .remain = 4.5f, .reachEdge = true }));
658 swiperPattern->parent_ = mockScroll;
659 NestedScrollOptions nestedOpt = {
660 .forward = NestedScrollMode::SELF_FIRST,
661 .backward = NestedScrollMode::SELF_FIRST,
662 };
663 swiperPattern->SetNestedScroll(nestedOpt);
664 res = swiperPattern->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL, 0.f);
665 EXPECT_EQ(res.remain, 4.5f);
666 }
667
668 /**
669 * @tc.name: SwiperPatternHandleScroll006
670 * @tc.desc: test HandleScroll from child animation
671 * @tc.type: FUNC
672 */
673 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll006, TestSize.Level1)
674 {
__anon8de6c5f21502(SwiperModelNG model) 675 CreateWithItem([](SwiperModelNG model) {});
676 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(true);
677 // during animation
678 pattern_->targetIndex_ = 1;
679
680 auto res = pattern_->HandleScroll(5.0f, SCROLL_FROM_ANIMATION, NestedState::CHILD_SCROLL);
681 EXPECT_EQ(res.remain, 5.0f);
682 pattern_->targetIndex_.reset();
683
__anon8de6c5f21602() 684 pattern_->fadeAnimation_ = AnimationUtils::StartAnimation({}, [&]() {});
685 pattern_->fadeAnimationIsRunning_ = true;
686 res = pattern_->HandleScroll(5.0f, SCROLL_FROM_ANIMATION, NestedState::CHILD_SCROLL);
687 // fade animation doesn't affect scrolling
688 EXPECT_EQ(res.remain, 0.0f);
689 }
690
691 /**
692 * @tc.name: SwiperPatternHandleScroll007
693 * @tc.desc: test HandleScroll from child mouse scroll
694 * @tc.type: FUNC
695 */
696 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll007, TestSize.Level1)
697 {
__anon8de6c5f21702(SwiperModelNG model) 698 CreateWithItem([](SwiperModelNG model) {});
699 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(true);
700
701 // showPrevious
702 auto res = pattern_->HandleScroll(5.0f, SCROLL_FROM_AXIS, NestedState::CHILD_SCROLL);
703 EXPECT_EQ(res.remain, 0.0f);
704
705 // showNext
706 res = pattern_->HandleScroll(-5.0f, SCROLL_FROM_AXIS, NestedState::CHILD_SCROLL);
707 EXPECT_EQ(res.remain, 0.0f);
708 }
709
710 /**
711 * @tc.name: SwiperPatternHandleScroll008
712 * @tc.desc: test HandleScroll triggering event
713 * @tc.type: FUNC
714 */
715 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll008, TestSize.Level1)
716 {
__anon8de6c5f21802(SwiperModelNG model) 717 CreateWithItem([](SwiperModelNG model) {});
718 pattern_->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = 0.0f } });
719 int32_t callCount = 0;
__anon8de6c5f21902(int32_t index, const AnimationCallbackInfo& info) 720 eventHub_->SetGestureSwipeEvent([&](int32_t index, const AnimationCallbackInfo& info) {
721 ++callCount;
722 });
723 pattern_->OnScrollStartRecursive(pattern_, 0.0f, 0.0f);
724 pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
725 pattern_->HandleScroll(-5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
726 pattern_->HandleScroll(-2.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
727 EXPECT_EQ(callCount, 3);
728 }
729
730 /**
731 * @tc.name: SwiperPatternHandleScroll009
732 * @tc.desc: test HandleScroll called by CHILD_SCROLL when edge is reached. Should pass offset back to child.
733 * @tc.type: FUNC
734 */
735 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScroll009, TestSize.Level1)
736 {
737 /**
738 * @tc.steps: step1. Create swipernode.
739 */
740 auto swiperNode = FrameNode::CreateFrameNode("Swiper", 0, AceType::MakeRefPtr<SwiperPattern>());
741 auto swiperPattern = swiperNode->GetPattern<SwiperPattern>();
742 ASSERT_NE(swiperPattern, nullptr);
743 auto layoutproperty = swiperPattern->GetLayoutProperty<SwiperLayoutProperty>();
744 layoutproperty->UpdateLoop(false);
745 /**
746 * @tc.steps: step2. alignment Right to left (RTL),state != NestedState::GESTURE.
747 */
748 layoutproperty->UpdateLayoutDirection(TextDirection::RTL);
749 EXPECT_EQ(swiperPattern->IsHorizontalAndRightToLeft(), true);
750 swiperPattern->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::SPRING);
751 swiperPattern->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = -0.5 } });
752 auto res = swiperPattern->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
753 EXPECT_EQ(res.remain, -4.5f);
754 }
755
756
757 /**
758 * @tc.name: SwiperPatternHandleScrollMultiChildren001
759 * @tc.desc: test HandleScroll with multiple scrollable children
760 * @tc.type: FUNC
761 */
762 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScrollMultiChildren001, TestSize.Level1)
763 {
__anon8de6c5f21a02(SwiperModelNG model) 764 CreateWithItem([](SwiperModelNG model) {});
765 pattern_->usePropertyAnimation_ = true;
766 pattern_->OnScrollStartRecursive(pattern_, 0.0f, 0.0f);
767 EXPECT_TRUE(pattern_->childScrolling_);
768 EXPECT_FALSE(pattern_->usePropertyAnimation_);
769 pattern_->HandleScroll(5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
770 EXPECT_TRUE(pattern_->childScrolling_);
771 EXPECT_FALSE(pattern_->usePropertyAnimation_);
772
773 // second child calling
774 pattern_->OnScrollStartRecursive(pattern_, 0.0f, 0.0f);
775
776 // first child ending
777 pattern_->OnScrollEndRecursive(std::nullopt);
778 EXPECT_FALSE(pattern_->childScrolling_);
779 EXPECT_EQ(pattern_->targetIndex_, 0);
780
781 // second child scrolling
782 pattern_->HandleScroll(-5.0f, SCROLL_FROM_UPDATE, NestedState::CHILD_SCROLL);
783 EXPECT_TRUE(pattern_->childScrolling_);
784 EXPECT_FALSE(pattern_->usePropertyAnimation_);
785 pattern_->OnScrollEndRecursive(std::nullopt);
786
787 // self scroll
788 pattern_->HandleScroll(-5.0f, SCROLL_FROM_UPDATE, NestedState::GESTURE);
789 EXPECT_FALSE(pattern_->childScrolling_);
790 }
791
792 /**
793 * @tc.name: SwiperPatternHandleScrollVelocity001
794 * @tc.desc: test HandleScrollVelocity self handle
795 * @tc.type: FUNC
796 */
797 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScrollVelocity001, TestSize.Level1)
798 {
__anon8de6c5f21b02(SwiperModelNG model) 799 CreateWithItem([](SwiperModelNG model) {});
800 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(true);
801 pattern_->childScrolling_ = true;
802 auto res = pattern_->HandleScrollVelocity(5.0f);
803 EXPECT_TRUE(res);
804 EXPECT_FALSE(pattern_->childScrolling_);
805
806 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateDisableSwipe(true);
807 res = pattern_->HandleScrollVelocity(5.0f);
808 EXPECT_FALSE(res);
809 }
810
811 /**
812 * @tc.name: SwiperPatternHandleScrollVelocity002
813 * @tc.desc: test HandleScrollVelocity pass to parent
814 * @tc.type: FUNC
815 */
816 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScrollVelocity002, TestSize.Level1)
817 {
__anon8de6c5f21c02(SwiperModelNG model) 818 CreateWithItem([](SwiperModelNG model) {
819 model.SetEdgeEffect(EdgeEffect::NONE);
820 model.SetLoop(false);
821 });
822 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
823 EXPECT_CALL(*mockScroll, HandleScrollVelocity).Times(1).WillOnce(Return(true));
824 pattern_->parent_ = mockScroll;
825 NestedScrollOptions nestedOpt = {
826 .forward = NestedScrollMode::SELF_FIRST,
827 .backward = NestedScrollMode::SELF_FIRST,
828 };
829 pattern_->SetNestedScroll(nestedOpt);
830 pattern_->childScrolling_ = true;
831
832 auto res = pattern_->HandleScrollVelocity(5.0f);
833 EXPECT_TRUE(res);
834 EXPECT_FALSE(pattern_->childScrolling_);
835 // shouldn't be passed parent
836 pattern_->nestedScroll_ = {
837 .forward = NestedScrollMode::SELF_ONLY,
838 .backward = NestedScrollMode::SELF_ONLY,
839 };
840 pattern_->HandleDragEnd(5.0f);
841
842 pattern_->nestedScroll_ = {
843 .forward = NestedScrollMode::SELF_FIRST,
844 .backward = NestedScrollMode::SELF_ONLY,
845 };
846 EXPECT_TRUE(pattern_->nestedScroll_.NeedParent(true));
847 EXPECT_FALSE(pattern_->nestedScroll_.NeedParent(false));
848 mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
849 EXPECT_CALL(*mockScroll, HandleScrollVelocity).Times(0);
850 pattern_->parent_ = mockScroll;
851 pattern_->HandleDragEnd(5.0f);
852
853 pattern_->UpdateCurrentOffset(-5.0f);
854 pattern_->MarkDirtyNodeSelf();
855 FlushLayoutTask(frameNode_);
856 pattern_->HandleDragEnd(-5.0f);
857 }
858
859 /**
860 * @tc.name: SwiperPatternHandleScrollVelocity003
861 * @tc.desc: test HandleScrollVelocity pass to parent and parent doesn't consume
862 * @tc.type: FUNC
863 */
864 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScrollVelocity003, TestSize.Level1)
865 {
__anon8de6c5f21d02(SwiperModelNG model) 866 CreateWithItem([](SwiperModelNG model) {});
867 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateLoop(false);
868 pattern_->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::NONE);
869 pattern_->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = 0.0f } });
870 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
871 EXPECT_CALL(*mockScroll, HandleScrollVelocity).Times(2).WillRepeatedly(Return(false));
872 pattern_->parent_ = mockScroll;
873 NestedScrollOptions nestedOpt = {
874 .forward = NestedScrollMode::SELF_FIRST,
875 .backward = NestedScrollMode::SELF_FIRST,
876 };
877 pattern_->SetNestedScroll(nestedOpt);
878 pattern_->childScrolling_ = true;
879 auto res = pattern_->HandleScrollVelocity(5.0f);
880 EXPECT_FALSE(res);
881 EXPECT_FALSE(pattern_->childScrolling_);
882
883 // change EdgeEffect to Spring and redo
884 // should consume velocity
885 pattern_->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::SPRING);
886 mockScroll.Reset();
887 mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
888 EXPECT_CALL(*mockScroll, HandleScrollVelocity).Times(2).WillRepeatedly(Return(false));
889
890 pattern_->parent_ = mockScroll;
891 pattern_->childScrolling_ = true;
892 res = pattern_->HandleScrollVelocity(5.0f);
893 EXPECT_FALSE(pattern_->childScrolling_);
894 EXPECT_TRUE(res);
895 }
896
897 /**
898 * @tc.name: SwiperPatternHandleScrollVelocity004
899 * @tc.desc: test HandleScrollVelocity self handle
900 * @tc.type: FUNC
901 */
902 HWTEST_F(SwiperEventTestNg, SwiperPatternHandleScrollVelocity004, TestSize.Level1)
903 {
__anon8de6c5f21e02(SwiperModelNG model) 904 CreateWithItem([](SwiperModelNG model) {
905 model.SetEdgeEffect(EdgeEffect::NONE);
906 model.SetLoop(false);
907 });
908 /**
909 * @tc.steps: step1. alignment Right to left (RTL).
910 */
911 layoutProperty_->UpdateLayoutDirection(TextDirection::RTL);
912 EXPECT_EQ(pattern_->IsHorizontalAndRightToLeft(), true);
913 pattern_->GetPaintProperty<SwiperPaintProperty>()->UpdateEdgeEffect(EdgeEffect::SPRING);
914 pattern_->itemPosition_.insert({ 0, SwiperItemInfo { .startPos = -0.5 } });
915 auto res = pattern_->HandleScrollVelocity(5.0f);
916 EXPECT_TRUE(res);
917 pattern_->GetLayoutProperty<SwiperLayoutProperty>()->UpdateDisableSwipe(true);
918 res = pattern_->HandleScrollVelocity(5.0f);
919 EXPECT_FALSE(res);
920 }
921
922 /**
923 * @tc.name: HandleTouchBottomLoop001
924 * @tc.desc: test Swiper indicator no touch bottom in loop
925 * @tc.type: FUNC
926 */
927 HWTEST_F(SwiperEventTestNg, HandleTouchBottomLoop001, TestSize.Level1)
928 {
__anon8de6c5f21f02(SwiperModelNG model) 929 CreateWithItem([](SwiperModelNG model) {});
930 EXPECT_EQ(pattern_->TotalCount(), 4);
931 pattern_->currentFirstIndex_ = 1;
932 pattern_->currentIndex_ = 1;
933 pattern_->gestureState_ = GestureState::GESTURE_STATE_FOLLOW_RIGHT;
934 pattern_->HandleTouchBottomLoop();
935 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_NONE);
936 }
937
938 /**
939 * @tc.name: HandleTouchBottomLoop002
940 * @tc.desc: test Swiper indicator touch left bottom in loop
941 * @tc.type: FUNC
942 */
943 HWTEST_F(SwiperEventTestNg, HandleTouchBottomLoop002, TestSize.Level1)
944 {
__anon8de6c5f22002(SwiperModelNG model) 945 CreateWithItem([](SwiperModelNG model) {});
946 EXPECT_EQ(pattern_->TotalCount(), 4);
947 pattern_->currentFirstIndex_ = pattern_->TotalCount() - 1;
948 pattern_->currentIndex_ = 0;
949 pattern_->gestureState_ = GestureState::GESTURE_STATE_FOLLOW_LEFT;
950 pattern_->HandleTouchBottomLoop();
951 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_LEFT);
952
953 pattern_->gestureState_ = GestureState::GESTURE_STATE_FOLLOW_RIGHT;
954 pattern_->HandleTouchBottomLoop();
955 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_RIGHT);
956
957 pattern_->gestureState_ = GestureState::GESTURE_STATE_RELEASE_LEFT;
958 pattern_->HandleTouchBottomLoop();
959 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_LEFT);
960 }
961
962 /**
963 * @tc.name: HandleTouchBottomLoop003
964 * @tc.desc: test Swiper indicator touch right bottom in loop
965 * @tc.type: FUNC
966 */
967 HWTEST_F(SwiperEventTestNg, HandleTouchBottomLoop003, TestSize.Level1)
968 {
__anon8de6c5f22102(SwiperModelNG model) 969 CreateWithItem([](SwiperModelNG model) {});
970 EXPECT_EQ(pattern_->TotalCount(), 4);
971 pattern_->currentFirstIndex_ = 0;
972 pattern_->currentIndex_ = pattern_->TotalCount() - 1;
973 pattern_->gestureState_ = GestureState::GESTURE_STATE_RELEASE_RIGHT;
974 pattern_->HandleTouchBottomLoop();
975 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_RIGHT);
976
977 pattern_->currentFirstIndex_ = pattern_->TotalCount() - 1;
978 pattern_->currentIndex_ = pattern_->TotalCount() - 1;
979 pattern_->gestureState_ = GestureState::GESTURE_STATE_FOLLOW_LEFT;
980 pattern_->HandleTouchBottomLoop();
981 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_LEFT);
982
983 pattern_->currentFirstIndex_ = pattern_->TotalCount() - 1;
984 pattern_->currentIndex_ = pattern_->TotalCount() - 1;
985 pattern_->gestureState_ = GestureState::GESTURE_STATE_FOLLOW_RIGHT;
986 pattern_->HandleTouchBottomLoop();
987 EXPECT_EQ(pattern_->touchBottomType_, TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_RIGHT);
988 }
989
990 /**
991 * @tc.name: SwiperFunc002
992 * @tc.desc: OnVisibleChange
993 * @tc.type: FUNC
994 */
995 HWTEST_F(SwiperEventTestNg, SwiperFunc002, TestSize.Level1)
996 {
__anon8de6c5f22202(SwiperModelNG model) 997 CreateWithItem([](SwiperModelNG model) {});
998 bool isVisible = false;
999 pattern_->OnVisibleChange(isVisible);
1000 pattern_->isInit_ = false;
1001 pattern_->OnWindowHide();
1002 pattern_->OnVisibleChange(isVisible);
1003 EXPECT_FALSE(pattern_->isVisible_);
1004 isVisible = true;
1005 pattern_->OnWindowShow();
1006 pattern_->OnVisibleChange(isVisible);
1007 EXPECT_TRUE(pattern_->isVisible_);
1008 pattern_->isVisibleArea_ = true;
1009 pattern_->OnWindowShow();
1010 }
1011
1012 /**
1013 * @tc.name: SwiperPatternOnVisibleChange003
1014 * @tc.desc: OnVisibleChange
1015 * @tc.type: FUNC
1016 */
1017 HWTEST_F(SwiperEventTestNg, SwiperPatternOnVisibleChange003, TestSize.Level1)
1018 {
__anon8de6c5f22302(SwiperModelNG model) 1019 CreateWithItem([](SwiperModelNG model) {});
1020 pattern_->isWindowShow_ = false;
1021
1022 /**
1023 * @tc.cases: call OnVisibleChange.
1024 * @tc.expected: Related function runs ok.
1025 */
1026 pattern_->isInit_ = true;
1027 pattern_->OnVisibleChange(true);
1028 EXPECT_TRUE(pattern_->isInit_);
1029 }
1030
1031 /**
1032 * @tc.name: OnIndexChange001
1033 * @tc.desc: OnIndexChange
1034 * @tc.type: FUNC
1035 */
1036 HWTEST_F(SwiperEventTestNg, OnIndexChange001, TestSize.Level1)
1037 {
1038 bool isTrigger = false;
__anon8de6c5f22402(const BaseEventInfo* info) 1039 auto onChangeEvent = [&isTrigger](const BaseEventInfo* info) { isTrigger = true; };
__anon8de6c5f22502(SwiperModelNG model) 1040 CreateWithItem([=](SwiperModelNG model) {
1041 model.SetOnChangeEvent(std::move(onChangeEvent));
1042 });
1043
1044 /**
1045 * @tc.steps: step1. Call ShowNext
1046 * @tc.expected: Trigger onChangeEvent
1047 */
1048 ShowNext();
1049 EXPECT_TRUE(isTrigger);
1050
1051 /**
1052 * @tc.steps: step2. Call ShowPrevious
1053 * @tc.expected: Trigger onChangeEvent
1054 */
1055 isTrigger = false;
1056 ShowPrevious();
1057 EXPECT_TRUE(isTrigger);
1058
1059 /**
1060 * @tc.steps: step3. Call ChangeIndex
1061 * @tc.expected: Trigger onChangeEvent
1062 */
1063 isTrigger = false;
1064 ChangeIndex(3);
1065 EXPECT_TRUE(isTrigger);
1066 }
1067
1068 /**
1069 * @tc.name: OnScrollStartEnd001
1070 * @tc.desc: test OnScrollStartRecursive/OnScrollEndRecursive
1071 * @tc.type: FUNC
1072 */
1073 HWTEST_F(SwiperEventTestNg, OnScrollStartEnd001, TestSize.Level1)
1074 {
__anon8de6c5f22602(SwiperModelNG model) 1075 CreateWithItem([](SwiperModelNG model) {
1076 model.SetLoop(false);
1077 model.SetEdgeEffect(EdgeEffect::NONE);
1078 });
1079 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
1080 auto mockScrollNode = FrameNode::CreateFrameNode("MockScroll", -1, mockScroll);
1081 frameNode_->MountToParent(mockScrollNode);
1082 EXPECT_CALL(*mockScroll, OnScrollStartRecursive).Times(1);
1083 EXPECT_CALL(*mockScroll, GetAxis).Times(1).WillOnce(Return(Axis::HORIZONTAL));
1084 NestedScrollOptions nestedOpt = {
1085 .forward = NestedScrollMode::SELF_FIRST,
1086 .backward = NestedScrollMode::SELF_FIRST,
1087 };
1088 pattern_->SetNestedScroll(nestedOpt);
1089 pattern_->isDragging_ = false;
1090 pattern_->currentIndex_ = 3;
1091 EXPECT_EQ(pattern_->gestureSwipeIndex_, 0);
1092
1093 /**
1094 * @tc.steps: step1. Scroll start
1095 */
1096 pattern_->OnScrollStartRecursive(pattern_, 5.0f, 0.0f);
1097 EXPECT_TRUE(pattern_->childScrolling_);
1098 EXPECT_EQ(pattern_->gestureSwipeIndex_, 3);
1099
1100 /**
1101 * @tc.steps: step2. Scroll end
1102 */
1103 EXPECT_CALL(*mockScroll, HandleScrollVelocity).Times(1);
1104 pattern_->parent_ = mockScroll;
1105 pattern_->OnScrollEndRecursive(std::nullopt);
1106 EXPECT_FALSE(pattern_->childScrolling_);
1107
1108 /**
1109 * @tc.steps: step3. Scroll end when AnimationRunning
1110 * @tc.expected: Can not HandleDragEnd
1111 */
1112 pattern_->targetIndex_ = 1;
1113 pattern_->OnScrollEndRecursive(std::nullopt);
1114 EXPECT_FALSE(pattern_->childScrolling_);
1115 }
1116
1117 /**
1118 * @tc.name: OnScrollStartEnd002
1119 * @tc.desc: test OnScrollStartRecursive/OnScrollEndRecursive when DisableSwipe
1120 * @tc.type: FUNC
1121 */
1122 HWTEST_F(SwiperEventTestNg, OnScrollStartEnd002, TestSize.Level1)
1123 {
__anon8de6c5f22702(SwiperModelNG model) 1124 CreateWithItem([](SwiperModelNG model) {
1125 model.SetDisableSwipe(true);
1126 model.SetLoop(false);
1127 model.SetEdgeEffect(EdgeEffect::NONE);
1128 });
1129 auto mockScroll = AceType::MakeRefPtr<MockNestableScrollContainer>();
1130 auto mockScrollNode = FrameNode::CreateFrameNode("MockScroll", -1, mockScroll);
1131 frameNode_->MountToParent(mockScrollNode);
1132 NestedScrollOptions nestedOpt = {
1133 .forward = NestedScrollMode::SELF_FIRST,
1134 .backward = NestedScrollMode::SELF_FIRST,
1135 };
1136 pattern_->SetNestedScroll(nestedOpt);
1137 pattern_->isDragging_ = false;
1138 pattern_->currentIndex_ = 3;
1139 EXPECT_EQ(pattern_->gestureSwipeIndex_, 0);
1140
1141 /**
1142 * @tc.steps: step1. Scroll start
1143 * @tc.expected: Can not start because of DisableSwipe
1144 */
1145 pattern_->OnScrollStartRecursive(pattern_, 5.0f, 0.0f);
1146 EXPECT_FALSE(pattern_->childScrolling_);
1147 EXPECT_EQ(pattern_->gestureSwipeIndex_, 0);
1148
1149 /**
1150 * @tc.steps: step2. Scroll end
1151 */
1152 EXPECT_CALL(*mockScroll, OnScrollEndRecursive).Times(0);
1153 EXPECT_CALL(*mockScroll, HandleScrollVelocity).Times(0);
1154 pattern_->parent_ = mockScroll;
1155 pattern_->OnScrollEndRecursive(std::nullopt);
1156 EXPECT_FALSE(pattern_->childScrolling_);
1157 }
1158
1159 /**
1160 * @tc.name: OnChange001
1161 * @tc.desc: Test OnChange event
1162 * @tc.type: FUNC
1163 */
1164 HWTEST_F(SwiperEventTestNg, OnChange001, TestSize.Level1)
1165 {
1166 int32_t currentIndex = 0;
__anon8de6c5f22802(const BaseEventInfo* info) 1167 auto onChange = [¤tIndex](const BaseEventInfo* info) {
1168 const auto* swiperInfo = TypeInfoHelper::DynamicCast<SwiperChangeEvent>(info);
1169 currentIndex = swiperInfo->GetIndex();
1170 };
__anon8de6c5f22902(SwiperModelNG model) 1171 CreateWithItem([=](SwiperModelNG model) {
1172 model.SetOnChange(std::move(onChange));
1173 });
1174
1175 /**
1176 * @tc.steps: step1. Show next page
1177 * @tc.expected: currentIndex change to 1
1178 */
1179 ShowNext();
1180 EXPECT_EQ(currentIndex, 1);
1181
1182 /**
1183 * @tc.steps: step2. Show previous page
1184 * @tc.expected: currentIndex change to 0
1185 */
1186 ShowPrevious();
1187 EXPECT_EQ(currentIndex, 0);
1188 }
1189
1190 /**
1191 * @tc.name: OnAnimation001
1192 * @tc.desc: Test OnAnimationStart OnAnimationEnd event
1193 * @tc.type: FUNC
1194 */
1195 HWTEST_F(SwiperEventTestNg, OnAnimation001, TestSize.Level1)
1196 {
1197 bool isAnimationStart = false;
1198 auto onAnimationStart =
__anon8de6c5f22a02(int32_t index, int32_t targetIndex, const AnimationCallbackInfo& info) 1199 [&isAnimationStart](int32_t index, int32_t targetIndex, const AnimationCallbackInfo& info) {
1200 isAnimationStart = true;
1201 };
1202 bool isAnimationEnd = false;
__anon8de6c5f22b02(int32_t index, const AnimationCallbackInfo& info) 1203 auto onAnimationEnd = [&isAnimationEnd](int32_t index, const AnimationCallbackInfo& info) {
1204 isAnimationEnd = true;
1205 };
__anon8de6c5f22c02(SwiperModelNG model) 1206 CreateWithItem([=](SwiperModelNG model) {
1207 model.SetOnAnimationStart(std::move(onAnimationStart));
1208 model.SetOnAnimationEnd(std::move(onAnimationEnd));
1209 });
1210
1211 /**
1212 * @tc.steps: step1. Show next page
1213 * @tc.expected: Animation event will be called
1214 */
1215 ShowNext();
1216 EXPECT_TRUE(isAnimationStart);
1217 EXPECT_TRUE(isAnimationEnd);
1218 }
1219
1220 /**
1221 * @tc.name: UpdateSwiperPanEvent001
1222 * @tc.desc: Test UpdateSwiperPanEvent
1223 * @tc.type: FUNC
1224 */
1225 HWTEST_F(SwiperEventTestNg, UpdateSwiperPanEvent001, TestSize.Level1)
1226 {
1227 /**
1228 * @tc.steps: step1. DisableSwipe default is false
1229 * @tc.expected: Has panEvent_
1230 */
__anon8de6c5f22d02(SwiperModelNG model) 1231 CreateWithItem([](SwiperModelNG model) {});
1232 EXPECT_FALSE(pattern_->IsDisableSwipe());
1233 EXPECT_NE(pattern_->panEvent_, nullptr);
1234
1235 /**
1236 * @tc.steps: step2. Set DisableSwip to true
1237 * @tc.expected: Has no panEvent_
1238 */
1239 layoutProperty_->UpdateDisableSwipe(true);
1240 frameNode_->MarkModifyDone();
1241 EXPECT_EQ(pattern_->panEvent_, nullptr);
1242
1243 /**
1244 * @tc.steps: step3. When is dragging
1245 * @tc.expected: Stop dragging
1246 */
__anon8de6c5f22e02(SwiperModelNG model) 1247 CreateWithItem([](SwiperModelNG model) {});
1248 GestureEvent info = CreateDragInfo(true);
1249 HandleDragStart(info);
1250 EXPECT_TRUE(pattern_->isTouchDown_);
1251 layoutProperty_->UpdateDisableSwipe(true);
1252 frameNode_->MarkModifyDone();
1253 EXPECT_FALSE(pattern_->isTouchDown_);
1254 }
1255 } // namespace OHOS::Ace::NG
1256