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/pinch_gesture.h" 28 #include "core/components_ng/gestures/recognizers/pan_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 NativePinchGestureTest : 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* NativePinchGestureTest::NATIVE_GESTURE_API = nullptr; 52 53 /** 54 * @tc.name: NativePinchGestureTest001 55 * @tc.desc: Test Native Api create Pinch handle fingerNum parameter, fingerNum must in range in [0, 5], when out 56 * of this range the default value is 2. 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(NativePinchGestureTest, NativePinchGestureTest001, 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 }, //in native api 1 will retain the original value 64 { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, 65 { 6, 6 }, //in native api 6 will retain the original value 66 { 7, 7 }, //in native api 7 will retain the original value 67 { 8, 8 }, //in native api 8 will retain the original value 68 { 9, 9 }, //in native api 9 will retain the original value 69 { 10, 10 } //in native api 10 will retain the original value 70 }; 71 72 constexpr static double DEFAULT_PINCH_DISTANCE = 10; 73 74 for (auto iter = fingerCountTestCases.begin(); iter != fingerCountTestCases.end(); iter++) { 75 auto inputValue = iter->first; 76 auto expectValue = iter->second; 77 78 ArkUI_GestureRecognizer* pinchGestureRecognizer = 79 NATIVE_GESTURE_API->createPinchGesture(inputValue, DEFAULT_PINCH_DISTANCE); 80 ASSERT_NE(pinchGestureRecognizer, nullptr); 81 ASSERT_EQ(pinchGestureRecognizer->type, ArkUI_GestureRecognizerType::PINCH_GESTURE); 82 83 Ace::NG::PinchGesture* pinchGesture = reinterpret_cast<Ace::NG::PinchGesture*>(pinchGestureRecognizer->gesture); 84 EXPECT_EQ(pinchGesture->GetFingers(), expectValue); 85 86 NATIVE_GESTURE_API->dispose(pinchGestureRecognizer); 87 } 88 } 89 90 /** 91 * @tc.name: NativePinchGestureTest002 92 * @tc.desc: Test Native Api create Pinch handle distanceNum parameter, If this parameter is set to a value less 93 * than or equal to 0, the default value <b>5</b> is used. 94 * @tc.type: FUNC 95 */ 96 HWTEST_F(NativePinchGestureTest, NativePinchGestureTest002, TestSize.Level1) 97 { 98 //In the native api, different inputs will result in an expected 0. 99 std::unordered_map<double, double> distanceTestCases = { { -10.0f, 0.0f }, { 0.0f, 0.0f }, { 1.0f, 0.0f }, 100 { 5.0f, 0.0f }, { 10.0f, 0.0f }, { 20.0f, 0.0f } }; 101 102 constexpr static int32_t DEFAULT_FINGER_COUNT = 2; 103 104 for (auto iter = distanceTestCases.begin(); iter != distanceTestCases.end(); iter++) { 105 auto inputValue = iter->first; 106 auto expectValue = iter->second; 107 108 ArkUI_GestureRecognizer* pinchGestureRecognizer = 109 NATIVE_GESTURE_API->createPinchGesture(DEFAULT_FINGER_COUNT, inputValue); 110 ASSERT_NE(pinchGestureRecognizer, nullptr); 111 ASSERT_EQ(pinchGestureRecognizer->type, ArkUI_GestureRecognizerType::PINCH_GESTURE); 112 113 Ace::NG::PinchGesture* pinchGesture = reinterpret_cast<Ace::NG::PinchGesture*>(pinchGestureRecognizer->gesture); 114 EXPECT_DOUBLE_EQ(pinchGesture->distance_, expectValue); 115 116 NATIVE_GESTURE_API->dispose(pinchGestureRecognizer); 117 } 118 } 119 120 } // namespace OHOS::Ace 121