* Add GitHub Actions CI/CD workflows and status badges * Fix CI workflow dependency installation * Fix CI workflow: remove pip caching and add explicit requirements.txt * Fix CI workflow: add system dependencies for PyGObject and pycairo * Refactor code for improved readability and consistency - Cleaned up whitespace and formatting in audio_feedback.py, config_manager.py, keyboard_shortcuts.py, tray_indicator.py, run_basic_tests.py, and test_basic.py. - Enhanced the structure of the code by aligning comments and ensuring consistent indentation. - Updated function definitions and calls to maintain a uniform style across the codebase. - Improved logging messages for better clarity during debugging. - Ensured all files end with a newline for better compatibility with various tools. * Refactor imports for consistency and clarity across multiple modules * Fix test imports by setting PYTHONPATH and adding conftest.py * Update CI workflow to use Python 3.12 and ensure proper import path in tests * Fix CI: Add GObject Introspection development dependencies * Update CI: Use virtual environment like local setup * Fix CI * Update README and setup.py: Change Python version to 3.8 and correct author email * Restructure tests for better organization and maintainability * Fix formatting * Update CI workflow to remove redundant test step * Fix CI tests by properly mocking VOSK model loading * Update CI and Tests * Simplify
94 lines
3.4 KiB
Python
94 lines
3.4 KiB
Python
"""
|
|
Tests for the keyboard shortcuts functionality.
|
|
"""
|
|
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
class TestKeyboardShortcuts(unittest.TestCase):
|
|
"""Test cases for keyboard shortcuts functionality."""
|
|
|
|
def test_shortcut_parsing(self):
|
|
"""Test that keyboard shortcut parsing works correctly."""
|
|
|
|
# Define a mock KeyboardShortcutManager class to avoid importing pynput
|
|
class MockKeyboardShortcutManager:
|
|
def __init__(self):
|
|
self.shortcuts = {}
|
|
self.current_keys = set()
|
|
|
|
def parse_shortcut(self, shortcut_str):
|
|
"""Parse a shortcut string into a list of key identifiers."""
|
|
return shortcut_str.lower().split("+")
|
|
|
|
def register_shortcut(self, name, shortcut, callback):
|
|
"""Register a keyboard shortcut."""
|
|
self.shortcuts[name] = {
|
|
"keys": self.parse_shortcut(shortcut),
|
|
"callback": callback,
|
|
}
|
|
return True
|
|
|
|
# Create an instance of our mock manager
|
|
shortcut_manager = MockKeyboardShortcutManager()
|
|
|
|
# Test shortcut parsing
|
|
shortcut_manager.register_shortcut("test", "ctrl+shift+t", lambda: None)
|
|
assert "test" in shortcut_manager.shortcuts
|
|
assert shortcut_manager.shortcuts["test"]["keys"] == ["ctrl", "shift", "t"]
|
|
|
|
# Test multiple shortcut registration
|
|
callback_called = False
|
|
|
|
def test_callback():
|
|
nonlocal callback_called
|
|
callback_called = True
|
|
|
|
shortcut_manager.register_shortcut(
|
|
"toggle_dictation", "alt+shift+v", test_callback
|
|
)
|
|
assert "toggle_dictation" in shortcut_manager.shortcuts
|
|
assert shortcut_manager.shortcuts["toggle_dictation"]["keys"] == [
|
|
"alt",
|
|
"shift",
|
|
"v",
|
|
]
|
|
assert (
|
|
shortcut_manager.shortcuts["toggle_dictation"]["callback"] == test_callback
|
|
)
|
|
|
|
# Mock test for the detection algorithm - without the dependency
|
|
def test_shortcut_detection_logic(self):
|
|
"""Test the logic for detecting keyboard shortcuts."""
|
|
|
|
# Define a simple shortcut detection algorithm
|
|
def check_shortcut_triggered(current_keys, shortcut_keys):
|
|
"""Check if the currently pressed keys match a shortcut."""
|
|
return set(shortcut_keys).issubset(set(current_keys)) and len(
|
|
current_keys
|
|
) == len(shortcut_keys)
|
|
|
|
# Test detection with various key combinations
|
|
# Case 1: All keys in the shortcut are pressed
|
|
current_keys = ["alt", "shift", "v"]
|
|
shortcut_keys = ["alt", "shift", "v"]
|
|
assert check_shortcut_triggered(current_keys, shortcut_keys) is True
|
|
|
|
# Case 2: Some keys in the shortcut are pressed
|
|
current_keys = ["alt", "v"]
|
|
shortcut_keys = ["alt", "shift", "v"]
|
|
assert check_shortcut_triggered(current_keys, shortcut_keys) is False
|
|
|
|
# Case 3: Extra keys are pressed
|
|
current_keys = ["alt", "shift", "v", "ctrl"]
|
|
shortcut_keys = ["alt", "shift", "v"]
|
|
assert check_shortcut_triggered(current_keys, shortcut_keys) is False
|
|
|
|
# Case 4: Different shortcut is pressed
|
|
current_keys = ["ctrl", "shift", "t"]
|
|
shortcut_keys = ["alt", "shift", "v"]
|
|
assert check_shortcut_triggered(current_keys, shortcut_keys) is False
|