• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "input_manager.h"
17 #include "error_multimodal.h"
18 #include <bitset>
19 #include <cinttypes>
20 #include <regex>
21 #include <sstream>
22 #include <gtest/gtest.h>
23 #include "define_multimodal.h"
24 #include "input_handler_type.h"
25 #include "input_event_monitor_manager.h"
26 #include "interceptor_manager.h"
27 #include "input_manager.h"
28 #include "multimodal_event_handler.h"
29 #include "pointer_event.h"
30 #include "proto.h"
31 #include "mmi_client.h"
32 #include "mmi_token.h"
33 #include "run_shell_util.h"
34 
35 namespace OHOS {
36 namespace MMI {
37 namespace {
38 using namespace testing::ext;
39 using namespace OHOS;
40 using namespace OHOS::MMI;
41 } // namespace
42 namespace {
43 #ifdef OHOS_WESTEN_MODEL
44 constexpr int32_t MMI_KEY_BACK = 2;
45 constexpr bool ACTION_DOWN = true;
46 constexpr bool ACTION_UP = false;
47 #endif
48 constexpr int32_t DEFAULT_DEVICE_ID = 1;
49 constexpr int32_t DEFAULT_POINTER_ID = 0;
50 constexpr int32_t NANOSECOND_TO_MILLISECOND = 1000000;
51 constexpr int32_t SEC_TO_NANOSEC = 1000000000;
52 constexpr int32_t TIME_WAIT_FOR_OP = 500;
53 constexpr int32_t TIME_WAIT_FOR_LOG = 100;
54 constexpr int32_t N_TRIES_FOR_LOG = 10;
55 #ifdef OHOS_WESTEN_MODEL
56 constexpr bool ISINTERCEPTED_TRUE = true;
57 #endif
58 constexpr int32_t INDEX_FIRST = 1;
59 constexpr int32_t INDEX_SECOND = 2;
60 constexpr int32_t INDEX_THIRD = 3;
61 constexpr int32_t INDEX_INVALID = -1;
62 
63 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputManagerTest" };
64 } // namespace
65 
66 class InputManagerTest : public testing::Test {
67 public:
SetUpTestCase(void)68     static void SetUpTestCase(void) {}
TearDownTestCase(void)69     static void TearDownTestCase(void) {}
70     static int64_t GetNanoTime();
71     static bool FindCommand(const std::string &log, const std::string &command);
72     static std::vector<std::string> SearchLog(const std::string &command, bool noWait = false);
73     static std::vector<std::string> SearchLog(const std::string &command,
74     const std::vector<std::string> &excludes, bool noWait = false);
75     static std::string DumpPointerItem(const PointerEvent::PointerItem &item);
76     static std::string DumpPointerEvent(const std::shared_ptr<PointerEvent> &pointE);
77     static std::shared_ptr<PointerEvent> SetupPointerEvent001();
78     static std::shared_ptr<PointerEvent> SetupPointerEvent002();
79     static std::shared_ptr<PointerEvent> SetupPointerEvent003();
80     static std::shared_ptr<PointerEvent> SetupPointerEvent006();
81     static std::shared_ptr<PointerEvent> SetupPointerEvent007();
82     static std::shared_ptr<PointerEvent> SetupPointerEvent008();
83     static std::shared_ptr<PointerEvent> SetupPointerEvent009();
84     static std::shared_ptr<PointerEvent> SetupPointerEvent012();
85     static std::string DumpPointerItem2(const PointerEvent::PointerItem &item);
86     static std::string DumpPointerEvent2(const std::shared_ptr<PointerEvent> &pointE);
87     static void TestInputEventInterceptor(std::shared_ptr<PointerEvent> pointerEvent);
88     static void TestInputEventInterceptor2(std::shared_ptr<PointerEvent> pointerEvent);
89     std::shared_ptr<PointerEvent> TestMarkConsumedStep1();
90     std::shared_ptr<PointerEvent> TestMarkConsumedStep2();
91     void TestMarkConsumedStep3(int32_t monitorId, int32_t eventId);
92     void TestMarkConsumedStep4();
93     void TestMarkConsumedStep5();
94     void TestMarkConsumedStep6();
95     static void KeyMonitorCallBack(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent);
96     static void TouchPadMonitorCallBack(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent);
97 
98 private:
99     static RunShellUtil runCommand_;
100 };
101 
102 RunShellUtil InputManagerTest::runCommand_ { };
103 
GetNanoTime()104 int64_t InputManagerTest::GetNanoTime()
105 {
106     struct timespec time = { 0 };
107     clock_gettime(CLOCK_MONOTONIC, &time);
108     return static_cast<int64_t>(time.tv_sec) * SEC_TO_NANOSEC + time.tv_nsec;
109 }
110 
111 class InputEventCallback : public OHOS::MMI::IInputEventConsumer {
112 public:
OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const113     virtual void OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const override
114     {
115         MMI_LOGD("keyCode:%{public}d", keyEvent->GetKeyCode());
116     }
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const117     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override
118     {
119         MMI_LOGD("PointerEvent received.");
120     }
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const121     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {}
122     static std::shared_ptr<InputEventCallback> GetPtr();
123 };
124 
GetPtr()125 std::shared_ptr<InputEventCallback> InputEventCallback::GetPtr()
126 {
127     return std::make_shared<InputEventCallback>();
128 }
129 
FindCommand(const std::string & log,const std::string & command)130 bool InputManagerTest::FindCommand(const std::string &log, const std::string &command)
131 {
132     std::ostringstream sCmd;
133     std::string::size_type spos { 0 }, tpos;
134     while (spos < command.size()) {
135         tpos = command.find("\\", spos);
136         if (tpos != std::string::npos) {
137             if (((tpos + 1) < command.size()) &&
138                 (('{' == command[tpos + 1]) || ('}' == command[tpos + 1]))) {
139                 sCmd << command.substr(spos, tpos - spos);
140             } else {
141                 sCmd << command.substr(spos, tpos - spos + 1);
142             }
143             spos = tpos + 1;
144         } else {
145             sCmd << command.substr(spos);
146             spos = command.size();
147         }
148     }
149 
150     std::regex pattern(sCmd.str());
151     return std::regex_search(log, pattern);
152 }
153 
SearchLog(const std::string & command,bool noWait)154 std::vector<std::string> InputManagerTest::SearchLog(const std::string &command, bool noWait)
155 {
156     std::vector<std::string> excludes;
157     return SearchLog(command, excludes, noWait);
158 }
159 
SearchLog(const std::string & command,const std::vector<std::string> & excludes,bool noWait)160 std::vector<std::string> InputManagerTest::SearchLog(const std::string &command,
161     const std::vector<std::string> &excludes, bool noWait)
162 {
163     int32_t nTries { N_TRIES_FOR_LOG };
164     std::vector<std::string> results;
165 
166     while (true) {
167         std::vector<std::string> logs;
168         (void)runCommand_.RunShellCommand(command, logs);
169         for (const std::string& s : logs) {
170             if (FindCommand(s, command) &&
171                 (std::find(excludes.cbegin(), excludes.cend(), s) == excludes.cend())) {
172                 results.push_back(s);
173             }
174         }
175         if (noWait || !results.empty() || (--nTries <= 0)) {
176             break;
177         }
178         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
179     }
180     return results;
181 }
182 
TestMarkConsumedStep1()183 std::shared_ptr<PointerEvent> InputManagerTest::TestMarkConsumedStep1()
184 {
185     auto pointerEvent = PointerEvent::Create();
186     PointerEvent::PointerItem item;
187     item.SetPointerId(0);   // test code,set the PointerId = 0
188     item.SetGlobalX(823);   // test code,set the GlobalX = 823
189     item.SetGlobalY(723);   // test code,set the GlobalY = 723
190     item.SetPressure(5);    // test code,set the Pressure = 5
191     item.SetDeviceId(1);    // test code,set the DeviceId = 1
192     pointerEvent->AddPointerItem(item);
193 
194     pointerEvent->SetId(std::numeric_limits<int32_t>::max() - INDEX_THIRD);
195     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
196     pointerEvent->SetPointerId(0);  // test code,set the PointerId = 1
197     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
198 
199     MMI_LOGD("Call InputManager::SimulatePointerEvent");
200     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
201     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
202     return pointerEvent;
203 }
204 
TestMarkConsumedStep2()205 std::shared_ptr<PointerEvent> InputManagerTest::TestMarkConsumedStep2()
206 {
207     auto pointerEvent = PointerEvent::Create();
208     PointerEvent::PointerItem item;
209     item.SetPointerId(0);   // test code,set the PointerId = 0
210     item.SetGlobalX(1023);  // test code,set the GlobalX = 823
211     item.SetGlobalY(723);   // test code,set the GlobalY = 723
212     item.SetPressure(5);    // test code,set the Pressure = 5
213     item.SetDeviceId(1);    // test code,set the DeviceId = 1
214     pointerEvent->AddPointerItem(item);
215 
216     pointerEvent->SetId(std::numeric_limits<int32_t>::max() - INDEX_SECOND);
217     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
218     pointerEvent->SetPointerId(0);  // test code,set the PointerId = 1
219     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
220 
221     MMI_LOGD("Call InputManager::SimulatePointerEvent");
222     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
223     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
224     return pointerEvent;
225 }
226 
TestMarkConsumedStep3(int32_t monitorId,int32_t eventId)227 void InputManagerTest::TestMarkConsumedStep3(int32_t monitorId, int32_t eventId)
228 {
229     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
230     std::string command {
231         "ClientMsgHandler: in OnPointerEvent, #[[:digit:]]\\{1,\\}, "
232         "Operation canceled"
233     };
234     std::vector<std::string> sLogs { SearchLog(command, true) };
235 
236     MMI_LOGD("Call InputManager::MarkConsumed");
237     InputManager::GetInstance()->MarkConsumed(monitorId, eventId);
238 
239     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
240     EXPECT_TRUE(!tLogs.empty());
241 }
242 
TestMarkConsumedStep4()243 void InputManagerTest::TestMarkConsumedStep4()
244 {
245     auto pointerEvent = PointerEvent::Create();
246     PointerEvent::PointerItem item;
247     item.SetPointerId(0);   // test code,set the PointerId = 0
248     item.SetGlobalX(1123);  // test code,set the GlobalX = 823
249     item.SetGlobalY(723);   // test code,set the GlobalY = 723
250     item.SetPressure(5);    // test code,set the Pressure = 5
251     item.SetDeviceId(1);    // test code,set the DeviceId = 1
252     pointerEvent->AddPointerItem(item);
253 
254     pointerEvent->SetId(std::numeric_limits<int32_t>::max() - INDEX_FIRST);
255     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
256     pointerEvent->SetPointerId(0);  // test code,set the PointerId = 1
257     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
258 
259     std::string command {
260         "InputHandlerManagerGlobal: in HandleEvent, #[[:digit:]]\\{1,\\}, "
261         "Pointer event was monitor"
262     };
263     std::vector<std::string> sLogs { SearchLog(command, true) };
264 
265     MMI_LOGD("Call InputManager::SimulatePointerEvent");
266     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
267 
268     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
269     EXPECT_TRUE(!tLogs.empty());
270 }
271 
TestMarkConsumedStep5()272 void InputManagerTest::TestMarkConsumedStep5()
273 {
274     auto pointerEvent = PointerEvent::Create();
275     PointerEvent::PointerItem item;
276     item.SetPointerId(0);   // test code,set the PointerId = 0
277     item.SetGlobalX(0);  // test code,set the GlobalX = 823
278     item.SetGlobalY(0);   // test code,set the GlobalY = 723
279     item.SetPressure(0);    // test code,set the Pressure = 5
280     item.SetDeviceId(1);    // test code,set the DeviceId = 1
281     pointerEvent->AddPointerItem(item);
282 
283     pointerEvent->SetId(std::numeric_limits<int32_t>::max());
284     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
285     pointerEvent->SetPointerId(0);  // test code,set the PointerId = 1
286     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
287 
288     std::string command {
289         "InputHandlerManagerGlobal: in HandleEvent, #[[:digit:]]\\{1,\\}, "
290         "Pointer event was monitor"
291     };
292     std::vector<std::string> sLogs { SearchLog(command, true) };
293 
294     MMI_LOGD("Call InputManager::SimulatePointerEvent");
295     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
296 
297     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
298     EXPECT_TRUE(!tLogs.empty());
299 }
300 
TestMarkConsumedStep6()301 void InputManagerTest::TestMarkConsumedStep6()
302 {
303     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
304     auto pointerEvent = PointerEvent::Create();
305     PointerEvent::PointerItem item;
306     item.SetPointerId(0);   // test code,set the PointerId = 0
307     item.SetGlobalX(823);   // test code,set the GlobalX = 823
308     item.SetGlobalY(723);   // test code,set the GlobalY = 723
309     item.SetPressure(5);    // test code,set the Pressure = 5
310     item.SetDeviceId(1);    // test code,set the DeviceId = 1
311     pointerEvent->AddPointerItem(item);
312 
313     pointerEvent->SetId(std::numeric_limits<int32_t>::max());
314     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
315     pointerEvent->SetPointerId(0);  // test code,set the PointerId = 1
316     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
317 
318     std::string command {
319         "InputManagerImpl: in OnPointerEvent, #[[:digit:]]\\{1,\\}, "
320         "Pointer event received, processing"
321     };
322     std::vector<std::string> sLogs { SearchLog(command, true) };
323 
324     MMI_LOGD("Call InputManager::SimulatePointerEvent");
325     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
326 
327     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
328     EXPECT_TRUE(!tLogs.empty());
329 }
330 
331 HWTEST_F(InputManagerTest, MultimodalEventHandler_InjectKeyEvent_002, TestSize.Level1)
332 {
333     std::shared_ptr<OHOS::MMI::KeyEvent> injectDownEvent = OHOS::MMI::KeyEvent::Create();
334     int32_t downTime = -1;
335     OHOS::MMI::KeyEvent::KeyItem kitDown;
336     kitDown.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_HOME);
337     kitDown.SetPressed(true);
338     kitDown.SetDownTime(downTime);
339     injectDownEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_HOME);
340     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
341     injectDownEvent->AddPressedKeyItems(kitDown);
342     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
343 }
344 
345 HWTEST_F(InputManagerTest, MultimodalEventHandler_InjectKeyEvent_003, TestSize.Level1)
346 {
347     std::string command = "Inject keyCode:2, action:2";
348     std::vector<std::string> slogs {SearchLog(command, true)};
349     std::shared_ptr<OHOS::MMI::KeyEvent> injectDownEvent = OHOS::MMI::KeyEvent::Create();
350     int32_t downTime = 0;
351     OHOS::MMI::KeyEvent::KeyItem kitDown;
352     kitDown.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
353     kitDown.SetPressed(true);
354     kitDown.SetDownTime(downTime);
355     injectDownEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
356     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
357     injectDownEvent->AddPressedKeyItems(kitDown);
358     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
359 
360     std::shared_ptr<OHOS::MMI::KeyEvent> injectUpEvent = OHOS::MMI::KeyEvent::Create();
361     OHOS::MMI::KeyEvent::KeyItem kitUp;
362     kitUp.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
363     kitUp.SetPressed(false);
364     kitUp.SetDownTime(downTime);
365     injectUpEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
366     injectUpEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
367     injectUpEvent->RemoveReleasedKeyItems(kitUp);
368     InputManager::GetInstance()->SimulateInputEvent(injectUpEvent);
369     std::vector<std::string> tlogs {SearchLog(command, slogs)};
370     EXPECT_TRUE(!tlogs.empty());
371 }
372 
373 HWTEST_F(InputManagerTest, MultimodalEventHandler_InjectKeyEvent_004, TestSize.Level1)
374 {
375     std::shared_ptr<OHOS::MMI::KeyEvent> injectDownEvent = OHOS::MMI::KeyEvent::Create();
376     int32_t downTime = static_cast<int32_t>(GetNanoTime()/NANOSECOND_TO_MILLISECOND);
377     OHOS::MMI::KeyEvent::KeyItem kitDown;
378     kitDown.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_UNKNOWN);
379     kitDown.SetPressed(true);
380     kitDown.SetDownTime(downTime);
381     injectDownEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_UNKNOWN);
382     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
383     injectDownEvent->AddPressedKeyItems(kitDown);
384     MMI_LOGD("MMIEventHdl.InjectEvent begin");
385     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
386     MMI_LOGD("MMIEventHdl.InjectEvent end");
387 }
388 
389 HWTEST_F(InputManagerTest, MultimodalEventHandler_InjectKeyEvent_005, TestSize.Level1)
390 {
391     std::string command = "Inject keyCode:0, action:2";
392     std::vector<std::string> slogs {SearchLog(command, true)};
393     std::shared_ptr<OHOS::MMI::KeyEvent> injectDownEvent = OHOS::MMI::KeyEvent::Create();
394     int32_t downTime = static_cast<int32_t>(GetNanoTime()/NANOSECOND_TO_MILLISECOND);
395     OHOS::MMI::KeyEvent::KeyItem kitDown;
396     kitDown.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_FN);
397     kitDown.SetPressed(true);
398     kitDown.SetDownTime(downTime);
399     injectDownEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_FN);
400     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
401     injectDownEvent->AddPressedKeyItems(kitDown);
402     if (injectDownEvent == nullptr) {
403         MMI_LOGD("injectDownEvent is nullptr!");
404     }
405     MMI_LOGD("MMIEventHdl.InjectEvent begin!");
406     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
407     MMI_LOGD("MMIEventHdl.InjectEvent end!");
408 
409     std::shared_ptr<OHOS::MMI::KeyEvent> injectUpEvent = OHOS::MMI::KeyEvent::Create();
410     downTime = static_cast<int32_t>(GetNanoTime()/NANOSECOND_TO_MILLISECOND);
411     OHOS::MMI::KeyEvent::KeyItem kitUp;
412     kitUp.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_FN);
413     kitUp.SetPressed(false);
414     kitUp.SetDownTime(downTime);
415     injectUpEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_FN);
416     injectUpEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_UP);
417     injectUpEvent->RemoveReleasedKeyItems(kitUp);
418     InputManager::GetInstance()->SimulateInputEvent(injectUpEvent);
419     std::vector<std::string> tlogs {SearchLog(command, slogs)};
420     EXPECT_TRUE(!tlogs.empty());
421 }
422 
423 #ifdef OHOS_WESTEN_MODEL
424 HWTEST_F(InputManagerTest, InputManagerTest_SetWindowInputEventConsumer_001, TestSize.Level1)
425 {
426     std::string command = "ServerStartTime =";
427     std::vector<std::string> sLogs { SearchLog(command, true) };
428 
429     auto callBackPtr = InputEventCallback::GetPtr();
430     EXPECT_TRUE(callBackPtr != nullptr);
431     InputManager::GetInstance()->SetWindowInputEventConsumer(callBackPtr);
432     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
433 
434     OHOS::MMI::KeyEvent injectDownEvent;
435     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
436     injectDownEvent.Initialize(0, ACTION_DOWN, MMI_KEY_BACK, downTime, 0, "", 0, 0, "", 0, false, 0,
437         ISINTERCEPTED_TRUE);
438     int32_t response = MMIEventHdl.InjectEvent(injectDownEvent);
439     EXPECT_TRUE(response);
440 
441     KeyEvent injectUpEvent;
442     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
443     injectUpEvent.Initialize(0, ACTION_UP, MMI_KEY_BACK, downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
444     response = MMIEventHdl.InjectEvent(injectUpEvent);
445     EXPECT_TRUE(response);
446 
447     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
448     EXPECT_TRUE(!tLogs.empty());
449 }
450 #endif
451 
DumpPointerItem(const PointerEvent::PointerItem & item)452 std::string InputManagerTest::DumpPointerItem(const PointerEvent::PointerItem &item)
453 {
454     std::ostringstream strm;
455     strm << "ClientMsgHandler: in OnPointerEvent, #[[:digit:]]\\{1,\\}, downTime=" << item.GetDownTime()
456          << ",isPressed=" << std::boolalpha << item.IsPressed() << ",globalX=" << item.GetGlobalX()
457          << ",globalY=" << item.GetGlobalY()
458          << ",localX=-\\{0,1\\}[[:digit:]]\\{1,\\},localY=-\\{0,1\\}[[:digit:]]\\{1,\\}"
459          << ",width=" << item.GetWidth() << ",height=" << item.GetHeight()
460          << ",pressure=" << item.GetPressure();
461     return strm.str();
462 }
463 
DumpPointerEvent(const std::shared_ptr<PointerEvent> & pointerEvent)464 std::string InputManagerTest::DumpPointerEvent(const std::shared_ptr<PointerEvent> &pointerEvent)
465 {
466     const int precision = 2;
467     std::ostringstream strm;
468     strm << "ClientMsgHandler: in OnPointerEvent, #[[:digit:]]\\{1,\\}, "
469          << "pointer event dispatcher of client"
470          << ", eventType=" << pointerEvent->DumpEventType()
471          << ",actionTime=" << pointerEvent->GetActionTime()
472          << ",action=" << pointerEvent->GetAction()
473          << ",actionStartTime=" << pointerEvent->GetActionStartTime()
474          << ",flag=" << pointerEvent->GetFlag()
475          << ",pointerAction=" << pointerEvent->DumpPointerAction()
476          << ",sourceType=" << pointerEvent->DumpSourceType()
477          << ",VerticalAxisValue=" << std::fixed << std::setprecision(precision)
478          << pointerEvent->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL)
479          << ",HorizontalAxisValue=" << std::fixed << std::setprecision(precision)
480          << pointerEvent->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL);
481     return strm.str();
482 }
483 
SetupPointerEvent001()484 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent001()
485 {
486     auto pointerEvent = PointerEvent::Create();
487     PointerEvent::PointerItem item;
488     item.SetPointerId(0);   // test code,set the PointerId = 0
489     item.SetGlobalX(823);   // test code,set the GlobalX = 823
490     item.SetGlobalY(723);   // test code,set the GlobalY = 723
491     item.SetPressure(5);    // test code,set the Pressure = 5
492     item.SetDeviceId(1);    // test code,set the DeviceId = 1
493     pointerEvent->AddPointerItem(item);
494 
495     item.SetPointerId(1);   // test code,set the PointerId = 1
496     item.SetGlobalX(1010);   // test code,set the GlobalX = 1010
497     item.SetGlobalY(910);   // test code,set the GlobalY = 910
498     item.SetPressure(7);    // test code,set the Pressure = 7
499     item.SetDeviceId(1);    // test code,set the DeviceId = 1
500     pointerEvent->AddPointerItem(item);
501 
502     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
503     pointerEvent->SetPointerId(1);  // test code,set the PointerId = 1
504     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
505     return pointerEvent;
506 }
507 
SetupPointerEvent002()508 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent002()
509 {
510     auto pointerEvent = PointerEvent::Create();
511     PointerEvent::PointerItem item;
512     item.SetPointerId(0);   // test code,set the PointerId = 0
513     item.SetGlobalX(823);   // test code,set the GlobalX = 823
514     item.SetGlobalY(723);   // test code,set the GlobalY = 723
515     item.SetPressure(5);    // test code,set the Pressure = 5
516     item.SetDeviceId(1);    // test code,set the DeviceId = 1
517     pointerEvent->AddPointerItem(item);
518 
519     item.SetPointerId(1);   // test code,set the PointerId = 1
520     item.SetGlobalX(1000);   // test code,set the GlobalX = 1000
521     item.SetGlobalY(610);   // test code,set the GlobalY = 610
522     item.SetPressure(7);    // test code,set the Pressure = 7
523     item.SetDeviceId(1);    // test code,set the DeviceId = 1
524     pointerEvent->AddPointerItem(item);
525 
526     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
527     pointerEvent->SetPointerId(1);  // test code,set the PointerId = 1
528     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
529     return pointerEvent;
530 }
531 
SetupPointerEvent003()532 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent003()
533 {
534     auto pointerEvent = PointerEvent::Create();
535     PointerEvent::PointerItem item;
536     item.SetPointerId(0);   // test code,set the PointerId = 0
537     item.SetGlobalX(823);   // test code,set the GlobalX = 823
538     item.SetGlobalY(723);   // test code,set the GlobalY = 723
539     item.SetPressure(5);    // test code,set the Pressure = 5
540     item.SetDeviceId(1);    // test code,set the DeviceId = 1
541     pointerEvent->AddPointerItem(item);
542 
543     item.SetPointerId(1);   // test code,set the PointerId = 1
544     item.SetGlobalX(0);   // test code,set the GlobalX = 0
545     item.SetGlobalY(0);   // test code,set the GlobalY = 0
546     item.SetPressure(0);    // test code,set the Pressure = 0
547     item.SetDeviceId(1);    // test code,set the DeviceId = 1
548     pointerEvent->AddPointerItem(item);
549 
550     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
551     pointerEvent->SetPointerId(1);  // test code,set the PointerId = 1
552     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
553     return pointerEvent;
554 }
555 
556 HWTEST_F(InputManagerTest, InputManager_SimulateInputEvent_004, TestSize.Level1)
557 {
558     auto pointerEvent = PointerEvent::Create();
559     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
560     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
561     pointerEvent->SetPointerId(-1);
562 
563     std::string command {
564         "InputWindowsManager: in UpdateTouchScreenTarget, #[[:digit:]]\\{1,\\}, "
565         "Can.t find pointer item, pointer:"
566     };
567     std::vector<std::string> sLogs { SearchLog(command, true) };
568 
569     MMI_LOGD("Call InputManager::SimulateInputEvent");
570     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
571 
572     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
573     EXPECT_TRUE(!tLogs.empty());
574 }
575 
576 /**
577  * @tc.name:InputManager_ANR_TEST
578  * @tc.desc: detection of ANR
579  * @tc.type: FUNC
580  * @tc.require: AR000GJG6G
581  */
582 HWTEST_F(InputManagerTest, InputManager_ANR_TEST_001, TestSize.Level1)
583 {
584     std::this_thread::sleep_for(std::chrono::milliseconds(20000));
585     auto pointerEvent = PointerEvent::Create();
586 
587     PointerEvent::PointerItem item;
588     item.SetPointerId(0);
589     item.SetGlobalX(823);
590     item.SetGlobalY(723);
591     item.SetPressure(5);
592     pointerEvent->AddPointerItem(item);
593     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
594     pointerEvent->SetSourceType(-1);
595     pointerEvent->SetPointerId(0);
596     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
597     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
598     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
599     MMI_LOGD("InputManager_ANR_TEST_001 wait 2s");
600     std::this_thread::sleep_for(std::chrono::milliseconds(2000));
601 
602     item.SetPointerId(1);
603     item.SetGlobalX(823);
604     item.SetGlobalY(723);
605     item.SetPressure(5);
606     pointerEvent->AddPointerItem(item);
607     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
608     pointerEvent->SetSourceType(-1);
609     pointerEvent->SetPointerId(1);
610     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
611     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
612     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
613     MMI_LOGD("InputManager_ANR_TEST_001 wait 5s");
614     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
615 
616     item.SetPointerId(2);
617     item.SetGlobalX(823);
618     item.SetGlobalY(723);
619     item.SetPressure(5);
620     pointerEvent->AddPointerItem(item);
621     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
622     pointerEvent->SetSourceType(-1);
623     pointerEvent->SetPointerId(2);
624     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
625     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
626     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
627 }
628 
629 /**
630  * @tc.name:InputManager_ANR_TEST
631  * @tc.desc: detection of ANR
632  * @tc.type: FUNC
633  * @tc.require: SR000GGN6G
634  */
635 HWTEST_F(InputManagerTest, InputManager_ANR_TEST_002, TestSize.Level1)
636 {
637     std::this_thread::sleep_for(std::chrono::milliseconds(20000));
638     auto pointerEvent = PointerEvent::Create();
639 
640     PointerEvent::PointerItem item;
641     item.SetPointerId(0);
642     item.SetGlobalX(823);
643     item.SetGlobalY(723);
644     item.SetPressure(5);
645     pointerEvent->AddPointerItem(item);
646     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
647     pointerEvent->SetSourceType(-1);
648     pointerEvent->SetPointerId(0);
649     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
650     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
651     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
652     MMI_LOGD("InputManager_ANR_TEST_001 wait 2s");
653     std::this_thread::sleep_for(std::chrono::milliseconds(2000));
654 
655     item.SetPointerId(1);
656     item.SetGlobalX(823);
657     item.SetGlobalY(723);
658     item.SetPressure(5);
659     pointerEvent->AddPointerItem(item);
660     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
661     pointerEvent->SetSourceType(-1);
662     pointerEvent->SetPointerId(1);
663     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
664     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
665     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
666     MMI_LOGD("InputManager_ANR_TEST_001 wait 5s");
667     std::this_thread::sleep_for(std::chrono::milliseconds(5000));
668 
669     item.SetPointerId(2);
670     item.SetGlobalX(823);
671     item.SetGlobalY(723);
672     item.SetPressure(5);
673     pointerEvent->AddPointerItem(item);
674     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
675     pointerEvent->SetSourceType(-1);
676     pointerEvent->SetPointerId(2);
677     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
678     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
679     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
680 }
681 
SetupPointerEvent006()682 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent006()
683 {
684     auto pointerEvent = PointerEvent::Create();
685     int64_t downTime = GetNanoTime()/NANOSECOND_TO_MILLISECOND;
686     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
687     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
688     pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
689     pointerEvent->SetPointerId(1);
690     pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
691     PointerEvent::PointerItem item;
692     item.SetPointerId(1);
693     item.SetDownTime(downTime);
694     item.SetPressed(true);
695 
696     item.SetGlobalX(200);
697     item.SetGlobalY(200);
698     item.SetLocalX(300);
699     item.SetLocalY(300);
700 
701     item.SetWidth(0);
702     item.SetHeight(0);
703     item.SetPressure(0);
704     item.SetDeviceId(0);
705     pointerEvent->AddPointerItem(item);
706     return pointerEvent;
707 }
708 
SetupPointerEvent007()709 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent007()
710 {
711     auto pointerEvent = PointerEvent::Create();
712     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
713     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
714     pointerEvent->SetPointerId(1);
715     PointerEvent::PointerItem item;
716     item.SetPointerId(1);
717     item.SetDownTime(0);
718     item.SetPressed(false);
719 
720     item.SetGlobalX(200);
721     item.SetGlobalY(200);
722     item.SetLocalX(300);
723     item.SetLocalY(300);
724 
725     item.SetWidth(0);
726     item.SetHeight(0);
727     item.SetPressure(0);
728     item.SetDeviceId(0);
729     pointerEvent->AddPointerItem(item);
730     return pointerEvent;
731 }
732 
SetupPointerEvent008()733 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent008()
734 {
735     auto pointerEvent = PointerEvent::Create();
736     int64_t downTime = GetNanoTime()/NANOSECOND_TO_MILLISECOND;
737     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
738     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
739     pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
740     pointerEvent->SetPointerId(1);
741     pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
742     PointerEvent::PointerItem item;
743     item.SetPointerId(1);
744     item.SetDownTime(downTime);
745     item.SetPressed(false);
746 
747     item.SetGlobalX(200);
748     item.SetGlobalY(200);
749     item.SetLocalX(300);
750     item.SetLocalY(300);
751 
752     item.SetWidth(0);
753     item.SetHeight(0);
754     item.SetPressure(0);
755     item.SetDeviceId(0);
756     pointerEvent->AddPointerItem(item);
757     return pointerEvent;
758 }
759 
SetupPointerEvent009()760 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent009()
761 {
762     auto pointerEvent = PointerEvent::Create();
763     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
764     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
765     pointerEvent->SetPointerId(1);
766     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, -1.0000);
767     PointerEvent::PointerItem item;
768     item.SetPointerId(1);
769     item.SetDownTime(0);
770     item.SetPressed(false);
771 
772     item.SetGlobalX(200);
773     item.SetGlobalY(200);
774     item.SetLocalX(300);
775     item.SetLocalY(300);
776 
777     item.SetWidth(0);
778     item.SetHeight(0);
779     item.SetPressure(0);
780     item.SetDeviceId(0);
781     pointerEvent->AddPointerItem(item);
782     return pointerEvent;
783 }
784 
SetupPointerEvent012()785 std::shared_ptr<PointerEvent> InputManagerTest::SetupPointerEvent012()
786 {
787     auto pointerEvent = PointerEvent::Create();
788     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
789     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
790     pointerEvent->SetPointerId(1);
791     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, 30.0);
792     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, 40.0);
793     PointerEvent::PointerItem item;
794     item.SetPointerId(1);
795     item.SetDownTime(0);
796     item.SetPressed(false);
797 
798     item.SetGlobalX(200);
799     item.SetGlobalY(200);
800     item.SetLocalX(300);
801     item.SetLocalY(300);
802 
803     item.SetWidth(0);
804     item.SetHeight(0);
805     item.SetPressure(0);
806     item.SetDeviceId(0);
807     pointerEvent->AddPointerItem(item);
808     return pointerEvent;
809 }
810 
KeyMonitorCallBack(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent)811 void InputManagerTest::KeyMonitorCallBack(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent)
812 {
813     MMI_LOGD("KeyMonitorCallBack: keyCode:%{public}d,keyAction:%{public}d,action:%{public}d,"
814              "actionTime:%{public}" PRId64 "", keyEvent->GetKeyCode(), keyEvent->GetKeyAction(),
815              keyEvent->GetAction(),  keyEvent->GetActionTime());
816     EXPECT_EQ(keyEvent->GetKeyCode(), OHOS::MMI::KeyEvent::KEYCODE_BACK);
817     EXPECT_EQ(keyEvent->GetKeyAction(), OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
818     EXPECT_EQ(keyEvent->GetAction(), OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
819     EXPECT_EQ(keyEvent->GetDeviceId(), 0);
820 }
821 
822 HWTEST_F(InputManagerTest, InputManagerTest_AddMonitor_001, TestSize.Level1)
823 {
824     RunShellUtil runCommand;
825     std::string command = "consumer is null";
826     std::vector<std::string> log;
827     ASSERT_TRUE(runCommand.RunShellCommand(command, log) == RET_OK);
828 
829     int32_t id = InputManager::GetInstance()->AddMonitor(KeyMonitorCallBack);
830     EXPECT_TRUE(IsValidHandlerId(id));
831     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
832 
833     std::shared_ptr<OHOS::MMI::KeyEvent> injectDownEvent = OHOS::MMI::KeyEvent::Create();
834     int32_t downTime = static_cast<int32_t>(GetNanoTime()/NANOSECOND_TO_MILLISECOND);
835     OHOS::MMI::KeyEvent::KeyItem kitDown;
836     kitDown.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
837     kitDown.SetPressed(true);
838     kitDown.SetDownTime(downTime);
839     injectDownEvent->SetDeviceId(0);
840     injectDownEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
841     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
842     injectDownEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
843     injectDownEvent->AddPressedKeyItems(kitDown);
844 
845     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
846     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
847 
848     InputManager::GetInstance()->RemoveMonitor(INDEX_FIRST);
849     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
850 }
851 
852 HWTEST_F(InputManagerTest, InputManagerTest_AddMonitor_002, TestSize.Level1)
853 {
854     RunShellUtil runCommand;
855     std::string command = "consumer is null";
856     std::vector<std::string> log;
857     ASSERT_TRUE(runCommand.RunShellCommand(command, log) == RET_OK);
858 
859     int32_t id = InputManager::GetInstance()->AddMonitor(KeyMonitorCallBack);
860     EXPECT_TRUE(IsValidHandlerId(id));
861     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
862 
863     id = InputManager::GetInstance()->AddMonitor(KeyMonitorCallBack);
864     EXPECT_TRUE(IsValidHandlerId(id));
865     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
866 
867     id = InputManager::GetInstance()->AddMonitor(KeyMonitorCallBack);
868     EXPECT_TRUE(IsValidHandlerId(id));
869     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
870 
871     std::shared_ptr<OHOS::MMI::KeyEvent> injectDownEvent = OHOS::MMI::KeyEvent::Create();
872     int32_t downTime = static_cast<int32_t>(GetNanoTime()/NANOSECOND_TO_MILLISECOND);
873     OHOS::MMI::KeyEvent::KeyItem kitDown;
874     kitDown.SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
875     kitDown.SetPressed(false);
876     kitDown.SetDownTime(downTime);
877     injectDownEvent->SetDeviceId(0);
878     injectDownEvent->SetKeyCode(OHOS::MMI::KeyEvent::KEYCODE_BACK);
879     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
880     injectDownEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
881     injectDownEvent->AddPressedKeyItems(kitDown);
882     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
883     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
884 
885     InputManager::GetInstance()->RemoveMonitor(INDEX_FIRST);
886     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
887     InputManager::GetInstance()->RemoveMonitor(INDEX_SECOND);
888     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
889     InputManager::GetInstance()->RemoveMonitor(INDEX_THIRD);
890     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
891     InputManager::GetInstance()->RemoveMonitor(INDEX_INVALID);
892     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
893 }
894 
895 HWTEST_F(InputManagerTest, InputManagerTest_AddHandler_001, TestSize.Level1)
896 {
897     std::string command {
898         "InputHandlerManagerGlobal: in AddMonitor, #[[:digit:]]\\{1,\\}, "
899         "Service AddMonitor Success"
900     };
901     std::vector<std::string> sLogs { SearchLog(command, true) };
902 
903     auto callBackPtr = InputEventCallback::GetPtr();
904     EXPECT_TRUE(callBackPtr != nullptr);
905     MMI_LOGD("InputManagerTest_AddHandler_001");
906     int32_t id1 = InputManager::GetInstance()->AddMonitor(callBackPtr);
907     EXPECT_TRUE(IsValidHandlerId(id1));
908 
909     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
910     EXPECT_TRUE(!tLogs.empty());
911 
912     if (IsValidHandlerId(id1)) {
913         InputManager::GetInstance()->RemoveMonitor(id1);
914         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
915     }
916 }
917 
918 HWTEST_F(InputManagerTest, InputManagerTest_AddHandler_002, TestSize.Level1)
919 {
920     auto callBackPtr = InputEventCallback::GetPtr();
921     EXPECT_TRUE(callBackPtr != nullptr);
922     int32_t id1 = InputManager::GetInstance()->AddMonitor(callBackPtr);
923     EXPECT_TRUE(IsValidHandlerId(id1));
924     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
925 
926     std::string command {
927         "InputHandlerManagerGlobal: in RemoveMonitor, #[[:digit:]]\\{1,\\}, "
928         "Service RemoveMonitor Success"
929     };
930     std::vector<std::string> sLogs { SearchLog(command, true) };
931     MMI_LOGD("InputManagerTest_AddHandler_002");
932     if (IsValidHandlerId(id1)) {
933         InputManager::GetInstance()->RemoveMonitor(id1);
934     }
935     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
936     EXPECT_TRUE(!tLogs.empty());
937 }
938 
939 HWTEST_F(InputManagerTest, InputManagerTest_AddHandler_003, TestSize.Level1)
940 {
941     const std::vector<int32_t>::size_type N_TEST_CASES { 3 };
942     std::vector<int32_t> ids(N_TEST_CASES);
943     std::vector<std::shared_ptr<InputEventCallback>> cbs(N_TEST_CASES);
944 
945     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
946         cbs[i] = InputEventCallback::GetPtr();
947         EXPECT_TRUE(cbs[i] != nullptr);
948         ids[i] = InputManager::GetInstance()->AddMonitor(cbs[i]);
949         EXPECT_TRUE(IsValidHandlerId(ids[i]));
950         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
951     }
952 
953     std::string command {
954         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
955         "PointerEvent received"
956     };
957     std::vector<std::string> sLogs { SearchLog(command, true) };
958 
959     auto pointerEvent = SetupPointerEvent001();
960     MMI_LOGD("InputManagerTest_AddHandler_003");
961     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
962     int32_t nTries { N_TRIES_FOR_LOG };
963     std::vector<std::string> rLogs;
964 
965     while (true) {
966         std::vector<std::string> tLogs { SearchLog(command, sLogs, true) };
967         rLogs.insert(rLogs.end(), tLogs.begin(), tLogs.end());
968         if ((rLogs.size() >= N_TEST_CASES) || (--nTries <= 0)) {
969             break;
970         }
971         sLogs.insert(sLogs.end(), tLogs.begin(), tLogs.end());
972         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
973     }
974     EXPECT_TRUE(rLogs.size() >= N_TEST_CASES);
975 
976     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
977         if (IsValidHandlerId(ids[i])) {
978             InputManager::GetInstance()->RemoveMonitor(ids[i]);
979             std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
980         }
981     }
982 }
983 
984 HWTEST_F(InputManagerTest, InputManagerTest_AddHandler_004, TestSize.Level1)
985 {
986     std::string command {
987         "InputHandlerManager: in AddHandler, #[[:digit:]]\\{1,\\}, "
988         "The number of handlers exceeds the maximum"
989     };
990     std::vector<std::string> sLogs { SearchLog(command, true) };
991 
992     const std::vector<int32_t>::size_type N_TEST_CASES { MAX_N_INPUT_HANDLERS };
993     std::vector<int32_t> ids(N_TEST_CASES);
994     std::shared_ptr<InputEventCallback> cb = InputEventCallback::GetPtr();
995     EXPECT_TRUE(cb != nullptr);
996 
997     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
998         ids[i] = InputManager::GetInstance()->AddMonitor(cb);
999         EXPECT_TRUE(IsValidHandlerId(ids[i]));
1000         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1001     }
1002     MMI_LOGD("InputManagerTest_AddHandler_004");
1003     int32_t monitorId = InputManager::GetInstance()->AddMonitor(cb);
1004     EXPECT_TRUE(!IsValidHandlerId(monitorId));
1005 
1006     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
1007     EXPECT_TRUE(!tLogs.empty());
1008 
1009     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
1010         if (IsValidHandlerId(ids[i])) {
1011             InputManager::GetInstance()->RemoveMonitor(ids[i]);
1012             std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1013         }
1014     }
1015 }
1016 
1017 #ifdef OHOS_WESTEN_MODEL
1018 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_001, TestSize.Level1)
1019 {
1020     std::set<int32_t> preKeys;
1021     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1022     keyOption->SetPreKeys(preKeys);
1023     keyOption->SetFinalKey(KeyEvent::KEYCODE_MENU);
1024     keyOption->SetFinalKeyDown(true);
1025     keyOption->SetFinalKeyDownDuration(0);
1026     int32_t response = -1;
1027     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
1028         [=](std::shared_ptr<KeyEvent> keyEvent)
__anondeb374380302(std::shared_ptr<KeyEvent> keyEvent) 1029     {
1030         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1031                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1032                  "EventType:%{public}d,Flag:%{public}u",
1033                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1034                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1035                  keyEvent->GetEventType(), keyEvent->GetFlag());
1036         MMI_LOGD("subscribe key event trigger callback");
1037     });
1038     EXPECT_TRUE(response >= 0);
1039     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1040 
1041     KeyEvent injectDownEvent;
1042     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1043     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_MENU,
1044                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1045     MMIEventHdl.InjectEvent(injectDownEvent);
1046     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1047 
1048     // release pressed key
1049     KeyEvent injectUpEvent;
1050     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1051     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_MENU,
1052                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1053     MMIEventHdl.InjectEvent(injectUpEvent);
1054     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1055 
1056     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1057     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1058 }
1059 #endif
1060 
1061 #ifdef OHOS_WESTEN_MODEL
1062 /**
1063  * @tc.name:InputManagerTest_SubscribeKeyEvent_002
1064  * @tc.desc:Verify the repeat subscribe key event.
1065  * @tc.type: FUNC
1066  * @tc.require: SR000GGQL4  AR000GJNGN
1067  * @tc.author: yangguang
1068  */
1069 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_002, TestSize.Level1)
1070 {
1071     std::set<int32_t> preKeys;
1072     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1073     keyOption->SetPreKeys(preKeys);
1074     keyOption->SetFinalKey(KeyEvent::KEYCODE_POWER);
1075     keyOption->SetFinalKeyDown(true);
1076     keyOption->SetFinalKeyDownDuration(0);
1077     int32_t response = -1;
1078     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
1079         [](std::shared_ptr<KeyEvent> keyEvent)
__anondeb374380402(std::shared_ptr<KeyEvent> keyEvent) 1080     {
1081         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1082                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1083                  "EventType:%{public}d,Flag:%{public}u",
1084                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1085                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1086                  keyEvent->GetEventType(), keyEvent->GetFlag());
1087         MMI_LOGD("subscribe key event trigger callback");
1088     });
1089     EXPECT_TRUE(response >= 0);
1090     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1091     int32_t response2 = -1;
1092     response2 = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
1093         [](std::shared_ptr<KeyEvent> keyEvent)
__anondeb374380502(std::shared_ptr<KeyEvent> keyEvent) 1094     {
1095         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1096                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1097                  "EventType:%{public}d,Flag:%{public}u",
1098                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1099                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1100                  keyEvent->GetEventType(), keyEvent->GetFlag());
1101         MMI_LOGD("subscribe key event trigger callback");
1102     });
1103     EXPECT_TRUE(response2 < 0);
1104     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1105 
1106     KeyEvent injectDownEvent;
1107     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1108     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_POWER,
1109                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1110     MMIEventHdl.InjectEvent(injectDownEvent);
1111     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1112 
1113     // release pressed key
1114     KeyEvent injectUpEvent;
1115     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1116     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_POWER,
1117                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1118     MMIEventHdl.InjectEvent(injectUpEvent);
1119     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1120 
1121     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1122     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1123 }
1124 #endif
1125 
1126 #ifdef OHOS_WESTEN_MODEL
1127 /**
1128  * @tc.name:InputManagerTest_SubscribeKeyEvent_003
1129  * @tc.desc:Verify the unsubscribe key event.
1130  * @tc.type: FUNC
1131  * @tc.require: SR000GGQL4  AR000GJNGN
1132  * @tc.author: yangguang
1133  */
1134 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_003, TestSize.Level1)
1135 {
1136     std::set<int32_t> preKeys;
1137     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1138     keyOption->SetPreKeys(preKeys);
1139     keyOption->SetFinalKey(KeyEvent::KEYCODE_HOME);
1140     keyOption->SetFinalKeyDown(true);
1141     keyOption->SetFinalKeyDownDuration(0);
1142     int32_t response = -1;
1143     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
1144         [](std::shared_ptr<KeyEvent> keyEvent)
__anondeb374380602(std::shared_ptr<KeyEvent> keyEvent) 1145     {
1146         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1147                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1148                  "EventType:%{public}d,Flag:%{public}u",
1149                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1150                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1151                  keyEvent->GetEventType(), keyEvent->GetFlag());
1152         MMI_LOGD("subscribe key event trigger callback");
1153     });
1154     EXPECT_TRUE(response >= 0);
1155     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1156 
1157     KeyEvent injectDownEvent;
1158     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1159     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_HOME,
1160                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1161     MMIEventHdl.InjectEvent(injectDownEvent);
1162     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1163 
1164     // release pressed key
1165     KeyEvent injectUpEvent;
1166     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1167     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_HOME,
1168                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1169     MMIEventHdl.InjectEvent(injectUpEvent);
1170     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1171 
1172     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1173     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1174 }
1175 
1176 /**
1177  * @tc.name:InputManagerTest_SubscribeKeyEvent_004
1178  * @tc.desc:Verify down trigger subscribe key event.
1179  * @tc.type: FUNC
1180  * @tc.require: SR000GGQL4  AR000GJNGN
1181  * @tc.author: yangguang
1182  */
1183 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_004, TestSize.Level1)
1184 {
1185     std::set<int32_t> preKeys;
1186     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1187     keyOption->SetPreKeys(preKeys);
1188     keyOption->SetFinalKey(KeyEvent::KEYCODE_BACK);
1189     keyOption->SetFinalKeyDown(true);
1190     keyOption->SetFinalKeyDownDuration(0);
1191     int32_t response = -1;
1192     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
1193         [](std::shared_ptr<KeyEvent> keyEvent)
__anondeb374380702(std::shared_ptr<KeyEvent> keyEvent) 1194     {
1195         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1196                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1197                  "EventType:%{public}d,Flag:%{public}u",
1198                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1199                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1200                  keyEvent->GetEventType(), keyEvent->GetFlag());
1201         MMI_LOGD("subscribe key event down trigger callback");
1202     });
1203     EXPECT_TRUE(response >= 0);
1204     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1205 
1206     KeyEvent injectDownEvent;
1207     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1208     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_BACK,
1209                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1210     MMIEventHdl.InjectEvent(injectDownEvent);
1211     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1212 
1213     // release pressed key
1214     KeyEvent injectUpEvent;
1215     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1216     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_BACK,
1217                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1218     MMIEventHdl.InjectEvent(injectUpEvent);
1219     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1220 
1221     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1222     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1223 }
1224 
1225 /**
1226  * @tc.name:InputManagerTest_SubscribeKeyEvent_005
1227  * @tc.desc:Verify down trigger subscribe key event, need to hold down for a while.
1228  * @tc.type: FUNC
1229  * @tc.require: SR000GGQL4  AR000GJNGN
1230  * @tc.author: yangguang
1231  */
1232 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_005, TestSize.Level1)
1233 {
1234     std::set<int32_t> preKeys;
1235     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1236     keyOption->SetPreKeys(preKeys);
1237     keyOption->SetFinalKey(KeyEvent::KEYCODE_CALL);
1238     keyOption->SetFinalKeyDown(true);
1239     keyOption->SetFinalKeyDownDuration(2000);
1240     int32_t response = -1;
1241     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
__anondeb374380802(std::shared_ptr<KeyEvent> keyEvent) 1242         [](std::shared_ptr<KeyEvent> keyEvent) {
1243         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1244                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1245                  "EventType:%{public}d,flag:%{public}u",
1246                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1247                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1248                  keyEvent->GetEventType(), keyEvent->GetFlag());
1249         MMI_LOGD("hold down for a while. subscribe key event down trigger callback");
1250     });
1251     EXPECT_TRUE(response >= 0);
1252     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1253 
1254     KeyEvent injectDownEvent;
1255     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1256     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_CALL,
1257                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1258     MMIEventHdl.InjectEvent(injectDownEvent);
1259     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1260 
1261     // release pressed key
1262     KeyEvent injectUpEvent;
1263     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1264     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_CALL,
1265                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1266     MMIEventHdl.InjectEvent(injectUpEvent);
1267     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1268 
1269     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1270     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1271 }
1272 
1273 /**
1274  * @tc.name:InputManagerTest_SubscribeKeyEvent_006
1275  * @tc.desc:Verify down trigger subscribe key event, other keys are pressed during the hold time.
1276  * @tc.type: FUNC
1277  * @tc.require: SR000GGQL4  AR000GJNGN
1278  * @tc.author: yangguang
1279  */
1280 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_006, TestSize.Level1)
1281 {
1282     std::set<int32_t> preKeys;
1283     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1284     keyOption->SetPreKeys(preKeys);
1285     keyOption->SetFinalKey(KeyEvent::KEYCODE_ENDCALL);
1286     keyOption->SetFinalKeyDown(true);
1287     keyOption->SetFinalKeyDownDuration(2000);
1288     int32_t response = -1;
1289     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
__anondeb374380902(std::shared_ptr<KeyEvent> keyEvent) 1290         [](std::shared_ptr<KeyEvent> keyEvent) {
1291         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1292                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1293                  "EventType:%{public}d,flag:%{public}u",
1294                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1295                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1296                  keyEvent->GetEventType(), keyEvent->GetFlag());
1297         MMI_LOGD("hold down for a while. subscribe key event down trigger callback");
1298     });
1299     EXPECT_TRUE(response >= 0);
1300     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1301     KeyEvent injectDownEvent;
1302     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1303     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_ENDCALL,
1304                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1305     MMIEventHdl.InjectEvent(injectDownEvent);
1306     // other keys are pressed during the hold time
1307     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1308 
1309     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1310     OHOS::KeyEvent injectDownEvent2;
1311     injectDownEvent2.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_VOLUME_UP,
1312                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1313     MMIEventHdl.InjectEvent(injectDownEvent2);
1314     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1315 
1316     // release pressed key1
1317     OHOS::KeyEvent injectUpEvent1;
1318     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1319     injectUpEvent1.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_ENDCALL,
1320                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1321     MMIEventHdl.InjectEvent(injectUpEvent1);
1322     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1323 
1324     // release pressed key2
1325     OHOS::KeyEvent injectUpEvent2;
1326     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1327     injectUpEvent2.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_VOLUME_UP,
1328                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1329     MMIEventHdl.InjectEvent(injectUpEvent2);
1330     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1331 
1332     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1333     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1334 }
1335 
1336 /**
1337  * @tc.name:InputManagerTest_SubscribeKeyEvent_007
1338  * @tc.desc:Verify up trigger subscribe key event.
1339  * @tc.type: FUNC
1340  * @tc.require: SR000GGQL4  AR000GJNGN
1341  * @tc.author: yangguang
1342  */
1343 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_007, TestSize.Level1)
1344 {
1345     std::set<int32_t> preKeys;
1346     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1347     keyOption->SetPreKeys(preKeys);
1348     keyOption->SetFinalKey(KeyEvent::KEYCODE_VOLUME_DOWN);
1349     keyOption->SetFinalKeyDown(false);
1350     keyOption->SetFinalKeyDownDuration(0);
1351     int32_t response = -1;
1352     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
__anondeb374380a02(std::shared_ptr<KeyEvent> keyEvent) 1353         [](std::shared_ptr<KeyEvent> keyEvent) {
1354         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1355                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1356                  "EventType:%{public}d,flag:%{public}u",
1357                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1358                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1359                  keyEvent->GetEventType(), keyEvent->GetFlag());
1360         MMI_LOGD("subscribe key event up trigger callback");
1361     });
1362     EXPECT_TRUE(response >= 0);
1363     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1364 
1365     int64_t downTime = GetNanoTime()/NANOSECOND_TO_MILLISECOND;
1366     KeyEvent injectDownEvent;
1367     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_VOLUME_DOWN,
1368                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1369     MMIEventHdl.InjectEvent(injectDownEvent);
1370     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1371 
1372     // release pressed key , up trigger
1373     KeyEvent injectUpEvent;
1374     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1375     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_VOLUME_DOWN,
1376                              downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1377     MMIEventHdl.InjectEvent(injectUpEvent);
1378     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1379 
1380     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1381     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1382 }
1383 #endif
1384 
1385 /**
1386  * @tc.name:InputManagerTest_SubscribeKeyEvent_008
1387  * @tc.desc:Verify invalid parameter.
1388  * @tc.type: FUNC
1389  * @tc.require: SR000GGQL4  AR000GJNGN
1390  * @tc.author: yangguang
1391  */
1392 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_008, TestSize.Level1)
1393 {
1394     std::set<int32_t> preKeys;
1395     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1396     keyOption->SetPreKeys(preKeys);
1397     keyOption->SetFinalKey(KeyEvent::KEYCODE_VOLUME_MUTE);
1398     keyOption->SetFinalKeyDown(true);
1399     keyOption->SetFinalKeyDownDuration(0);
1400     int32_t response = -1;
1401     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, nullptr);
1402     EXPECT_TRUE(response < 0);
1403     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1404 }
1405 
1406 #ifdef OHOS_WESTEN_MODEL
1407 /**
1408  * @tc.name:InputManagerTest_SubscribeKeyEvent_009
1409  * @tc.desc:Verify subscribe different key event.
1410  * @tc.type: FUNC
1411  * @tc.require: SR000GGQL4  AR000GJNGN
1412  * @tc.author: yangguang
1413  */
1414 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_009, TestSize.Level1)
1415 {
1416     std::set<int32_t> preKeys;
1417     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1418     keyOption->SetPreKeys(preKeys);
1419     keyOption->SetFinalKey(KeyEvent::KEYCODE_MUTE);
1420     keyOption->SetFinalKeyDown(true);
1421     keyOption->SetFinalKeyDownDuration(0);
1422     // KEYCODE_MUTE, KEYCODE_HEADSETHOOK, MEDIA_PLAY, MEDIA_PAUSE, MEDIA_PLAY_PAUSE
1423     int32_t response = -1;
1424     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
__anondeb374380b02(std::shared_ptr<KeyEvent> keyEvent) 1425         [](std::shared_ptr<KeyEvent> keyEvent) {
1426         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1427                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1428                  "EventType:%{public}d,flag:%{public}u",
1429                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1430                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1431                  keyEvent->GetEventType(), keyEvent->GetFlag());
1432         MMI_LOGD("subscribe key event KEYCODE_MUTE trigger callback");
1433     });
1434     EXPECT_TRUE(response >= 0);
1435     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1436 
1437     KeyEvent injectDownEvent;
1438     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1439     injectDownEvent.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_MUTE,
1440                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1441     MMIEventHdl.InjectEvent(injectDownEvent);
1442     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1443 
1444     // release pressed key
1445     KeyEvent injectUpEvent;
1446     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1447     injectUpEvent.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_MUTE,
1448                              downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1449     MMIEventHdl.InjectEvent(injectUpEvent);
1450     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1451 
1452     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1453     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1454 }
1455 #endif
1456 
1457 /**
1458  * @tc.name:InputManagerTest_SubscribeKeyEvent_010
1459  * @tc.desc:Verify subscribe power key event.
1460  * @tc.type: FUNC
1461  * @tc.require: SR000GGQL4  AR000GJNGN
1462  * @tc.author: zhaoxueyuan
1463  */
1464 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_010, TestSize.Level1)
1465 {
1466     if (!MultimodalEventHandler::GetInstance().GetMMIClient()) {
1467         MMI_LOGD("get mmi client failed");
1468         return;
1469     }
1470     // 电源键长按按下订阅
1471     std::set<int32_t> preKeys;
1472     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1473     keyOption->SetPreKeys(preKeys);
1474     keyOption->SetFinalKey(KeyEvent::KEYCODE_POWER);
1475     keyOption->SetFinalKeyDown(true);
1476     keyOption->SetFinalKeyDownDuration(2000);
1477     int32_t subscribeId1 = -1;
1478     subscribeId1 = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
__anondeb374380c02(std::shared_ptr<KeyEvent> keyEvent) 1479         [](std::shared_ptr<KeyEvent> keyEvent) {
1480         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1481                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1482                  "EventType:%{public}d,flag:%{public}u",
1483                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1484                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1485                  keyEvent->GetEventType(), keyEvent->GetFlag());
1486         MMI_LOGD("subscribe key event KEYCODE_POWER down trigger callback");
1487     });
1488     EXPECT_TRUE(subscribeId1 >= 0);
1489 
1490     // 电源键抬起订阅
1491     std::shared_ptr<KeyOption> keyOption2 = std::make_shared<KeyOption>();
1492     keyOption2->SetPreKeys(preKeys);
1493     keyOption2->SetFinalKey(KeyEvent::KEYCODE_POWER);
1494     keyOption2->SetFinalKeyDown(false);
1495     keyOption2->SetFinalKeyDownDuration(0);
1496     int32_t subscribeId2 = -1;
1497     subscribeId2 = InputManager::GetInstance()->SubscribeKeyEvent(keyOption2,
__anondeb374380d02(std::shared_ptr<KeyEvent> keyEvent) 1498         [](std::shared_ptr<KeyEvent> keyEvent) {
1499         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1500                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1501                  "EventType:%{public}d,flag:%{public}u",
1502                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1503                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1504                  keyEvent->GetEventType(), keyEvent->GetFlag());
1505         MMI_LOGD("subscribe key event KEYCODE_POWER up trigger callback");
1506     });
1507     EXPECT_TRUE(subscribeId2 >= 0);
1508 
1509     std::this_thread::sleep_for(std::chrono::milliseconds(10000));
1510     InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId1);
1511     InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId2);
1512     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
1513 }
1514 
1515 #ifdef OHOS_WESTEN_MODEL
1516 /**
1517  * @tc.name:InputManagerTest_SubscribeKeyEvent_011
1518  * @tc.desc:Verify subscribe F1 key event.
1519  * @tc.type: FUNC
1520  * @tc.require: SR000GGQL4  AR000GJNGN
1521  * @tc.author: wanghao
1522  */
1523 HWTEST_F(InputManagerTest, InputManagerTest_SubscribeKeyEvent_011, TestSize.Level1)
1524 {
1525     if (!MultimodalEventHandler::GetInstance().GetMMIClient()) {
1526         MMI_LOGD("get mmi client failed");
1527         return;
1528     }
1529 
1530     std::set<int32_t> preKeys;
1531     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
1532     keyOption->SetPreKeys(preKeys);
1533     keyOption->SetFinalKey(KeyEvent::KEYCODE_F1);
1534     keyOption->SetFinalKeyDown(true);
1535     keyOption->SetFinalKeyDownDuration(0);
1536     int32_t response = -1;
1537     response = InputManager::GetInstance()->SubscribeKeyEvent(keyOption,
1538         [=](std::shared_ptr<KeyEvent> keyEvent)
__anondeb374380e02(std::shared_ptr<KeyEvent> keyEvent) 1539     {
1540         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1541                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1542                  "EventType:%{public}d,flag:%{public}u",
1543                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1544                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1545                  keyEvent->GetEventType(), keyEvent->GetFlag());
1546         MMI_LOGD("subscribe key event KEYCODE_F1 down trigger callback");
1547     });
1548     EXPECT_TRUE(response > 0);
1549 
1550     std::shared_ptr<KeyOption> keyOption2 = std::make_shared<KeyOption>();
1551     keyOption2->SetPreKeys(preKeys);
1552     keyOption2->SetFinalKey(KeyEvent::KEYCODE_F1);
1553     keyOption2->SetFinalKeyDown(false);
1554     keyOption2->SetFinalKeyDownDuration(0);
1555     int32_t subscribeId2 = -1;
1556     subscribeId2 = InputManager::GetInstance()->SubscribeKeyEvent(keyOption2,
__anondeb374380f02(std::shared_ptr<KeyEvent> keyEvent) 1557         [](std::shared_ptr<KeyEvent> keyEvent) {
1558         MMI_LOGD("KeyEvent:%{public}d,KeyCode:%{public}d,ActionTime:%{public}" PRId64 ","
1559                  "ActionStartTime:%{public}" PRId64 ",Action:%{public}d,KeyAction:%{public}d,"
1560                  "EventType:%{public}d,flag:%{public}u",
1561                  keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetActionTime(),
1562                  keyEvent->GetActionStartTime(), keyEvent->GetAction(), keyEvent->GetKeyAction(),
1563                  keyEvent->GetEventType(), keyEvent->GetFlag());
1564         MMI_LOGD("subscribe key event KEYCODE_F1 up trigger callback");
1565     });
1566     EXPECT_TRUE(subscribeId2 > 0);
1567 
1568     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1569 
1570     // pressed key
1571     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1572     OHOS::KeyEvent injectDownEvent1;
1573     injectDownEvent1.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_F1,
1574                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1575     MMIEventHdl.InjectEvent(injectDownEvent1);
1576     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1577 
1578     // release pressed key
1579     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1580     OHOS::KeyEvent injectUpEven1;
1581     injectUpEven1.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_F1,
1582                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1583     MMIEventHdl.InjectEvent(injectUpEven1);
1584     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1585 
1586     // pressed key
1587     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1588     OHOS::KeyEvent injectDownEvent2;
1589     injectDownEvent2.Initialize(0, ACTION_DOWN, KeyEvent::KEYCODE_F1,
1590                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1591     MMIEventHdl.InjectEvent(injectDownEvent2);
1592     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1593 
1594     // release pressed key
1595     downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
1596     OHOS::KeyEvent injectUpEvent2;
1597     injectUpEvent2.Initialize(0, ACTION_UP, KeyEvent::KEYCODE_F1,
1598                                downTime, 0, "", 0, 0, "", 0, false, 0, ISINTERCEPTED_TRUE);
1599     MMIEventHdl.InjectEvent(injectUpEvent2);
1600     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1601 
1602     InputManager::GetInstance()->UnsubscribeKeyEvent(response);
1603     InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId2);
1604     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1605 }
1606 #endif
1607 
1608 class InputEventInterceptor : public OHOS::MMI::IInputEventConsumer {
1609 public:
OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const1610     virtual void OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const override { }
1611     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const1612     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {}
1613     static std::shared_ptr<OHOS::MMI::IInputEventConsumer> GetPtr();
1614 };
1615 
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const1616 void InputEventInterceptor::OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const
1617 {
1618     std::vector<int32_t> pointerIds { pointerEvent->GetPointersIdList() };
1619     MMI_LOGD("Pointer event intercepted:");
1620     MMI_LOGD("EventType:%{public}s, actionTime:%{public}" PRId64 ","
1621              "action:%{public}d,actionStartTime:%{public}" PRId64 ","
1622              "flag:%{public}u,pointerAction:%{public}s, sourceType:%{public}s, "
1623              "VerticalAxisValue:%{public}.2f, HorizontalAxisValue:%{public}.2f, "
1624              "pointerCount:%{public}zu",
1625              pointerEvent->DumpEventType(), pointerEvent->GetActionTime(),
1626              pointerEvent->GetAction(), pointerEvent->GetActionStartTime(),
1627              pointerEvent->GetFlag(), pointerEvent->DumpPointerAction(),
1628              pointerEvent->DumpSourceType(),
1629              pointerEvent->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL),
1630              pointerEvent->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL),
1631              pointerIds.size());
1632     for (int32_t pointerId : pointerIds) {
1633         OHOS::MMI::PointerEvent::PointerItem item;
1634         CHK(pointerEvent->GetPointerItem(pointerId, item), PARAM_INPUT_FAIL);
1635 
1636         MMI_LOGD("DownTime:%{public}" PRId64 ",isPressed:%{public}s,"
1637                  "globalX:%{public}d,globalY:%{public}d,pressure:%{public}d",
1638                  item.GetDownTime(),
1639                  item.IsPressed() ? "true" : "false",
1640                  item.GetGlobalX(),
1641                  item.GetGlobalY(),
1642                  item.GetPressure());
1643     }
1644 }
1645 
GetPtr()1646 std::shared_ptr<OHOS::MMI::IInputEventConsumer> InputEventInterceptor::GetPtr()
1647 {
1648     return std::make_shared<InputEventInterceptor>();
1649 }
1650 
DumpPointerItem2(const PointerEvent::PointerItem & item)1651 std::string InputManagerTest::DumpPointerItem2(const PointerEvent::PointerItem &item)
1652 {
1653     std::ostringstream strm;
1654     strm << "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, downTime=" << item.GetDownTime()
1655          << ",isPressed=" << std::boolalpha << item.IsPressed() << ",globalX=" << item.GetGlobalX()
1656          << ",globalY=" << item.GetGlobalY() << ",pressure=" << item.GetPressure();
1657     return strm.str();
1658 }
1659 
DumpPointerEvent2(const std::shared_ptr<PointerEvent> & pointerEvent)1660 std::string InputManagerTest::DumpPointerEvent2(const std::shared_ptr<PointerEvent> &pointerEvent)
1661 {
1662     const int precision = 2;
1663     std::ostringstream strm;
1664     strm << "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
1665          << "eventType=" << pointerEvent->DumpEventType()
1666          << ",actionTime=" << pointerEvent->GetActionTime()
1667          << ",action=" << pointerEvent->GetAction()
1668          << ",actionStartTime=" << pointerEvent->GetActionStartTime()
1669          << ",flag=" << pointerEvent->GetFlag()
1670          << ",pointerAction=" << pointerEvent->DumpPointerAction()
1671          << ",sourceType=" << pointerEvent->DumpSourceType()
1672          << ",VerticalAxisValue=" << std::fixed << std::setprecision(precision)
1673          << pointerEvent->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL)
1674          << ",HorizontalAxisValue=" << std::fixed << std::setprecision(precision)
1675          << pointerEvent->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL);
1676     return strm.str();
1677 }
1678 
TestInputEventInterceptor(std::shared_ptr<PointerEvent> pointerEvent)1679 void InputManagerTest::TestInputEventInterceptor(std::shared_ptr<PointerEvent> pointerEvent)
1680 {
1681     std::string sCmd {
1682         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
1683         "Pointer event intercepted:"
1684     };
1685     std::vector<std::string> sLogs { SearchLog(sCmd, true) };
1686 
1687     std::string sPointeE { DumpPointerEvent2(pointerEvent) };
1688     std::vector<std::string> sLogPointerEs { SearchLog(sPointeE, true) };
1689     MMI_LOGD("sPointerE:%{public}s", sPointeE.c_str());
1690 
1691     PointerEvent::PointerItem item;
1692     EXPECT_TRUE(pointerEvent->GetPointerItem(DEFAULT_POINTER_ID, item));
1693     std::string sItem1 { DumpPointerItem2(item) };
1694     std::vector<std::string> sLogItem1s { SearchLog(sItem1, true) };
1695     MMI_LOGD("sItem1:%{public}s", sItem1.c_str());
1696 
1697     MMI_LOGD("Call InputManager::SimulateInputEvent");
1698     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1699     int32_t nTries { N_TRIES_FOR_LOG };
1700     // 这里主要测试以下两方面:
1701     //   (1) 拦截器可以成功接收到事件;
1702     //   (2) 拦截器接收到的事件结构的各个字段与初始设置的值一致;
1703     // 为此,这里有三项测试:
1704     //   (1) 拦截器成功接收到PointerEvent事件;
1705     //   (2) PointerEvent记录的按下手指的数据的各字段与设置的值是一致的;
1706     //   (3) PointerEvent结构各字段的值与设置的值是一致的;
1707     // 这三项测试各自成功与否依次由states[0]、states[1]和states[2]标识;
1708     std::bitset<3> states { };
1709 
1710     while (true) {
1711         if (!states.test(0)) {
1712             // 搜索标识拦截器成功接收到事件的关键性日志;
1713             std::vector<std::string> tLogs { SearchLog(sCmd, sLogs, true) };
1714             if (!tLogs.empty()) {
1715                 states.set(0);
1716             }
1717         }
1718         if (!states.test(1)) {
1719             // 搜索日志,匹配PointerEvent事件结构的数据;
1720             std::vector<std::string> tLogPointerEs { SearchLog(sPointeE, sLogPointerEs, true) };
1721             if (!tLogPointerEs.empty()) {
1722                 states.set(1);
1723             }
1724         }
1725         if (!states.test(2)) {
1726             // 搜索日志,匹配按下手指的数据;
1727             std::vector<std::string> tLogItem1s { SearchLog(sItem1, sLogItem1s, true) };
1728             if (!tLogItem1s.empty()) {
1729                 states.set(2);
1730             }
1731         }
1732         if (states.all() || (--nTries <= 0)) {
1733             break;
1734         }
1735         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
1736     }
1737     EXPECT_TRUE(states.all());
1738     EXPECT_TRUE(states.test(0));
1739     EXPECT_TRUE(states.test(1));
1740     EXPECT_TRUE(states.test(2));
1741 }
1742 
1743 HWTEST_F(InputManagerTest, TestInputEventInterceptor_002, TestSize.Level1)
1744 {
1745     auto pointerEvent = PointerEvent::Create();
1746     PointerEvent::PointerItem item;
1747     item.SetPointerId(0);
1748     item.SetDownTime(10010);
1749     item.SetPressed(true);
1750     item.SetGlobalX(823);
1751     item.SetGlobalY(723);
1752     item.SetDeviceId(1);
1753     pointerEvent->AddPointerItem(item);
1754 
1755     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
1756     pointerEvent->SetPointerId(0);
1757     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
1758     MMI_LOGD("Call InterceptorManager");
1759 
1760     const std::vector<int32_t>::size_type N_TEST_CASES { 3 };
1761     std::vector<int32_t> ids(N_TEST_CASES);
1762     std::shared_ptr<OHOS::MMI::IInputEventConsumer> interceptor { InputEventInterceptor::GetPtr() };
1763 
1764     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
1765         ids[i] = InputManager::GetInstance()->AddInterceptor(interceptor);
1766         EXPECT_TRUE(IsValidHandlerId(ids[i]));
1767         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1768     }
1769 
1770     std::string command {
1771         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
1772         "Pointer event intercepted"
1773     };
1774     std::vector<std::string> sLogs { SearchLog(command, true) };
1775 
1776     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1777     int32_t nTries { N_TRIES_FOR_LOG };
1778     std::vector<std::string> rLogs;
1779 
1780     while (true) {
1781         std::vector<std::string> tLogs { SearchLog(command, sLogs, true) };
1782         rLogs.insert(rLogs.end(), tLogs.begin(), tLogs.end());
1783         if ((rLogs.size() >= N_TEST_CASES) || (--nTries <= 0)) {
1784             break;
1785         }
1786         sLogs.insert(sLogs.end(), tLogs.begin(), tLogs.end());
1787         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
1788     }
1789     EXPECT_TRUE(rLogs.size() >= N_TEST_CASES);
1790 
1791     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
1792         if (IsValidHandlerId(ids[i])) {
1793             InputManager::GetInstance()->RemoveInterceptor(ids[i]);
1794             std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1795         }
1796     }
1797 }
1798 
1799 HWTEST_F(InputManagerTest, TestInputEventInterceptor_003, TestSize.Level1)
1800 {
1801     const std::vector<int32_t>::size_type N_TEST_CASES { 3 };
1802     std::vector<int32_t> ids(N_TEST_CASES);
1803     std::shared_ptr<OHOS::MMI::IInputEventConsumer> interceptor { InputEventInterceptor::GetPtr() };
1804 
1805     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
1806         ids[i] = InputManager::GetInstance()->AddInterceptor(interceptor);
1807         EXPECT_TRUE(IsValidHandlerId(ids[i]));
1808         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1809     }
1810 
1811     std::string command {
1812         "InputHandlerManagerGlobal: in RemoveInterceptor, #[[:digit:]]\\{1,\\}, "
1813         "Unregister interceptor successfully"
1814     };
1815     std::vector<std::string> sLogs { SearchLog(command, true) };
1816 
1817     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
1818         if (IsValidHandlerId(ids[i])) {
1819             InputManager::GetInstance()->RemoveInterceptor(ids[i]);
1820             std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1821         }
1822     }
1823 
1824     int32_t nTries { N_TRIES_FOR_LOG };
1825     std::vector<std::string> rLogs;
1826 
1827     while (true) {
1828         std::vector<std::string> tLogs { SearchLog(command, sLogs, true) };
1829         rLogs.insert(rLogs.end(), tLogs.begin(), tLogs.end());
1830         if ((rLogs.size() >= N_TEST_CASES) || (--nTries <= 0)) {
1831             break;
1832         }
1833         sLogs.insert(sLogs.end(), tLogs.begin(), tLogs.end());
1834         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
1835     }
1836     EXPECT_TRUE(rLogs.size() >= N_TEST_CASES);
1837 }
1838 
1839 HWTEST_F(InputManagerTest, TestInputEventInterceptor_004, TestSize.Level1)
1840 {
1841     std::shared_ptr<KeyEvent> injectDownEvent = KeyEvent::Create();
1842     ASSERT_TRUE(injectDownEvent != nullptr);
1843     int64_t downTime = GetNanoTime()/NANOSECOND_TO_MILLISECOND;
1844     KeyEvent::KeyItem kitDown;
1845     kitDown.SetKeyCode(KeyEvent::KEYCODE_BACK);
1846     kitDown.SetPressed(true);
1847     kitDown.SetDownTime(downTime);
1848     injectDownEvent->SetDeviceId(0);
1849     injectDownEvent->SetKeyCode(KeyEvent::KEYCODE_BACK);
1850     injectDownEvent->SetAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
1851     injectDownEvent->SetKeyAction(OHOS::MMI::KeyEvent::KEY_ACTION_DOWN);
1852     injectDownEvent->AddPressedKeyItems(kitDown);
1853 
1854     int32_t interceptorId { InputManager::GetInstance()->AddInterceptor(KeyMonitorCallBack) };
1855     EXPECT_TRUE(IsValidHandlerId(interceptorId));
1856     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
1857     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1858 
1859     if (IsValidHandlerId(interceptorId)) {
1860         InputManager::GetInstance()->RemoveInterceptor(interceptorId);
1861         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1862     }
1863 }
1864 
TestInputEventInterceptor2(std::shared_ptr<PointerEvent> pointerEvent)1865 void InputManagerTest::TestInputEventInterceptor2(std::shared_ptr<PointerEvent> pointerEvent)
1866 {
1867     std::string sCmd {
1868         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
1869         "Pointer event intercepted:"
1870     };
1871     std::vector<std::string> sLogs { SearchLog(sCmd, true) };
1872 
1873     std::string sPointeE { DumpPointerEvent2(pointerEvent) };
1874     std::vector<std::string> sLogPointerEs { SearchLog(sPointeE, true) };
1875 
1876     PointerEvent::PointerItem item;
1877     pointerEvent->GetPointerItem(0, item);
1878     std::string sItem1 { DumpPointerItem2(item) };
1879     std::vector<std::string> sLogItem1s { SearchLog(sItem1, true) };
1880 
1881     pointerEvent->GetPointerItem(1, item);
1882     std::string sItem2 { DumpPointerItem2(item) };
1883     std::vector<std::string> sLogItem2s { SearchLog(sItem2, true) };
1884 
1885     MMI_LOGD("Call InputManager::SimulateInputEvent");
1886     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1887     int32_t nTries { N_TRIES_FOR_LOG };
1888     std::bitset<4> states { };
1889 
1890     while (true) {
1891         if (!states.test(0)) {
1892             std::vector<std::string> tLogs { SearchLog(sCmd, sLogs, true) };
1893             if (!tLogs.empty()) {
1894                 states.set(0);
1895             }
1896         }
1897         if (!states.test(1)) {
1898             std::vector<std::string> tLogPointerEs { SearchLog(sPointeE, sLogPointerEs, true) };
1899             if (!tLogPointerEs.empty()) {
1900                 states.set(1);
1901             }
1902         }
1903         if (!states.test(2)) {
1904             std::vector<std::string> tLogItem1s { SearchLog(sItem1, sLogItem1s, true) };
1905             if (!tLogItem1s.empty()) {
1906                 states.set(2);
1907             }
1908         }
1909         if (!states.test(3)) {
1910             std::vector<std::string> tLogItem2s { SearchLog(sItem2, sLogItem2s, true) };
1911             if (!tLogItem2s.empty()) {
1912                 states.set(3);
1913             }
1914         }
1915         if (states.all() || (--nTries <= 0)) {
1916             break;
1917         }
1918         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
1919     }
1920     EXPECT_TRUE(states.all());
1921 }
1922 
TouchPadMonitorCallBack(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent)1923 void InputManagerTest::TouchPadMonitorCallBack(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent)
1924 {
1925     int32_t pointerId = pointerEvent->GetPointerId();
1926     OHOS::MMI::PointerEvent::PointerItem pointerItem;
1927     pointerEvent->GetPointerItem(pointerId, pointerItem);
1928     MMI_LOGD("TouchPadMonitorCallBack: pointerAction:%{public}d,pointerId:%{public}d,"
1929         "x:%{public}d,y:%{public}d", pointerEvent->GetPointerAction(),
1930         pointerEvent->GetPointerId(), pointerItem.GetGlobalX(), pointerItem.GetGlobalY());
1931 }
1932 
1933 HWTEST_F(InputManagerTest, InputManagerTest_OnAddTouchPadMonitor_001, TestSize.Level1)
1934 {
1935     auto pointerEvent = PointerEvent::Create();
1936     PointerEvent::PointerItem item;
1937     item.SetPointerId(0);
1938     item.SetDownTime(10010);
1939     item.SetPressed(true);
1940     item.SetGlobalX(823);
1941     item.SetGlobalY(723);
1942     item.SetDeviceId(1);
1943     pointerEvent->AddPointerItem(item);
1944 
1945     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
1946     pointerEvent->SetPointerId(0);
1947     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
1948     MMI_LOGD("Call MontiorManager");
1949 
1950     std::string command { "PointerEvent received" };
1951     std::vector<std::string> sLogs { SearchLog(command, true) };
1952 
1953     int32_t monitorId { };
1954     auto callBackPtr = InputEventCallback::GetPtr();
1955     EXPECT_TRUE(callBackPtr != nullptr);
1956     monitorId = InputManager::GetInstance()->AddMonitor(callBackPtr);
1957     EXPECT_TRUE(IsValidHandlerId(monitorId));
1958     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1959 
1960     int32_t response = MMIEventHdl.InjectPointerEvent(pointerEvent);
1961     EXPECT_EQ(RET_OK, response);
1962 
1963     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
1964     EXPECT_TRUE(!tLogs.empty());
1965 
1966     InputManager::GetInstance()->RemoveMonitor(monitorId);
1967     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1968 }
1969 
1970 HWTEST_F(InputManagerTest, InputManagerTest_OnAddTouchPadMonitor_002, TestSize.Level1)
1971 {
1972     auto pointerEvent = PointerEvent::Create();
1973     PointerEvent::PointerItem item;
1974     item.SetPointerId(0);
1975     item.SetDownTime(10010);
1976     item.SetPressed(true);
1977     item.SetGlobalX(823);
1978     item.SetGlobalY(723);
1979     item.SetDeviceId(1);
1980     pointerEvent->AddPointerItem(item);
1981 
1982     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
1983     pointerEvent->SetPointerId(0);
1984     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
1985     MMI_LOGD("Call MontiorManager");
1986 
1987     std::string command { "PointerEvent received" };
1988     std::vector<std::string> sLogs { SearchLog(command, true) };
1989 
1990     int32_t monitorId { };
1991     auto callBackPtr = InputEventCallback::GetPtr();
1992     EXPECT_TRUE(callBackPtr != nullptr);
1993     monitorId = InputManager::GetInstance()->AddMonitor(callBackPtr);
1994     EXPECT_TRUE(IsValidHandlerId(monitorId));
1995     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
1996 
1997     int32_t response = MMIEventHdl.InjectPointerEvent(pointerEvent);
1998     EXPECT_EQ(RET_OK, response);
1999 
2000     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2001     EXPECT_TRUE(!tLogs.empty());
2002 
2003     InputManager::GetInstance()->RemoveMonitor(monitorId);
2004     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2005 }
2006 
2007 HWTEST_F(InputManagerTest, InputManagerTest_OnAddTouchPadMonitor_003, TestSize.Level1)
2008 {
2009     auto pointerEvent = PointerEvent::Create();
2010     PointerEvent::PointerItem item;
2011     item.SetPointerId(0);
2012     item.SetDownTime(10010);
2013     item.SetPressed(true);
2014     item.SetGlobalX(823);
2015     item.SetGlobalY(723);
2016     item.SetDeviceId(1);
2017     pointerEvent->AddPointerItem(item);
2018 
2019     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
2020     pointerEvent->SetPointerId(0);
2021     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
2022     MMI_LOGD("Call MontiorManager");
2023 
2024     std::string command { "PointerEvent received" };
2025     std::vector<std::string> sLogs { SearchLog(command, true) };
2026 
2027     int32_t monitorId { };
2028     auto callBackPtr = InputEventCallback::GetPtr();
2029     EXPECT_TRUE(callBackPtr != nullptr);
2030     monitorId = InputManager::GetInstance()->AddMonitor(callBackPtr);
2031     EXPECT_TRUE(IsValidHandlerId(monitorId));
2032     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2033 
2034     int32_t response = MMIEventHdl.InjectPointerEvent(pointerEvent);
2035     EXPECT_EQ(RET_OK, response);
2036 
2037     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2038     EXPECT_TRUE(!tLogs.empty());
2039 
2040     InputManager::GetInstance()->RemoveMonitor(monitorId);
2041     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2042 }
2043 
2044 HWTEST_F(InputManagerTest, InputManagerTest_OnAddTouchPadMonitor_004, TestSize.Level1)
2045 {
2046     auto pointerEvent = PointerEvent::Create();
2047     PointerEvent::PointerItem item;
2048     item.SetPointerId(0);
2049     item.SetDownTime(10010);
2050     item.SetPressed(true);
2051     item.SetGlobalX(823);
2052     item.SetGlobalY(723);
2053     item.SetDeviceId(1);
2054     pointerEvent->AddPointerItem(item);
2055 
2056     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
2057     pointerEvent->SetPointerId(0);
2058     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
2059     MMI_LOGD("Call MontiorManager");
2060 
2061     const std::vector<int32_t>::size_type N_TEST_CASES { 3 };
2062     std::vector<int32_t> ids(N_TEST_CASES);
2063 
2064     auto callBackPtr = InputEventCallback::GetPtr();
2065     EXPECT_TRUE(callBackPtr != nullptr);
2066     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
2067         ids[i] = InputManager::GetInstance()->AddMonitor(callBackPtr);
2068         EXPECT_TRUE(IsValidHandlerId(ids[i]));
2069         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2070     }
2071 
2072     std::string command {
2073         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2074         "PointerEvent received"
2075     };
2076     std::vector<std::string> sLogs { SearchLog(command, true) };
2077 
2078     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
2079     int32_t nTries { N_TRIES_FOR_LOG };
2080     std::vector<std::string> rLogs;
2081 
2082     while (true) {
2083         std::vector<std::string> tLogs { SearchLog(command, sLogs, true) };
2084         rLogs.insert(rLogs.end(), tLogs.begin(), tLogs.end());
2085         if ((rLogs.size() >= N_TEST_CASES) || (--nTries <= 0)) {
2086             break;
2087         }
2088         sLogs.insert(sLogs.end(), tLogs.begin(), tLogs.end());
2089         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_LOG));
2090     }
2091     EXPECT_TRUE(rLogs.size() >= N_TEST_CASES);
2092 
2093     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
2094         InputManager::GetInstance()->RemoveMonitor(ids[i]);
2095         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2096     }
2097 }
2098 
2099 HWTEST_F(InputManagerTest, InputManagerTest_OnAddTouchPadMonitor_005, TestSize.Level1)
2100 {
2101     auto pointerEvent = PointerEvent::Create();
2102     PointerEvent::PointerItem item;
2103     item.SetPointerId(0);
2104     item.SetDownTime(10010);
2105     item.SetPressed(true);
2106     item.SetGlobalX(823);
2107     item.SetGlobalY(723);
2108     item.SetDeviceId(1);
2109     pointerEvent->AddPointerItem(item);
2110 
2111     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
2112     pointerEvent->SetPointerId(0);
2113     pointerEvent->SetSourceType(-1);
2114     MMI_LOGD("Call MontiorManager");
2115 
2116     std::string command {
2117         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2118         "PointerEvent received"
2119     };
2120     std::vector<std::string> sLogs { SearchLog(command, true) };
2121 
2122     int32_t monitorId { };
2123     auto callBackPtr = InputEventCallback::GetPtr();
2124     EXPECT_TRUE(callBackPtr != nullptr);
2125     monitorId = InputManager::GetInstance()->AddMonitor(callBackPtr);
2126     EXPECT_TRUE(IsValidHandlerId(monitorId));
2127     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2128 
2129     int32_t response = MMIEventHdl.InjectPointerEvent(pointerEvent);
2130     EXPECT_EQ(RET_OK, response);
2131 
2132     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2133     EXPECT_TRUE(!tLogs.empty());
2134 
2135     InputManager::GetInstance()->RemoveMonitor(monitorId);
2136     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2137 }
2138 
2139 HWTEST_F(InputManagerTest, InputManager_TouchPadSimulateInputEvent_001, TestSize.Level1)
2140 {
2141     auto callBackPtr = InputEventCallback::GetPtr();
2142     EXPECT_TRUE(callBackPtr != nullptr);
2143     int32_t monitorId { InputManager::GetInstance()->AddMonitor(callBackPtr) };
2144     EXPECT_TRUE(monitorId >= MIN_HANDLER_ID);
2145     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2146 
2147     int64_t actionTime = GetSysClockTime();
2148     auto pointerEvent = PointerEvent::Create();
2149     PointerEvent::PointerItem item { };
2150     item.SetPointerId(DEFAULT_POINTER_ID);
2151     item.SetDownTime(actionTime);
2152     item.SetPressed(true);
2153     item.SetGlobalX(823);
2154     item.SetGlobalY(723);
2155     item.SetDeviceId(DEFAULT_DEVICE_ID);
2156     pointerEvent->AddPointerItem(item);
2157 
2158     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
2159     pointerEvent->SetActionTime(actionTime);
2160     pointerEvent->SetPointerId(DEFAULT_POINTER_ID);
2161     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
2162 
2163     std::string command {
2164         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2165         "PointerEvent received"
2166     };
2167     std::vector<std::string> sLogs { SearchLog(command, true) };
2168 
2169     MMI_LOGD("Call InputManager::SimulateInputEvent");
2170     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
2171 
2172     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2173     EXPECT_TRUE(!tLogs.empty());
2174 
2175     InputManager::GetInstance()->RemoveMonitor(monitorId);
2176     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2177 }
2178 
2179 HWTEST_F(InputManagerTest, InputManager_TouchPadSimulateInputEvent_002, TestSize.Level1)
2180 {
2181     auto callBackPtr = InputEventCallback::GetPtr();
2182     EXPECT_TRUE(callBackPtr != nullptr);
2183     int32_t monitorId { InputManager::GetInstance()->AddMonitor(callBackPtr) };
2184     EXPECT_TRUE(monitorId >= MIN_HANDLER_ID);
2185     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2186 
2187     int64_t actionTime = GetSysClockTime();
2188     auto pointerEvent = PointerEvent::Create();
2189     PointerEvent::PointerItem item { };
2190     item.SetPointerId(DEFAULT_POINTER_ID);
2191     item.SetDownTime(actionTime);
2192     item.SetPressed(true);
2193     item.SetGlobalX(1000);
2194     item.SetGlobalY(610);
2195     item.SetDeviceId(DEFAULT_DEVICE_ID);
2196     pointerEvent->AddPointerItem(item);
2197 
2198     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
2199     pointerEvent->SetActionTime(actionTime);
2200     pointerEvent->SetPointerId(DEFAULT_POINTER_ID);
2201     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
2202 
2203     std::string command {
2204         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2205         "PointerEvent received"
2206     };
2207     std::vector<std::string> sLogs { SearchLog(command, true) };
2208 
2209     MMI_LOGD("Call InputManager::SimulateInputEvent");
2210     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
2211 
2212     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2213     EXPECT_TRUE(!tLogs.empty());
2214 
2215     InputManager::GetInstance()->RemoveMonitor(monitorId);
2216     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2217 }
2218 
2219 HWTEST_F(InputManagerTest, InputManager_TouchPadSimulateInputEvent_003, TestSize.Level1)
2220 {
2221     auto callBackPtr = InputEventCallback::GetPtr();
2222     EXPECT_TRUE(callBackPtr != nullptr);
2223     int32_t monitorId { InputManager::GetInstance()->AddMonitor(callBackPtr) };
2224     EXPECT_TRUE(monitorId >= MIN_HANDLER_ID);
2225     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2226 
2227     int64_t actionTime = GetSysClockTime();
2228     auto pointerEvent = PointerEvent::Create();
2229     PointerEvent::PointerItem item { };
2230     item.SetPointerId(DEFAULT_POINTER_ID);
2231     item.SetDownTime(actionTime);
2232     item.SetPressed(false);
2233     item.SetGlobalX(0);
2234     item.SetGlobalY(0);
2235     item.SetDeviceId(DEFAULT_DEVICE_ID);
2236     pointerEvent->AddPointerItem(item);
2237 
2238     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
2239     pointerEvent->SetActionTime(actionTime);
2240     pointerEvent->SetPointerId(DEFAULT_POINTER_ID);
2241     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
2242 
2243     std::string command {
2244         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2245         "PointerEvent received"
2246     };
2247     std::vector<std::string> sLogs { SearchLog(command, true) };
2248 
2249     MMI_LOGD("Call InputManager::SimulateInputEvent");
2250     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
2251 
2252     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2253     EXPECT_TRUE(!tLogs.empty());
2254 
2255     InputManager::GetInstance()->RemoveMonitor(monitorId);
2256     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2257 }
2258 
2259 HWTEST_F(InputManagerTest, InputManager_TouchPadSimulateInputEvent_004, TestSize.Level1)
2260 {
2261     auto callBackPtr = InputEventCallback::GetPtr();
2262     EXPECT_TRUE(callBackPtr != nullptr);
2263     int32_t monitorId { InputManager::GetInstance()->AddMonitor(callBackPtr) };
2264     EXPECT_TRUE(monitorId >= MIN_HANDLER_ID);
2265     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2266 
2267     int64_t actionTime = GetSysClockTime();
2268     auto pointerEvent = PointerEvent::Create();
2269     PointerEvent::PointerItem item { };
2270     item.SetPointerId(DEFAULT_POINTER_ID);
2271     item.SetDownTime(actionTime);
2272     item.SetPressed(true);
2273     item.SetGlobalX(823);
2274     item.SetGlobalY(723);
2275     item.SetDeviceId(DEFAULT_DEVICE_ID);
2276     pointerEvent->AddPointerItem(item);
2277 
2278     item.SetPointerId(1);
2279     item.SetDownTime(actionTime);
2280     item.SetPressed(true);
2281     item.SetGlobalX(840);
2282     item.SetGlobalY(740);
2283     item.SetDeviceId(DEFAULT_DEVICE_ID);
2284     pointerEvent->AddPointerItem(item);
2285 
2286     item.SetPointerId(2);
2287     item.SetDownTime(actionTime);
2288     item.SetPressed(true);
2289     item.SetGlobalX(860);
2290     item.SetGlobalY(760);
2291     item.SetDeviceId(DEFAULT_DEVICE_ID);
2292     pointerEvent->AddPointerItem(item);
2293 
2294     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
2295     pointerEvent->SetActionTime(actionTime);
2296     pointerEvent->SetPointerId(DEFAULT_POINTER_ID);
2297     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
2298 
2299     std::string command {
2300         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2301         "PointerEvent received"
2302     };
2303     std::vector<std::string> sLogs { SearchLog(command, true) };
2304 
2305     MMI_LOGD("Call InputManager::SimulateInputEvent");
2306     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
2307 
2308     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2309     EXPECT_TRUE(!tLogs.empty());
2310 
2311     InputManager::GetInstance()->RemoveMonitor(monitorId);
2312     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2313 }
2314 
2315 HWTEST_F(InputManagerTest, InputManagerTest_AddMouseMonitor_001, TestSize.Level1)
2316 {
2317     std::string command {
2318         "InputHandlerManagerGlobal: in AddMonitor, #[[:digit:]]\\{1,\\}, "
2319         "Service AddMonitor Success"
2320     };
2321     std::vector<std::string> sLogs { SearchLog(command, true) };
2322 
2323     auto callBackPtr = InputEventCallback::GetPtr();
2324     EXPECT_TRUE(callBackPtr != nullptr);
2325     int32_t id1 = InputManager::GetInstance()->AddMonitor(callBackPtr);
2326     EXPECT_TRUE(IsValidHandlerId(id1));
2327     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2328     if (IsValidHandlerId(id1)) {
2329         InputManager::GetInstance()->RemoveMonitor(id1);
2330         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2331     }
2332 }
2333 
2334 HWTEST_F(InputManagerTest, InputManagerTest_AddMouseMonitor_002, TestSize.Level1)
2335 {
2336     auto callBackPtr = InputEventCallback::GetPtr();
2337     EXPECT_TRUE(callBackPtr != nullptr);
2338 
2339     int32_t id1 = InputManager::GetInstance()->AddMonitor(callBackPtr);
2340     EXPECT_TRUE(IsValidHandlerId(id1));
2341     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2342 
2343     std::string command {
2344         "InputHandlerManagerGlobal: in RemoveMonitor, #[[:digit:]]\\{1,\\}, "
2345         "Service RemoveMonitor Success"
2346     };
2347     std::vector<std::string> sLogs { SearchLog(command, true) };
2348     if (IsValidHandlerId(id1)) {
2349         InputManager::GetInstance()->RemoveMonitor(id1);
2350         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2351     }
2352     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2353     EXPECT_TRUE(!tLogs.empty());
2354 }
2355 
2356 HWTEST_F(InputManagerTest, InputManagerTest_AddMouseMonitor_003, TestSize.Level1)
2357 {
2358     std::string command {
2359         "InputHandlerManager: in AddHandler, #[[:digit:]]\\{1,\\}, "
2360         "The number of handlers exceeds the maximum"
2361     };
2362     std::vector<std::string> sLogs { SearchLog(command, true) };
2363 
2364     const std::vector<int32_t>::size_type N_TEST_CASES { MAX_N_INPUT_HANDLERS };
2365     std::vector<int32_t> ids(N_TEST_CASES);
2366     std::shared_ptr<InputEventCallback> cb = InputEventCallback::GetPtr();
2367     EXPECT_TRUE(cb != nullptr);
2368 
2369     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
2370         ids[i] = InputManager::GetInstance()->AddMonitor(cb);
2371         EXPECT_TRUE(IsValidHandlerId(ids[i]));
2372         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2373     }
2374 
2375     int32_t monitorId = InputManager::GetInstance()->AddMonitor(cb);
2376     EXPECT_TRUE(!IsValidHandlerId(monitorId));
2377     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2378     EXPECT_TRUE(!tLogs.empty());
2379 
2380     for (std::vector<int32_t>::size_type i = 0; i < N_TEST_CASES; i++) {
2381         if (IsValidHandlerId(ids[i])) {
2382             InputManager::GetInstance()->RemoveMonitor(ids[i]);
2383             std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2384         }
2385     }
2386 }
2387 
2388 HWTEST_F(InputManagerTest, InputManagerTest_AddMouseMonitor_004, TestSize.Level1)
2389 {
2390     auto callBackPtr = InputEventCallback::GetPtr();
2391     EXPECT_TRUE(callBackPtr != nullptr);
2392     int32_t id1 = InputManager::GetInstance()->AddMonitor(callBackPtr);
2393     EXPECT_TRUE(IsValidHandlerId(id1));
2394     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2395 
2396     std::string command {
2397         "InputManagerTest: in OnInputEvent, #[[:digit:]]\\{1,\\}, "
2398         "PointerEvent received"
2399     };
2400     std::vector<std::string> sLogs { SearchLog(command, true) };
2401 
2402     auto pointerEvent = SetupPointerEvent006();
2403     EXPECT_TRUE(pointerEvent != nullptr);
2404     MMI_LOGD("Call InputManager::SimulateInputEvent");
2405     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
2406 
2407     std::vector<std::string> tLogs { SearchLog(command, sLogs) };
2408     EXPECT_TRUE(!tLogs.empty());
2409     if (IsValidHandlerId(id1)) {
2410         InputManager::GetInstance()->RemoveMonitor(id1);
2411         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
2412     }
2413 }
2414 } // namespace MMI
2415 } // namespace OHOS