• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 
18 #include "event_statistic.h"
19 #include "mmi_log.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "EventStatisticTest"
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 using namespace testing::ext;
28 constexpr int32_t EVENT_OUT_SIZE { 30 };
29 constexpr int32_t POINTER_RECORD_MAX_SIZE { 100 };
30 const char* EVENT_FILE_NAME = "/data/service/el1/public/multimodalinput/multimodal_event.dmp";
31 const char* EVENT_FILE_NAME_HISTORY = "/data/service/el1/public/multimodalinput/multimodal_event_history.dmp";
32 constexpr int32_t FILE_MAX_SIZE = 100 * 1024 * 1024;
33 } // namespace
34 
35 class EventStatisticTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void) {}
TearDownTestCase(void)38     static void TearDownTestCase(void) {}
SetUp()39     void SetUp() {}
TearDown()40     void TearDown() {}
41 };
42 
43 /**
44  * @tc.name: EventDumpTest_ConvertInputEventToStr
45  * @tc.desc: Event dump ConvertInputEventToStr
46  * @tc.type: FUNC
47  * @tc.require:
48  */
49 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertInputEventToStr, TestSize.Level1)
50 {
51     CALL_TEST_DEBUG;
52     EventStatistic eventStatistic;
53     auto inputEvent = std::make_shared<InputEvent>(3);
54     inputEvent->eventType_ = 3;
55     inputEvent->actionTime_ = 280000000;
56     inputEvent->deviceId_ = 2;
57     inputEvent->sourceType_ = 6;
58     std::string str = "";
59     str = eventStatistic.ConvertInputEventToStr(inputEvent);
60     ASSERT_FALSE(str.empty());
61 }
62 
63 /**
64  * @tc.name: EventDumpTest_ConvertTimeToStr
65  * @tc.desc: Event dump ConvertTimeToStr
66  * @tc.type: FUNC
67  * @tc.require:
68  */
69 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertTimeToStr, TestSize.Level1)
70 {
71     CALL_TEST_DEBUG;
72     EventStatistic eventStatistic;
73     int64_t time = -1;
74     std::string str = "";
75     str = eventStatistic.ConvertTimeToStr(time);
76     ASSERT_EQ(str, "1970-01-01 07:59:59");
77 
78     time = 280000000;
79     str = eventStatistic.ConvertTimeToStr(time);
80     ASSERT_EQ(str, "1978-11-16 01:46:40");
81 }
82 
83 /**
84  * @tc.name: EventDumpTest_PushPointerEvent
85  * @tc.desc: Event dump PushPointerEvent
86  * @tc.type: FUNC
87  * @tc.require:
88  */
89 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent, TestSize.Level1)
90 {
91     CALL_TEST_DEBUG;
92     EventStatistic eventStatistic;
93     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
94     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_MOVE);
95     pointerEvent->bitwise_ = 0x000040;
96     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
97 
98     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_UP);
99     pointerEvent->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
100     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
101 
102     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_DOWN);
103     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
104 }
105 
106 /**
107  * @tc.name: EventDumpTest_PushKeyEvent
108  * @tc.desc: Event dump PushKeyEvent
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(EventStatisticTest, EventStatisticTest_PushKeyEvent, TestSize.Level1)
113 {
114     CALL_TEST_DEBUG;
115     EventStatistic eventStatistic;
116     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
117     keyEvent->SetAction(KeyEvent::KEY_ACTION_DOWN);
118     keyEvent->bitwise_ = 0x000040;
119     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(keyEvent));
120 
121     keyEvent->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
122     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(keyEvent));
123 
124     keyEvent->SetAction(KeyEvent::KEY_ACTION_UP);
125     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(keyEvent));
126 }
127 
128 /**
129  * @tc.name: EventDumpTest_PushSwitchEvent
130  * @tc.desc: Event dump PushSwitchEvent
131  * @tc.type: FUNC
132  * @tc.require:
133  */
134 HWTEST_F(EventStatisticTest, EventStatisticTest_PushSwitchEvent, TestSize.Level1)
135 {
136     CALL_TEST_DEBUG;
137     EventStatistic eventStatistic;
138     std::shared_ptr<SwitchEvent> switchEvent = std::make_shared<SwitchEvent>(0);
139     switchEvent->SetSwitchType(SwitchEvent::SWITCH_DEFAULT);
140     switchEvent->bitwise_ = 0x000040;
141     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushSwitchEvent(switchEvent));
142 
143     switchEvent->SetSwitchType(SwitchEvent::SWITCH_TABLET);
144     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushSwitchEvent(switchEvent));
145 }
146 
147 /**
148  * @tc.name: EventDumpTest_PushEventStr
149  * @tc.desc: Event dump PushEventStr
150  * @tc.type: FUNC
151  * @tc.require:
152  */
153 HWTEST_F(EventStatisticTest, EventStatisticTest_PushEventStr, TestSize.Level1)
154 {
155     CALL_TEST_DEBUG;
156     EventStatistic eventStatistic;
157     eventStatistic.writeFileEnabled_ = true;
158     std::string str = "test_push_event_str";
159     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEventStr(str));
160 
161     for (auto i = 0; i < EVENT_OUT_SIZE - 1; i++) {
162         auto inputEvent1 = std::make_shared<InputEvent>(2);
163         eventStatistic.dumperEventList_.push_back(EventStatistic::ConvertInputEventToStr(inputEvent1));
164     }
165     eventStatistic.writeFileEnabled_ = false;
166     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEventStr(str));
167 }
168 
169 /**
170  * @tc.name: EventDumpTest_Dump
171  * @tc.desc: Event dump Dump
172  * @tc.type: FUNC
173  * @tc.require:
174  */
175 HWTEST_F(EventStatisticTest, EventStatisticTest_Dump, TestSize.Level1)
176 {
177     CALL_TEST_DEBUG;
178     EventStatistic eventStatistic;
179     int32_t fd = 0;
180     std::vector<std::string> dumpStr;
181     for (auto i = 0; i < 5; i++) {
182         std::string str = "EventStatistic Test Dump ";
183         eventStatistic.dumperEventList_.push_back(str);
184         dumpStr.push_back(str);
185     }
186     ASSERT_NO_FATAL_FAILURE(eventStatistic.Dump(fd, dumpStr));
187 }
188 
189 /**
190  * @tc.name: EventDumpTest_ConvertEventTypeToString
191  * @tc.desc: Event dump ConvertEventTypeToString
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertEventTypeToString, TestSize.Level1)
196 {
197     CALL_TEST_DEBUG;
198     EventStatistic eventStatistic;
199     int32_t eventType = InputEvent::EVENT_TYPE_BASE;
200     ASSERT_STREQ(eventStatistic.ConvertEventTypeToString(eventType), "base");
201 
202     eventType = InputEvent::EVENT_TYPE_KEY;
203     ASSERT_STREQ(eventStatistic.ConvertEventTypeToString(eventType), "key");
204 
205     eventType = InputEvent::EVENT_TYPE_POINTER;
206     ASSERT_STREQ(eventStatistic.ConvertEventTypeToString(eventType), "pointer");
207 
208     eventType = InputEvent::EVENT_TYPE_AXIS;
209     ASSERT_STREQ(eventStatistic.ConvertEventTypeToString(eventType), "axis");
210 
211     eventType = InputEvent::EVENT_TYPE_FINGERPRINT;
212     ASSERT_STREQ(eventStatistic.ConvertEventTypeToString(eventType), "fingerprint");
213 
214     eventType = -1;
215     ASSERT_STREQ(eventStatistic.ConvertEventTypeToString(eventType), "unknown");
216 }
217 
218 /**
219  * @tc.name: EventDumpTest_ConvertSourceTypeToString
220  * @tc.desc: Event dump ConvertSourceTypeToString
221  * @tc.type: FUNC
222  * @tc.require:
223  */
224 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertSourceTypeToString, TestSize.Level1)
225 {
226     CALL_TEST_DEBUG;
227     EventStatistic eventStatistic;
228     int32_t sourceType = InputEvent::SOURCE_TYPE_MOUSE;
229     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "mouse");
230 
231     sourceType = InputEvent::SOURCE_TYPE_TOUCHSCREEN;
232     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "touch-screen");
233 
234     sourceType = InputEvent::SOURCE_TYPE_TOUCHPAD;
235     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "touch-pad");
236 
237     sourceType = InputEvent::SOURCE_TYPE_JOYSTICK;
238     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "joystick");
239 
240     sourceType = InputEvent::SOURCE_TYPE_FINGERPRINT;
241     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "fingerprint");
242 
243     sourceType = InputEvent::SOURCE_TYPE_CROWN;
244     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "crown");
245 
246     sourceType = InputEvent::EVENT_FLAG_NONE;
247     ASSERT_STREQ(eventStatistic.ConvertSourceTypeToString(sourceType), "unknown");
248 }
249 
250 /**
251  * @tc.name: EventDumpTest_ConvertPointerActionToString_001
252  * @tc.desc: Event dump ConvertPointerActionToString_001
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertPointerActionToString_001, TestSize.Level1)
257 {
258     CALL_TEST_DEBUG;
259     EventStatistic eventStatistic;
260     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
261     pointerEvent->bitwise_ = 0x000040;
262     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
263     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, 0);
264     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "axis-begin");
265     pointerEvent->ClearAxisValue();
266 
267     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, 0);
268     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "axis-begin");
269     pointerEvent->ClearAxisValue();
270 
271     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, 0);
272     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "pinch-begin");
273     pointerEvent->ClearAxisValue();
274 }
275 
276 /**
277  * @tc.name: EventDumpTest_ConvertPointerActionToString_002
278  * @tc.desc: Event dump ConvertPointerActionToString_002
279  * @tc.type: FUNC
280  * @tc.require:
281  */
282 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertPointerActionToString_002, TestSize.Level1)
283 {
284     CALL_TEST_DEBUG;
285     EventStatistic eventStatistic;
286     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
287     pointerEvent->bitwise_ = 0x000040;
288     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
289     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, 0);
290     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "axis-update");
291     pointerEvent->ClearAxisValue();
292 
293     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, 0);
294     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "axis-update");
295     pointerEvent->ClearAxisValue();
296 
297     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, 0);
298     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "pinch-update");
299     pointerEvent->ClearAxisValue();
300 }
301 
302 /**
303  * @tc.name: EventDumpTest_ConvertPointerActionToString_003
304  * @tc.desc: Event dump ConvertPointerActionToString_003
305  * @tc.type: FUNC
306  * @tc.require:
307  */
308 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertPointerActionToString_003, TestSize.Level1)
309 {
310     CALL_TEST_DEBUG;
311     EventStatistic eventStatistic;
312     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
313     pointerEvent->bitwise_ = 0x000040;
314     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
315     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, 0);
316     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "axis-end");
317     pointerEvent->ClearAxisValue();
318 
319     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, 0);
320     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "axis-end");
321     pointerEvent->ClearAxisValue();
322 
323     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, 0);
324     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "pinch-end");
325     pointerEvent->ClearAxisValue();
326 }
327 
328 /**
329  * @tc.name: EventDumpTest_ConvertPointerActionToString_004
330  * @tc.desc: Event dump ConvertPointerActionToString_004
331  * @tc.type: FUNC
332  * @tc.require:
333  */
334 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertPointerActionToString_004, TestSize.Level1)
335 {
336     CALL_TEST_DEBUG;
337     EventStatistic eventStatistic;
338     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
339     pointerEvent->bitwise_ = 0x000040;
340     pointerEvent->SetPointerAction(PointerEvent::TOUCH_ACTION_SWIPE_UP);
341     ASSERT_STREQ(eventStatistic.ConvertPointerActionToString(pointerEvent), "touch-swipe-up");
342 }
343 
344 /**
345  * @tc.name: EventDumpTest_ConvertKeyActionToString
346  * @tc.desc: Event dump ConvertKeyActionToString
347  * @tc.type: FUNC
348  * @tc.require:
349  */
350 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertKeyActionToString, TestSize.Level1)
351 {
352     CALL_TEST_DEBUG;
353     EventStatistic eventStatistic;
354     ASSERT_STREQ(eventStatistic.ConvertKeyActionToString(KeyEvent::KEY_ACTION_CANCEL), "key_action_cancel");
355 
356     ASSERT_STREQ(eventStatistic.ConvertKeyActionToString(KeyEvent::INTENTION_UNKNOWN), "unknown");
357 }
358 
359 /**
360  * @tc.name: EventDumpTest_ConvertSwitchTypeToString
361  * @tc.desc: Event dump ConvertSwitchTypeToString
362  * @tc.type: FUNC
363  * @tc.require:
364  */
365 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertSwitchTypeToString, TestSize.Level1)
366 {
367     CALL_TEST_DEBUG;
368     EventStatistic eventStatistic;
369     int32_t switchType = SwitchEvent::SWITCH_DEFAULT;
370     ASSERT_STREQ(eventStatistic.ConvertSwitchTypeToString(switchType), "switch_default");
371 
372     switchType = SwitchEvent::SWITCH_LID;
373     ASSERT_STREQ(eventStatistic.ConvertSwitchTypeToString(switchType), "switch_lid");
374 
375     switchType = SwitchEvent::SWITCH_TABLET;
376     ASSERT_STREQ(eventStatistic.ConvertSwitchTypeToString(switchType), "switch_tablet");
377 
378     switchType = SwitchEvent::SWITCH_PRIVACY;
379     ASSERT_STREQ(eventStatistic.ConvertSwitchTypeToString(switchType), "switch_privacy");
380 
381     switchType = -1;
382     ASSERT_STREQ(eventStatistic.ConvertSwitchTypeToString(switchType), "unknown");
383 }
384 
385 /**
386  * @tc.name: EventDumpTest_PushPointerRecord
387  * @tc.desc: Event dump PushPointerRecord
388  * @tc.type: FUNC
389  * @tc.require:
390  */
391 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerRecord, TestSize.Level1)
392 {
393     CALL_TEST_DEBUG;
394     EventStatistic eventStatistic;
395     auto pointerEvent = PointerEvent::Create();
396     PointerEvent::PointerItem pointerItem;
397     pointerItem.SetPressure(0);
398     pointerItem.SetTiltX(0);
399     pointerItem.SetTiltY(0);
400     pointerEvent->AddPointerItem(pointerItem);
401     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerRecord(pointerEvent));
402     for (auto i = 0; i <= POINTER_RECORD_MAX_SIZE; ++i) {
403         auto pointerEvent = PointerEvent::Create();
404         eventStatistic.PushPointerRecord(pointerEvent);
405     }
406     EXPECT_EQ(eventStatistic.pointerRecordDeque_.size(), POINTER_RECORD_MAX_SIZE);
407 }
408 
409 /**
410  * @tc.name: EventDumpTest_QueryPointerRecord_001
411  * @tc.desc: Event dump QueryPointerRecord
412  * @tc.type: FUNC
413  * @tc.require:
414  */
415 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_001, TestSize.Level1)
416 {
417     CALL_TEST_DEBUG;
418     EventStatistic eventStatistic;
419     int32_t count = -1;
420     std::vector<std::shared_ptr<PointerEvent>> pointerList;
421     eventStatistic.pointerRecordDeque_.clear();
422     EXPECT_EQ(eventStatistic.QueryPointerRecord(count, pointerList), RET_OK);
423 }
424 
425 /**
426  * @tc.name: EventDumpTest_QueryPointerRecord_002
427  * @tc.desc: Event dump QueryPointerRecord
428  * @tc.type: FUNC
429  * @tc.require:
430  */
431 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_002, TestSize.Level1)
432 {
433     CALL_TEST_DEBUG;
434     EventStatistic eventStatistic;
435     int32_t count = 0;
436     std::vector<std::shared_ptr<PointerEvent>> pointerList;
437     auto pointerEvent = PointerEvent::Create();
438     eventStatistic.PushPointerRecord(pointerEvent);
439     EXPECT_EQ(eventStatistic.QueryPointerRecord(count, pointerList), RET_OK);
440 }
441 
442 /**
443  * @tc.name: EventDumpTest_QueryPointerRecord_003
444  * @tc.desc: Event dump QueryPointerRecord
445  * @tc.type: FUNC
446  * @tc.require:
447  */
448 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_003, TestSize.Level1)
449 {
450     CALL_TEST_DEBUG;
451     EventStatistic eventStatistic;
452     int32_t count = 30;
453     std::vector<std::shared_ptr<PointerEvent>> pointerList;
454     eventStatistic.pointerRecordDeque_.clear();
455     EXPECT_EQ(eventStatistic.QueryPointerRecord(count, pointerList), RET_OK);
456 }
457 
458 /**
459  * @tc.name: EventDumpTest_QueryPointerRecord_004
460  * @tc.desc: Event dump QueryPointerRecord
461  * @tc.type: FUNC
462  * @tc.require:
463  */
464 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_004, TestSize.Level1)
465 {
466     CALL_TEST_DEBUG;
467     EventStatistic eventStatistic;
468     int32_t count = 100;
469     std::vector<std::shared_ptr<PointerEvent>> pointerList;
470     auto pointerEvent = PointerEvent::Create();
471     eventStatistic.PushPointerRecord(pointerEvent);
472     EXPECT_EQ(eventStatistic.QueryPointerRecord(count, pointerList), RET_OK);
473 }
474 
475 /**
476  * @tc.name: EventDumpTest_QueryPointerRecord_005
477  * @tc.desc: Event dump QueryPointerRecord
478  * @tc.type: FUNC
479  * @tc.require:
480  */
481 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_005, TestSize.Level1)
482 {
483     CALL_TEST_DEBUG;
484     EventStatistic eventStatistic;
485     int32_t count = 101;
486     std::vector<std::shared_ptr<PointerEvent>> pointerList;
487     auto pointerEvent = PointerEvent::Create();
488     pointerEvent->AddFlag(InputEvent::EVENT_FLAG_SIMULATE);
489     eventStatistic.PushPointerRecord(pointerEvent);
490     EXPECT_EQ(eventStatistic.QueryPointerRecord(count, pointerList), RET_OK);
491 }
492 
493 /**
494  * @tc.name: EventStatisticTest_PushPointerEvent_001
495  * @tc.desc: Verify PushPointerEvent with nullptr eventPtr
496  * @tc.type: FUNC
497  * @tc.require:
498  */
499 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_001, TestSize.Level1)
500 {
501     CALL_TEST_DEBUG;
502     EventStatistic eventStatistic;
503     std::shared_ptr<PointerEvent> nullEvent = nullptr;
504     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(nullEvent));
505 }
506 
507 /**
508  * @tc.name: EventStatisticTest_PushPointerEvent_002
509  * @tc.desc: Verify PushPointerEvent with pointerAction that should be filtered (POINTER_ACTION_MOVE)
510  * @tc.type: FUNC
511  * @tc.require:
512  */
513 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_002, TestSize.Level1)
514 {
515     CALL_TEST_DEBUG;
516     EventStatistic eventStatistic;
517     auto pointerEvent = PointerEvent::Create();
518     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_MOVE);
519     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
520 }
521 
522 /**
523  * @tc.name: EventStatisticTest_PushPointerEvent_003
524  * @tc.desc: Verify PushPointerEvent with privacy mode flag
525  * @tc.type: FUNC
526  * @tc.require:
527  */
528 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_003, TestSize.Level1)
529 {
530     CALL_TEST_DEBUG;
531     EventStatistic eventStatistic;
532     auto pointerEvent = PointerEvent::Create();
533     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_UP);
534     pointerEvent->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
535     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
536 }
537 
538 /**
539  * @tc.name: EventStatisticTest_PushPointerEvent_004
540  * @tc.desc: Verify PushPointerEvent with multiple pointer items and pressed buttons
541  * @tc.type: FUNC
542  * @tc.require:
543  */
544 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_004, TestSize.Level1)
545 {
546     CALL_TEST_DEBUG;
547     EventStatistic eventStatistic;
548     auto pointerEvent = PointerEvent::Create();
549     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_DOWN);
550     PointerEvent::PointerItem item1;
551     item1.SetDisplayX(10);
552     item1.SetDisplayY(20);
553     item1.SetPressure(1.0f);
554     pointerEvent->AddPointerItem(item1);
555     PointerEvent::PointerItem item2;
556     item2.SetDisplayX(30);
557     item2.SetDisplayY(40);
558     item2.SetPressure(0.5f);
559     pointerEvent->AddPointerItem(item2);
560     pointerEvent->SetPressedKeys({1, 2});
561     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
562 }
563 
564 /**
565  * @tc.name: EventStatisticTest_PushPointerEvent_005
566  * @tc.desc: Verify PushPointerEvent with SOURCE_TYPE_TOUCHSCREEN
567  * @tc.type: FUNC
568  * @tc.require:
569  */
570 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_005, TestSize.Level1)
571 {
572     CALL_TEST_DEBUG;
573     EventStatistic eventStatistic;
574     auto pointerEvent = PointerEvent::Create();
575     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_DOWN);
576     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
577     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
578 }
579 
580 /**
581  * @tc.name: EventStatisticTest_PushKeyEvent_001
582  * @tc.desc: Verify PushKeyEvent with nullptr eventPtr
583  * @tc.type: FUNC
584  * @tc.require:
585  */
586 HWTEST_F(EventStatisticTest, EventStatisticTest_PushKeyEvent_001, TestSize.Level1)
587 {
588     CALL_TEST_DEBUG;
589     EventStatistic eventStatistic;
590     std::shared_ptr<KeyEvent> nullKeyEvent = nullptr;
591     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(nullKeyEvent));
592 }
593 
594 /**
595  * @tc.name: EventStatisticTest_PushKeyEvent_002
596  * @tc.desc: Verify PushKeyEvent with KEY_ACTION_DOWN
597  * @tc.type: FUNC
598  * @tc.require:
599  */
600 HWTEST_F(EventStatisticTest, EventStatisticTest_PushKeyEvent_002, TestSize.Level1)
601 {
602     CALL_TEST_DEBUG;
603     EventStatistic eventStatistic;
604     auto keyEvent = KeyEvent::Create();
605     keyEvent->SetAction(KeyEvent::KEY_ACTION_DOWN);
606     keyEvent->SetKeyCode(65); // 'A'
607     keyEvent->bitwise_ = 0x000040;
608     KeyEvent::KeyItem keyItem;
609     keyItem.SetDeviceId(1);
610     keyItem.SetKeyCode(65);
611     keyItem.SetDownTime(100);
612     keyItem.SetUnicode(65);
613     keyItem.SetPressed(true);
614     keyEvent->AddKeyItem(keyItem);
615     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(keyEvent));
616 }
617 
618 /**
619  * @tc.name: EventStatisticTest_PushKeyEvent_003
620  * @tc.desc: Verify PushKeyEvent with privacy mode flag
621  * @tc.type: FUNC
622  * @tc.require:
623  */
624 HWTEST_F(EventStatisticTest, EventStatisticTest_PushKeyEvent_003, TestSize.Level1)
625 {
626     CALL_TEST_DEBUG;
627     EventStatistic eventStatistic;
628     auto keyEvent = KeyEvent::Create();
629     keyEvent->SetAction(KeyEvent::KEY_ACTION_DOWN);
630     keyEvent->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE); // 隐私模式
631     keyEvent->SetKeyCode(66); // 'B'
632     KeyEvent::KeyItem keyItem;
633     keyItem.SetDeviceId(2);
634     keyItem.SetKeyCode(66);
635     keyItem.SetDownTime(200);
636     keyItem.SetUnicode(66);
637     keyItem.SetPressed(false);
638     keyEvent->AddKeyItem(keyItem);
639     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(keyEvent));
640 }
641 
642 /**
643  * @tc.name: EventStatisticTest_PushKeyEvent_004
644  * @tc.desc: Verify PushKeyEvent with KEY_ACTION_UP and multiple key items
645  * @tc.type: FUNC
646  * @tc.require:
647  */
648 HWTEST_F(EventStatisticTest, EventStatisticTest_PushKeyEvent_004, TestSize.Level1)
649 {
650     CALL_TEST_DEBUG;
651     EventStatistic eventStatistic;
652     auto keyEvent = KeyEvent::Create();
653     keyEvent->SetAction(KeyEvent::KEY_ACTION_UP);
654     keyEvent->SetKeyCode(67); // 'C'
655     KeyEvent::KeyItem keyItem1;
656     keyItem1.SetDeviceId(1);
657     keyItem1.SetKeyCode(67);
658     keyItem1.SetDownTime(300);
659     keyItem1.SetUnicode(67);
660     keyItem1.SetPressed(true);
661     keyEvent->AddKeyItem(keyItem1);
662     KeyEvent::KeyItem keyItem2;
663     keyItem2.SetDeviceId(2);
664     keyItem2.SetKeyCode(68);
665     keyItem2.SetDownTime(400);
666     keyItem2.SetUnicode(68);
667     keyItem2.SetPressed(false);
668     keyEvent->AddKeyItem(keyItem2);
669     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushKeyEvent(keyEvent));
670 }
671 
672 /**
673  * @tc.name: EventStatisticTest_QueryPointerRecord_006
674  * @tc.desc: Verify QueryPointerRecord with multiple pointer items
675  * @tc.type: FUNC
676  * @tc.require:
677  */
678 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_006, TestSize.Level1)
679 {
680     CALL_TEST_DEBUG;
681     EventStatistic eventStatistic;
682     std::vector<std::shared_ptr<PointerEvent>> pointerList;
683 
684     auto pointerEvent = PointerEvent::Create();
685     pointerEvent->SetActionTime(123456);
686     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
687     PointerEvent::PointerItem item1;
688     item1.SetPressure(1.0f);
689     item1.SetTiltX(10.0f);
690     item1.SetTiltY(30.0f);
691     pointerEvent->AddPointerItem(item1);
692     PointerEvent::PointerItem item2;
693     item2.SetPressure(0.5f);
694     item2.SetTiltX(20.0f);
695     item2.SetTiltY(40.0f);
696     pointerEvent->AddPointerItem(item2);
697     eventStatistic.PushPointerRecord(pointerEvent);
698     EXPECT_EQ(eventStatistic.QueryPointerRecord(1, pointerList), RET_OK);
699     EXPECT_EQ(pointerList.size(), 1u);
700     EXPECT_EQ(pointerList[0]->GetAllPointerItems().size(), 1u);
701 }
702 
703 /**
704  * @tc.name: EventStatisticTest_QueryPointerRecord_007
705  * @tc.desc: Verify QueryPointerRecord with simulate flag (EVENT_FLAG_SIMULATE)
706  * @tc.type: FUNC
707  * @tc.require:
708  */
709 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_007, TestSize.Level1)
710 {
711     CALL_TEST_DEBUG;
712     EventStatistic eventStatistic;
713     std::vector<std::shared_ptr<PointerEvent>> pointerList;
714     auto pointerEvent = PointerEvent::Create();
715     pointerEvent->SetActionTime(987654);
716     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
717     pointerEvent->AddFlag(InputEvent::EVENT_FLAG_SIMULATE);
718     PointerEvent::PointerItem item;
719     item.SetPressure(0.8f);
720     item.SetTiltX(15.0f);
721     item.SetTiltY(25.0f);
722     pointerEvent->AddPointerItem(item);
723     eventStatistic.PushPointerRecord(pointerEvent);
724     EXPECT_EQ(eventStatistic.QueryPointerRecord(1, pointerList), RET_OK);
725     EXPECT_EQ(pointerList.size(), 1u);
726     EXPECT_TRUE(pointerList[0]->HasFlag(InputEvent::EVENT_FLAG_SIMULATE));
727 }
728 
729 /**
730  * @tc.name: EventStatisticTest_QueryPointerRecord_008
731  * @tc.desc: Verify QueryPointerRecord when count exceeds pointerRecordDeque_ size
732  * @tc.type: FUNC
733  * @tc.require:
734  */
735 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_008, TestSize.Level1)
736 {
737     CALL_TEST_DEBUG;
738     EventStatistic eventStatistic;
739     eventStatistic.pointerRecordDeque_.clear();
740     std::vector<std::shared_ptr<PointerEvent>> pointerList;
741     for (int i = 0; i < 3; i++) {
742         auto pointerEvent = PointerEvent::Create();
743         pointerEvent->SetAction(PointerEvent::POINTER_ACTION_DOWN);
744         eventStatistic.PushPointerRecord(pointerEvent);
745     }
746     EXPECT_EQ(eventStatistic.QueryPointerRecord(10, pointerList), RET_OK);
747     EXPECT_EQ(pointerList.size(), 3u);
748 }
749 
750 /**
751  * @tc.name: EventStatisticTest_QueryPointerRecord_009
752  * @tc.desc: Verify QueryPointerRecord with zero count (should return RET_OK and not crash)
753  * @tc.type: FUNC
754  * @tc.require:
755  */
756 HWTEST_F(EventStatisticTest, EventStatisticTest_QueryPointerRecord_009, TestSize.Level1)
757 {
758     CALL_TEST_DEBUG;
759     EventStatistic eventStatistic;
760     std::vector<std::shared_ptr<PointerEvent>> pointerList;
761     auto pointerEvent = PointerEvent::Create();
762     eventStatistic.PushPointerRecord(pointerEvent);
763     EXPECT_EQ(eventStatistic.QueryPointerRecord(0, pointerList), RET_OK);
764     EXPECT_TRUE(pointerList.empty());
765 }
766 
767 /**
768  * @tc.name: EventStatisticTest_PopEvent_001
769  * @tc.desc: Verify PopEvent returns the correct event string when queue is not empty.
770  * @tc.type: FUNC
771  * @tc.require:
772  */
773 HWTEST_F(EventStatisticTest, EventStatisticTest_PopEvent_001, TestSize.Level1)
774 {
775     CALL_TEST_DEBUG;
776     EventStatistic eventStatistic;
777     {
778         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
779         while (!eventStatistic.eventQueue_.empty()) {
780             eventStatistic.eventQueue_.pop();
781         }
782         eventStatistic.eventQueue_.push("TestEvent1");
783         eventStatistic.queueCondition_.notify_one();
784     }
785     std::string eventStr = eventStatistic.PopEvent();
786     EXPECT_EQ(eventStr, "TestEvent1");
787 }
788 
789 /**
790  * @tc.name: EventStatisticTest_PopEvent_002
791  * @tc.desc: Verify PopEvent blocks until eventQueue_ is filled (using thread).
792  * @tc.type: FUNC
793  * @tc.require:
794  */
795 HWTEST_F(EventStatisticTest, EventStatisticTest_PopEvent_002, TestSize.Level1)
796 {
797     CALL_TEST_DEBUG;
798     EventStatistic eventStatistic;
799     {
800         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
801         while (!eventStatistic.eventQueue_.empty()) {
802             eventStatistic.eventQueue_.pop();
803         }
804     }
__anon6337f9e50202() 805     std::thread producer([&eventStatistic]() {
806         std::this_thread::sleep_for(std::chrono::milliseconds(50));
807         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
808         eventStatistic.eventQueue_.push("DelayedEvent");
809         eventStatistic.queueCondition_.notify_one();
810     });
811     std::string result = eventStatistic.PopEvent();
812     producer.join();
813     EXPECT_EQ(result, "DelayedEvent");
814 }
815 
816 /**
817  * @tc.name: EventStatisticTest_WriteEventFile_001
818  * @tc.desc: Verify WriteEventFile writes event string into the file
819  * @tc.type: FUNC
820  * @tc.require:
821  */
822 HWTEST_F(EventStatisticTest, EventStatisticTest_WriteEventFile_001, TestSize.Level1)
823 {
824     CALL_TEST_DEBUG;
825     EventStatistic eventStatistic;
826     {
827         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
828         while (!eventStatistic.eventQueue_.empty()) {
829             eventStatistic.eventQueue_.pop();
830         }
831     }
832     unlink(EVENT_FILE_NAME);
833     unlink(EVENT_FILE_NAME_HISTORY);
834     {
835         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
836         eventStatistic.eventQueue_.push("TestEventFile");
837         eventStatistic.queueCondition_.notify_one();
838     }
839     eventStatistic.writeFileEnabled_ = true;
__anon6337f9e50302() 840     std::thread writer([&eventStatistic]() {
841         eventStatistic.WriteEventFile();
842     });
843     std::this_thread::sleep_for(std::chrono::milliseconds(100));
844     eventStatistic.writeFileEnabled_ = false;
845     {
846         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
847         eventStatistic.eventQueue_.push("Exit");
848         eventStatistic.queueCondition_.notify_one();
849     }
850     writer.join();
851     std::ifstream file(EVENT_FILE_NAME);
852     ASSERT_TRUE(file.is_open());
853     std::string content;
854     std::getline(file, content);
855     file.close();
856     EXPECT_EQ(content, "TestEventFile");
857     unlink(EVENT_FILE_NAME);
858     unlink(EVENT_FILE_NAME_HISTORY);
859 }
860 
861 /**
862  * @tc.name: EventStatisticTest_WriteEventFile_002
863  * @tc.desc: Verify WriteEventFile rotates file when file exceeds FILE_MAX_SIZE
864  * @tc.type: FUNC
865  * @tc.require:
866  */
867 HWTEST_F(EventStatisticTest, EventStatisticTest_WriteEventFile_002, TestSize.Level1)
868 {
869     CALL_TEST_DEBUG;
870     EventStatistic eventStatistic;
871     {
872         std::ofstream bigFile(EVENT_FILE_NAME);
873         bigFile << std::string(FILE_MAX_SIZE + 10, 'X');
874         bigFile.close();
875     }
876     {
877         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
878         eventStatistic.eventQueue_.push("RotateEvent");
879         eventStatistic.queueCondition_.notify_one();
880     }
881     eventStatistic.writeFileEnabled_ = true;
__anon6337f9e50402() 882     std::thread writer([&eventStatistic]() {
883         eventStatistic.WriteEventFile();
884     });
885     std::this_thread::sleep_for(std::chrono::milliseconds(100));
886     eventStatistic.writeFileEnabled_ = false;
887     {
888         std::lock_guard<std::mutex> lock(eventStatistic.queueMutex_);
889         eventStatistic.eventQueue_.push("Exit");
890         eventStatistic.queueCondition_.notify_one();
891     }
892     writer.join();
893     EXPECT_EQ(access(EVENT_FILE_NAME_HISTORY, F_OK), 0);
894     unlink(EVENT_FILE_NAME);
895     unlink(EVENT_FILE_NAME_HISTORY);
896 }
897 } // OHOS
898 } // MMI