1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 
18 #include <queue>
19 #include <vector>
20 
21 #include "event_resample.h"
22 #include "input_event.h"
23 #include "mmi_log.h"
24 #include "window_info.h"
25 
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "EventResampleTest"
28 
29 namespace OHOS {
30 namespace MMI {
31 
32 namespace {
33 using namespace testing::ext;
34 constexpr int64_t START_TIME = 10000;
35 constexpr int64_t TIME_DELTA = 2500;
36 constexpr uint32_t INITIAL_COORDS = 10;
37 constexpr uint32_t COORDS_DELTA = 10;
38 constexpr int64_t FRAME_TIME = 8000;
39 } // namespace
40 
41 class EventResampleTest : public testing::Test {
42 public:
SetUpTestCase(void)43     static void SetUpTestCase(void) {}
TearDownTestCase(void)44     static void TearDownTestCase(void) {}
45 
46     struct TestData {
47         uint32_t framesNum { 10 };
48         uint32_t pointerId { 0 };
49         uint32_t fingerNum { 1 };
50         uint32_t evtNum { 0 };
51         int64_t timeDelta { TIME_DELTA };
52         uint32_t coordsDelta { COORDS_DELTA };
53     };
54 
55     struct Context {
56         int32_t lastDispX { INITIAL_COORDS };
57         int32_t lastDispY { INITIAL_COORDS };
58         int64_t lastTime { START_TIME };
59         int64_t frameTime { START_TIME };
60         int64_t lastFrameTime { START_TIME };
61 
ResetOHOS::MMI::EventResampleTest::Context62         void Reset(void)
63         {
64             lastDispX = INITIAL_COORDS;
65             lastDispY = INITIAL_COORDS;
66             lastTime = START_TIME;
67             frameTime = START_TIME;
68             lastFrameTime = START_TIME;
69         }
70     };
71 
72     struct InputEvt {
73         int32_t action { PointerEvent::POINTER_ACTION_UNKNOWN };
74         int64_t actionTime { START_TIME };
75         int32_t dispX { INITIAL_COORDS };
76         int32_t dispY { INITIAL_COORDS };
77         int32_t id { 0 };
78 
InitializeOHOS::MMI::EventResampleTest::InputEvt79         void Initialize(int32_t action, TestData &testData, Context &context)
80         {
81             this->action = action;
82             dispX = context.lastDispX + testData.coordsDelta;
83             context.lastDispX = dispX;
84             dispY = context.lastDispY + testData.coordsDelta;
85             context.lastDispY = dispY;
86             id = testData.pointerId;
87 
88             if (action == PointerEvent::POINTER_ACTION_DOWN) {
89                 actionTime = context.lastFrameTime;
90                 context.lastTime = 0;
91             } else {
92                 actionTime = context.lastFrameTime + context.lastTime + testData.timeDelta;
93                 context.lastTime = actionTime - context.lastFrameTime;
94             }
95         }
96 
InitializeFromOHOS::MMI::EventResampleTest::InputEvt97         void InitializeFrom(InputEvt &event)
98         {
99             action = event.action;
100             actionTime = event.actionTime;
101             dispX = event.dispX;
102             dispY = event.dispY;
103             id = event.id;
104         };
105     };
106 
107     struct ExpectedData {
108         int64_t actionTime { 0 };
109         int32_t dispX { 0 };
110         int32_t dispY { 0 };
111         int64_t touchUpTime { 0 };
112         int32_t touchUpX { 0 };
113         int32_t touchUpY { 0 };
114         int32_t id { 0 };
115 
ExpectedDataOHOS::MMI::EventResampleTest::ExpectedData116         ExpectedData() {}
117 
ResetOHOS::MMI::EventResampleTest::ExpectedData118         void Reset(int32_t id)
119         {
120             this->id = id;
121             actionTime = 0;
122             dispX = 0;
123             dispY = 0;
124             touchUpTime = 0;
125             touchUpX = 0;
126             touchUpY = 0;
127         }
128 
UpdateTouchStateOHOS::MMI::EventResampleTest::ExpectedData129         void UpdateTouchState(InputEvt &event)
130         {
131             if (id != event.id) {
132                 return;
133             }
134 
135             switch (event.action) {
136                 case PointerEvent::POINTER_ACTION_DOWN : {
137                     touchState.clear();
138                     eventBatch.clear();
139                     InputEvt evt;
140                     evt.InitializeFrom(event);
141                     touchState.insert(touchState.begin(), std::move(evt));
142                     actionTime = event.actionTime;
143                     dispX = event.dispX;
144                     dispY = event.dispY;
145                     break;
146                 }
147                 case PointerEvent::POINTER_ACTION_UP : {
148                     touchState.clear();
149                     eventBatch.clear();
150                     touchUpTime = event.actionTime;
151                     touchUpX = event.dispX;
152                     touchUpY = event.dispY;
153                     break;
154                 }
155                 case PointerEvent::POINTER_ACTION_MOVE : {
156                     while (touchState.size() > 1) {
157                         touchState.pop_back();
158                     }
159                     InputEvt evt;
160                     evt.InitializeFrom(event);
161                     touchState.insert(touchState.begin(), std::move(evt));
162                     actionTime = event.actionTime;
163                     dispX = event.dispX;
164                     dispY = event.dispY;
165                     break;
166                 }
167             }
168         }
169 
AddEventOHOS::MMI::EventResampleTest::ExpectedData170         void AddEvent(InputEvt &event)
171         {
172             if (id != event.id) {
173                 return;
174             }
175 
176             if (event.action == PointerEvent::POINTER_ACTION_MOVE) {
177                 InputEvt evt;
178                 evt.InitializeFrom(event);
179                 eventBatch.push_back(std::move(evt));
180             } else {
181                 if ((event.action == PointerEvent::POINTER_ACTION_UP) && (!eventBatch.empty())) {
182                     for (size_t i = 0; i < eventBatch.size(); i++) {
183                         InputEvt& event = eventBatch.at(i);
184                         UpdateTouchState(event);
185                     }
186                     eventBatch.erase(eventBatch.begin(), eventBatch.begin() + eventBatch.size() - 1);
187                 }
188                 UpdateTouchState(event);
189             }
190         }
191 
CalculateExpectedOHOS::MMI::EventResampleTest::ExpectedData192         int32_t CalculateExpected(int64_t frameTime)
193         {
194             int64_t sampleTime = frameTime - EventResample::RESAMPLE_LATENCY;
195             InputEvt current;
196 
197             if (eventBatch.empty()) {
198                 MMI_HILOGD("Event Batch empty");
199                 return ERR_WOULD_BLOCK;
200             }
201 
202             size_t numSamples = eventBatch.size();
203             size_t idx = 0;
204             while ((idx < numSamples) && (eventBatch.at(idx).actionTime <= sampleTime)) {
205                 idx += 1;
206             }
207             ssize_t split = ssize_t(idx) - 1;
208             if (split < 0) {
209                 MMI_HILOGD("Negative split value");
210                 return ERR_WOULD_BLOCK;
211             }
212             size_t count = split + 1;
213 
214             // Consume samples in batch
215             for (size_t i = 0; i < count; i++) {
216                 InputEvt& event = eventBatch.at(i);
217                 UpdateTouchState(event);
218             }
219             eventBatch.erase(eventBatch.begin(), eventBatch.begin() + count);
220 
221             current.InitializeFrom(touchState[0]);
222 
223             return ResampleCoord(sampleTime, current);
224         }
225 
ResampleCoordOHOS::MMI::EventResampleTest::ExpectedData226         int32_t ResampleCoord(int64_t sampleTime, InputEvt &current)
227         {
228             float alpha = 0.0;
229             InputEvt other;
230 
231             if (eventBatch.empty()) {
232                 // Coordinates extrapolation
233                 MMI_HILOGD("Extrapolation");
234                 if (touchState.size() < EventResample::HISTORY_SIZE_MAX) {
235                     return ERR_OK;
236                 }
237                 other.InitializeFrom(touchState[1]);
238                 int64_t delta = touchState[0].actionTime - touchState[1].actionTime;
239                 if (delta < EventResample::RESAMPLE_MIN_DELTA) {
240                     return ERR_OK;
241                 } else if (delta > EventResample::RESAMPLE_MAX_DELTA) {
242                     return ERR_OK;
243                 }
244                 int64_t maxPredict = touchState[0].actionTime +
245                                      std::min(delta / 2, EventResample::RESAMPLE_MAX_PREDICTION);
246                 if (sampleTime > maxPredict) {
247                     sampleTime = maxPredict;
248                 }
249                 alpha = static_cast<float>(touchState[0].actionTime - sampleTime) / delta;
250             } else {
251                 // Coordinates interpolation
252                 MMI_HILOGD("Interpolation");
253                 InputEvt &next = eventBatch.front();
254                 other.InitializeFrom(next);
255                 int64_t delta = next.actionTime - touchState[0].actionTime;
256                 if (delta < EventResample::RESAMPLE_MIN_DELTA) {
257                     MMI_HILOGD("RESAMPLE_MIN_DELTA = %{public}" PRId64 ", next_x = %{public}d, ts0_x = %{public}d",
258                                delta, next.dispX, touchState[0].dispX);
259                     return ERR_OK;
260                 }
261                 alpha = static_cast<float>(sampleTime - touchState[0].actionTime) / delta;
262             }
263 
264             dispX = CalcCoord(current.dispX, other.dispX, alpha);
265             dispY = CalcCoord(current.dispY, other.dispY, alpha);
266             actionTime = sampleTime;
267 
268             return ERR_OK;
269         }
270 
271     protected:
272         std::vector<InputEvt> touchState;
273         std::vector<InputEvt> eventBatch;
274     };
275 
276     EventResampleTest();
277     ~EventResampleTest();
278 
279     bool SetupPointerEvent(InputEvt &event, TestData &testData);
280     int32_t CheckResults(std::shared_ptr<PointerEvent> outEvent,
281                          std::vector<ExpectedData> &expected, Context &context);
282     bool DoTest(TestData &testData, int32_t testId);
283     void ReadQueue(TestData &testData, Context &ctx, std::vector<ExpectedData> &expected);
284     void SendTouchUp(TestData &testData, Context &ctx, std::vector<ExpectedData> &expected);
285 
286     std::shared_ptr<PointerEvent> pointerEvent_ = nullptr;
287     std::queue<InputEvt> eventQueue_;
288     uint32_t failCount_ = 0;
289 };
290 
EventResampleTest()291 EventResampleTest::EventResampleTest()
292 {
293     pointerEvent_ = PointerEvent::Create();
294 }
295 
~EventResampleTest()296 EventResampleTest::~EventResampleTest()
297 {
298 }
299 
SetupPointerEvent(InputEvt & event,TestData & testData)300 bool EventResampleTest::SetupPointerEvent(InputEvt &event, TestData &testData)
301 {
302     pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
303     pointerEvent_->SetPointerAction(event.action);
304     pointerEvent_->SetPointerId(event.id);
305     pointerEvent_->SetDeviceId(0);
306 
307     auto pointIds = pointerEvent_->GetPointerIds();
308     int64_t time = event.actionTime;
309     if (pointIds.empty()) {
310         pointerEvent_->SetActionStartTime(time);
311     }
312     pointerEvent_->SetActionTime(time);
313 
314     for (uint32_t idx = 0; idx < testData.fingerNum; idx++) {
315         PointerEvent::PointerItem item;
316         if (pointerEvent_->GetPointerItem(idx, item)) {
317             item.SetPointerId(idx);
318             item.SetDisplayX(event.dispX);
319             item.SetDisplayY(event.dispY);
320             item.SetToolType(PointerEvent::TOOL_TYPE_FINGER);
321             item.SetDeviceId(0);
322             pointerEvent_->UpdatePointerItem(idx, item);
323         } else {
324             item.SetPointerId(idx);
325             item.SetDisplayX(event.dispX);
326             item.SetDisplayY(event.dispY);
327             item.SetToolType(PointerEvent::TOOL_TYPE_FINGER);
328             item.SetDeviceId(0);
329             pointerEvent_->AddPointerItem(item);
330         }
331     }
332 
333     return true;
334 }
335 
CheckResults(std::shared_ptr<PointerEvent> outEvent,std::vector<ExpectedData> & expected,Context & context)336 int32_t EventResampleTest::CheckResults(std::shared_ptr<PointerEvent> outEvent,
337                                         std::vector<ExpectedData> &expected, Context &context)
338 {
339     bool ret = ERR_OK;
340     int32_t failCount = 0;
341     int64_t actionTime = 0;
342     int32_t dispX = 0;
343     int32_t dispY = 0;
344 
345     for (auto &it : expected) {
346         PointerEvent::PointerItem pointerItem;
347         if (!outEvent->GetPointerItem(it.id, pointerItem)) {
348             EXPECT_TRUE(false);
349             ret = ERR_INVALID_VALUE;
350             break;
351         }
352 
353         if (outEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
354             actionTime = expected[it.id].actionTime;
355             dispX = expected[it.id].dispX;
356             dispY = expected[it.id].dispY;
357         } else if (outEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE) {
358             expected[it.id].CalculateExpected(context.frameTime);
359             actionTime = expected[it.id].actionTime;
360             dispX = expected[it.id].dispX;
361             dispY = expected[it.id].dispY;
362         } else if (outEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP) {
363             actionTime = expected[it.id].touchUpTime;
364             dispX = expected[it.id].touchUpX;
365             dispY = expected[it.id].touchUpY;
366         }
367 
368         MMI_HILOGD("OutEvent: x=%{public}d y=%{public}d t=%{public}" PRId64 " f=%{public}" PRId64 " (%{public}d)",
369                    pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), outEvent->GetActionTime(),
370                    context.frameTime, outEvent->GetPointerAction());
371         MMI_HILOGD("Expected: x=%{public}d y=%{public}d t=%{public}" PRId64, dispX, dispY, actionTime);
372 
373         if (pointerItem.GetDisplayX() != dispX) {
374             failCount++;
375             EXPECT_EQ(pointerItem.GetDisplayX(), dispX);
376         }
377         if (pointerItem.GetDisplayY() != dispY) {
378             failCount++;
379             EXPECT_EQ(pointerItem.GetDisplayY(), dispY);
380         }
381         if (outEvent->GetActionTime() != actionTime) {
382             failCount++;
383             EXPECT_EQ(outEvent->GetActionTime(), actionTime);
384         }
385 
386         if (failCount != 0) {
387             MMI_HILOGD("Test Failed");
388         }
389         failCount_ += failCount;
390     }
391 
392     return ret;
393 }
394 
DoTest(TestData & testData,int32_t testId)395 bool EventResampleTest::DoTest(TestData &testData, int32_t testId)
396 {
397     CHKPF(pointerEvent_);
398     pointerEvent_->Reset();
399     Context ctx;
400     std::shared_ptr<PointerEvent> outEvent = nullptr;
401     std::vector<ExpectedData> expected(testData.fingerNum);
402 
403     MMI_HILOGD("Start test %{public}d", testId);
404 
405     for (uint32_t idx = 0; idx < testData.fingerNum; idx++) {
406         expected[idx].Reset(idx);
407     }
408 
409     failCount_ = 0;
410     ctx.Reset();
411 
412     // Send touch down event
413     InputEvt touchDown;
414     touchDown.Initialize(PointerEvent::POINTER_ACTION_DOWN, testData, ctx);
415     eventQueue_.push(std::move(touchDown));
416 
417     // Send touch moving events
418     for (uint32_t idx = 0; idx < testData.framesNum; idx++) {
419         ctx.lastFrameTime = ctx.frameTime;
420         ctx.frameTime += FRAME_TIME;
421         ctx.lastTime = 0;
422         MMI_HILOGD("Frame %{public}d: lf = %{public}" PRId64 " f = %{public}" PRId64,
423                    idx, ctx.lastFrameTime, ctx.frameTime);
424 
425         for (uint32_t eidx = 0; eidx < testData.evtNum; eidx++) {
426             InputEvt touchMove;
427             touchMove.Initialize(PointerEvent::POINTER_ACTION_MOVE, testData, ctx);
428             eventQueue_.push(std::move(touchMove));
429         }
430 
431         // Read data from queue and check results
432         ReadQueue(testData, ctx, expected);
433     }
434 
435     // Send touch up event
436     SendTouchUp(testData, ctx, expected);
437 
438     return (failCount_ != 0) ? false : true;
439 }
440 
ReadQueue(TestData & testData,Context & ctx,std::vector<ExpectedData> & expected)441 void EventResampleTest::ReadQueue(TestData &testData, Context &ctx, std::vector<ExpectedData> &expected)
442 {
443     std::shared_ptr<PointerEvent> outEvent = nullptr;
444     ErrCode status = RET_OK;
445 
446     while (!eventQueue_.empty()) {
447         InputEvt &event = eventQueue_.front();
448         expected[event.id].AddEvent(event);
449         SetupPointerEvent(event, testData);
450 
451         PointerEvent::PointerItem pointerItem;
452         pointerEvent_->GetPointerItem(0, pointerItem);
453         MMI_HILOGD("pointerEvent_: x = %{public}d y = %{public}d t = %{public}" PRId64,
454                    pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), pointerEvent_->GetActionTime());
455 
456         outEvent = EventResampleHdr->OnEventConsume(pointerEvent_, ctx.frameTime, status);
457         if ((outEvent != nullptr) && (PointerEvent::POINTER_ACTION_DOWN != outEvent->GetPointerAction())) {
458             MMI_HILOGE("Unexpected pointer action: %{public}d while %{public}d expected",
459                        outEvent->GetPointerAction(), PointerEvent::POINTER_ACTION_DOWN);
460             failCount_++;
461         } else if (outEvent != nullptr) {
462             EXPECT_EQ(ERR_OK, CheckResults(outEvent, expected, ctx));
463             EXPECT_EQ(ERR_OK, status);
464         }
465         eventQueue_.pop();
466     }
467 
468     outEvent = EventResampleHdr->OnEventConsume(nullptr, ctx.frameTime, status);
469     if (outEvent != nullptr) {
470         EXPECT_EQ(ERR_OK, CheckResults(outEvent, expected, ctx));
471         EXPECT_EQ(ERR_OK, status);
472     } else {
473         MMI_HILOGD("NULL Event_: status = %{public}d", status);
474     }
475 }
476 
SendTouchUp(TestData & testData,Context & ctx,std::vector<ExpectedData> & expected)477 void EventResampleTest::SendTouchUp(TestData &testData, Context &ctx, std::vector<ExpectedData> &expected)
478 {
479     std::shared_ptr<PointerEvent> outEvent = nullptr;
480     ErrCode status = RET_OK;
481     InputEvt touchUp;
482 
483     touchUp.Initialize(PointerEvent::POINTER_ACTION_UP, testData, ctx);
484     expected[touchUp.id].AddEvent(touchUp);
485     SetupPointerEvent(touchUp, testData);
486     outEvent = EventResampleHdr->OnEventConsume(pointerEvent_, ctx.frameTime, status);
487     if (outEvent != nullptr) {
488         MMI_HILOGD("Pointer Action: %{public}d", outEvent->GetPointerAction());
489         EXPECT_EQ(ERR_OK, CheckResults(outEvent, expected, ctx));
490         EXPECT_EQ(ERR_OK, status);
491     } else {
492         MMI_HILOGD("NULL Event_: status = %{public}d", status);
493     }
494 }
495 
496 /**
497  * @tc.name: EventResampleTest_001
498  * @tc.desc: Test to check single touch without moving events
499  * @tc.type: FUNC
500  * @tc.require:
501  */
502 HWTEST_F(EventResampleTest, EventResampleTest_001, TestSize.Level1)
503 {
504     CALL_TEST_DEBUG;
505     TestData testData = {.framesNum = 5, .fingerNum = 1, .evtNum = 0};
506     EXPECT_EQ(EventResampleHdr->GetPointerEvent(), nullptr);
507     EXPECT_TRUE(DoTest(testData, 1));
508     EXPECT_NE(EventResampleHdr->GetPointerEvent(), nullptr);
509 }
510 
511 /**
512  * @tc.name: EventResampleTest_002
513  * @tc.desc: Basic test to check events interpolation
514  * @tc.type: FUNC
515  * @tc.require:
516  */
517 HWTEST_F(EventResampleTest, EventResampleTest_002, TestSize.Level1)
518 {
519     CALL_TEST_DEBUG;
520     TestData testData = {.framesNum = 5, .fingerNum = 1, .evtNum = 2};
521     EXPECT_TRUE(DoTest(testData, 2));
522 }
523 
524 /**
525  * @tc.name: EventResampleTest_003
526  * @tc.desc: Basic test to check events extrapolation
527  * @tc.type: FUNC
528  * @tc.require:
529  */
530 HWTEST_F(EventResampleTest, EventResampleTest_003, TestSize.Level1)
531 {
532     CALL_TEST_DEBUG;
533     TestData testData = {.framesNum = 5, .fingerNum = 1, .evtNum = 1};
534     EXPECT_TRUE(DoTest(testData, 3));
535 }
536 
537 /**
538  * @tc.name: EventResampleTest_004
539  * @tc.desc: Test to check interpolation behavior when event received later then latency time
540  * @tc.type: FUNC
541  * @tc.require:
542  */
543 HWTEST_F(EventResampleTest, EventResampleTest_004, TestSize.Level1)
544 {
545     CALL_TEST_DEBUG;
546     TestData testData = {.framesNum = 5, .fingerNum = 1, .evtNum = 1, .timeDelta = 6000};
547     EXPECT_TRUE(DoTest(testData, 4));
548 }
549 
550 /**
551  * @tc.name: EventResampleTest_005
552  * @tc.desc: Test to check case when events intervals less than minimal delta value
553  * @tc.type: FUNC
554  * @tc.require:
555  */
556 HWTEST_F(EventResampleTest, EventResampleTest_005, TestSize.Level1)
557 {
558     CALL_TEST_DEBUG;
559     TestData testData = {.framesNum = 5, .fingerNum = 1, .evtNum = 5, .timeDelta = 1000};
560     EXPECT_TRUE(DoTest(testData, 5));
561 }
562 
563 /**
564  * @tc.name: EventResampleTest_006
565  * @tc.desc: Test to check case when many events are received during time frame
566  * @tc.type: FUNC
567  * @tc.require:
568  */
569 HWTEST_F(EventResampleTest, EventResampleTest_006, TestSize.Level1)
570 {
571     CALL_TEST_DEBUG;
572     TestData testData = {.framesNum = 5, .fingerNum = 1, .evtNum = 3};
573     EXPECT_TRUE(DoTest(testData, 6));
574 }
575 
576 /**
577  * @tc.name: EventResampleTest_OnEventConsume
578  * @tc.desc: Test OnEventConsume
579  * @tc.type: FUNC
580  * @tc.require:
581  */
582 HWTEST_F(EventResampleTest, EventResampleTest_OnEventConsume, TestSize.Level1)
583 {
584     CALL_TEST_DEBUG;
585     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
586     ASSERT_NE(pointerEvent, nullptr);
587     int64_t frameTime = 0;
588     ErrCode status = ERR_OK;
589     EventResampleHdr->frameTime_ = 0;
590     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
591     ASSERT_NE(EventResampleHdr->OnEventConsume(pointerEvent, frameTime, status), nullptr);
592 }
593 
594 /**
595  * @tc.name: EventResampleTest_InitializeInputEvent
596  * @tc.desc: Test InitializeInputEvent
597  * @tc.type: FUNC
598  * @tc.require:
599  */
600 HWTEST_F(EventResampleTest, EventResampleTest_InitializeInputEvent, TestSize.Level1)
601 {
602     CALL_TEST_DEBUG;
603     std::shared_ptr<PointerEvent> pointerEvent = nullptr;
604     int64_t frameTime = 0;
605     EventResampleHdr->frameTime_ = 0;
606     ASSERT_EQ(EventResampleHdr->InitializeInputEvent(pointerEvent, frameTime), ERR_OK);
607 }
608 
609 /**
610  * @tc.name: EventResampleTest_TransformSampleWindowXY
611  * @tc.desc: Test TransformSampleWindowXY
612  * @tc.type: FUNC
613  * @tc.require:
614  */
615 HWTEST_F(EventResampleTest, EventResampleTest_TransformSampleWindowXY, TestSize.Level1)
616 {
617     CALL_TEST_DEBUG;
618     std::shared_ptr<PointerEvent> pointerEvent = nullptr;
619     PointerEvent::PointerItem item;
620     int32_t logicX = 100;
621     int32_t logicY = 100;
622     std::pair<int32_t, int32_t> pair { logicX, logicY };
623     ASSERT_EQ(EventResampleHdr->TransformSampleWindowXY(pointerEvent, item, logicX, logicY), pair);
624 }
625 
626 /**
627  * @tc.name: EventResampleTest_UpdatePointerEvent_001
628  * @tc.desc: Test the funcation UpdatePointerEvent
629  * @tc.type: FUNC
630  * @tc.require:
631  */
632 HWTEST_F(EventResampleTest, EventResampleTest_UpdatePointerEvent_001, TestSize.Level1)
633 {
634     CALL_TEST_DEBUG;
635     EventResample::MotionEvent outEvent;
636     outEvent.actionTime = 100;
637     outEvent.pointerAction = PointerEvent::POINTER_ACTION_MOVE;
638     outEvent.actionTime = 5;
639     outEvent.eventId = 6;
640     EventResample::Pointer p;
641     p.coordX = 100;
642     p.coordY = 10;
643     p.toolType = 1;
644     p.id = 6;
645     outEvent.pointers.insert(std::make_pair(1, p));
646     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->UpdatePointerEvent(&outEvent));
647     outEvent.pointerAction = PointerEvent::POINTER_ACTION_UP;
648     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->UpdatePointerEvent(&outEvent));
649     outEvent.pointerAction = PointerEvent::POINTER_ACTION_DOWN;
650     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->UpdatePointerEvent(&outEvent));
651 }
652 
653 /**
654  * @tc.name: EventResampleTest_UpdatePointerEvent_002
655  * @tc.desc: Test the funcation UpdatePointerEvent
656  * @tc.type: FUNC
657  * @tc.require:
658  */
659 HWTEST_F(EventResampleTest, EventResampleTest_UpdatePointerEvent_002, TestSize.Level1)
660 {
661     CALL_TEST_DEBUG;
662     EventResample::MotionEvent outEvent;
663     outEvent.actionTime = 20;
664     outEvent.pointerAction = 10;
665     outEvent.actionTime = 2;
666     outEvent.eventId = 6;
667     EventResample::Pointer p;
668     p.coordX = 20;
669     p.coordY = 30;
670     p.toolType = 2;
671     p.id = 3;
672     outEvent.pointers.insert(std::make_pair(1, p));
673     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->UpdatePointerEvent(&outEvent));
674 }
675 
676 /**
677  * @tc.name: EventResampleTest_TransformSampleWindowXY_001
678  * @tc.desc: Test the funcation TransformSampleWindowXY
679  * @tc.type: FUNC
680  * @tc.require:
681  */
682 HWTEST_F(EventResampleTest, EventResampleTest_TransformSampleWindowXY_001, TestSize.Level1)
683 {
684     CALL_TEST_DEBUG;
685     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
686     ASSERT_NE(pointerEvent, nullptr);
687     PointerEvent::PointerItem item;
688     item.SetPointerId(0);
689     item.SetDownTime(100);
690     item.SetToolDisplayX(90);
691     item.SetToolDisplayY(90);
692     item.SetToolWindowX(50);
693     item.SetToolWindowY(50);
694     item.SetToolWidth(30);
695     item.SetToolHeight(30);
696     item.SetLongAxis(100);
697     item.SetShortAxis(20);
698     item.SetToolType(2);
699     item.SetTargetWindowId(0);
700     pointerEvent->AddPointerItem(item);
701     int32_t logicX = 100;
702     int32_t logicY = 100;
703     std::shared_ptr<InputEvent> inputEvent = InputEvent::Create();
704     EXPECT_NE(inputEvent, nullptr);
705     inputEvent->targetDisplayId_ = 10;
706     inputEvent->targetWindowId_ = 10;
707     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->TransformSampleWindowXY(pointerEvent, item, logicX, logicY));
708     WindowInfo window;
709     window.transform.push_back(1.0f);
710     window.transform.push_back(2.0f);
711     window.transform.push_back(3.0f);
712     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->TransformSampleWindowXY(pointerEvent, item, logicX, logicY));
713 }
714 
715 /**
716  * @tc.name: EventResampleTest_TransformSampleWindowXY_002
717  * @tc.desc: Test the funcation TransformSampleWindowXY
718  * @tc.type: FUNC
719  * @tc.require:
720  */
721 HWTEST_F(EventResampleTest, EventResampleTest_TransformSampleWindowXY_002, TestSize.Level1)
722 {
723     CALL_TEST_DEBUG;
724     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
725     ASSERT_NE(pointerEvent, nullptr);
726     PointerEvent::PointerItem item;
727     item.SetPointerId(1);
728     item.SetDownTime(10);
729     item.SetToolDisplayX(9);
730     item.SetToolDisplayY(8);
731     item.SetToolWindowX(7);
732     item.SetToolWindowY(6);
733     item.SetToolWidth(5);
734     item.SetToolHeight(4);
735     item.SetLongAxis(3);
736     item.SetShortAxis(2);
737     item.SetToolType(1);
738     item.SetTargetWindowId(0);
739     pointerEvent->AddPointerItem(item);
740     int32_t logicX = 10;
741     int32_t logicY = 20;
742     std::shared_ptr<InputEvent> inputEvent = InputEvent::Create();
743     EXPECT_NE(inputEvent, nullptr);
744     inputEvent->targetDisplayId_ = 20;
745     inputEvent->targetWindowId_ = 10;
746     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->TransformSampleWindowXY(pointerEvent, item, logicX, logicY));
747 }
748 
749 /**
750  * @tc.name: EventResampleTest_ConsumeBatch_001
751  * @tc.desc: Test the funcation ConsumeBatch
752  * @tc.type: FUNC
753  * @tc.require:
754  */
755 HWTEST_F(EventResampleTest, EventResampleTest_ConsumeBatch_001, TestSize.Level1)
756 {
757     CALL_TEST_DEBUG;
758     int64_t frameTime = 5;
759     EventResample::MotionEvent** outEvent = new EventResample::MotionEvent*[3];
760     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ConsumeBatch(frameTime, outEvent));
761     frameTime = -4;
762     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ConsumeBatch(frameTime, outEvent));
763 }
764 
765 /**
766  * @tc.name: EventResampleTest_ResampleTouchState_001
767  * @tc.desc: Test the funcation ResampleTouchState
768  * @tc.type: FUNC
769  * @tc.require:
770  */
771 HWTEST_F(EventResampleTest, EventResampleTest_ResampleTouchState_001, TestSize.Level1)
772 {
773     CALL_TEST_DEBUG;
774     int64_t sampleTime = 5;
775     EventResample::MotionEvent event;
776     EventResample::MotionEvent next;
777     event.actionTime = 10;
778     event.pointerAction = 20;
779     event.actionTime = 5;
780     event.eventId = 6;
781     next.actionTime = 20;
782     next.pointerAction = 30;
783     next.actionTime = 8;
784     next.eventId = 9;
785     EventResampleHdr->resampleTouch_ = false;
786     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleTouchState(sampleTime, &event, &next));
787     EventResampleHdr->resampleTouch_ = true;
788     event.sourceType = PointerEvent::SOURCE_TYPE_TOUCHPAD;
789     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleTouchState(sampleTime, &event, &next));
790     event.sourceType = PointerEvent::SOURCE_TYPE_TOUCHSCREEN;
791     event.pointerAction = PointerEvent::POINTER_ACTION_UP;
792     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleTouchState(sampleTime, &event, &next));
793     event.pointerAction = PointerEvent::POINTER_ACTION_MOVE;
794     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleTouchState(sampleTime, &event, &next));
795 }
796 
797 /**
798  * @tc.name: EventResampleTest_ResampleTouchState_002
799  * @tc.desc: Test the funcation ResampleTouchState
800  * @tc.type: FUNC
801  * @tc.require:
802  */
803 HWTEST_F(EventResampleTest, EventResampleTest_ResampleTouchState_002, TestSize.Level1)
804 {
805     CALL_TEST_DEBUG;
806     int64_t sampleTime = 7;
807     EventResample::MotionEvent event;
808     EventResample::MotionEvent next;
809     event.actionTime = 1;
810     event.pointerAction = 2;
811     event.actionTime = 2;
812     event.eventId = 4;
813     next.actionTime = 5;
814     next.pointerAction = 6;
815     next.actionTime = 7;
816     next.eventId = 8;
817     EventResampleHdr->resampleTouch_ = true;
818     event.sourceType = PointerEvent::SOURCE_TYPE_TOUCHSCREEN;
819     event.pointerAction = PointerEvent::POINTER_ACTION_MOVE;
820     int32_t deviceId = 9;
821     int32_t source = 8;
822     EventResample::TouchState ts;
823     ts.deviceId = 9;
824     ts.source = 8;
825     ts.historyCurrent = 3;
826     ts.historySize = 4;
827     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleTouchState(sampleTime, &event, &next));
828     deviceId = 15;
829     source = 13;
830     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleTouchState(sampleTime, &event, &next));
831 }
832 
833 /**
834  * @tc.name: EventResampleTest_ResampleCoordinates_001
835  * @tc.desc: Test the funcation ResampleCoordinates
836  * @tc.type: FUNC
837  * @tc.require:
838  */
839 HWTEST_F(EventResampleTest, EventResampleTest_ResampleCoordinates_001, TestSize.Level1)
840 {
841     CALL_TEST_DEBUG;
842     int64_t sampleTime = 6;
843     EventResample::MotionEvent event;
844     EventResample::TouchState touchState;
845     EventResample::History current;
846     EventResample::History other;
847     float alpha = 1.0;
848     EventResample::Pointer p;
849     p.coordX = 20;
850     p.coordY = 30;
851     p.toolType = 2;
852     p.id = 5;
853     event.pointers.insert(std::make_pair(1, p));
854     EventResample::TouchState ts;
855     ts.deviceId = 5;
856     ts.source = 2;
857     ts.historyCurrent = 3;
858     ts.historySize = 4;
859     EventResampleHdr->touchStates_.push_back(ts);
860     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleCoordinates(sampleTime, &event, touchState, &current,
861         &other, alpha));
862 }
863 
864 /**
865  * @tc.name: EventResampleTest_ResampleCoordinates_002
866  * @tc.desc: Test the funcation ResampleCoordinates
867  * @tc.type: FUNC
868  * @tc.require:
869  */
870 HWTEST_F(EventResampleTest, EventResampleTest_ResampleCoordinates_002, TestSize.Level1)
871 {
872     CALL_TEST_DEBUG;
873     int64_t sampleTime = 2;
874     EventResample::MotionEvent event;
875     EventResample::TouchState touchState;
876     EventResample::History current;
877     EventResample::History other;
878     float alpha = 5.0;
879     EventResample::Pointer p;
880     p.coordX = 5;
881     p.coordY = 8;
882     p.toolType = 9;
883     p.id = 2;
884     event.pointers.insert(std::make_pair(1, p));
885     EventResample::TouchState ts;
886     ts.deviceId = 3;
887     ts.source = 4;
888     ts.historyCurrent = 7;
889     ts.historySize = 5;
890     EventResampleHdr->touchStates_.push_back(ts);
891     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->ResampleCoordinates(sampleTime, &event, touchState, &current,
892         &other, alpha));
893 }
894 
895 /**
896  * @tc.name: EventResampleTest_FindBatch_001
897  * @tc.desc: Test the funcation FindBatch
898  * @tc.type: FUNC
899  * @tc.require:
900  */
901 HWTEST_F(EventResampleTest, EventResampleTest_FindBatch_001, TestSize.Level1)
902 {
903     CALL_TEST_DEBUG;
904     int32_t deviceId = 1;
905     int32_t source = 3;
906     EventResample::MotionEvent motionEvent;
907     motionEvent.deviceId = 1;
908     motionEvent.sourceType = 3;
909     EventResample::Batch batch;
910     batch.samples.push_back(motionEvent);
911     EventResampleHdr->batches_.push_back(batch);
912     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->FindBatch(deviceId, source));
913     deviceId = 5;
914     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->FindBatch(deviceId, source));
915     source = 6;
916     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->FindBatch(deviceId, source));
917 }
918 
919 /**
920  * @tc.name: EventResampleTest_FindTouchState_001
921  * @tc.desc: Test the funcation FindTouchState
922  * @tc.type: FUNC
923  * @tc.require:
924  */
925 HWTEST_F(EventResampleTest, EventResampleTest_FindTouchState_001, TestSize.Level1)
926 {
927     CALL_TEST_DEBUG;
928     int32_t deviceId = 1;
929     int32_t source = 2;
930     EventResample::TouchState ts;
931     ts.deviceId = 1;
932     ts.source = 2;
933     ts.historyCurrent = 3;
934     ts.historySize = 4;
935     EventResampleHdr->touchStates_.push_back(ts);
936     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->FindTouchState(deviceId, source));
937     deviceId = 5;
938     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->FindTouchState(deviceId, source));
939     source = 6;
940     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->FindTouchState(deviceId, source));
941 }
942 
943 /**
944  * @tc.name: EventResampleTest_RewriteMessage_001
945  * @tc.desc: Test the funcation RewriteMessage
946  * @tc.type: FUNC
947  * @tc.require:
948  */
949 HWTEST_F(EventResampleTest, EventResampleTest_RewriteMessage_001, TestSize.Level1)
950 {
951     CALL_TEST_DEBUG;
952     EventResample::TouchState state;
953     EventResample::MotionEvent event;
954     EventResample::Pointer p;
955     p.coordX = 20;
956     p.coordY = 30;
957     p.toolType = 2;
958     p.id = 5;
959     event.pointers.insert(std::make_pair(1, p));
960     EventResample::TouchState ts;
961     ts.deviceId = 3;
962     ts.source = 2;
963     ts.historyCurrent = 3;
964     ts.historySize = 4;
965     EventResampleHdr->touchStates_.push_back(ts);
966     event.actionTime = 10;
967     state.lastResample.actionTime = 5;
968     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->RewriteMessage(state, event));
969     event.actionTime = 5;
970     state.lastResample.actionTime = 10;
971     ASSERT_NO_FATAL_FAILURE(EventResampleHdr->RewriteMessage(state, event));
972 }
973 } // namespace MMI
974 } // namespace OHOS
975