• 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 } // namespace
54 
55 class PanEventTestNg : public testing::Test {
56 public:
57     static void SetUpTestSuite();
58     static void TearDownTestSuite();
59     void SetUp() override;
60     void TearDown() override;
61 };
62 
SetUpTestSuite()63 void PanEventTestNg::SetUpTestSuite()
64 {
65     GTEST_LOG_(INFO) << "PanEventTestNg SetUpTestCase";
66 }
67 
TearDownTestSuite()68 void PanEventTestNg::TearDownTestSuite()
69 {
70     GTEST_LOG_(INFO) << "PanEventTestNg TearDownTestCase";
71 }
72 
SetUp()73 void PanEventTestNg::SetUp()
74 {
75     MockPipelineContext::SetUp();
76 }
77 
TearDown()78 void PanEventTestNg::TearDown()
79 {
80     MockPipelineContext::TearDown();
81 }
82 
83 /**
84  * @tc.name: PanEventOnCollectTouchTargetTest001
85  * @tc.desc: Create PanEvent.
86  * @tc.type: FUNC
87  */
88 HWTEST_F(PanEventTestNg, PanEventOnCollectTouchTargetTest001, TestSize.Level1)
89 {
90     /**
91      * @tc.steps: step1. Create DragEventActuator when fingers number and distance are both less than the default.
92      * @tc.expected: panEventActuator is initialized with the fingers_ and distance_ has been assigned with the default
93      * value.
94      */
95     auto eventHub = AceType::MakeRefPtr<EventHub>();
96     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
97     eventHub->AttachHost(frameNode);
98     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
99     auto panEventActuator = AceType::MakeRefPtr<PanEventActuator>(
100         AceType::WeakClaim(AceType::RawPtr(gestureEventHub)), PAN_EVENT_DIRECTION, FINGERS_NUMBER, DISTANCE);
101     EXPECT_NE(panEventActuator, nullptr);
102     EXPECT_EQ(panEventActuator->fingers_, DEFAULT_PAN_FINGER);
103     EXPECT_EQ(panEventActuator->distance_, DISTANCE);
104 
105     /**
106      * @tc.steps: step2. Create DragEventActuator when fingers number and distance are both greater than the default.
107      * @tc.expected: panEventActuator is initialized with the fingers_ and distance_ defined before.
108      */
109     auto panEventActuator2 = AceType::MakeRefPtr<PanEventActuator>(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)),
110         PAN_EVENT_DIRECTION, FINGERS_NUMBER_GREATER_THAN_DEFAULT, DISTANCE_GREATER_THAN_DEFAULT);
111     EXPECT_NE(panEventActuator2, nullptr);
112     EXPECT_EQ(panEventActuator2->fingers_, FINGERS_NUMBER_GREATER_THAN_DEFAULT);
113     EXPECT_EQ(panEventActuator2->distance_, DISTANCE_GREATER_THAN_DEFAULT);
114 }
115 
116 /**
117  * @tc.name: PanEventOnCollectTouchTargetTest002
118  * @tc.desc: Create PanEvent and invoke OnCollectTouchTarget.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(PanEventTestNg, PanEventOnCollectTouchTargetTest002, TestSize.Level1)
122 {
123     /**
124      * @tc.steps: step1. Create DragEventActuator.
125      */
126     auto eventHub = AceType::MakeRefPtr<EventHub>();
127     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
128     eventHub->AttachHost(frameNode);
129     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
130     auto panEventActuator = AceType::MakeRefPtr<PanEventActuator>(
131         AceType::WeakClaim(AceType::RawPtr(gestureEventHub)), PAN_EVENT_DIRECTION, FINGERS_NUMBER, DISTANCE);
132     EXPECT_NE(panEventActuator, nullptr);
133 
134     /**
135      * @tc.steps: step2. Invoke OnCollectTouchTarget when panEvents_ and userCallback_ are both empty.
136      * @tc.expected: OnCollectTouchTarget function will return directly and result size is 0.
137      */
138     TouchTestResult result;
139     ResponseLinkResult responseLinkResult;
140     panEventActuator->OnCollectTouchTarget(
141         COORDINATE_OFFSET, PAN_EVENT_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
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](
__anonc01442540202( GestureEvent& info) 150                                        GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
151     GestureEventFunc actionUpdate = [&unknownPropertyValue](
__anonc01442540302( GestureEvent& info) 152                                         GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
153     GestureEventFunc actionEnd = [&unknownPropertyValue](
__anonc01442540402( GestureEvent& info) 154                                      GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
__anonc01442540502() 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, responseLinkResult);
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, responseLinkResult);
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_))(info);
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_))(info);
214     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_DEFAULT_VALUE);
215 }
216 } // namespace OHOS::Ace::NG
217