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 "ability_manager_client.h"
21 #include "display_event_monitor.h"
22 #include "event_log_helper.h"
23 #include "key_option.h"
24 #include "key_gesture_manager.h"
25 #include "key_event.h"
26 #include "mmi_log.h"
27 #include "nap_process.h"
28 #include "switch_subscriber_handler.h"
29 #include "uds_server.h"
30
31 #undef MMI_LOG_TAG
32 #define MMI_LOG_TAG "KeyGestureManagerTest"
33
34 namespace OHOS {
35 namespace MMI {
36 namespace {
37 using namespace testing::ext;
38 constexpr int32_t INVALID_ENTITY_ID { -1 };
39 constexpr size_t SINGLE_KEY_PRESSED { 1 };
40 } // namespace
41
42 class KeyGestureManagerTest : public testing::Test {
43 public:
SetUpTestCase(void)44 static void SetUpTestCase(void) {}
TearDownTestCase(void)45 static void TearDownTestCase(void) {}
46 };
47
48 class MyKeyGesture : public KeyGestureManager::KeyGesture {
49 public:
50 MyKeyGesture() = default;
51 ~MyKeyGesture() override = default;
52
53 bool IsWorking() override
54 {
55 return true;
56 }
57
58 bool ShouldIntercept(std::shared_ptr<KeyOption> keyOption) const override
59 {
60 return true;
61 }
62
63 bool Intercept(std::shared_ptr<KeyEvent> keyEvent) override
64 {
65 return true;
66 }
67
68 void Dump(std::ostringstream &output) const override
69 {
70 output << "MyKeyGesture";
71 }
72 };
73
74 /**
75 * @tc.name: KeyGestureManagerTest_Intercept_002
76 * @tc.desc: Test the funcation Intercept
77 * @tc.type: FUNC
78 * @tc.require:
79 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_002, TestSize.Level1)80 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_002, TestSize.Level1)
81 {
82 CALL_TEST_DEBUG;
83 KeyGestureManager keyGestureManager;
84 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
85 ASSERT_NE(keyEvent, nullptr);
86 auto keyGesture1 = std::make_unique<MyKeyGesture>();
87 auto keyGesture2 = std::make_unique<MyKeyGesture>();
88 auto keyGesture3 = std::make_unique<MyKeyGesture>();
89 keyGestureManager.keyGestures_.push_back(std::move(keyGesture1));
90 keyGestureManager.keyGestures_.push_back(std::move(keyGesture2));
91 keyGestureManager.keyGestures_.push_back(std::move(keyGesture3));
92 EXPECT_FALSE(EventLogHelper::IsBetaVersion());
93 EXPECT_FALSE(keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE));
94 EXPECT_TRUE(keyGestureManager.Intercept(keyEvent));
95 }
96
97 /**
98 * @tc.name: KeyGestureManagerTest_TriggerHandlers_01
99 * @tc.desc: Test the funcation TriggerHandlers
100 * @tc.type: FUNC
101 * @tc.require:
102 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerHandlers_01, TestSize.Level1)103 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerHandlers_01, TestSize.Level1)
104 {
105 CALL_TEST_DEBUG;
106 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
107 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
108 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
109 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
110
111 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
112 myKeyGesture->handlers_.push_back(handler1);
113 myKeyGesture->handlers_.push_back(handler2);
114 myKeyGesture->handlers_.push_back(handler3);
115 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
116 ASSERT_NE(keyEvent, nullptr);
117
118 std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids();
119 bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids);
120 EXPECT_FALSE(haveForeground);
121 ASSERT_NO_FATAL_FAILURE(myKeyGesture->TriggerHandlers(keyEvent));
122 }
123
124 /**
125 * @tc.name: LongPressSingleKey_Dump_01
126 * @tc.desc: Test the funcation LongPressSingleKey_Dump
127 * @tc.type: FUNC
128 * @tc.require:
129 */
HWTEST_F(KeyGestureManagerTest, LongPressSingleKey_Dump_01, TestSize.Level1)130 HWTEST_F(KeyGestureManagerTest, LongPressSingleKey_Dump_01, TestSize.Level1)
131 {
132 CALL_TEST_DEBUG;
133 int32_t keyCode = 1;
134 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
135 std::ostringstream output;
136 longPressSingleKey.keyCode_ = 2;
137
138 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
139 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
140 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
141 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
142
143 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
144 myKeyGesture->handlers_.push_back(handler1);
145 myKeyGesture->handlers_.push_back(handler2);
146 myKeyGesture->handlers_.push_back(handler3);
147 ASSERT_NO_FATAL_FAILURE(longPressSingleKey.Dump(output));
148 }
149
150 /**
151 * @tc.name: LongPressCombinationKey_Dump_01
152 * @tc.desc: Test the funcation LongPressCombinationKey_Dump
153 * @tc.type: FUNC
154 * @tc.require:
155 */
HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Dump_01, TestSize.Level1)156 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Dump_01, TestSize.Level1)
157 {
158 CALL_TEST_DEBUG;
159 std::set<int32_t> keys = {1, 2, 3};
160 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
161 std::ostringstream output;
162 longPressCombinationKey.keys_ = {3, 4, 5, 6};
163 ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.Dump(output));
164
165 longPressCombinationKey.keys_ = {};
166 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
167 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
168 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
169 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
170 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
171 myKeyGesture->handlers_.push_back(handler1);
172 myKeyGesture->handlers_.push_back(handler2);
173 myKeyGesture->handlers_.push_back(handler3);
174 ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.Dump(output));
175 }
176
177 /**
178 * @tc.name: KeyGestureManagerTest_Intercept_01
179 * @tc.desc: Test the funcation Intercept
180 * @tc.type: FUNC
181 * @tc.require:
182 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_01, TestSize.Level1)183 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_01, TestSize.Level1)
184 {
185 CALL_TEST_DEBUG;
186 KeyGestureManager keyGestureManager;
187 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
188 ASSERT_NE(keyEvent, nullptr);
189 bool ret = keyGestureManager.Intercept(keyEvent);
190 EXPECT_FALSE(ret);
191 }
192
193 /**
194 * @tc.name: KeyGestureManagerTest_RemoveKeyGesture_01
195 * @tc.desc: Test the funcation RemoveKeyGesture
196 * @tc.type: FUNC
197 * @tc.require:
198 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_01, TestSize.Level1)199 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_01, TestSize.Level1)
200 {
201 CALL_TEST_DEBUG;
202 KeyGestureManager keyGestureManager;
203 int32_t id = 1;
204 ASSERT_NO_FATAL_FAILURE(keyGestureManager.RemoveKeyGesture(id));
205 }
206
207 /**
208 * @tc.name: KeyGestureManagerTest_RemoveKeyGesture_02
209 * @tc.desc: Test the funcation RemoveKeyGesture
210 * @tc.type: FUNC
211 * @tc.require:
212 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_02, TestSize.Level1)213 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_02, TestSize.Level1)
214 {
215 CALL_TEST_DEBUG;
216 KeyGestureManager keyGestureManager;
217 int32_t id = -2;
218 ASSERT_NO_FATAL_FAILURE(keyGestureManager.RemoveKeyGesture(id));
219 }
220
221 /**
222 * @tc.name: KeyGestureManagerTest_AddKeyGesture_01
223 * @tc.desc: Test the funcation AddKeyGesture
224 * @tc.type: FUNC
225 * @tc.require:
226 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_AddKeyGesture_01, TestSize.Level1)227 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_AddKeyGesture_01, TestSize.Level1)
228 {
229 CALL_TEST_DEBUG;
230 KeyGestureManager keyGestureManager;
231 int32_t pid = 1;
232 std::shared_ptr<KeyOption> keyOption = nullptr;
233 auto callback = [](std::shared_ptr<KeyEvent> event) {};
234 int32_t result = keyGestureManager.AddKeyGesture(pid, keyOption, callback);
235 EXPECT_EQ(result, INVALID_ENTITY_ID);
236 }
237
238 /**
239 * @tc.name: KeyGestureManagerTest_ShouldIntercept_01
240 * @tc.desc: Test the funcation ShouldIntercept
241 * @tc.type: FUNC
242 * @tc.require:
243 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ShouldIntercept_01, TestSize.Level1)244 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ShouldIntercept_01, TestSize.Level1)
245 {
246 CALL_TEST_DEBUG;
247 KeyGestureManager keyGestureManager;
248 std::shared_ptr<KeyOption> keyOption = nullptr;
249 bool result = keyGestureManager.ShouldIntercept(keyOption);
250 EXPECT_FALSE(result);
251 }
252
253 /**
254 * @tc.name: KeyGestureManagerTest_Intercept_02
255 * @tc.desc: Test the funcation ShouldIntercept
256 * @tc.type: FUNC
257 * @tc.require:
258 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_02, TestSize.Level1)259 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_02, TestSize.Level1)
260 {
261 CALL_TEST_DEBUG;
262 int32_t keyCode = 1;
263 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
264 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
265 ASSERT_NE(keyEvent, nullptr);
266 keyEvent->keyCode_ = 2;
267 longPressSingleKey.keyCode_ = 2;
268 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN;
269
270 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
271 myKeyGesture->active_ = true;
272 bool ret = longPressSingleKey.Intercept(keyEvent);
273 EXPECT_TRUE(ret);
274
275 myKeyGesture->active_ = true;
276 bool ret2 = longPressSingleKey.Intercept(keyEvent);
277 EXPECT_TRUE(ret2);
278 }
279
280 /**
281 * @tc.name: KeyGestureManagerTest_Intercept_03
282 * @tc.desc: Test the funcation ShouldIntercept
283 * @tc.type: FUNC
284 * @tc.require:
285 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_03, TestSize.Level1)286 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_03, TestSize.Level1)
287 {
288 CALL_TEST_DEBUG;
289 int32_t keyCode = 1;
290 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
291 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
292 ASSERT_NE(keyEvent, nullptr);
293 keyEvent->keyCode_ = 3;
294 longPressSingleKey.keyCode_ = 2;
295 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN;
296 bool ret = longPressSingleKey.Intercept(keyEvent);
297 EXPECT_FALSE(ret);
298 }
299
300 /**
301 * @tc.name: KeyGestureManagerTest_Intercept_04
302 * @tc.desc: Test the funcation ShouldIntercept
303 * @tc.type: FUNC
304 * @tc.require:
305 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_04, TestSize.Level1)306 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_04, TestSize.Level1)
307 {
308 CALL_TEST_DEBUG;
309 int32_t keyCode = 1;
310 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
311 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
312 ASSERT_NE(keyEvent, nullptr);
313 keyEvent->keyCode_ = 2;
314 longPressSingleKey.keyCode_ = 2;
315 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
316 bool ret = longPressSingleKey.Intercept(keyEvent);
317 EXPECT_FALSE(ret);
318 }
319
320 /**
321 * @tc.name: KeyGestureManagerTest_Intercept_05
322 * @tc.desc: Test the funcation ShouldIntercept
323 * @tc.type: FUNC
324 * @tc.require:
325 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_05, TestSize.Level1)326 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_05, TestSize.Level1)
327 {
328 CALL_TEST_DEBUG;
329 int32_t keyCode = 1;
330 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
331 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
332 ASSERT_NE(keyEvent, nullptr);
333 keyEvent->keyCode_ = 3;
334 longPressSingleKey.keyCode_ = 2;
335 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
336 bool ret = longPressSingleKey.Intercept(keyEvent);
337 EXPECT_FALSE(ret);
338 }
339
340 /**
341 * @tc.name: KeyGestureManagerTest_Intercept_06
342 * @tc.desc: Test the funcation ShouldIntercept
343 * @tc.type: FUNC
344 * @tc.require:
345 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_06, TestSize.Level1)346 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_06, TestSize.Level1)
347 {
348 CALL_TEST_DEBUG;
349 int32_t keyCode = 1;
350 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
351 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
352 ASSERT_NE(keyEvent, nullptr);
353 keyEvent->keyCode_ = 3;
354 longPressSingleKey.keyCode_ = 2;
355 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
356
357 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
358 myKeyGesture->active_ = true;
359 bool ret = longPressSingleKey.Intercept(keyEvent);
360 EXPECT_FALSE(ret);
361
362 myKeyGesture->active_ = false;
363 bool ret2 = longPressSingleKey.Intercept(keyEvent);
364 EXPECT_FALSE(ret2);
365 }
366
367 /**
368 * @tc.name: KeyGestureManagerTest_IsWorking_01
369 * @tc.desc: Test the funcation ShouldIntercept
370 * @tc.type: FUNC
371 * @tc.require:
372 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_01, TestSize.Level1)373 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_01, TestSize.Level1)
374 {
375 CALL_TEST_DEBUG;
376 KeyGestureManager::PullUpAccessibility pullUpAccessibility;
377 DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF;
378 bool ret = pullUpAccessibility.IsWorking();
379 EXPECT_FALSE(ret);
380 }
381
382 /**
383 * @tc.name: KeyGestureManagerTest_IsWorking_02
384 * @tc.desc: Test the funcation ShouldIntercept
385 * @tc.type: FUNC
386 * @tc.require:
387 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_02, TestSize.Level1)388 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_02, TestSize.Level1)
389 {
390 CALL_TEST_DEBUG;
391 KeyGestureManager::PullUpAccessibility pullUpAccessibility;
392 DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON;
393 DISPLAY_MONITOR->isScreenLocked_ = true;
394 bool ret = pullUpAccessibility.IsWorking();
395 EXPECT_FALSE(ret);
396 }
397
398 /**
399 * @tc.name: KeyGestureManagerTest_IsWorking_03
400 * @tc.desc: Test the funcation ShouldIntercept
401 * @tc.type: FUNC
402 * @tc.require:
403 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_03, TestSize.Level1)404 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_03, TestSize.Level1)
405 {
406 CALL_TEST_DEBUG;
407 KeyGestureManager::PullUpAccessibility pullUpAccessibility;
408 DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON;
409 DISPLAY_MONITOR->isScreenLocked_ = false;
410 bool ret = pullUpAccessibility.IsWorking();
411 EXPECT_FALSE(ret);
412 }
413
414 /**
415 * @tc.name: KeyGestureManagerTest_OnTriggerAll_01
416 * @tc.desc: Test the funcation OnTriggerAll
417 * @tc.type: FUNC
418 * @tc.require:
419 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_OnTriggerAll_01, TestSize.Level1)420 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_OnTriggerAll_01, TestSize.Level1)
421 {
422 CALL_TEST_DEBUG;
423 KeyGestureManager::PullUpAccessibility pullUpAccessibility;
424 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
425 ASSERT_NE(keyEvent, nullptr);
426 ASSERT_NO_FATAL_FAILURE(pullUpAccessibility.OnTriggerAll(keyEvent));
427 }
428
429 /**
430 * @tc.name: KeyGestureManagerTest_RecognizeGesture_01
431 * @tc.desc: Test the funcation RecognizeGesture
432 * @tc.type: FUNC
433 * @tc.require:
434 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_01, TestSize.Level1)435 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_01, TestSize.Level1)
436 {
437 CALL_TEST_DEBUG;
438 std::set<int32_t> keys = {1, 2, 3};
439 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
440 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
441 ASSERT_NE(keyEvent, nullptr);
442
443 std::vector<int32_t> pressedKeys = {1};
444 EXPECT_TRUE(pressedKeys.size() == SINGLE_KEY_PRESSED);
445 bool ret = longPressCombinationKey.RecognizeGesture(keyEvent);
446 EXPECT_FALSE(ret);
447 }
448
449 /**
450 * @tc.name: KeyGestureManagerTest_RecognizeGesture_02
451 * @tc.desc: Test the funcation RecognizeGesture
452 * @tc.type: FUNC
453 * @tc.require:
454 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_02, TestSize.Level1)455 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_02, TestSize.Level1)
456 {
457 CALL_TEST_DEBUG;
458 std::set<int32_t> keys = { 1, 2, 3 };
459 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
460 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
461 ASSERT_NE(keyEvent, nullptr);
462
463 std::vector<int32_t> pressedKeys = { 2, 3, 4 };
464 EXPECT_FALSE(pressedKeys.size() == SINGLE_KEY_PRESSED);
465 bool ret = longPressCombinationKey.RecognizeGesture(keyEvent);
466 EXPECT_FALSE(ret);
467 }
468
469 /**
470 * @tc.name: KeyGestureManagerTest_TriggerAll_01
471 * @tc.desc: Test the funcation TriggerAll
472 * @tc.type: FUNC
473 * @tc.require:
474 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerAll_01, TestSize.Level1)475 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerAll_01, TestSize.Level1)
476 {
477 CALL_TEST_DEBUG;
478 std::set<int32_t> keys = { 1, 2, 3 };
479 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
480 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
481 ASSERT_NE(keyEvent, nullptr);
482 ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.TriggerAll(keyEvent));
483 }
484
485 /**
486 * @tc.name: KeyGestureManagerTest_RunPending_01
487 * @tc.desc: Test the funcation RunPending
488 * @tc.type: FUNC
489 * @tc.require:
490 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_01, TestSize.Level1)491 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_01, 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
497 handler.keyEvent_ = nullptr;
498 ASSERT_NO_FATAL_FAILURE(handler.RunPending());
499 }
500
501 /**
502 * @tc.name: KeyGestureManagerTest_RunPending_02
503 * @tc.desc: Test the funcation RunPending
504 * @tc.type: FUNC
505 * @tc.require:
506 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_02, TestSize.Level1)507 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_02, TestSize.Level1)
508 {
509 CALL_TEST_DEBUG;
510 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
511 KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
512
513 handler.keyEvent_ = KeyEvent::Create();
514 ASSERT_NE(handler.keyEvent_, nullptr);
515 ASSERT_NO_FATAL_FAILURE(handler.RunPending());
516 }
517
518 /**
519 * @tc.name: KeyGestureManagerTest_ResetTimer_01
520 * @tc.desc: Test the funcation ResetTimer
521 * @tc.type: FUNC
522 * @tc.require:
523 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_01, TestSize.Level1)524 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_01, TestSize.Level1)
525 {
526 CALL_TEST_DEBUG;
527 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
528 KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
529 handler.timerId_ = 1;
530 ASSERT_NO_FATAL_FAILURE(handler.ResetTimer());
531 }
532
533 /**
534 * @tc.name: KeyGestureManagerTest_ResetTimer_02
535 * @tc.desc: Test the funcation ResetTimer
536 * @tc.type: FUNC
537 * @tc.require:
538 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_02, TestSize.Level1)539 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_02, TestSize.Level1)
540 {
541 CALL_TEST_DEBUG;
542 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
543 KeyGestureManager::Handler handler(1, 2, 3000, myCallback);
544 handler.timerId_ = -2;
545 ASSERT_NO_FATAL_FAILURE(handler.ResetTimer());
546 }
547
548 /**
549 * @tc.name: KeyGestureManagerTest_RunPendingHandlers_01
550 * @tc.desc: Test the funcation RunPendingHandlers
551 * @tc.type: FUNC
552 * @tc.require:
553 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPendingHandlers_01, TestSize.Level1)554 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPendingHandlers_01, TestSize.Level1)
555 {
556 CALL_TEST_DEBUG;
557 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
558 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
559 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
560 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
561
562 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
563 myKeyGesture->handlers_.push_back(handler1);
564 myKeyGesture->handlers_.push_back(handler2);
565 myKeyGesture->handlers_.push_back(handler3);
566
567 std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids();
568 bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids);
569 EXPECT_FALSE(haveForeground);
570
571 int32_t keyCode = 1;
572 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode);
573 ASSERT_NO_FATAL_FAILURE(longPressSingleKey.RunPendingHandlers());
574 }
575
576 /**
577 * @tc.name: KeyGestureManagerTest_RunHandler_01
578 * @tc.desc: Test the funcation RunHandler
579 * @tc.type: FUNC
580 * @tc.require:
581 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunHandler_01, TestSize.Level1)582 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunHandler_01, TestSize.Level1)
583 {
584 CALL_TEST_DEBUG;
585 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
586 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
587 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
588 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
589
590 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
591 myKeyGesture->handlers_.push_back(handler1);
592 myKeyGesture->handlers_.push_back(handler2);
593 myKeyGesture->handlers_.push_back(handler3);
594
595 int32_t handlerId = 1;
596 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
597 ASSERT_NE(keyEvent, nullptr);
598 ASSERT_NO_FATAL_FAILURE(myKeyGesture->RunHandler(handlerId, keyEvent));
599
600 handlerId = 5;
601 ASSERT_NO_FATAL_FAILURE(myKeyGesture->RunHandler(handlerId, keyEvent));
602 }
603
604 /**
605 * @tc.name: LongPressCombinationKey_Intercept_01
606 * @tc.desc: Test the funcation RecognizeGesture
607 * @tc.type: FUNC
608 * @tc.require:
609 */
HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_01, TestSize.Level1)610 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_01, TestSize.Level1)
611 {
612 CALL_TEST_DEBUG;
613 std::set<int32_t> keys = {1, 2, 3};
614 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
615 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
616 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
617 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
618 ASSERT_NE(keyEvent, nullptr);
619
620 keyEvent->keyCode_ = 3;
621 longPressCombinationKey.keys_ = {2, 3, 4};
622 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN;
623 myKeyGesture->active_ = true;
624 EXPECT_FALSE(EventLogHelper::IsBetaVersion());
625 EXPECT_FALSE(keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE));
626 bool ret = longPressCombinationKey.Intercept(keyEvent);
627 EXPECT_FALSE(ret);
628 myKeyGesture->active_ = false;
629 EXPECT_TRUE(myKeyGesture->IsWorking());
630
631 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
632 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
633 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
634 myKeyGesture->handlers_.push_back(handler1);
635 myKeyGesture->handlers_.push_back(handler2);
636 myKeyGesture->handlers_.push_back(handler3);
637 EXPECT_FALSE(myKeyGesture->handlers_.empty());
638 bool ret2 = longPressCombinationKey.Intercept(keyEvent);
639 EXPECT_FALSE(ret2);
640 }
641
642 /**
643 * @tc.name: LongPressCombinationKey_Intercept_02
644 * @tc.desc: Test the funcation RecognizeGesture
645 * @tc.type: FUNC
646 * @tc.require:
647 */
HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_02, TestSize.Level1)648 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_02, TestSize.Level1)
649 {
650 CALL_TEST_DEBUG;
651 std::set<int32_t> keys = {1, 2, 3};
652 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys);
653 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
654 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
655 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
656 ASSERT_NE(keyEvent, nullptr);
657
658 keyEvent->keyCode_ = 5;
659 longPressCombinationKey.keys_ = {2, 3, 4};
660 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP;
661 myKeyGesture->active_ = true;
662 bool ret = longPressCombinationKey.Intercept(keyEvent);
663 EXPECT_FALSE(ret);
664
665 myKeyGesture->active_ = false;
666 bool ret2 = longPressCombinationKey.Intercept(keyEvent);
667 EXPECT_FALSE(ret2);
668 }
669
670 /**
671 * @tc.name: KeyGestureManagerTest_NotifyHandlers_01
672 * @tc.desc: Test the funcation NotifyHandlers
673 * @tc.type: FUNC
674 * @tc.require:
675 */
HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_NotifyHandlers_01, TestSize.Level1)676 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_NotifyHandlers_01, TestSize.Level1)
677 {
678 CALL_TEST_DEBUG;
679 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
680 ASSERT_NE(keyEvent, nullptr);
681 std::function<void(std::shared_ptr<KeyEvent>)> myCallback;
682 KeyGestureManager::Handler handler1(1, 10, 500, myCallback);
683 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback);
684 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback);
685
686 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>();
687 myKeyGesture->handlers_.push_back(handler1);
688 myKeyGesture->handlers_.push_back(handler2);
689 myKeyGesture->handlers_.push_back(handler3);
690
691 std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids();
692 bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids);
693 EXPECT_FALSE(haveForeground);
694 ASSERT_NO_FATAL_FAILURE(myKeyGesture->NotifyHandlers(keyEvent));
695 }
696 } // namespace MMI
697 } // namespace OHOS