Skip to content

Commit

Permalink
Added hotkey manager test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Jul 19, 2024
1 parent c36cb6d commit cda800a
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/services/hotkey_manager_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}

0 comments on commit cda800a

Please sign in to comment.