• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <unordered_map>
17 #include <vector>
18 
19 #include "gtest/gtest.h"
20 #include "interfaces/native/native_gesture.h"
21 #include "interfaces/native/native_interface.h"
22 #include "interfaces/native/native_node.h"
23 #include "interfaces/native/native_type.h"
24 #include "interfaces/native/node/gesture_impl.h"
25 
26 #define private public
27 #include "core/components_ng/gestures/recognizers/pan_recognizer.h"
28 #include "core/gestures/gesture_info.h"
29 #include "frameworks/core/interfaces/arkoala/arkoala_api.h"
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace OHOS::Ace {
35 
36 class NativePanGestureTest : public testing::Test {
37 public:
38     static ArkUI_NativeGestureAPI_1* NATIVE_GESTURE_API;
SetUpTestCase()39     static void SetUpTestCase()
40     {
41         NATIVE_GESTURE_API = reinterpret_cast<ArkUI_NativeGestureAPI_1*>(
42             OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1"));
43         ASSERT_NE(NATIVE_GESTURE_API, nullptr);
44     };
TearDownTestCase()45     static void TearDownTestCase()
46     {
47         NATIVE_GESTURE_API = nullptr;
48     };
49 };
50 ArkUI_NativeGestureAPI_1* NativePanGestureTest::NATIVE_GESTURE_API = nullptr;
51 
52 /**
53  * @tc.name: NativePanGestureTest001
54  * @tc.desc: Test Native Api create PanGesture handle fingerNum parameter, fingerNum must in range in [0, 10], when out
55  * of this range the default value is 1.
56  * @tc.type: FUNC
57  */
58 HWTEST_F(NativePanGestureTest, NativePanGestureTest001, TestSize.Level1)
59 {
60     std::unordered_map<int32_t, int32_t> fingerCountTestCases = { { -1, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 },
61         { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 1 }, { 100, 1 } };
62 
63     constexpr static ArkUI_GestureDirection DEFAULT_PAN_DIRECTION = ArkUI_GestureDirection::GESTURE_DIRECTION_NONE;
64     constexpr static double DEFAULT_PAN_DISTANCE = 10;
65 
66     for (auto iter = fingerCountTestCases.begin(); iter != fingerCountTestCases.end(); iter++) {
67         auto inputValue = iter->first;
68         auto expectValue = iter->second;
69 
70         ArkUI_GestureRecognizer* panGestureRecognizer =
71             NATIVE_GESTURE_API->createPanGesture(inputValue, DEFAULT_PAN_DIRECTION, DEFAULT_PAN_DISTANCE);
72         ASSERT_NE(panGestureRecognizer, nullptr);
73         ASSERT_EQ(panGestureRecognizer->type, ArkUI_GestureRecognizerType::PAN_GESTURE);
74 
75         Ace::NG::PanGesture* panGesture = reinterpret_cast<Ace::NG::PanGesture*>(panGestureRecognizer->gesture);
76         EXPECT_EQ(panGesture->GetFingers(), expectValue);
77 
78         NATIVE_GESTURE_API->dispose(panGestureRecognizer);
79     }
80 }
81 
82 /**
83  * @tc.name: NativePanGestureTest002
84  * @tc.desc: Test Native Api create PanGesture handle direction parameter, direction must in range in [0, 15], when out
85  * of this range the default value is 0. in native api [ 5, 6, 7, 8, 9, 10, 11, 13, 14 ] is also invalid value.
86  * @tc.type: FUNC
87  */
88 HWTEST_F(NativePanGestureTest, NativePanGestureTest002, TestSize.Level1)
89 {
90     std::unordered_map<int32_t, int32_t> directionTestCases = {
91         { -1, 0 }, // invalid value will regard as default value GESTURE_DIRECTION_NONE(0)
92         { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 },
93         { 5, 0 },              // in native api 5 is invalid value, different with js api
94         { 6, 0 },              // in native api 6 is invalid value, different with js api
95         { 7, 0 },              // in native api 7 is invalid value, different with js api
96         { 8, 8 }, { 9, 0 },    // in native api 9 is invalid value, different with js api
97         { 10, 0 },             // in native api 10 is invalid value, different with js api
98         { 11, 0 },             // in native api 11 is invalid value, different with js api
99         { 12, 12 }, { 13, 0 }, // in native api 13 is invalid value, different with js api
100         { 14, 0 },             // in native api 14 is invalid value, different with js api
101         { 15, 15 }, { 16, 0 }, { 30, 0 }, { 100, 0 }
102     };
103 
104     constexpr static int32_t DEFAULT_FINGER_NUM = 1;
105     constexpr static double DEFAULT_PAN_DISTANCE = 10.0;
106 
107     for (auto iter = directionTestCases.begin(); iter != directionTestCases.end(); iter++) {
108         auto inputValue = iter->first;
109         auto expectValue = iter->second;
110 
111         ArkUI_GestureRecognizer* panGestureRecognizer =
112             NATIVE_GESTURE_API->createPanGesture(DEFAULT_FINGER_NUM, inputValue, DEFAULT_PAN_DISTANCE);
113         ASSERT_NE(panGestureRecognizer, nullptr);
114         ASSERT_EQ(panGestureRecognizer->type, ArkUI_GestureRecognizerType::PAN_GESTURE);
115 
116         Ace::NG::PanGesture* panGesture = reinterpret_cast<Ace::NG::PanGesture*>(panGestureRecognizer->gesture);
117         EXPECT_EQ(panGesture->direction_.type, expectValue);
118 
119         NATIVE_GESTURE_API->dispose(panGestureRecognizer);
120     }
121 }
122 
123 /**
124  * @tc.name: NativePanGestureTest003
125  * @tc.desc: Test Native Api create PanGesture handle distance parameter, in native api invaild value will not transform
126  * to default value.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(NativePanGestureTest, NativePanGestureTest003, TestSize.Level1)
130 {
131     std::vector<std::pair<double, double>> distanceTestCases = { { -100.0, -100.0 }, { -1.0, -1.0 }, { 0.0, 0.0 },
132         { 5.0, 5.0 }, { 100.0, 100.0 }, { 10000000.0, 10000000.0 } };
133 
134     constexpr static int32_t DEFAULT_FINGER_NUM = 1;
135     constexpr static ArkUI_GestureDirection DEFAULT_PAN_DIRECTION = ArkUI_GestureDirection::GESTURE_DIRECTION_NONE;
136 
137     for (auto iter = distanceTestCases.begin(); iter != distanceTestCases.end(); iter++) {
138         auto inputValue = iter->first;
139         auto expectValue = iter->second;
140 
141         ArkUI_GestureRecognizer* panGestureRecognizer =
142             NATIVE_GESTURE_API->createPanGesture(DEFAULT_FINGER_NUM, DEFAULT_PAN_DIRECTION, inputValue);
143         ASSERT_NE(panGestureRecognizer, nullptr);
144         ASSERT_EQ(panGestureRecognizer->type, ArkUI_GestureRecognizerType::PAN_GESTURE);
145         Ace::NG::PanGesture* panGesture = reinterpret_cast<Ace::NG::PanGesture*>(panGestureRecognizer->gesture);
146         EXPECT_DOUBLE_EQ(panGesture->distance_, expectValue);
147 
148         NATIVE_GESTURE_API->dispose(panGestureRecognizer);
149     }
150 }
151 
152 } // namespace OHOS::Ace
153