1 /*
2  * Copyright (c) 2022 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 <cstdint>
17 #include <unistd.h>
18 
19 #include "gtest/gtest.h"
20 
21 #define private public
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/event/event_hub.h"
24 #include "core/components_ng/event/long_press_event.h"
25 #include "core/components_ng/pattern/pattern.h"
26 #include "core/components_v2/inspector/inspector_constants.h"
27 #include "test/mock/core/pipeline/mock_pipeline_context.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS::Ace::NG {
33 namespace {
34 constexpr double GESTURE_EVENT_PROPERTY_VALUE = 10.0;
35 const TouchRestrict LONG_PRESS_TOUCH_RESTRICT = { TouchRestrict::CLICK };
36 constexpr uint32_t LONG_PRESS_TEST_RESULT_SIZE = 0;
37 constexpr uint32_t LONG_PRESS_TEST_RESULT_SIZE_1 = 1;
38 constexpr uint32_t LONG_PRESS_TEST_RESULT_SIZE_2 = 2;
39 constexpr float WIDTH = 400.0f;
40 constexpr float HEIGHT = 400.0f;
41 const OffsetF COORDINATE_OFFSET(WIDTH, HEIGHT);
42 } // namespace
43 
44 class LongPressEventTestNg : public testing::Test {
45 public:
46     static void SetUpTestSuite();
47     static void TearDownTestSuite();
48     void SetUp() override;
49     void TearDown() override;
50 };
51 
SetUpTestSuite()52 void LongPressEventTestNg::SetUpTestSuite()
53 {
54     GTEST_LOG_(INFO) << "LongPressEventTestNg SetUpTestCase";
55 }
56 
TearDownTestSuite()57 void LongPressEventTestNg::TearDownTestSuite()
58 {
59     GTEST_LOG_(INFO) << "LongPressEventTestNg TearDownTestCase";
60 }
61 
SetUp()62 void LongPressEventTestNg::SetUp()
63 {
64     MockPipelineContext::SetUp();
65 }
66 
TearDown()67 void LongPressEventTestNg::TearDown()
68 {
69     MockPipelineContext::TearDown();
70 }
71 
72 /**
73  * @tc.name: LongPressEventTest001
74  * @tc.desc: Create LongPressEvent and execute its callback functions.
75  * @tc.type: FUNC
76  */
77 HWTEST_F(LongPressEventTestNg, LongPressEventTest001, TestSize.Level1)
78 {
79     /**
80      * @tc.steps: step1. Create LongPressEvent.
81      * @tc.expected: longPressEvent is not nullptr.
82      */
83     double unknownPropertyValue = 0.0;
__anon3737de070202(GestureEvent& info) 84     GestureEventFunc callback = [&unknownPropertyValue](GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
85     auto longPressEvent = AceType::MakeRefPtr<LongPressEvent>(std::move(callback));
86     EXPECT_NE(longPressEvent, nullptr);
87 
88     /**
89      * @tc.steps: step2. Get and execute LongPressEvent callback function.
90      * @tc.expected: Execute callback where unknownPropertyValue is assigned in.
91      */
92     GestureEvent info = GestureEvent();
93     info.SetScale(GESTURE_EVENT_PROPERTY_VALUE);
94     longPressEvent->GetGestureEventFunc()(info);
95     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
96 }
97 
98 /**
99  * @tc.name: LongPressEventActuatorTest002
100  * @tc.desc: Create LongPressEvent and execute its callback functions.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(LongPressEventTestNg, LongPressEventActuatorTest002, TestSize.Level1)
104 {
105     /**
106      * @tc.steps: step1. Create longPressEventActuator.
107      * @tc.expected: longPressEventActuator is not nullptr.
108      */
109     auto eventHub = AceType::MakeRefPtr<EventHub>();
110     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
111     eventHub->AttachHost(frameNode);
112     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
113     auto longPressEventActuator =
114         AceType::MakeRefPtr<LongPressEventActuator>(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
115     EXPECT_NE(longPressEventActuator, nullptr);
116 
117     /**
118      * @tc.steps: step2. Invoke OnCollectTouchTarget when longPressEvent_ is nullptr.
119      * @tc. OnCollectTouchTarget return directly
120      */
121     TouchTestResult result;
122     ResponseLinkResult responseLinkResult;
123     longPressEventActuator->OnCollectTouchTarget(
124         COORDINATE_OFFSET, LONG_PRESS_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
125     EXPECT_EQ(result.size(), LONG_PRESS_TEST_RESULT_SIZE);
126     EXPECT_EQ(longPressEventActuator->longPressRecognizer_, nullptr);
127     /**
128      * @tc.steps: step2.1. Execute callback when longPressEvent_ and onAccessibilityEventFunc_ are nullptr.
129      */
130     GestureEvent info;
131     longPressEventActuator->GetGestureEventFunc()(info);
132 
133     /**
134      * @tc.steps: step3. Create LongPressEvent.
135      * @tc.expected: longPressEvent is not nullptr.
136      */
__anon3737de070302(GestureEvent& info) 137     GestureEventFunc callback = [](GestureEvent& info) {};
138     auto longPressEvent = AceType::MakeRefPtr<LongPressEvent>(std::move(callback));
139     EXPECT_NE(longPressEvent, nullptr);
140 
141     /**
142      * @tc.steps: step4. Set LongPressEvent with default value.
143      * @tc.expected: Properties were assigned with default value.
144      */
145     longPressEventActuator->SetLongPressEvent(longPressEvent);
146     EXPECT_EQ(longPressEventActuator->isForDrag_, false);
147     EXPECT_EQ(longPressEventActuator->isDisableMouseLeft_, false);
148     EXPECT_NE(longPressEvent, nullptr);
149     /**
150      * @tc.steps: step5. Set LongPressEvent.
151      * @tc.expected: Properties were assigned with correct value.
152      */
153     longPressEventActuator->SetLongPressEvent(longPressEvent, true, true);
154     EXPECT_EQ(longPressEventActuator->isForDrag_, true);
155     EXPECT_EQ(longPressEventActuator->isDisableMouseLeft_, true);
156 
157     /**
158      * @tc.steps: step6. Invoke OnCollectTouchTarget when longPressEvent_ is not nullptr but longPressRecognizer_ is
159      * nullptr.
160      * @tc.expected:  TouchTestResult size has been increased one.
161      */
162     EXPECT_EQ(longPressEventActuator->longPressRecognizer_, nullptr);
163     longPressEventActuator->longPressRecognizer_ = nullptr;
164     longPressEventActuator->OnCollectTouchTarget(
165         COORDINATE_OFFSET, LONG_PRESS_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
166     EXPECT_EQ(longPressEventActuator->longPressRecognizer_->GetCoordinateOffset(), Offset(WIDTH, HEIGHT));
167     EXPECT_EQ(result.size(), LONG_PRESS_TEST_RESULT_SIZE_1);
168 
169     // coverage longPressRecognizer_ is true
170     longPressEventActuator->OnCollectTouchTarget(
171         COORDINATE_OFFSET, LONG_PRESS_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result, responseLinkResult);
172     EXPECT_EQ(longPressEventActuator->longPressRecognizer_->GetCoordinateOffset(), Offset(WIDTH, HEIGHT));
173     EXPECT_EQ(result.size(), LONG_PRESS_TEST_RESULT_SIZE_2);
174 
175     /**
176      * @tc.steps: step6.1. Execute callback when longPressEvent_ and onAccessibilityEventFunc_ are not nullptr.
177      */
__anon3737de070402(AccessibilityEventType type) 178     const OnAccessibilityEventFunc onAccessibility = [](AccessibilityEventType type) {};
179     longPressEventActuator->SetOnAccessibility(onAccessibility);
180     longPressEventActuator->GetGestureEventFunc()(info);
181 }
182 } // namespace OHOS::Ace::NG
183