1 /*
2  * Copyright (c) 2022 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 <clocale>
17 #include <cmath>
18 #include <cstdint>
19 #include <unistd.h>
20 
21 #include "gtest/gtest.h"
22 
23 #include "base/geometry/ng/offset_t.h"
24 #include "base/memory/ace_type.h"
25 #define private public
26 #define protected public
27 #include "core/components_ng/base/frame_node.h"
28 #include "core/components_ng/event/event_hub.h"
29 #include "core/components_ng/event/scrollable_event.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_v2/inspector/inspector_constants.h"
32 #include "test/mock/core/pipeline/mock_pipeline_context.h"
33 
34 using namespace testing;
35 using namespace testing::ext;
36 
37 namespace OHOS::Ace::NG {
38 namespace {
39 constexpr double GESTURE_EVENT_PROPERTY_DEFAULT_VALUE = 0.0;
40 constexpr double GESTURE_EVENT_PROPERTY_VALUE = 10.0;
41 constexpr uint32_t PAN_EVENT_TEST_RESULT_SIZE_1 = 2;
42 constexpr uint32_t PAN_EVENT_TEST_RESULT_SIZE = 0;
43 const TouchRestrict PAN_EVENT_RESTRICT = { TouchRestrict::LONG_PRESS };
44 constexpr float WIDTH = 400.0f;
45 constexpr float HEIGHT = 400.0f;
46 const OffsetF COORDINATE_OFFSET(WIDTH, HEIGHT);
47 const PanDirection PAN_EVENT_DIRECTION = { PanDirection::LEFT };
48 constexpr int32_t FINGERS_NUMBER = 0;
49 constexpr int32_t FINGERS_NUMBER_GREATER_THAN_DEFAULT = 2;
50 constexpr float DISTANCE = 3.0f;
51 constexpr float DISTANCE_GREATER_THAN_DEFAULT = 6.0f;
52 constexpr int32_t DEFAULT_PAN_FINGER = 1;
53 constexpr Dimension DEFAULT_PAN_DISTANCE = 5.0_vp;
54 } // namespace
55 
56 class PanEventTestNg : public testing::Test {
57 public:
58     static void SetUpTestSuite();
59     static void TearDownTestSuite();
60     void SetUp() override;
61     void TearDown() override;
62 };
63 
SetUpTestSuite()64 void PanEventTestNg::SetUpTestSuite()
65 {
66     GTEST_LOG_(INFO) << "PanEventTestNg SetUpTestCase";
67 }
68 
TearDownTestSuite()69 void PanEventTestNg::TearDownTestSuite()
70 {
71     GTEST_LOG_(INFO) << "PanEventTestNg TearDownTestCase";
72 }
73 
SetUp()74 void PanEventTestNg::SetUp()
75 {
76     MockPipelineContext::SetUp();
77 }
78 
TearDown()79 void PanEventTestNg::TearDown()
80 {
81     MockPipelineContext::TearDown();
82 }
83 
84 /**
85  * @tc.name: PanEventOnCollectTouchTargetTest001
86  * @tc.desc: Create PanEvent.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(PanEventTestNg, PanEventOnCollectTouchTargetTest001, TestSize.Level1)
90 {
91     /**
92      * @tc.steps: step1. Create DragEventActuator when fingers number and distance are both less than the default.
93      * @tc.expected: panEventActuator is initialized with the fingers_ and distance_ has been assigned with the default
94      * value.
95      */
96     auto eventHub = AceType::MakeRefPtr<EventHub>();
97     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
98     eventHub->AttachHost(frameNode);
99     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
100     auto panEventActuator = AceType::MakeRefPtr<PanEventActuator>(
101         AceType::WeakClaim(AceType::RawPtr(gestureEventHub)), PAN_EVENT_DIRECTION, FINGERS_NUMBER, DISTANCE);
102     EXPECT_NE(panEventActuator, nullptr);
103     EXPECT_EQ(panEventActuator->fingers_, DEFAULT_PAN_FINGER);
104     EXPECT_EQ(panEventActuator->distance_, DEFAULT_PAN_DISTANCE.ConvertToPx());
105 
106     /**
107      * @tc.steps: step2. Create DragEventActuator when fingers number and distance are both greater than the default.
108      * @tc.expected: panEventActuator is initialized with the fingers_ and distance_ defined before.
109      */
110     auto panEventActuator2 = AceType::MakeRefPtr<PanEventActuator>(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)),
111         PAN_EVENT_DIRECTION, FINGERS_NUMBER_GREATER_THAN_DEFAULT, DISTANCE_GREATER_THAN_DEFAULT);
112     EXPECT_NE(panEventActuator2, nullptr);
113     EXPECT_EQ(panEventActuator2->fingers_, FINGERS_NUMBER_GREATER_THAN_DEFAULT);
114     EXPECT_EQ(panEventActuator2->distance_, DISTANCE_GREATER_THAN_DEFAULT);
115 }
116 
117 /**
118  * @tc.name: PanEventOnCollectTouchTargetTest002
119  * @tc.desc: Create PanEvent and invoke OnCollectTouchTarget.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(PanEventTestNg, PanEventOnCollectTouchTargetTest002, TestSize.Level1)
123 {
124     /**
125      * @tc.steps: step1. Create DragEventActuator.
126      */
127     auto eventHub = AceType::MakeRefPtr<EventHub>();
128     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
129     eventHub->AttachHost(frameNode);
130     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
131     auto panEventActuator = AceType::MakeRefPtr<PanEventActuator>(
132         AceType::WeakClaim(AceType::RawPtr(gestureEventHub)), PAN_EVENT_DIRECTION, FINGERS_NUMBER, DISTANCE);
133     EXPECT_NE(panEventActuator, nullptr);
134 
135     /**
136      * @tc.steps: step2. Invoke OnCollectTouchTarget when panEvents_ and userCallback_ are both empty.
137      * @tc.expected: OnCollectTouchTarget function will return directly and result size is 0.
138      */
139     TouchTestResult result;
140     ResponseLinkResult responseLinkResult;
141     panEventActuator->OnCollectTouchTarget(
142         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
143     EXPECT_EQ(result.size(), PAN_EVENT_TEST_RESULT_SIZE);
144 
145     /**
146      * @tc.steps: step3. ReplacePanEvent to initialize userCallback_ and AddPanEvent to add panEvent into panEvents_.
147      * @tc.expected: userCallback_ is not nullptr and panEvents_ is not empty.
148      */
149     double unknownPropertyValue = GESTURE_EVENT_PROPERTY_DEFAULT_VALUE;
150     GestureEventFunc actionStart = [&unknownPropertyValue](
__anon02c718ea0202( GestureEvent& info) 151                                        GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
152     GestureEventFunc actionUpdate = [&unknownPropertyValue](
__anon02c718ea0302( GestureEvent& info) 153                                         GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
154     GestureEventFunc actionEnd = [&unknownPropertyValue](
__anon02c718ea0402( GestureEvent& info) 155                                      GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
__anon02c718ea0502() 156     GestureEventNoParameter actionCancel = [&unknownPropertyValue]() {
157         unknownPropertyValue = GESTURE_EVENT_PROPERTY_VALUE;
158     };
159 
160     auto panEvent = AceType::MakeRefPtr<PanEvent>(
161         std::move(actionStart), std::move(actionUpdate), std::move(actionEnd), std::move(actionCancel));
162     EXPECT_NE(panEvent, nullptr);
163     panEventActuator->AddPanEvent(panEvent);
164     auto panEventNullptr = AceType::MakeRefPtr<PanEvent>(nullptr, nullptr, nullptr, nullptr);
165     panEventActuator->AddPanEvent(panEventNullptr);
166     EXPECT_FALSE(panEventActuator->panEvents_.empty());
167     panEventActuator->OnCollectTouchTarget(
168         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
169     panEventActuator->ReplacePanEvent(panEvent);
170     EXPECT_NE(panEventActuator->userCallback_, nullptr);
171 
172     /**
173      * @tc.steps: step4. Invoke OnCollectTouchTarget when panEvents_ and userCallback_ are not empty
174      * @tc.expected: result size was increased by one.
175      */
176     panEventActuator->OnCollectTouchTarget(
177         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
178 
179     EXPECT_NE(panEventActuator->panRecognizer_->onActionStart_, nullptr);
180     EXPECT_NE(panEventActuator->panRecognizer_->onActionUpdate_, nullptr);
181     EXPECT_NE(panEventActuator->panRecognizer_->onActionEnd_, nullptr);
182     EXPECT_NE(panEventActuator->panRecognizer_->getEventTargetImpl_, nullptr);
183     EXPECT_EQ(panEventActuator->panRecognizer_->GetCoordinateOffset(), Offset(WIDTH, HEIGHT));
184     EXPECT_EQ(result.size(), PAN_EVENT_TEST_RESULT_SIZE_1);
185 
186     /**
187      * @tc.steps: step5. Invoke onActionStart, onActionUpdate, onActionEnd, onActionCancel when the onActionStart
188      * function exists.
189      * @tc.expected: The functions have been executed and the unknownPropertyValue has been assigned the correct
190      * value.
191      */
192     GestureEvent info = GestureEvent();
193     info.SetScale(GESTURE_EVENT_PROPERTY_VALUE);
194     (*(panEventActuator->panRecognizer_->onActionStart_))(info);
195     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
196     (*(panEventActuator->panRecognizer_->onActionUpdate_))(info);
197     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
198     (*(panEventActuator->panRecognizer_->onActionEnd_))(info);
199     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
200     (*(panEventActuator->panRecognizer_->onActionCancel_))();
201     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
202 
203     /**
204      * @tc.steps: step6. Invoke onActionStart, onActionUpdate, onActionEnd, onActionCancel when the onActionStart
205      * function not exist.
206      * @tc.expected: The functions have not been executed.
207      */
208     panEventActuator->ReplacePanEvent(panEventNullptr);
209     panEventActuator->RemovePanEvent(panEvent);
210     unknownPropertyValue = 0.0;
211     (*(panEventActuator->panRecognizer_->onActionStart_))(info);
212     (*(panEventActuator->panRecognizer_->onActionUpdate_))(info);
213     (*(panEventActuator->panRecognizer_->onActionEnd_))(info);
214     (*(panEventActuator->panRecognizer_->onActionCancel_))();
215     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_DEFAULT_VALUE);
216 }
217 } // namespace OHOS::Ace::NG
218