• 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 #include "core/event/ace_events.h"
20 #include "core/event/axis_event.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS::Ace::NG {
26 
27 struct SwipeAxisInputInfo {
28     PointF startPoint;
29     SourceTool sourceTool;
30     bool isShiftKeyPressed = false;
31     SwipeInputDirection direction;
32     ComparationResult compare;
SwipeAxisInputInfoOHOS::Ace::NG::SwipeAxisInputInfo33     SwipeAxisInputInfo(PointF startPoint, SourceTool sourceTool, bool isShiftKeyPressed, SwipeInputDirection direction,
34         ComparationResult compare)
35         : startPoint(startPoint), sourceTool(sourceTool), isShiftKeyPressed(isShiftKeyPressed), direction(direction),
36           compare(compare)
37     {}
38 };
39 
40 struct SwipeAxisTestCase {
41     double speed = 0.0;
42     SwipeDirection direction;
43     SwipeAxisInputInfo info;
44     RefereeState expectResult;
SwipeAxisTestCaseOHOS::Ace::NG::SwipeAxisTestCase45     SwipeAxisTestCase(double speed, SwipeDirection direction, SwipeAxisInputInfo info, RefereeState expectResult)
46         : speed(speed), direction(direction), info(info), expectResult(expectResult)
47     {}
48 };
49 
50 const std::vector<SwipeAxisTestCase> AXIS_TEST_CASES = {
51     SwipeAxisTestCase(100.0, { SwipeDirection::VERTICAL },
52         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::HORIZONTAL,
53             ComparationResult::LESS),
54         RefereeState::FAIL), // case 0
55     SwipeAxisTestCase(100.0, { SwipeDirection::VERTICAL },
56         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
57             ComparationResult::GREAT),
58         RefereeState::SUCCEED), // case 1
59     SwipeAxisTestCase(100.0, { SwipeDirection::VERTICAL },
60         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::HORIZONTAL,
61             ComparationResult::GREAT),
62         RefereeState::FAIL), // case 2
63     SwipeAxisTestCase(100.0, { SwipeDirection::VERTICAL },
64         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false,
65             SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
66         RefereeState::FAIL), // case 3
67     SwipeAxisTestCase(100.0, { SwipeDirection::VERTICAL },
68         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
69             ComparationResult::EQUAL),
70         RefereeState::SUCCEED), // case 4
71     SwipeAxisTestCase(200.0, { SwipeDirection::VERTICAL },
72         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::MOUSE, false, SwipeInputDirection::VERTICAL,
73             ComparationResult::LESS),
74         RefereeState::SUCCEED), // case 5
75     SwipeAxisTestCase(200.0, { SwipeDirection::VERTICAL },
76         SwipeAxisInputInfo(
77             PointF { 100.0f, 100.0f }, SourceTool::MOUSE, true, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
78         RefereeState::FAIL), // case 6
79     SwipeAxisTestCase(100.0, { SwipeDirection::HORIZONTAL },
80         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
81             ComparationResult::LESS),
82         RefereeState::FAIL), // case 7
83     SwipeAxisTestCase(100.0, { SwipeDirection::HORIZONTAL },
84         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
85             ComparationResult::GREAT),
86         RefereeState::FAIL), // case 8
87     SwipeAxisTestCase(100.0, { SwipeDirection::HORIZONTAL },
88         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::HORIZONTAL,
89             ComparationResult::GREAT),
90         RefereeState::SUCCEED), // case 9
91     SwipeAxisTestCase(100.0, { SwipeDirection::HORIZONTAL },
92         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false,
93             SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
94         RefereeState::FAIL), // case 10
95     SwipeAxisTestCase(100.0, { SwipeDirection::HORIZONTAL },
96         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::HORIZONTAL,
97             ComparationResult::EQUAL),
98         RefereeState::SUCCEED), // case 11
99     SwipeAxisTestCase(200.0, { SwipeDirection::HORIZONTAL },
100         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::MOUSE, false, SwipeInputDirection::VERTICAL,
101             ComparationResult::LESS),
102         RefereeState::SUCCEED), // case 12
103     SwipeAxisTestCase(200.0, { SwipeDirection::HORIZONTAL },
104         SwipeAxisInputInfo(
105             PointF { 100.0f, 100.0f }, SourceTool::MOUSE, true, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
106         RefereeState::SUCCEED), // case 13
107     SwipeAxisTestCase(100.0, { SwipeDirection::ALL },
108         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
109             ComparationResult::LESS),
110         RefereeState::FAIL), // case 14
111     SwipeAxisTestCase(100.0, { SwipeDirection::ALL },
112         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
113             ComparationResult::GREAT),
114         RefereeState::SUCCEED), // case 15
115     SwipeAxisTestCase(100.0, { SwipeDirection::ALL },
116         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::HORIZONTAL,
117             ComparationResult::GREAT),
118         RefereeState::SUCCEED), // case 16
119     SwipeAxisTestCase(100.0, { SwipeDirection::ALL },
120         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false,
121             SwipeInputDirection::VERTICAL_HORIZONTAL, ComparationResult::GREAT),
122         RefereeState::FAIL), // case 17
123     SwipeAxisTestCase(100.0, { SwipeDirection::ALL },
124         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false,
125             SwipeInputDirection::HORIZONTAL_VERTICAL, ComparationResult::GREAT),
126         RefereeState::SUCCEED), // case 18
127     SwipeAxisTestCase(100.0, { SwipeDirection::ALL },
128         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::VERTICAL,
129             ComparationResult::EQUAL),
130         RefereeState::SUCCEED), // case 19
131     SwipeAxisTestCase(200.0, { SwipeDirection::ALL },
132         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::MOUSE, false, SwipeInputDirection::VERTICAL,
133             ComparationResult::LESS),
134         RefereeState::SUCCEED), // case 20
135     SwipeAxisTestCase(200.0, { SwipeDirection::ALL },
136         SwipeAxisInputInfo(
137             PointF { 100.0f, 100.0f }, SourceTool::MOUSE, true, SwipeInputDirection::VERTICAL, ComparationResult::LESS),
138         RefereeState::SUCCEED), // case 21
139     SwipeAxisTestCase(100.0, { SwipeDirection::NONE },
140         SwipeAxisInputInfo(PointF { 100.0f, 100.0f }, SourceTool::TOUCHPAD, false, SwipeInputDirection::HORIZONTAL,
141             ComparationResult::GREAT),
142         RefereeState::FAIL), // case 22
143 };
144 
145 namespace {
CreateAxisEvents(const SwipeAxisInputInfo & info,double speed,std::vector<AxisEvent> & moveEvents,AxisEvent & upEvent)146 void CreateAxisEvents(
147     const SwipeAxisInputInfo& info, double speed, std::vector<AxisEvent>& moveEvents, AxisEvent& upEvent)
148 {
149     auto inputSpeed = AdjustSpeed(speed, info.compare);
150     float step = (info.sourceTool == SourceTool::TOUCHPAD) ? SWIPE_MOVE_STEP : SWIPE_MOVE_STEP / MOUSE_RATIO;
151     auto timeStep = (SWIPE_MOVE_STEP / inputSpeed) * 1000 * 1000 * 1000;
152     for (int32_t num = 1; num <= STEP_COUNT; num++) {
153         AxisEvent moveEvent;
154         moveEvent.x = info.startPoint.GetX();
155         moveEvent.y = info.startPoint.GetY();
156         moveEvent.action = AxisAction::UPDATE;
157         moveEvent.time = TimeStamp(std::chrono::nanoseconds(static_cast<int64_t>(1000 + num * timeStep)));
158         moveEvent.sourceTool = info.sourceTool;
159         if (info.isShiftKeyPressed) {
160             moveEvent.pressedCodes.emplace_back(KeyCode::KEY_SHIFT_LEFT);
161         }
162         if (info.sourceTool == SourceTool::TOUCHPAD) {
163             bool isHorizontal = (info.direction == SwipeInputDirection::HORIZONTAL ||
164                                  info.direction == SwipeInputDirection::HORIZONTAL_VERTICAL);
165             bool isVertical = (info.direction == SwipeInputDirection::VERTICAL ||
166                                info.direction == SwipeInputDirection::VERTICAL_HORIZONTAL);
167             if (isHorizontal) {
168                 moveEvent.horizontalAxis = step;
169             } else if (isVertical) {
170                 moveEvent.verticalAxis = step;
171             }
172             if (info.direction == SwipeInputDirection::HORIZONTAL_VERTICAL && num > 3) {
173                 moveEvent.verticalAxis = step;
174                 moveEvent.horizontalAxis = 0.0;
175             }
176             if (info.direction == SwipeInputDirection::VERTICAL_HORIZONTAL && num > 3) {
177                 moveEvent.horizontalAxis = step;
178                 moveEvent.verticalAxis = 0.0;
179             }
180         } else {
181             moveEvent.verticalAxis = step;
182         }
183         moveEvents.emplace_back(moveEvent);
184     }
185     upEvent.x = info.startPoint.GetX();
186     upEvent.y = info.startPoint.GetY();
187     upEvent.action = AxisAction::END;
188     upEvent.time = moveEvents.back().time;
189     upEvent.sourceTool = info.sourceTool;
190 }
191 
192 } // namespace
193 
194 class SwipeRecognizerAxisBaseTestNg : public GesturesCommonTestNg {
195 public:
196     static void SetUpTestSuite();
197     static void TearDownTestSuite();
198 };
199 
SetUpTestSuite()200 void SwipeRecognizerAxisBaseTestNg::SetUpTestSuite()
201 {
202     MockPipelineContext::SetUp();
203 }
204 
TearDownTestSuite()205 void SwipeRecognizerAxisBaseTestNg::TearDownTestSuite()
206 {
207     MockPipelineContext::TearDown();
208 }
209 
210 /**
211  * @tc.name: SwipeRecognizerAxisBaseTest001
212  * @tc.desc: Test PanRecognizer function: OnAccepted OnRejected
213  * @tc.type: FUNC
214  */
215 HWTEST_F(SwipeRecognizerAxisBaseTestNg, SwipeRecognizerAxisBaseTest001, TestSize.Level1)
216 {
217     int32_t caseNum = 0;
218     for (const auto& testCase : AXIS_TEST_CASES) {
219         /**
220          * @tc.steps: step1. create SwipeRecognizer.
221          */
222         auto swipeRecognizer = AceType::MakeRefPtr<SwipeRecognizer>(1, testCase.direction, testCase.speed);
223         /**
224          * @tc.steps: step2. DispatchTouchEvent.
225          * @tc.expected: step2. refereeState_ equals.
226          */
227         AxisEvent downEvent;
228         downEvent.x = testCase.info.startPoint.GetX();
229         downEvent.y = testCase.info.startPoint.GetY();
230         downEvent.action = AxisAction::BEGIN;
231         downEvent.sourceTool = testCase.info.sourceTool;
232         downEvent.time = TimeStamp(std::chrono::nanoseconds(1000));
233         downEvent.pressedCodes.emplace_back(KeyCode::KEY_SHIFT_LEFT);
234         std::vector<AxisEvent> moveEvents;
235         AxisEvent upEvent;
236         CreateAxisEvents(testCase.info, testCase.speed, moveEvents, upEvent);
237         swipeRecognizer->HandleEvent(downEvent);
238         for (const auto& item : moveEvents) {
239             swipeRecognizer->HandleEvent(item);
240         }
241         swipeRecognizer->HandleEvent(upEvent);
242         EXPECT_TRUE(IsRecognizerStateEqual(caseNum, swipeRecognizer->refereeState_, testCase.expectResult));
243         caseNum++;
244     }
245 }
246 
247 } // namespace OHOS::Ace::NG