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 <cstdio>
17 #include <gtest/gtest.h>
18 
19 #include "event_statistic.h"
20 #include "mmi_log.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "EventStatisticTest"
24 
25 namespace OHOS {
26 namespace MMI {
27 using namespace testing;
28 using namespace testing::ext;
29 
30 class EventStatisticTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36 };
37 
SetUpTestCase(void)38 void EventStatisticTest::SetUpTestCase(void)
39 {}
40 
TearDownTestCase(void)41 void EventStatisticTest::TearDownTestCase(void)
42 {}
43 
SetUp()44 void EventStatisticTest::SetUp()
45 {}
46 
TearDown()47 void EventStatisticTest::TearDown()
48 {}
49 
50 /**
51  * @tc.name: EventStatisticTest_PushPointerEvent_01
52  * @tc.desc: Test PushPointerEvent
53  * @tc.type: FUNC
54  * @tc.require:
55  */
HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_01, TestSize.Level1)56 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_01, TestSize.Level1)
57 {
58     CALL_TEST_DEBUG;
59     EventStatistic eventStatistic;
60     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
61     ASSERT_NE(pointerEvent, nullptr);
62     pointerEvent->pointerAction_ = PointerEvent::POINTER_ACTION_MOVE;
63     pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
64     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
65 }
66 
67 /**
68  * @tc.name: EventStatisticTest_PushPointerEvent_02
69  * @tc.desc: Test PushPointerEvent
70  * @tc.type: FUNC
71  * @tc.require:
72  */
HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_02, TestSize.Level1)73 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent_02, TestSize.Level1)
74 {
75     CALL_TEST_DEBUG;
76     EventStatistic eventStatistic;
77     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
78     ASSERT_NE(pointerEvent, nullptr);
79     pointerEvent->pointerAction_ = PointerEvent::POINTER_ACTION_UP;
80     pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE);
81     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
82 }
83 
84 /**
85  * @tc.name: EventStatisticTest_PopEvent_02
86  * @tc.desc: Test PopEvent
87  * @tc.type: FUNC
88  * @tc.require:
89  */
HWTEST_F(EventStatisticTest, EventStatisticTest_PopEvent_02, TestSize.Level1)90 HWTEST_F(EventStatisticTest, EventStatisticTest_PopEvent_02, TestSize.Level1)
91 {
92     CALL_TEST_DEBUG;
93     EventStatistic eventStatistic;
94     eventStatistic.eventQueue_.push("event1");
95     eventStatistic.eventQueue_.push("event2");
96     eventStatistic.eventQueue_.push("event3");
97     ASSERT_NO_FATAL_FAILURE(eventStatistic.PopEvent());
98 }
99 
100 /**
101  * @tc.name: EventStatisticTest_ConvertTimeToStr_01
102  * @tc.desc: Test ConvertTimeToStr
103  * @tc.type: FUNC
104  * @tc.require:
105  */
HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertTimeToStr_01, TestSize.Level1)106 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertTimeToStr_01, TestSize.Level1)
107 {
108     CALL_TEST_DEBUG;
109     EventStatistic eventStatistic;
110     int64_t timestamp = 50000;
111     ASSERT_NO_FATAL_FAILURE(eventStatistic.ConvertTimeToStr(timestamp));
112 }
113 
114 /**
115  * @tc.name: EventStatisticTest_PushEvent_01
116  * @tc.desc: Test PushEvent
117  * @tc.type: FUNC
118  * @tc.require:
119  */
HWTEST_F(EventStatisticTest, EventStatisticTest_PushEvent_01, TestSize.Level1)120 HWTEST_F(EventStatisticTest, EventStatisticTest_PushEvent_01, TestSize.Level1)
121 {
122     CALL_TEST_DEBUG;
123     EventStatistic eventStatistic;
124     std::shared_ptr<InputEvent> inputEvent = InputEvent::Create();
125     EXPECT_NE(inputEvent, nullptr);
126     EventStatistic::writeFileEnabled_ = true;
127     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEvent(inputEvent));
128     EventStatistic::writeFileEnabled_ = false;
129     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEvent(inputEvent));
130 }
131 } // namespace MMI
132 } // namespace OHOS