• 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 <clocale>
17 #include <cmath>
18 #include <cstdint>
19 #include <unistd.h>
20 
21 #include "gtest/gtest.h"
22 #define private public
23 #define protected public
24 #include "core/components_ng/event/event_hub.h"
25 #include "core/components_ng/event/touch_event.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 bool STOP_PROPAGATION_VALUE = true;
35 constexpr int32_t TOUCH_TEST_RESULT_SIZE_1 = 1;
36 constexpr uint32_t TOUCH_EVENTS_SIZE = 1;
37 constexpr uint32_t TOUCH_EVENTS_SIZE_2 = 2;
38 const TouchRestrict Touch_TOUCH_RESTRICT = { TouchRestrict::LONG_PRESS };
39 constexpr float TILT_X_VALUE = 400.0f;
40 constexpr float TILT_Y_VALUE = 400.0f;
41 constexpr float WIDTH = 400.0f;
42 constexpr float HEIGHT = 400.0f;
43 const OffsetF COORDINATE_OFFSET(WIDTH, HEIGHT);
44 const std::string TOUCH_EVENT_INFO_TYPE = "onTouchDown";
45 const std::vector<TouchPoint> POINTERS = { TouchPoint(), TouchPoint(), TouchPoint() };
46 const std::vector<TouchPoint> POINTERS_2 = { { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE } };
47 } // namespace
48 
49 class TouchEventTestNg : 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 TouchEventTestNg::SetUpTestSuite()
58 {
59     GTEST_LOG_(INFO) << "TouchEventTestNg SetUpTestCase";
60 }
61 
TearDownTestSuite()62 void TouchEventTestNg::TearDownTestSuite()
63 {
64     GTEST_LOG_(INFO) << "TouchEventTestNg TearDownTestCase";
65 }
66 
SetUp()67 void TouchEventTestNg::SetUp()
68 {
69     MockPipelineContext::SetUp();
70 }
71 
TearDown()72 void TouchEventTestNg::TearDown()
73 {
74     MockPipelineContext::TearDown();
75 }
76 
77 /**
78  * @tc.name: TouchEventCreateTest001
79  * @tc.desc: Create TouchEvent and execute its callback functions.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(TouchEventTestNg, TouchEventCreateTest001, TestSize.Level1)
83 {
84     /**
85      * @tc.steps: step1. Create TouchEvent.
86      */
87     std::string unknownType;
__anon40f282370202(TouchEventInfo& info) 88     TouchEventFunc callback = [&unknownType](TouchEventInfo& info) { unknownType = info.GetType(); };
89     const TouchEventImpl touchEvent = TouchEventImpl(std::move(callback));
90 
91     /**
92      * @tc.steps: step2. Get and execute TouchEvent callback function.
93      * @tc.expected: Execute callback where unknownPropertyValue is assigned in.
94      */
95     TouchEventInfo info(TOUCH_EVENT_INFO_TYPE);
96     touchEvent(info);
97     EXPECT_EQ(unknownType, TOUCH_EVENT_INFO_TYPE);
98 }
99 
100 /**
101  * @tc.name: TouchEventActuatorTest002
102  * @tc.desc: Create TouchEventActuator and replace, add and remove touchEvent.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(TouchEventTestNg, TouchEventActuatorTest002, TestSize.Level1)
106 {
107     /**
108      * @tc.steps: step1. Create TouchEventActuator.
109      * @tc.expected: touchEventActuator is not nullptr.
110      */
111     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
112     EXPECT_NE(touchEventActuator, nullptr);
113 
114     /**
115      * @tc.steps: step2. Replace TouchEventFunc when userCallback_ is nullptr.
116      * @tc.expected: userCallback_ will be reset and Make an new instance.
117      */
__anon40f282370302(TouchEventInfo& info) 118     const TouchEventFunc callback = [](TouchEventInfo& info) {};
119     TouchEventFunc callback1 = callback;
120     touchEventActuator->ReplaceTouchEvent(std::move(callback1));
121     EXPECT_NE(touchEventActuator->userCallback_, nullptr);
122 
123     /**
124      * @tc.steps: step3. Add touch event when touchEvents_ is empty.
125      * @tc.expected: Add touch event to the list of touchEvents_.
126      */
127     TouchEventFunc callback2 = callback;
128     auto touchEvent2 = AceType::MakeRefPtr<TouchEventImpl>(std::move(callback2));
129     touchEventActuator->AddTouchEvent(touchEvent2);
130     EXPECT_EQ(touchEventActuator->touchEvents_.size(), TOUCH_EVENTS_SIZE);
131 
132     /**
133      * @tc.steps: step4. Add touch event when touchEvents_ is not empty.
134      * @tc.expected: Add touch event to the list of touchEvents_ where it is not found in.
135      */
136     TouchEventFunc callback3 = callback;
137     auto touchEvent3 = AceType::MakeRefPtr<TouchEventImpl>(std::move(callback3));
138     touchEventActuator->AddTouchEvent(touchEvent3);
139     EXPECT_EQ(touchEventActuator->touchEvents_.size(), TOUCH_EVENTS_SIZE_2);
140 
141     /**
142      * @tc.steps: step5. Remove touch event.
143      * @tc.expected: The list of touch event size will minus one.
144      */
145     touchEventActuator->RemoveTouchEvent(touchEvent3);
146     EXPECT_EQ(touchEventActuator->touchEvents_.size(), TOUCH_EVENTS_SIZE);
147 }
148 
149 /**
150  * @tc.name: TouchEventActuatorOnCollectTouchTargetTest003
151  * @tc.desc: Create TouchEventActuator and Invoke OnCollectTouchTarget event.
152  * @tc.type: FUNC
153  */
154 HWTEST_F(TouchEventTestNg, TouchEventActuatorOnCollectTouchTargetTest003, TestSize.Level1)
155 {
156     /**
157      * @tc.steps: step1. Create TouchEventActuator.
158      * @tc.expected: touchEventActuator is not nullptr.
159      */
160     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
161     EXPECT_NE(touchEventActuator, nullptr);
162 
163     /**
164      * @tc.steps: step2. Invoke OnCollectTouchTarget.
165      * @tc.expected: TouchTestResult size has been increased one.
166      */
167     TouchTestResult result;
168     auto eventHub = AceType::MakeRefPtr<EventHub>();
169     touchEventActuator->OnCollectTouchTarget(
170         COORDINATE_OFFSET, Touch_TOUCH_RESTRICT, eventHub->CreateGetEventTargetImpl(), result);
171 
172     EXPECT_EQ(touchEventActuator->coordinateOffset_, Offset(WIDTH, HEIGHT));
173     EXPECT_EQ(result.size(), TOUCH_TEST_RESULT_SIZE_1);
174 }
175 
176 /**
177  * @tc.name: TouchEventActuatorHandleAndDispatchTest004
178  * @tc.desc: Create TouchEventActuator and replace, add and remove touchEvent.
179  * @tc.type: FUNC
180  */
181 HWTEST_F(TouchEventTestNg, TouchEventActuatorHandleAndDispatchTest004, TestSize.Level1)
182 {
183     /**
184      * @tc.steps: step1. Create TouchEventActuator.
185      * @tc.expected: touchEventActuator is not nullptr.
186      */
187     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
188     EXPECT_NE(touchEventActuator, nullptr);
189 
190     /**
191      * @tc.steps: step2. Invoke DispatchEvent.
192      * @tc.expected: TouchTestResult size has been increased one.
193      */
194     const TouchEvent touchEvent { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE, .pointers = POINTERS };
195     EXPECT_TRUE(touchEventActuator->DispatchEvent(touchEvent));
196 
197     /**
198      * @tc.steps: step3. Invoke HandleEvent when touchEvents_ and userCallback_ is empty.
199      * @tc.expected: HandleEvent will return true directly.
200      */
201     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent));
202 
203     /**
204      * @tc.steps: step4. Invoke ReplaceTouchEvent to initialize userCallback_.
205      */
206     double unknownTiltX = 0.0;
207     const TouchEventFunc callback = [&unknownTiltX](
__anon40f282370402( TouchEventInfo& info) 208                                         TouchEventInfo& info) { unknownTiltX = info.GetTiltX().value_or(0.0); };
209     TouchEventFunc callback1 = callback;
210     touchEventActuator->ReplaceTouchEvent(std::move(callback1));
211     EXPECT_NE(touchEventActuator->userCallback_, nullptr);
212 
213     /**
214      * @tc.steps: step5. Invoke AddTouchEvent to add touch event to touchEvents_.
215      */
216     TouchEventFunc callback2 = callback;
217     auto touchEvent2 = AceType::MakeRefPtr<TouchEventImpl>(std::move(callback2));
218     touchEventActuator->AddTouchEvent(touchEvent2);
219     touchEventActuator->AddTouchEvent(nullptr);
220     EXPECT_EQ(touchEventActuator->touchEvents_.size(), TOUCH_EVENTS_SIZE_2);
221 
222     /**
223      * @tc.steps: step6. Invoke HandleEvent when touchEvents_ and userCallback_ is not empty.
224      * @tc.expected: HandleEvent will execute touchEvents_ and userCallback_ event where unknownTiltX was assigned the
225      * touchPoint value, and the function return true.
226      */
227     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent));
228     EXPECT_NE(unknownTiltX, 0);
229 
230     /**
231      * @tc.steps: step7. Invoke HandleEvent when touchEvents_ and userCallback_ is not empty but the event is
232      * stopPropagation_ is true.
233      * @tc.expected: HandleEvent return false;
234      */
__anon40f282370502(TouchEventInfo& info) 235     TouchEventFunc callback3 = [](TouchEventInfo& info) { info.SetStopPropagation(STOP_PROPAGATION_VALUE); };
236     touchEventActuator->ReplaceTouchEvent(std::move(callback3));
237     EXPECT_FALSE(touchEventActuator->HandleEvent(touchEvent));
238 
239     /**
240      * @tc.steps: step8. Invoke HandleEvent when touchEvents_ has nullptr event and userCallback_ is nullptr.
241      * @tc.expected: HandleEvent return true;
242      */
243     touchEventActuator->userCallback_ = nullptr;
244     const TouchEvent touchEvent3 { .pointers = POINTERS_2 };
245     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent3));
246 
247     /**
248      * @tc.steps: step9. add history.
249      * @tc.expected: HandleEvent return TRUE;
250      */
251     TouchEvent touchEvent4 = touchEvent;
252     touchEvent4.history.push_back(touchEvent);
253     touchEvent4.history.push_back(touchEvent);
254     touchEvent4.history.push_back(touchEvent);
255     touchEvent4.isInterpolated = true;
256     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent4));
257 }
258 
259 /**
260  * @tc.name: TouchEventDisable001
261  * @tc.desc: Create TouchEventActuator and test disable.
262  * @tc.type: FUNC
263  */
264 HWTEST_F(TouchEventTestNg, TouchEventDisable001, TestSize.Level1)
265 {
266     /**
267      * @tc.steps: step1. Create TouchEventActuator.
268      * @tc.expected: touchEventActuator is not nullptr.
269      */
270     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
271     EXPECT_NE(touchEventActuator, nullptr);
272 
273     /**
274      * @tc.steps: step2. Invoke DispatchEvent.
275      * @tc.expected: TouchTestResult size has been increased one.
276      */
277     const TouchEvent touchEvent { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE, .pointers = POINTERS };
278     EXPECT_TRUE(touchEventActuator->DispatchEvent(touchEvent));
279 
280     /**
281      * @tc.steps: step3. Invoke HandleEvent when touchEvents_ and userCallback_ is empty.
282      * @tc.expected: HandleEvent will return true directly.
283      */
284     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent));
285 
286     /**
287      * @tc.steps: step4. Invoke ReplaceTouchEvent to initialize userCallback_.
288      */
289     double unknownTiltX = 0.0;
290     const TouchEventFunc callback = [&unknownTiltX](
__anon40f282370602( TouchEventInfo& info) 291                                         TouchEventInfo& info) { unknownTiltX = info.GetTiltX().value_or(0.0); };
292     TouchEventFunc callback1 = callback;
293     touchEventActuator->ReplaceTouchEvent(std::move(callback1));
294     EXPECT_NE(touchEventActuator->userCallback_, nullptr);
295 
296     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent));
297     EXPECT_NE(unknownTiltX, 0);
298 
299     /**
300      * @tc.steps: step5. Invoke Clear func to clear userCallback_.
301      */
302     touchEventActuator->ClearUserCallback();
303     EXPECT_EQ(touchEventActuator->userCallback_, nullptr);
304     EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent));
305 }
306 
307 /**
308  * @tc.name: OnFlushTouchEventsBegin001
309  * @tc.desc: test functions OnFlushTouchEventsBegin.
310  * @tc.type: FUNC
311  */
312 HWTEST_F(TouchEventTestNg, OnFlushTouchEventsBegin001, TestSize.Level1)
313 {
314     /**
315      * @tc.steps: step1. Create TouchEventActuator.
316      */
317     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
318 
319     /**
320      * @tc.steps: step2. call OnFlushTouchEventsBegin.
321      * @tc.expected: Expected isFlushTouchEventsEnd_ The value of is false.
322      */
323     touchEventActuator->OnFlushTouchEventsBegin();
324     EXPECT_FALSE(touchEventActuator->isFlushTouchEventsEnd_);
325 }
326 
327 /**
328  * @tc.name: OnFlushTouchEventsEnd001
329  * @tc.desc: test functions OnFlushTouchEventsEnd.
330  * @tc.type: FUNC
331  */
332 HWTEST_F(TouchEventTestNg, OnFlushTouchEventsEnd001, TestSize.Level1)
333 {
334     /**
335      * @tc.steps: step1. Create TouchEventActuator.
336      */
337     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
338 
339     /**
340      * @tc.steps: step2. call OnFlushTouchEventsEnd.
341      * @tc.expected: Expected isFlushTouchEventsEnd_ The value of is true.
342      */
343     touchEventActuator->OnFlushTouchEventsEnd();
344     EXPECT_TRUE(touchEventActuator->isFlushTouchEventsEnd_);
345 }
346 
347 /**
348  * @tc.name: ShouldResponse001
349  * @tc.desc: test functions ShouldResponse.
350  * @tc.type: FUNC
351  */
352 HWTEST_F(TouchEventTestNg, ShouldResponse001, TestSize.Level1)
353 {
354     /**
355      * @tc.steps: step1. Create TouchEventActuator.
356      */
357     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
358 
359     /**
360      * @tc.steps: step2. call ShouldResponse.
361      * @tc.expected: Execute function return value is false.
362      */
363     EXPECT_TRUE(touchEventActuator->ShouldResponse());
364 }
365 
366 /**
367  * @tc.name: TriggerTouchCallBack001
368  * @tc.desc: test functions TriggerTouchCallBack.
369  * @tc.type: FUNC
370  */
371 HWTEST_F(TouchEventTestNg, TriggerTouchCallBack001, TestSize.Level1)
372 {
373     /**
374      * @tc.steps: step1. Create TouchEventActuator..
375      */
376     auto touchEventActuator = AceType::MakeRefPtr<TouchEventActuator>();
377 
378     /**
379      * @tc.steps: step2. create TouchEvent object and call TriggerTouchCallBack.
380      * @tc.expected: Execute function return value is true.
381      */
382     TouchEvent touchEvent { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE, .pointers = POINTERS };
383     EXPECT_TRUE(touchEventActuator->TriggerTouchCallBack(touchEvent));
384 
385     /**
386      * @tc.steps: step3. create TouchEventFunc object and Convert to Right Reference.
387      */
388     std::string unknownType;
__anon40f282370702(TouchEventInfo& info) 389     TouchEventFunc callback = [&unknownType](TouchEventInfo& info) { unknownType = info.GetType(); };
390     TouchEventFunc&& callback_1 = std::forward<TouchEventFunc>(callback);
391 
392     /**
393      * @tc.steps: step4. onTouchEventCallback_ assignment and call TriggerTouchCallBack.
394      * @tc.expected: Execute function return value is true.
395      */
396     touchEventActuator->onTouchEventCallback_ = AceType::MakeRefPtr<TouchEventImpl>(std::move(callback_1));
397     EXPECT_TRUE(touchEventActuator->TriggerTouchCallBack(touchEvent));
398 
399     /**
400      * @tc.steps: step5. userCallback_ assignment and call TriggerTouchCallBack.
401      * @tc.expected: Execute function return value is true.
402      */
403     touchEventActuator->userCallback_ = AceType::MakeRefPtr<TouchEventImpl>(std::move(callback_1));
404     EXPECT_TRUE(touchEventActuator->TriggerTouchCallBack(touchEvent));
405 
406     /**
407      * @tc.steps: step6. isInterpolated assignment and call TriggerTouchCallBack.
408      * @tc.expected: Execute function return value is true.
409      */
410     auto touchEventImp = AceType::MakeRefPtr<TouchEventImpl>(std::move(callback));
411     touchEventActuator->AddTouchEvent(touchEventImp);
412     touchEvent.isInterpolated = true;
413     EXPECT_TRUE(touchEventActuator->TriggerTouchCallBack(touchEvent));
414 
415     /**
416      * @tc.steps: step7. create TouchEvent object and call TriggerTouchCallBack.
417      * @tc.expected: Execute function return value is true.
418      */
419     TouchEvent touchEvent_2 { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE, .pointers = POINTERS_2 };
420     TouchEvent touchEvent_3;
421     touchEvent_2.history.push_back(touchEvent_3);
422     EXPECT_TRUE(touchEventActuator->TriggerTouchCallBack(touchEvent_3));
423 
424     /**
425      * @tc.steps: step8. create TouchEvent object and call isFlushTouchEventsEnd_ assignment.
426      * @tc.expected: Execute function return value is true.
427      */
428     TouchEvent touchEvent_4 { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE, .pointers = POINTERS_2 };
429     touchEvent_2.history.push_back(touchEvent_4);
430     touchEventActuator->isFlushTouchEventsEnd_ = true;
431     EXPECT_TRUE(touchEventActuator->TriggerTouchCallBack(touchEvent_2));
432 }
433 } // namespace OHOS::Ace::NG
434