1 /*
2  * Copyright (c) 2024 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 "gtest/gtest.h"
17 #include "core/common/event_dump.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS::Ace::NG {
23 namespace {
24 constexpr size_t MAX_EVENT_TREE_RECORD_CNT = 5;
25 constexpr int32_t MAX_EVENT_TREE_TOUCH_DOWN_CNT = 10;
26 constexpr int32_t MAX_EVENT_TREE_TOUCH_POINT_CNT = 20;
27 constexpr int32_t PARENT_NODEID = 0;
28 constexpr int32_t NODEID = 1;
29 constexpr int32_t DEFAULT_HITTEST_MODE = 0;
30 constexpr int32_t DEFAULT_DEPTH = 1;
31 constexpr int64_t DEFAULT_TIME_STAMP = 65536;
32 const std::string TAG = "column";
33 } // namespace
34 
35 class EventDumpTestNg : public testing::Test {
36 public:
37     static void SetUpTestSuite();
38     static void TearDownTestSuite();
39     void SetUp() override;
40     void TearDown() override;
41 
42     std::shared_ptr<FrameNodeSnapshot> CreateFrameNodeSnapshotWithInitValue();
43     std::shared_ptr<TouchPointSnapshot> CreateTouchPointSnapshot(const TouchEvent& event);
44     std::shared_ptr<EventTreeRecord> CreateEventTreeRecord();
45     void FillTouchDownEventToEventTree(std::shared_ptr<EventTreeRecord> eventTreeRecord, const TouchEvent& event,
46         int32_t eventTreeRecordCount = 0, int32_t touchDownCount = 0, int32_t touchPointCount = 0);
47 };
48 
SetUp()49 void EventDumpTestNg::SetUp()
50 {}
51 
TearDown()52 void EventDumpTestNg::TearDown()
53 {}
54 
SetUpTestSuite()55 void EventDumpTestNg::SetUpTestSuite()
56 {}
57 
TearDownTestSuite()58 void EventDumpTestNg::TearDownTestSuite()
59 {}
60 
CreateFrameNodeSnapshotWithInitValue()61 std::shared_ptr<FrameNodeSnapshot> EventDumpTestNg::CreateFrameNodeSnapshotWithInitValue()
62 {
63     auto frameNodeSnapshotInstance = std::make_shared<FrameNodeSnapshot>();
64     CHECK_NULL_RETURN(frameNodeSnapshotInstance, nullptr);
65     frameNodeSnapshotInstance->nodeId = NODEID;
66     frameNodeSnapshotInstance->parentNodeId = PARENT_NODEID;
67     frameNodeSnapshotInstance->tag = TAG;
68     frameNodeSnapshotInstance->comId = "comId_01";
69     frameNodeSnapshotInstance->monopolizeEvents = true;
70     frameNodeSnapshotInstance->isHit = true;
71     frameNodeSnapshotInstance->hitTestMode = DEFAULT_HITTEST_MODE;
72     frameNodeSnapshotInstance->responseRegionList = { RectF(0, 0, 20, 10), RectF(25, 0, 20, 10) };
73     return frameNodeSnapshotInstance;
74 }
75 
CreateTouchPointSnapshot(const TouchEvent & event)76 std::shared_ptr<TouchPointSnapshot> EventDumpTestNg::CreateTouchPointSnapshot(const TouchEvent& event)
77 {
78     return std::make_shared<TouchPointSnapshot>(event);
79 }
80 
CreateEventTreeRecord()81 std::shared_ptr<EventTreeRecord> EventDumpTestNg::CreateEventTreeRecord()
82 {
83     return std::make_shared<EventTreeRecord>();
84 }
85 
FillTouchDownEventToEventTree(std::shared_ptr<EventTreeRecord> eventTreeRecord,const TouchEvent & event,const int32_t eventTreeRecordCount,int32_t touchDownCount,int32_t touchPointCount)86 void EventDumpTestNg::FillTouchDownEventToEventTree(std::shared_ptr<EventTreeRecord> eventTreeRecord,
87     const TouchEvent& event, const int32_t eventTreeRecordCount, int32_t touchDownCount, int32_t touchPointCount)
88 {
89     if (!eventTreeRecord || event.type != Ace::TouchType::DOWN) {
90         return;
91     }
92     eventTreeRecord->eventTreeList.clear();
93     for (int32_t i = 0; i < eventTreeRecordCount; ++i) {
94         eventTreeRecord->eventTreeList.emplace_back(EventTree());
95     }
96     if (!eventTreeRecord->eventTreeList.empty()) {
97         for (int32_t i = 0; i < touchPointCount; ++i) {
98             eventTreeRecord->eventTreeList.back().touchPoints.emplace_back(TouchPointSnapshot(event));
99         }
100         eventTreeRecord->eventTreeList.back().touchDownCount = touchDownCount;
101     }
102 }
103 
104 /**
105  * @tc.name: EventDumpTestNg001
106  * @tc.desc: FrameNodeSnapshot dump function test.
107  * @tc.type: FUNC
108  */
109 HWTEST_F(EventDumpTestNg, EventDumpTestNg001, TestSize.Level1)
110 {
111     /**
112      * @tc.steps: step1. create FrameNodeSnapshot instance and init value.
113      */
114     auto frameNodeSnapshotInstance = CreateFrameNodeSnapshotWithInitValue();
115 
116     /**
117      * @tc.steps: step2. Invoke dump function.
118      * @tc.expected: dump list exist data, size is not empty.
119      */
120     ASSERT_NE(frameNodeSnapshotInstance, nullptr);
121     std::list<std::pair<int32_t, std::string>> dumpList;
122     EXPECT_TRUE(dumpList.empty());
123     frameNodeSnapshotInstance->Dump(dumpList, DEFAULT_DEPTH);
124     EXPECT_FALSE(dumpList.empty());
125 }
126 
127 /**
128  * @tc.name: EventDumpTestNg002
129  * @tc.desc: TouchPointSnapshot dump function test.
130  * @tc.type: FUNC
131  */
132 HWTEST_F(EventDumpTestNg, EventDumpTestNg002, TestSize.Level1)
133 {
134     /**
135      * @tc.steps: step1. create TouchPointSnapshot instance and init value.
136      */
137     TouchEvent event;
138     auto touchPointSnapshot = CreateTouchPointSnapshot(event);
139 
140     /**
141      * @tc.steps: step2. Invoke dump function.
142      * @tc.expected: dump list exist data, size is not empty.
143      */
144     ASSERT_NE(touchPointSnapshot, nullptr);
145     std::list<std::pair<int32_t, std::string>> dumpList;
146     EXPECT_TRUE(dumpList.empty());
147     touchPointSnapshot->Dump(dumpList, DEFAULT_DEPTH);
148     EXPECT_FALSE(dumpList.empty());
149 }
150 
151 /**
152  * @tc.name: EventDumpTestNg003
153  * @tc.desc: EventTreeRecord AddTouchPoint function test.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(EventDumpTestNg, EventDumpTestNg003, TestSize.Level1)
157 {
158     /**
159      * @tc.steps: step1. create EventTreeRecord instance.
160      */
161     auto eventTreeRecord = CreateEventTreeRecord();
162     ASSERT_NE(eventTreeRecord, nullptr);
163 
164     /**
165      * @tc.steps: step2. mock touch event DOWN to UP.
166      * @tc.expected: touchDownCount is zero.
167      */
168     const std::vector<Ace::TouchType> touchTypeArray = { Ace::TouchType::DOWN, Ace::TouchType::UP };
169     TouchEvent event;
170     int32_t eventTreeRecordCount = 1;
171     int32_t touchDownCount = 4;
172     for (int32_t i = 0; i < eventTreeRecordCount * touchDownCount; ++i) {
173         int32_t index = i % touchTypeArray.size();
174         event.type = touchTypeArray[index];
175         event.id = touchTypeArray.size() > 0 ? i / touchTypeArray.size() : i;
176         eventTreeRecord->AddTouchPoint(event);
177     }
178     ASSERT_FALSE(eventTreeRecord->eventTreeList.empty());
179     EXPECT_EQ(eventTreeRecord->eventTreeList.back().touchDownCount, (eventTreeRecordCount * touchDownCount) % 2);
180 }
181 
182 /**
183  * @tc.name: EventDumpTestNg004
184  * @tc.desc: EventTreeRecord AddTouchPoint function test.
185  * @tc.type: FUNC
186  */
187 HWTEST_F(EventDumpTestNg, EventDumpTestNg004, TestSize.Level2)
188 {
189     /**
190      * @tc.steps: step1. create EventTreeRecord instance.
191      */
192     auto eventTreeRecord = CreateEventTreeRecord();
193     ASSERT_NE(eventTreeRecord, nullptr);
194     TouchEvent event;
195     event.type = Ace::TouchType::DOWN;
196     event.id = 1;
197 
198     /**
199      * @tc.steps: step2. fill touch event vale to eventTree.
200      * @tc.expected: list is not empty.
201      */
202     FillTouchDownEventToEventTree(eventTreeRecord, event, MAX_EVENT_TREE_RECORD_CNT + 1,
203         MAX_EVENT_TREE_TOUCH_DOWN_CNT, MAX_EVENT_TREE_TOUCH_POINT_CNT + 1);
204     eventTreeRecord->AddTouchPoint(event);
205     ASSERT_FALSE(eventTreeRecord->eventTreeList.empty());
206     EXPECT_EQ(eventTreeRecord->eventTreeList.size(), MAX_EVENT_TREE_RECORD_CNT);
207 }
208 
209 /**
210  * @tc.name: EventDumpTestNg005
211  * @tc.desc: EventTreeRecord AddGestureSnapshot function test.
212  * @tc.type: FUNC
213  */
214 HWTEST_F(EventDumpTestNg, EventDumpTestNg005, TestSize.Level1)
215 {
216     /**
217      * @tc.steps: step1. create EventTreeRecord instance and fill touch event vale to eventTree.
218      */
219     auto eventTreeRecord = CreateEventTreeRecord();
220     ASSERT_NE(eventTreeRecord, nullptr);
221     TouchEvent event;
222     event.type = Ace::TouchType::DOWN;
223     event.id = 1;
224     eventTreeRecord->AddTouchPoint(event);
225 
226     /**
227      * @tc.steps: step2. Invoke AddGestureSnapshot function.
228      * @tc.expected: list is not empty.
229      */
230     auto gestureSnapshot = AceType::MakeRefPtr<GestureSnapshot>();
231     int32_t finger = 0;
232     eventTreeRecord->AddGestureSnapshot(finger, std::move(gestureSnapshot));
233 
234     EXPECT_FALSE(eventTreeRecord->eventTreeList.back().gestureTree.empty());
235     EXPECT_FALSE(eventTreeRecord->eventTreeList.back().gestureMap.empty());
236 }
237 
238 /**
239  * @tc.name: EventDumpTestNg006
240  * @tc.desc: EventTreeRecord AddGestureSnapshot function test.
241  * @tc.type: FUNC
242  */
243 HWTEST_F(EventDumpTestNg, EventDumpTestNg006, TestSize.Level2)
244 {
245     /**
246      * @tc.steps: step1. create EventTreeRecord instance and fill touch event vale to eventTree.
247      */
248     auto eventTreeRecord = CreateEventTreeRecord();
249     ASSERT_NE(eventTreeRecord, nullptr);
250     TouchEvent event;
251     event.type = Ace::TouchType::DOWN;
252     event.id = 1;
253 
254     /**
255      * @tc.steps: step2. Invoke AddGestureSnapshot function.
256      * @tc.expected: list is empty.
257      */
258     FillTouchDownEventToEventTree(eventTreeRecord, event, MAX_EVENT_TREE_RECORD_CNT + 1,
259         MAX_EVENT_TREE_TOUCH_DOWN_CNT, MAX_EVENT_TREE_TOUCH_POINT_CNT + 1);
260 
261     int32_t finger = 0;
262     eventTreeRecord->AddGestureSnapshot(finger, nullptr);
263     EXPECT_TRUE(eventTreeRecord->eventTreeList.empty());
264 }
265 
266 /**
267  * @tc.name: EventDumpTestNg007
268  * @tc.desc: EventTreeRecord AddGestureProcedure function test.
269  * @tc.type: FUNC
270  */
271 HWTEST_F(EventDumpTestNg, EventDumpTestNg007, TestSize.Level1)
272 {
273     /**
274      * @tc.steps: step1. create EventTreeRecord instance and fill touch event vale to eventTree.
275      */
276     auto eventTreeRecord = CreateEventTreeRecord();
277     ASSERT_NE(eventTreeRecord, nullptr);
278     TouchEvent event;
279     event.type = Ace::TouchType::DOWN;
280     event.id = 1;
281     eventTreeRecord->AddTouchPoint(event);
282     auto gestureSnapshot = AceType::MakeRefPtr<GestureSnapshot>();
283     int32_t finger = 0;
284     eventTreeRecord->AddGestureSnapshot(finger, std::move(gestureSnapshot));
285 
286     /**
287      * @tc.steps: step2. Invoke AddGestureProcedure function.
288      * @tc.expected: stateHistory is not empty.
289      */
290     std::string procedure = "HandleTouchDown";
291     std::string state = "current";
292     std::string disposal = "mainThread";
293 
294     eventTreeRecord->AddGestureProcedure(finger, procedure, "", state, disposal, DEFAULT_TIME_STAMP);
295     ASSERT_FALSE(eventTreeRecord->eventTreeList.empty());
296     auto iter = eventTreeRecord->eventTreeList.back().gestureMap.find(finger);
297     ASSERT_TRUE(iter != eventTreeRecord->eventTreeList.back().gestureMap.end());
298     ASSERT_TRUE(iter->second != nullptr);
299     EXPECT_FALSE(iter->second->stateHistory.empty());
300 }
301 
302 /**
303  * @tc.name: EventDumpTestNg008
304  * @tc.desc: EventTreeRecord AddGestureProcedure function test.
305  * @tc.type: FUNC
306  */
307 HWTEST_F(EventDumpTestNg, EventDumpTestNg008, TestSize.Level2)
308 {
309     /**
310      * @tc.steps: step1. create EventTreeRecord instance and fill touch event vale to eventTree.
311      */
312     auto eventTreeRecord = CreateEventTreeRecord();
313     ASSERT_NE(eventTreeRecord, nullptr);
314     TouchEvent event;
315     event.type = Ace::TouchType::DOWN;
316     event.id = 1;
317     eventTreeRecord->AddTouchPoint(event);
318     auto gestureSnapshot = AceType::MakeRefPtr<GestureSnapshot>();
319     gestureSnapshot->type = "TouchEventActuator";
320     int32_t finger = 0;
321     eventTreeRecord->AddGestureSnapshot(finger, std::move(gestureSnapshot));
322 
323     /**
324      * @tc.steps: step2. Invoke AddGestureProcedure function.
325      * @tc.expected: stateHistory is empty.
326      */
327     std::string procedure = "HandleTouchMove";
328     std::string state = "current";
329     std::string disposal = "mainThread";
330     eventTreeRecord->AddGestureProcedure(finger, procedure, "", state, disposal, DEFAULT_TIME_STAMP);
331     ASSERT_FALSE(eventTreeRecord->eventTreeList.empty());
332     auto iter = eventTreeRecord->eventTreeList.back().gestureMap.find(finger);
333     ASSERT_TRUE(iter != eventTreeRecord->eventTreeList.back().gestureMap.end());
334     ASSERT_TRUE(iter->second != nullptr);
335     EXPECT_TRUE(iter->second->stateHistory.empty());
336 }
337 
338 /**
339  * @tc.name: EventDumpTestNg007
340  * @tc.desc: EventTreeRecord AddGestureProcedure function test.
341  * @tc.type: FUNC
342  */
343 HWTEST_F(EventDumpTestNg, EventDumpTestNg009, TestSize.Level1)
344 {
345     /**
346      * @tc.steps: step1. create EventTreeRecord instance and fill touch event vale to eventTree.
347      */
348     auto eventTreeRecord = CreateEventTreeRecord();
349     ASSERT_NE(eventTreeRecord, nullptr);
350     TouchEvent event;
351     event.type = Ace::TouchType::DOWN;
352     event.id = 1;
353     eventTreeRecord->AddTouchPoint(event);
354 
355     /**
356      * @tc.steps: step2. Invoke AddGestureProcedure function.
357      * @tc.expected: stateHistory is not empty.
358      */
359     auto gestureSnapshot = AceType::MakeRefPtr<GestureSnapshot>();
360     int32_t finger = 0;
361     eventTreeRecord->AddGestureSnapshot(finger, std::move(gestureSnapshot));
362     std::string state = "current";
363     std::string disposal = "mainThread";
364 
365     eventTreeRecord->AddGestureProcedure(finger, event, "", state, disposal, DEFAULT_TIME_STAMP);
366     ASSERT_FALSE(eventTreeRecord->eventTreeList.empty());
367     auto iter = eventTreeRecord->eventTreeList.back().gestureMap.find(finger);
368     ASSERT_TRUE(iter != eventTreeRecord->eventTreeList.back().gestureMap.end());
369     ASSERT_TRUE(iter->second != nullptr);
370     EXPECT_FALSE(iter->second->stateHistory.empty());
371 }
372 
373 /**
374  * @tc.name: EventDumpTestNg010
375  * @tc.desc: EventTreeRecord dump function test.
376  * @tc.type: FUNC
377  */
378 HWTEST_F(EventDumpTestNg, EventDumpTestNg010, TestSize.Level1)
379 {
380     /**
381      * @tc.steps: step1. create EventTreeRecord instance and fill touch event vale to eventTree.
382      */
383     auto eventTreeRecord = CreateEventTreeRecord();
384     ASSERT_NE(eventTreeRecord, nullptr);
385     TouchEvent event;
386     event.type = Ace::TouchType::DOWN;
387     event.id = 1;
388     eventTreeRecord->AddTouchPoint(event);
389     auto frameNodeSnapshotInstance = CreateFrameNodeSnapshotWithInitValue();
390     ASSERT_NE(frameNodeSnapshotInstance, nullptr);
391     eventTreeRecord->AddFrameNodeSnapshot(std::move(*frameNodeSnapshotInstance));
392     auto gestureSnapshot = AceType::MakeRefPtr<GestureSnapshot>();
393     int32_t finger = 0;
394     eventTreeRecord->AddGestureSnapshot(finger, std::move(gestureSnapshot));
395     std::string state = "current";
396     std::string disposal = "mainThread";
397     eventTreeRecord->AddGestureProcedure(finger, event, "", state, disposal, DEFAULT_TIME_STAMP);
398 
399     /**
400      * @tc.steps: step2. Invoke dump function.
401      * @tc.expected: dump list exist data, size is not empty.
402      */
403     std::list<std::pair<int32_t, std::string>> dumpList;
404     EXPECT_TRUE(dumpList.empty());
405     eventTreeRecord->Dump(dumpList, DEFAULT_DEPTH);
406     EXPECT_FALSE(dumpList.empty());
407 }
408 } // namespace OHOS::Ace::NG
409