• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <fstream>
17 #include <list>
18 #include <gtest/gtest.h>
19 
20 #include "display_event_monitor.h"
21 #include "event_log_helper.h"
22 #include "key_option.h"
23 #include "key_gesture_manager.h"
24 #include "key_event.h"
25 #include "mmi_log.h"
26 #include "nap_process.h"
27 #include "switch_subscriber_handler.h"
28 #include "uds_server.h"
29 
30 #undef MMI_LOG_TAG
31 #define MMI_LOG_TAG "KeyGestureManagerTest"
32 
33 namespace OHOS {
34 namespace MMI {
35 namespace {
36 using namespace testing::ext;
37 constexpr int32_t INVALID_ENTITY_ID { -1 };
38 constexpr size_t SINGLE_KEY_PRESSED { 1 };
39 } // namespace
40 
41 class KeyGestureManagerTest : public testing::Test {
42 public:
SetUpTestCase(void)43     static void SetUpTestCase(void) {}
TearDownTestCase(void)44     static void TearDownTestCase(void) {}
45 };
46 
47 class MyKeyGesture : public KeyGestureManager::KeyGesture {
48 public:
49     MyKeyGesture() = default;
50     ~MyKeyGesture() override = default;
51 
IsWorking()52     bool IsWorking() override
53     {
54         return true;
55     }
56 
ShouldIntercept(std::shared_ptr<KeyOption> keyOption) const57     bool ShouldIntercept(std::shared_ptr<KeyOption> keyOption) const override
58     {
59         return true;
60     }
61 
Intercept(std::shared_ptr<KeyEvent> keyEvent)62     bool Intercept(std::shared_ptr<KeyEvent> keyEvent) override
63     {
64         return true;
65     }
66 
Dump(std::ostringstream & output) const67     void Dump(std::ostringstream &output) const override
68     {
69         output << "MyKeyGesture";
70     }
71 };
72 
73 /**
74  * @tc.name: KeyGestureManagerTest_Intercept_002
75  * @tc.desc: Test the funcation Intercept
76  * @tc.type: FUNC
77  * @tc.require:
78  */
79 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_002, TestSize.Level1)
80 {
81     CALL_TEST_DEBUG;
82     KeyGestureManager keyGestureManager;
83     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
84     ASSERT_NE(keyEvent, nullptr);
85     auto keyGesture1 = std::make_unique<MyKeyGesture>();
86     auto keyGesture2 = std::make_unique<MyKeyGesture>();
87     auto keyGesture3 = std::make_unique<MyKeyGesture>();
88     keyGestureManager.keyGestures_.push_back(std::move(keyGesture1));
89     keyGestureManager.keyGestures_.push_back(std::move(keyGesture2));
90     keyGestureManager.keyGestures_.push_back(std::move(keyGesture3));
91     EXPECT_FALSE(EventLogHelper::IsBetaVersion());
92     EXPECT_FALSE(keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE));
93     EXPECT_TRUE(keyGestureManager.Intercept(keyEvent));
94 }
95 
96 /**
97  * @tc.name: KeyGestureManagerTest_TriggerHandlers_01
98  * @tc.desc: Test the funcation TriggerHandlers
99  * @tc.type: FUNC
100  * @tc.require:
101  */
102 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerHandlers_01, TestSize.Level1)
103 {
104     CALL_TEST_DEBUG;
105     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
106     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
107     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
108     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
109 
110     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
111     myKeyGesture->handlers_.push_back(handler1);
112     myKeyGesture->handlers_.push_back(handler2);
113     myKeyGesture->handlers_.push_back(handler3);
114     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
115     ASSERT_NE(keyEvent, nullptr);
116 
117     std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids();
118     bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids);
119     EXPECT_FALSE(haveForeground);
120     ASSERT_NO_FATAL_FAILURE(myKeyGesture->TriggerHandlers(keyEvent));
121 }
122 
123 /**
124  * @tc.name: LongPressSingleKey_Dump_01
125  * @tc.desc: Test the funcation LongPressSingleKey_Dump
126  * @tc.type: FUNC
127  * @tc.require:
128  */
129 HWTEST_F(KeyGestureManagerTest, LongPressSingleKey_Dump_01, TestSize.Level1)
130 {
131     CALL_TEST_DEBUG;
132     int32_t keyCode = 1;
133     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
134     std::ostringstream output;
135     longPressSingleKey.keyCode_ = 2;
136 
137     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
138     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
139     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
140     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
141 
142     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
143     myKeyGesture->handlers_.push_back(handler1);
144     myKeyGesture->handlers_.push_back(handler2);
145     myKeyGesture->handlers_.push_back(handler3);
146     ASSERT_NO_FATAL_FAILURE(longPressSingleKey.Dump(output));
147 }
148 
149 /**
150  * @tc.name: LongPressCombinationKey_Dump_01
151  * @tc.desc: Test the funcation LongPressCombinationKey_Dump
152  * @tc.type: FUNC
153  * @tc.require:
154  */
155 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Dump_01, TestSize.Level1)
156 {
157     CALL_TEST_DEBUG;
158     std::set<int32_t> keys = {1, 2, 3};
159     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
160     std::ostringstream output;
161     longPressCombinationKey.keys_ = {3, 4, 5, 6};
162     ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.Dump(output));
163 
164     longPressCombinationKey.keys_ = {};
165     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
166     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
167     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
168     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
169     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
170     myKeyGesture->handlers_.push_back(handler1);
171     myKeyGesture->handlers_.push_back(handler2);
172     myKeyGesture->handlers_.push_back(handler3);
173     ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.Dump(output));
174 }
175 
176 /**
177  * @tc.name: KeyGestureManagerTest_Intercept_01
178  * @tc.desc: Test the funcation Intercept
179  * @tc.type: FUNC
180  * @tc.require:
181  */
182 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_01, TestSize.Level1)
183 {
184     CALL_TEST_DEBUG;
185     KeyGestureManager keyGestureManager;
186     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
187     ASSERT_NE(keyEvent, nullptr);
188     bool ret = keyGestureManager.Intercept(keyEvent);
189     EXPECT_FALSE(ret);
190 }
191 
192 /**
193  * @tc.name: KeyGestureManagerTest_RemoveKeyGesture_01
194  * @tc.desc: Test the funcation RemoveKeyGesture
195  * @tc.type: FUNC
196  * @tc.require:
197  */
198 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_01, TestSize.Level1)
199 {
200     CALL_TEST_DEBUG;
201     KeyGestureManager keyGestureManager;
202     int32_t id = 1;
203     ASSERT_NO_FATAL_FAILURE(keyGestureManager.RemoveKeyGesture(id));
204 }
205 
206 /**
207  * @tc.name: KeyGestureManagerTest_RemoveKeyGesture_02
208  * @tc.desc: Test the funcation RemoveKeyGesture
209  * @tc.type: FUNC
210  * @tc.require:
211  */
212 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_02, TestSize.Level1)
213 {
214     CALL_TEST_DEBUG;
215     KeyGestureManager keyGestureManager;
216     int32_t id = -2;
217     ASSERT_NO_FATAL_FAILURE(keyGestureManager.RemoveKeyGesture(id));
218 }
219 
220 /**
221  * @tc.name: KeyGestureManagerTest_AddKeyGesture_01
222  * @tc.desc: Test the funcation AddKeyGesture
223  * @tc.type: FUNC
224  * @tc.require:
225  */
226 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_AddKeyGesture_01, TestSize.Level1)
227 {
228     CALL_TEST_DEBUG;
229     KeyGestureManager keyGestureManager;
230     int32_t pid = 1;
231     std::shared_ptr<KeyOption> keyOption = nullptr;
__anon08f24b010202(std::shared_ptr<KeyEvent> event) 232     auto callback = [](std::shared_ptr<KeyEvent> event) {};
233     int32_t result = keyGestureManager.AddKeyGesture(pid, keyOption, callback);
234     EXPECT_EQ(result, INVALID_ENTITY_ID);
235 }
236 
237 /**
238  * @tc.name: KeyGestureManagerTest_ShouldIntercept_01
239  * @tc.desc: Test the funcation ShouldIntercept
240  * @tc.type: FUNC
241  * @tc.require:
242  */
243 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ShouldIntercept_01, TestSize.Level1)
244 {
245     CALL_TEST_DEBUG;
246     KeyGestureManager keyGestureManager;
247     std::shared_ptr<KeyOption> keyOption = nullptr;
248     bool result = keyGestureManager.ShouldIntercept(keyOption);
249     EXPECT_FALSE(result);
250 }
251 
252 /**
253  * @tc.name: KeyGestureManagerTest_Intercept_02
254  * @tc.desc: Test the funcation ShouldIntercept
255  * @tc.type: FUNC
256  * @tc.require:
257  */
258 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_02, TestSize.Level1)
259 {
260     CALL_TEST_DEBUG;
261     int32_t keyCode = 1;
262     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
263     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
264     ASSERT_NE(keyEvent, nullptr);
265     keyEvent->keyCode_ = 2;
266     longPressSingleKey.keyCode_ = 2;
267     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN;
268 
269     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
270     myKeyGesture->active_ = true;
271     bool ret = longPressSingleKey.Intercept(keyEvent);
272     EXPECT_TRUE(ret);
273 
274     myKeyGesture->active_ = true;
275     bool ret2 = longPressSingleKey.Intercept(keyEvent);
276     EXPECT_TRUE(ret2);
277 }
278 
279 /**
280  * @tc.name: KeyGestureManagerTest_Intercept_03
281  * @tc.desc: Test the funcation ShouldIntercept
282  * @tc.type: FUNC
283  * @tc.require:
284  */
285 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_03, TestSize.Level1)
286 {
287     CALL_TEST_DEBUG;
288     int32_t keyCode = 1;
289     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
290     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
291     ASSERT_NE(keyEvent, nullptr);
292     keyEvent->keyCode_ = 3;
293     longPressSingleKey.keyCode_ = 2;
294     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN;
295     bool ret = longPressSingleKey.Intercept(keyEvent);
296     EXPECT_FALSE(ret);
297 }
298 
299 /**
300  * @tc.name: KeyGestureManagerTest_Intercept_04
301  * @tc.desc: Test the funcation ShouldIntercept
302  * @tc.type: FUNC
303  * @tc.require:
304  */
305 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_04, TestSize.Level1)
306 {
307     CALL_TEST_DEBUG;
308     int32_t keyCode = 1;
309     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
310     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
311     ASSERT_NE(keyEvent, nullptr);
312     keyEvent->keyCode_ = 2;
313     longPressSingleKey.keyCode_ = 2;
314     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
315     bool ret = longPressSingleKey.Intercept(keyEvent);
316     EXPECT_FALSE(ret);
317 }
318 
319 /**
320  * @tc.name: KeyGestureManagerTest_Intercept_05
321  * @tc.desc: Test the funcation ShouldIntercept
322  * @tc.type: FUNC
323  * @tc.require:
324  */
325 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_05, TestSize.Level1)
326 {
327     CALL_TEST_DEBUG;
328     int32_t keyCode = 1;
329     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
330     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
331     ASSERT_NE(keyEvent, nullptr);
332     keyEvent->keyCode_ = 3;
333     longPressSingleKey.keyCode_ = 2;
334     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
335     bool ret = longPressSingleKey.Intercept(keyEvent);
336     EXPECT_FALSE(ret);
337 }
338 
339 /**
340  * @tc.name: KeyGestureManagerTest_Intercept_06
341  * @tc.desc: Test the funcation ShouldIntercept
342  * @tc.type: FUNC
343  * @tc.require:
344  */
345 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_06, TestSize.Level1)
346 {
347     CALL_TEST_DEBUG;
348     int32_t keyCode = 1;
349     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
350     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
351     ASSERT_NE(keyEvent, nullptr);
352     keyEvent->keyCode_ = 3;
353     longPressSingleKey.keyCode_ = 2;
354     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
355 
356     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
357     myKeyGesture->active_ = true;
358     bool ret = longPressSingleKey.Intercept(keyEvent);
359     EXPECT_FALSE(ret);
360 
361     myKeyGesture->active_ = false;
362     bool ret2 = longPressSingleKey.Intercept(keyEvent);
363     EXPECT_FALSE(ret2);
364 }
365 
366 /**
367  * @tc.name: KeyGestureManagerTest_IsWorking_01
368  * @tc.desc: Test the funcation ShouldIntercept
369  * @tc.type: FUNC
370  * @tc.require:
371  */
372 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_01, TestSize.Level1)
373 {
374     CALL_TEST_DEBUG;
375     KeyGestureManager::PullUpAccessibility pullUpAccessibility;
376     DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF;
377     bool ret = pullUpAccessibility.IsWorking();
378     EXPECT_FALSE(ret);
379 }
380 
381 /**
382  * @tc.name: KeyGestureManagerTest_RecognizeGesture_01
383  * @tc.desc: Test the funcation RecognizeGesture
384  * @tc.type: FUNC
385  * @tc.require:
386  */
387 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_01, TestSize.Level1)
388 {
389     CALL_TEST_DEBUG;
390     std::set<int32_t> keys = {1, 2, 3};
391     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
392     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
393     ASSERT_NE(keyEvent, nullptr);
394 
395     std::vector<int32_t> pressedKeys = {1};
396     EXPECT_TRUE(pressedKeys.size() == SINGLE_KEY_PRESSED);
397     bool ret = longPressCombinationKey.RecognizeGesture(keyEvent);
398     EXPECT_FALSE(ret);
399 }
400 
401 /**
402  * @tc.name: KeyGestureManagerTest_RecognizeGesture_02
403  * @tc.desc: Test the funcation RecognizeGesture
404  * @tc.type: FUNC
405  * @tc.require:
406  */
407 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_02, TestSize.Level1)
408 {
409     CALL_TEST_DEBUG;
410     std::set<int32_t> keys = { 1, 2, 3 };
411     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
412     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
413     ASSERT_NE(keyEvent, nullptr);
414 
415     std::vector<int32_t> pressedKeys = { 2, 3, 4 };
416     EXPECT_FALSE(pressedKeys.size() == SINGLE_KEY_PRESSED);
417     bool ret = longPressCombinationKey.RecognizeGesture(keyEvent);
418     EXPECT_FALSE(ret);
419 }
420 
421 /**
422  * @tc.name: KeyGestureManagerTest_TriggerAll_01
423  * @tc.desc: Test the funcation TriggerAll
424  * @tc.type: FUNC
425  * @tc.require:
426  */
427 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerAll_01, TestSize.Level1)
428 {
429     CALL_TEST_DEBUG;
430     std::set<int32_t> keys = { 1, 2, 3 };
431     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
432     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
433     ASSERT_NE(keyEvent, nullptr);
434     ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.TriggerAll(keyEvent));
435 }
436 
437 /**
438  * @tc.name: KeyGestureManagerTest_RunPending_01
439  * @tc.desc: Test the funcation RunPending
440  * @tc.type: FUNC
441  * @tc.require:
442  */
443 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_01, TestSize.Level1)
444 {
445     CALL_TEST_DEBUG;
446     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
447     KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
448 
449     handler.keyEvent_ = nullptr;
450     ASSERT_NO_FATAL_FAILURE(handler.RunPending());
451 }
452 
453 /**
454  * @tc.name: KeyGestureManagerTest_RunPending_02
455  * @tc.desc: Test the funcation RunPending
456  * @tc.type: FUNC
457  * @tc.require:
458  */
459 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_02, TestSize.Level1)
460 {
461     CALL_TEST_DEBUG;
462     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
463     KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
464 
465     handler.keyEvent_ = KeyEvent::Create();
466     ASSERT_NE(handler.keyEvent_, nullptr);
467     ASSERT_NO_FATAL_FAILURE(handler.RunPending());
468 }
469 
470 /**
471  * @tc.name: KeyGestureManagerTest_ResetTimer_01
472  * @tc.desc: Test the funcation ResetTimer
473  * @tc.type: FUNC
474  * @tc.require:
475  */
476 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_01, TestSize.Level1)
477 {
478     CALL_TEST_DEBUG;
479     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
480     KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
481     handler.timerId_ = 1;
482     ASSERT_NO_FATAL_FAILURE(handler.ResetTimer());
483 }
484 
485 /**
486  * @tc.name: KeyGestureManagerTest_ResetTimer_02
487  * @tc.desc: Test the funcation ResetTimer
488  * @tc.type: FUNC
489  * @tc.require:
490  */
491 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_02, TestSize.Level1)
492 {
493     CALL_TEST_DEBUG;
494     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
495     KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
496     handler.timerId_ = -2;
497     ASSERT_NO_FATAL_FAILURE(handler.ResetTimer());
498 }
499 
500 /**
501  * @tc.name: KeyGestureManagerTest_RunPendingHandlers_01
502  * @tc.desc: Test the funcation RunPendingHandlers
503  * @tc.type: FUNC
504  * @tc.require:
505  */
506 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPendingHandlers_01, TestSize.Level1)
507 {
508     CALL_TEST_DEBUG;
509     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
510     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
511     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
512     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
513 
514     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
515     myKeyGesture->handlers_.push_back(handler1);
516     myKeyGesture->handlers_.push_back(handler2);
517     myKeyGesture->handlers_.push_back(handler3);
518 
519     std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids();
520     bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids);
521     EXPECT_FALSE(haveForeground);
522 
523     int32_t keyCode = 1;
524     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
525     ASSERT_NO_FATAL_FAILURE(longPressSingleKey.RunPendingHandlers());
526 }
527 
528 /**
529  * @tc.name: KeyGestureManagerTest_RunHandler_01
530  * @tc.desc: Test the funcation RunHandler
531  * @tc.type: FUNC
532  * @tc.require:
533  */
534 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunHandler_01, TestSize.Level1)
535 {
536     CALL_TEST_DEBUG;
537     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
538     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
539     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
540     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
541 
542     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
543     myKeyGesture->handlers_.push_back(handler1);
544     myKeyGesture->handlers_.push_back(handler2);
545     myKeyGesture->handlers_.push_back(handler3);
546 
547     int32_t handlerId = 1;
548     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
549     ASSERT_NE(keyEvent, nullptr);
550     ASSERT_NO_FATAL_FAILURE(myKeyGesture->RunHandler(handlerId, keyEvent));
551 
552     handlerId = 5;
553     ASSERT_NO_FATAL_FAILURE(myKeyGesture->RunHandler(handlerId, keyEvent));
554 }
555 
556 /**
557  * @tc.name: LongPressCombinationKey_Intercept_01
558  * @tc.desc: Test the funcation RecognizeGesture
559  * @tc.type: FUNC
560  * @tc.require:
561  */
562 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_01, TestSize.Level1)
563 {
564     CALL_TEST_DEBUG;
565     std::set<int32_t> keys = {1, 2, 3};
566     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
567     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
568     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
569     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
570     ASSERT_NE(keyEvent, nullptr);
571 
572     keyEvent->keyCode_ = 3;
573     longPressCombinationKey.keys_ = {2, 3, 4};
574     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN;
575     myKeyGesture->active_ = true;
576     EXPECT_FALSE(EventLogHelper::IsBetaVersion());
577     EXPECT_FALSE(keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE));
578     bool ret = longPressCombinationKey.Intercept(keyEvent);
579     EXPECT_FALSE(ret);
580     myKeyGesture->active_ = false;
581     EXPECT_TRUE(myKeyGesture->IsWorking());
582 
583     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
584     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
585     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
586     myKeyGesture->handlers_.push_back(handler1);
587     myKeyGesture->handlers_.push_back(handler2);
588     myKeyGesture->handlers_.push_back(handler3);
589     EXPECT_FALSE(myKeyGesture->handlers_.empty());
590     bool ret2 = longPressCombinationKey.Intercept(keyEvent);
591     EXPECT_FALSE(ret2);
592 }
593 
594 /**
595  * @tc.name: LongPressCombinationKey_Intercept_02
596  * @tc.desc: Test the funcation RecognizeGesture
597  * @tc.type: FUNC
598  * @tc.require:
599  */
600 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_02, TestSize.Level1)
601 {
602     CALL_TEST_DEBUG;
603     std::set<int32_t> keys = {1, 2, 3};
604     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
605     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
606     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
607     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
608     ASSERT_NE(keyEvent, nullptr);
609 
610     keyEvent->keyCode_ = 5;
611     longPressCombinationKey.keys_ = {2, 3, 4};
612     keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
613     myKeyGesture->active_ = true;
614     bool ret = longPressCombinationKey.Intercept(keyEvent);
615     EXPECT_FALSE(ret);
616 
617     myKeyGesture->active_ = false;
618     bool ret2 = longPressCombinationKey.Intercept(keyEvent);
619     EXPECT_FALSE(ret2);
620 }
621 
622 /**
623  * @tc.name: KeyGestureManagerTest_NotifyHandlers_01
624  * @tc.desc: Test the funcation NotifyHandlers
625  * @tc.type: FUNC
626  * @tc.require:
627  */
628 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_NotifyHandlers_01, TestSize.Level1)
629 {
630     CALL_TEST_DEBUG;
631     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
632     ASSERT_NE(keyEvent, nullptr);
633     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
634     KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
635     KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
636     KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
637 
638     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
639     myKeyGesture->handlers_.push_back(handler1);
640     myKeyGesture->handlers_.push_back(handler2);
641     myKeyGesture->handlers_.push_back(handler3);
642 
643     std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids();
644     bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids);
645     EXPECT_FALSE(haveForeground);
646     ASSERT_NO_FATAL_FAILURE(myKeyGesture->NotifyHandlers(keyEvent));
647 }
648 
649 /**
650  * @tc.name: KeyGestureManagerTest_AddHandler
651  * @tc.desc: Test the funcation AddHandler
652  * @tc.type: FUNC
653  * @tc.require:
654  */
655 HWTEST_F(KeyGestureManagerTest, AddHandler_Success, TestSize.Level1)
656 {
657     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
658     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
659 
660     int32_t id1 = myKeyGesture->AddHandler(10, 500, myCallback);
661     EXPECT_GT(id1, 0);
662     EXPECT_EQ(myKeyGesture->handlers_.size(), 1);
663 
664     int32_t id2 = myKeyGesture->AddHandler(20, 1000, myCallback);
665     EXPECT_GT(id2, id1);
666     EXPECT_EQ(myKeyGesture->handlers_.size(), 2);
667 }
668 
669 /**
670  * @tc.name: KeyGestureManagerTest_RemoveHandler
671  * @tc.desc: Test the funcation RemoveHandler
672  * @tc.type: FUNC
673  * @tc.require:
674  */
675 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveHandler, TestSize.Level1)
676 {
677     CALL_TEST_DEBUG;
678 
679     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
680     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
681 
682     int32_t id1 = myKeyGesture->AddHandler(10, 500, myCallback);
683     int32_t id2 = myKeyGesture->AddHandler(20, 1000, myCallback);
684 
685     bool resultSuccess = myKeyGesture->RemoveHandler(id1);
686     EXPECT_TRUE(resultSuccess);
687     EXPECT_EQ(myKeyGesture->handlers_.size(), 1);
688 
689     bool resultfailed = myKeyGesture->RemoveHandler(id1);
690     EXPECT_FALSE(resultfailed);
691     EXPECT_EQ(myKeyGesture->handlers_.size(), 1);
692 
693     EXPECT_EQ(myKeyGesture->handlers_.front().GetId(), id2);
694 }
695 
696 /**
697  * @tc.name: KeyGestureManagerTest_ResetTimers
698  * @tc.desc: Test the funcation KeyGestureManager::KeyGesture::ResetTimers
699  * @tc.type: FUNC
700  * @tc.require:
701  */
702 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_KeyGesture_ResetTimers, TestSize.Level1)
703 {
704     CALL_TEST_DEBUG;
705     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
706     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
707 
708     int32_t id1 = myKeyGesture->AddHandler(10, 500, myCallback);
709     int32_t id2 = myKeyGesture->AddHandler(30, 1500, myCallback);
710 
711     EXPECT_EQ(myKeyGesture->handlers_.size(), 2);
712     myKeyGesture->ResetTimers();
713     EXPECT_EQ(myKeyGesture->handlers_.size(), 2);
714     bool resultfailed1 = myKeyGesture->RemoveHandler(id1);
715     EXPECT_TRUE(resultfailed1);
716     bool resultfailed2 = myKeyGesture->RemoveHandler(id2);
717     EXPECT_TRUE(resultfailed2);
718 }
719 
720 /**
721  * @tc.name: KeyGestureManagerTest_KeyGesture_IsWorking
722  * @tc.desc: Test the funcation KeyGestureManager::KeyGesture::IsWorking
723  * @tc.type: FUNC
724  * @tc.require:
725  */
726 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_KeyGesture_IsWorking, TestSize.Level1)
727 {
728     CALL_TEST_DEBUG;
729     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
730     EXPECT_TRUE(myKeyGesture->IsWorking());
731 }
732 
733 /**
734  * @tc.name: KeyGestureManagerTest_KeyGesture_GetForegroundPids
735  * @tc.desc: Test the funcation KeyGestureManager::KeyGesture::GetForegroundPids
736  * @tc.type: FUNC
737  * @tc.require:
738  */
739 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_KeyGesture_GetForegroundPids, TestSize.Level1)
740 {
741     CALL_TEST_DEBUG;
742     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
743     std::set<int32_t> pids = myKeyGesture->GetForegroundPids();
744     EXPECT_EQ(pids.size(), 0);
745 }
746 
747 /**
748  * @tc.name: KeyGestureManagerTest_KeyGesture_HaveForegroundHandler
749  * @tc.desc: Test the funcation KeyGestureManager::KeyGesture::HaveForegroundHandler
750  * @tc.type: FUNC
751  * @tc.require:
752  */
753 HWTEST_F(KeyGestureManagerTest, KeyGesture_HaveForegroundHandler, TestSize.Level1)
754 {
755     CALL_TEST_DEBUG;
756     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
757     std::set<int32_t> foregroundPids = {1001, 1002};
758     EXPECT_FALSE(myKeyGesture->HaveForegroundHandler(foregroundPids));
759 }
760 
761 /**
762  * @tc.name: KeyGestureManagerTest_KeyGesture_ShowHandlers
763  * @tc.desc: Test the funcation KeyGestureManager::KeyGesture::_ShowHandlers
764  * @tc.type: FUNC
765  * @tc.require:
766  */
767 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_KeyGesture_ShowHandlers, TestSize.Level1)
768 {
769     CALL_TEST_DEBUG;
770     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
771     std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
772     myKeyGesture->AddHandler(1001, 500, myCallback);
773     myKeyGesture->AddHandler(1002, 1500, myCallback);
774     myKeyGesture->AddHandler(1003, 800, myCallback);
775     myKeyGesture->AddHandler(1004, 1600, myCallback);
776     ASSERT_NO_FATAL_FAILURE(myKeyGesture->ShowHandlers("TestPrefix", {1001, 1002, 1003, 1004}));
777 }
778 
779 /**
780  * @tc.name: KeyGestureManagerTest_LongPressSingleKey_ShouldIntercept
781  * @tc.desc: Test the funcation LongPressSingleKey::ShouldIntercept
782  * @tc.type: FUNC
783  * @tc.require:
784  */
785 HWTEST_F(KeyGestureManagerTest, LongPressSingleKey_ShouldIntercept, TestSize.Level1)
786 {
787     CALL_TEST_DEBUG;
788     int32_t keyCode = 1001;
789     KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
790     std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
791     keyOption->SetFinalKey(keyCode);
792     keyOption->SetFinalKeyDown(true);
793     keyOption->SetFinalKeyDownDuration(100);
794     EXPECT_TRUE(longPressSingleKey.ShouldIntercept(keyOption));
795 }
796 
797 /**
798  * @tc.name: KeyGestureManagerTest_LongPressSingleKey_Dump
799  * @tc.desc: Test the funcation LongPressSingleKey::Dump
800  * @tc.type: FUNC
801  * @tc.require:
802  */
803 HWTEST_F(KeyGestureManagerTest, LongPressSingleKey_Dump, TestSize.Level1)
804 {
805     CALL_TEST_DEBUG;
806     KeyGestureManager::LongPressSingleKey longPressSingleKey(1001);
807     std::ostringstream output;
808     longPressSingleKey.Dump(output);
809     EXPECT_FALSE(output.str().empty());
810 }
811 
812 /**
813  * @tc.name: LongPressCombinationKey_Intercept
814  * @tc.desc: Test the funcation LongPressCombinationKey::Intercept
815  * @tc.type: FUNC
816  * @tc.require:
817  */
818 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept, TestSize.Level1)
819 {
820     CALL_TEST_DEBUG;
821     std::set<int32_t> keys = {KeyEvent::KEYCODE_VOLUME_DOWN, KeyEvent::KEYCODE_VOLUME_UP};
822     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
823     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
824     ASSERT_NE(keyEvent, nullptr);
825     keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN);
826     keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
827     EXPECT_FALSE(longPressCombinationKey.Intercept(keyEvent));
828 }
829 
830 /**
831  * @tc.name: LongPressCombinationKey_Dump
832  * @tc.desc: Test the funcation LongPressCombinationKey::Dump
833  * @tc.type: FUNC
834  * @tc.require:
835  */
836 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Dump, TestSize.Level1)
837 {
838     CALL_TEST_DEBUG;
839     std::set<int32_t> keys = {KeyEvent::KEYCODE_VOLUME_DOWN, KeyEvent::KEYCODE_VOLUME_UP};
840     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
841     std::ostringstream output;
842     longPressCombinationKey.Dump(output);
843     EXPECT_FALSE(output.str().empty());
844 }
845 
846 /**
847  * @tc.name: LongPressCombinationKey_RecognizeGesture
848  * @tc.desc: Test the funcation LongPressCombinationKey::RecognizeGesture
849  * @tc.type: FUNC
850  * @tc.require:
851  */
852 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_RecognizeGesture, TestSize.Level1)
853 {
854     CALL_TEST_DEBUG;
855     std::set<int32_t> keys = {KeyEvent::KEYCODE_VOLUME_DOWN, KeyEvent::KEYCODE_VOLUME_UP};
856     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
857     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
858     ASSERT_NE(keyEvent, nullptr);
859     keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN);
860     keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
861     EXPECT_FALSE(longPressCombinationKey.RecognizeGesture(keyEvent));
862 }
863 
864 /**
865  * @tc.name: LongPressCombinationKey_TriggerAll
866  * @tc.desc: Test the funcation LongPressCombinationKey::TriggerAll
867  * @tc.type: FUNC
868  * @tc.require:
869  */
870 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_TriggerAll, TestSize.Level1)
871 {
872     CALL_TEST_DEBUG;
873     std::set<int32_t> keys = {KeyEvent::KEYCODE_VOLUME_DOWN, KeyEvent::KEYCODE_VOLUME_UP};
874     KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
875     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
876     ASSERT_NE(keyEvent, nullptr);
877     keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN);
878     keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
879     ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.TriggerAll(keyEvent));
880 }
881 
882 /**
883  * @tc.name: PullUpAccessibility_AddHandler
884  * @tc.desc: Test the funcation PullUpAccessibility::AddHandler
885  * @tc.type: FUNC
886  * @tc.require:
887  */
888 HWTEST_F(KeyGestureManagerTest, PullUpAccessibility_AddHandler, TestSize.Level1)
889 {
890     CALL_TEST_DEBUG;
891     KeyGestureManager::PullUpAccessibility pullUpAccessibility;
892     std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
893     int32_t id = pullUpAccessibility.AddHandler(10, 500, myCallback);
894     EXPECT_GT(id, 0);
895 }
896 
897 /**
898  * @tc.name: PullUpAccessibility_OnTriggerAll
899  * @tc.desc: Test the funcation PullUpAccessibility::OnTriggerAll
900  * @tc.type: FUNC
901  * @tc.require:
902  */
903 HWTEST_F(KeyGestureManagerTest, PullUpAccessibility_OnTriggerAll, TestSize.Level1)
904 {
905     CALL_TEST_DEBUG;
906     KeyGestureManager::PullUpAccessibility pullUpAccessibility;
907     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
908     ASSERT_NE(keyEvent, nullptr);
909     ASSERT_NO_FATAL_FAILURE(pullUpAccessibility.OnTriggerAll(keyEvent));
910 }
911 
912 /**
913  * @tc.name: KeyGestureManager_Dump
914  * @tc.desc: Test the funcation KeyGestureManager::Dump
915  * @tc.type: FUNC
916  * @tc.require:
917  */
918 HWTEST_F(KeyGestureManagerTest, KeyGestureManager_Dump, TestSize.Level1)
919 {
920     CALL_TEST_DEBUG;
921     KeyGestureManager keyGestureManager;
922     ASSERT_NO_FATAL_FAILURE(keyGestureManager.Dump());
923 }
924 
925 /**
926  * @tc.name: KeyGestureManager_KeyMonitorIntercept
927  * @tc.desc: Test the funcation KeyGestureManager::KeyMonitorIntercept
928  * @tc.type: FUNC
929  * @tc.require:
930  */
931 HWTEST_F(KeyGestureManagerTest, KeyGestureManager_KeyMonitorIntercept, TestSize.Level1)
932 {
933     CALL_TEST_DEBUG;
934     KeyGestureManager keyGestureManager;
935     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
936     ASSERT_NE(keyEvent, nullptr);
937 
938     keyEvent->SetKeyCode(1001);
939     keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
940     EXPECT_FALSE(keyGestureManager.KeyMonitorIntercept(keyEvent));
941 }
942 } // namespace MMI
943 } // namespace OHOS