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 #include "gtest/gtest.h"
16 #include "test/unittest/core/gestures/gestures_common_test_ng.h"
17
18 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
19 #include "core/event/ace_events.h"
20 #include "core/gestures/gesture_info.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Ace::NG {
26
27 namespace {
28 constexpr int32_t DEFAULT_TOUCH_FINGER_ID = 0;
29 constexpr int32_t DEFAULT_TOUCH_POINT_SIZE = 5;
30 constexpr int32_t DEFAULT_MOVE_DISTANCE = 100;
31 constexpr uint64_t DEFAULT_MOVE_TIME = 100;
32 } // namespace
33
34 class PanRecognizerVelocityTestNg : public GesturesCommonTestNg {
35 public:
36 static void SetUpTestSuite();
37 static void TearDownTestSuite();
38 };
39
SetUpTestSuite()40 void PanRecognizerVelocityTestNg::SetUpTestSuite()
41 {
42 MockPipelineContext::SetUp();
43 }
44
TearDownTestSuite()45 void PanRecognizerVelocityTestNg::TearDownTestSuite()
46 {
47 MockPipelineContext::TearDown();
48 }
49
50 /**
51 * @tc.name: PanRecognizerHandleTouchUpVelocityTest001
52 * @tc.desc: Test PanRecognizer HandleTouchUpEvent panVelocity_.Reset.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(PanRecognizerVelocityTestNg, PanRecognizerHandleTouchUpVelocityTest001, TestSize.Level1)
56 {
57 /**
58 * @tc.steps: step1. create PanRecognizer.
59 */
60 RefPtr<PanGestureOption> panGestureOption = AceType::MakeRefPtr<PanGestureOption>();
61 RefPtr<PanRecognizer> panRecognizer = AceType::MakeRefPtr<PanRecognizer>(panGestureOption);
62 panRecognizer->panVelocity_.ResetAll();
63
64 /**
65 * @tc.steps: step2. update default touch point.
66 */
67 for (int32_t i = 0; i < DEFAULT_TOUCH_POINT_SIZE; i++) {
68 TouchEvent touchEvent;
69 touchEvent.id = DEFAULT_TOUCH_FINGER_ID;
70 touchEvent.x = DEFAULT_MOVE_DISTANCE * i;
71 touchEvent.y = 0;
72 touchEvent.time = TimeStamp(std::chrono::nanoseconds(DEFAULT_MOVE_TIME));
73 panRecognizer->panVelocity_.UpdateTouchPoint(DEFAULT_TOUCH_FINGER_ID, touchEvent, false);
74 }
75
76 /**
77 * @tc.steps: step3. update default panRecognizer info.
78 */
79 TouchEvent triggerTouchEvent;
80 triggerTouchEvent.x = DEFAULT_MOVE_DISTANCE * (DEFAULT_TOUCH_POINT_SIZE + 1);
81 triggerTouchEvent.type = TouchType::UP;
82 panRecognizer->fingersId_.insert(1);
83 panRecognizer->refereeState_ = RefereeState::FAIL;
84
85 /**
86 * @tc.steps: step4. test panVelocity_.Reset.
87 * @tc.expected: if last finger up, not reset finger's velocity.
88 */
89 panRecognizer->currentFingers_ = 1;
90 panRecognizer->fingers_ = 1;
91 panRecognizer->HandleTouchUpEvent(triggerTouchEvent);
92 EXPECT_EQ(panRecognizer->panVelocity_.trackerMap_[0].xAxis_.GetTrackNum(), 5);
93
94 /**
95 * @tc.steps: step5. test panVelocity_.Reset.
96 * @tc.expected: if not last finger up, reset finger's velocity.
97 */
98 panRecognizer->currentFingers_ = 2;
99 panRecognizer->fingers_ = 1;
100 panRecognizer->HandleTouchUpEvent(triggerTouchEvent);
101 EXPECT_EQ(panRecognizer->panVelocity_.trackerMap_[0].xAxis_.GetTrackNum(), 0);
102
103
104 /**
105 * @tc.steps: step5. test panVelocity_.Reset.
106 * @tc.expected: if not last finger up, reset finger's velocity.
107 */
108 panRecognizer->refereeState_ = RefereeState::FAIL;
109 TouchEvent moveEvent;
110 moveEvent.type = TouchType::MOVE;
111 EXPECT_EQ(panRecognizer->panVelocity_.trackerMap_[0].xAxis_.GetTrackNum(), 0);
112 panRecognizer->HandleTouchMoveEvent(moveEvent);
113 EXPECT_EQ(panRecognizer->panVelocity_.trackerMap_[0].xAxis_.GetTrackNum(), 0);
114 }
115 } // namespace OHOS::Ace::NG