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 <gtest/gtest.h>
17 
18 #include "i_input_event_consumer.h"
19 #include "input_handler_manager.h"
20 #include "mmi_log.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "InputHandlerManagerTest"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 using namespace testing::ext;
29 } // namespace
30 
31 class InputHandlerManagerTest : public testing::Test {
32 public:
SetUpTestCase(void)33     static void SetUpTestCase(void) {}
TearDownTestCase(void)34     static void TearDownTestCase(void) {}
35 };
36 
37 
38 class MyInputHandlerManager : public InputHandlerManager {
39 public:
40     MyInputHandlerManager() = default;
41     ~MyInputHandlerManager() override = default;
42 
43 protected:
GetHandlerType() const44     InputHandlerType GetHandlerType() const override
45     {
46         return InputHandlerType::INTERCEPTOR;
47     }
48 };
49 
50 class MYInputHandlerManager : public InputHandlerManager {
51 public:
52     MYInputHandlerManager() = default;
53     ~MYInputHandlerManager() override = default;
54 
55 protected:
GetHandlerType() const56     InputHandlerType GetHandlerType() const override
57     {
58         return InputHandlerType::MONITOR;
59     }
60 };
61 
62 /**
63  * @tc.name: InputHandlerManagerTest_FindHandler_001
64  * @tc.desc:
65  * @tc.type: FUNC
66  * @tc.require:
67  */
68 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_FindHandler_001, TestSize.Level1)
69 {
70     MyInputHandlerManager manager;
71     int32_t handlerId = 1;
72     ASSERT_NO_FATAL_FAILURE(manager.FindHandler(handlerId));
73     handlerId = -1;
74     ASSERT_NO_FATAL_FAILURE(manager.FindHandler(handlerId));
75 }
76 
77 /**
78  * @tc.name: InputHandlerManagerTest_AddMouseEventId_001
79  * @tc.desc:
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_AddMouseEventId_001, TestSize.Level1)
84 {
85     MyInputHandlerManager manager;
86     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
87     ASSERT_NE(pointerEvent, nullptr);
88     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
89     ASSERT_NO_FATAL_FAILURE(manager.AddMouseEventId(pointerEvent));
90     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
91     ASSERT_NO_FATAL_FAILURE(manager.AddMouseEventId(pointerEvent));
92     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
93     ASSERT_NO_FATAL_FAILURE(manager.AddMouseEventId(pointerEvent));
94 }
95 
96 /**
97  * @tc.name: InputHandlerManagerTest_HasHandler_001
98  * @tc.desc:
99  * @tc.type: FUNC
100  * @tc.require:
101  */
102 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_HasHandler_001, TestSize.Level1)
103 {
104     MyInputHandlerManager manager;
105     int32_t handlerId = 1;
106     ASSERT_NO_FATAL_FAILURE(manager.HasHandler(handlerId));
107     handlerId = -1;
108     ASSERT_NO_FATAL_FAILURE(manager.HasHandler(handlerId));
109 }
110 
111 /**
112  * @tc.name: InputHandlerManagerTest_OnDispatchEventProcessed_001
113  * @tc.desc:
114  * @tc.type: FUNC
115  * @tc.require:
116  */
117 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDispatchEventProcessed_001, TestSize.Level1)
118 {
119     MyInputHandlerManager manager;
120     int32_t eventId = 1;
121     int64_t actionTime = 2;
122     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
123     eventId = -1;
124     actionTime = -2;
125     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
126 }
127 
128 /**
129  * @tc.name: InputHandlerManagerTest_OnDispatchEventProcessed_002
130  * @tc.desc: Test the funcation OnDispatchEventProcessed
131  * @tc.type: FUNC
132  * @tc.require:
133  */
134 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDispatchEventProcessed_002, TestSize.Level1)
135 {
136     MyInputHandlerManager manager;
137     int32_t eventId = 2;
138     int64_t actionTime = 3;
139     manager.mouseEventIds_.insert(10);
140     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
141     eventId = 10;
142     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
143 }
144 
145 /**
146  * @tc.name: InputHandlerManagerTest_GetNextId_001
147  * @tc.desc: Verify GetNextId
148  * @tc.type: FUNC
149  * @tc.require:
150  */
151 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_GetNextId_001, TestSize.Level1)
152 {
153     MyInputHandlerManager manager;
154     manager.nextId_ = std::numeric_limits<int32_t>::max();
155     int32_t result = manager.GetNextId();
156     ASSERT_EQ(result, INVALID_HANDLER_ID);
157 }
158 
159 /**
160  * @tc.name: InputHandlerManagerTest_GetNextId_002
161  * @tc.desc: Verify GetNextId
162  * @tc.type: FUNC
163  * @tc.require:
164  */
165 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_GetNextId_002, TestSize.Level1)
166 {
167     MyInputHandlerManager manager;
168     manager.nextId_ = 5;
169     int32_t result = manager.GetNextId();
170     ASSERT_EQ(result, 5);
171 }
172 
173 /**
174  * @tc.name: InputHandlerManagerTest_FindHandler_002
175  * @tc.desc: Verify FindHandler
176  * @tc.type: FUNC
177  * @tc.require:
178  */
179 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_FindHandler_002, TestSize.Level1)
180 {
181     MYInputHandlerManager manager;
182     int32_t handlerId = 1;
183     InputHandlerManager::Handler handler;
184     std::shared_ptr<IInputEventConsumer> consumer = nullptr;
185     handler.consumer_ = consumer;
186     manager.monitorHandlers_[handlerId] = handler;
187     std::shared_ptr<IInputEventConsumer> result = manager.FindHandler(handlerId);
188     ASSERT_EQ(result, consumer);
189 }
190 
191 /**
192  * @tc.name: InputHandlerManagerTest_FindHandler_003
193  * @tc.desc: Verify FindHandler
194  * @tc.type: FUNC
195  * @tc.require:
196  */
197 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_FindHandler_003, TestSize.Level1)
198 {
199     MYInputHandlerManager manager;
200     int32_t handlerId = 1;
201     std::shared_ptr<IInputEventConsumer> result = manager.FindHandler(handlerId);
202     ASSERT_EQ(result, nullptr);
203 }
204 
205 /**
206  * @tc.name: InputHandlerManagerTest_FindHandler_004
207  * @tc.desc: Verify FindHandler
208  * @tc.type: FUNC
209  * @tc.require:
210  */
211 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_FindHandler_004, TestSize.Level1)
212 {
213     MyInputHandlerManager manager;
214     int32_t handlerId = 1;
215     InputHandlerManager::Handler handler;
216     handler.handlerId_ = 1;
217     manager.interHandlers_.push_back(handler);
218     std::shared_ptr<IInputEventConsumer> result = manager.FindHandler(handlerId);
219     EXPECT_EQ(result, nullptr);
220 }
221 
222 /**
223  * @tc.name: InputHandlerManagerTest_FindHandler_005
224  * @tc.desc: Verify FindHandler
225  * @tc.type: FUNC
226  * @tc.require:
227  */
228 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_FindHandler_005, TestSize.Level1)
229 {
230     MyInputHandlerManager manager;
231     int32_t handlerId = 5;
232     InputHandlerManager::Handler handler;
233     handler.handlerId_ = 1;
234     manager.interHandlers_.push_back(handler);
235     std::shared_ptr<IInputEventConsumer> result = manager.FindHandler(handlerId);
236     EXPECT_EQ(result, nullptr);
237 }
238 
239 /**
240  * @tc.name: InputHandlerManagerTest_OnDispatchEventProcessed_003
241  * @tc.desc: Verify OnDispatchEventProcessed
242  * @tc.type: FUNC
243  * @tc.require:
244  */
245 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDispatchEventProcessed_003, TestSize.Level1)
246 {
247     MyInputHandlerManager manager;
248     int32_t eventId = 4;
249     int64_t actionTime = 2;
250     manager.mouseEventIds_.insert(1);
251     manager.mouseEventIds_.insert(2);
252     manager.mouseEventIds_.insert(3);
253     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
254 }
255 
256 /**
257  * @tc.name: InputHandlerManagerTest_OnDispatchEventProcessed_004
258  * @tc.desc: Verify OnDispatchEventProcessed
259  * @tc.type: FUNC
260  * @tc.require:
261  */
262 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDispatchEventProcessed_004, TestSize.Level1)
263 {
264     MyInputHandlerManager manager;
265     int32_t eventId = 10;
266     int64_t actionTime = 2;
267     manager.mouseEventIds_.insert(1);
268     manager.mouseEventIds_.insert(2);
269     manager.mouseEventIds_.insert(3);
270     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
271 }
272 
273 /**
274  * @tc.name: InputHandlerManagerTest_OnDispatchEventProcessed_005
275  * @tc.desc: Verify OnDispatchEventProcessed
276  * @tc.type: FUNC
277  * @tc.require:
278  */
279 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDispatchEventProcessed_005, TestSize.Level1)
280 {
281     MyInputHandlerManager manager;
282     int32_t eventId = 1;
283     int64_t actionTime = 2;
284     manager.mouseEventIds_.insert(2);
285     manager.mouseEventIds_.insert(3);
286     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
287 }
288 
289 /**
290  * @tc.name: InputHandlerManagerTest_OnDispatchEventProcessed_006
291  * @tc.desc: Verify OnDispatchEventProcessed
292  * @tc.type: FUNC
293  * @tc.require:
294  */
295 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDispatchEventProcessed_006, TestSize.Level1)
296 {
297     MyInputHandlerManager manager;
298     int32_t eventId = 2;
299     int64_t actionTime = 2;
300     manager.mouseEventIds_.insert(1);
301     manager.mouseEventIds_.insert(2);
302     manager.mouseEventIds_.insert(3);
303     ASSERT_NO_FATAL_FAILURE(manager.OnDispatchEventProcessed(eventId, actionTime));
304 }
305 
306 /**
307  * @tc.name: InputHandlerManagerTest_CheckInputDeviceSource_001
308  * @tc.desc: Verify CheckInputDeviceSource
309  * @tc.type: FUNC
310  * @tc.require:
311  */
312 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_CheckInputDeviceSource_001, TestSize.Level1)
313 {
314     MyInputHandlerManager manager;
315     uint32_t deviceTags = 4;
316     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
317     ASSERT_NE(pointerEvent, nullptr);
318     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
319     bool result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
320     ASSERT_TRUE(result);
321     deviceTags = 5;
322     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
323     result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
324     ASSERT_TRUE(result);
325 }
326 
327 /**
328  * @tc.name: InputHandlerManagerTest_CheckInputDeviceSource_002
329  * @tc.desc: Verify CheckInputDeviceSource
330  * @tc.type: FUNC
331  * @tc.require:
332  */
333 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_CheckInputDeviceSource_002, TestSize.Level1)
334 {
335     MyInputHandlerManager manager;
336     uint32_t deviceTags = 2;
337     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
338     ASSERT_NE(pointerEvent, nullptr);
339     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
340     bool result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
341     ASSERT_TRUE(result);
342     deviceTags = 3;
343     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
344     result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
345     ASSERT_TRUE(result);
346     deviceTags = 2;
347     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
348     result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
349     ASSERT_TRUE(result);
350 }
351 
352 /**
353  * @tc.name: InputHandlerManagerTest_CheckInputDeviceSource_003
354  * @tc.desc: Verify CheckInputDeviceSource
355  * @tc.type: FUNC
356  * @tc.require:
357  */
358 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_CheckInputDeviceSource_003, TestSize.Level1)
359 {
360     MyInputHandlerManager manager;
361     uint32_t deviceTags = 2;
362     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
363     ASSERT_NE(pointerEvent, nullptr);
364     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
365     bool result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
366     ASSERT_FALSE(result);
367     deviceTags = 10;
368     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
369     result = manager.CheckInputDeviceSource(pointerEvent, deviceTags);
370     ASSERT_FALSE(result);
371 }
372 
373 /**
374  * @tc.name: InputHandlerManagerTest_RecoverPointerEvent
375  * @tc.desc: Test RecoverPointerEvent
376  * @tc.type: FUNC
377  * @tc.require:
378  */
379 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_RecoverPointerEvent, TestSize.Level1)
380 {
381     CALL_TEST_DEBUG;
382     MYInputHandlerManager inputHdlMgr;
383     inputHdlMgr.lastPointerEvent_ = PointerEvent::Create();
384     ASSERT_NE(inputHdlMgr.lastPointerEvent_, nullptr);
385     inputHdlMgr.lastPointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
386     std::initializer_list<int32_t> pointerActionEvents { PointerEvent::POINTER_ACTION_DOWN,
387         PointerEvent::POINTER_ACTION_UP };
388     int32_t pointerActionEvent = PointerEvent::POINTER_ACTION_DOWN;
389     inputHdlMgr.lastPointerEvent_->SetPointerId(0);
390     PointerEvent::PointerItem item;
391     item.SetPointerId(1);
392     inputHdlMgr.lastPointerEvent_->AddPointerItem(item);
393     EXPECT_FALSE(inputHdlMgr.RecoverPointerEvent(pointerActionEvents, pointerActionEvent));
394 
395     inputHdlMgr.lastPointerEvent_->SetPointerId(1);
396     EXPECT_TRUE(inputHdlMgr.RecoverPointerEvent(pointerActionEvents, pointerActionEvent));
397 
398     inputHdlMgr.lastPointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
399     EXPECT_FALSE(inputHdlMgr.RecoverPointerEvent(pointerActionEvents, pointerActionEvent));
400 }
401 
402 /**
403  * @tc.name: InputHandlerManagerTest_OnDisconnected
404  * @tc.desc: Test OnDisconnected
405  * @tc.type: FUNC
406  * @tc.require:
407  */
408 HWTEST_F(InputHandlerManagerTest, InputHandlerManagerTest_OnDisconnected, TestSize.Level1)
409 {
410     CALL_TEST_DEBUG;
411     MYInputHandlerManager inputHdlMgr;
412     inputHdlMgr.lastPointerEvent_ = PointerEvent::Create();
413     ASSERT_NE(inputHdlMgr.lastPointerEvent_, nullptr);
414     inputHdlMgr.lastPointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_SWIPE_BEGIN);
415     inputHdlMgr.lastPointerEvent_->SetPointerId(1);
416     PointerEvent::PointerItem item;
417     item.SetPointerId(1);
418     inputHdlMgr.lastPointerEvent_->AddPointerItem(item);
419     EXPECT_NO_FATAL_FAILURE(inputHdlMgr.OnDisconnected());
420 
421     inputHdlMgr.lastPointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
422     EXPECT_NO_FATAL_FAILURE(inputHdlMgr.OnDisconnected());
423 }
424 } // namespace MMI
425 } // namespace OHOS