• 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/long_press_gesture.h"
28 #include "core/components_ng/gestures/recognizers/long_press_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 NativeLongPressGestureTest : 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* NativeLongPressGestureTest::NATIVE_GESTURE_API = nullptr;
52 
53 /**
54  * @tc.name: NativeLongPressGestureTest001
55  * @tc.desc: Test Native Api create LongPress handle fingerNum parameter, fingerNum must in range in [0, 10], when out
56  * of this range the default value is 1.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(NativeLongPressGestureTest, NativeLongPressGestureTest001, 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 },
64         { 6, 6 }, { 7, 7 }, { 8, 8 }, { 9, 9 }, { 10, 10 },
65         {11, 11}  //in native api 11 will retain the original value
66     };
67 
68     constexpr static int32_t DEFAULT_LONG_PRESS_DURATION = 1000;
69 
70     for (auto iter = fingerCountTestCases.begin(); iter != fingerCountTestCases.end(); iter++) {
71         auto inputValue = iter->first;
72         auto expectValue = iter->second;
73 
74         ArkUI_GestureRecognizer* longPressGestureRecognizer = NATIVE_GESTURE_API->createLongPressGesture(
75             inputValue, false, DEFAULT_LONG_PRESS_DURATION);
76         ASSERT_NE(longPressGestureRecognizer, nullptr);
77         ASSERT_EQ(longPressGestureRecognizer->type, ArkUI_GestureRecognizerType::LONG_PRESS_GESTURE);
78 
79         Ace::NG::LongPressGesture* longPressGesture =
80             reinterpret_cast<Ace::NG::LongPressGesture*>(longPressGestureRecognizer->gesture);
81         EXPECT_EQ(longPressGesture->GetFingers(), expectValue);
82 
83         NATIVE_GESTURE_API->dispose(longPressGestureRecognizer);
84     }
85 }
86 
87 /**
88  * @tc.name: NativeLongPressGestureTest002
89  * @tc.desc: Test Native Api create LongPress handle duration parameter, duration must be positive, invalid values will
90  * default to 500ms.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(NativeLongPressGestureTest, NativeLongPressGestureTest002, TestSize.Level1)
94 {
95     std::unordered_map<int32_t, int32_t> durationTestCases = {
96         { -100, -100 }, //in native api -100 will retain the original value
97         { 0, 0 }, // in native api 0 will retain the original value
98         { 500, 500 }, { 1000, 1000 }, { 2000, 2000 }};
99 
100     constexpr static int32_t DEFAULT_FINGER_NUM = 1;
101 
102     for (auto iter = durationTestCases.begin(); iter != durationTestCases.end(); iter++) {
103         auto inputValue = iter->first;
104         auto expectValue = iter->second;
105 
106         ArkUI_GestureRecognizer* longPressGestureRecognizer =
107             NATIVE_GESTURE_API->createLongPressGesture(DEFAULT_FINGER_NUM, false, inputValue);
108         ASSERT_NE(longPressGestureRecognizer, nullptr);
109         ASSERT_EQ(longPressGestureRecognizer->type, ArkUI_GestureRecognizerType::LONG_PRESS_GESTURE);
110 
111         Ace::NG::LongPressGesture* longPressGesture =
112             reinterpret_cast<Ace::NG::LongPressGesture*>(longPressGestureRecognizer->gesture);
113         EXPECT_DOUBLE_EQ(longPressGesture->duration_, expectValue);
114 
115         NATIVE_GESTURE_API->dispose(longPressGestureRecognizer);
116     }
117 }
118 
119 } // namespace OHOS::Ace
120