• 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 } // namespace
30 
31 class EventStatisticTest : public testing::Test {
32 public:
SetUpTestCase(void)33     static void SetUpTestCase(void) {}
TearDownTestCase(void)34     static void TearDownTestCase(void) {}
SetUp()35     void SetUp() {}
TearDown()36     void TearDown() {}
37 };
38 
39 /**
40  * @tc.name: EventDumpTest_ConvertEventToStr
41  * @tc.desc: Event dump ConvertEventToStr
42  * @tc.type: FUNC
43  * @tc.require:
44  */
45 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertEventToStr, TestSize.Level1)
46 {
47     CALL_TEST_DEBUG;
48     EventStatistic eventStatistic;
49     auto inputEvent = std::make_shared<InputEvent>(3);
50     inputEvent->eventType_ = 3;
51     inputEvent->actionTime_ = 280000000;
52     inputEvent->deviceId_ = 2;
53     inputEvent->sourceType_ = 6;
54     std::string str = "";
55     str = eventStatistic.ConvertEventToStr(inputEvent);
56     ASSERT_FALSE(str.empty());
57 }
58 
59 /**
60  * @tc.name: EventDumpTest_ConvertTimeToStr
61  * @tc.desc: Event dump ConvertTimeToStr
62  * @tc.type: FUNC
63  * @tc.require:
64  */
65 HWTEST_F(EventStatisticTest, EventStatisticTest_ConvertTimeToStr, TestSize.Level1)
66 {
67     CALL_TEST_DEBUG;
68     EventStatistic eventStatistic;
69     int64_t time = -1;
70     std::string str = "";
71     str = eventStatistic.ConvertTimeToStr(time);
72     ASSERT_EQ(str, "1970-01-01 07:59:59");
73 
74     time = 280000000;
75     str = eventStatistic.ConvertTimeToStr(time);
76     ASSERT_EQ(str, "1978-11-16 01:46:40");
77 }
78 
79 /**
80  * @tc.name: EventDumpTest_PushPointerEvent
81  * @tc.desc: Event dump PushPointerEvent
82  * @tc.type: FUNC
83  * @tc.require:
84  */
85 HWTEST_F(EventStatisticTest, EventStatisticTest_PushPointerEvent, TestSize.Level1)
86 {
87     CALL_TEST_DEBUG;
88     EventStatistic eventStatistic;
89     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
90     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_MOVE);
91     pointerEvent->bitwise_ = 0x000040;
92     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
93 
94     pointerEvent->SetAction(PointerEvent::POINTER_ACTION_DOWN);
95     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushPointerEvent(pointerEvent));
96 }
97 
98 /**
99  * @tc.name: EventDumpTest_PushEvent
100  * @tc.desc: Event dump PushEvent
101  * @tc.type: FUNC
102  * @tc.require:
103  */
104 HWTEST_F(EventStatisticTest, EventStatisticTest_PushEvent, TestSize.Level1)
105 {
106     CALL_TEST_DEBUG;
107     EventStatistic eventStatistic;
108     auto inputEvent = std::make_shared<InputEvent>(3);
109     eventStatistic.writeFileEnabled_ = true;
110     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEvent(inputEvent));
111 
112     for (auto i = 0; i < EVENT_OUT_SIZE - 1; i++) {
113         auto inputEvent1 = std::make_shared<InputEvent>(2);
114         eventStatistic.dumperEventList_.push_back(EventStatistic::ConvertEventToStr(inputEvent1));
115     }
116     eventStatistic.writeFileEnabled_ = false;
117     auto inputEvent2 = std::make_shared<InputEvent>(1);
118     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEvent(inputEvent));
119 }
120 
121 /**
122  * @tc.name: EventDumpTest_Dump
123  * @tc.desc: Event dump Dump
124  * @tc.type: FUNC
125  * @tc.require:
126  */
127 HWTEST_F(EventStatisticTest, EventStatisticTest_Dump, TestSize.Level1)
128 {
129     CALL_TEST_DEBUG;
130     EventStatistic eventStatistic;
131     int32_t fd = 0;
132     std::vector<std::string> dumpStr;
133     for (auto i = 0; i < 5; i++) {
134         std::string str = "EventStatistic Test Dump ";
135         eventStatistic.dumperEventList_.push_back(str);
136         dumpStr.push_back(str);
137     }
138     ASSERT_NO_FATAL_FAILURE(eventStatistic.Dump(fd, dumpStr));
139 }
140 
141 /**
142  * @tc.name: EventDumpTest_PopEvent
143  * @tc.desc: Event dump PopEvent
144  * @tc.type: FUNC
145  * @tc.require:
146  */
147 HWTEST_F(EventStatisticTest, EventStatisticTest_PopEvent, TestSize.Level1)
148 {
149     CALL_TEST_DEBUG;
150     EventStatistic eventStatistic;
151     auto inputEvent = std::make_shared<InputEvent>(3);
152     eventStatistic.writeFileEnabled_ = true;
153     ASSERT_NO_FATAL_FAILURE(eventStatistic.PushEvent(inputEvent));
154     std::string str = "";
155     str = eventStatistic.PopEvent();
156     ASSERT_TRUE(!str.empty());
157 }
158 } // OHOS
159 } // MMI