• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <cstddef>
17 #include <cstdint>
18 #include <unistd.h>
19 
20 #include "gtest/gtest.h"
21 
22 #define private public
23 #define protected public
24 #include "core/components_ng/base/frame_node.h"
25 #include "core/components_ng/event/click_event.h"
26 #include "core/components_ng/event/event_hub.h"
27 #include "core/components_ng/pattern/pattern.h"
28 #include "core/components_v2/inspector/inspector_constants.h"
29 #include "test/mock/core/pipeline/mock_pipeline_context.h"
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace OHOS::Ace::NG {
35 namespace {
36 constexpr double GESTURE_EVENT_PROPERTY_VALUE = 10.0;
37 constexpr int32_t CLICK_TEST_RESULT_SIZE = 1;
38 constexpr int32_t CLICK_TEST_RESULT_SIZE_2 = 2;
39 constexpr int32_t CLICK_TEST_RESULT_SIZE_0 = 0;
40 constexpr uint32_t CLICK_EVENTS_SIZE = 1;
41 constexpr uint32_t CLICK_EVENTS_SIZE_2 = 2;
42 const TouchRestrict CLICK_TOUCH_RESTRICT = { TouchRestrict::CLICK };
43 constexpr float WIDTH = 400.0f;
44 constexpr float HEIGHT = 400.0f;
45 const std::string RESULT_SUCCESS = "success";
46 const OffsetF COORDINATE_OFFSET(WIDTH, HEIGHT);
47 } // namespace
48 
49 class ClickEventTestNg : public testing::Test {
50 public:
51     static void SetUpTestSuite();
52     static void TearDownTestSuite();
53     void SetUp() override;
54     void TearDown() override;
55 };
56 
SetUpTestSuite()57 void ClickEventTestNg::SetUpTestSuite()
58 {
59     GTEST_LOG_(INFO) << "ClickEventTestNg SetUpTestCase";
60 }
61 
TearDownTestSuite()62 void ClickEventTestNg::TearDownTestSuite()
63 {
64     GTEST_LOG_(INFO) << "ClickEventTestNg TearDownTestCase";
65 }
66 
SetUp()67 void ClickEventTestNg::SetUp()
68 {
69     MockPipelineContext::SetUp();
70 }
71 
TearDown()72 void ClickEventTestNg::TearDown()
73 {
74     MockPipelineContext::TearDown();
75 }
76 
77 /**
78  * @tc.name: ClickEventTest001
79  * @tc.desc: Create ClickEvent and execute its functions.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(ClickEventTestNg, ClickEventTest001, TestSize.Level1)
83 {
84     /**
85      * @tc.steps: step1. Create GestureEventFunc as the arguments of the construction of ClickEvent.
86      * @tc.expected: clickEvent is not nullptr.
87      */
88     double unknownPropertyValue = 0.0;
__anone3518c3a0202(GestureEvent& info) 89     GestureEventFunc callback = [&unknownPropertyValue](GestureEvent& info) { unknownPropertyValue = info.GetScale(); };
90     auto clickEvent = AceType::MakeRefPtr<ClickEvent>(std::move(callback));
91     EXPECT_NE(clickEvent, nullptr);
92 
93     /**
94      * @tc.steps: step2. Get callback function and execute.
95      * @tc.expected: Execute ActionUpdateEvent which unknownPropertyValue is assigned in.
96      */
97     GestureEvent info = GestureEvent();
98     info.SetScale(GESTURE_EVENT_PROPERTY_VALUE);
99     clickEvent->GetGestureEventFunc()(info);
100     EXPECT_EQ(unknownPropertyValue, GESTURE_EVENT_PROPERTY_VALUE);
101 }
102 
103 /**
104  * @tc.name: ClickEventActuatorTest002
105  * @tc.desc: Create ClickEventActuator and replace, add and remove clickEvent.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(ClickEventTestNg, ClickEventActuatorTest002, TestSize.Level1)
109 {
110     /**
111      * @tc.steps: step1. Create DragEventActuator.
112      */
113     auto eventHub = AceType::MakeRefPtr<EventHub>();
114     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
115     ClickEventActuator clickEventActuator = ClickEventActuator(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
116 
117     /**
118      * @tc.steps: step2. Replace ClickEvent when userCallback_ is not nullptr.
119      * @tc.expected: userCallback_ will be reset and Make an new instance.
120      */
__anone3518c3a0302(GestureEvent& info) 121     GestureEventFunc callback1 = [](GestureEvent& info) {};
122     auto clickEvent = AceType::MakeRefPtr<ClickEvent>(std::move(callback1));
123     clickEventActuator.userCallback_ = clickEvent;
__anone3518c3a0402(GestureEvent& info) 124     GestureEventFunc callback2 = [](GestureEvent& info) {};
125     clickEventActuator.SetUserCallback(std::move(callback2));
126     EXPECT_NE(clickEventActuator.userCallback_, nullptr);
127 
128     /**
129      * @tc.steps: step3. Add click event when clickEvents_ is empty.
130      * @tc.expected: Add click event to the list of clickEvents.
131      */
132     clickEventActuator.AddClickEvent(clickEvent);
133     EXPECT_EQ(clickEventActuator.clickEvents_.size(), CLICK_EVENTS_SIZE);
134 
135     /**
136      * @tc.steps: step4. Add click event when clickEvents_ is not empty.
137      * @tc.expected: Add click event to the list of clickEvents when it is not found in the list.
138      */
__anone3518c3a0502(GestureEvent& info) 139     GestureEventFunc callback3 = [](GestureEvent& info) {};
140     auto clickEvent2 = AceType::MakeRefPtr<ClickEvent>(std::move(callback3));
141     clickEventActuator.AddClickEvent(clickEvent2);
142     EXPECT_EQ(clickEventActuator.clickEvents_.size(), CLICK_EVENTS_SIZE_2);
143 
144     /**
145      * @tc.steps: step5. Remove click event.
146      * @tc.expected: The list of click event size will minus one.
147      */
148     clickEventActuator.RemoveClickEvent(clickEvent2);
149     EXPECT_EQ(clickEventActuator.clickEvents_.size(), CLICK_EVENTS_SIZE);
150 }
151 
152 /**
153  * @tc.name: ClickEventActuatorTest003
154  * @tc.desc: Create ClickEventActuator and invoke OnCollectTouchTarget.
155  * @tc.type: FUNC
156  */
157 HWTEST_F(ClickEventTestNg, ClickEventActuatorTest003, TestSize.Level1)
158 {
159     /**
160      * @tc.steps: step1. Create DragEventActuator.
161      */
162     auto eventHub = AceType::MakeRefPtr<EventHub>();
163     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
164     eventHub->AttachHost(frameNode);
165     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
166     ClickEventActuator clickEventActuator = ClickEventActuator(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
167 
168     /**
169      * @tc.steps: step2. Invoke OnCollectTouchTarget when clickEvents_ is empty and userCallback_ is nullptr.
170      * @tc.expected: OnCollectTouchTarget will return directly and finalResult is empty.
171      */
172     auto getEventTargetImpl = eventHub->CreateGetEventTargetImpl();
173     EXPECT_NE(getEventTargetImpl, nullptr);
174     TouchTestResult finalResult;
175     clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult);
176     EXPECT_TRUE(finalResult.empty());
177 
178     /**
179      * @tc.steps: step3. Invoke OnCollectTouchTarget when clickEvents_ is not empty but userCallback_ is nullptr.
180      * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1.
181      */
__anone3518c3a0602(GestureEvent& info) 182     GestureEventFunc callback = [](GestureEvent& info) {};
183     auto clickEvent = AceType::MakeRefPtr<ClickEvent>(std::move(callback));
184     clickEventActuator.AddClickEvent(clickEvent);
185     clickEventActuator.AddClickEvent(nullptr);
186     clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult);
187     EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE);
188 
189     /**
190      * @tc.steps: step4. Execute onAction_ when clickEvents_ has nullptr and userCallback_ and
191      * onAccessibilityEventFunc_ are nullptr.
192      * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1.
193      */
194     GestureEvent info = GestureEvent();
195     (*clickEventActuator.clickRecognizer_->onAction_)(info);
196 
197     /**
198      * @tc.steps: step5. Invoke OnCollectTouchTarget when userCallback_ and clickEvents_ is not empty but
199      * clickRecognizer_ is nullptr.
200      * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1.
201      */
202     clickEventActuator.clickRecognizer_ = nullptr;
203     clickEventActuator.userCallback_ = clickEvent;
__anone3518c3a0702(AccessibilityEventType type) 204     const OnAccessibilityEventFunc onAccessibility = [](AccessibilityEventType type) {};
205     clickEventActuator.SetOnAccessibility(onAccessibility);
206     clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult);
207     EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE_2);
208 
209     /**
210      * @tc.steps: step6. Execute onAction_ when clickEvents_ has nullptr and userCallback_ and
211      * onAccessibilityEventFunc_ are not nullptr.
212      */
213     (*clickEventActuator.clickRecognizer_->onAction_)(info);
214 }
215 
216 /**
217  * @tc.name: ClickEventActuatorTest004
218  * @tc.desc: test clear user callback.
219  * @tc.type: FUNC
220  */
221 HWTEST_F(ClickEventTestNg, ClickEventActuatorTest004, TestSize.Level1)
222 {
223     /**
224      * @tc.steps: step1. Create EventActuator.
225      */
226     auto eventHub = AceType::MakeRefPtr<EventHub>();
227     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
228     eventHub->AttachHost(frameNode);
229     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
230     ClickEventActuator clickEventActuator = ClickEventActuator(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
231 
232     /**
233      * @tc.steps: step2. Invoke OnCollectTouchTarget when clickEvents_ is empty and userCallback_ is not nullptr.
234      * @tc.expected: OnCollectTouchTarget will return directly and finalResult is 1.
235      */
236     auto getEventTargetImpl = eventHub->CreateGetEventTargetImpl();
237     EXPECT_NE(getEventTargetImpl, nullptr);
238 
239     TouchTestResult finalResult;
240     std::string result;
__anone3518c3a0802(GestureEvent& info) 241     GestureEventFunc callback = [&result](GestureEvent& info) { result = RESULT_SUCCESS; };
242 
243     clickEventActuator.SetUserCallback(std::move(callback));
244     clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult);
245     EXPECT_NE(clickEventActuator.userCallback_->callback_, nullptr);
246 
247     GestureEvent info = GestureEvent();
248     clickEventActuator.userCallback_->callback_(info);
249     EXPECT_EQ(result, RESULT_SUCCESS);
250     EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE);
251 
252     clickEventActuator.ClearUserCallback();
253     clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult);
254     EXPECT_EQ(clickEventActuator.userCallback_, nullptr);
255     EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE);
256 }
257 
258 /**
259  * @tc.name: ClickEventActuatorTest005
260  * @tc.desc: test user callback and clickevent are different.
261  * @tc.type: FUNC
262  */
263 HWTEST_F(ClickEventTestNg, ClickEventActuatorTest005, TestSize.Level1)
264 {
265     /**
266      * @tc.steps: step1. Create EventActuator.
267      */
268     auto eventHub = AceType::MakeRefPtr<EventHub>();
269     auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
270     eventHub->AttachHost(frameNode);
271     auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
272     ClickEventActuator clickEventActuator = ClickEventActuator(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
273 
274     /**
275      * @tc.steps: step2. test clear callback and add event.
276      * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1.
277      */
278     auto getEventTargetImpl = eventHub->CreateGetEventTargetImpl();
279     EXPECT_NE(getEventTargetImpl, nullptr);
280     clickEventActuator.ClearUserCallback();
281 
__anone3518c3a0902(GestureEvent& info) 282     GestureEventFunc callback = [](GestureEvent& info) {};
283     auto clickEvent = AceType::MakeRefPtr<ClickEvent>(std::move(callback));
284     clickEventActuator.AddClickEvent(clickEvent);
285 
286     TouchTestResult finalResult;
287     clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult);
288     EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE);
289 
290     /**
291      * @tc.steps: step3. test clear callback again.
292      * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1.
293      */
294     TouchTestResult finalResultAfterClear;
295     clickEventActuator.ClearUserCallback();
296     clickEventActuator.OnCollectTouchTarget(
297         COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResultAfterClear);
298     EXPECT_EQ(finalResultAfterClear.size(), CLICK_TEST_RESULT_SIZE);
299 
300     /**
301      * @tc.steps: step4. test clear event again.
302      * @tc.expected: callback and event are null, and it's size is equal 0.
303      */
304     clickEventActuator.RemoveClickEvent(clickEvent);
305     TouchTestResult finalResultAfterClearEvent;
306     clickEventActuator.OnCollectTouchTarget(
307         COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResultAfterClearEvent);
308     EXPECT_EQ(finalResultAfterClearEvent.size(), CLICK_TEST_RESULT_SIZE_0);
309 }
310 } // namespace OHOS::Ace::NG
311