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 "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_01 75 * @tc.desc: Test the funcation Intercept 76 * @tc.type: FUNC 77 * @tc.require: 78 */ 79 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_01, TestSize.Level1) 80 { 81 CALL_TEST_DEBUG; 82 KeyGestureManager keyGestureManager; 83 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 84 ASSERT_NE(keyEvent, nullptr); 85 bool ret = keyGestureManager.Intercept(keyEvent); 86 EXPECT_FALSE(ret); 87 } 88 89 /** 90 * @tc.name: KeyGestureManagerTest_RemoveKeyGesture_01 91 * @tc.desc: Test the funcation RemoveKeyGesture 92 * @tc.type: FUNC 93 * @tc.require: 94 */ 95 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_01, TestSize.Level1) 96 { 97 CALL_TEST_DEBUG; 98 KeyGestureManager keyGestureManager; 99 int32_t id = 1; 100 ASSERT_NO_FATAL_FAILURE(keyGestureManager.RemoveKeyGesture(id)); 101 } 102 103 /** 104 * @tc.name: KeyGestureManagerTest_RemoveKeyGesture_02 105 * @tc.desc: Test the funcation RemoveKeyGesture 106 * @tc.type: FUNC 107 * @tc.require: 108 */ 109 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RemoveKeyGesture_02, TestSize.Level1) 110 { 111 CALL_TEST_DEBUG; 112 KeyGestureManager keyGestureManager; 113 int32_t id = -2; 114 ASSERT_NO_FATAL_FAILURE(keyGestureManager.RemoveKeyGesture(id)); 115 } 116 117 /** 118 * @tc.name: KeyGestureManagerTest_AddKeyGesture_01 119 * @tc.desc: Test the funcation AddKeyGesture 120 * @tc.type: FUNC 121 * @tc.require: 122 */ 123 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_AddKeyGesture_01, TestSize.Level1) 124 { 125 CALL_TEST_DEBUG; 126 KeyGestureManager keyGestureManager; 127 int32_t pid = 1; 128 std::shared_ptr<KeyOption> keyOption = nullptr; __anonf7d829600202(std::shared_ptr<KeyEvent> event) 129 auto callback = [](std::shared_ptr<KeyEvent> event) {}; 130 int32_t result = keyGestureManager.AddKeyGesture(pid, keyOption, callback); 131 EXPECT_EQ(result, INVALID_ENTITY_ID); 132 } 133 134 /** 135 * @tc.name: KeyGestureManagerTest_ShouldIntercept_01 136 * @tc.desc: Test the funcation ShouldIntercept 137 * @tc.type: FUNC 138 * @tc.require: 139 */ 140 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ShouldIntercept_01, TestSize.Level1) 141 { 142 CALL_TEST_DEBUG; 143 KeyGestureManager keyGestureManager; 144 std::shared_ptr<KeyOption> keyOption = nullptr; 145 bool result = keyGestureManager.ShouldIntercept(keyOption); 146 EXPECT_FALSE(result); 147 } 148 149 /** 150 * @tc.name: KeyGestureManagerTest_Intercept_02 151 * @tc.desc: Test the funcation ShouldIntercept 152 * @tc.type: FUNC 153 * @tc.require: 154 */ 155 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_02, TestSize.Level1) 156 { 157 CALL_TEST_DEBUG; 158 int32_t keyCode = 1; 159 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode); 160 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 161 ASSERT_NE(keyEvent, nullptr); 162 keyEvent->keyCode_ = 2; 163 longPressSingleKey.keyCode_ = 2; 164 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN; 165 166 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 167 myKeyGesture->active_ = true; 168 bool ret = longPressSingleKey.Intercept(keyEvent); 169 EXPECT_TRUE(ret); 170 171 myKeyGesture->active_ = true; 172 bool ret2 = longPressSingleKey.Intercept(keyEvent); 173 EXPECT_TRUE(ret2); 174 } 175 176 /** 177 * @tc.name: KeyGestureManagerTest_Intercept_03 178 * @tc.desc: Test the funcation ShouldIntercept 179 * @tc.type: FUNC 180 * @tc.require: 181 */ 182 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_03, TestSize.Level1) 183 { 184 CALL_TEST_DEBUG; 185 int32_t keyCode = 1; 186 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode); 187 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 188 ASSERT_NE(keyEvent, nullptr); 189 keyEvent->keyCode_ = 3; 190 longPressSingleKey.keyCode_ = 2; 191 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN; 192 bool ret = longPressSingleKey.Intercept(keyEvent); 193 EXPECT_FALSE(ret); 194 } 195 196 /** 197 * @tc.name: KeyGestureManagerTest_Intercept_04 198 * @tc.desc: Test the funcation ShouldIntercept 199 * @tc.type: FUNC 200 * @tc.require: 201 */ 202 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_04, TestSize.Level1) 203 { 204 CALL_TEST_DEBUG; 205 int32_t keyCode = 1; 206 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode); 207 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 208 ASSERT_NE(keyEvent, nullptr); 209 keyEvent->keyCode_ = 2; 210 longPressSingleKey.keyCode_ = 2; 211 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP; 212 bool ret = longPressSingleKey.Intercept(keyEvent); 213 EXPECT_FALSE(ret); 214 } 215 216 /** 217 * @tc.name: KeyGestureManagerTest_Intercept_05 218 * @tc.desc: Test the funcation ShouldIntercept 219 * @tc.type: FUNC 220 * @tc.require: 221 */ 222 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_05, TestSize.Level1) 223 { 224 CALL_TEST_DEBUG; 225 int32_t keyCode = 1; 226 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode); 227 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 228 ASSERT_NE(keyEvent, nullptr); 229 keyEvent->keyCode_ = 3; 230 longPressSingleKey.keyCode_ = 2; 231 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP; 232 bool ret = longPressSingleKey.Intercept(keyEvent); 233 EXPECT_FALSE(ret); 234 } 235 236 /** 237 * @tc.name: KeyGestureManagerTest_Intercept_06 238 * @tc.desc: Test the funcation ShouldIntercept 239 * @tc.type: FUNC 240 * @tc.require: 241 */ 242 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_Intercept_06, TestSize.Level1) 243 { 244 CALL_TEST_DEBUG; 245 int32_t keyCode = 1; 246 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode); 247 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 248 ASSERT_NE(keyEvent, nullptr); 249 keyEvent->keyCode_ = 3; 250 longPressSingleKey.keyCode_ = 2; 251 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP; 252 253 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 254 myKeyGesture->active_ = true; 255 bool ret = longPressSingleKey.Intercept(keyEvent); 256 EXPECT_FALSE(ret); 257 258 myKeyGesture->active_ = false; 259 bool ret2 = longPressSingleKey.Intercept(keyEvent); 260 EXPECT_FALSE(ret2); 261 } 262 263 /** 264 * @tc.name: KeyGestureManagerTest_IsWorking_01 265 * @tc.desc: Test the funcation ShouldIntercept 266 * @tc.type: FUNC 267 * @tc.require: 268 */ 269 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_01, TestSize.Level1) 270 { 271 CALL_TEST_DEBUG; 272 KeyGestureManager::PullUpAccessibility pullUpAccessibility; 273 DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF; 274 bool ret = pullUpAccessibility.IsWorking(); 275 EXPECT_FALSE(ret); 276 } 277 278 /** 279 * @tc.name: KeyGestureManagerTest_IsWorking_02 280 * @tc.desc: Test the funcation ShouldIntercept 281 * @tc.type: FUNC 282 * @tc.require: 283 */ 284 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_02, TestSize.Level1) 285 { 286 CALL_TEST_DEBUG; 287 KeyGestureManager::PullUpAccessibility pullUpAccessibility; 288 DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON; 289 DISPLAY_MONITOR->isScreenLocked_ = true; 290 bool ret = pullUpAccessibility.IsWorking(); 291 EXPECT_FALSE(ret); 292 } 293 294 /** 295 * @tc.name: KeyGestureManagerTest_IsWorking_03 296 * @tc.desc: Test the funcation ShouldIntercept 297 * @tc.type: FUNC 298 * @tc.require: 299 */ 300 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_IsWorking_03, TestSize.Level1) 301 { 302 CALL_TEST_DEBUG; 303 KeyGestureManager::PullUpAccessibility pullUpAccessibility; 304 DISPLAY_MONITOR->screenStatus_ = EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON; 305 DISPLAY_MONITOR->isScreenLocked_ = false; 306 bool ret = pullUpAccessibility.IsWorking(); 307 EXPECT_FALSE(ret); 308 } 309 310 /** 311 * @tc.name: KeyGestureManagerTest_OnTriggerAll_01 312 * @tc.desc: Test the funcation OnTriggerAll 313 * @tc.type: FUNC 314 * @tc.require: 315 */ 316 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_OnTriggerAll_01, TestSize.Level1) 317 { 318 CALL_TEST_DEBUG; 319 KeyGestureManager::PullUpAccessibility pullUpAccessibility; 320 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 321 ASSERT_NE(keyEvent, nullptr); 322 ASSERT_NO_FATAL_FAILURE(pullUpAccessibility.OnTriggerAll(keyEvent)); 323 } 324 325 /** 326 * @tc.name: KeyGestureManagerTest_RecognizeGesture_01 327 * @tc.desc: Test the funcation RecognizeGesture 328 * @tc.type: FUNC 329 * @tc.require: 330 */ 331 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_01, TestSize.Level1) 332 { 333 CALL_TEST_DEBUG; 334 std::set<int32_t> keys = {1, 2, 3}; 335 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys); 336 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 337 ASSERT_NE(keyEvent, nullptr); 338 339 std::vector<int32_t> pressedKeys = {1}; 340 EXPECT_TRUE(pressedKeys.size() == SINGLE_KEY_PRESSED); 341 bool ret = longPressCombinationKey.RecognizeGesture(keyEvent); 342 EXPECT_FALSE(ret); 343 } 344 345 /** 346 * @tc.name: KeyGestureManagerTest_RecognizeGesture_02 347 * @tc.desc: Test the funcation RecognizeGesture 348 * @tc.type: FUNC 349 * @tc.require: 350 */ 351 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RecognizeGesture_02, TestSize.Level1) 352 { 353 CALL_TEST_DEBUG; 354 std::set<int32_t> keys = { 1, 2, 3 }; 355 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys); 356 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 357 ASSERT_NE(keyEvent, nullptr); 358 359 std::vector<int32_t> pressedKeys = { 2, 3, 4 }; 360 EXPECT_FALSE(pressedKeys.size() == SINGLE_KEY_PRESSED); 361 bool ret = longPressCombinationKey.RecognizeGesture(keyEvent); 362 EXPECT_FALSE(ret); 363 } 364 365 /** 366 * @tc.name: KeyGestureManagerTest_TriggerAll_01 367 * @tc.desc: Test the funcation TriggerAll 368 * @tc.type: FUNC 369 * @tc.require: 370 */ 371 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_TriggerAll_01, TestSize.Level1) 372 { 373 CALL_TEST_DEBUG; 374 std::set<int32_t> keys = { 1, 2, 3 }; 375 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys); 376 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 377 ASSERT_NE(keyEvent, nullptr); 378 ASSERT_NO_FATAL_FAILURE(longPressCombinationKey.TriggerAll(keyEvent)); 379 } 380 381 /** 382 * @tc.name: KeyGestureManagerTest_RunPending_01 383 * @tc.desc: Test the funcation RunPending 384 * @tc.type: FUNC 385 * @tc.require: 386 */ 387 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_01, TestSize.Level1) 388 { 389 CALL_TEST_DEBUG; 390 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 391 KeyGestureManager::Handler handler(1, 2, 3000, myCallback); 392 393 handler.keyEvent_ = nullptr; 394 ASSERT_NO_FATAL_FAILURE(handler.RunPending()); 395 } 396 397 /** 398 * @tc.name: KeyGestureManagerTest_RunPending_02 399 * @tc.desc: Test the funcation RunPending 400 * @tc.type: FUNC 401 * @tc.require: 402 */ 403 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPending_02, TestSize.Level1) 404 { 405 CALL_TEST_DEBUG; 406 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 407 KeyGestureManager::Handler handler(1, 2, 3000, myCallback); 408 409 handler.keyEvent_ = KeyEvent::Create(); 410 ASSERT_NE(handler.keyEvent_, nullptr); 411 ASSERT_NO_FATAL_FAILURE(handler.RunPending()); 412 } 413 414 /** 415 * @tc.name: KeyGestureManagerTest_ResetTimer_01 416 * @tc.desc: Test the funcation ResetTimer 417 * @tc.type: FUNC 418 * @tc.require: 419 */ 420 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_01, TestSize.Level1) 421 { 422 CALL_TEST_DEBUG; 423 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 424 KeyGestureManager::Handler handler(1, 2, 3000, myCallback); 425 handler.timerId_ = 1; 426 ASSERT_NO_FATAL_FAILURE(handler.ResetTimer()); 427 } 428 429 /** 430 * @tc.name: KeyGestureManagerTest_ResetTimer_02 431 * @tc.desc: Test the funcation ResetTimer 432 * @tc.type: FUNC 433 * @tc.require: 434 */ 435 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_ResetTimer_02, TestSize.Level1) 436 { 437 CALL_TEST_DEBUG; 438 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 439 KeyGestureManager::Handler handler(1, 2, 3000, myCallback); 440 handler.timerId_ = -2; 441 ASSERT_NO_FATAL_FAILURE(handler.ResetTimer()); 442 } 443 444 /** 445 * @tc.name: KeyGestureManagerTest_RunPendingHandlers_01 446 * @tc.desc: Test the funcation RunPendingHandlers 447 * @tc.type: FUNC 448 * @tc.require: 449 */ 450 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunPendingHandlers_01, TestSize.Level1) 451 { 452 CALL_TEST_DEBUG; 453 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 454 KeyGestureManager::Handler handler1(1, 10, 500, myCallback); 455 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback); 456 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback); 457 458 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 459 myKeyGesture->handlers_.push_back(handler1); 460 myKeyGesture->handlers_.push_back(handler2); 461 myKeyGesture->handlers_.push_back(handler3); 462 463 std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids(); 464 bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids); 465 EXPECT_FALSE(haveForeground); 466 467 int32_t keyCode = 1; 468 KeyGestureManager::LongPressSingleKey longPressSingleKey(keyCode); 469 ASSERT_NO_FATAL_FAILURE(longPressSingleKey.RunPendingHandlers()); 470 } 471 472 /** 473 * @tc.name: KeyGestureManagerTest_RunHandler_01 474 * @tc.desc: Test the funcation RunHandler 475 * @tc.type: FUNC 476 * @tc.require: 477 */ 478 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_RunHandler_01, TestSize.Level1) 479 { 480 CALL_TEST_DEBUG; 481 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 482 KeyGestureManager::Handler handler1(1, 10, 500, myCallback); 483 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback); 484 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback); 485 486 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 487 myKeyGesture->handlers_.push_back(handler1); 488 myKeyGesture->handlers_.push_back(handler2); 489 myKeyGesture->handlers_.push_back(handler3); 490 491 int32_t handlerId = 1; 492 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 493 ASSERT_NE(keyEvent, nullptr); 494 ASSERT_NO_FATAL_FAILURE(myKeyGesture->RunHandler(handlerId, keyEvent)); 495 496 handlerId = 5; 497 ASSERT_NO_FATAL_FAILURE(myKeyGesture->RunHandler(handlerId, keyEvent)); 498 } 499 500 /** 501 * @tc.name: LongPressCombinationKey_Intercept_01 502 * @tc.desc: Test the funcation RecognizeGesture 503 * @tc.type: FUNC 504 * @tc.require: 505 */ 506 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_01, TestSize.Level1) 507 { 508 CALL_TEST_DEBUG; 509 std::set<int32_t> keys = {1, 2, 3}; 510 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys); 511 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 512 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 513 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 514 ASSERT_NE(keyEvent, nullptr); 515 516 keyEvent->keyCode_ = 3; 517 longPressCombinationKey.keys_ = {2, 3, 4}; 518 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_DOWN; 519 myKeyGesture->active_ = true; 520 bool ret = longPressCombinationKey.Intercept(keyEvent); 521 EXPECT_FALSE(ret); 522 myKeyGesture->active_ = false; 523 EXPECT_TRUE(myKeyGesture->IsWorking()); 524 525 KeyGestureManager::Handler handler1(1, 10, 500, myCallback); 526 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback); 527 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback); 528 myKeyGesture->handlers_.push_back(handler1); 529 myKeyGesture->handlers_.push_back(handler2); 530 myKeyGesture->handlers_.push_back(handler3); 531 EXPECT_FALSE(myKeyGesture->handlers_.empty()); 532 bool ret2 = longPressCombinationKey.Intercept(keyEvent); 533 EXPECT_FALSE(ret2); 534 } 535 536 /** 537 * @tc.name: LongPressCombinationKey_Intercept_02 538 * @tc.desc: Test the funcation RecognizeGesture 539 * @tc.type: FUNC 540 * @tc.require: 541 */ 542 HWTEST_F(KeyGestureManagerTest, LongPressCombinationKey_Intercept_02, TestSize.Level1) 543 { 544 CALL_TEST_DEBUG; 545 std::set<int32_t> keys = {1, 2, 3}; 546 KeyGestureManager::LongPressCombinationKey longPressCombinationKey(keys); 547 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 548 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 549 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 550 ASSERT_NE(keyEvent, nullptr); 551 552 keyEvent->keyCode_ = 5; 553 longPressCombinationKey.keys_ = {2, 3, 4}; 554 keyEvent->keyAction_ = KeyEvent::KEY_ACTION_UP; 555 myKeyGesture->active_ = true; 556 bool ret = longPressCombinationKey.Intercept(keyEvent); 557 EXPECT_FALSE(ret); 558 559 myKeyGesture->active_ = false; 560 bool ret2 = longPressCombinationKey.Intercept(keyEvent); 561 EXPECT_FALSE(ret2); 562 } 563 564 /** 565 * @tc.name: KeyGestureManagerTest_NotifyHandlers_01 566 * @tc.desc: Test the funcation NotifyHandlers 567 * @tc.type: FUNC 568 * @tc.require: 569 */ 570 HWTEST_F(KeyGestureManagerTest, KeyGestureManagerTest_NotifyHandlers_01, TestSize.Level1) 571 { 572 CALL_TEST_DEBUG; 573 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create(); 574 ASSERT_NE(keyEvent, nullptr); 575 std::function<void(std::shared_ptr<KeyEvent>)> myCallback; 576 KeyGestureManager::Handler handler1(1, 10, 500, myCallback); 577 KeyGestureManager::Handler handler2(2, 20, 1000, myCallback); 578 KeyGestureManager::Handler handler3(3, 30, 1500, myCallback); 579 580 std::shared_ptr<MyKeyGesture> myKeyGesture = std::make_shared<MyKeyGesture>(); 581 myKeyGesture->handlers_.push_back(handler1); 582 myKeyGesture->handlers_.push_back(handler2); 583 myKeyGesture->handlers_.push_back(handler3); 584 585 std::set<int32_t> foregroundPids = myKeyGesture->GetForegroundPids(); 586 bool haveForeground = myKeyGesture->HaveForegroundHandler(foregroundPids); 587 EXPECT_FALSE(haveForeground); 588 ASSERT_NO_FATAL_FAILURE(myKeyGesture->NotifyHandlers(keyEvent)); 589 } 590 } // namespace MMI 591 } // namespace OHOS