• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     panEventActuator->OnCollectTouchTarget(
141         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
142     EXPECT_EQ(result.size(), PAN_EVENT_TEST_RESULT_SIZE);
143 
144     /**
145      * @tc.steps: step3. ReplacePanEvent to initialize userCallback_ and AddPanEvent to add panEvent into panEvents_.
146      * @tc.expected: userCallback_ is not nullptr and panEvents_ is not empty.
147      */
148     double unknownPropertyValue = GESTURE_EVENT_PROPERTY_DEFAULT_VALUE;
149     GestureEventFunc actionStart = [&unknownPropertyValue](
__anon6e9c53730202( GestureEvent& info) 150                                        GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
151     GestureEventFunc actionUpdate = [&unknownPropertyValue](
__anon6e9c53730302( GestureEvent& info) 152                                         GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
153     GestureEventFunc actionEnd = [&unknownPropertyValue](
__anon6e9c53730402( GestureEvent& info) 154                                      GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
__anon6e9c53730502() 155     GestureEventNoParameter actionCancel = [&unknownPropertyValue]() {
156         unknownPropertyValue = GESTURE_EVENT_PROPERTY_VALUE;
157     };
158 
159     auto panEvent = AceType::MakeRefPtr<PanEvent>(
160         std::move(actionStart), std::move(actionUpdate), std::move(actionEnd), std::move(actionCancel));
161     EXPECT_NE(panEvent, nullptr);
162     panEventActuator->AddPanEvent(panEvent);
163     auto panEventNullptr = AceType::MakeRefPtr<PanEvent>(nullptr, nullptr, nullptr, nullptr);
164     panEventActuator->AddPanEvent(panEventNullptr);
165     EXPECT_FALSE(panEventActuator->panEvents_.empty());
166     panEventActuator->OnCollectTouchTarget(
167         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
168     panEventActuator->ReplacePanEvent(panEvent);
169     EXPECT_NE(panEventActuator->userCallback_, nullptr);
170 
171     /**
172      * @tc.steps: step4. Invoke OnCollectTouchTarget when panEvents_ and userCallback_ are not empty
173      * @tc.expected: result size was increased by one.
174      */
175     panEventActuator->OnCollectTouchTarget(
176         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
177 
178     EXPECT_NE(panEventActuator->panRecognizer_->onActionStart_, nullptr);
179     EXPECT_NE(panEventActuator->panRecognizer_->onActionUpdate_, nullptr);
180     EXPECT_NE(panEventActuator->panRecognizer_->onActionEnd_, nullptr);
181     EXPECT_NE(panEventActuator->panRecognizer_->getEventTargetImpl_, nullptr);
182     EXPECT_EQ(panEventActuator->panRecognizer_->GetCoordinateOffset(), Offset(WIDTH, HEIGHT));
183     EXPECT_EQ(result.size(), PAN_EVENT_TEST_RESULT_SIZE_1);
184 
185     /**
186      * @tc.steps: step5. Invoke onActionStart, onActionUpdate, onActionEnd, onActionCancel when the onActionStart
187      * function exists.
188      * @tc.expected: The functions have been executed and the unknownPropertyValue has been assigned the correct
189      * value.
190      */
191     GestureEvent info = GestureEvent();
192     info.SetScale(GESTURE_EVENT_PROPERTY_VALUE);
193     (*(panEventActuator->panRecognizer_->onActionStart_))(info);
194     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
195     (*(panEventActuator->panRecognizer_->onActionUpdate_))(info);
196     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
197     (*(panEventActuator->panRecognizer_->onActionEnd_))(info);
198     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
199     (*(panEventActuator->panRecognizer_->onActionCancel_))();
200     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
201 
202     /**
203      * @tc.steps: step6. Invoke onActionStart, onActionUpdate, onActionEnd, onActionCancel when the onActionStart
204      * function not exist.
205      * @tc.expected: The functions have not been executed.
206      */
207     panEventActuator->ReplacePanEvent(panEventNullptr);
208     panEventActuator->RemovePanEvent(panEvent);
209     unknownPropertyValue = 0.0;
210     (*(panEventActuator->panRecognizer_->onActionStart_))(info);
211     (*(panEventActuator->panRecognizer_->onActionUpdate_))(info);
212     (*(panEventActuator->panRecognizer_->onActionEnd_))(info);
213     (*(panEventActuator->panRecognizer_->onActionCancel_))();
214     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_DEFAULT_VALUE);
215 }
216 } // namespace OHOS::Ace::NG
217