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
38 using namespace testing;
39 using namespace testing::ext;
40
41 class KeyShortcutManagerTest : public testing::Test {
42 public:
SetUpTestCase(void)43 static void SetUpTestCase(void) {}
TearDownTestCase(void)44 static void TearDownTestCase(void) {}
45
46 std::shared_ptr<KeyEvent> TriggerSystemKey01();
47 std::shared_ptr<KeyEvent> TriggerSystemKey02();
48 std::shared_ptr<KeyEvent> TriggerSystemKey03();
49 std::shared_ptr<KeyEvent> TriggerSystemKey04();
50 std::shared_ptr<KeyEvent> TriggerSystemKey05();
51 std::shared_ptr<KeyEvent> TriggerSystemKey06();
52 std::shared_ptr<KeyEvent> TriggerSystemKey07();
53 std::shared_ptr<KeyEvent> TriggerGlobalKey01();
54 std::shared_ptr<KeyEvent> TriggerGlobalKey0101();
55 std::shared_ptr<KeyEvent> ResetAllTriggering();
56 };
57
58 /**
59 * @tc.name: KeyShortcutManagerTest_SystemKey_01
60 * @tc.desc: We can register system shortcut key that consist of modifiers only.
61 * @tc.type: FUNC
62 * @tc.require:
63 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_01, TestSize.Level1)64 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_01, TestSize.Level1)
65 {
66 CALL_TEST_DEBUG;
67 KeyShortcutManager::SystemShortcutKey sysKey {
68 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT },
69 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
70 };
71 KeyShortcutManager shortcutMgr;
72 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
73 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
74 }
75
76 /**
77 * @tc.name: KeyShortcutManagerTest_SystemKey_02
78 * @tc.desc: We can register system shortcut key that consist of modifiers and
79 * single non-modifier key, with the non-modifier key as final key.
80 * @tc.type: FUNC
81 * @tc.require:
82 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_02, TestSize.Level1)83 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_02, TestSize.Level1)
84 {
85 CALL_TEST_DEBUG;
86 KeyShortcutManager::SystemShortcutKey sysKey {
87 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT },
88 .finalKey = KeyEvent::KEYCODE_S,
89 };
90 KeyShortcutManager shortcutMgr;
91 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
92 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
93 }
94
95 /**
96 * @tc.name: KeyShortcutManagerTest_SystemKey_03
97 * @tc.desc: We can register system shortcut key that consist of modifiers and
98 * single non-modifier key, with the non-modifier key as final key.
99 * @tc.type: FUNC
100 * @tc.require:
101 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_03, TestSize.Level1)102 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_03, TestSize.Level1)
103 {
104 CALL_TEST_DEBUG;
105 KeyShortcutManager::SystemShortcutKey sysKey {
106 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
107 .finalKey = KeyEvent::KEYCODE_BACKSLASH,
108 };
109 KeyShortcutManager shortcutMgr;
110 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
111 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
112 }
113
114 /**
115 * @tc.name: KeyShortcutManagerTest_SystemKey_04
116 * @tc.desc: We can register system shortcut key that consist of modifiers and
117 * single non-modifier key, with the non-modifier key as final key.
118 * @tc.type: FUNC
119 * @tc.require:
120 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_04, TestSize.Level1)121 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_04, TestSize.Level1)
122 {
123 CALL_TEST_DEBUG;
124 KeyShortcutManager::SystemShortcutKey sysKey {
125 .finalKey = KeyEvent::KEYCODE_A,
126 };
127 KeyShortcutManager shortcutMgr;
128 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
129 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
130 }
131
132 /**
133 * @tc.name: KeyShortcutManagerTest_SystemKey_05
134 * @tc.desc: Only 'LOGO' can be single-key shortcut.
135 * @tc.type: FUNC
136 * @tc.require:
137 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_05, TestSize.Level1)138 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_05, TestSize.Level1)
139 {
140 CALL_TEST_DEBUG;
141 KeyShortcutManager::SystemShortcutKey sysKey {
142 .modifiers = { KeyEvent::KEYCODE_META_LEFT },
143 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
144 };
145 KeyShortcutManager shortcutMgr;
146 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
147 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
148 }
149
150 /**
151 * @tc.name: KeyShortcutManagerTest_SystemKey_06
152 * @tc.desc: Only 'LOGO' can be single-key shortcut.
153 * @tc.type: FUNC
154 * @tc.require:
155 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_06, TestSize.Level1)156 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_06, TestSize.Level1)
157 {
158 CALL_TEST_DEBUG;
159 KeyShortcutManager::SystemShortcutKey sysKey {
160 .modifiers = { KeyEvent::KEYCODE_SHIFT_LEFT },
161 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
162 };
163 KeyShortcutManager shortcutMgr;
164 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
165 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
166 }
167
168 /**
169 * @tc.name: KeyShortcutManagerTest_SystemKey_07
170 * @tc.desc: Can not register reserved system key.
171 * @tc.type: FUNC
172 * @tc.require:
173 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_07, TestSize.Level1)174 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_07, TestSize.Level1)
175 {
176 CALL_TEST_DEBUG;
177 KeyShortcutManager::SystemShortcutKey sysKey {
178 .modifiers = { KeyEvent::KEYCODE_META_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
179 .finalKey = KeyEvent::KEYCODE_S,
180 };
181 KeyShortcutManager shortcutMgr;
182 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
183 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
184 }
185
186 /**
187 * @tc.name: KeyShortcutManagerTest_SystemKey_08
188 * @tc.desc: System key support DOWN and UP trigger.
189 * @tc.type: FUNC
190 * @tc.require:
191 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_08, TestSize.Level1)192 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_08, TestSize.Level1)
193 {
194 CALL_TEST_DEBUG;
195 KeyShortcutManager::SystemShortcutKey sysKey {
196 .modifiers = { KeyEvent::KEYCODE_META_LEFT },
197 .finalKey = KeyEvent::KEYCODE_D,
198 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP,
199 };
200 KeyShortcutManager shortcutMgr;
201 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
202 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
203 }
204
205 /**
206 * @tc.name: KeyShortcutManagerTest_SystemKey_09
207 * @tc.desc: System key support long press.
208 * @tc.type: FUNC
209 * @tc.require:
210 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_09, TestSize.Level1)211 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_SystemKey_09, TestSize.Level1)
212 {
213 CALL_TEST_DEBUG;
214 KeyShortcutManager::SystemShortcutKey sysKey {
215 .modifiers = { KeyEvent::KEYCODE_META_LEFT },
216 .finalKey = KeyEvent::KEYCODE_D,
217 .longPressTime = DEFAULT_LONG_PRESS_TIME,
218 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP,
219 };
220 KeyShortcutManager shortcutMgr;
221 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
222 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
223 }
224
225 /**
226 * @tc.name: KeyShortcutManagerTest_GlobalKey_01
227 * @tc.desc: Global shortcut key that consist of modifiers and single non-modifier key,
228 * with the non-modifier key as final key.
229 * @tc.type: FUNC
230 * @tc.require:
231 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_01, TestSize.Level1)232 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_01, TestSize.Level1)
233 {
234 CALL_TEST_DEBUG;
235 KeyShortcutManager::HotKey globalKey {
236 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
237 .finalKey = KeyEvent::KEYCODE_M,
238 };
239 KeyShortcutManager shortcutMgr;
240 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
241 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
242 }
243
244 /**
245 * @tc.name: KeyShortcutManagerTest_GlobalKey_02
246 * @tc.desc: Global shortcut key that consist of modifiers and single non-modifier key,
247 * with the non-modifier key as final key.
248 * @tc.type: FUNC
249 * @tc.require:
250 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_02, TestSize.Level1)251 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_02, TestSize.Level1)
252 {
253 CALL_TEST_DEBUG;
254 KeyShortcutManager::HotKey globalKey {
255 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
256 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
257 };
258 KeyShortcutManager shortcutMgr;
259 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
260 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
261 }
262
263 /**
264 * @tc.name: KeyShortcutManagerTest_GlobalKey_03
265 * @tc.desc: Global shortcut key that consist of modifiers and single non-modifier key,
266 * with the non-modifier key as final key.
267 * @tc.type: FUNC
268 * @tc.require:
269 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_03, TestSize.Level1)270 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_03, TestSize.Level1)
271 {
272 CALL_TEST_DEBUG;
273 KeyShortcutManager::HotKey globalKey {
274 .finalKey = KeyEvent::KEYCODE_M,
275 };
276 KeyShortcutManager shortcutMgr;
277 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
278 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
279 }
280
281 /**
282 * @tc.name: KeyShortcutManagerTest_GlobalKey_04
283 * @tc.desc: 'LOGO' can not be modifier of Global shortcut key.
284 * @tc.type: FUNC
285 * @tc.require:
286 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_04, TestSize.Level1)287 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_04, TestSize.Level1)
288 {
289 CALL_TEST_DEBUG;
290 KeyShortcutManager::HotKey globalKey {
291 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_META_LEFT },
292 .finalKey = KeyEvent::KEYCODE_M,
293 };
294 KeyShortcutManager shortcutMgr;
295 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
296 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
297 }
298
299 /**
300 * @tc.name: KeyShortcutManagerTest_GlobalKey_05
301 * @tc.desc: We can not register registered system key as global shortcut key.
302 * @tc.type: FUNC
303 * @tc.require:
304 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_05, TestSize.Level1)305 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_05, TestSize.Level1)
306 {
307 CALL_TEST_DEBUG;
308 KeyShortcutManager::SystemShortcutKey sysKey {
309 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
310 .finalKey = KeyEvent::KEYCODE_M,
311 };
312 KeyShortcutManager shortcutMgr;
313 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
314 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
315
316 KeyShortcutManager::HotKey globalKey {
317 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
318 .finalKey = KeyEvent::KEYCODE_M,
319 };
320 shortcutId = shortcutMgr.RegisterHotKey(globalKey);
321 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
322 }
323
324 /**
325 * @tc.name: KeyShortcutManagerTest_GlobalKey_06
326 * @tc.desc: We can not register reserved system key as global shortcut key.
327 * @tc.type: FUNC
328 * @tc.require:
329 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_06, TestSize.Level1)330 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_06, TestSize.Level1)
331 {
332 CALL_TEST_DEBUG;
333 KeyShortcutManager::HotKey globalKey {
334 .modifiers = { KeyEvent::KEYCODE_META_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
335 .finalKey = KeyEvent::KEYCODE_S,
336 };
337 KeyShortcutManager shortcutMgr;
338 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
339 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
340 }
341
342 /**
343 * @tc.name: KeyShortcutManagerTest_GlobalKey_07
344 * @tc.desc: We can register a global shortcut key only once.
345 * @tc.type: FUNC
346 * @tc.require:
347 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_07, TestSize.Level1)348 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GlobalKey_07, TestSize.Level1)
349 {
350 CALL_TEST_DEBUG;
351 KeyShortcutManager shortcutMgr;
352 KeyShortcutManager::HotKey globalKey {
353 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
354 .finalKey = KeyEvent::KEYCODE_M,
355 };
356 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey);
357 EXPECT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
358 shortcutId = shortcutMgr.RegisterHotKey(globalKey);
359 EXPECT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
360 }
361
TriggerSystemKey01()362 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey01()
363 {
364 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
365 CHKPP(keyEvent);
366 int64_t now = GetSysClockTime();
367 KeyEvent::KeyItem keyItem {};
368 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
369 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
370 keyItem.SetPressed(true);
371 keyEvent->AddKeyItem(keyItem);
372
373 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
374 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
375 keyEvent->AddKeyItem(keyItem);
376
377 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
378 keyItem.SetDownTime(now);
379 keyEvent->AddKeyItem(keyItem);
380
381 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
382 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
383 keyEvent->SetActionTime(now);
384 return keyEvent;
385 }
386
387 /**
388 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_01
389 * @tc.desc: Trigger system key immediately.
390 * @tc.type: FUNC
391 * @tc.require:
392 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_01, TestSize.Level1)393 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_01, TestSize.Level1)
394 {
395 CALL_TEST_DEBUG;
396 bool triggered = false;
397 KeyShortcutManager::SystemShortcutKey sysKey {
398 .modifiers = { KeyEvent::KEYCODE_SHIFT_LEFT },
399 .finalKey = KeyEvent::KEYCODE_S,
400 .longPressTime = NO_LONG_PRESS,
401 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
402 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
403 triggered = true;
404 },
405 };
406 KeyShortcutManager shortcutMgr;
407 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
408 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
409
410 auto keyEvent = TriggerSystemKey01();
411 ASSERT_TRUE(keyEvent != nullptr);
412 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
413 EXPECT_FALSE(triggered);
414 }
415
TriggerSystemKey02()416 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey02()
417 {
418 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
419 CHKPP(keyEvent);
420 int64_t now = GetSysClockTime();
421 KeyEvent::KeyItem keyItem {};
422 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
423 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
424 keyItem.SetPressed(true);
425 keyEvent->AddKeyItem(keyItem);
426
427 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
428 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
429 keyEvent->AddKeyItem(keyItem);
430
431 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
432 keyItem.SetDownTime(now);
433 keyEvent->AddKeyItem(keyItem);
434
435 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
436 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
437 keyEvent->SetActionTime(now);
438 return keyEvent;
439 }
440
441 /**
442 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_02
443 * @tc.desc: Trigger system key immediately.
444 * @tc.type: FUNC
445 * @tc.require:
446 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_02, TestSize.Level1)447 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_02, TestSize.Level1)
448 {
449 CALL_TEST_DEBUG;
450 bool triggered = false;
451 KeyShortcutManager::SystemShortcutKey sysKey {
452 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
453 .finalKey = KeyEvent::KEYCODE_S,
454 .longPressTime = NO_LONG_PRESS,
455 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
456 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
457 triggered = true;
458 },
459 };
460 KeyShortcutManager shortcutMgr;
461 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
462 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
463
464 auto keyEvent = TriggerSystemKey02();
465 ASSERT_TRUE(keyEvent != nullptr);
466 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
467 EXPECT_FALSE(triggered);
468 }
469
TriggerSystemKey03()470 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey03()
471 {
472 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
473 CHKPP(keyEvent);
474 int64_t now = GetSysClockTime();
475 KeyEvent::KeyItem keyItem {};
476 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
477 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
478 keyItem.SetPressed(true);
479 keyEvent->AddKeyItem(keyItem);
480
481 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
482 keyItem.SetDownTime(now);
483 keyEvent->AddKeyItem(keyItem);
484
485 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
486 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
487 keyEvent->SetActionTime(now);
488 return keyEvent;
489 }
490
491 /**
492 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_03
493 * @tc.desc: Long press system key.
494 * @tc.type: FUNC
495 * @tc.require:
496 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_03, TestSize.Level1)497 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_03, TestSize.Level1)
498 {
499 CALL_TEST_DEBUG;
500 std::mutex mutex;
501 std::condition_variable condVar;
502 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
503 KeyShortcutManager::SystemShortcutKey sysKey {
504 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
505 .finalKey = KeyEvent::KEYCODE_S,
506 .longPressTime = DEFAULT_LONG_PRESS_TIME,
507 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
508 .callback = [&](std::shared_ptr<KeyEvent> keyEvent) {
509 std::unique_lock<std::mutex> lock(mutex);
510 keyCode = keyEvent->GetKeyCode();
511 condVar.notify_all();
512 },
513 };
514 std::unique_lock<std::mutex> lock(mutex);
515 KeyShortcutManager shortcutMgr;
516 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
517 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
518
519 auto keyEvent = TriggerSystemKey03();
520 ASSERT_TRUE(keyEvent != nullptr);
521 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
522 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
523 bool cvRet = condVar.wait_for(lock, std::chrono::milliseconds(TWICE_LONG_PRESS_TIME),
524 [&keyCode]() {
525 return (keyCode != KeyEvent::KEYCODE_UNKNOWN);
526 });
527 EXPECT_FALSE(cvRet);
528 EXPECT_NE(keyCode, KeyEvent::KEYCODE_S);
529 }
530
TriggerSystemKey04()531 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey04()
532 {
533 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
534 CHKPP(keyEvent);
535 int64_t now = GetSysClockTime();
536 KeyEvent::KeyItem keyItem {};
537 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
538 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
539 keyItem.SetPressed(true);
540 keyEvent->AddKeyItem(keyItem);
541
542 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
543 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
544 keyEvent->AddKeyItem(keyItem);
545
546 keyItem.SetKeyCode(KeyEvent::KEYCODE_A);
547 keyItem.SetDownTime(now);
548 keyEvent->AddKeyItem(keyItem);
549
550 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
551 keyEvent->SetKeyCode(KeyEvent::KEYCODE_A);
552 keyEvent->SetActionTime(now);
553 return keyEvent;
554 }
555
556 /**
557 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_04
558 * @tc.desc: Reset pending shortcut when press down another key.
559 * @tc.type: FUNC
560 * @tc.require:
561 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_04, TestSize.Level1)562 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_04, TestSize.Level1)
563 {
564 CALL_TEST_DEBUG;
565 std::mutex mutex;
566 std::condition_variable condVar;
567 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
568 KeyShortcutManager::SystemShortcutKey sysKey {
569 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
570 .finalKey = KeyEvent::KEYCODE_S,
571 .longPressTime = DEFAULT_LONG_PRESS_TIME,
572 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
573 .callback = [&](std::shared_ptr<KeyEvent> keyEvent) {
574 std::unique_lock<std::mutex> lock(mutex);
575 keyCode = keyEvent->GetKeyCode();
576 condVar.notify_all();
577 },
578 };
579 std::unique_lock<std::mutex> lock(mutex);
580 KeyShortcutManager shortcutMgr;
581 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
582 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
583
584 auto keyEvent = TriggerSystemKey03();
585 ASSERT_TRUE(keyEvent != nullptr);
586 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
587 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
588
589 keyEvent = TriggerSystemKey04();
590 ASSERT_TRUE(keyEvent != nullptr);
591 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
592 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
593
594 bool cvRet = condVar.wait_for(lock, std::chrono::milliseconds(TWICE_LONG_PRESS_TIME),
595 [&keyCode]() {
596 return (keyCode != KeyEvent::KEYCODE_UNKNOWN);
597 });
598 EXPECT_FALSE(cvRet);
599 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
600 }
601
TriggerSystemKey05()602 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey05()
603 {
604 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
605 CHKPP(keyEvent);
606 int64_t now = GetSysClockTime();
607 KeyEvent::KeyItem keyItem {};
608 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
609 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
610 keyItem.SetPressed(false);
611 keyEvent->AddKeyItem(keyItem);
612
613 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
614 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
615 keyItem.SetPressed(true);
616 keyEvent->AddKeyItem(keyItem);
617
618 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
619 keyEvent->SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
620 keyEvent->SetActionTime(now);
621 return keyEvent;
622 }
623
624 /**
625 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_05
626 * @tc.desc: Reset pending shortcut when lift up dedicated key(s) before running shortcut.
627 * @tc.type: FUNC
628 * @tc.require:
629 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_05, TestSize.Level1)630 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_05, TestSize.Level1)
631 {
632 CALL_TEST_DEBUG;
633 std::mutex mutex;
634 std::condition_variable condVar;
635 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
636 KeyShortcutManager::SystemShortcutKey sysKey {
637 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
638 .finalKey = KeyEvent::KEYCODE_S,
639 .longPressTime = DEFAULT_LONG_PRESS_TIME,
640 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
641 .callback = [&](std::shared_ptr<KeyEvent> keyEvent) {
642 std::unique_lock<std::mutex> lock(mutex);
643 keyCode = keyEvent->GetKeyCode();
644 condVar.notify_all();
645 },
646 };
647 std::unique_lock<std::mutex> lock(mutex);
648 KeyShortcutManager shortcutMgr;
649 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
650 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
651
652 auto keyEvent = TriggerSystemKey03();
653 ASSERT_TRUE(keyEvent != nullptr);
654 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
655 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
656
657 keyEvent = TriggerSystemKey05();
658 ASSERT_TRUE(keyEvent != nullptr);
659 EXPECT_FALSE(shortcutMgr.HandleEvent(keyEvent));
660 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
661
662 bool cvRet = condVar.wait_for(lock, std::chrono::milliseconds(TWICE_LONG_PRESS_TIME),
663 [&keyCode]() {
664 return (keyCode != KeyEvent::KEYCODE_UNKNOWN);
665 });
666 EXPECT_FALSE(cvRet);
667 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
668 }
669
TriggerSystemKey06()670 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey06()
671 {
672 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
673 CHKPP(keyEvent);
674 int64_t now = GetSysClockTime();
675 KeyEvent::KeyItem keyItem {};
676 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
677 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_LONG_PRESS_TIME));
678 keyItem.SetPressed(true);
679 keyEvent->AddKeyItem(keyItem);
680
681 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
682 keyItem.SetDownTime(now - MS2US(DEFAULT_LONG_PRESS_TIME));
683 keyItem.SetPressed(false);
684 keyEvent->AddKeyItem(keyItem);
685
686 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
687 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
688 keyEvent->SetActionTime(now);
689 return keyEvent;
690 }
691
692 /**
693 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_06
694 * @tc.desc: Trigger key-up-trigger system key.
695 * @tc.type: FUNC
696 * @tc.require:
697 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_06, TestSize.Level1)698 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_06, TestSize.Level1)
699 {
700 CALL_TEST_DEBUG;
701 bool triggered = false;
702 KeyShortcutManager::SystemShortcutKey sysKey {
703 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
704 .finalKey = KeyEvent::KEYCODE_S,
705 .longPressTime = DEFAULT_LONG_PRESS_TIME,
706 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_UP,
707 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
708 triggered = true;
709 },
710 };
711 KeyShortcutManager shortcutMgr;
712 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
713 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
714
715 auto keyEvent = TriggerSystemKey06();
716 ASSERT_TRUE(keyEvent != nullptr);
717 ASSERT_FALSE(shortcutMgr.HandleEvent(keyEvent));
718 ASSERT_FALSE(triggered);
719 }
720
TriggerSystemKey07()721 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerSystemKey07()
722 {
723 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
724 CHKPP(keyEvent);
725 int64_t now = GetSysClockTime();
726 KeyEvent::KeyItem keyItem {};
727 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
728 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
729 keyItem.SetPressed(true);
730 keyEvent->AddKeyItem(keyItem);
731
732 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_RIGHT);
733 keyItem.SetDownTime(now);
734 keyEvent->AddKeyItem(keyItem);
735
736 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
737 keyEvent->SetKeyCode(KeyEvent::KEYCODE_SHIFT_RIGHT);
738 keyEvent->SetActionTime(now);
739 return keyEvent;
740 }
741
742 /**
743 * @tc.name: KeyShortcutManagerTest_TriggerSystemKey_07
744 * @tc.desc: Trigger pure-modifiers system key.
745 * @tc.type: FUNC
746 * @tc.require:
747 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_07, TestSize.Level1)748 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerSystemKey_07, TestSize.Level1)
749 {
750 CALL_TEST_DEBUG;
751 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
752 KeyShortcutManager::SystemShortcutKey sysKey {
753 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
754 .finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS,
755 .longPressTime = NO_LONG_PRESS,
756 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
757 .callback = [&keyCode](std::shared_ptr<KeyEvent> keyEvent) {
758 keyCode = keyEvent->GetKeyCode();
759 },
760 };
761 KeyShortcutManager shortcutMgr;
762 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
763 ASSERT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
764
765 auto keyEvent = TriggerSystemKey07();
766 ASSERT_TRUE(keyEvent != nullptr);
767 EXPECT_TRUE(shortcutMgr.HandleEvent(keyEvent));
768 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_SHIFT_RIGHT);
769 }
770
TriggerGlobalKey01()771 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerGlobalKey01()
772 {
773 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
774 CHKPP(keyEvent);
775 int64_t now = GetSysClockTime();
776 KeyEvent::KeyItem keyItem {};
777 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
778 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
779 keyItem.SetPressed(true);
780 keyEvent->AddKeyItem(keyItem);
781
782 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
783 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
784 keyEvent->AddKeyItem(keyItem);
785
786 keyItem.SetKeyCode(KeyEvent::KEYCODE_A);
787 keyItem.SetDownTime(now);
788 keyEvent->AddKeyItem(keyItem);
789
790 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
791 keyEvent->SetKeyCode(KeyEvent::KEYCODE_A);
792 keyEvent->SetActionTime(now);
793 return keyEvent;
794 }
795
TriggerGlobalKey0101()796 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::TriggerGlobalKey0101()
797 {
798 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
799 CHKPP(keyEvent);
800 int64_t now = GetSysClockTime();
801 KeyEvent::KeyItem keyItem {};
802 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
803 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
804 keyItem.SetPressed(true);
805 keyEvent->AddKeyItem(keyItem);
806
807 keyItem.SetKeyCode(KeyEvent::KEYCODE_SHIFT_LEFT);
808 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD + DEFAULT_SAMPLING_PERIOD));
809 keyEvent->AddKeyItem(keyItem);
810
811 keyItem.SetKeyCode(KeyEvent::KEYCODE_A);
812 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
813 keyEvent->AddKeyItem(keyItem);
814
815 keyItem.SetKeyCode(KeyEvent::KEYCODE_D);
816 keyItem.SetDownTime(now);
817 keyEvent->AddKeyItem(keyItem);
818
819 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
820 keyEvent->SetKeyCode(KeyEvent::KEYCODE_D);
821 keyEvent->SetActionTime(now);
822 return keyEvent;
823 }
824
825 /**
826 * @tc.name: KeyShortcutManagerTest_TriggerGlobalKey_01
827 * @tc.desc: Trigger key-up-trigger system key.
828 * @tc.type: FUNC
829 * @tc.require:
830 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerGlobalKey_01, TestSize.Level1)831 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerGlobalKey_01, TestSize.Level1)
832 {
833 CALL_TEST_DEBUG;
834 int32_t keyCode1 = KeyEvent::KEYCODE_UNKNOWN;
835 KeyShortcutManager::HotKey globalKey1 {
836 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_LEFT },
837 .finalKey = KeyEvent::KEYCODE_A,
838 .callback = [&keyCode1](std::shared_ptr<KeyEvent> keyEvent) {
839 keyCode1 = keyEvent->GetKeyCode();
840 },
841 };
842 KeyShortcutManager shortcutMgr;
843 int32_t shortcutId = shortcutMgr.RegisterHotKey(globalKey1);
844 ASSERT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
845
846 int32_t keyCode2 = KeyEvent::KEYCODE_UNKNOWN;
847 KeyShortcutManager::HotKey globalKey2 {
848 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT, KeyEvent::KEYCODE_SHIFT_RIGHT },
849 .finalKey = KeyEvent::KEYCODE_D,
850 .callback = [&keyCode2](std::shared_ptr<KeyEvent> keyEvent) {
851 keyCode2 = keyEvent->GetKeyCode();
852 },
853 };
854 shortcutId = shortcutMgr.RegisterHotKey(globalKey2);
855 ASSERT_TRUE(shortcutId >= BASE_SHORTCUT_ID);
856
857 auto keyEvent1 = TriggerGlobalKey01();
858 ASSERT_TRUE(keyEvent1 != nullptr);
859 EXPECT_TRUE(shortcutMgr.HandleEvent(keyEvent1));
860 EXPECT_EQ(keyCode1, KeyEvent::KEYCODE_A);
861 EXPECT_EQ(keyCode2, KeyEvent::KEYCODE_UNKNOWN);
862
863 auto keyEvent2 = TriggerGlobalKey0101();
864 ASSERT_TRUE(keyEvent2 != nullptr);
865 EXPECT_TRUE(shortcutMgr.HandleEvent(keyEvent2));
866 EXPECT_EQ(keyCode2, KeyEvent::KEYCODE_D);
867 }
868
ResetAllTriggering()869 std::shared_ptr<KeyEvent> KeyShortcutManagerTest::ResetAllTriggering()
870 {
871 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
872 CHKPP(keyEvent);
873 int64_t now = GetSysClockTime();
874 KeyEvent::KeyItem keyItem {};
875 keyItem.SetKeyCode(KeyEvent::KEYCODE_CTRL_LEFT);
876 keyItem.SetDownTime(now - MS2US(DEFAULT_SAMPLING_PERIOD));
877 keyItem.SetPressed(true);
878 keyEvent->AddKeyItem(keyItem);
879
880 keyItem.SetKeyCode(KeyEvent::KEYCODE_S);
881 keyItem.SetDownTime(now);
882 keyEvent->AddKeyItem(keyItem);
883
884 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
885 keyEvent->SetKeyCode(KeyEvent::KEYCODE_S);
886 keyEvent->SetActionTime(now);
887 return keyEvent;
888 }
889
890 /**
891 * @tc.name: KeyShortcutManagerTest_ResetAllTriggering_01
892 * @tc.desc: Trigger key-up-trigger system key.
893 * @tc.type: FUNC
894 * @tc.require:
895 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ResetAllTriggering_01, TestSize.Level1)896 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ResetAllTriggering_01, TestSize.Level1)
897 {
898 int32_t keyCode = KeyEvent::KEYCODE_UNKNOWN;
899 KeyShortcutManager::SystemShortcutKey sysKey {
900 .modifiers = { KeyEvent::KEYCODE_CTRL_LEFT },
901 .finalKey = KeyEvent::KEYCODE_S,
902 .longPressTime = DEFAULT_LONG_PRESS_TIME,
903 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
904 .callback = [&keyCode](std::shared_ptr<KeyEvent> keyEvent) {
905 keyCode = keyEvent->GetKeyCode();
906 },
907 };
908 KeyShortcutManager shortcutMgr;
909 int32_t shortcutId = shortcutMgr.RegisterSystemKey(sysKey);
910 ASSERT_FALSE(shortcutId >= BASE_SHORTCUT_ID);
911
912 auto keyEvent = ResetAllTriggering();
913 ASSERT_TRUE(keyEvent != nullptr);
914 ASSERT_FALSE(shortcutMgr.HandleEvent(keyEvent));
915 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
916 shortcutMgr.ResetAll();
917 std::this_thread::sleep_for(std::chrono::milliseconds(TWICE_LONG_PRESS_TIME));
918 EXPECT_EQ(keyCode, KeyEvent::KEYCODE_UNKNOWN);
919 }
920
921 /**
922 * @tc.name: KeyShortcutManagerTest_GetInstance_01
923 * @tc.desc: Test the funcation GetInstance
924 * @tc.type: FUNC
925 * @tc.require:
926 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GetInstance_01, TestSize.Level1)927 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_GetInstance_01, TestSize.Level1)
928 {
929 CALL_TEST_DEBUG;
930 KeyShortcutManager shortcutMgr;
931 shortcutMgr.instance_ = nullptr;
932 EXPECT_NO_FATAL_FAILURE(shortcutMgr.GetInstance());
933 shortcutMgr.instance_ = std::make_shared<KeyShortcutManager>();
934 ASSERT_TRUE(shortcutMgr.instance_ != nullptr);
935 EXPECT_NO_FATAL_FAILURE(shortcutMgr.GetInstance());
936 }
937
938 /**
939 * @tc.name: KeyShortcutManagerTest_UnregisterHotKey_01
940 * @tc.desc: Test the funcation UnregisterHotKey
941 * @tc.type: FUNC
942 * @tc.require:
943 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UnregisterHotKey_01, TestSize.Level1)944 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UnregisterHotKey_01, TestSize.Level1)
945 {
946 CALL_TEST_DEBUG;
947 bool triggered = false;
948 KeyShortcutManager::KeyShortcut keyShortcut {
949 .modifiers = KeyEvent::KEYCODE_META_LEFT,
950 .finalKey = KeyEvent::KEYCODE_T,
951 .longPressTime = DEFAULT_LONG_PRESS_TIME,
952 .triggerType = KeyShortcutManager::SHORTCUT_TRIGGER_TYPE_DOWN,
953 .callback = [&triggered](std::shared_ptr<KeyEvent> keyEvent) {
954 triggered = true;
955 },
956 };
957 KeyShortcutManager shortcutMgr;
958 int32_t shortcutId = 100;
959 shortcutMgr.shortcuts_[100] = keyShortcut;
960 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterHotKey(shortcutId));
961 shortcutId = 66;
962 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UnregisterHotKey(shortcutId));
963 }
964
965 /**
966 * @tc.name: KeyShortcutManagerTest_UpdateShortcutConsumed_01
967 * @tc.desc: Test the funcation UpdateShortcutConsumed
968 * @tc.type: FUNC
969 * @tc.require:
970 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UpdateShortcutConsumed_01, TestSize.Level1)971 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_UpdateShortcutConsumed_01, TestSize.Level1)
972 {
973 CALL_TEST_DEBUG;
974 KeyShortcutManager shortcutMgr;
975 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
976 ASSERT_NE(keyEvent, nullptr);
977 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
978 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UpdateShortcutConsumed(keyEvent));
979 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
980 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UpdateShortcutConsumed(keyEvent));
981 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UNKNOWN);
982 EXPECT_NO_FATAL_FAILURE(shortcutMgr.UpdateShortcutConsumed(keyEvent));
983 }
984
985 /**
986 * @tc.name: KeyShortcutManagerTest_MarkShortcutConsumed_01
987 * @tc.desc: Test the funcation MarkShortcutConsumed(ShortcutKey)
988 * @tc.type: FUNC
989 * @tc.require:
990 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_MarkShortcutConsumed_01, TestSize.Level1)991 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_MarkShortcutConsumed_01, TestSize.Level1)
992 {
993 CALL_TEST_DEBUG;
994 KeyShortcutManager shortcutMgr;
995 ShortcutKey shortcut;
996 shortcut.preKeys = {1, 2, 3};
997 shortcut.businessId = "businessId";
998 shortcut.statusConfig = "statusConfig";
999 shortcut.statusConfigValue = true;
1000 shortcut.finalKey = 1;
1001 shortcut.keyDownDuration = 2;
1002 shortcut.triggerType = KeyEvent::KEY_ACTION_DOWN;
1003 shortcut.timerId = 1;
1004 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1005 shortcut.triggerType = KeyEvent::KEY_ACTION_CANCEL;
1006 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1007 shortcut.triggerType = KeyEvent::KEY_ACTION_UP;
1008 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1009 }
1010
1011 /**
1012 * @tc.name: KeyShortcutManagerTest_MarkShortcutConsumed_001
1013 * @tc.desc: Test the funcation MarkShortcutConsumed(KeyOption)
1014 * @tc.type: FUNC
1015 * @tc.require:
1016 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_MarkShortcutConsumed_001, TestSize.Level1)1017 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_MarkShortcutConsumed_001, TestSize.Level1)
1018 {
1019 CALL_TEST_DEBUG;
1020 KeyShortcutManager shortcutMgr;
1021 KeyOption shortcut;
1022 std::set<int32_t> preKeys = {1, 2, 3, 4, 5};
1023 shortcut.SetPreKeys(preKeys);
1024 shortcut.SetFinalKeyDown(true);
1025 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1026 shortcut.SetFinalKeyDown(false);
1027 EXPECT_NO_FATAL_FAILURE(shortcutMgr.MarkShortcutConsumed(shortcut));
1028 }
1029
1030 /**
1031 * @tc.name: KeyShortcutManagerTest_ResetTriggering_01
1032 * @tc.desc: Test the funcation ResetTriggering
1033 * @tc.type: FUNC
1034 * @tc.require:
1035 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ResetTriggering_01, TestSize.Level1)1036 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_ResetTriggering_01, TestSize.Level1)
1037 {
1038 CALL_TEST_DEBUG;
1039 KeyShortcutManager shortcutMgr;
1040 int32_t shortcutId = 1;
1041 shortcutMgr.triggering_[1] = 1;
1042 EXPECT_NO_FATAL_FAILURE(shortcutMgr.ResetTriggering(shortcutId));
1043 shortcutId = 10;
1044 EXPECT_NO_FATAL_FAILURE(shortcutMgr.ResetTriggering(shortcutId));
1045 }
1046
1047 /**
1048 * @tc.name: KeyShortcutManagerTest_HandleEvent_001
1049 * @tc.desc: Test the funcation HandleEvent
1050 * @tc.type: FUNC
1051 * @tc.require:
1052 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleEvent_001, TestSize.Level1)1053 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleEvent_001, TestSize.Level1)
1054 {
1055 CALL_TEST_DEBUG;
1056 KeyShortcutManager shortcutMgr;
1057 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1058 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_CANCEL);
1059 bool ret = shortcutMgr.HandleEvent(keyEvent);
1060 ASSERT_EQ(ret, false);
1061 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UNKNOWN);
1062 ret = shortcutMgr.HandleEvent(keyEvent);
1063 ASSERT_EQ(ret, false);
1064 }
1065
1066 /**
1067 * @tc.name: KeyShortcutManagerTest_WillResetOnKeyDown_001
1068 * @tc.desc: Test the funcation WillResetOnKeyDown
1069 * @tc.type: FUNC
1070 * @tc.require:
1071 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_WillResetOnKeyDown_001, TestSize.Level1)1072 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_WillResetOnKeyDown_001, TestSize.Level1)
1073 {
1074 CALL_TEST_DEBUG;
1075 KeyShortcutManager shortcutMgr;
1076 int32_t keyCode = 1;
1077 KeyShortcutManager::KeyShortcut shortcut;
1078 shortcut.finalKey = 1;
1079 bool ret = shortcutMgr.WillResetOnKeyDown(keyCode, shortcut);
1080 ASSERT_EQ(ret, false);
1081 keyCode = 3;
1082 ret = shortcutMgr.WillResetOnKeyDown(keyCode, shortcut);
1083 ASSERT_EQ(ret, true);
1084 }
1085
1086 /**
1087 * @tc.name: KeyShortcutManagerTest_WillResetOnKeyUp_001
1088 * @tc.desc: Test the funcation WillResetOnKeyUp
1089 * @tc.type: FUNC
1090 * @tc.require:
1091 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_WillResetOnKeyUp_001, TestSize.Level1)1092 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_WillResetOnKeyUp_001, TestSize.Level1)
1093 {
1094 CALL_TEST_DEBUG;
1095 KeyShortcutManager shortcutMgr;
1096 int32_t keyCode = 1;
1097 KeyShortcutManager::KeyShortcut shortcut;
1098 shortcut.finalKey = 1;
1099 bool ret = shortcutMgr.WillResetOnKeyUp(keyCode, shortcut);
1100 ASSERT_EQ(ret, true);
1101 keyCode = 3;
1102 ret = shortcutMgr.WillResetOnKeyUp(keyCode, shortcut);
1103 ASSERT_EQ(ret, false);
1104 }
1105
myCallback(std::shared_ptr<KeyEvent> event)1106 void myCallback(std::shared_ptr<KeyEvent> event)
1107 {
1108 std::cout << "Callback triggered!" << std::endl;
1109 }
1110
1111 /**
1112 * @tc.name: KeyShortcutManagerTest_TriggerUp_001
1113 * @tc.desc: Test the funcation TriggerUp
1114 * @tc.type: FUNC
1115 * @tc.require:
1116 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_001, TestSize.Level1)1117 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_001, TestSize.Level1)
1118 {
1119 CALL_TEST_DEBUG;
1120 KeyShortcutManager shortcutMgr;
1121 int32_t shortcutId = 1;
1122 KeyShortcutManager::KeyShortcut shortcut;
1123 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1124 shortcut.longPressTime = -1;
1125 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1126 shortcut.callback = myCallback;
1127 shortcut.callback(keyEvent);
1128 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1129 }
1130
1131 /**
1132 * @tc.name: KeyShortcutManagerTest_TriggerUp_002
1133 * @tc.desc: Test the funcation TriggerUp
1134 * @tc.type: FUNC
1135 * @tc.require:
1136 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_002, TestSize.Level1)1137 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerUp_002, TestSize.Level1)
1138 {
1139 CALL_TEST_DEBUG;
1140 KeyShortcutManager shortcutMgr;
1141 int32_t shortcutId = 1;
1142 KeyShortcutManager::KeyShortcut shortcut;
1143 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1144 shortcut.longPressTime = 1000;
1145 KeyEvent::KeyItem item;
1146 item.SetKeyCode(-1);
1147 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1148 item.SetKeyCode(5);
1149 keyEvent->SetActionTime(3);
1150 item.SetDownTime(1);
1151 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerUp(keyEvent, shortcutId, shortcut));
1152 }
1153
1154 /**
1155 * @tc.name: KeyShortcutManagerTest_RunShortcut_001
1156 * @tc.desc: Test the funcation RunShortcut
1157 * @tc.type: FUNC
1158 * @tc.require:
1159 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_001, TestSize.Level1)1160 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_001, TestSize.Level1)
1161 {
1162 CALL_TEST_DEBUG;
1163 KeyShortcutManager shortcutMgr;
1164 int32_t shortcutId = 1;
1165 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1166 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1167 }
1168
1169 /**
1170 * @tc.name: KeyShortcutManagerTest_RunShortcut_002
1171 * @tc.desc: Test the funcation RunShortcut
1172 * @tc.type: FUNC
1173 * @tc.require:
1174 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_002, TestSize.Level1)1175 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_RunShortcut_002, TestSize.Level1)
1176 {
1177 CALL_TEST_DEBUG;
1178 KeyShortcutManager shortcutMgr;
1179 int32_t shortcutId = 1;
1180 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1181 KeyShortcutManager::KeyShortcut key1;
1182 shortcutMgr.shortcuts_[1] = key1;
1183 EXPECT_NO_FATAL_FAILURE(shortcutMgr.RunShortcut(keyEvent, shortcutId));
1184 }
1185
1186 /**
1187 * @tc.name: KeyShortcutManagerTest_TriggerDown_002
1188 * @tc.desc: Test the funcation TriggerDown
1189 * @tc.type: FUNC
1190 * @tc.require:
1191 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerDown_002, TestSize.Level1)1192 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_TriggerDown_002, TestSize.Level1)
1193 {
1194 CALL_TEST_DEBUG;
1195 KeyShortcutManager shortcutMgr;
1196 int32_t shortcutId = 1;
1197 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1198 KeyShortcutManager::KeyShortcut shortcut;
1199 shortcut.longPressTime = -1;
1200 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1201 shortcut.callback = myCallback;
1202 shortcut.callback(keyEvent);
1203 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1204 shortcut.longPressTime = 2;
1205 shortcutMgr.triggering_[1] = 100;
1206 EXPECT_NO_FATAL_FAILURE(shortcutMgr.TriggerDown(keyEvent, shortcutId, shortcut));
1207 }
1208
1209 /**
1210 * @tc.name: KeyShortcutManagerTest_HandleKeyUp_001
1211 * @tc.desc: Test the funcation HandleKeyUp
1212 * @tc.type: FUNC
1213 * @tc.require:
1214 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_001, TestSize.Level1)1215 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_001, TestSize.Level1)
1216 {
1217 CALL_TEST_DEBUG;
1218 KeyShortcutManager shortcutMgr;
1219 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1220 KeyShortcutManager::KeyShortcut shortcut;
1221 shortcut.modifiers = 0x1;
1222 shortcut.finalKey = 0x2;
1223 shortcut.longPressTime = 500;
1224 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_DOWN;
1225 shortcut.session = 1;
1226 shortcut.callback = myCallback;
1227 shortcut.callback(keyEvent);
1228 shortcutMgr.shortcuts_[1] = shortcut;
1229 bool ret = shortcutMgr.HandleKeyUp(keyEvent);
1230 EXPECT_EQ(ret, false);
1231 }
1232
1233 /**
1234 * @tc.name: KeyShortcutManagerTest_HandleKeyUp_002
1235 * @tc.desc: Test the funcation HandleKeyUp
1236 * @tc.type: FUNC
1237 * @tc.require:
1238 */
HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_002, TestSize.Level1)1239 HWTEST_F(KeyShortcutManagerTest, KeyShortcutManagerTest_HandleKeyUp_002, TestSize.Level1)
1240 {
1241 CALL_TEST_DEBUG;
1242 KeyShortcutManager shortcutMgr;
1243 std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
1244 KeyShortcutManager::KeyShortcut shortcut;
1245 shortcut.modifiers = 0x1;
1246 shortcut.finalKey = 0x2;
1247 shortcut.longPressTime = 500;
1248 shortcut.triggerType = KeyShortcutManager::ShortcutTriggerType::SHORTCUT_TRIGGER_TYPE_UP;
1249 shortcut.session = 1;
1250 shortcut.callback = myCallback;
1251 shortcut.callback(keyEvent);
1252 shortcutMgr.shortcuts_[1] = shortcut;
1253 bool ret = shortcutMgr.HandleKeyUp(keyEvent);
1254 EXPECT_EQ(ret, false);
1255 shortcut.finalKey = KeyShortcutManager::SHORTCUT_PURE_MODIFIERS;
1256 shortcutMgr.shortcuts_[1] = shortcut;
1257 keyEvent->SetKeyCode(2046);
1258 ret = shortcutMgr.HandleKeyUp(keyEvent);
1259 EXPECT_EQ(ret, true);
1260 }
1261 } // namespace MMI
1262 } // namespace OHOS
1263