From cda800a78a05b50b3c13a002cdd531a05a9a4106 Mon Sep 17 00:00:00 2001 From: Gold87 <91761103+Gold872@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:46:19 -0400 Subject: [PATCH] Added hotkey manager test --- test/services/hotkey_manager_test.dart | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/services/hotkey_manager_test.dart diff --git a/test/services/hotkey_manager_test.dart b/test/services/hotkey_manager_test.dart new file mode 100644 index 00000000..8825d759 --- /dev/null +++ b/test/services/hotkey_manager_test.dart @@ -0,0 +1,72 @@ +import 'package:flutter/services.dart'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; + +import 'package:elastic_dashboard/services/hotkey_manager.dart'; + +class MockShortcutCallback extends Mock { + void callback(); +} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + tearDown(() { + hotKeyManager.tearDown(); + HardwareKeyboard.instance.clearState(); + }); + + test('Shortcut no modifiers', () async { + MockShortcutCallback mockCallback = MockShortcutCallback(); + + hotKeyManager.register( + HotKey(LogicalKeyboardKey.keyA), + callback: mockCallback.callback, + ); + + await simulateKeyDownEvent(LogicalKeyboardKey.keyA); + + verifyNever(mockCallback.callback()); + + await simulateKeyUpEvent(LogicalKeyboardKey.keyA); + + verify(mockCallback.callback()).called(1); + + await simulateKeyDownEvent(LogicalKeyboardKey.control); + + await simulateKeyDownEvent(LogicalKeyboardKey.keyA); + await simulateKeyUpEvent(LogicalKeyboardKey.keyA); + + verify(mockCallback.callback()).called(1); + }); + + test('Shortcut with modifiers', () async { + MockShortcutCallback mockCallback = MockShortcutCallback(); + + hotKeyManager.register( + HotKey( + LogicalKeyboardKey.keyA, + modifiers: [KeyModifier.control, KeyModifier.shift, KeyModifier.alt], + ), + callback: mockCallback.callback, + ); + + await simulateKeyDownEvent(LogicalKeyboardKey.keyA); + + verifyNever(mockCallback.callback()); + + await simulateKeyUpEvent(LogicalKeyboardKey.keyA); + + verifyNever(mockCallback.callback()); + + await simulateKeyDownEvent(LogicalKeyboardKey.control); + await simulateKeyDownEvent(LogicalKeyboardKey.shift); + await simulateKeyDownEvent(LogicalKeyboardKey.alt); + + await simulateKeyDownEvent(LogicalKeyboardKey.keyA); + await simulateKeyUpEvent(LogicalKeyboardKey.keyA); + + verify(mockCallback.callback()).called(1); + }); +}