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 <fstream>
17 
18 #include <gtest/gtest.h>
19 
20 #include "event_interceptor_handler.h"
21 #include "mmi_log.h"
22 #include "uds_server.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 using namespace testing::ext;
28 constexpr int32_t UID_ROOT { 0 };
29 const std::string PROGRAM_NAME = "uds_sesion_test";
30 int32_t g_moduleType = 3;
31 int32_t g_pid = 0;
32 int32_t g_writeFd = -1;
33 } // namespace
34 
35 class EventInterceptorHandlerTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void) {}
TearDownTestCase(void)38     static void TearDownTestCase(void) {}
39 };
40 
41 /**
42  * @tc.name: EventInterceptorHandler_Test_001
43  * @tc.desc: Test the function HandleKeyEvent
44  * @tc.type: FUNC
45  * @tc.require:
46  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_001, TestSize.Level1)47 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_001, TestSize.Level1)
48 {
49     CALL_TEST_DEBUG;
50     EventInterceptorHandler handler;
51     std::shared_ptr<KeyEvent> event = KeyEvent::Create();
52     ASSERT_NO_FATAL_FAILURE(handler.HandleKeyEvent(event));
53 }
54 
55 /**
56  * @tc.name: EventInterceptorHandler_Test_002
57  * @tc.desc: Test the function HandlePointerEvent
58  * @tc.type: FUNC
59  * @tc.require:
60  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_002, TestSize.Level1)61 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_002, TestSize.Level1)
62 {
63     CALL_TEST_DEBUG;
64     EventInterceptorHandler handler;
65     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
66     ASSERT_NO_FATAL_FAILURE(handler.HandlePointerEvent(pointerEvent));
67 }
68 
69 /**
70  * @tc.name: EventInterceptorHandler_Test_003
71  * @tc.desc: Test the function HandleTouchEvent
72  * @tc.type: FUNC
73  * @tc.require:
74  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_003, TestSize.Level1)75 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_003, TestSize.Level1)
76 {
77     CALL_TEST_DEBUG;
78     EventInterceptorHandler handler;
79     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
80     ASSERT_NO_FATAL_FAILURE(handler.HandleTouchEvent(pointerEvent));
81 }
82 
83 /**
84  * @tc.name: EventInterceptorHandler_Test_004
85  * @tc.desc: Test the function OnHandleEvent
86  * @tc.type: FUNC
87  * @tc.require:
88  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_004, TestSize.Level1)89 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_004, TestSize.Level1)
90 {
91     CALL_TEST_DEBUG;
92     EventInterceptorHandler handler;
93     std::shared_ptr<KeyEvent> event = KeyEvent::Create();
94     EXPECT_FALSE(handler.OnHandleEvent(event));
95 }
96 
97 /**
98  * @tc.name: EventInterceptorHandler_Test_005
99  * @tc.desc: Test the function OnHandleEvent
100  * @tc.type: FUNC
101  * @tc.require:
102  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_005, TestSize.Level1)103 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_005, TestSize.Level1)
104 {
105     CALL_TEST_DEBUG;
106     EventInterceptorHandler handler;
107     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
108     EXPECT_FALSE(handler.OnHandleEvent(pointerEvent));
109 }
110 
111 /**
112  * @tc.name: EventInterceptorHandler_Test_007
113  * @tc.desc: Test the function HandleEvent
114  * @tc.type: FUNC
115  * @tc.require:
116  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_007, TestSize.Level1)117 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_007, TestSize.Level1)
118 {
119     CALL_TEST_DEBUG;
120     EventInterceptorHandler::InterceptorCollection interceptorHandler;
121     std::shared_ptr<KeyEvent> KeyEvent = KeyEvent::Create();
122     bool ret = interceptorHandler.HandleEvent(KeyEvent);
123     EXPECT_FALSE(ret);
124 }
125 
126 /**
127  * @tc.name: EventInterceptorHandler_Test_008
128  * @tc.desc: Test the function CheckInputDeviceSource
129  * @tc.type: FUNC
130  * @tc.require:
131  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_008, TestSize.Level1)132 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_008, TestSize.Level1)
133 {
134     CALL_TEST_DEBUG;
135     EventInterceptorHandler::InterceptorCollection interceptorHandler;
136     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
137     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
138     uint32_t deviceTags = 4;
139     bool ret = EventInterceptorHandler::CheckInputDeviceSource(pointerEvent, deviceTags);
140     EXPECT_TRUE(ret);
141 
142     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
143     deviceTags = 2;
144     ret = EventInterceptorHandler::CheckInputDeviceSource(pointerEvent, deviceTags);
145     EXPECT_TRUE(ret);
146 
147     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
148     ret = EventInterceptorHandler::CheckInputDeviceSource(pointerEvent, deviceTags);
149     EXPECT_TRUE(ret);
150 }
151 
152 /**
153  * @tc.name: EventInterceptorHandler_Test_009
154  * @tc.desc: Test the function HandleEvent
155  * @tc.type: FUNC
156  * @tc.require:
157  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_009, TestSize.Level1)158 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_009, TestSize.Level1)
159 {
160     CALL_TEST_DEBUG;
161     EventInterceptorHandler::InterceptorCollection interceptorHandler;
162     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
163     bool ret = interceptorHandler.HandleEvent(pointerEvent);
164     EXPECT_FALSE(ret);
165 }
166 
167 /**
168  * @tc.name: EventInterceptorHandler_Test_010
169  * @tc.desc: Test the function HandleEvent
170  * @tc.type: FUNC
171  * @tc.require:
172  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_010, TestSize.Level1)173 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Test_010, TestSize.Level1)
174 {
175     CALL_TEST_DEBUG;
176     EventInterceptorHandler::InterceptorCollection interceptorHandler;
177     InputHandlerType handlerType = InputHandlerType::NONE;
178     HandleEventType eventType = 0;
179     int32_t priority = 0;
180     uint32_t deviceTags = 0;
181     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
182         g_writeFd, UID_ROOT, g_pid);
183     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
184         deviceTags, sessionFirst);
185     interceptorHandler.interceptors_.push_back(interceptorFirst);
186     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
187     bool ret = interceptorHandler.HandleEvent(pointerEvent);
188     EXPECT_FALSE(ret);
189 }
190 
191 /**
192  * @tc.name: EventInterceptorHandler_AddInterceptor_01
193  * @tc.desc: Test AddInterceptor
194  * @tc.type: FUNC
195  * @tc.require:
196  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_01, TestSize.Level1)197 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_01, TestSize.Level1)
198 {
199     CALL_TEST_DEBUG;
200     EventInterceptorHandler::InterceptorCollection interceptorHandler;
201     InputHandlerType handlerType = InputHandlerType::NONE;
202     HandleEventType eventType = 0;
203     int32_t priority = 0;
204     uint32_t deviceTags = 0;
205     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
206         g_writeFd, UID_ROOT, g_pid);
207     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
208         deviceTags, sessionFirst);
209 
210     handlerType = InputHandlerType::NONE;
211     eventType = 0;
212     priority = 0;
213     deviceTags = 0;
214     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
215         g_writeFd, UID_ROOT, g_pid);
216     EventInterceptorHandler::SessionHandler interceptorSecond(handlerType, eventType, priority,
217         deviceTags, sessionSecond);
218     for (int32_t i = 0; i < 20; i++) {
219         interceptorHandler.interceptors_.push_back(interceptorSecond);
220     }
221     ASSERT_NO_FATAL_FAILURE(interceptorHandler.AddInterceptor(interceptorFirst));
222 }
223 
224 /**
225  * @tc.name: EventInterceptorHandler_AddInterceptor_02
226  * @tc.desc: Test AddInterceptor
227  * @tc.type: FUNC
228  * @tc.require:
229  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_02, TestSize.Level1)230 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_02, TestSize.Level1)
231 {
232     CALL_TEST_DEBUG;
233     EventInterceptorHandler::InterceptorCollection interceptorHandler;
234     InputHandlerType handlerType = InputHandlerType::NONE;
235     HandleEventType eventType = 0;
236     int32_t priority = 0;
237     uint32_t deviceTags = 0;
238     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
239         g_writeFd, UID_ROOT, g_pid);
240     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
241         deviceTags, sessionFirst);
242     for (int32_t i = 0; i < 20; i++) {
243         interceptorHandler.interceptors_.push_back(interceptorFirst);
244     }
245     ASSERT_NO_FATAL_FAILURE(interceptorHandler.AddInterceptor(interceptorFirst));
246 }
247 
248 /**
249  * @tc.name: EventInterceptorHandler_AddInterceptor_03
250  * @tc.desc: Test AddInterceptor
251  * @tc.type: FUNC
252  * @tc.require:
253  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_03, TestSize.Level1)254 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_03, TestSize.Level1)
255 {
256     CALL_TEST_DEBUG;
257     EventInterceptorHandler::InterceptorCollection interceptorHandler;
258     InputHandlerType handlerType = InputHandlerType::NONE;
259     HandleEventType eventType = 0;
260     int32_t priority = 1;
261     uint32_t deviceTags = 0;
262     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
263         g_writeFd, UID_ROOT, g_pid);
264     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
265         deviceTags, sessionFirst);
266 
267     handlerType = InputHandlerType::NONE;
268     eventType = 0;
269     priority = 2;
270     deviceTags = 0;
271     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
272         g_writeFd, UID_ROOT, g_pid);
273     EventInterceptorHandler::SessionHandler interceptorSecond(handlerType, eventType, priority,
274         deviceTags, sessionSecond);
275     interceptorHandler.interceptors_.push_back(interceptorSecond);
276     ASSERT_NO_FATAL_FAILURE(interceptorHandler.AddInterceptor(interceptorFirst));
277 }
278 
279 /**
280  * @tc.name: EventInterceptorHandler_AddInterceptor_04
281  * @tc.desc: Test AddInterceptor
282  * @tc.type: FUNC
283  * @tc.require:
284  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_04, TestSize.Level1)285 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInterceptor_04, TestSize.Level1)
286 {
287     CALL_TEST_DEBUG;
288     EventInterceptorHandler::InterceptorCollection interceptorHandler;
289     InputHandlerType handlerType = InputHandlerType::NONE;
290     HandleEventType eventType = 0;
291     int32_t priority = 1;
292     uint32_t deviceTags = 0;
293     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
294         g_writeFd, UID_ROOT, g_pid);
295     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
296         deviceTags, sessionFirst);
297 
298     handlerType = InputHandlerType::NONE;
299     eventType = 0;
300     priority = 0;
301     deviceTags = 0;
302     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
303         g_writeFd, UID_ROOT, g_pid);
304     EventInterceptorHandler::SessionHandler interceptorSecond(handlerType, eventType, priority,
305         deviceTags, sessionSecond);
306     interceptorHandler.interceptors_.push_back(interceptorSecond);
307     ASSERT_NO_FATAL_FAILURE(interceptorHandler.AddInterceptor(interceptorFirst));
308 }
309 
310 /**
311  * @tc.name: EventInterceptorHandler_RemoveInterceptor_01
312  * @tc.desc: Test RemoveInterceptor
313  * @tc.type: FUNC
314  * @tc.require:
315  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_01, TestSize.Level1)316 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_01, TestSize.Level1)
317 {
318     CALL_TEST_DEBUG;
319     EventInterceptorHandler::InterceptorCollection interceptorHandler;
320     InputHandlerType handlerType = InputHandlerType::NONE;
321     HandleEventType eventType = 0;
322     int32_t priority = 0;
323     uint32_t deviceTags = 0;
324     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
325         g_writeFd, UID_ROOT, g_pid);
326     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
327         deviceTags, sessionFirst);
328 
329     handlerType = InputHandlerType::NONE;
330     eventType = 0;
331     priority = 0;
332     deviceTags = 0;
333     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
334         g_writeFd, UID_ROOT, g_pid);
335     EventInterceptorHandler::SessionHandler interceptorSecond(handlerType, eventType, priority,
336         deviceTags, sessionSecond);
337     interceptorHandler.interceptors_.push_back(interceptorSecond);
338     ASSERT_NO_FATAL_FAILURE(interceptorHandler.RemoveInterceptor(interceptorFirst));
339 }
340 
341 /**
342  * @tc.name: EventInterceptorHandler_RemoveInterceptor_02
343  * @tc.desc: Test RemoveInterceptor
344  * @tc.type: FUNC
345  * @tc.require:
346  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_02, TestSize.Level1)347 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_02, TestSize.Level1)
348 {
349     CALL_TEST_DEBUG;
350     EventInterceptorHandler::InterceptorCollection interceptorHandler;
351     InputHandlerType handlerType = InputHandlerType::NONE;
352     HandleEventType eventType = 0;
353     int32_t priority = 0;
354     uint32_t deviceTags = 0;
355     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
356         g_writeFd, UID_ROOT, g_pid);
357     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
358         deviceTags, sessionFirst);
359     interceptorHandler.interceptors_.push_back(interceptorFirst);
360     ASSERT_NO_FATAL_FAILURE(interceptorHandler.RemoveInterceptor(interceptorFirst));
361 }
362 
363 /**
364  * @tc.name: EventInterceptorHandler_RemoveInterceptor_03
365  * @tc.desc: Test RemoveInterceptor
366  * @tc.type: FUNC
367  * @tc.require:
368  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_03, TestSize.Level1)369 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_03, TestSize.Level1)
370 {
371     CALL_TEST_DEBUG;
372     EventInterceptorHandler::InterceptorCollection interceptorHandler;
373     InputHandlerType handlerType = InputHandlerType::NONE;
374     HandleEventType eventType = 1;
375     int32_t priority = 1;
376     uint32_t deviceTags = 0;
377     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
378         g_writeFd, UID_ROOT, g_pid);
379     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
380         deviceTags, sessionFirst);
381 
382     handlerType = InputHandlerType::NONE;
383     eventType = 1;
384     priority = 2;
385     deviceTags = 0;
386     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
387         g_writeFd, UID_ROOT, g_pid);
388     EventInterceptorHandler::SessionHandler interceptorSecond(handlerType, eventType, priority,
389         deviceTags, sessionSecond);
390     interceptorHandler.interceptors_.push_back(interceptorSecond);
391     ASSERT_NO_FATAL_FAILURE(interceptorHandler.RemoveInterceptor(interceptorFirst));
392 }
393 
394 /**
395  * @tc.name: EventInterceptorHandler_RemoveInterceptor_04
396  * @tc.desc: Test RemoveInterceptor
397  * @tc.type: FUNC
398  * @tc.require:
399  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_04, TestSize.Level1)400 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInterceptor_04, TestSize.Level1)
401 {
402     CALL_TEST_DEBUG;
403     EventInterceptorHandler::InterceptorCollection interceptorHandler;
404     InputHandlerType handlerType = InputHandlerType::NONE;
405     HandleEventType eventType = 1;
406     int32_t priority = 1;
407     uint32_t deviceTags = 0;
408     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
409         g_writeFd, UID_ROOT, g_pid);
410     EventInterceptorHandler::SessionHandler interceptorFirst(handlerType, eventType, priority,
411         deviceTags, sessionFirst);
412 
413     handlerType = InputHandlerType::NONE;
414     eventType = 1;
415     priority = 0;
416     deviceTags = 0;
417     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
418         g_writeFd, UID_ROOT, g_pid);
419     EventInterceptorHandler::SessionHandler interceptorSecond(handlerType, eventType, priority,
420         deviceTags, sessionSecond);
421     interceptorHandler.interceptors_.push_back(interceptorSecond);
422     ASSERT_NO_FATAL_FAILURE(interceptorHandler.RemoveInterceptor(interceptorFirst));
423 }
424 
425 /**
426  * @tc.name: EventInterceptorHandler_OnSessionLost_01
427  * @tc.desc: Test OnSessionLost
428  * @tc.type: FUNC
429  * @tc.require:
430  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_OnSessionLost_01, TestSize.Level1)431 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_OnSessionLost_01, TestSize.Level1)
432 {
433     CALL_TEST_DEBUG;
434     EventInterceptorHandler::InterceptorCollection interceptorHandler;
435     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
436         g_writeFd, UID_ROOT, g_pid);
437     SessionPtr sessionSecond = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
438         g_writeFd, UID_ROOT, g_pid);
439     InputHandlerType handlerType = InputHandlerType::NONE;
440     HandleEventType eventType = 0;
441     int32_t priority = 0;
442     uint32_t deviceTags = 0;
443     SessionPtr session = sessionSecond;
444     EventInterceptorHandler::SessionHandler interceptor(handlerType, eventType, priority,
445         deviceTags, session);
446     interceptorHandler.interceptors_.push_back(interceptor);
447     ASSERT_NO_FATAL_FAILURE(interceptorHandler.OnSessionLost(sessionFirst));
448 }
449 
450 /**
451  * @tc.name: EventInterceptorHandler_OnSessionLost_02
452  * @tc.desc: Test OnSessionLost
453  * @tc.type: FUNC
454  * @tc.require:
455  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_OnSessionLost_02, TestSize.Level1)456 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_OnSessionLost_02, TestSize.Level1)
457 {
458     CALL_TEST_DEBUG;
459     EventInterceptorHandler::InterceptorCollection interceptorHandler;
460     SessionPtr sessionFirst = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType,
461         g_writeFd, UID_ROOT, g_pid);
462     InputHandlerType handlerType = InputHandlerType::NONE;
463     HandleEventType eventType = 0;
464     int32_t priority = 0;
465     uint32_t deviceTags = 0;
466     SessionPtr session = sessionFirst;
467     EventInterceptorHandler::SessionHandler interceptor(handlerType, eventType, priority,
468         deviceTags, session);
469     interceptorHandler.interceptors_.push_back(interceptor);
470     ASSERT_NO_FATAL_FAILURE(interceptorHandler.OnSessionLost(sessionFirst));
471 }
472 
473 /**
474  * @tc.name: EventInterceptorHandler_AddInputHandler_01
475  * @tc.desc: Test AddInputHandler
476  * @tc.type: FUNC
477  * @tc.require:
478  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInputHandler_01, TestSize.Level1)479 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInputHandler_01, TestSize.Level1)
480 {
481     CALL_TEST_DEBUG;
482     EventInterceptorHandler interceptorHandler;
483     SessionPtr sess = nullptr;
484     InputHandlerType handlerType = InputHandlerType::NONE;
485     HandleEventType eventType = HANDLE_EVENT_TYPE_NONE;
486     int32_t priority = 1;
487     uint32_t deviceTags = 0x01;
488     EXPECT_EQ(interceptorHandler.AddInputHandler(handlerType, eventType, priority, deviceTags, sess), RET_ERR);
489 }
490 
491 /**
492  * @tc.name: EventInterceptorHandler_AddInputHandler_02
493  * @tc.desc: Test AddInputHandler
494  * @tc.type: FUNC
495  * @tc.require:
496  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInputHandler_02, TestSize.Level1)497 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInputHandler_02, TestSize.Level1)
498 {
499     CALL_TEST_DEBUG;
500     EventInterceptorHandler interceptorHandler;
501     SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
502     InputHandlerType handlerType = InputHandlerType::INTERCEPTOR;
503     HandleEventType eventType = 1;
504     int32_t priority = 1;
505     uint32_t deviceTags = 0x01;
506     EXPECT_EQ(interceptorHandler.AddInputHandler(handlerType, eventType, priority, deviceTags, sess), RET_OK);
507 }
508 
509 /**
510  * @tc.name: EventInterceptorHandler_RemoveInputHandler
511  * @tc.desc: Test RemoveInputHandler
512  * @tc.type: FUNC
513  * @tc.require:
514  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInputHandler, TestSize.Level1)515 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInputHandler, TestSize.Level1)
516 {
517     CALL_TEST_DEBUG;
518     EventInterceptorHandler interceptorHandler;
519     SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
520     InputHandlerType handlerType = InputHandlerType::INTERCEPTOR;
521     HandleEventType eventType = 1;
522     int32_t priority = 1;
523     uint32_t deviceTags = 0x01;
524     ASSERT_NO_FATAL_FAILURE(interceptorHandler.RemoveInputHandler(handlerType, eventType, priority, deviceTags, sess));
525 }
526 
527 /**
528  * @tc.name: EventInterceptorHandler_AddInputHandler_001
529  * @tc.desc: Test the function AddInputHandler
530  * @tc.type: FUNC
531  * @tc.require:
532  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInputHandler_001, TestSize.Level1)533 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_AddInputHandler_001, TestSize.Level1)
534 {
535     CALL_TEST_DEBUG;
536     EventInterceptorHandler handler;
537     SessionPtr sess = nullptr;
538     InputHandlerType handlerType = InputHandlerType::NONE;
539     HandleEventType eventType = HANDLE_EVENT_TYPE_NONE;
540     int32_t priority = 2;
541     uint32_t deviceTags = 3;
542     int32_t ret = handler.AddInputHandler(handlerType, eventType, priority, deviceTags, sess);
543     EXPECT_EQ(ret, RET_ERR);
544     sess = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
545     ret = handler.AddInputHandler(handlerType, eventType, priority, deviceTags, sess);
546     EXPECT_EQ(ret, RET_ERR);
547     eventType = HANDLE_EVENT_TYPE_KEY;
548     ret = handler.AddInputHandler(handlerType, eventType, priority, deviceTags, sess);
549     EXPECT_EQ(ret, RET_OK);
550 }
551 
552 /**
553  * @tc.name: EventInterceptorHandler_RemoveInputHandler_001
554  * @tc.desc: Test the function RemoveInputHandler
555  * @tc.type: FUNC
556  * @tc.require:
557  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInputHandler_001, TestSize.Level1)558 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInputHandler_001, TestSize.Level1)
559 {
560     CALL_TEST_DEBUG;
561     EventInterceptorHandler handler;
562     InputHandlerType handlerType = InputHandlerType::INTERCEPTOR;
563     HandleEventType eventType = 1;
564     int32_t priority = 2;
565     uint32_t deviceTags = 1;
566     SessionPtr session = nullptr;
567     ASSERT_NO_FATAL_FAILURE(handler.RemoveInputHandler(handlerType, eventType, priority, deviceTags, session));
568     session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
569     ASSERT_NO_FATAL_FAILURE(handler.RemoveInputHandler(handlerType, eventType, priority, deviceTags, session));
570 }
571 
572 /**
573  * @tc.name: EventInterceptorHandler_RemoveInputHandler_002
574  * @tc.desc: Test the function RemoveInputHandler
575  * @tc.type: FUNC
576  * @tc.require:
577  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInputHandler_002, TestSize.Level1)578 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_RemoveInputHandler_002, TestSize.Level1)
579 {
580     CALL_TEST_DEBUG;
581     EventInterceptorHandler handler;
582     InputHandlerType handlerType = InputHandlerType::NONE;
583     HandleEventType eventType = 1;
584     int32_t priority = 2;
585     uint32_t deviceTags = 1;
586     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
587     ASSERT_NO_FATAL_FAILURE(handler.RemoveInputHandler(handlerType, eventType, priority, deviceTags, session));
588     handlerType = InputHandlerType::MONITOR;
589     ASSERT_NO_FATAL_FAILURE(handler.RemoveInputHandler(handlerType, eventType, priority, deviceTags, session));
590 }
591 
592 /**
593  * @tc.name: EventInterceptorHandler_InitSessionLostCallback_001
594  * @tc.desc: Test the function InitSessionLostCallback
595  * @tc.type: FUNC
596  * @tc.require:
597  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_InitSessionLostCallback_001, TestSize.Level1)598 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_InitSessionLostCallback_001, TestSize.Level1)
599 {
600     CALL_TEST_DEBUG;
601     EventInterceptorHandler handler;
602     handler.sessionLostCallbackInitialized_ = true;
603     ASSERT_NO_FATAL_FAILURE(handler.InitSessionLostCallback());
604     handler.sessionLostCallbackInitialized_ = false;
605     ASSERT_NO_FATAL_FAILURE(handler.InitSessionLostCallback());
606 }
607 
608 /**
609  * @tc.name: EventInterceptorHandler_SendToClient_keyEvent_001
610  * @tc.desc: Test the function SendToClient,parameter is keyEvent
611  * @tc.type: FUNC
612  * @tc.require:
613  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_SendToClient_keyEvent_001, TestSize.Level1)614 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_SendToClient_keyEvent_001, TestSize.Level1)
615 {
616     CALL_TEST_DEBUG;
617     InputHandlerType handlerType = InputHandlerType::NONE;
618     HandleEventType eventType = 0;
619     int32_t priority = 1;
620     uint32_t deviceTags = 0x01;
621     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
622     EventInterceptorHandler::SessionHandler sessionHandler { handlerType, eventType, priority, deviceTags, session };
623     std::shared_ptr<KeyEvent> keyEvent = nullptr;
624     ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(keyEvent));
625     keyEvent = KeyEvent::Create();
626     ASSERT_NE(keyEvent, nullptr);
627     ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(keyEvent));
628 }
629 
630 /**
631  * @tc.name: EventInterceptorHandler_SendToClient_pointerEvent_001
632  * @tc.desc: Test the function SendToClient,parameter is pointerEvent
633  * @tc.type: FUNC
634  * @tc.require:
635  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_SendToClient_pointerEvent_001, TestSize.Level1)636 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_SendToClient_pointerEvent_001, TestSize.Level1)
637 {
638     CALL_TEST_DEBUG;
639     InputHandlerType handlerType = InputHandlerType::NONE;
640     HandleEventType eventType = 0;
641     int32_t priority = 1;
642     uint32_t deviceTags = 0x01;
643     SessionPtr session = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
644     EventInterceptorHandler::SessionHandler sessionHandler { handlerType, eventType, priority, deviceTags, session };
645     std::shared_ptr<PointerEvent> pointerEvent = nullptr;
646     ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(pointerEvent));
647     pointerEvent = PointerEvent::Create();
648     ASSERT_NE(pointerEvent, nullptr);
649     ASSERT_NO_FATAL_FAILURE(sessionHandler.SendToClient(pointerEvent));
650 }
651 
652 /**
653  * @tc.name: EventInterceptorHandler_OnSessionLost
654  * @tc.desc: Test OnSessionLost
655  * @tc.type: FUNC
656  * @tc.require:
657  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_OnSessionLost, TestSize.Level1)658 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_OnSessionLost, TestSize.Level1)
659 {
660     CALL_TEST_DEBUG;
661     EventInterceptorHandler interceptorHandler;
662     SessionPtr sess = std::make_shared<UDSSession>(PROGRAM_NAME, g_moduleType, g_writeFd, UID_ROOT, g_pid);
663     ASSERT_NO_FATAL_FAILURE(interceptorHandler.OnSessionLost(sess));
664 }
665 
666 /**
667  * @tc.name: EventInterceptorHandler_Dump
668  * @tc.desc: Test Dump
669  * @tc.type: FUNC
670  * @tc.require:
671  */
HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Dump, TestSize.Level1)672 HWTEST_F(EventInterceptorHandlerTest, EventInterceptorHandler_Dump, TestSize.Level1)
673 {
674     CALL_TEST_DEBUG;
675     EventInterceptorHandler interceptorHandler;
676     int32_t fd = 1;
677     std::vector<std::string> args = {"-i"};
678     ASSERT_NO_FATAL_FAILURE(interceptorHandler.Dump(fd, args));
679 }
680 } // namespace MMI
681 } // namespace OHOS
682