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/swipe_gesture.h" 28 #include "core/components_ng/gestures/recognizers/swipe_recognizer.h" 29 #include "core/gestures/gesture_info.h" 30 #include "frameworks/core/interfaces/arkoala/arkoala_api.h" 31 32 using namespace testing; 33 using namespace testing::ext; 34 35 namespace OHOS::Ace { 36 37 class NativeSwiperGestureTest : public testing::Test { 38 public: 39 static ArkUI_NativeGestureAPI_1* NATIVE_GESTURE_API; SetUpTestCase()40 static void SetUpTestCase() 41 { 42 NATIVE_GESTURE_API = reinterpret_cast<ArkUI_NativeGestureAPI_1*>( 43 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 44 ASSERT_NE(NATIVE_GESTURE_API, nullptr); 45 }; TearDownTestCase()46 static void TearDownTestCase() 47 { 48 NATIVE_GESTURE_API = nullptr; 49 }; 50 }; 51 ArkUI_NativeGestureAPI_1* NativeSwiperGestureTest::NATIVE_GESTURE_API = nullptr; 52 53 /** 54 * @tc.name: NativeSwiperGestureTest001 55 * @tc.desc: Test Native Api create SwiperGesture handle fingerNum parameter, 56 * fingerNum must in range in [1, 10], when out of this range the default value is 2. 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(NativeSwiperGestureTest, NativeSwiperGestureTest001, TestSize.Level1) 60 { 61 std::unordered_map<int32_t, int32_t> fingerCountTestCases = { 62 { -1, -1 }, // in native api -1 will retain the original value 63 { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, 64 { 7, 7 }, { 8, 8 }, { 9, 9 }, { 10, 10 } }; 65 66 constexpr static ArkUI_GestureDirectionMask DEFAULT_DIRECTION_MASK = GESTURE_DIRECTION_LEFT; 67 constexpr static double DEFAULT_SPEED_NUM = 10.0; 68 69 for (auto iter = fingerCountTestCases.begin(); iter != fingerCountTestCases.end(); iter++) { 70 auto inputValue = iter->first; 71 auto expectValue = iter->second; 72 73 ArkUI_GestureRecognizer* swiperGestureRecognizer = nullptr; 74 if (NATIVE_GESTURE_API && NATIVE_GESTURE_API->createSwipeGesture) { 75 swiperGestureRecognizer = NATIVE_GESTURE_API->createSwipeGesture(inputValue, 76 DEFAULT_DIRECTION_MASK, DEFAULT_SPEED_NUM); 77 } 78 ASSERT_NE(swiperGestureRecognizer, nullptr); 79 ASSERT_EQ(swiperGestureRecognizer->type, ArkUI_GestureRecognizerType::SWIPE_GESTURE); 80 81 Ace::NG::SwipeGesture* swipeGesture = reinterpret_cast<Ace::NG::SwipeGesture*> 82 (swiperGestureRecognizer->gesture); 83 EXPECT_EQ(swipeGesture->GetFingers(), expectValue); 84 85 NATIVE_GESTURE_API->dispose(swiperGestureRecognizer); 86 } 87 } 88 89 /** 90 * @tc.name: NativeSwiperGestureTest002 91 * @tc.desc: Test Native API createSwipeGesture with various ArkUI_GestureDirectionMask values. 92 * @tc.type: FUNC 93 */ 94 HWTEST_F(NativeSwiperGestureTest, NativeSwiperGestureTest002, TestSize.Level1) 95 { 96 std::unordered_map<ArkUI_GestureDirectionMask, uint32_t> directionTestCases = { 97 { GESTURE_DIRECTION_LEFT, 1 }, { GESTURE_DIRECTION_RIGHT, 1 }, 98 { GESTURE_DIRECTION_UP, 2 }, { GESTURE_DIRECTION_DOWN, 2 } 99 }; 100 101 constexpr static int DEFAULT_FINGER_COUNT = 1; 102 constexpr static double DEFAULT_SPEED_NUM = 10.0; 103 104 for (auto iter = directionTestCases.begin(); iter != directionTestCases.end(); iter++) { 105 auto inputDirection = iter->first; 106 auto expectedDirection = iter->second; 107 108 ArkUI_GestureRecognizer* swiperGestureRecognizer = nullptr; 109 if (NATIVE_GESTURE_API && NATIVE_GESTURE_API->createSwipeGesture) { 110 swiperGestureRecognizer = NATIVE_GESTURE_API->createSwipeGesture(DEFAULT_FINGER_COUNT, 111 inputDirection, DEFAULT_SPEED_NUM); 112 } 113 ASSERT_NE(swiperGestureRecognizer, nullptr); 114 ASSERT_EQ(swiperGestureRecognizer->type, ArkUI_GestureRecognizerType::SWIPE_GESTURE); 115 116 Ace::NG::SwipeGesture* swipeGesture = reinterpret_cast<Ace::NG::SwipeGesture*> 117 (swiperGestureRecognizer->gesture); 118 EXPECT_EQ(swipeGesture->direction_.type, expectedDirection); 119 120 NATIVE_GESTURE_API->dispose(swiperGestureRecognizer); 121 } 122 } 123 124 /** 125 * @tc.name: NativeSwiperGestureTest003 126 * @tc.desc: Test Native API createSwipeGesture with various speed values. speed must in range in [1, +∞], 127 * when out of this range the default value is 100. 128 * @tc.type: FUNC 129 */ 130 HWTEST_F(NativeSwiperGestureTest, NativeSwiperGestureTest003, TestSize.Level1) 131 { 132 //In the native api, different inputs will result in an expected 0. 133 std::unordered_map<double, double> speedTestCases = { { -10.0f, 0.0f }, { 0.0f, 0.0f }, 134 { 1.0f, 0.0f }, { 50.0f, 0.0f }, { 100.0f, 0.0f }, { 200.0f, 0.0f } }; 135 136 constexpr static int DEFAULT_FINGER_COUNT = 1; 137 constexpr static ArkUI_GestureDirectionMask DEFAULT_DIRECTION_MASK = GESTURE_DIRECTION_LEFT; 138 139 for (auto iter = speedTestCases.begin(); iter != speedTestCases.end(); iter++) { 140 auto inputSpeed = iter->first; 141 auto expectedSpeed = iter->second; 142 143 ArkUI_GestureRecognizer* swiperGestureRecognizer = nullptr; 144 if (NATIVE_GESTURE_API && NATIVE_GESTURE_API->createSwipeGesture) { 145 swiperGestureRecognizer = NATIVE_GESTURE_API->createSwipeGesture(DEFAULT_FINGER_COUNT, 146 DEFAULT_DIRECTION_MASK, inputSpeed); 147 } 148 ASSERT_NE(swiperGestureRecognizer, nullptr); 149 ASSERT_EQ(swiperGestureRecognizer->type, ArkUI_GestureRecognizerType::SWIPE_GESTURE); 150 151 Ace::NG::SwipeGesture* swipeGesture = reinterpret_cast<Ace::NG::SwipeGesture*> 152 (swiperGestureRecognizer->gesture); 153 EXPECT_EQ(swipeGesture->speed_, expectedSpeed); 154 155 NATIVE_GESTURE_API->dispose(swiperGestureRecognizer); 156 } 157 } 158 159 } // namespace OHOS::Ace