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 #define protected public 26 #define private public 27 #include "test/mock/core/pipeline/mock_pipeline_context.h" 28 29 #include "core/components_ng/gestures/recognizers/click_recognizer.h" 30 #include "core/gestures/gesture_info.h" 31 #include "frameworks/core/interfaces/arkoala/arkoala_api.h" 32 33 using namespace testing; 34 using namespace testing::ext; 35 36 namespace OHOS::Ace { 37 38 class NativeTapGestureTest : public testing::Test { 39 public: 40 static ArkUI_NativeGestureAPI_1* NATIVE_GESTURE_API; SetUp()41 void SetUp() override {}; TearDown()42 void TearDown() override {}; 43 SetUpTestSuite()44 static void SetUpTestSuite() 45 { 46 NATIVE_GESTURE_API = reinterpret_cast<ArkUI_NativeGestureAPI_1*>( 47 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1")); 48 ASSERT_NE(NATIVE_GESTURE_API, nullptr); 49 } 50 TearDownTestSuite()51 static void TearDownTestSuite() 52 { 53 NATIVE_GESTURE_API = nullptr; 54 } 55 }; 56 ArkUI_NativeGestureAPI_1* NativeTapGestureTest::NATIVE_GESTURE_API = nullptr; 57 58 /** 59 * @tc.name: NativeTapGestureTest001 60 * @tc.desc: Test Native Api create TapGesture handle numCount parameter, numCount must in range in [1, +∞], when out 61 * of this range the default value is 1. 62 * @tc.type: FUNC 63 */ 64 HWTEST_F(NativeTapGestureTest, NativeTapGestureTest001, TestSize.Level1) 65 { 66 std::unordered_map<int32_t, int32_t> numCountTestCases = { { -100, 1 }, { 0, 1 }, { 5, 5 }, { 10, 10 } }; 67 constexpr int32_t fingerNum = 1; 68 69 for (auto iter = numCountTestCases.begin(); iter != numCountTestCases.end(); iter++) { 70 auto inputValue = iter->first; 71 auto expectValue = iter->second; 72 73 ArkUI_GestureRecognizer* tapGestureRecognizer = NATIVE_GESTURE_API->createTapGesture(inputValue, fingerNum); 74 ASSERT_NE(tapGestureRecognizer, nullptr); 75 ASSERT_EQ(tapGestureRecognizer->type, ArkUI_GestureRecognizerType::TAP_GESTURE); 76 77 Ace::NG::TapGesture* tapGesture = reinterpret_cast<Ace::NG::TapGesture*>(tapGestureRecognizer->gesture); 78 EXPECT_EQ(tapGesture->GetTapCount(), expectValue); 79 80 NATIVE_GESTURE_API->dispose(tapGestureRecognizer); 81 } 82 } 83 84 /** 85 * @tc.name: NativeTapGestureTest002 86 * @tc.desc: Test Native Api create TapGesture handle fingerNum parameter, fingerNum must in range in [1, 10], when out 87 * of this range the default value is 1 or 10. 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(NativeTapGestureTest, NativeTapGestureTest002, TestSize.Level1) 91 { 92 std::unordered_map<int32_t, int32_t> fingerNumTestCases = { { -100, 1 }, { 0, 1 }, { 5, 5 }, { 10, 10 }, 93 { 100, 10 } }; 94 constexpr int32_t numCount = 1; 95 96 for (auto iter = fingerNumTestCases.begin(); iter != fingerNumTestCases.end(); iter++) { 97 auto inputValue = iter->first; 98 auto expectValue = iter->second; 99 100 ArkUI_GestureRecognizer* tapGestureRecognizer = NATIVE_GESTURE_API->createTapGesture(numCount, inputValue); 101 ASSERT_NE(tapGestureRecognizer, nullptr); 102 ASSERT_EQ(tapGestureRecognizer->type, ArkUI_GestureRecognizerType::TAP_GESTURE); 103 104 Ace::NG::TapGesture* tapGesture = reinterpret_cast<Ace::NG::TapGesture*>(tapGestureRecognizer->gesture); 105 EXPECT_EQ(tapGesture->GetFingers(), expectValue); 106 107 NATIVE_GESTURE_API->dispose(tapGestureRecognizer); 108 } 109 } 110 111 /** 112 * @tc.name: NativeTapGestureTest003 113 * @tc.desc: Test Native Api create TapGesture handle numCount parameter, numCount must in range in [1, +∞], when out 114 * of this range the default value is 1. 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(NativeTapGestureTest, NativeTapGestureTest003, TestSize.Level1) 118 { 119 std::unordered_map<int32_t, int32_t> numCountTestCases = { { -100, 1 }, { 0, 1 }, { 5, 5 }, { 10, 10 } }; 120 constexpr int32_t fingerNum = 1; 121 constexpr double distance = 10.0; 122 123 for (auto iter = numCountTestCases.begin(); iter != numCountTestCases.end(); iter++) { 124 auto inputValue = iter->first; 125 auto expectValue = iter->second; 126 127 ArkUI_GestureRecognizer* tapGestureRecognizer = 128 NATIVE_GESTURE_API->createTapGestureWithDistanceThreshold(inputValue, fingerNum, distance); 129 ASSERT_NE(tapGestureRecognizer, nullptr); 130 ASSERT_EQ(tapGestureRecognizer->type, ArkUI_GestureRecognizerType::TAP_GESTURE); 131 132 Ace::NG::TapGesture* tapGesture = reinterpret_cast<Ace::NG::TapGesture*>(tapGestureRecognizer->gesture); 133 EXPECT_EQ(tapGesture->GetTapCount(), expectValue); 134 EXPECT_EQ(tapGesture->GetFingers(), fingerNum); 135 EXPECT_DOUBLE_EQ(tapGesture->distanceThreshold_.ConvertToPx(), 0.0); // not support 136 137 NATIVE_GESTURE_API->dispose(tapGestureRecognizer); 138 } 139 } 140 141 /** 142 * @tc.name: NativeTapGestureTest004 143 * @tc.desc: Test Native Api create TapGesture handle fingerNum parameter, fingerNum must in range in [1, 10], when out 144 * of this range the default value is 1 or 10. 145 * @tc.type: FUNC 146 */ 147 HWTEST_F(NativeTapGestureTest, NativeTapGestureTest004, TestSize.Level1) 148 { 149 std::unordered_map<int32_t, int32_t> fingerNumTestCases = { { -100, 1 }, { 0, 1 }, { 5, 5 }, { 10, 10 }, 150 { 100, 10 } }; 151 constexpr int32_t numCount = 1; 152 constexpr double distance = 10.0; 153 154 for (auto iter = fingerNumTestCases.begin(); iter != fingerNumTestCases.end(); iter++) { 155 auto inputValue = iter->first; 156 auto expectValue = iter->second; 157 158 ArkUI_GestureRecognizer* tapGestureRecognizer = 159 NATIVE_GESTURE_API->createTapGestureWithDistanceThreshold(numCount, inputValue, distance); 160 ASSERT_NE(tapGestureRecognizer, nullptr); 161 ASSERT_EQ(tapGestureRecognizer->type, ArkUI_GestureRecognizerType::TAP_GESTURE); 162 163 Ace::NG::TapGesture* tapGesture = reinterpret_cast<Ace::NG::TapGesture*>(tapGestureRecognizer->gesture); 164 EXPECT_EQ(tapGesture->GetTapCount(), numCount); 165 EXPECT_EQ(tapGesture->GetFingers(), expectValue); 166 EXPECT_DOUBLE_EQ(tapGesture->distanceThreshold_.ConvertToPx(), 0.0); // not support 167 168 NATIVE_GESTURE_API->dispose(tapGestureRecognizer); 169 } 170 } 171 172 /** 173 * @tc.name: NativeTapGestureTest005 174 * @tc.desc: Test Native Api create TapGesture handle distance parameter, invaild value will not transform 175 * to default value. 176 * @tc.type: FUNC 177 */ 178 HWTEST_F(NativeTapGestureTest, NativeTapGestureTest005, TestSize.Level1) 179 { 180 std::vector<std::pair<double, double>> distanceTestCases = { { -100.0, 0.0 }, { 0.0, 0.0 }, { 5.0, 0.0 }, 181 { 10, 0.0 }, { 100.0, 0.0 } }; 182 183 constexpr int32_t numCount = 1; 184 constexpr int32_t fingerNum = 5; 185 186 for (auto iter = distanceTestCases.begin(); iter != distanceTestCases.end(); iter++) { 187 auto inputValue = iter->first; 188 auto expectValue = iter->second; 189 190 ArkUI_GestureRecognizer* tapGestureRecognizer = 191 NATIVE_GESTURE_API->createTapGestureWithDistanceThreshold(numCount, fingerNum, inputValue); 192 ASSERT_NE(tapGestureRecognizer, nullptr); 193 ASSERT_EQ(tapGestureRecognizer->type, ArkUI_GestureRecognizerType::TAP_GESTURE); 194 195 Ace::NG::TapGesture* tapGesture = reinterpret_cast<Ace::NG::TapGesture*>(tapGestureRecognizer->gesture); 196 EXPECT_EQ(tapGesture->GetTapCount(), numCount); 197 EXPECT_EQ(tapGesture->GetFingers(), fingerNum); 198 EXPECT_DOUBLE_EQ(tapGesture->distanceThreshold_.ConvertToPx(), expectValue); // not support 199 200 NATIVE_GESTURE_API->dispose(tapGestureRecognizer); 201 } 202 } 203 204 } // namespace OHOS::Ace