1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 18 #include "key_command_handler_util.h" 19 #include "mmi_log.h" 20 21 #undef MMI_LOG_TAG 22 #define MMI_LOG_TAG "KeyCommandHandlerUtilTest" 23 24 namespace OHOS { 25 namespace MMI { 26 namespace { 27 using namespace testing::ext; 28 } // namespace 29 30 class KeyCommandHandlerUtilTest : public testing::Test { 31 public: SetUpTestCase(void)32 static void SetUpTestCase(void) {} TearDownTestCase(void)33 static void TearDownTestCase(void) {} 34 }; 35 36 /** 37 * @tc.name: KeyCommandHandlerUtilTest_IsSpecialType_001 38 * @tc.desc: Test the function IsSpecialType 39 * @tc.type: FUNC 40 * @tc.require: 41 */ 42 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_IsSpecialType_001, TestSize.Level1) 43 { 44 CALL_TEST_DEBUG; 45 SpecialType type = SPECIAL_ALL; 46 int32_t keyCode = 1; 47 bool result = OHOS::MMI::IsSpecialType(keyCode, type); 48 EXPECT_FALSE(result); 49 type = SUBSCRIBER_BEFORE_DELAY; 50 keyCode = 2; 51 result = OHOS::MMI::IsSpecialType(keyCode, type); 52 EXPECT_FALSE(result); 53 type = KEY_DOWN_ACTION; 54 keyCode = 3; 55 result = OHOS::MMI::IsSpecialType(keyCode, type); 56 EXPECT_FALSE(result); 57 type = KEY_DOWN_ACTION; 58 keyCode = -1; 59 result = OHOS::MMI::IsSpecialType(keyCode, type); 60 EXPECT_FALSE(result); 61 } 62 63 /** 64 * @tc.name: KeyCommandHandlerUtilTest_GetBusinessId_001 65 * @tc.desc: Test the function GetBusinessId 66 * @tc.type: FUNC 67 * @tc.require: 68 */ 69 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetBusinessId_001, TestSize.Level1) 70 { 71 CALL_TEST_DEBUG; 72 cJSON *jsonData = cJSON_CreateString("not an object"); 73 std::string businessIdValue; 74 std::vector<std::string> businessIds; 75 bool result = OHOS::MMI::GetBusinessId(jsonData, businessIdValue, businessIds); 76 EXPECT_FALSE(result); 77 cJSON_Delete(jsonData); 78 } 79 80 /** 81 * @tc.name: KeyCommandHandlerUtilTest_GetBusinessId_002 82 * @tc.desc: Test the function GetBusinessId 83 * @tc.type: FUNC 84 * @tc.require: 85 */ 86 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetBusinessId_002, TestSize.Level1) 87 { 88 CALL_TEST_DEBUG; 89 cJSON *jsonData = cJSON_CreateObject(); 90 cJSON_AddItemToObject(jsonData, "businessId", cJSON_CreateNumber(123)); 91 std::string businessIdValue; 92 std::vector<std::string> businessIds; 93 bool result = OHOS::MMI::GetBusinessId(jsonData, businessIdValue, businessIds); 94 EXPECT_FALSE(result); 95 cJSON_Delete(jsonData); 96 } 97 98 /** 99 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_001 100 * @tc.desc: Test the function GetPreKeys 101 * @tc.type: FUNC 102 * @tc.require: 103 */ 104 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_001, TestSize.Level1) 105 { 106 CALL_TEST_DEBUG; 107 cJSON* jsonData = cJSON_CreateObject(); 108 ShortcutKey shortcutKey; 109 bool result = OHOS::MMI::GetPreKeys(jsonData, shortcutKey); 110 EXPECT_FALSE(result); 111 cJSON_Delete(jsonData); 112 } 113 114 /** 115 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_002 116 * @tc.desc: Test the function GetPreKeys 117 * @tc.type: FUNC 118 * @tc.require: 119 */ 120 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_002, TestSize.Level1) 121 { 122 CALL_TEST_DEBUG; 123 cJSON* jsonData = cJSON_CreateObject(); 124 cJSON* preKey = cJSON_CreateArray(); 125 for (int i = 0; i < MAX_PREKEYS_NUM + 1; ++i) { 126 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i)); 127 } 128 cJSON_AddItemToObject(jsonData, "preKey", preKey); 129 ShortcutKey shortcutKey; 130 bool result = OHOS::MMI::GetPreKeys(jsonData, shortcutKey); 131 EXPECT_FALSE(result); 132 cJSON_Delete(jsonData); 133 } 134 135 /** 136 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_003 137 * @tc.desc: Test the function GetPreKeys 138 * @tc.type: FUNC 139 * @tc.require: 140 */ 141 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_003, TestSize.Level1) 142 { 143 CALL_TEST_DEBUG; 144 cJSON* jsonData = cJSON_CreateObject(); 145 cJSON_AddItemToObject(jsonData, "preKey", cJSON_CreateString("invalid")); 146 ShortcutKey shortcutKey; 147 bool result = OHOS::MMI::GetPreKeys(jsonData, shortcutKey); 148 EXPECT_FALSE(result); 149 cJSON_Delete(jsonData); 150 } 151 152 /** 153 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_004 154 * @tc.desc: Test the function GetPreKeys 155 * @tc.type: FUNC 156 * @tc.require: 157 */ 158 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_004, TestSize.Level1) 159 { 160 CALL_TEST_DEBUG; 161 cJSON* jsonData = cJSON_CreateObject(); 162 cJSON* preKey = cJSON_CreateArray(); 163 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(-1)); 164 cJSON_AddItemToObject(jsonData, "preKey", preKey); 165 ShortcutKey shortcutKey; 166 bool result = OHOS::MMI::GetPreKeys(jsonData, shortcutKey); 167 EXPECT_FALSE(result); 168 cJSON_Delete(jsonData); 169 } 170 171 /** 172 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_005 173 * @tc.desc: Test the function GetPreKeys 174 * @tc.type: FUNC 175 * @tc.require: 176 */ 177 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_005, TestSize.Level1) 178 { 179 CALL_TEST_DEBUG; 180 cJSON* jsonData = cJSON_CreateObject(); 181 cJSON* preKey = cJSON_CreateArray(); 182 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1)); 183 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1)); 184 cJSON_AddItemToObject(jsonData, "preKey", preKey); 185 ShortcutKey shortcutKey; 186 bool result = OHOS::MMI::GetPreKeys(jsonData, shortcutKey); 187 EXPECT_FALSE(result); 188 cJSON_Delete(jsonData); 189 } 190 191 /** 192 * @tc.name: KeyCommandHandlerUtilTest_IsSpecialType_002 193 * @tc.desc: Test keyCode is not in SPECIAL_KEYS 194 * @tc.type: FUNC 195 * @tc.require: 196 */ 197 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_IsSpecialType_002, TestSize.Level1) 198 { 199 CALL_TEST_DEBUG; 200 int32_t keyCode = 999; 201 SpecialType type = SpecialType::SPECIAL_ALL; 202 EXPECT_FALSE(OHOS::MMI::IsSpecialType(keyCode, type)); 203 } 204 205 /** 206 * @tc.name: KeyCommandHandlerUtilTest_IsSpecialType_003 207 * @tc.desc: The corresponding value is not equal to SpecialType.: SPECIAL_ALL and input type 208 * @tc.type: FUNC 209 * @tc.require: 210 */ 211 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_IsSpecialType_003, TestSize.Level1) 212 { 213 CALL_TEST_DEBUG; 214 int32_t keyCode = 16; 215 SpecialType type = SpecialType::SPECIAL_ALL; 216 EXPECT_FALSE(OHOS::MMI::IsSpecialType(keyCode, type)); 217 } 218 219 /** 220 * @tc.name: KeyCommandHandlerUtilTest_IsSpecialType_004 221 * @tc.desc: The test keyCode is in SPECIAL_KEYS and the value is equal to SpecialType.: SPECIAL_ALL 222 * @tc.type: FUNC 223 * @tc.require: 224 */ 225 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_IsSpecialType_004, TestSize.Level1) 226 { 227 CALL_TEST_DEBUG; 228 int32_t keyCode = 0; 229 SpecialType type = SpecialType::SPECIAL_ALL; 230 EXPECT_FALSE(OHOS::MMI::IsSpecialType(keyCode, type)); 231 } 232 233 /** 234 * @tc.name: KeyCommandHandlerUtilTest_GetBusinessId_003 235 * @tc.desc: Test the scenario where the JSON object is not a valid object 236 * @tc.type: FUNC 237 * @tc.require: 238 */ 239 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetBusinessId_003, TestSize.Level1) 240 { 241 CALL_TEST_DEBUG; 242 cJSON *jsonData = nullptr; 243 std::string businessIdValue; 244 std::vector<std::string> businessIds; 245 bool result = OHOS::MMI::GetBusinessId(jsonData, businessIdValue, businessIds); 246 EXPECT_FALSE(result); 247 cJSON_Delete(jsonData); 248 } 249 250 /** 251 * @tc.name: KeyCommandHandlerUtilTest_GetBusinessId_004 252 * @tc.desc: Test the scenario where businessId is not a string 253 * @tc.type: FUNC 254 * @tc.require: 255 */ 256 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetBusinessId_004, TestSize.Level1) 257 { 258 CALL_TEST_DEBUG; 259 cJSON *jsonData = cJSON_CreateObject(); 260 std::vector<std::string> businessIds; 261 cJSON_AddItemToObject(jsonData, "businessIds", cJSON_CreateNumber(123)); 262 std::string businessIdValue; 263 bool result = OHOS::MMI::GetBusinessId(jsonData, businessIdValue, businessIds); 264 EXPECT_FALSE(result); 265 cJSON_Delete(jsonData); 266 } 267 268 /** 269 * @tc.name: KeyCommandHandlerUtilTest_GetBusinessId_005 270 * @tc.desc: Test the normal running condition 271 * @tc.type: FUNC 272 * @tc.require: 273 */ 274 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetBusinessId_005, TestSize.Level1) 275 { 276 CALL_TEST_DEBUG; 277 cJSON *jsonData = cJSON_CreateObject(); 278 std::vector<std::string> businessIds; 279 cJSON_AddStringToObject(jsonData, "businessId", "testBusinessId"); 280 std::string businessIdValue; 281 bool result = OHOS::MMI::GetBusinessId(jsonData, businessIdValue, businessIds); 282 EXPECT_TRUE(result); 283 cJSON_Delete(jsonData); 284 } 285 286 /** 287 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_006 288 * @tc.desc: Test the case that the input jsonData is not an object 289 * @tc.type: FUNC 290 * @tc.require: 291 */ 292 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_006, TestSize.Level1) 293 { 294 CALL_TEST_DEBUG; 295 cJSON* jsonData = nullptr; 296 ShortcutKey shortcutKey; 297 EXPECT_FALSE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 298 cJSON_Delete(jsonData); 299 } 300 301 /** 302 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_007 303 * @tc.desc: Test the case that preKey is not an array 304 * @tc.type: FUNC 305 * @tc.require: 306 */ 307 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_007, TestSize.Level1) 308 { 309 CALL_TEST_DEBUG; 310 cJSON* jsonData = cJSON_CreateObject(); 311 cJSON_AddItemToObject(jsonData, "preKey", cJSON_CreateString("test")); 312 ShortcutKey shortcutKey; 313 EXPECT_FALSE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 314 cJSON_Delete(jsonData); 315 } 316 317 /** 318 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_008 319 * @tc.desc: Test the case that the size of preKey exceeds MAX_PREKEYS_NUM 320 * @tc.type: FUNC 321 * @tc.require: 322 */ 323 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_008, TestSize.Level1) 324 { 325 CALL_TEST_DEBUG; 326 cJSON* jsonData = cJSON_CreateObject(); 327 cJSON* preKey = cJSON_CreateArray(); 328 for (int i = 0; i < 10; ++i) { 329 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i)); 330 } 331 cJSON_AddItemToObject(jsonData, "preKey", preKey); 332 ShortcutKey shortcutKey; 333 EXPECT_FALSE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 334 cJSON_Delete(jsonData); 335 } 336 337 /** 338 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_009 339 * @tc.desc: Test if the element in preKey is not a number 340 * @tc.type: FUNC 341 * @tc.require: 342 */ 343 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_009, TestSize.Level1) 344 { 345 CALL_TEST_DEBUG; 346 cJSON* jsonData = cJSON_CreateObject(); 347 cJSON* preKey = cJSON_CreateArray(); 348 cJSON_AddItemToArray(preKey, cJSON_CreateString("not a number")); 349 cJSON_AddItemToObject(jsonData, "preKey", preKey); 350 ShortcutKey shortcutKey; 351 EXPECT_FALSE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 352 cJSON_Delete(jsonData); 353 } 354 355 /** 356 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_010 357 * @tc.desc: Tests if the number in preKey is less than 0 358 * @tc.type: FUNC 359 * @tc.require: 360 */ 361 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_010, TestSize.Level1) 362 { 363 CALL_TEST_DEBUG; 364 cJSON* jsonData = cJSON_CreateObject(); 365 cJSON* preKey = cJSON_CreateArray(); 366 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(-1)); 367 cJSON_AddItemToObject(jsonData, "preKey", preKey); 368 ShortcutKey shortcutKey; 369 EXPECT_FALSE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 370 cJSON_Delete(jsonData); 371 } 372 373 /** 374 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_011 375 * @tc.desc: Test the duplicated number in preKey 376 * @tc.type: FUNC 377 * @tc.require: 378 */ 379 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_011, TestSize.Level1) 380 { 381 CALL_TEST_DEBUG; 382 cJSON* jsonData = cJSON_CreateObject(); 383 cJSON* preKey = cJSON_CreateArray(); 384 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1)); 385 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1)); 386 cJSON_AddItemToObject(jsonData, "preKey", preKey); 387 ShortcutKey shortcutKey; 388 EXPECT_FALSE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 389 cJSON_Delete(jsonData); 390 } 391 392 /** 393 * @tc.name: KeyCommandHandlerUtilTest_GetPreKeys_012 394 * @tc.desc: Test the normal running condition 395 * @tc.type: FUNC 396 * @tc.require: 397 */ 398 HWTEST_F(KeyCommandHandlerUtilTest, KeyCommandHandlerUtilTest_GetPreKeys_012, TestSize.Level1) 399 { 400 CALL_TEST_DEBUG; 401 cJSON* jsonData = cJSON_CreateObject(); 402 cJSON* preKey = cJSON_CreateArray(); 403 cJSON_AddItemToObject(jsonData, "preKey", preKey); 404 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1)); 405 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(2)); 406 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(3)); 407 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(4)); 408 ShortcutKey shortcutKey; 409 EXPECT_TRUE(OHOS::MMI::GetPreKeys(jsonData, shortcutKey)); 410 cJSON_Delete(jsonData); 411 } 412 413 #ifdef OHOS_BUILD_ENABLE_MISTOUCH_PREVENTION 414 /** 415 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey001 416 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_001 417 * @tc.desc: Verify that when the key code configured for a repeat key does not match the actual key code 418 * of the key event, the special repeat key check should return false. 419 */ 420 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey001, TestSize.Level1) 421 { 422 RepeatKey item; 423 item.keyCode = 1; 424 auto keyEvent = std::make_shared<KeyEvent>(); 425 keyEvent->SetKeyCode(0); 426 427 KeyCommandHandler handler; 428 EXPECT_FALSE(handler.CheckSpecialRepeatKey(item, keyEvent)); 429 } 430 431 /** 432 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey002 433 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_002 434 * @tc.desc: Test the key event for the non-volume down button; 435 * it should return false even if the key code matches. 436 */ 437 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey002, TestSize.Level1) 438 { 439 RepeatKey item; 440 item.keyCode = 1; 441 auto keyEvent = std::make_shared<KeyEvent>(); 442 keyEvent->SetKeyCode(1); 443 444 KeyCommandHandler handler; 445 EXPECT_FALSE(handler.CheckSpecialRepeatKey(item, keyEvent)); 446 } 447 448 /** 449 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey003 450 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_003 451 * @tc.desc: Verify that when the application bundleName does not contain ".camera", 452 * the special repeat key check for the volume down button should return false. 453 */ 454 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey003, TestSize.Level1) 455 { 456 RepeatKey item; 457 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 458 item.ability.bundleName = "com.example.app"; 459 auto keyEvent = std::make_shared<KeyEvent>(); 460 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 461 462 KeyCommandHandler handler; 463 EXPECT_FALSE(handler.CheckSpecialRepeatKey(item, keyEvent)); 464 } 465 466 /** 467 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey004 468 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_004 469 * @tc.desc: When the camera app is in the foreground and the screen is locked, 470 * the special repeat key check for the volume down button should return true. 471 */ 472 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey004, TestSize.Level1) 473 { 474 RepeatKey item; 475 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 476 item.ability.bundleName = "com.example.camera.camera"; 477 auto keyEvent = std::make_shared<KeyEvent>(); 478 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 479 480 EXPECT_CALL(*winMgr, JudgeCaramaInFore()).WillOnce(Return(true)); 481 EXPECT_CALL(*displayMonitor, GetScreenStatus()).WillOnce(Return("ON")); 482 EXPECT_CALL(*displayMonitor, GetScreenLocked()).WillOnce(Return(true)); 483 484 KeyCommandHandler handler; 485 EXPECT_TRUE(handler.CheckSpecialRepeatKey(item, keyEvent)); 486 } 487 488 /** 489 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey005 490 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_005 491 * @tc.desc: Verify that when the call state is active, 492 * the special repeat key check for the volume down button should return true. 493 */ 494 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey005, TestSize.Level1) 495 { 496 RepeatKey item; 497 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 498 item.ability.bundleName = "com.example.camera.camera"; 499 auto keyEvent = std::make_shared<KeyEvent>(); 500 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 501 502 EXPECT_CALL(*deviceMonitor, GetCallState()).WillOnce(Return(StateType::CALL_STATUS_ACTIVE)); 503 504 KeyCommandHandler handler; 505 EXPECT_TRUE(handler.CheckSpecialRepeatKey(item, keyEvent)); 506 } 507 508 /** 509 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey006 510 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_006 511 * @tc.desc: When testing screen lock with no music activated, 512 * the special repeat key check for the volume down button should return false. 513 */ 514 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey006, TestSize.Level1) 515 { 516 RepeatKey item; 517 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 518 item.ability.bundleName = "com.example.camera.camera"; 519 auto keyEvent = std::make_shared<KeyEvent>(); 520 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 521 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 522 523 EXPECT_CALL(*displayMonitor, GetScreenLocked()).WillOnce(Return(true)); 524 EXPECT_CALL(*deviceMonitor, IsMusicActivate()).WillOnce(Return(false)); 525 526 KeyCommandHandler handler; 527 EXPECT_FALSE(handler.CheckSpecialRepeatKey(item, keyEvent)); 528 } 529 530 /** 531 * @tc.name: KeyCommandHandlerTest_CheckSpecialRepeatKey007 532 * @tc.number: KeyCommandHandlerTest_CheckSpecialRepeatKey_007 533 * @tc.desc: Verify that when music is active and the screen is not locked, 534 * the special repeat key check for the volume down button should return true. 535 */ 536 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_CheckSpecialRepeatKey007, TestSize.Level1) 537 { 538 RepeatKey item; 539 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 540 item.ability.bundleName = "com.example.camera.camera"; 541 auto keyEvent = std::make_shared<KeyEvent>(); 542 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 543 544 EXPECT_CALL(*displayMonitor, GetScreenStatus()).WillOnce(Return("ON")); 545 EXPECT_CALL(*displayMonitor, GetScreenLocked()).WillOnce(Return(false)); 546 EXPECT_CALL(*deviceMonitor, IsMusicActivate()).WillOnce(Return(true)); 547 548 KeyCommandHandler handler; 549 EXPECT_TRUE(handler.CheckSpecialRepeatKey(item, keyEvent)); 550 } 551 552 /** 553 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility001 554 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_001 555 * @tc.desc: Test the ability to initiate repeated key presses, 556 * verifying the correct process for handling key events and initiating functionality. 557 */ 558 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility001, TestSize.Level1) 559 { 560 RepeatKey item; 561 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 562 item.ability.bundleName = "com.example.camera"; 563 auto keyEvent = std::make_shared<KeyEvent>(); 564 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 565 566 KeyCommandHandler handler; 567 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 568 .WillOnce(Return(subscriberHandlerMock_)); 569 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) 570 .Times(1); 571 EXPECT_CALL(handler, LaunchAbility(_)) 572 .Times(1); 573 EXPECT_CALL(handler, UnregisterMistouchPrevention()) 574 .Times(1); 575 576 handler.LaunchRepeatKeyAbility(item, keyEvent); 577 } 578 579 /** 580 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount001 581 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_001 582 * @tc.desc: To verify that false is returned when a null key event is transferred. 583 */ 584 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount001, TestSize.Level1) 585 { 586 RepeatKey item; 587 EXPECT_FALSE(handler_->HandleRepeatKeyCount(item, nullptr)); 588 } 589 590 /** 591 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount002 592 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_002 593 * @tc.desc: When the key code configured for the repeat key does not match 594 * the actual key event key code, the UP event handler should return false. 595 */ 596 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount002, TestSize.Level1) 597 { 598 RepeatKey item; 599 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 600 auto keyEvent = KeyEvent::Create(); 601 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 602 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 603 604 EXPECT_FALSE(handler_->HandleRepeatKeyCount(item, keyEvent)); 605 } 606 607 /** 608 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount003 609 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_003 610 * @tc.desc: Verify the time interval calculation logic and timer settings for the POWER key UP event. 611 */ 612 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount003, TestSize.Level1) 613 { 614 RepeatKey item; 615 item.keyCode = KeyEvent::KEYCODE_POWER; 616 auto keyEvent = KeyEvent::Create(); 617 keyEvent->SetKeyCode(KeyEvent::KEYCODE_POWER); 618 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 619 keyEvent->SetActionTime(200); 620 621 handler_->downActionTime_ = 100; 622 623 EXPECT_CALL(mockTimerMgr_, AddTimer(900, 1, _, "KeyCommandHandler-HandleRepeatKeyCount")) 624 .WillOnce(Return(VALID_TIMER_ID)); 625 626 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, keyEvent)); 627 EXPECT_EQ(handler_->upActionTime_, 200); 628 EXPECT_EQ(handler_->repeatKey_.keyCode, KeyEvent::KEYCODE_POWER); 629 EXPECT_EQ(handler_->repeatKey_.keyAction, KeyEvent::KEY_ACTION_UP); 630 EXPECT_EQ(handler_->repeatTimerId_, VALID_TIMER_ID); 631 } 632 633 /** 634 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount004 635 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_004 636 * @tc.desc: When there is a wallet delay, 637 * the POWER key UP event should use the wallet delay time to set the timer. 638 */ 639 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount004, TestSize.Level1) 640 { 641 RepeatKey item; 642 item.keyCode = KeyEvent::KEYCODE_POWER; 643 auto keyEvent = KeyEvent::Create(); 644 keyEvent->SetKeyCode(KeyEvent::KEYCODE_POWER); 645 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 646 647 handler_->walletLaunchDelayTimes_ = 500; 648 649 EXPECT_CALL(mockTimerMgr_, AddTimer(500, 1, _, _)) 650 .WillOnce(Return(VALID_TIMER_ID)); 651 652 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, keyEvent)); 653 } 654 655 /** 656 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount005 657 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_005 658 * @tc.desc: To verify that false is returned when the timer fails to be added. 659 */ 660 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount005, TestSize.Level1) 661 { 662 RepeatKey item; 663 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 664 auto keyEvent = KeyEvent::Create(); 665 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 666 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 667 668 EXPECT_CALL(mockTimerMgr_, AddTimer(_, 1, _, _)) 669 .WillOnce(Return(INVALID_TIMER_ID)); 670 671 EXPECT_FALSE(handler_->HandleRepeatKeyCount(item, keyEvent)); 672 } 673 674 /** 675 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount006 676 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_006 677 * @tc.desc: The DOWN event for the new button should correctly initialize the count and status. 678 */ 679 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount006, TestSize.Level1) 680 { 681 RepeatKey item; 682 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 683 auto keyEvent = KeyEvent::Create(); 684 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 685 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 686 keyEvent->SetActionTime(100); 687 688 handler_->repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 689 690 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, keyEvent)); 691 EXPECT_EQ(handler_->count_, 1); 692 EXPECT_EQ(handler_->repeatKey_.keyCode, KeyEvent::KEYCODE_VOLUME_UP); 693 EXPECT_EQ(handler_->repeatKey_.keyAction, KeyEvent::KEY_ACTION_DOWN); 694 EXPECT_TRUE(handler_->isDownStart_); 695 EXPECT_EQ(handler_->downActionTime_, 100); 696 } 697 698 /** 699 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount007 700 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_007 701 * @tc.desc: Verify that the count and state mapping should be reset when a repeated DOWN event occurs. 702 */ 703 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount007, TestSize.Level1) 704 { 705 RepeatKey item; 706 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 707 auto keyEvent = KeyEvent::Create(); 708 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 709 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 710 711 handler_->repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 712 handler_->repeatKey_.keyAction = KeyEvent::KEY_ACTION_DOWN; 713 handler_->count_ = 5; 714 handler_->isDownStart_ = true; 715 handler_->repeatKeyCountMap_[KeyEvent::KEYCODE_VOLUME_UP] = 3; 716 717 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, keyEvent)); 718 EXPECT_EQ(handler_->count_, 0); 719 EXPECT_FALSE(handler_->isDownStart_); 720 EXPECT_TRUE(handler_->repeatKeyCountMap_.empty()); 721 } 722 /** 723 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount008 724 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_008 725 * @tc.desc: The complete key sequence should correctly update the count and action status. 726 */ 727 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount008, TestSize.Level1) 728 { 729 RepeatKey item; 730 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 731 732 auto upEvent = KeyEvent::Create(); 733 upEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 734 upEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 735 upEvent->SetActionTime(UP_ACTION_TIME); 736 737 EXPECT_CALL(mockTimerMgr_, AddTimer(_, 1, _, _)) 738 .WillOnce(Return(VALID_TIMER_ID)); 739 740 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, upEvent)); 741 742 auto downEvent = KeyEvent::Create(); 743 downEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 744 downEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 745 downEvent->SetActionTime(150); 746 747 handler_->repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 748 handler_->repeatKey_.keyAction = KeyEvent::KEY_ACTION_UP; 749 handler_->count_ = 1; 750 751 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, downEvent)); 752 EXPECT_EQ(handler_->count_, 2); 753 EXPECT_EQ(handler_->repeatKey_.keyAction, KeyEvent::KEY_ACTION_DOWN); 754 EXPECT_TRUE(handler_->isDownStart_); 755 EXPECT_EQ(handler_->downActionTime_, 150); 756 } 757 758 /** 759 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount009 760 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_009 761 * @tc.desc: Verify that when the key press interval is less than the set value, 762 * the existing timer should be canceled and the state reset. 763 */ 764 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount009, TestSize.Level1) 765 { 766 RepeatKey item; 767 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 768 769 handler_->repeatTimerId_ = VALID_TIMER_ID; 770 771 auto keyEvent = KeyEvent::Create(); 772 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 773 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 774 keyEvent->SetActionTime(150); 775 776 handler_->upActionTime_ = UP_ACTION_TIME; 777 handler_->intervalTime_ = UP_ACTION_TIME; 778 779 EXPECT_CALL(mockTimerMgr_, RemoveTimer(VALID_TIMER_ID)); 780 781 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, keyEvent)); 782 EXPECT_EQ(handler_->repeatTimerId_, -1); 783 EXPECT_FALSE(handler_->isHandleSequence_); 784 } 785 786 /** 787 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount010 788 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_010 789 * @tc.desc: To test the UP event of the non-POWER key, 790 * set the timer using the default interval time. 791 */ 792 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount010, TestSize.Level1) 793 { 794 RepeatKey item; 795 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 796 auto keyEvent = KeyEvent::Create(); 797 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 798 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 799 800 EXPECT_CALL(mockTimerMgr_, AddTimer(handler_->intervalTime_, 1, _, _)) 801 .WillOnce(Return(VALID_TIMER_ID)); 802 803 EXPECT_TRUE(handler_->HandleRepeatKeyCount(item, keyEvent)); 804 } 805 806 /** 807 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility001 808 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_001 809 * @tc.desc: Verify VOLUME_DOWN with camera app when ret_ is not LIGHT_STAY_AWAY 810 */ 811 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility001, TestSize.Level1) 812 { 813 RepeatKey item; 814 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 815 item.ability.bundleName = "com.ohos.camera"; 816 auto keyEvent = std::make_shared<KeyEvent>(); 817 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 818 819 KeyCommandHandler handler; 820 handler.ret_.store(0); // Not LIGHT_STAY_AWAY 821 822 EXPECT_CALL(handler, LaunchAbility(_)).Times(1); 823 EXPECT_CALL(handler, UnregisterMistouchPrevention()).Times(1); 824 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 825 .WillOnce(Return(subscriberHandlerMock_)); 826 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0202(std::shared_ptr<KeyEvent> event) 827 .WillOnce([keyEvent](std::shared_ptr<KeyEvent> event) { 828 EXPECT_EQ(event->GetKeyAction(), KeyEvent::KEY_ACTION_CANCEL); 829 EXPECT_EQ(event->GetKeyCode(), keyEvent->GetKeyCode()); 830 }); 831 832 handler.LaunchRepeatKeyAbility(item, keyEvent); 833 EXPECT_TRUE(handler.repeatKeyCountMap_.empty()); 834 } 835 836 /** 837 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility002 838 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_002 839 * @tc.desc: Verify VOLUME_DOWN with camera app when ret_ is LIGHT_STAY_AWAY 840 */ 841 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility002, TestSize.Level1) 842 { 843 RepeatKey item; 844 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 845 item.ability.bundleName = "com.ohos.camera"; 846 auto keyEvent = std::make_shared<KeyEvent>(); 847 848 KeyCommandHandler handler; 849 handler.ret_.store(LIGHT_STAY_AWAY); 850 851 EXPECT_CALL(handler, LaunchAbility(_)).Times(0); 852 EXPECT_CALL(handler, UnregisterMistouchPrevention()).Times(1); 853 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 854 .WillOnce(Return(subscriberHandlerMock_)); 855 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)); 856 857 handler.LaunchRepeatKeyAbility(item, keyEvent); 858 } 859 860 /** 861 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility003 862 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_003 863 * @tc.desc: Verify non-VOLUME_DOWN key with camera app 864 */ 865 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility003, TestSize.Level1) 866 { 867 RepeatKey item; 868 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; // Non-volume down 869 item.ability.bundleName = "com.ohos.camera"; 870 auto keyEvent = std::make_shared<KeyEvent>(); 871 872 KeyCommandHandler handler; 873 handler.ret_.store(0); 874 875 EXPECT_CALL(handler, LaunchAbility(_)).Times(1); 876 EXPECT_CALL(handler, UnregisterMistouchPrevention()).Times(0); // Not called for non-volume 877 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 878 .WillOnce(Return(subscriberHandlerMock_)); 879 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)); 880 881 handler.LaunchRepeatKeyAbility(item, keyEvent); 882 } 883 884 /** 885 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility004 886 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_004 887 * @tc.desc: Verify VOLUME_DOWN with non-camera app 888 */ 889 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility004, TestSize.Level1) 890 { 891 RepeatKey item; 892 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 893 item.ability.bundleName = "com.ohos.music"; // Non-camera 894 auto keyEvent = std::make_shared<KeyEvent>(); 895 896 KeyCommandHandler handler; 897 handler.ret_.store(0); 898 899 EXPECT_CALL(handler, LaunchAbility(_)).Times(1); 900 EXPECT_CALL(handler, UnregisterMistouchPrevention()).Times(0); 901 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 902 .WillOnce(Return(subscriberHandlerMock_)); 903 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)); 904 905 handler.LaunchRepeatKeyAbility(item, keyEvent); 906 } 907 908 /** 909 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility005 910 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_005 911 * @tc.desc: Verify cancel event propagation 912 */ 913 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility005, TestSize.Level1) 914 { 915 RepeatKey item; 916 item.keyCode = KeyEvent::KEYCODE_POWER; 917 item.ability.bundleName = "com.ohos.settings"; 918 auto keyEvent = std::make_shared<KeyEvent>(); 919 keyEvent->SetKeyCode(KeyEvent::KEYCODE_POWER); 920 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 921 922 KeyCommandHandler handler; 923 924 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0302(std::shared_ptr<KeyEvent> event) 925 .WillOnce([](std::shared_ptr<KeyEvent> event) { 926 EXPECT_EQ(KeyEvent::KEY_ACTION_CANCEL, event->GetKeyAction()); 927 EXPECT_EQ(KeyEvent::KEYCODE_POWER, event->GetKeyCode()); 928 }); 929 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 930 .WillOnce(Return(subscriberHandlerMock_)); 931 932 handler.LaunchRepeatKeyAbility(item, keyEvent); 933 } 934 935 /** 936 * @tc.name: KeyCommandHandlerTest_LaunchRepeatKeyAbility006 937 * @tc.number: KeyCommandHandlerTest_LaunchRepeatKeyAbility_006 938 * @tc.desc: Verify map clearing 939 */ 940 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_LaunchRepeatKeyAbility006, TestSize.Level1) 941 { 942 RepeatKey item; 943 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 944 item.ability.bundleName = "com.ohos.music"; 945 auto keyEvent = std::make_shared<KeyEvent>(); 946 947 KeyCommandHandler handler; 948 handler.repeatKeyCountMap_[KeyEvent::KEYCODE_VOLUME_UP] = 3; 949 950 handler.LaunchRepeatKeyAbility(item, keyEvent); 951 EXPECT_TRUE(handler.repeatKeyCountMap_.empty()); 952 } 953 954 /** 955 * @tc.name: KeyCommandHandlerTest_SetIsFreezePowerKey001 956 * @tc.number: KeyCommandHandlerTest_SetIsFreezePowerKey_001 957 * @tc.desc: Verify that non-SOS page names disable power key freezing 958 */ 959 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SetIsFreezePowerKey001, TestSize.Level1) 960 { 961 KeyCommandHandler handler; 962 handler.isFreezePowerKey_ = true; // Initial state 963 964 EXPECT_EQ(handler.SetIsFreezePowerKey("HomePage"), RET_OK); 965 EXPECT_FALSE(handler.isFreezePowerKey_); 966 } 967 968 /** 969 * @tc.name: KeyCommandHandlerTest_SetIsFreezePowerKey002 970 * @tc.number: KeyCommandHandlerTest_SetIsFreezePowerKey_002 971 * @tc.desc: Verify SOS page name resets state and starts timer 972 */ 973 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SetIsFreezePowerKey002, TestSize.Level1) 974 { 975 KeyCommandHandler handler; 976 handler.count_ = 5; 977 handler.launchAbilityCount_ = 2; 978 handler.repeatKeyCountMap_ = {{1, 1}}; 979 handler.sosDelayTimerId_ = 100; // Existing timer 980 981 // Setup timer expectations 982 EXPECT_CALL(*timerMgrMock_, RemoveTimer(100)).Times(1); 983 EXPECT_CALL(*timerMgrMock_, AddTimer(SOS_COUNT_DOWN_TIMES/1000, 1, _, "KeyCommandHandler-SetIsFreezePowerKey")) 984 .WillOnce(Return(200)); // New timer ID 985 986 EXPECT_EQ(handler.SetIsFreezePowerKey("SosCountdown"), RET_OK); 987 EXPECT_TRUE(handler.isFreezePowerKey_); 988 EXPECT_EQ(handler.count_, 0); 989 EXPECT_EQ(handler.launchAbilityCount_, 0); 990 EXPECT_TRUE(handler.repeatKeyCountMap_.empty()); 991 EXPECT_GT(handler.sosLaunchTime_, 0); 992 EXPECT_EQ(handler.sosDelayTimerId_, 200); 993 } 994 995 /** 996 * @tc.name: KeyCommandHandlerTest_SetIsFreezePowerKey003 997 * @tc.number: KeyCommandHandlerTest_SetIsFreezePowerKey_003 998 * @tc.desc: Verify timer failure handling 999 */ 1000 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SetIsFreezePowerKey003, TestSize.Level1) 1001 { 1002 KeyCommandHandler handler; 1003 1004 EXPECT_CALL(*timerMgrMock_, AddTimer(_, _, _, _)) 1005 .WillOnce(Return(-1)); // Timer failure 1006 1007 EXPECT_EQ(handler.SetIsFreezePowerKey("SosCountdown"), RET_ERR); 1008 EXPECT_FALSE(handler.isFreezePowerKey_); 1009 } 1010 1011 /** 1012 * @tc.name: KeyCommandHandlerTest_SetIsFreezePowerKey004 1013 * @tc.number: KeyCommandHandlerTest_SetIsFreezePowerKey_004 1014 * @tc.desc: Verify no timer removal when no existing timer 1015 */ 1016 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SetIsFreezePowerKey004, TestSize.Level1) 1017 { 1018 KeyCommandHandler handler; 1019 handler.sosDelayTimerId_ = -1; // No existing timer 1020 1021 EXPECT_CALL(*timerMgrMock_, RemoveTimer(_)).Times(0); // No removal 1022 EXPECT_CALL(*timerMgrMock_, AddTimer(_, _, _, _)) 1023 .WillOnce(Return(300)); 1024 1025 EXPECT_EQ(handler.SetIsFreezePowerKey("SosCountdown"), RET_OK); 1026 } 1027 1028 /** 1029 * @tc.name: KeyCommandHandlerTest_SetIsFreezePowerKey005 1030 * @tc.number: KeyCommandHandlerTest_SetIsFreezePowerKey_005 1031 * @tc.desc: Verify timer callback resets freeze state 1032 */ 1033 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SetIsFreezePowerKey005, TestSize.Level1) 1034 { 1035 KeyCommandHandler handler; 1036 TimerCallback callback; 1037 1038 // Capture timer callback 1039 EXPECT_CALL(*timerMgrMock_, AddTimer(_, _, _, _)) 1040 .WillOnce(DoAll(SaveArg<2>(&callback), Return(400))); 1041 1042 handler.SetIsFreezePowerKey("SosCountdown"); 1043 ASSERT_TRUE(callback); 1044 1045 // Execute timer callback 1046 callback(); 1047 EXPECT_FALSE(handler.isFreezePowerKey_); 1048 } 1049 1050 /** 1051 * @tc.name: KeyCommandHandlerTest_SetIsFreezePowerKey006 1052 * @tc.number: KeyCommandHandlerTest_SetIsFreezePowerKey_006 1053 * @tc.desc: Verify mutex lock during execution 1054 */ 1055 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SetIsFreezePowerKey006, TestSize.Level1) 1056 { 1057 KeyCommandHandler handler; 1058 handler.mutex_.lock(); // Lock mutex to test contention 1059 __anon133dc6ce0402null1060 std::thread testThread([&] { 1061 EXPECT_EQ(handler.SetIsFreezePowerKey("SosCountdown"), RET_OK); 1062 }); 1063 1064 // Verify thread blocks on mutex 1065 auto status = testThread.join_for(std::chrono::milliseconds(100)); 1066 EXPECT_NE(status, std::future_status::ready); 1067 1068 handler.mutex_.unlock(); // Release lock 1069 testThread.join(); 1070 } 1071 1072 /** 1073 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount001 1074 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_001 1075 * @tc.desc: Verify UP event for non-POWER key sets timer with default interval 1076 */ 1077 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount001, TestSize.Level1) 1078 { 1079 RepeatKey item; 1080 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 1081 auto keyEvent = KeyEvent::Create(); 1082 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 1083 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 1084 keyEvent->SetActionTime(100); 1085 1086 KeyCommandHandler handler; 1087 handler.intervalTime_ = 500; 1088 1089 EXPECT_CALL(*timerMgrMock_, AddTimer(500, 1, _, "KeyCommandHandler-HandleRepeatKeyCount")) 1090 .WillOnce(Return(100)); 1091 1092 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1093 EXPECT_EQ(handler.upActionTime_, 100); 1094 EXPECT_EQ(handler.repeatKey_.keyCode, KeyEvent::KEYCODE_VOLUME_UP); 1095 EXPECT_EQ(handler.repeatKey_.keyAction, KeyEvent::KEY_ACTION_UP); 1096 EXPECT_EQ(handler.repeatTimerId_, 100); 1097 } 1098 1099 /** 1100 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount002 1101 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_002 1102 * @tc.desc: Verify POWER UP event calculates interval from key press duration 1103 */ 1104 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount002, TestSize.Level1) 1105 { 1106 RepeatKey item; 1107 item.keyCode = KeyEvent::KEYCODE_POWER; 1108 auto keyEvent = KeyEvent::Create(); 1109 keyEvent->SetKeyCode(KeyEvent::KEYCODE_POWER); 1110 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 1111 keyEvent->SetActionTime(300); 1112 1113 KeyCommandHandler handler; 1114 handler.intervalTime_ = 1000; 1115 handler.downActionTime_ = 100; // Press duration = 200ms 1116 1117 EXPECT_CALL(*timerMgrMock_, AddTimer(800, 1, _, _)) // 1000 - 200 = 800 1118 .WillOnce(Return(101)); 1119 1120 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1121 } 1122 1123 /** 1124 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount003 1125 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_003 1126 * @tc.desc: Verify wallet delay overrides POWER key interval calculation 1127 */ 1128 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount003, TestSize.Level1) 1129 { 1130 RepeatKey item; 1131 item.keyCode = KeyEvent::KEYCODE_POWER; 1132 auto keyEvent = KeyEvent::Create(); 1133 keyEvent->SetKeyCode(KeyEvent::KEYCODE_POWER); 1134 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 1135 1136 KeyCommandHandler handler; 1137 handler.walletLaunchDelayTimes_ = 300; 1138 1139 EXPECT_CALL(*timerMgrMock_, AddTimer(300, 1, _, _)) 1140 .WillOnce(Return(102)); 1141 1142 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1143 } 1144 1145 /** 1146 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount004 1147 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_004 1148 * @tc.desc: Verify timer failure returns false for UP event 1149 */ 1150 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount004, TestSize.Level1) 1151 { 1152 RepeatKey item; 1153 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 1154 auto keyEvent = KeyEvent::Create(); 1155 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 1156 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP); 1157 1158 EXPECT_CALL(*timerMgrMock_, AddTimer(_, _, _, _)) 1159 .WillOnce(Return(-1)); 1160 1161 KeyCommandHandler handler; 1162 EXPECT_FALSE(handler.HandleRepeatKeyCount(item, keyEvent)); 1163 } 1164 1165 /** 1166 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount005 1167 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_005 1168 * @tc.desc: Verify new key DOWN initializes state 1169 */ 1170 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount005, TestSize.Level1) 1171 { 1172 RepeatKey item; 1173 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 1174 auto keyEvent = KeyEvent::Create(); 1175 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 1176 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 1177 keyEvent->SetActionTime(200); 1178 1179 KeyCommandHandler handler; 1180 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_UP; // Different key 1181 1182 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1183 EXPECT_EQ(handler.count_, 1); 1184 EXPECT_EQ(handler.repeatKey_.keyCode, KeyEvent::KEYCODE_VOLUME_DOWN); 1185 EXPECT_EQ(handler.repeatKey_.keyAction, KeyEvent::KEY_ACTION_DOWN); 1186 EXPECT_TRUE(handler.isDownStart_); 1187 EXPECT_EQ(handler.downActionTime_, 200); 1188 } 1189 1190 /** 1191 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount006 1192 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_006 1193 * @tc.desc: Verify repeated DOWN resets state 1194 */ 1195 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount006, TestSize.Level1) 1196 { 1197 RepeatKey item; 1198 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 1199 auto keyEvent = KeyEvent::Create(); 1200 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 1201 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 1202 1203 KeyCommandHandler handler; 1204 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 1205 handler.repeatKey_.keyAction = KeyEvent::KEY_ACTION_DOWN; 1206 handler.count_ = 3; 1207 handler.isDownStart_ = true; 1208 handler.repeatKeyCountMap_[KeyEvent::KEYCODE_VOLUME_UP] = 2; 1209 1210 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1211 EXPECT_EQ(handler.count_, 0); 1212 EXPECT_FALSE(handler.isDownStart_); 1213 EXPECT_TRUE(handler.repeatKeyCountMap_.empty()); 1214 } 1215 1216 /** 1217 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount007 1218 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_007 1219 * @tc.desc: Verify DOWN after UP increments count 1220 */ 1221 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount007, TestSize.Level1) 1222 { 1223 RepeatKey item; 1224 item.keyCode = KeyEvent::KEYCODE_POWER; 1225 auto downEvent = KeyEvent::Create(); 1226 downEvent->SetKeyCode(KeyEvent::KEYCODE_POWER); 1227 downEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 1228 downEvent->SetActionTime(150); 1229 1230 KeyCommandHandler handler; 1231 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_POWER; 1232 handler.repeatKey_.keyAction = KeyEvent::KEY_ACTION_UP; // Previous was UP 1233 handler.count_ = 1; 1234 1235 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, downEvent)); 1236 EXPECT_EQ(handler.count_, 2); 1237 EXPECT_EQ(handler.repeatKey_.keyAction, KeyEvent::KEY_ACTION_DOWN); 1238 EXPECT_TRUE(handler.isDownStart_); 1239 EXPECT_EQ(handler.downActionTime_, 150); 1240 } 1241 1242 /** 1243 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount008 1244 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_008 1245 * @tc.desc: Verify early DOWN cancels existing timer 1246 */ 1247 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount008, TestSize.Level1) 1248 { 1249 RepeatKey item; 1250 item.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 1251 auto keyEvent = KeyEvent::Create(); 1252 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_UP); 1253 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 1254 keyEvent->SetActionTime(150); 1255 1256 KeyCommandHandler handler; 1257 handler.upActionTime_ = 100; 1258 handler.intervalTime_ = 500; // Time since up: 50ms < interval 1259 handler.repeatTimerId_ = 200; 1260 1261 EXPECT_CALL(*timerMgrMock_, RemoveTimer(200)).Times(1); 1262 1263 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1264 EXPECT_EQ(handler.repeatTimerId_, -1); 1265 EXPECT_FALSE(handler.isHandleSequence_); 1266 } 1267 1268 /** 1269 * @tc.name: KeyCommandHandlerTest_HandleRepeatKeyCount009 1270 * @tc.number: KeyCommandHandlerTest_HandleRepeatKeyCount_009 1271 * @tc.desc: Verify late DOWN doesn't cancel timer 1272 */ 1273 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleRepeatKeyCount009, TestSize.Level1) 1274 { 1275 RepeatKey item; 1276 item.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 1277 auto keyEvent = KeyEvent::Create(); 1278 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VOLUME_DOWN); 1279 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN); 1280 keyEvent->SetActionTime(700); 1281 1282 KeyCommandHandler handler; 1283 handler.upActionTime_ = 100; 1284 handler.intervalTime_ = 500; // Time since up: 600ms > interval 1285 handler.repeatTimerId_ = 201; 1286 1287 EXPECT_CALL(*timerMgrMock_, RemoveTimer(_)).Times(0); 1288 1289 EXPECT_TRUE(handler.HandleRepeatKeyCount(item, keyEvent)); 1290 } 1291 1292 /** 1293 * @tc.name: KeyCommandHandlerTest_SendKeyEvent001 1294 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_001 1295 * @tc.desc: Verify no action when isHandleSequence_ is true 1296 */ 1297 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent001, TestSize.Level1) 1298 { 1299 KeyCommandHandler handler; 1300 handler.isHandleSequence_ = true; 1301 handler.count_ = 5; 1302 handler.launchAbilityCount_ = 2; 1303 1304 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()).Times(0); 1305 1306 handler.SendKeyEvent(); 1307 EXPECT_EQ(handler.count_, 0); 1308 EXPECT_EQ(handler.launchAbilityCount_, 0); 1309 EXPECT_FALSE(handler.isDownStart_); 1310 EXPECT_FALSE(handler.isHandleSequence_); 1311 } 1312 1313 /** 1314 * @tc.name: KeyCommandHandlerTest_SendKeyEvent002 1315 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_002 1316 * @tc.desc: Verify special key handling with KEY_DOWN_ACTION 1317 */ 1318 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent002, TestSize.Level1) 1319 { 1320 KeyCommandHandler handler; 1321 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_BACK; 1322 handler.count_ = 3; 1323 handler.launchAbilityCount_ = 1; 1324 handler.repeatKeyMaxTimes_[KeyEvent::KEYCODE_BACK] = 5; 1325 1326 EXPECT_CALL(handler, IsSpecialType(KeyEvent::KEYCODE_BACK, SpecialType::KEY_DOWN_ACTION)) 1327 .WillOnce(Return(true)); 1328 EXPECT_CALL(handler, HandleSpecialKeys(KeyEvent::KEYCODE_BACK, KeyEvent::KEY_ACTION_UP)) 1329 .Times(2); 1330 1331 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) 1332 .Times(4); 1333 1334 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 1335 .WillRepeatedly(Return(subscriberHandlerMock_)); 1336 1337 handler.SendKeyEvent(); 1338 } 1339 1340 /** 1341 * @tc.name: KeyCommandHandlerTest_SendKeyEvent003 1342 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_003 1343 * @tc.desc: Verify POWER key cancel event at max count 1344 */ 1345 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent003, TestSize.Level1) 1346 { 1347 KeyCommandHandler handler; 1348 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_POWER; 1349 handler.count_ = 5; 1350 handler.launchAbilityCount_ = 4; 1351 handler.repeatKeyMaxTimes_[KeyEvent::KEYCODE_POWER] = 5; 1352 1353 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0502(std::shared_ptr<KeyEvent> event) 1354 .WillOnce([](std::shared_ptr<KeyEvent> event) { 1355 EXPECT_EQ(event->GetKeyCode(), KeyEvent::KEYCODE_POWER); 1356 EXPECT_EQ(event->GetKeyAction(), KeyEvent::KEY_ACTION_CANCEL); 1357 }); 1358 1359 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)).Times(1); 1360 1361 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 1362 .WillRepeatedly(Return(subscriberHandlerMock_)); 1363 1364 handler.SendKeyEvent(); 1365 } 1366 1367 /** 1368 * @tc.name: KeyCommandHandlerTest_SendKeyEvent004 1369 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_004 1370 * @tc.desc: Verify DOWN event skipping on first iteration 1371 */ 1372 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent004, TestSize.Level1) 1373 { 1374 KeyCommandHandler handler; 1375 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_UP; 1376 handler.count_ = 3; 1377 handler.launchAbilityCount_ = 0; 1378 1379 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) 1380 .Times(0); 1381 1382 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0602(std::shared_ptr<KeyEvent> event) 1383 .WillOnce([](std::shared_ptr<KeyEvent> event) { 1384 EXPECT_EQ(event->GetKeyAction(), KeyEvent::KEY_ACTION_UP); 1385 }); 1386 1387 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) 1388 .Times(2); 1389 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) 1390 .Times(2); 1391 1392 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 1393 .WillRepeatedly(Return(subscriberHandlerMock_)); 1394 1395 handler.SendKeyEvent(); 1396 } 1397 1398 /** 1399 * @tc.name: KeyCommandHandlerTest_SendKeyEvent005 1400 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_005 1401 * @tc.desc: Verify state reset after execution 1402 */ 1403 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent005, TestSize.Level1) 1404 { 1405 KeyCommandHandler handler; 1406 handler.count_ = 2; 1407 handler.launchAbilityCount_ = 1; 1408 handler.isDownStart_ = true; 1409 handler.isHandleSequence_ = false; 1410 handler.repeatKeyCountMap_[KeyEvent::KEYCODE_VOLUME_UP] = 3; 1411 1412 handler.SendKeyEvent(); 1413 1414 EXPECT_EQ(handler.count_, 0); 1415 EXPECT_EQ(handler.launchAbilityCount_, 0); 1416 EXPECT_FALSE(handler.isDownStart_); 1417 EXPECT_FALSE(handler.isHandleSequence_); 1418 EXPECT_TRUE(handler.repeatKeyCountMap_.empty()); 1419 } 1420 1421 /** 1422 * @tc.name: KeyCommandHandlerTest_SendKeyEvent006 1423 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_006 1424 * @tc.desc: Verify no events when count <= launchAbilityCount 1425 */ 1426 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent006, TestSize.Level1) 1427 { 1428 KeyCommandHandler handler; 1429 handler.count_ = 2; 1430 handler.launchAbilityCount_ = 3; 1431 1432 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()).Times(0); 1433 1434 handler.SendKeyEvent(); 1435 } 1436 1437 /** 1438 * @tc.name: KeyCommandHandlerTest_SendKeyEvent007 1439 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_007 1440 * @tc.desc: Verify normal DOWN/UP sequence for non-special keys 1441 */ 1442 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent007, TestSize.Level1) 1443 { 1444 KeyCommandHandler handler; 1445 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_ENTER; 1446 handler.count_ = 2; 1447 handler.launchAbilityCount_ = 0; 1448 1449 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0702(auto event) 1450 .WillOnce([](auto event) { EXPECT_EQ(KeyEvent::KEY_ACTION_UP, event->GetKeyAction()); }); 1451 1452 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0802(auto event) 1453 .WillOnce([](auto event) { EXPECT_EQ(KeyEvent::KEY_ACTION_DOWN, event->GetKeyAction()); }); 1454 EXPECT_CALL(*subscriberHandlerMock_, HandleKeyEvent(_)) __anon133dc6ce0902(auto event) 1455 .WillOnce([](auto event) { EXPECT_EQ(KeyEvent::KEY_ACTION_UP, event->GetKeyAction()); }); 1456 1457 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 1458 .WillRepeatedly(Return(subscriberHandlerMock_)); 1459 1460 handler.SendKeyEvent(); 1461 } 1462 1463 /** 1464 * @tc.name: KeyCommandHandlerTest_SendKeyEvent008 1465 * @tc.number: KeyCommandHandlerTest_SendKeyEvent_008 1466 * @tc.desc: Verify null subscriber handler safety 1467 */ 1468 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_SendKeyEvent008, TestSize.Level1) 1469 { 1470 KeyCommandHandler handler; 1471 handler.repeatKey_.keyCode = KeyEvent::KEYCODE_VOLUME_DOWN; 1472 handler.count_ = 2; 1473 handler.launchAbilityCount_ = 0; 1474 1475 EXPECT_CALL(*inputHandlerMock_, GetSubscriberHandler()) 1476 .WillOnce(Return(nullptr)); 1477 1478 handler.SendKeyEvent(); 1479 } 1480 1481 /** 1482 * @tc.name: KeyCommandHandlerTest_HandleShortKeys001 1483 * @tc.number: KeyCommandHandlerTest_HandleShortKeys_001 1484 * @tc.desc: Verify returns false when no shortcut keys configured 1485 */ 1486 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleShortKeys001, TestSize.Level1) 1487 { 1488 auto keyEvent = KeyEvent::Create(); 1489 KeyCommandHandler handler; 1490 handler.shortcutKeys_.clear(); 1491 1492 EXPECT_FALSE(handler.HandleShortKeys(keyEvent)); 1493 } 1494 1495 /** 1496 * @tc.name: KeyCommandHandlerTest_HandleShortKeys002 1497 * @tc.number: KeyCommandHandlerTest_HandleShortKeys_002 1498 * @tc.desc: Verify skips when same key is waiting timeout 1499 */ 1500 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleShortKeys002, TestSize.Level1) 1501 { 1502 auto keyEvent = KeyEvent::Create(); 1503 keyEvent->SetKeyCode(KeyEvent::KEYCODE_A); 1504 1505 KeyCommandHandler handler; 1506 handler.shortcutKeys_.push_back({}); 1507 handler.lastMatchedKey_.finalKey = KeyEvent::KEYCODE_A; 1508 handler.lastMatchedKey_.timerId = 100; 1509 1510 EXPECT_CALL(handler, IsKeyMatch(_, _)) 1511 .WillOnce(Return(true)); 1512 1513 EXPECT_TRUE(handler.HandleShortKeys(keyEvent)); 1514 } 1515 1516 /** 1517 * @tc.name: KeyCommandHandlerTest_HandleShortKeys003 1518 * @tc.number: KeyCommandHandlerTest_HandleShortKeys_003 1519 * @tc.desc: Verify camera blocks VCR2 key 1520 */ 1521 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleShortKeys003, TestSize.Level1) 1522 { 1523 auto keyEvent = KeyEvent::Create(); 1524 keyEvent->SetKeyCode(KeyEvent::KEYCODE_VCR2); 1525 1526 KeyCommandHandler handler; 1527 handler.shortcutKeys_.push_back({}); 1528 1529 // Camera in foreground 1530 EXPECT_CALL(*winMgrMock_, JudgeCaramaInFore()) 1531 .WillOnce(Return(true)); 1532 1533 EXPECT_FALSE(handler.HandleShortKeys(keyEvent)); 1534 } 1535 1536 /** 1537 * @tc.name: KeyCommandHandlerTest_HandleShortKeys004 1538 * @tc.number: KeyCommandHandlerTest_HandleShortKeys_004 1539 * @tc.desc: Verify skips when current ability key matches 1540 */ 1541 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleShortKeys004, TestSize.Level1) 1542 { 1543 auto keyEvent = KeyEvent::Create(); 1544 keyEvent->SetKeyCode(KeyEvent::KEYCODE_B); 1545 1546 KeyCommandHandler handler; 1547 handler.shortcutKeys_.push_back({}); 1548 handler.currentLaunchAbilityKey_.finalKey = KeyEvent::KEYCODE_B; 1549 handler.currentLaunchAbilityKey_.timerId = 200; 1550 1551 EXPECT_CALL(handler, IsKeyMatch(_, _)) 1552 .WillOnce(Return(true)); 1553 1554 EXPECT_TRUE(handler.HandleShortKeys(keyEvent)); 1555 } 1556 1557 /** 1558 * @tc.name: KeyCommandHandlerTest_HandleShortKeys005 1559 * @tc.number: KeyCommandHandlerTest_HandleShortKeys_005 1560 * @tc.desc: Verify removes pending timer before matching 1561 */ 1562 HWTEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleShortKeys005, TestSize.Level1) 1563 { 1564 auto keyEvent = KeyEvent::Create(); 1565 1566 KeyCommandHandler handler; 1567 handler.shortcutKeys_.push_back({}); 1568 handler.lastMatchedKey_.timerId = 300; 1569 1570 EXPECT_CALL(*timerMgrMock_, RemoveTimer(300)).Times(1); 1571 1572 EXPECT_CALL(handler, MatchShortcutKeys(_)) 1573 .WillOnce(Return(true)); 1574 1575 EXPECT_TRUE(handler.HandleShortKeys(keyEvent)); 1576 EXPECT_EQ(handler.lastMatchedKey_.timerId, -1); 1577 } 1578 1579 /** 1580 * @tc.name: KeyCommandHandlerTest_HandleShortKeys006 1581 * @tc.number: KeyCommandHandlerTest_HandleShortKeys_006 1582 * @tc.desc: Verify shortcut match returns true 1583 */ 1584 TEST_F(KeyCommandHandlerTest, KeyCommandHandlerTest_HandleShortKeys006, TestSize.Level1) 1585 { 1586 auto keyEvent = KeyEvent::Create(); 1587 1588 KeyCommandHandler handler; 1589 handler.shortcutKeys_.push_back({}); 1590 1591 EXPECT_CALL(handler, MatchShortcutKeys(_)) 1592 .WillOnce(Return(true)); 1593 1594 EXPECT_TRUE(handler.HandleShortKeys(keyEvent)); 1595 } 1596 #endif // OHOS_BUILD_ENABLE_MISTOUCH_PREVENTION 1597 } // namespace MMI 1598 } // namespace OHOS