• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
__anonbae2c4700202(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     longPressEventActuator->OnCollectTouchTarget(
123         COORDINATE_OFFSET, LONG_PRESS_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
124     EXPECT_EQ(result.size(), LONG_PRESS_TEST_RESULT_SIZE);
125     EXPECT_EQ(longPressEventActuator->longPressRecognizer_, nullptr);
126     /**
127      * @tc.steps: step2.1. Execute callback when longPressEvent_ and onAccessibilityEventFunc_ are nullptr.
128      */
129     GestureEvent info;
130     longPressEventActuator->GetGestureEventFunc()(info);
131 
132     /**
133      * @tc.steps: step3. Create LongPressEvent.
134      * @tc.expected: longPressEvent is not nullptr.
135      */
__anonbae2c4700302(GestureEvent& info) 136     GestureEventFunc callback = [](GestureEvent& info) {};
137     auto longPressEvent = AceType::MakeRefPtr<LongPressEvent>(std::move(callback));
138     EXPECT_NE(longPressEvent, nullptr);
139 
140     /**
141      * @tc.steps: step4. Set LongPressEvent with default value.
142      * @tc.expected: Properties were assigned with default value.
143      */
144     longPressEventActuator->SetLongPressEvent(longPressEvent);
145     EXPECT_EQ(longPressEventActuator->isForDrag_, false);
146     EXPECT_EQ(longPressEventActuator->isDisableMouseLeft_, false);
147     EXPECT_NE(longPressEvent, nullptr);
148     /**
149      * @tc.steps: step5. Set LongPressEvent.
150      * @tc.expected: Properties were assigned with correct value.
151      */
152     longPressEventActuator->SetLongPressEvent(longPressEvent, true, true);
153     EXPECT_EQ(longPressEventActuator->isForDrag_, true);
154     EXPECT_EQ(longPressEventActuator->isDisableMouseLeft_, true);
155 
156     /**
157      * @tc.steps: step6. Invoke OnCollectTouchTarget when longPressEvent_ is not nullptr but longPressRecognizer_ is
158      * nullptr.
159      * @tc.expected:  TouchTestResult size has been increased one.
160      */
161     EXPECT_EQ(longPressEventActuator->longPressRecognizer_, nullptr);
162     longPressEventActuator->longPressRecognizer_ = nullptr;
163     longPressEventActuator->OnCollectTouchTarget(
164         COORDINATE_OFFSET, LONG_PRESS_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
165     EXPECT_EQ(longPressEventActuator->longPressRecognizer_->GetCoordinateOffset(), Offset(WIDTH, HEIGHT));
166     EXPECT_EQ(result.size(), LONG_PRESS_TEST_RESULT_SIZE_1);
167 
168     // coverage longPressRecognizer_ is true
169     longPressEventActuator->OnCollectTouchTarget(
170         COORDINATE_OFFSET, LONG_PRESS_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
171     EXPECT_EQ(longPressEventActuator->longPressRecognizer_->GetCoordinateOffset(), Offset(WIDTH, HEIGHT));
172     EXPECT_EQ(result.size(), LONG_PRESS_TEST_RESULT_SIZE_2);
173 
174     /**
175      * @tc.steps: step6.1. Execute callback when longPressEvent_ and onAccessibilityEventFunc_ are not nullptr.
176      */
__anonbae2c4700402(AccessibilityEventType type) 177     const OnAccessibilityEventFunc onAccessibility = [](AccessibilityEventType type) {};
178     longPressEventActuator->SetOnAccessibility(onAccessibility);
179     longPressEventActuator->GetGestureEventFunc()(info);
180 }
181 } // namespace OHOS::Ace::NG
182