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 <fcntl.h>
17 #include <gtest/gtest.h>
18 
19 #include <cerrno>
20 #include <cstdio>
21 
22 #include "event_dump.h"
23 #include "event_log_helper.h"
24 #include "i_input_windows_manager.h"
25 #include "input_device_manager.h"
26 #include "input_event_handler.h"
27 #include "input_manager.h"
28 #include "input_manager_util.h"
29 #include "mouse_event_normalize.h"
30 #include "multimodal_event_handler.h"
31 #include "system_info.h"
32 
33 #undef MMI_LOG_TAG
34 #define MMI_LOG_TAG "EventDumpTest"
35 
36 namespace OHOS {
37 namespace MMI {
38 namespace {
39 using namespace testing::ext;
40 const std::string TEST_FILE_NAME = "/data/log.log";
41 } // namespace
42 
43 class EventDumpTest : public testing::Test {
44 public:
SetUp()45     void SetUp() override
46     {
47         fd_ = open(TEST_FILE_NAME.c_str(), O_WRONLY);
48     }
49 
TearDown()50     void TearDown() override
51     {
52         close(fd_);
53         fd_ = -1;
54     }
55 
56     int32_t fd_;
57 };
58 
59 /**
60  * @tc.name: EventDumpTest_CheckCount_001
61  * @tc.desc: Event dump CheckCount
62  * @tc.type: FUNC
63  * @tc.require:
64  */
65 HWTEST_F(EventDumpTest, EventDumpTest001, TestSize.Level1)
66 {
67     CALL_TEST_DEBUG;
68     std::vector<std::string> args;
69     int32_t count = 0;
70     MMIEventDump->CheckCount(fd_, args, count);
71     EXPECT_EQ(count, 0);
72 }
73 
74 /**
75  * @tc.name: EventDumpTest_CheckCount_002
76  * @tc.desc: Event dump CheckCount
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(EventDumpTest, EventDumpTest002, TestSize.Level1)
81 {
82     CALL_TEST_DEBUG;
83     std::vector<std::string> args = {"--help"};
84     int32_t count = 0;
85     MMIEventDump->CheckCount(fd_, args, count);
86     MMIEventDump->ParseCommand(fd_, args);
87     EXPECT_EQ(count, 1);
88 }
89 
90 /**
91  * @tc.name: EventDumpTest_CheckCount_003
92  * @tc.desc: Event dump CheckCount
93  * @tc.type: FUNC
94  * @tc.require:
95  */
96 HWTEST_F(EventDumpTest, EventDumpTest003, TestSize.Level1)
97 {
98     CALL_TEST_DEBUG;
99     std::vector<std::string> args = {"-h"};
100     int32_t count = 0;
101     MMIEventDump->CheckCount(fd_, args, count);
102     MMIEventDump->ParseCommand(fd_, args);
103     EXPECT_EQ(count, 1);
104 }
105 
106 /**
107  * @tc.name: EventDumpTest_CheckCount_004
108  * @tc.desc: Event dump CheckCount
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(EventDumpTest, EventDumpTest004, TestSize.Level1)
113 {
114     CALL_TEST_DEBUG;
115     std::vector<std::string> args = {"-abc"};
116     int32_t count = 0;
117     MMIEventDump->CheckCount(fd_, args, count);
118     MMIEventDump->ParseCommand(fd_, args);
119     EXPECT_EQ(count, 3);
120 }
121 
122 /**
123  * @tc.name: EventDumpTest_CheckCount_005
124  * @tc.desc: Event dump CheckCount
125  * @tc.type: FUNC
126  * @tc.require:
127  */
128 HWTEST_F(EventDumpTest, EventDumpTest005, TestSize.Level1)
129 {
130     CALL_TEST_DEBUG;
131     std::vector<std::string> args = {"-a", "--help", "foo", "-bc", "bar"};
132     int32_t count = 0;
133     MMIEventDump->CheckCount(fd_, args, count);
134     MMIEventDump->ParseCommand(fd_, args);
135     MMIEventDump->DumpEventHelp(fd_, args);
136     EXPECT_EQ(count, 4);
137 }
138 
139 /**
140  * @tc.name: EventDumpTest_006
141  * @tc.desc: Event dump InputDeviceManager
142  * @tc.type: FUNC
143  * @tc.require:
144  */
145 HWTEST_F(EventDumpTest, EventDumpTest006, TestSize.Level1)
146 {
147     CALL_TEST_DEBUG;
148     std::vector<std::string> args = {"-d"};
149     int32_t count = 0;
150     MMIEventDump->CheckCount(fd_, args, count);
151     MMIEventDump->ParseCommand(fd_, args);
152     ASSERT_NO_FATAL_FAILURE(INPUT_DEV_MGR->Dump(fd_, args));
153 }
154 
155 /**
156  * @tc.name: EventDumpTest_007
157  * @tc.desc: Event dump DeviceList
158  * @tc.type: FUNC
159  * @tc.require:
160  */
161 HWTEST_F(EventDumpTest, EventDumpTest007, TestSize.Level1)
162 {
163     CALL_TEST_DEBUG;
164     std::vector<std::string> args = {"-l"};
165     int32_t count = 0;
166     MMIEventDump->CheckCount(fd_, args, count);
167     MMIEventDump->ParseCommand(fd_, args);
168     ASSERT_NO_FATAL_FAILURE(INPUT_DEV_MGR->DumpDeviceList(fd_, args));
169 }
170 
171 /**
172  * @tc.name: EventDumpTest_008
173  * @tc.desc: Event dump WindowsManager
174  * @tc.type: FUNC
175  * @tc.require:
176  */
177 HWTEST_F(EventDumpTest, EventDumpTest008, TestSize.Level1)
178 {
179     CALL_TEST_DEBUG;
180     std::vector<std::string> args = {"-w"};
181     int32_t count = 0;
182     MMIEventDump->CheckCount(fd_, args, count);
183     MMIEventDump->ParseCommand(fd_, args);
184     ASSERT_NO_FATAL_FAILURE(WIN_MGR->Dump(fd_, args));
185 }
186 
187 /**
188  * @tc.name: EventDumpTest_009
189  * @tc.desc: Event dump UDSServer
190  * @tc.type: FUNC
191  * @tc.require:
192  */
193 HWTEST_F(EventDumpTest, EventDumpTest009, TestSize.Level1)
194 {
195     CALL_TEST_DEBUG;
196     std::vector<std::string> args = {"-u"};
197     int32_t count = 0;
198     auto udsServer = InputHandler->GetUDSServer();
199     CHKPV(udsServer);
200     MMIEventDump->CheckCount(fd_, args, count);
201     MMIEventDump->ParseCommand(fd_, args);
202     ASSERT_NO_FATAL_FAILURE(udsServer->Dump(fd_, args));
203 }
204 
205 /**
206  * @tc.name: EventDumpTest_010
207  * @tc.desc: Event dump SubscriberHandler
208  * @tc.type: FUNC
209  * @tc.require:
210  */
211 HWTEST_F(EventDumpTest, EventDumpTest010, TestSize.Level1)
212 {
213     CALL_TEST_DEBUG;
214     std::vector<std::string> args = {"-s"};
215     int32_t count = 0;
216     auto subscriberHandler = InputHandler->GetSubscriberHandler();
217     CHKPV(subscriberHandler);
218     MMIEventDump->CheckCount(fd_, args, count);
219     MMIEventDump->ParseCommand(fd_, args);
220     ASSERT_NO_FATAL_FAILURE(subscriberHandler->Dump(fd_, args));
221 }
222 
223 /**
224  * @tc.name: EventDumpTest_011
225  * @tc.desc: Event dump MonitorHandler
226  * @tc.type: FUNC
227  * @tc.require:
228  */
229 HWTEST_F(EventDumpTest, EventDumpTest011, TestSize.Level1)
230 {
231     CALL_TEST_DEBUG;
232     std::vector<std::string> args = {"-o"};
233     int32_t count = 0;
234     auto monitorHandler = InputHandler->GetMonitorHandler();
235     CHKPV(monitorHandler);
236     MMIEventDump->CheckCount(fd_, args, count);
237     MMIEventDump->ParseCommand(fd_, args);
238     ASSERT_NO_FATAL_FAILURE(monitorHandler->Dump(fd_, args));
239 }
240 
241 /**
242  * @tc.name: EventDumpTest_012
243  * @tc.desc: Event dump InterceptorHandler
244  * @tc.type: FUNC
245  * @tc.require:
246  */
247 HWTEST_F(EventDumpTest, EventDumpTest012, TestSize.Level1)
248 {
249     CALL_TEST_DEBUG;
250     std::vector<std::string> args = {"-i"};
251     int32_t count = 0;
252     auto interceptorHandler = InputHandler->GetInterceptorHandler();
253     CHKPV(interceptorHandler);
254     MMIEventDump->CheckCount(fd_, args, count);
255     MMIEventDump->ParseCommand(fd_, args);
256     ASSERT_NO_FATAL_FAILURE(interceptorHandler->Dump(fd_, args));
257 }
258 
259 /**
260  * @tc.name: EventDumpTest_013
261  * @tc.desc: Event dump FilterHandler
262  * @tc.type: FUNC
263  * @tc.require:
264  */
265 HWTEST_F(EventDumpTest, EventDumpTest013, TestSize.Level1)
266 {
267     CALL_TEST_DEBUG;
268     std::vector<std::string> args = {"-f"};
269     int32_t count = 0;
270     auto filterHandler = InputHandler->GetFilterHandler();
271     CHKPV(filterHandler);
272     MMIEventDump->CheckCount(fd_, args, count);
273     MMIEventDump->ParseCommand(fd_, args);
274     ASSERT_NO_FATAL_FAILURE(filterHandler->Dump(fd_, args));
275 }
276 
277 /**
278  * @tc.name: EventDumpTest_014
279  * @tc.desc: Event dump MouseEventHandler
280  * @tc.type: FUNC
281  * @tc.require:
282  */
283 HWTEST_F(EventDumpTest, EventDumpTest014, TestSize.Level1)
284 {
285     CALL_TEST_DEBUG;
286     std::vector<std::string> args = {"-m"};
287     int32_t count = 0;
288     MMIEventDump->CheckCount(fd_, args, count);
289     MMIEventDump->ParseCommand(fd_, args);
290     ASSERT_NO_FATAL_FAILURE(MouseEventHdr->Dump(fd_, args));
291 }
292 } // namespace MMI
293 } // namespace OHOS