• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/unittest/core/gestures/gestures_common_test_ng.h"
16 #include "test/unittest/core/gestures/recognizer_test_utils.h"
17 #include "test/unittest/core/gestures/swipe_recognizer_test_utils.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS::Ace::NG {
23 
24 struct SwipeInputInfos {
25     PointF startPoint;
26     int32_t fingerId;
27     SwipeInputDirection direction;
28     ComparationResult compare;
SwipeInputInfosOHOS::Ace::NG::SwipeInputInfos29     SwipeInputInfos(PointF startPoint, int32_t fingerId, SwipeInputDirection direction, ComparationResult compare)
30         : startPoint(startPoint), fingerId(fingerId), direction(direction), compare(compare)
31     {}
32 };
33 
34 struct SwipeTestCase {
35     int32_t fingers = 0;
36     double speed = 0.0;
37     SwipeDirection direction;
38     std::vector<SwipeInputInfos> infos;
39     RefereeState expectResult;
SwipeTestCaseOHOS::Ace::NG::SwipeTestCase40     SwipeTestCase(int32_t fingers, double speed, SwipeDirection direction, std::vector<SwipeInputInfos> infos,
41         RefereeState expectResult)
42         : fingers(fingers), speed(speed), direction(direction), infos(std::move(infos)), expectResult(expectResult)
43     {}
44 };
45 
46 const std::vector<SwipeTestCase> TEST_CASES = {
47     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
48         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS),
49             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS) },
50         RefereeState::FAIL), // case 0
51     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
52         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
53             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT) },
54         RefereeState::SUCCEED), // case 1
55     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
56         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
57             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
58         RefereeState::FAIL), // case 2
59     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
60         { SwipeInputInfos(
61             PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
62             SwipeInputInfos(
63                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT) },
64         RefereeState::FAIL), // case 3
65     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
66         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
67             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL) },
68         RefereeState::SUCCEED), // case 4
69     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
70         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::LESS) },
71         RefereeState::FAIL), // case 5
72     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
73         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT) },
74         RefereeState::SUCCEED), // case 6
75     SwipeTestCase(1, 100.0, { SwipeDirection::VERTICAL },
76         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL) },
77         RefereeState::SUCCEED), // case 7
78     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
79         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
80             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::LESS) },
81         RefereeState::FAIL), // case 8
82     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
83         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
84             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT) },
85         RefereeState::FAIL), // case 9
86     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
87         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
88             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
89         RefereeState::SUCCEED), // case 10
90     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
91         { SwipeInputInfos(
92             PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
93             SwipeInputInfos(
94                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT) },
95         RefereeState::FAIL), // case 11
96     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
97         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL),
98             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL) },
99         RefereeState::SUCCEED), // case 12
100     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
101         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS) },
102         RefereeState::FAIL), // case 13
103     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
104         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
105         RefereeState::SUCCEED), // case 14
106     SwipeTestCase(1, 100.0, { SwipeDirection::HORIZONTAL },
107         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL) },
108         RefereeState::SUCCEED), // case 15
109     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
110         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
111             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::LESS) },
112         RefereeState::DETECTING), // case 16
113     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
114         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
115             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT) },
116         RefereeState::SUCCEED), // case 17
117     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
118         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
119             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
120         RefereeState::SUCCEED), // case 18
121     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
122         { SwipeInputInfos(
123             PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
124             SwipeInputInfos(
125                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT) },
126         RefereeState::FAIL), // case 19
127     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
128         { SwipeInputInfos(
129             PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
130             SwipeInputInfos(
131                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT) },
132         RefereeState::SUCCEED), // case 20
133     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
134         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
135             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL) },
136         RefereeState::SUCCEED), // case 21
137     SwipeTestCase(1, 100.0, { SwipeDirection::ALL },
138         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
139         RefereeState::SUCCEED), // case 22
140     SwipeTestCase(1, 100.0, { SwipeDirection::NONE },
141         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
142             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
143         RefereeState::FAIL), // case 23
144     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
145         {
146             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS),
147             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS),
148             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS),
149         },
150         RefereeState::FAIL), // case 24
151     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
152         {
153             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
154             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
155             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
156         },
157         RefereeState::SUCCEED), // case 25
158     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
159         {
160             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
161             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
162             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
163         },
164         RefereeState::FAIL), // case 26
165     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
166         {
167             SwipeInputInfos(
168                 PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
169             SwipeInputInfos(
170                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
171             SwipeInputInfos(
172                 PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
173         },
174         RefereeState::FAIL), // case 27
175     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
176         {
177             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
178             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
179             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
180         },
181         RefereeState::SUCCEED), // case 28
182     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
183         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
184             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::LESS) },
185         RefereeState::FAIL), // case 29
186     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
187         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
188             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT) },
189         RefereeState::SUCCEED), // case 30
190     SwipeTestCase(2, 200.0, { SwipeDirection::VERTICAL },
191         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
192             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL) },
193         RefereeState::SUCCEED), // case 31
194     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
195         {
196             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
197             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
198             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
199         },
200         RefereeState::FAIL), // case 32
201     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
202         {
203             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
204             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
205             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
206         },
207         RefereeState::FAIL), // case 33
208     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
209         {
210             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
211             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
212             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
213         },
214         RefereeState::SUCCEED), // case 34
215     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
216         {
217             SwipeInputInfos(
218                 PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
219             SwipeInputInfos(
220                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
221             SwipeInputInfos(
222                 PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
223         },
224         RefereeState::FAIL), // case 35
225     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
226         {
227             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL),
228             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL),
229             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL),
230         },
231         RefereeState::SUCCEED), // case 36
232     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
233         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS),
234             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::LESS) },
235         RefereeState::FAIL), // case 37
236     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
237         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
238             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT) },
239         RefereeState::SUCCEED), // case 38
240     SwipeTestCase(2, 200.0, { SwipeDirection::HORIZONTAL },
241         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL),
242             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::EQUAL) },
243         RefereeState::SUCCEED), // case 39
244     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
245         {
246             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
247             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
248             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
249         },
250         RefereeState::FAIL), // case 40
251     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
252         {
253             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
254             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
255             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
256         },
257         RefereeState::SUCCEED), // case 41
258     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
259         {
260             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
261             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
262             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL, ComparationResult::GREAT),
263         },
264         RefereeState::SUCCEED), // case 42
265     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
266         {
267             SwipeInputInfos(
268                 PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
269             SwipeInputInfos(
270                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
271             SwipeInputInfos(
272                 PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
273         },
274         RefereeState::FAIL), // case 43
275     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
276         {
277             SwipeInputInfos(
278                 PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
279             SwipeInputInfos(
280                 PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
281             SwipeInputInfos(
282                 PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
283         },
284         RefereeState::SUCCEED), // case 44
285     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
286         {
287             SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
288             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
289             SwipeInputInfos(PointF { 100.0f, 100.0f }, 2, SwipeInputDirection::VERTICAL, ComparationResult::EQUAL),
290         },
291         RefereeState::SUCCEED), // case 45
292     SwipeTestCase(2, 200.0, { SwipeDirection::ALL },
293         { SwipeInputInfos(PointF { 100.0f, 100.0f }, 0, SwipeInputDirection::VERTICAL, ComparationResult::GREAT),
294             SwipeInputInfos(PointF { 100.0f, 100.0f }, 1, SwipeInputDirection::VERTICAL, ComparationResult::GREAT) },
295         RefereeState::SUCCEED), // case 46
296 };
297 
298 namespace {
299 
CreateDownEvent(const SwipeInputInfos & info)300 TouchEvent CreateDownEvent(const SwipeInputInfos& info)
301 {
302     TouchEvent downEvent;
303     std::chrono::nanoseconds nanoseconds(1000);
304     downEvent.SetId(info.fingerId)
305         .SetType(TouchType::DOWN)
306         .SetX(info.startPoint.GetX())
307         .SetY(info.startPoint.GetY())
308         .SetTime(TimeStamp(nanoseconds));
309     return downEvent;
310 }
311 
CreateMoveAndUpEvents(const SwipeInputInfos & info,double speed,std::unordered_map<int32_t,std::vector<TouchEvent>> & moveEvents,std::vector<TouchEvent> & upEvents)312 void CreateMoveAndUpEvents(const SwipeInputInfos& info, double speed,
313     std::unordered_map<int32_t, std::vector<TouchEvent>>& moveEvents, std::vector<TouchEvent>& upEvents)
314 {
315     auto timeStep = (SWIPE_MOVE_STEP / speed) * 1000 * 1000 * 1000;
316     for (int32_t num = 1; num <= STEP_COUNT; num++) {
317         TouchEvent moveEvent;
318         std::chrono::nanoseconds moveNanoseconds(static_cast<int64_t>(1000 + num * timeStep));
319         moveEvent.SetId(info.fingerId).SetType(TouchType::MOVE).SetTime(TimeStamp(moveNanoseconds));
320 
321         float xOffset = 0.0f;
322         float yOffset = 0.0f;
323 
324         if (info.direction == SwipeInputDirection::HORIZONTAL ||
325             info.direction == SwipeInputDirection::HORIZONTAL_VERTICAL) {
326             xOffset = SWIPE_MOVE_STEP * num;
327         }
328         if (info.direction == SwipeInputDirection::VERTICAL ||
329             info.direction == SwipeInputDirection::VERTICAL_HORIZONTAL) {
330             yOffset = SWIPE_MOVE_STEP * num;
331         }
332         if (info.direction == SwipeInputDirection::VERTICAL_HORIZONTAL && num > 3) {
333             xOffset = SWIPE_MOVE_STEP * (num - 3);
334             yOffset = SWIPE_MOVE_STEP * 3;
335         } else if (info.direction == SwipeInputDirection::HORIZONTAL_VERTICAL && num > 3) {
336             xOffset = SWIPE_MOVE_STEP * 3;
337             yOffset = SWIPE_MOVE_STEP * (num - 3);
338         }
339 
340         moveEvent.SetX(info.startPoint.GetX() + xOffset);
341         moveEvent.SetY(info.startPoint.GetY() + yOffset);
342         moveEvents[info.fingerId].emplace_back(moveEvent);
343     }
344     TouchEvent upEvent;
345     upEvent.SetId(info.fingerId)
346         .SetType(TouchType::UP)
347         .SetX(moveEvents[info.fingerId].back().x)
348         .SetY(moveEvents[info.fingerId].back().y)
349         .SetTime(moveEvents[info.fingerId].back().time);
350     upEvents.emplace_back(upEvent);
351 }
352 
CreateTouchEvents(const std::vector<SwipeInputInfos> & infos,double speed,std::vector<TouchEvent> & downEvents,std::unordered_map<int32_t,std::vector<TouchEvent>> & moveEvents,std::vector<TouchEvent> & upEvents)353 void CreateTouchEvents(const std::vector<SwipeInputInfos>& infos, double speed, std::vector<TouchEvent>& downEvents,
354     std::unordered_map<int32_t, std::vector<TouchEvent>>& moveEvents, std::vector<TouchEvent>& upEvents)
355 {
356     for (const auto& info : infos) {
357         auto singleFingerSpeed = AdjustSpeed(speed, info.compare);
358         TouchEvent downEvent = CreateDownEvent(info);
359         downEvents.emplace_back(downEvent);
360         CreateMoveAndUpEvents(info, singleFingerSpeed, moveEvents, upEvents);
361     }
362 }
363 
364 } // namespace
365 
366 class SwipeRecognizerBaseTestNg : public GesturesCommonTestNg {
367 public:
368     static void SetUpTestSuite();
369     static void TearDownTestSuite();
370 };
371 
SetUpTestSuite()372 void SwipeRecognizerBaseTestNg::SetUpTestSuite()
373 {
374     MockPipelineContext::SetUp();
375 }
376 
TearDownTestSuite()377 void SwipeRecognizerBaseTestNg::TearDownTestSuite()
378 {
379     MockPipelineContext::TearDown();
380 }
381 
382 /**
383  * @tc.name: SwipeRecognizerBaseTest001
384  * @tc.desc: Test SwipeRecognizer: Basic function test
385  * @tc.type: FUNC
386  */
387 HWTEST_F(SwipeRecognizerBaseTestNg, SwipeRecognizerBaseTest001, TestSize.Level1)
388 {
389     int32_t caseNum = 0;
390     for (const auto& testCase : TEST_CASES) {
391         /**
392          * @tc.steps: step1. create SwipeRecognizer.
393          */
394         auto swipeRecognizer =
395             AceType::MakeRefPtr<SwipeRecognizer>(testCase.fingers, testCase.direction, testCase.speed);
396         /**
397          * @tc.steps: step2. DispatchTouchEvent.
398          * @tc.expected: step2. refereeState_ equals.
399          */
400         std::vector<TouchEvent> downEvents;
401         std::unordered_map<int32_t, std::vector<TouchEvent>> moveEvents;
402         std::vector<TouchEvent> upEvents;
403         CreateTouchEvents(testCase.infos, testCase.speed, downEvents, moveEvents, upEvents);
404         for (const auto& item : downEvents) {
405             swipeRecognizer->HandleEvent(item);
406         }
407         for (int32_t index = 0; index < STEP_COUNT; index++) {
408             for (const auto& item : moveEvents) {
409                 swipeRecognizer->HandleEvent((item.second)[index]);
410             }
411         }
412         auto eventCount = 0;
413         for (const auto& item : upEvents) {
414             swipeRecognizer->HandleEvent(item);
415             eventCount++;
416             if (eventCount == testCase.fingers) {
417                 break;
418             }
419         }
420         EXPECT_TRUE(IsRecognizerStateEqual(caseNum, swipeRecognizer->refereeState_, testCase.expectResult));
421         caseNum++;
422     }
423 }
424 
425 /**
426  * @tc.name: SwipeRecognizerBaseTest002
427  * @tc.desc: Test SwipeRecognizer: Test for TriggerGestureJudgeCallback.
428  * @tc.type: FUNC
429  */
430 HWTEST_F(SwipeRecognizerBaseTestNg, SwipeRecognizerBaseTest002, TestSize.Level1)
431 {
432     /**
433      * @tc.steps: step1. create swipeRecognizer.
434      */
435     int32_t fingers = 1;
436     SwipeDirection direction = { SwipeDirection::HORIZONTAL };
437     double speed = 10.0;
438     auto swipeRecognizer = AceType::MakeRefPtr<SwipeRecognizer>(fingers, direction, speed);
439     RefPtr<NG::TargetComponent> targetComponent = AceType::MakeRefPtr<NG::TargetComponent>();
440     auto judgeFunc1 = [](const std::shared_ptr<BaseGestureEvent>& info, const RefPtr<NGGestureRecognizer>& current,
__anonb7f6bb9d0202(const std::shared_ptr<BaseGestureEvent>& info, const RefPtr<NGGestureRecognizer>& current, const std::list<RefPtr<NGGestureRecognizer>>& others) 441                           const std::list<RefPtr<NGGestureRecognizer>>& others) -> GestureJudgeResult {
442         return GestureJudgeResult::REJECT;
443     };
444     auto judgeFunc2 = [](const RefPtr<GestureInfo>& gestureInfo,
__anonb7f6bb9d0302(const RefPtr<GestureInfo>& gestureInfo, const std::shared_ptr<BaseGestureEvent>& info) 445                           const std::shared_ptr<BaseGestureEvent>& info) -> GestureJudgeResult {
446         return GestureJudgeResult::REJECT;
447     };
448     targetComponent->SetOnGestureRecognizerJudgeBegin(std::move(judgeFunc1));
449     targetComponent->onGestureJudgeBegin_ = judgeFunc2;
450     swipeRecognizer->SetTargetComponent(targetComponent);
451     /**
452      * @tc.steps: step2. TriggerGestureJudgeCallback with inputEventType::AXIS.
453      * @tc.expected: step2. result equals.
454      */
455     AxisEvent axisEvent;
456     swipeRecognizer->inputEventType_ = InputEventType::AXIS;
457     swipeRecognizer->lastAxisEvent_ = axisEvent;
458     EXPECT_EQ(swipeRecognizer->TriggerGestureJudgeCallback(), GestureJudgeResult::REJECT);
459 
460     /**
461      * @tc.steps: step3. TriggerGestureJudgeCallback with inputEventType::TOUCH_SCREEN.
462      * @tc.expected: step3. result equals.
463      */
464     TouchEvent touchEvent;
465     swipeRecognizer->inputEventType_ = InputEventType::TOUCH_SCREEN;
466     swipeRecognizer->lastTouchEvent_ = touchEvent;
467     EXPECT_EQ(swipeRecognizer->TriggerGestureJudgeCallback(), GestureJudgeResult::REJECT);
468 }
469 } // namespace OHOS::Ace::NG