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 <vector>
17
18 #include <gtest/gtest.h>
19
20 #include "key_shortcut_manager.h"
21 #include "mmi_log.h"
22
23 #undef MMI_LOG_DOMAIN
24 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "KeyShortcutManagerTest"
27
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 constexpr int32_t NO_LONG_PRESS { 0 };
32 constexpr int32_t DEFAULT_LONG_PRESS_TIME { 100 }; // 100ms
33 constexpr int32_t TWICE_LONG_PRESS_TIME { DEFAULT_LONG_PRESS_TIME + DEFAULT_LONG_PRESS_TIME };
34 constexpr int32_t BASE_SHORTCUT_ID { 1 };
35 constexpr int32_t DEFAULT_SAMPLING_PERIOD { 8 }; // 8ms
36 }
37 using namespace testing;
38 using namespace testing::ext;
39
40 class KeyShortcutManagerTest : public testing::Test {
41 public:
SetUpTestCase(void)42 static void SetUpTestCase(void) {}
TearDownTestCase(void)43 static void TearDownTestCase(void) {}
44
45 std::shared_ptr<KeyEvent> TriggerSystemKey01();
46 std::shared_ptr<KeyEvent> TriggerSystemKey02();
47 std::shared_ptr<KeyEvent> TriggerSystemKey03();
48 std::shared_ptr<KeyEvent> TriggerSystemKey04();
49 std::shared_ptr<KeyEvent> TriggerSystemKey05();
50 std::shared_ptr<KeyEvent> TriggerSystemKey06();
51 std::shared_ptr<KeyEvent> TriggerSystemKey07();
52 std::shared_ptr<KeyEvent> TriggerGlobalKey01();
53 std::shared_ptr<KeyEvent> TriggerGlobalKey0101();
54 std::shared_ptr<KeyEvent> ResetAllTriggering();
55 };
56
57 /**
58 * @tc.name: KeyShortcutManagerTest_SystemKey_01
59 * @tc.desc: We can register system shortcut key that consist of modifiers only.
60 * @tc.type: FUNC
61 * @tc.require:
62 */
63 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_01, TestSize.Level1)
64 {
65 CALL_TEST_DEBUG;
66 KeyShortcutManager::SystemShortcutKey sysKey {
67 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT },
68 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
69 };
70 KeyShortcutManager shortcutMgr;
71 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
72 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
73 }
74
75 /**
76 * @tc.name: KeyShortcutManagerTest_SystemKey_02
77 * @tc.desc: We can register system shortcut key that consist of modifiers and
78 * single non-modifier key, with the non-modifier key as final key.
79 * @tc.type: FUNC
80 * @tc.require:
81 */
82 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_02, TestSize.Level1)
83 {
84 CALL_TEST_DEBUG;
85 KeyShortcutManager::SystemShortcutKey sysKey {
86 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT },
87 .finalKey = KeyEvent::KEYCODE_S,
88 };
89 KeyShortcutManager shortcutMgr;
90 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
91 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
92 }
93
94 /**
95 * @tc.name: KeyShortcutManagerTest_SystemKey_03
96 * @tc.desc: We can register system shortcut key that consist of modifiers and
97 * single non-modifier key, with the non-modifier key as final key.
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_03, TestSize.Level1)
102 {
103 CALL_TEST_DEBUG;
104 KeyShortcutManager::SystemShortcutKey sysKey {
105 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
106 .finalKey = KeyEvent::KEYCODE_BACKSLASH,
107 };
108 KeyShortcutManager shortcutMgr;
109 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
110 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
111 }
112
113 /**
114 * @tc.name: KeyShortcutManagerTest_SystemKey_04
115 * @tc.desc: We can register system shortcut key that consist of modifiers and
116 * single non-modifier key, with the non-modifier key as final key.
117 * @tc.type: FUNC
118 * @tc.require:
119 */
120 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_04, TestSize.Level1)
121 {
122 CALL_TEST_DEBUG;
123 KeyShortcutManager::SystemShortcutKey sysKey {
124 .finalKey = KeyEvent::KEYCODE_A,
125 };
126 KeyShortcutManager shortcutMgr;
127 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
128 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
129 }
130
131 /**
132 * @tc.name: KeyShortcutManagerTest_SystemKey_05
133 * @tc.desc: Only 'LOGO' can be single-key shortcut.
134 * @tc.type: FUNC
135 * @tc.require:
136 */
137 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_05, TestSize.Level1)
138 {
139 CALL_TEST_DEBUG;
140 KeyShortcutManager::SystemShortcutKey sysKey {
141 .modifiers = { KeyEvent::KEYCODE_META_LEFT },
142 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
143 };
144 KeyShortcutManager shortcutMgr;
145 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
146 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
147 }
148
149 /**
150 * @tc.name: KeyShortcutManagerTest_SystemKey_06
151 * @tc.desc: Only 'LOGO' can be single-key shortcut.
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_06, TestSize.Level1)
156 {
157 CALL_TEST_DEBUG;
158 KeyShortcutManager::SystemShortcutKey sysKey {
159 .modifiers = { KeyEvent::KEYCODE_SHIFT_LEFT },
160 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
161 };
162 KeyShortcutManager shortcutMgr;
163 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
164 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
165 }
166
167 /**
168 * @tc.name: KeyShortcutManagerTest_SystemKey_07
169 * @tc.desc: Can not register reserved system key.
170 * @tc.type: FUNC
171 * @tc.require:
172 */
173 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_07, TestSize.Level1)
174 {
175 CALL_TEST_DEBUG;
176 KeyShortcutManager::SystemShortcutKey sysKey {
177 .modifiers = { KeyEvent::KEYCODE_META_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
178 .finalKey = KeyEvent::KEYCODE_S,
179 };
180 KeyShortcutManager shortcutMgr;
181 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
182 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
183 }
184
185 /**
186 * @tc.name: KeyShortcutManagerTest_SystemKey_08
187 * @tc.desc: System key support DOWN and UP trigger.
188 * @tc.type: FUNC
189 * @tc.require:
190 */
191 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_08, TestSize.Level1)
192 {
193 CALL_TEST_DEBUG;
194 KeyShortcutManager::SystemShortcutKey sysKey {
195 .modifiers = { KeyEvent::KEYCODE_META_LEFT },
196 .finalKey = KeyEvent::KEYCODE_D,
197 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP,
198 };
199 KeyShortcutManager shortcutMgr;
200 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
201 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
202 }
203
204 /**
205 * @tc.name: KeyShortcutManagerTest_SystemKey_09
206 * @tc.desc: System key support long press.
207 * @tc.type: FUNC
208 * @tc.require:
209 */
210 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_09, TestSize.Level1)
211 {
212 CALL_TEST_DEBUG;
213 KeyShortcutManager::SystemShortcutKey sysKey {
214 .modifiers = { KeyEvent::KEYCODE_META_LEFT },
215 .finalKey = KeyEvent::KEYCODE_D,
216 .longPressTime = DEFAULT_LONG_PRESS_TIME,
217 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP,
218 };
219 KeyShortcutManager shortcutMgr;
220 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
221 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
222 }
223
224 /**
225 * @tc.name: KeyShortcutManagerTest_GlobalKey_01
226 * @tc.desc: Global shortcut key that consist of modifiers and single non-modifier key,
227 * with the non-modifier key as final key.
228 * @tc.type: FUNC
229 * @tc.require:
230 */
231 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_01, TestSize.Level1)
232 {
233 CALL_TEST_DEBUG;
234 KeyShortcutManager::HotKey globalKey {
235 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
236 .finalKey = KeyEvent::KEYCODE_M,
237 };
238 KeyShortcutManager shortcutMgr;
239 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
240 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
241 }
242
243 /**
244 * @tc.name: KeyShortcutManagerTest_GlobalKey_02
245 * @tc.desc: Global shortcut key that consist of modifiers and single non-modifier key,
246 * with the non-modifier key as final key.
247 * @tc.type: FUNC
248 * @tc.require:
249 */
250 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_02, TestSize.Level1)
251 {
252 CALL_TEST_DEBUG;
253 KeyShortcutManager::HotKey globalKey {
254 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
255 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
256 };
257 KeyShortcutManager shortcutMgr;
258 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
259 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
260 }
261
262 /**
263 * @tc.name: KeyShortcutManagerTest_GlobalKey_03
264 * @tc.desc: Global shortcut key that consist of modifiers and single non-modifier key,
265 * with the non-modifier key as final key.
266 * @tc.type: FUNC
267 * @tc.require:
268 */
269 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_03, TestSize.Level1)
270 {
271 CALL_TEST_DEBUG;
272 KeyShortcutManager::HotKey globalKey {
273 .finalKey = KeyEvent::KEYCODE_M,
274 };
275 KeyShortcutManager shortcutMgr;
276 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
277 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
278 }
279
280 /**
281 * @tc.name: KeyShortcutManagerTest_GlobalKey_04
282 * @tc.desc: 'LOGO' can not be modifier of Global shortcut key.
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_04, TestSize.Level1)
287 {
288 CALL_TEST_DEBUG;
289 KeyShortcutManager::HotKey globalKey {
290 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_META_LEFT },
291 .finalKey = KeyEvent::KEYCODE_M,
292 };
293 KeyShortcutManager shortcutMgr;
294 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
295 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
296 }
297
298 /**
299 * @tc.name: KeyShortcutManagerTest_GlobalKey_05
300 * @tc.desc: We can not register registered system key as global shortcut key.
301 * @tc.type: FUNC
302 * @tc.require:
303 */
304 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_05, TestSize.Level1)
305 {
306 CALL_TEST_DEBUG;
307 KeyShortcutManager::SystemShortcutKey sysKey {
308 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
309 .finalKey = KeyEvent::KEYCODE_M,
310 };
311 KeyShortcutManager shortcutMgr;
312 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
313 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
314
315 KeyShortcutManager::HotKey globalKey {
316 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
317 .finalKey = KeyEvent::KEYCODE_M,
318 };
319 shortcutId = shortcutMgr.RegisterHotKey(globalKey);
320 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
321 }
322
323 /**
324 * @tc.name: KeyShortcutManagerTest_GlobalKey_06
325 * @tc.desc: We can not register reserved system key as global shortcut key.
326 * @tc.type: FUNC
327 * @tc.require:
328 */
329 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_06, TestSize.Level1)
330 {
331 CALL_TEST_DEBUG;
332 KeyShortcutManager::HotKey globalKey {
333 .modifiers = { KeyEvent::KEYCODE_META_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
334 .finalKey = KeyEvent::KEYCODE_S,
335 };
336 KeyShortcutManager shortcutMgr;
337 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
338 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
339 }
340
341 /**
342 * @tc.name: KeyShortcutManagerTest_GlobalKey_07
343 * @tc.desc: We can register a global shortcut key only once.
344 * @tc.type: FUNC
345 * @tc.require:
346 */
347 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_07, TestSize.Level1)
348 {
349 CALL_TEST_DEBUG;
350 KeyShortcutManager shortcutMgr;
351 KeyShortcutManager::HotKey globalKey {
352 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
353 .finalKey = KeyEvent::KEYCODE_M,
354 };
355 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
356 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
357 shortcutId = shortcutMgr.RegisterHotKey(globalKey);
358 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
359 }
360
TriggerSystemKey01()361 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey01()
362 {
363 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
364 CHKPP(keyEvent);
365 int64_t now = GetSysClockTime();
366 KeyEvent::KeyItem keyItem {};
367 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
368 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
369 keyItem.SetPressed(true);
370 keyEvent->AddKeyItem(keyItem);
371
372 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
373 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
374 keyEvent->AddKeyItem(keyItem);
375
376 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
377 keyItem.SetDownTime(now);
378 keyEvent->AddKeyItem(keyItem);
379
380 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
381 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
382 keyEvent->SetActionTime(now);
383 return keyEvent;
384 }
385
386 /**
387 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_01
388 * @tc.desc: Trigger system key immediately.
389 * @tc.type: FUNC
390 * @tc.require:
391 */
392 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_01, TestSize.Level1)
393 {
394 CALL_TEST_DEBUG;
395 bool triggered = false;
396 KeyShortcutManager::SystemShortcutKey sysKey {
397 .modifiers = { KeyEvent::KEYCODE_SHIFT_LEFT },
398 .finalKey = KeyEvent::KEYCODE_S,
399 .longPressTime = NO_LONG_PRESS,
400 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0202() 401 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
402 triggered = true;
403 },
404 };
405 KeyShortcutManager shortcutMgr;
406 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
407 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
408
409 auto keyEvent = TriggerSystemKey01();
410 ASSERT_TRUE(keyEvent != nullptr);
411 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
412 EXPECT_FALSE(triggered);
413 }
414
TriggerSystemKey02()415 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey02()
416 {
417 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
418 CHKPP(keyEvent);
419 int64_t now = GetSysClockTime();
420 KeyEvent::KeyItem keyItem {};
421 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
422 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
423 keyItem.SetPressed(true);
424 keyEvent->AddKeyItem(keyItem);
425
426 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
427 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
428 keyEvent->AddKeyItem(keyItem);
429
430 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
431 keyItem.SetDownTime(now);
432 keyEvent->AddKeyItem(keyItem);
433
434 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
435 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
436 keyEvent->SetActionTime(now);
437 return keyEvent;
438 }
439
440 /**
441 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_02
442 * @tc.desc: Trigger system key immediately.
443 * @tc.type: FUNC
444 * @tc.require:
445 */
446 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_02, TestSize.Level1)
447 {
448 CALL_TEST_DEBUG;
449 bool triggered = false;
450 KeyShortcutManager::SystemShortcutKey sysKey {
451 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
452 .finalKey = KeyEvent::KEYCODE_S,
453 .longPressTime = NO_LONG_PRESS,
454 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0302() 455 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
456 triggered = true;
457 },
458 };
459 KeyShortcutManager shortcutMgr;
460 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
461 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
462
463 auto keyEvent = TriggerSystemKey02();
464 ASSERT_TRUE(keyEvent != nullptr);
465 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
466 EXPECT_FALSE(triggered);
467 }
468
TriggerSystemKey03()469 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey03()
470 {
471 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
472 CHKPP(keyEvent);
473 int64_t now = GetSysClockTime();
474 KeyEvent::KeyItem keyItem {};
475 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
476 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
477 keyItem.SetPressed(true);
478 keyEvent->AddKeyItem(keyItem);
479
480 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
481 keyItem.SetDownTime(now);
482 keyEvent->AddKeyItem(keyItem);
483
484 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
485 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
486 keyEvent->SetActionTime(now);
487 return keyEvent;
488 }
489
490 /**
491 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_03
492 * @tc.desc: Long press system key.
493 * @tc.type: FUNC
494 * @tc.require:
495 */
496 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_03, TestSize.Level1)
497 {
498 CALL_TEST_DEBUG;
499 std::mutex mutex;
500 std::condition_variable condVar;
501 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
502 KeyShortcutManager::SystemShortcutKey sysKey {
503 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
504 .finalKey = KeyEvent::KEYCODE_S,
505 .longPressTime = DEFAULT_LONG_PRESS_TIME,
506 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0402() 507 .callback = [&](std::shared_ptr<KeyEvent> keyEvent) {
508 std::unique_lock<std::mutex> lock(mutex);
509 keyCode = keyEvent->GetKeyCode();
510 condVar.notify_all();
511 },
512 };
513 std::unique_lock<std::mutex> lock(mutex);
514 KeyShortcutManager shortcutMgr;
515 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
516 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
517
518 auto keyEvent = TriggerSystemKey03();
519 ASSERT_TRUE(keyEvent != nullptr);
520 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
521 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
522 bool cvRet = condVar.wait_for(lock, std::chrono::milliseconds(TWICE_LONG_PRESS_TIME),
__anon52785f1e0502() 523 [&keyCode]() {
524 return (keyCode != KeyEvent::KEYCODE_UNKNOWN);
525 });
526 EXPECT_FALSE(cvRet);
527 EXPECT_NE(keyCode, KeyEvent::KEYCODE_S);
528 }
529
TriggerSystemKey04()530 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey04()
531 {
532 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
533 CHKPP(keyEvent);
534 int64_t now = GetSysClockTime();
535 KeyEvent::KeyItem keyItem {};
536 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
537 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
538 keyItem.SetPressed(true);
539 keyEvent->AddKeyItem(keyItem);
540
541 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
542 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
543 keyEvent->AddKeyItem(keyItem);
544
545 keyItem.SetKeyCode(KeyEvent::KEYCODE_A);
546 keyItem.SetDownTime(now);
547 keyEvent->AddKeyItem(keyItem);
548
549 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
550 keyEvent->SetKeyCode(KeyEvent::KEYCODE_A);
551 keyEvent->SetActionTime(now);
552 return keyEvent;
553 }
554
555 /**
556 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_04
557 * @tc.desc: Reset pending shortcut when press down another key.
558 * @tc.type: FUNC
559 * @tc.require:
560 */
561 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_04, TestSize.Level1)
562 {
563 CALL_TEST_DEBUG;
564 std::mutex mutex;
565 std::condition_variable condVar;
566 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
567 KeyShortcutManager::SystemShortcutKey sysKey {
568 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
569 .finalKey = KeyEvent::KEYCODE_S,
570 .longPressTime = DEFAULT_LONG_PRESS_TIME,
571 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0602() 572 .callback = [&](std::shared_ptr<KeyEvent> keyEvent) {
573 std::unique_lock<std::mutex> lock(mutex);
574 keyCode = keyEvent->GetKeyCode();
575 condVar.notify_all();
576 },
577 };
578 std::unique_lock<std::mutex> lock(mutex);
579 KeyShortcutManager shortcutMgr;
580 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
581 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
582
583 auto keyEvent = TriggerSystemKey03();
584 ASSERT_TRUE(keyEvent != nullptr);
585 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
586 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
587
588 keyEvent = TriggerSystemKey04();
589 ASSERT_TRUE(keyEvent != nullptr);
590 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
591 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
592
593 bool cvRet = condVar.wait_for(lock, std::chrono::milliseconds(TWICE_LONG_PRESS_TIME),
__anon52785f1e0702() 594 [&keyCode]() {
595 return (keyCode != KeyEvent::KEYCODE_UNKNOWN);
596 });
597 EXPECT_FALSE(cvRet);
598 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
599 }
600
TriggerSystemKey05()601 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey05()
602 {
603 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
604 CHKPP(keyEvent);
605 int64_t now = GetSysClockTime();
606 KeyEvent::KeyItem keyItem {};
607 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
608 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
609 keyItem.SetPressed(false);
610 keyEvent->AddKeyItem(keyItem);
611
612 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
613 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
614 keyItem.SetPressed(true);
615 keyEvent->AddKeyItem(keyItem);
616
617 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
618 keyEvent->SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
619 keyEvent->SetActionTime(now);
620 return keyEvent;
621 }
622
623 /**
624 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_05
625 * @tc.desc: Reset pending shortcut when lift up dedicated key(s) before running shortcut.
626 * @tc.type: FUNC
627 * @tc.require:
628 */
629 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_05, TestSize.Level1)
630 {
631 CALL_TEST_DEBUG;
632 std::mutex mutex;
633 std::condition_variable condVar;
634 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
635 KeyShortcutManager::SystemShortcutKey sysKey {
636 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
637 .finalKey = KeyEvent::KEYCODE_S,
638 .longPressTime = DEFAULT_LONG_PRESS_TIME,
639 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0802() 640 .callback = [&](std::shared_ptr<KeyEvent> keyEvent) {
641 std::unique_lock<std::mutex> lock(mutex);
642 keyCode = keyEvent->GetKeyCode();
643 condVar.notify_all();
644 },
645 };
646 std::unique_lock<std::mutex> lock(mutex);
647 KeyShortcutManager shortcutMgr;
648 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
649 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
650
651 auto keyEvent = TriggerSystemKey03();
652 ASSERT_TRUE(keyEvent != nullptr);
653 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
654 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
655
656 keyEvent = TriggerSystemKey05();
657 ASSERT_TRUE(keyEvent != nullptr);
658 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
659 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
660
661 bool cvRet = condVar.wait_for(lock, std::chrono::milliseconds(TWICE_LONG_PRESS_TIME),
__anon52785f1e0902() 662 [&keyCode]() {
663 return (keyCode != KeyEvent::KEYCODE_UNKNOWN);
664 });
665 EXPECT_FALSE(cvRet);
666 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
667 }
668
TriggerSystemKey06()669 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey06()
670 {
671 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
672 CHKPP(keyEvent);
673 int64_t now = GetSysClockTime();
674 KeyEvent::KeyItem keyItem {};
675 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
676 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_LONG_PRESS_TIME));
677 keyItem.SetPressed(true);
678 keyEvent->AddKeyItem(keyItem);
679
680 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
681 keyItem.SetDownTime(now - MS2US(DEFAULT_LONG_PRESS_TIME));
682 keyItem.SetPressed(false);
683 keyEvent->AddKeyItem(keyItem);
684
685 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
686 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
687 keyEvent->SetActionTime(now);
688 return keyEvent;
689 }
690
691 /**
692 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_06
693 * @tc.desc: Trigger key-up-trigger system key.
694 * @tc.type: FUNC
695 * @tc.require:
696 */
697 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_06, TestSize.Level1)
698 {
699 CALL_TEST_DEBUG;
700 bool triggered = false;
701 KeyShortcutManager::SystemShortcutKey sysKey {
702 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
703 .finalKey = KeyEvent::KEYCODE_S,
704 .longPressTime = DEFAULT_LONG_PRESS_TIME,
705 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP,
__anon52785f1e0a02() 706 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
707 triggered = true;
708 },
709 };
710 KeyShortcutManager shortcutMgr;
711 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
712 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
713
714 auto keyEvent = TriggerSystemKey06();
715 ASSERT_TRUE(keyEvent != nullptr);
716 ASSERT_FALSE(shortcutMgr.HandleEvent(keyEvent));
717 ASSERT_FALSE(triggered);
718 }
719
TriggerSystemKey07()720 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey07()
721 {
722 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
723 CHKPP(keyEvent);
724 int64_t now = GetSysClockTime();
725 KeyEvent::KeyItem keyItem {};
726 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
727 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
728 keyItem.SetPressed(true);
729 keyEvent->AddKeyItem(keyItem);
730
731 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_RIGHT);
732 keyItem.SetDownTime(now);
733 keyEvent->AddKeyItem(keyItem);
734
735 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
736 keyEvent->SetKeyCode(KeyEvent::KEYCODE_SHIFT_RIGHT);
737 keyEvent->SetActionTime(now);
738 return keyEvent;
739 }
740
741 /**
742 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_07
743 * @tc.desc: Trigger pure-modifiers system key.
744 * @tc.type: FUNC
745 * @tc.require:
746 */
747 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_07, TestSize.Level1)
748 {
749 CALL_TEST_DEBUG;
750 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
751 KeyShortcutManager::SystemShortcutKey sysKey {
752 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
753 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
754 .longPressTime = NO_LONG_PRESS,
755 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0b02() 756 .callback = [&keyCode](std::shared_ptr<KeyEvent> keyEvent) {
757 keyCode = keyEvent->GetKeyCode();
758 },
759 };
760 KeyShortcutManager shortcutMgr;
761 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
762 ASSERT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
763
764 auto keyEvent = TriggerSystemKey07();
765 ASSERT_TRUE(keyEvent != nullptr);
766 EXPECT_TRUE(shortcutMgr.HandleEvent(keyEvent));
767 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_SHIFT_RIGHT);
768 }
769
TriggerGlobalKey01()770 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerGlobalKey01()
771 {
772 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
773 CHKPP(keyEvent);
774 int64_t now = GetSysClockTime();
775 KeyEvent::KeyItem keyItem {};
776 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
777 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
778 keyItem.SetPressed(true);
779 keyEvent->AddKeyItem(keyItem);
780
781 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
782 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
783 keyEvent->AddKeyItem(keyItem);
784
785 keyItem.SetKeyCode(KeyEvent::KEYCODE_A);
786 keyItem.SetDownTime(now);
787 keyEvent->AddKeyItem(keyItem);
788
789 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
790 keyEvent->SetKeyCode(KeyEvent::KEYCODE_A);
791 keyEvent->SetActionTime(now);
792 return keyEvent;
793 }
794
TriggerGlobalKey0101()795 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerGlobalKey0101()
796 {
797 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
798 CHKPP(keyEvent);
799 int64_t now = GetSysClockTime();
800 KeyEvent::KeyItem keyItem {};
801 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
802 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
803 keyItem.SetPressed(true);
804 keyEvent->AddKeyItem(keyItem);
805
806 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
807 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
808 keyEvent->AddKeyItem(keyItem);
809
810 keyItem.SetKeyCode(KeyEvent::KEYCODE_A);
811 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
812 keyEvent->AddKeyItem(keyItem);
813
814 keyItem.SetKeyCode(KeyEvent::KEYCODE_D);
815 keyItem.SetDownTime(now);
816 keyEvent->AddKeyItem(keyItem);
817
818 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
819 keyEvent->SetKeyCode(KeyEvent::KEYCODE_D);
820 keyEvent->SetActionTime(now);
821 return keyEvent;
822 }
823
824 /**
825 * @tc.name: KeyShortcutManagerTest_TriggerGlobalKey_01
826 * @tc.desc: Trigger key-up-trigger system key.
827 * @tc.type: FUNC
828 * @tc.require:
829 */
830 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerGlobalKey_01, TestSize.Level1)
831 {
832 CALL_TEST_DEBUG;
833 int32_t keyCode1 = KeyEvent::KEYCODE_UNKNOWN;
834 KeyShortcutManager::HotKey globalKey1 {
835 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
836 .finalKey = KeyEvent::KEYCODE_A,
__anon52785f1e0c02() 837 .callback = [&keyCode1](std::shared_ptr<KeyEvent> keyEvent) {
838 keyCode1 = keyEvent->GetKeyCode();
839 },
840 };
841 KeyShortcutManager shortcutMgr;
842 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey1);
843 ASSERT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
844
845 int32_t keyCode2 = KeyEvent::KEYCODE_UNKNOWN;
846 KeyShortcutManager::HotKey globalKey2 {
847 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT },
848 .finalKey = KeyEvent::KEYCODE_D,
__anon52785f1e0d02() 849 .callback = [&keyCode2](std::shared_ptr<KeyEvent> keyEvent) {
850 keyCode2 = keyEvent->GetKeyCode();
851 },
852 };
853 shortcutId = shortcutMgr.RegisterHotKey(globalKey2);
854 ASSERT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
855
856 auto keyEvent1 = TriggerGlobalKey01();
857 ASSERT_TRUE(keyEvent1 != nullptr);
858 EXPECT_TRUE(shortcutMgr.HandleEvent(keyEvent1));
859 EXPECT_EQ(keyCode1, KeyEvent::KEYCODE_A);
860 EXPECT_EQ(keyCode2, KeyEvent::KEYCODE_UNKNOWN);
861
862 auto keyEvent2 = TriggerGlobalKey0101();
863 ASSERT_TRUE(keyEvent2 != nullptr);
864 EXPECT_TRUE(shortcutMgr.HandleEvent(keyEvent2));
865 EXPECT_EQ(keyCode2, KeyEvent::KEYCODE_D);
866 }
867
ResetAllTriggering()868 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::ResetAllTriggering()
869 {
870 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
871 CHKPP(keyEvent);
872 int64_t now = GetSysClockTime();
873 KeyEvent::KeyItem keyItem {};
874 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
875 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
876 keyItem.SetPressed(true);
877 keyEvent->AddKeyItem(keyItem);
878
879 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
880 keyItem.SetDownTime(now);
881 keyEvent->AddKeyItem(keyItem);
882
883 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
884 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
885 keyEvent->SetActionTime(now);
886 return keyEvent;
887 }
888
889 /**
890 * @tc.name: KeyShortcutManagerTest_ResetAllTriggering_01
891 * @tc.desc: Trigger key-up-trigger system key.
892 * @tc.type: FUNC
893 * @tc.require:
894 */
895 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ResetAllTriggering_01, TestSize.Level1)
896 {
897 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
898 KeyShortcutManager::SystemShortcutKey sysKey {
899 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
900 .finalKey = KeyEvent::KEYCODE_S,
901 .longPressTime = DEFAULT_LONG_PRESS_TIME,
902 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0e02() 903 .callback = [&keyCode](std::shared_ptr<KeyEvent> keyEvent) {
904 keyCode = keyEvent->GetKeyCode();
905 },
906 };
907 KeyShortcutManager shortcutMgr;
908 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
909 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
910
911 auto keyEvent = ResetAllTriggering();
912 ASSERT_TRUE(keyEvent != nullptr);
913 ASSERT_FALSE(shortcutMgr.HandleEvent(keyEvent));
914 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
915 shortcutMgr.ResetAll();
916 std::this_thread::sleep_for(std::chrono::milliseconds(TWICE_LONG_PRESS_TIME));
917 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
918 }
919
920 /**
921 * @tc.name: KeyShortcutManagerTest_GetInstance_01
922 * @tc.desc: Test the funcation GetInstance
923 * @tc.type: FUNC
924 * @tc.require:
925 */
926 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GetInstance_01, TestSize.Level1)
927 {
928 CALL_TEST_DEBUG;
929 KeyShortcutManager shortcutMgr;
930 shortcutMgr.instance_ = nullptr;
931 EXPECT_NO_FATAL_FAILURE(shortcutMgr.GetInstance());
932 shortcutMgr.instance_ = std::make_shared<KeyShortcutManager>();
933 ASSERT_TRUE(shortcutMgr.instance_ != nullptr);
934 EXPECT_NO_FATAL_FAILURE(shortcutMgr.GetInstance());
935 }
936
937 /**
938 * @tc.name: KeyShortcutManagerTest_UnregisterHotKey_01
939 * @tc.desc: Test the funcation UnregisterHotKey
940 * @tc.type: FUNC
941 * @tc.require:
942 */
943 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UnregisterHotKey_01, TestSize.Level1)
944 {
945 CALL_TEST_DEBUG;
946 bool triggered = false;
947 KeyShortcutManager::KeyShortcut keyShortcut {
948 .modifiers = KeyEvent::KEYCODE_META_LEFT,
949 .finalKey = KeyEvent::KEYCODE_T,
950 .longPressTime = DEFAULT_LONG_PRESS_TIME,
951 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
__anon52785f1e0f02() 952 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
953 triggered = true;
954 },
955 };
956 KeyShortcutManager shortcutMgr;
957 int32_t shortcutId = 100;
958 shortcutMgr.shortcuts_[100] = keyShortcut;
959 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterHotKey(shortcutId));
960 shortcutId = 66;
961 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterHotKey(shortcutId));
962 }
963
964 /**
965 * @tc.name: KeyShortcutManagerTest_UpdateShortcutConsumed_01
966 * @tc.desc: Test the funcation UpdateShortcutConsumed
967 * @tc.type: FUNC
968 * @tc.require:
969 */
970 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UpdateShortcutConsumed_01, TestSize.Level1)
971 {
972 CALL_TEST_DEBUG;
973 KeyShortcutManager shortcutMgr;
974 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
975 ASSERT_NE(keyEvent, nullptr);
976 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
977 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UpdateShortcutConsumed(keyEvent));
978 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
979 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UpdateShortcutConsumed(keyEvent));
980 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UNKNOWN);
981 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UpdateShortcutConsumed(keyEvent));
982 }
983
984 /**
985 * @tc.name: KeyShortcutManagerTest_MarkShortcutConsumed_01
986 * @tc.desc: Test the funcation MarkShortcutConsumed(ShortcutKey)
987 * @tc.type: FUNC
988 * @tc.require:
989 */
990 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_MarkShortcutConsumed_01, TestSize.Level1)
991 {
992 CALL_TEST_DEBUG;
993 KeyShortcutManager shortcutMgr;
994 ShortcutKey shortcut;
995 shortcut.preKeys = {1, 2, 3};
996 shortcut.businessId = "businessId";
997 shortcut.statusConfig = "statusConfig";
998 shortcut.statusConfigValue = true;
999 shortcut.finalKey = 1;
1000 shortcut.keyDownDuration = 2;
1001 shortcut.triggerType = KeyEvent::KEY_ACTION_DOWN;
1002 shortcut.timerId = 1;
1003 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1004 shortcut.triggerType = KeyEvent::KEY_ACTION_CANCEL;
1005 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1006 shortcut.triggerType = KeyEvent::KEY_ACTION_UP;
1007 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1008 }
1009
1010 /**
1011 * @tc.name: KeyShortcutManagerTest_MarkShortcutConsumed_001
1012 * @tc.desc: Test the funcation MarkShortcutConsumed(KeyOption)
1013 * @tc.type: FUNC
1014 * @tc.require:
1015 */
1016 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_MarkShortcutConsumed_001, TestSize.Level1)
1017 {
1018 CALL_TEST_DEBUG;
1019 KeyShortcutManager shortcutMgr;
1020 KeyOption shortcut;
1021 std::set<int32_t> preKeys = {1, 2, 3, 4, 5};
1022 shortcut.SetPreKeys(preKeys);
1023 shortcut.SetFinalKeyDown(true);
1024 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1025 shortcut.SetFinalKeyDown(false);
1026 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1027 }
1028
1029 /**
1030 * @tc.name: KeyShortcutManagerTest_ResetTriggering_01
1031 * @tc.desc: Test the funcation ResetTriggering
1032 * @tc.type: FUNC
1033 * @tc.require:
1034 */
1035 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ResetTriggering_01, TestSize.Level1)
1036 {
1037 CALL_TEST_DEBUG;
1038 KeyShortcutManager shortcutMgr;
1039 int32_t shortcutId = 1;
1040 shortcutMgr.triggering_[1] = 1;
1041 EXPECT_NO_FATAL_FAILURE(shortcutMgr.ResetTriggering(shortcutId));
1042 shortcutId = 10;
1043 EXPECT_NO_FATAL_FAILURE(shortcutMgr.ResetTriggering(shortcutId));
1044 }
1045
1046 /**
1047 * @tc.name: KeyShortcutManagerTest_HandleEvent_001
1048 * @tc.desc: Test the funcation HandleEvent
1049 * @tc.type: FUNC
1050 * @tc.require:
1051 */
1052 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleEvent_001, TestSize.Level1)
1053 {
1054 CALL_TEST_DEBUG;
1055 KeyShortcutManager shortcutMgr;
1056 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1057 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_CANCEL);
1058 bool ret = shortcutMgr.HandleEvent(keyEvent);
1059 ASSERT_EQ(ret, false);
1060 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UNKNOWN);
1061 ret = shortcutMgr.HandleEvent(keyEvent);
1062 ASSERT_EQ(ret, false);
1063 }
1064
1065 /**
1066 * @tc.name: KeyShortcutManagerTest_WillResetOnKeyDown_001
1067 * @tc.desc: Test the funcation WillResetOnKeyDown
1068 * @tc.type: FUNC
1069 * @tc.require:
1070 */
1071 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_WillResetOnKeyDown_001, TestSize.Level1)
1072 {
1073 CALL_TEST_DEBUG;
1074 KeyShortcutManager shortcutMgr;
1075 int32_t keyCode = 1;
1076 KeyShortcutManager::KeyShortcut shortcut;
1077 shortcut.finalKey = 1;
1078 bool ret = shortcutMgr.WillResetOnKeyDown(keyCode, shortcut);
1079 ASSERT_EQ(ret, false);
1080 keyCode = 3;
1081 ret = shortcutMgr.WillResetOnKeyDown(keyCode, shortcut);
1082 ASSERT_EQ(ret, true);
1083 }
1084
1085 /**
1086 * @tc.name: KeyShortcutManagerTest_WillResetOnKeyUp_001
1087 * @tc.desc: Test the funcation WillResetOnKeyUp
1088 * @tc.type: FUNC
1089 * @tc.require:
1090 */
1091 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_WillResetOnKeyUp_001, TestSize.Level1)
1092 {
1093 CALL_TEST_DEBUG;
1094 KeyShortcutManager shortcutMgr;
1095 int32_t keyCode = 1;
1096 KeyShortcutManager::KeyShortcut shortcut;
1097 shortcut.finalKey = 1;
1098 bool ret = shortcutMgr.WillResetOnKeyUp(keyCode, shortcut);
1099 ASSERT_EQ(ret, true);
1100 keyCode = 3;
1101 ret = shortcutMgr.WillResetOnKeyUp(keyCode, shortcut);
1102 ASSERT_EQ(ret, false);
1103 }
1104
myCallback(std::shared_ptr<KeyEvent> event)1105 void myCallback(std::shared_ptr<KeyEvent> event)
1106 {
1107 std::cout << "Callback triggered!" << std::endl;
1108 }
1109
1110 /**
1111 * @tc.name: KeyShortcutManagerTest_TriggerUp_001
1112 * @tc.desc: Test the funcation TriggerUp
1113 * @tc.type: FUNC
1114 * @tc.require:
1115 */
1116 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_001, TestSize.Level1)
1117 {
1118 CALL_TEST_DEBUG;
1119 KeyShortcutManager shortcutMgr;
1120 int32_t shortcutId = 1;
1121 KeyShortcutManager::KeyShortcut shortcut;
1122 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1123 shortcut.longPressTime = -1;
1124 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1125 shortcut.callback = myCallback;
1126 shortcut.callback(keyEvent);
1127 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1128 }
1129
1130 /**
1131 * @tc.name: KeyShortcutManagerTest_TriggerUp_002
1132 * @tc.desc: Test the funcation TriggerUp
1133 * @tc.type: FUNC
1134 * @tc.require:
1135 */
1136 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_002, TestSize.Level1)
1137 {
1138 CALL_TEST_DEBUG;
1139 KeyShortcutManager shortcutMgr;
1140 int32_t shortcutId = 1;
1141 KeyShortcutManager::KeyShortcut shortcut;
1142 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1143 shortcut.longPressTime = 1000;
1144 KeyEvent::KeyItem item;
1145 item.SetKeyCode(-1);
1146 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1147 item.SetKeyCode(5);
1148 keyEvent->SetActionTime(3);
1149 item.SetDownTime(1);
1150 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1151 }
1152
1153 /**
1154 * @tc.name: KeyShortcutManagerTest_RunShortcut_001
1155 * @tc.desc: Test the funcation RunShortcut
1156 * @tc.type: FUNC
1157 * @tc.require:
1158 */
1159 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_001, TestSize.Level1)
1160 {
1161 CALL_TEST_DEBUG;
1162 KeyShortcutManager shortcutMgr;
1163 int32_t shortcutId = 1;
1164 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1165 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1166 }
1167
1168 /**
1169 * @tc.name: KeyShortcutManagerTest_RunShortcut_002
1170 * @tc.desc: Test the funcation RunShortcut
1171 * @tc.type: FUNC
1172 * @tc.require:
1173 */
1174 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_002, TestSize.Level1)
1175 {
1176 CALL_TEST_DEBUG;
1177 KeyShortcutManager shortcutMgr;
1178 int32_t shortcutId = 1;
1179 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1180 KeyShortcutManager::KeyShortcut key1;
1181 shortcutMgr.shortcuts_[1] = key1;
1182 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1183 }
1184
1185 /**
1186 * @tc.name: KeyShortcutManagerTest_TriggerDown_002
1187 * @tc.desc: Test the funcation TriggerDown
1188 * @tc.type: FUNC
1189 * @tc.require:
1190 */
1191 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerDown_002, TestSize.Level1)
1192 {
1193 CALL_TEST_DEBUG;
1194 KeyShortcutManager shortcutMgr;
1195 int32_t shortcutId = 1;
1196 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1197 KeyShortcutManager::KeyShortcut shortcut;
1198 shortcut.longPressTime = -1;
1199 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1200 shortcut.callback = myCallback;
1201 shortcut.callback(keyEvent);
1202 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1203 shortcut.longPressTime = 2;
1204 shortcutMgr.triggering_[1] = 100;
1205 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1206 }
1207
1208 /**
1209 * @tc.name: KeyShortcutManagerTest_HandleKeyUp_001
1210 * @tc.desc: Test the funcation HandleKeyUp
1211 * @tc.type: FUNC
1212 * @tc.require:
1213 */
1214 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_001, TestSize.Level1)
1215 {
1216 CALL_TEST_DEBUG;
1217 KeyShortcutManager shortcutMgr;
1218 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1219 KeyShortcutManager::KeyShortcut shortcut;
1220 shortcut.modifiers = 0x1;
1221 shortcut.finalKey = 0x2;
1222 shortcut.longPressTime = 500;
1223 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_DOWN;
1224 shortcut.session = 1;
1225 shortcut.callback = myCallback;
1226 shortcut.callback(keyEvent);
1227 shortcutMgr.shortcuts_[1] = shortcut;
1228 bool ret = shortcutMgr.HandleKeyUp(keyEvent);
1229 EXPECT_EQ(ret, false);
1230 }
1231
1232 /**
1233 * @tc.name: KeyShortcutManagerTest_HandleKeyUp_002
1234 * @tc.desc: Test the funcation HandleKeyUp
1235 * @tc.type: FUNC
1236 * @tc.require:
1237 */
1238 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_002, TestSize.Level1)
1239 {
1240 CALL_TEST_DEBUG;
1241 KeyShortcutManager shortcutMgr;
1242 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1243 KeyShortcutManager::KeyShortcut shortcut;
1244 shortcut.modifiers = 0x1;
1245 shortcut.finalKey = 0x2;
1246 shortcut.longPressTime = 500;
1247 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1248 shortcut.session = 1;
1249 shortcut.callback = myCallback;
1250 shortcut.callback(keyEvent);
1251 shortcutMgr.shortcuts_[1] = shortcut;
1252 bool ret = shortcutMgr.HandleKeyUp(keyEvent);
1253 EXPECT_EQ(ret, false);
1254 shortcut.finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS;
1255 shortcutMgr.shortcuts_[1] = shortcut;
1256 keyEvent->SetKeyCode(2046);
1257 ret = shortcutMgr.HandleKeyUp(keyEvent);
1258 EXPECT_EQ(ret, true);
1259 }
1260
1261 /**
1262 * @tc.name: KeyShortcutManagerTest_UnregisterSystemKey_001
1263 * @tc.desc: Test the funcation UnregisterSystemKey
1264 * @tc.type: FUNC
1265 * @tc.require:
1266 */
1267 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UnregisterSystemKey_001, TestSize.Level1)
1268 {
1269 CALL_TEST_DEBUG;
1270 KeyShortcutManager shortcutMgr;
1271 KeyShortcutManager::KeyShortcut shortcut;
1272 shortcut.modifiers = 0x1;
1273 shortcut.finalKey = 0x2;
1274 shortcut.longPressTime = 500;
1275 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1276 shortcut.session = 1;
1277 shortcutMgr.shortcuts_[1] = shortcut;
1278 int32_t shortcutId = 1;
1279 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterSystemKey(shortcutId));
1280 }
1281
1282 /**
1283 * @tc.name: KeyShortcutManagerTest_UnregisterHotKey_002
1284 * @tc.desc: Test the funcation UnregisterHotKey
1285 * @tc.type: FUNC
1286 * @tc.require:
1287 */
1288 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UnregisterHotKey_002, TestSize.Level1)
1289 {
1290 CALL_TEST_DEBUG;
1291 KeyShortcutManager shortcutMgr;
1292 KeyShortcutManager::KeyShortcut shortcut;
1293 shortcut.modifiers = 0x1;
1294 shortcut.finalKey = 0x2;
1295 shortcut.longPressTime = 500;
1296 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1297 shortcut.session = 1;
1298 shortcutMgr.shortcuts_[1] = shortcut;
1299 int32_t shortcutId = 1;
1300 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterHotKey(shortcutId));
1301 shortcutId = 5;
1302 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterHotKey(shortcutId));
1303 }
1304
1305 /**
1306 * @tc.name: KeyShortcutManagerTest_HandleEvent_002
1307 * @tc.desc: Test the funcation HandleEvent
1308 * @tc.type: FUNC
1309 * @tc.require:
1310 */
1311 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleEvent_002, TestSize.Level1)
1312 {
1313 CALL_TEST_DEBUG;
1314 KeyShortcutManager shortcutMgr;
1315 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1316 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
1317 bool ret = shortcutMgr.HandleEvent(keyEvent);
1318 ASSERT_EQ(ret, false);
1319 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
1320 ret = shortcutMgr.HandleEvent(keyEvent);
1321 ASSERT_EQ(ret, false);
1322 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_CANCEL);
1323 ret = shortcutMgr.HandleEvent(keyEvent);
1324 ASSERT_EQ(ret, false);
1325 keyEvent->SetKeyAction(KeyEvent::INTENTION_LEFT);
1326 ret = shortcutMgr.HandleEvent(keyEvent);
1327 ASSERT_EQ(ret, false);
1328 }
1329
1330 /**
1331 * @tc.name: KeyShortcutManagerTest_FormatPressedKeys_001
1332 * @tc.desc: Test the funcation FormatPressedKeys
1333 * @tc.type: FUNC
1334 * @tc.require:
1335 */
1336 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_FormatPressedKeys_001, TestSize.Level1)
1337 {
1338 CALL_TEST_DEBUG;
1339 KeyShortcutManager shortcutMgr;
1340 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1341 int64_t downTime = 2;
1342 KeyEvent::KeyItem kitDown;
1343 kitDown.SetKeyCode(KeyEvent::KEYCODE_UNKNOWN);
1344 kitDown.SetPressed(true);
1345 kitDown.SetDownTime(downTime);
1346 keyEvent->AddPressedKeyItems(kitDown);
1347 std::string ret = shortcutMgr.FormatPressedKeys(keyEvent);
1348 ASSERT_EQ(ret, "-1");
1349 }
1350
1351 /**
1352 * @tc.name: KeyShortcutManagerTest_CheckGlobalKey_001
1353 * @tc.desc: Test the funcation CheckGlobalKey
1354 * @tc.type: FUNC
1355 * @tc.require:
1356 */
1357 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_CheckGlobalKey_001, TestSize.Level1)
1358 {
1359 CALL_TEST_DEBUG;
1360 KeyShortcutManager shortcutMgr;
1361 KeyShortcutManager::HotKey globalKey {
1362 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
1363 .finalKey = KeyEvent::KEYCODE_M,
1364 };
1365 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1366 KeyShortcutManager::KeyShortcut shortcut;
1367 shortcut.modifiers = 0x1;
1368 shortcut.finalKey = 0x2;
1369 shortcut.longPressTime = 500;
1370 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_DOWN;
1371 shortcut.session = 1;
1372 shortcut.callback = myCallback;
1373 shortcut.callback(keyEvent);
1374 bool ret = shortcutMgr.CheckGlobalKey(globalKey, shortcut);
1375 ASSERT_TRUE(ret);
1376 }
1377
1378 /**
1379 * @tc.name: KeyShortcutManagerTest_GetForegroundPids_001
1380 * @tc.desc: Test the funcation GetForegroundPids
1381 * @tc.type: FUNC
1382 * @tc.require:
1383 */
1384 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GetForegroundPids_001, TestSize.Level1)
1385 {
1386 CALL_TEST_DEBUG;
1387 KeyShortcutManager shortcutMgr;
1388 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1389 KeyShortcutManager::KeyShortcut shortcut;
1390 shortcut.modifiers = 0x1;
1391 shortcut.finalKey = 0x2;
1392 shortcut.longPressTime = 500;
1393 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_DOWN;
1394 shortcut.session = 1;
1395 shortcut.callback = myCallback;
1396 shortcut.callback(keyEvent);
1397 shortcutMgr.shortcuts_[1] = shortcut;
1398 std::set<int32_t> ret = shortcutMgr.GetForegroundPids();
1399 std::set<int> mySet;
1400 ASSERT_EQ(ret, mySet);
1401 }
1402
1403 /**
1404 * @tc.name: KeyShortcutManagerTest_HandleKeyDown_001
1405 * @tc.desc: Test the funcation HandleKeyDown
1406 * @tc.type: FUNC
1407 * @tc.require:
1408 */
1409 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyDown_001, TestSize.Level1)
1410 {
1411 CALL_TEST_DEBUG;
1412 KeyShortcutManager shortcutMgr;
1413 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1414 KeyShortcutManager::KeyShortcut shortcut;
1415 shortcut.modifiers = 0x1;
1416 shortcut.finalKey = 0x2;
1417 shortcut.longPressTime = 500;
1418 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_DOWN;
1419 shortcut.session = 1;
1420 shortcut.callback = myCallback;
1421 shortcut.callback(keyEvent);
1422 shortcutMgr.shortcuts_[1] = shortcut;
1423 bool ret = shortcutMgr.HandleKeyDown(keyEvent);
1424 EXPECT_EQ(ret, false);
1425 }
1426
1427 /**
1428 * @tc.name: KeyShortcutManagerTest_HandleKeyDown_002
1429 * @tc.desc: Test the funcation HandleKeyDown
1430 * @tc.type: FUNC
1431 * @tc.require:
1432 */
1433 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyDown_002, TestSize.Level1)
1434 {
1435 CALL_TEST_DEBUG;
1436 KeyShortcutManager shortcutMgr;
1437 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1438 KeyShortcutManager::KeyShortcut shortcut;
1439 shortcut.modifiers = 0x1;
1440 shortcut.finalKey = 0x2;
1441 shortcut.longPressTime = 500;
1442 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1443 shortcut.session = 1;
1444 shortcut.callback = myCallback;
1445 shortcut.callback(keyEvent);
1446 shortcutMgr.shortcuts_[1] = shortcut;
1447 bool ret = shortcutMgr.HandleKeyDown(keyEvent);
1448 EXPECT_EQ(ret, false);
1449 shortcut.finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS;
1450 shortcutMgr.shortcuts_[1] = shortcut;
1451 keyEvent->SetKeyCode(2046);
1452 ret = shortcutMgr.HandleKeyDown(keyEvent);
1453 EXPECT_FALSE(ret);
1454 }
1455
1456 /**
1457 * @tc.name: KeyShortcutManagerTest_HandleKeyUp_003
1458 * @tc.desc: Test the funcation HandleKeyUp
1459 * @tc.type: FUNC
1460 * @tc.require:
1461 */
1462 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_003, TestSize.Level1)
1463 {
1464 CALL_TEST_DEBUG;
1465 KeyShortcutManager shortcutMgr;
1466 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1467 KeyShortcutManager::KeyShortcut shortcut;
1468 shortcut.modifiers = 0x1;
1469 shortcut.finalKey = 0x2;
1470 shortcut.longPressTime = 500;
1471 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1472 shortcut.session = 1;
1473 shortcut.callback = myCallback;
1474 shortcut.callback(keyEvent);
1475 shortcutMgr.shortcuts_[1] = shortcut;
1476 bool ret = shortcutMgr.HandleKeyUp(keyEvent);
1477 ASSERT_FALSE(ret);
1478 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_DOWN;
1479 shortcutMgr.shortcuts_[1] = shortcut;
1480 ret = shortcutMgr.HandleKeyUp(keyEvent);
1481 ASSERT_FALSE(ret);
1482 }
1483
1484 /**
1485 * @tc.name: KeyShortcutManagerTest_CheckPureModifiers_001
1486 * @tc.desc: Test the funcation CheckPureModifiers
1487 * @tc.type: FUNC
1488 * @tc.require:
1489 */
1490 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_CheckPureModifiers_001, TestSize.Level1)
1491 {
1492 CALL_TEST_DEBUG;
1493 KeyShortcutManager shortcutMgr;
1494 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1495 KeyShortcutManager::KeyShortcut shortcut;
1496 shortcut.modifiers = 0x1;
1497 shortcut.finalKey = 0x2;
1498 shortcut.longPressTime = 500;
1499 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1500 shortcut.session = 1;
1501 shortcut.callback = myCallback;
1502 shortcut.callback(keyEvent);
1503 bool ret = shortcutMgr.CheckPureModifiers(keyEvent, shortcut);
1504 ASSERT_FALSE(ret);
1505 keyEvent->SetKeyCode(2045);
1506 int64_t downTime = 2;
1507 KeyEvent::KeyItem kitDown;
1508 kitDown.SetKeyCode(KeyEvent::KEYCODE_UNKNOWN);
1509 kitDown.SetPressed(true);
1510 kitDown.SetDownTime(downTime);
1511 keyEvent->AddPressedKeyItems(kitDown);
1512 ret = shortcutMgr.CheckPureModifiers(keyEvent, shortcut);
1513 ASSERT_TRUE(ret);
1514 }
1515
1516 /**
1517 * @tc.name: KeyShortcutManagerTest_CheckModifiers_001
1518 * @tc.desc: Test the funcation CheckModifiers
1519 * @tc.type: FUNC
1520 * @tc.require:
1521 */
1522 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_CheckModifiers_001, TestSize.Level1)
1523 {
1524 CALL_TEST_DEBUG;
1525 KeyShortcutManager shortcutMgr;
1526 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1527 KeyShortcutManager::KeyShortcut shortcut;
1528 shortcut.modifiers = 0x1;
1529 shortcut.finalKey = 0x2;
1530 shortcut.longPressTime = 500;
1531 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1532 shortcut.session = 1;
1533 shortcut.callback = myCallback;
1534 shortcut.callback(keyEvent);
1535 bool ret = shortcutMgr.CheckModifiers(keyEvent, shortcut);
1536 ASSERT_FALSE(ret);
1537 keyEvent->SetKeyCode(2045);
1538 int64_t downTime = 2;
1539 KeyEvent::KeyItem kitDown;
1540 kitDown.SetKeyCode(KeyEvent::KEYCODE_UNKNOWN);
1541 kitDown.SetPressed(true);
1542 kitDown.SetDownTime(downTime);
1543 keyEvent->AddPressedKeyItems(kitDown);
1544 ret = shortcutMgr.CheckModifiers(keyEvent, shortcut);
1545 ASSERT_FALSE(ret);
1546 }
1547
1548 /**
1549 * @tc.name: KeyShortcutManagerTest_TriggerDown_001
1550 * @tc.desc: Test the funcation TriggerDown
1551 * @tc.type: FUNC
1552 * @tc.require:
1553 */
1554 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerDown_001, TestSize.Level1)
1555 {
1556 CALL_TEST_DEBUG;
1557 KeyShortcutManager shortcutMgr;
1558 int32_t shortcutId = 1;
1559 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1560 KeyShortcutManager::KeyShortcut shortcut;
1561 shortcut.modifiers = 0x1;
1562 shortcut.finalKey = 0x2;
1563 shortcut.longPressTime = -5;
1564 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1565 shortcut.session = 1;
1566 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1567 shortcut.callback = myCallback;
1568 shortcut.callback(keyEvent);
1569 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1570 }
1571
1572 /**
1573 * @tc.name: KeyShortcutManagerTest_RunShortcut_003
1574 * @tc.desc: Test the funcation RunShortcut
1575 * @tc.type: FUNC
1576 * @tc.require:
1577 */
1578 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_003, TestSize.Level1)
1579 {
1580 CALL_TEST_DEBUG;
1581 KeyShortcutManager shortcutMgr;
1582 int32_t shortcutId = 1;
1583 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1584 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1585 KeyShortcutManager::KeyShortcut shortcut;
1586 shortcut.modifiers = 0x1;
1587 shortcut.finalKey = 0x2;
1588 shortcut.longPressTime = 500;
1589 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1590 shortcut.session = 1;
1591 shortcutMgr.shortcuts_[1] = shortcut;
1592 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1593 shortcut.callback = myCallback;
1594 shortcut.callback(keyEvent);
1595 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1596 }
1597
1598 /**
1599 * @tc.name: KeyShortcutManagerTest_TriggerUp_003
1600 * @tc.desc: Test the funcation TriggerUp
1601 * @tc.type: FUNC
1602 * @tc.require:
1603 */
1604 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_003, TestSize.Level1)
1605 {
1606 CALL_TEST_DEBUG;
1607 KeyShortcutManager shortcutMgr;
1608 int32_t shortcutId = 1;
1609 KeyShortcutManager::KeyShortcut shortcut;
1610 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1611 shortcut.longPressTime = 1000;
1612 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1613 KeyEvent::KeyItem item;
1614 item.SetKeyCode(5);
1615 keyEvent->SetActionTime(3);
1616 item.SetDownTime(1);
1617 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1618 keyEvent->SetActionTime(1003);
1619 item.SetDownTime(1);
1620 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1621 shortcut.longPressTime = -10;
1622 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1623 shortcut.callback = myCallback;
1624 shortcut.callback(keyEvent);
1625 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1626 }
1627
1628 /**
1629 * @tc.name: KeyShortcutManagerTest_ReadHotkey_001
1630 * @tc.desc: Test the funcation ReadHotkey
1631 * @tc.type: FUNC
1632 * @tc.require:
1633 */
1634 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadHotkey_001, TestSize.Level1)
1635 {
1636 CALL_TEST_DEBUG;
1637 KeyShortcutManager shortcutMgr;
1638 cJSON *jsonHotkey = cJSON_CreateString("not an object");
1639 int32_t ret = shortcutMgr.ReadHotkey(jsonHotkey) ;
1640 EXPECT_EQ(ret, RET_ERR);
1641 cJSON_Delete(jsonHotkey);
1642 }
1643
1644 /**
1645 * @tc.name: KeyShortcutManagerTest_ReadHotkey_002
1646 * @tc.desc: Test the funcation ReadHotkey
1647 * @tc.type: FUNC
1648 * @tc.require:
1649 */
1650 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadHotkey_002, TestSize.Level1)
1651 {
1652 CALL_TEST_DEBUG;
1653 KeyShortcutManager shortcutMgr;
1654 cJSON *jsonHotkey = cJSON_CreateObject();
1655 int32_t ret = shortcutMgr.ReadHotkey(jsonHotkey) ;
1656 EXPECT_EQ(ret, RET_ERR);
1657 cJSON_Delete(jsonHotkey);
1658 }
1659
1660 /**
1661 * @tc.name: KeyShortcutManagerTest_ReadHotkey_003
1662 * @tc.desc: Test the funcation ReadHotkey
1663 * @tc.type: FUNC
1664 * @tc.require:
1665 */
1666 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadHotkey_003, TestSize.Level1)
1667 {
1668 CALL_TEST_DEBUG;
1669 KeyShortcutManager shortcutMgr;
1670 cJSON *jsonHotkey = cJSON_CreateObject();
1671 cJSON_AddItemToObject(jsonHotkey, "businessId", cJSON_CreateNumber(123));
1672 int32_t ret = shortcutMgr.ReadHotkey(jsonHotkey) ;
1673 EXPECT_EQ(ret, RET_ERR);
1674 cJSON_Delete(jsonHotkey);
1675 }
1676
1677 /**
1678 * @tc.name: KeyShortcutManagerTest_ReadHotkey_004
1679 * @tc.desc: Test the funcation ReadHotkey
1680 * @tc.type: FUNC
1681 * @tc.require:
1682 */
1683 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadHotkey_004, TestSize.Level1)
1684 {
1685 CALL_TEST_DEBUG;
1686 KeyShortcutManager shortcutMgr;
1687 cJSON *jsonHotkey = cJSON_CreateObject();
1688 cJSON* preKey = cJSON_CreateArray();
1689 for (int i = 0; i < 5; ++i) {
1690 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i));
1691 }
1692 cJSON_AddItemToObject(jsonHotkey, "preKey", preKey);
1693 int32_t ret = shortcutMgr.ReadHotkey(jsonHotkey) ;
1694 EXPECT_EQ(ret, RET_ERR);
1695 cJSON_Delete(jsonHotkey);
1696 }
1697
1698 /**
1699 * @tc.name: KeyShortcutManagerTest_ReadHotkey_005
1700 * @tc.desc: Test the funcation ReadHotkey
1701 * @tc.type: FUNC
1702 * @tc.require:
1703 */
1704 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadHotkey_005, TestSize.Level1)
1705 {
1706 CALL_TEST_DEBUG;
1707 KeyShortcutManager shortcutMgr;
1708 cJSON *jsonHotkey = cJSON_CreateObject();
1709 cJSON* preKey = cJSON_CreateArray();
1710 for (int i = 0; i < 5; ++i) {
1711 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i));
1712 }
1713 cJSON_AddItemToObject(jsonHotkey, "preKey", preKey);
1714 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1));
1715 int32_t ret = shortcutMgr.ReadHotkey(jsonHotkey) ;
1716 EXPECT_EQ(ret, RET_ERR);
1717 cJSON_Delete(jsonHotkey);
1718 }
1719
1720 /**
1721 * @tc.name: KeyShortcutManagerTest_ReadSystemKey_001
1722 * @tc.desc: Test the funcation ReadSystemKey
1723 * @tc.type: FUNC
1724 * @tc.require:
1725 */
1726 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadSystemKey_001, TestSize.Level1)
1727 {
1728 CALL_TEST_DEBUG;
1729 KeyShortcutManager shortcutMgr;
1730 cJSON *jsonSysKey = cJSON_CreateString("not an object");
1731 int32_t ret = shortcutMgr.ReadSystemKey(jsonSysKey) ;
1732 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1733 cJSON_Delete(jsonSysKey);
1734 }
1735
1736 /**
1737 * @tc.name: KeyShortcutManagerTest_ReadSystemKey_002
1738 * @tc.desc: Test the funcation ReadSystemKey
1739 * @tc.type: FUNC
1740 * @tc.require:
1741 */
1742 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadSystemKey_002, TestSize.Level1)
1743 {
1744 CALL_TEST_DEBUG;
1745 KeyShortcutManager shortcutMgr;
1746 cJSON *jsonSysKey = cJSON_CreateObject();
1747 int32_t ret = shortcutMgr.ReadSystemKey(jsonSysKey) ;
1748 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1749 cJSON_Delete(jsonSysKey);
1750 }
1751
1752 /**
1753 * @tc.name: KeyShortcutManagerTest_ReadSystemKey_003
1754 * @tc.desc: Test the funcation ReadSystemKey
1755 * @tc.type: FUNC
1756 * @tc.require:
1757 */
1758 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadSystemKey_003, TestSize.Level1)
1759 {
1760 CALL_TEST_DEBUG;
1761 KeyShortcutManager shortcutMgr;
1762 cJSON *jsonSysKey = cJSON_CreateObject();
1763 cJSON* preKey = cJSON_CreateArray();
1764 cJSON_AddItemToObject(jsonSysKey, "preKey", preKey);
1765 int32_t ret = shortcutMgr.ReadSystemKey(jsonSysKey) ;
1766 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1767 cJSON_Delete(jsonSysKey);
1768 }
1769
1770 /**
1771 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKey_001
1772 * @tc.desc: Test the funcation ReadExceptionalSystemKey
1773 * @tc.type: FUNC
1774 * @tc.require:
1775 */
1776 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKey_001, TestSize.Level1)
1777 {
1778 CALL_TEST_DEBUG;
1779 KeyShortcutManager shortcutMgr;
1780 cJSON *jsonSysKey = cJSON_CreateString("not an object");
1781 int32_t ret = shortcutMgr.ReadExceptionalSystemKey(jsonSysKey) ;
1782 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1783 cJSON_Delete(jsonSysKey);
1784 }
1785
1786 /**
1787 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKey_002
1788 * @tc.desc: Test the funcation ReadExceptionalSystemKey
1789 * @tc.type: FUNC
1790 * @tc.require:
1791 */
1792 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKey_002, TestSize.Level1)
1793 {
1794 CALL_TEST_DEBUG;
1795 KeyShortcutManager shortcutMgr;
1796 cJSON *jsonSysKey = cJSON_CreateObject();
1797 int32_t ret = shortcutMgr.ReadExceptionalSystemKey(jsonSysKey) ;
1798 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1799 cJSON_Delete(jsonSysKey);
1800 }
1801
1802 /**
1803 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKey_003
1804 * @tc.desc: Test the funcation ReadExceptionalSystemKey
1805 * @tc.type: FUNC
1806 * @tc.require:
1807 */
1808 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKey_003, TestSize.Level1)
1809 {
1810 CALL_TEST_DEBUG;
1811 KeyShortcutManager shortcutMgr;
1812 cJSON *jsonSysKey = cJSON_CreateObject();
1813 cJSON* preKey = cJSON_CreateArray();
1814 cJSON_AddItemToObject(jsonSysKey, "preKey", preKey);
1815 int32_t ret = shortcutMgr.ReadExceptionalSystemKey(jsonSysKey) ;
1816 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1817 cJSON_Delete(jsonSysKey);
1818 }
1819
1820 /**
1821 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKey_004
1822 * @tc.desc: Test the funcation ReadExceptionalSystemKey
1823 * @tc.type: FUNC
1824 * @tc.require:
1825 */
1826 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKey_004, TestSize.Level1)
1827 {
1828 CALL_TEST_DEBUG;
1829 KeyShortcutManager shortcutMgr;
1830 cJSON *jsonSysKey = cJSON_CreateObject();
1831 cJSON* preKey = cJSON_CreateArray();
1832 for (int i = 0; i < 5; ++i) {
1833 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i));
1834 }
1835 cJSON_AddItemToObject(jsonSysKey, "preKey", preKey);
1836 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1));
1837 int32_t ret = shortcutMgr.ReadExceptionalSystemKey(jsonSysKey) ;
1838 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1839 cJSON_Delete(jsonSysKey);
1840 }
1841
1842 /**
1843 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKey_005
1844 * @tc.desc: Test the funcation ReadExceptionalSystemKey
1845 * @tc.type: FUNC
1846 * @tc.require:
1847 */
1848 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKey_005, TestSize.Level1)
1849 {
1850 CALL_TEST_DEBUG;
1851 KeyShortcutManager shortcutMgr;
1852 cJSON *jsonSysKey = cJSON_CreateObject();
1853 cJSON* preKey = cJSON_CreateArray();
1854 for (int i = 0; i < 5; ++i) {
1855 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i));
1856 }
1857 cJSON_AddItemToObject(jsonSysKey, "longPressTime", preKey);
1858 int32_t ret = shortcutMgr.ReadExceptionalSystemKey(jsonSysKey) ;
1859 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
1860 cJSON_Delete(jsonSysKey);
1861 }
1862
1863 /**
1864 * @tc.name: KeyShortcutManagerTest_IsReservedSystemKey
1865 * @tc.desc: Test the funcation IsReservedSystemKey
1866 * @tc.type: FUNC
1867 * @tc.require:
1868 */
1869 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_IsReservedSystemKey, TestSize.Level1)
1870 {
1871 CALL_TEST_DEBUG;
1872 KeyShortcutManager shortcutMgr;
1873 KeyShortcutManager::KeyShortcut shortcut;
1874 bool ret = shortcutMgr.IsReservedSystemKey(shortcut) ;
1875 ASSERT_FALSE(ret);
1876 }
1877
1878 /**
1879 * @tc.name: KeyShortcutManagerTest_HaveRegisteredGlobalKey
1880 * @tc.desc: Test the funcation HaveRegisteredGlobalKey
1881 * @tc.type: FUNC
1882 * @tc.require:
1883 */
1884 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HaveRegisteredGlobalKey, TestSize.Level1)
1885 {
1886 CALL_TEST_DEBUG;
1887 KeyShortcutManager shortcutMgr;
1888 KeyShortcutManager::KeyShortcut shortcut;
1889 bool ret = shortcutMgr.HaveRegisteredGlobalKey(shortcut) ;
1890 ASSERT_FALSE(ret);
1891 }
1892
1893 /**
1894 * @tc.name: KeyShortcutManagerTest_HandleKeyCancel
1895 * @tc.desc: Test the funcation HandleKeyCancel
1896 * @tc.type: FUNC
1897 * @tc.require:
1898 */
1899 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyCancel, TestSize.Level1)
1900 {
1901 CALL_TEST_DEBUG;
1902 KeyShortcutManager shortcutMgr;
1903 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1904 bool ret = shortcutMgr.HandleKeyCancel(keyEvent);
1905 ASSERT_FALSE(ret);
1906 }
1907
1908 /**
1909 * @tc.name: KeyShortcutManagerTest_CheckCombination
1910 * @tc.desc: Test the funcation CheckCombination
1911 * @tc.type: FUNC
1912 * @tc.require:
1913 */
1914 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_CheckCombination, TestSize.Level1)
1915 {
1916 CALL_TEST_DEBUG;
1917 KeyShortcutManager shortcutMgr;
1918 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1919 KeyShortcutManager::KeyShortcut shortcut;
1920 bool ret = shortcutMgr.CheckCombination(keyEvent, shortcut);
1921 ASSERT_FALSE(ret);
1922 }
1923
1924 /**
1925 * @tc.name: KeyShortcutManagerTest_GetAllSystemHotkeys
1926 * @tc.desc: Test the funcation GetAllSystemHotkeys
1927 * @tc.type: FUNC
1928 * @tc.require:
1929 */
1930 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GetAllSystemHotkeys, TestSize.Level1)
1931 {
1932 CALL_TEST_DEBUG;
1933 KeyShortcutManager shortcutMgr;
1934 std::vector<std::unique_ptr<KeyOption>> sysKeys;
1935 bool ret = shortcutMgr.GetAllSystemHotkeys(sysKeys);
1936 EXPECT_EQ(ret, RET_OK);
1937 }
1938
1939 /**
1940 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKeys
1941 * @tc.desc: Test the funcation ReadExceptionalSystemKeys
1942 * @tc.type: FUNC
1943 * @tc.require:
1944 */
1945 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKeys, TestSize.Level1)
1946 {
1947 CALL_TEST_DEBUG;
1948 KeyShortcutManager shortcutMgr;
1949 const std::string cfgPath { "ExceptionalSystemKeys" };
1950 shortcutMgr.ReadExceptionalSystemKeys(cfgPath);
1951 EXPECT_EQ(cfgPath, "ExceptionalSystemKeys");
1952 }
1953
1954 /**
1955 * @tc.name: KeyShortcutManagerTest_IsValid
1956 * @tc.desc: Test the funcation IsValid
1957 * @tc.type: FUNC
1958 * @tc.require:
1959 */
1960 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_IsValid, TestSize.Level1)
1961 {
1962 CALL_TEST_DEBUG;
1963 KeyShortcutManager shortcutMgr;
1964 KeyShortcutManager::ShortcutTriggerType triggerType {
1965 KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP };
1966 bool ret = shortcutMgr.IsValid(triggerType);
1967 EXPECT_TRUE(ret);
1968 }
1969
1970 /**
1971 * @tc.name: KeyShortcutManagerTest_RegisterHotKey_01
1972 * @tc.desc: Test the funcation RegisterHotKey
1973 * @tc.type: FUNC
1974 * @tc.require:
1975 */
1976 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RegisterHotKey_01, TestSize.Level1)
1977 {
1978 CALL_TEST_DEBUG;
1979 KeyShortcutManager::HotKey globalKey1 {
1980 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_META_LEFT },
1981 .finalKey = KeyEvent::KEYCODE_M,
1982 .session = 1,
1983 };
1984 KeyShortcutManager shortcutMgr;
1985 int32_t result = shortcutMgr.RegisterHotKey(globalKey1);
1986 KeyShortcutManager::HotKey globalKey2 {
1987 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_META_LEFT },
1988 .finalKey = KeyEvent::KEYCODE_M,
1989 .session = 2,
1990 };
1991 result = shortcutMgr.RegisterHotKey(globalKey2);
1992 EXPECT_FALSE(result >= BASE_SHORTCUT_ID);
1993 }
1994
1995 /**
1996 * @tc.name: KeyShortcutManagerTest_RegisterHotKey_02
1997 * @tc.desc: Test the funcation RegisterHotKey
1998 * @tc.type: FUNC
1999 * @tc.require:
2000 */
2001 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RegisterHotKey_02, TestSize.Level1)
2002 {
2003 CALL_TEST_DEBUG;
2004 KeyShortcutManager::HotKey validKey {
2005 .modifiers = { KeyEvent::KEYCODE_F1 },
2006 .finalKey = 10,
2007 .session = 1,
2008 };
2009 KeyShortcutManager shortcutMgr;
2010 int32_t result = shortcutMgr.RegisterHotKey(validKey);
2011 ASSERT_EQ(result, KEY_SHORTCUT_ERROR_COMBINATION_KEY);
2012 }
2013
2014 /**
2015 * @tc.name: KeyShortcutManagerTest_ReadSystemKeys
2016 * @tc.desc: Test the funcation RegisterHotKey
2017 * @tc.type: FUNC
2018 * @tc.require:
2019 */
2020 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadSystemKeys, TestSize.Level1)
2021 {
2022 CALL_TEST_DEBUG;
2023 KeyShortcutManager manager;
2024 std::string cfgPath = "not a json string";
2025 ASSERT_NO_FATAL_FAILURE(manager.ReadSystemKeys(cfgPath));
2026 }
2027
2028 /**
2029 * @tc.name: KeyShortcutManagerTest_AddHotkey
2030 * @tc.desc: Test the funcation AddHotkey
2031 * @tc.type: FUNC
2032 * @tc.require:
2033 */
2034 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_AddHotkey, TestSize.Level1)
2035 {
2036 CALL_TEST_DEBUG;
2037 KeyShortcutManager shortcutMgr;
2038 KeyOption shortcut;
2039 std::set<int32_t> preKeys;
2040 preKeys.insert(1);
2041 preKeys.insert(2);
2042 preKeys.insert(3);
2043 preKeys.insert(4);
2044 preKeys.insert(5);
2045 int32_t finalKey = KeyEvent::KEYCODE_M;
2046 shortcut.SetPreKeys(preKeys);
2047 shortcut.SetFinalKey(KeyEvent::KEYCODE_M);
2048 auto result = shortcutMgr.AddHotkey(preKeys, finalKey);
2049 EXPECT_EQ(result, RET_ERR);
2050 }
2051
2052 /**
2053 * @tc.name: KeyShortcutManagerTest_ReadSystemKey_004
2054 * @tc.desc: Test the funcation ReadSystemKey
2055 * @tc.type: FUNC
2056 * @tc.require:
2057 */
2058 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadSystemKey_004, TestSize.Level1)
2059 {
2060 CALL_TEST_DEBUG;
2061 KeyShortcutManager shortcutMgr;
2062 cJSON* jsonSysKey = cJSON_CreateObject();
2063 cJSON* preKey = cJSON_CreateArray();
2064 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1));
2065 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(1));
2066 cJSON_AddItemToObject(jsonSysKey, "preKey", preKey);
2067
2068 int32_t ret = shortcutMgr.ReadSystemKey(jsonSysKey) ;
2069 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
2070 cJSON_Delete(jsonSysKey);
2071 }
2072
2073 /**
2074 * @tc.name: KeyShortcutManagerTest_ReadSystemKey_005
2075 * @tc.desc: Test the funcation ReadSystemKey
2076 * @tc.type: FUNC
2077 * @tc.require:
2078 */
2079 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadSystemKey_005, TestSize.Level1)
2080 {
2081 CALL_TEST_DEBUG;
2082 KeyShortcutManager shortcutMgr;
2083 cJSON *jsonSysKey = cJSON_CreateObject();
2084 cJSON_AddNumberToObject(jsonSysKey, "keyDownDuration", 1);
2085 int32_t ret = shortcutMgr.ReadSystemKey(jsonSysKey) ;
2086 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
2087 cJSON_Delete(jsonSysKey);
2088 }
2089
2090 /**
2091 * @tc.name: KeyShortcutManagerTest_AddSystemKey_001
2092 * @tc.desc: Test the funcation AddSystemKey
2093 * @tc.type: FUNC
2094 * @tc.require:
2095 */
2096 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_AddSystemKey_001, TestSize.Level1)
2097 {
2098 CALL_TEST_DEBUG;
2099 KeyShortcutManager shortcutMgr;
2100 std::set<int32_t> preKeys;
2101 preKeys.insert(1);
2102 preKeys.insert(2);
2103 preKeys.insert(3);
2104 preKeys.insert(4);
2105 preKeys.insert(5);
2106 int32_t finalKey = KeyEvent::KEYCODE_M;
2107 auto result = shortcutMgr.AddSystemKey(preKeys, finalKey);
2108 EXPECT_EQ(result, KEY_SHORTCUT_ERROR_COMBINATION_KEY);
2109 }
2110
2111 /**
2112 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKeys_001
2113 * @tc.desc: Test the funcation ReadExceptionalSystemKeys
2114 * @tc.type: FUNC
2115 * @tc.require:
2116 */
2117 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKeys_001, TestSize.Level1)
2118 {
2119 CALL_TEST_DEBUG;
2120 KeyShortcutManager shortcutMgr;
2121
2122 cJSON* root = cJSON_CreateObject();
2123 cJSON* sysKeys = cJSON_CreateArray();
2124
2125 // 构造缺失必要字段的对象
2126 cJSON* invalidItem1 = cJSON_CreateObject();
2127 cJSON_AddStringToObject(invalidItem1, "action", "disable");
2128
2129 // 构造包含错误类型字段的对象
2130 cJSON* invalidItem2 = cJSON_CreateObject();
2131 cJSON_AddStringToObject(invalidItem2, "keyCode", "invalid_number");
2132
2133 cJSON_AddItemToArray(sysKeys, invalidItem1);
2134 cJSON_AddItemToArray(sysKeys, invalidItem2);
2135 cJSON_AddItemToObject(root, "ExceptionalSystemKeys", sysKeys);
2136 ASSERT_NO_FATAL_FAILURE(shortcutMgr.ReadExceptionalSystemKeys("dummy_path"));
2137 }
2138
2139 /**
2140 * @tc.name: KeyShortcutManagerTest_ReadExceptionalSystemKey_006
2141 * @tc.desc: Test the funcation ReadExceptionalSystemKey
2142 * @tc.type: FUNC
2143 * @tc.require:
2144 */
2145 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ReadExceptionalSystemKey_006, TestSize.Level1)
2146 {
2147 CALL_TEST_DEBUG;
2148 KeyShortcutManager shortcutMgr;
2149 cJSON *jsonSysKey = cJSON_CreateObject();
2150 cJSON* preKey = cJSON_CreateArray();
2151 KeyShortcutManager::KeyShortcut shortcut;
2152 shortcut.modifiers = 0x1;
2153 shortcut.finalKey = 0x2;
2154 shortcut.longPressTime = 500;
2155 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
2156 shortcut.session = 1;
2157 shortcutMgr.shortcuts_[1] = shortcut;
2158 for (int i = 0; i < 5; ++i) {
2159 cJSON_AddItemToArray(preKey, cJSON_CreateNumber(i));
2160 }
2161 cJSON_AddItemToObject(jsonSysKey, "triggerType", preKey);
2162 int32_t ret = shortcutMgr.ReadExceptionalSystemKey(jsonSysKey) ;
2163 EXPECT_EQ(ret, KEY_SHORTCUT_ERROR_CONFIG);
2164 cJSON_Delete(jsonSysKey);
2165 }
2166
2167 /**
2168 * @tc.name: KeyShortcutManagerTest_FormatModifiers
2169 * @tc.desc: Test the funcation FormatModifiers
2170 * @tc.type: FUNC
2171 * @tc.require:
2172 */
2173 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_FormatModifiers, TestSize.Level1)
2174 {
2175 CALL_TEST_DEBUG;
2176 KeyShortcutManager shortcutMgr;
2177 std::set<int32_t> modifiers;
2178 modifiers.insert(KeyEvent::KEYCODE_CTRL_LEFT);
2179 modifiers.insert(KeyEvent::KEYCODE_SHIFT_RIGHT);
2180 modifiers.insert(KeyEvent::KEYCODE_ENTER);
2181 modifiers.insert(KeyEvent::KEYCODE_ALT_LEFT);
2182 modifiers.insert(KeyEvent::KEYCODE_ALT_RIGHT);
2183 auto ret = shortcutMgr.FormatModifiers(modifiers) ;
2184 EXPECT_EQ(ret, "2045,2046,2048,2054,...");
2185 }
2186 } // namespace MMI
2187 } // namespace OHOS
2188