* feat: prepare for v0.2.0-alpha release * feat: prepare for v0.2.0-alpha release * chore: remove CHANGELOG.md, use GitHub Releases instead * refactor: use existing About dialog instead of Settings section * style: enhance About dialog UI readability * style: fix formatting with black and isort * feat: show VAD and silence timeout settings for Whisper engine - Renamed 'VOSK Settings' to 'Recognition Settings' - VAD Sensitivity and Silence Timeout now apply to both engines - Added tooltips explaining each setting - VOSK-specific model info only shown when VOSK selected * fix: resolve overlapping UI elements in settings dialog * fix: save VAD and silence timeout settings for both engines Previously these settings were only saved when VOSK was selected. Now they persist correctly for Whisper as well. * fix: show download progress when selecting undownloaded model Previously when auto-applying settings, models would download silently in the background without showing the progress dialog. Now: - Download progress dialog appears for undownloaded models - Model icons update correctly after download completes * feat: add download progress, speed, ETA and cancel button - Show real download progress percentage for VOSK models - Display download speed (MB/s) and estimated time remaining - Add Cancel button to abort downloads - Update status during extraction phase - Whisper downloads still pulse (uses internal downloader) * fix: defer model download until needed, don't block startup - App now starts instantly even if models aren't downloaded - Models are downloaded on-demand when selected in Settings - If user tries to dictate without a model, plays error sound - Added model_ready property to check if engine is usable * fix: improve first-run UX without downloaded models - Settings dialog now defaults to smallest available model, not saved config - Only show saved model if it's actually downloaded - Add desktop notification when trying to dictate without a model - Prevents auto-downloading large models on first launch * feat: add download progress tracking for Whisper models - Implement custom Whisper model download with progress/speed/ETA - Uses OpenAI's CDN URLs directly instead of whisper.load_model() - Progress callback works the same as VOSK downloads - Supports cancellation during download * fix: enable download progress callback for Whisper models Progress callback was only being set for VOSK downloads, not Whisper. Now both engines show real-time progress, speed, and ETA. * fix: update tests for defer_download and config manager mocking - test_main: Mock ConfigManager to prevent saved config from overriding test values - test_settings_dialog: Mock ModelDownloadDialog to prevent GTK init errors - test_speech_recognition: Use proper MockVoskModule class instead of MagicMock, pass defer_download=False for download test - test_whisper_support: Pass defer_download=False to trigger model loading in test * fix: resolve test failures for CI - test_main: Fix ConfigManager patch path to use source module - test_settings_dialog: Mock _is_vosk_model_downloaded and _is_whisper_model_downloaded to skip download flow, add missing dialog attributes - test_speech_recognition: Replace recognizer with properly configured mock that returns valid JSON, fixing test isolation issues when running all tests together * fix: mock _download_whisper_model in whisper test to avoid requests exception handling issue * fix: add gir1.2-girepository-2.0 dep for PyGObject on Ubuntu 24.04 - Add missing girepository-2.0 package for newer Ubuntu runners - Use --no-build-isolation for PyGObject to use system packages * fix: skip PyGObject pip install, use system python3-gi package - Use --no-deps for editable install to skip PyGObject - Install other deps explicitly without PyGObject - Add python3-gi-cairo for complete GTK support - Removes problematic girepository-2.0 build requirement * fix: install PyGObject via pip with proper build deps - Remove python3-gi/python3-gi-cairo (doesn't work with setup-python) - Install PyGObject from pip after installing build dependencies - Simplify venv setup without --system-site-packages hack * Fix GObject * fix: set GI_TYPELIB_PATH for pip-installed PyGObject to find AppIndicator3
307 lines
12 KiB
Python
307 lines
12 KiB
Python
"""
|
|
Tests for system tray indicator functionality.
|
|
"""
|
|
|
|
import os
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
# We need to patch modules BEFORE they are imported
|
|
@patch("gi.repository.AppIndicator3", MagicMock())
|
|
@patch("gi.repository.Gtk", MagicMock())
|
|
@patch("gi.repository.GLib", MagicMock())
|
|
@patch("gi.repository.GObject", MagicMock())
|
|
@patch("gi.repository.GdkPixbuf", MagicMock())
|
|
@patch("gi.require_version", MagicMock())
|
|
class TestTrayIndicator(unittest.TestCase):
|
|
"""Test cases for the tray indicator."""
|
|
|
|
def setUp(self):
|
|
"""Set up test environment before each test."""
|
|
# Patch threading module before any imports
|
|
self.thread_patcher = patch("threading.Thread")
|
|
self.mock_thread_class = self.thread_patcher.start()
|
|
|
|
# Make Thread.start() actually run the target function
|
|
def fake_start(self_thread):
|
|
if self_thread.target:
|
|
args = self_thread.args or ()
|
|
kwargs = self_thread.kwargs or {}
|
|
self_thread.target(*args, **kwargs)
|
|
|
|
self.mock_thread_class.return_value.start = fake_start
|
|
|
|
# Patch the pynput keyboard module
|
|
self.keyboard_patcher = patch("vocalinux.ui.keyboard_shortcuts.keyboard", create=True)
|
|
self.mock_keyboard = self.keyboard_patcher.start()
|
|
|
|
# Patch keyboard module's KEYBOARD_AVAILABLE constant before importing
|
|
self.keyboard_available_patcher = patch(
|
|
"vocalinux.ui.keyboard_shortcuts.KEYBOARD_AVAILABLE", True
|
|
)
|
|
self.mock_keyboard_available = self.keyboard_available_patcher.start()
|
|
|
|
# Import here after patching for proper mocking
|
|
from vocalinux.common_types import RecognitionState
|
|
|
|
# Setup mock keyboard listener that works properly
|
|
self.mock_listener = MagicMock()
|
|
self.mock_listener.is_alive.return_value = True
|
|
self.mock_keyboard.Listener.return_value = self.mock_listener
|
|
|
|
# Define some fake keyboard keys for testing
|
|
self.mock_keyboard.Key = MagicMock()
|
|
self.mock_keyboard.Key.ctrl = "ctrl"
|
|
self.mock_keyboard.Key.ctrl_l = "ctrl_l"
|
|
self.mock_keyboard.Key.ctrl_r = "ctrl_r"
|
|
|
|
# Create our mock for the shortcut manager
|
|
self.mock_ksm = MagicMock()
|
|
|
|
# Patch keyboard shortcuts manager to return our mock
|
|
self.ksm_patcher = patch("vocalinux.ui.keyboard_shortcuts.KeyboardShortcutManager")
|
|
self.mock_ksm_class = self.ksm_patcher.start()
|
|
self.mock_ksm_class.return_value = self.mock_ksm
|
|
|
|
# Patch the settings dialog BEFORE importing TrayIndicator
|
|
self.patcher_settings_dialog = patch("vocalinux.ui.tray_indicator.SettingsDialog")
|
|
self.mock_settings_dialog_class = self.patcher_settings_dialog.start()
|
|
self.mock_settings_dialog = MagicMock()
|
|
self.mock_settings_dialog_class.return_value = self.mock_settings_dialog
|
|
|
|
# Now import TrayIndicator after all patches are applied
|
|
from vocalinux.ui.tray_indicator import TrayIndicator
|
|
|
|
self.RecognitionState = RecognitionState
|
|
|
|
# Create mocks for dependencies
|
|
self.mock_speech_engine = MagicMock()
|
|
self.mock_speech_engine.state = RecognitionState.IDLE
|
|
self.mock_text_injector = MagicMock()
|
|
self.mock_config_manager = MagicMock() # Mock the ConfigManager
|
|
|
|
# Patch specific methods we need to control
|
|
self.patcher_glib_idle = patch("gi.repository.GLib.idle_add")
|
|
self.mock_glib_idle = self.patcher_glib_idle.start()
|
|
# Make idle_add execute the function directly
|
|
self.mock_glib_idle.side_effect = lambda func, *args: func(*args) or False
|
|
|
|
# Patch os path exists to return True for icon files
|
|
self.patcher_path_exists = patch("os.path.exists", return_value=True)
|
|
self.mock_path_exists = self.patcher_path_exists.start()
|
|
|
|
# Patch os.listdir to return mock icon files
|
|
self.patcher_listdir = patch(
|
|
"os.listdir",
|
|
return_value=[
|
|
"vocalinux.svg",
|
|
"vocalinux-microphone-off.svg",
|
|
"vocalinux-microphone.svg",
|
|
"vocalinux-microphone-process.svg",
|
|
],
|
|
)
|
|
self.mock_listdir = self.patcher_listdir.start()
|
|
|
|
# Patch ConfigManager constructor
|
|
self.patcher_config_manager = patch("vocalinux.ui.tray_indicator.ConfigManager")
|
|
self.mock_config_manager_class = self.patcher_config_manager.start()
|
|
self.mock_config_manager_class.return_value = self.mock_config_manager
|
|
|
|
# Create the indicator with our mocked dependencies
|
|
self.tray_indicator = TrayIndicator(
|
|
speech_engine=self.mock_speech_engine,
|
|
text_injector=self.mock_text_injector,
|
|
)
|
|
|
|
# Manually set the shortcut_manager to our mock
|
|
# This ensures the tray indicator uses our mock instead of creating a new one
|
|
self.tray_indicator.shortcut_manager = self.mock_ksm
|
|
|
|
def tearDown(self):
|
|
"""Clean up test environment after each test."""
|
|
self.patcher_glib_idle.stop()
|
|
self.patcher_path_exists.stop()
|
|
self.patcher_listdir.stop()
|
|
self.patcher_config_manager.stop()
|
|
self.patcher_settings_dialog.stop()
|
|
self.thread_patcher.stop()
|
|
self.ksm_patcher.stop()
|
|
self.keyboard_available_patcher.stop()
|
|
self.keyboard_patcher.stop()
|
|
|
|
# Ensure that the keyboard shortcut manager is stopped
|
|
if hasattr(self, "tray_indicator") and hasattr(self.tray_indicator, "shortcut_manager"):
|
|
self.tray_indicator.shortcut_manager.stop()
|
|
|
|
def test_initialization(self):
|
|
"""Test initialization of the tray indicator."""
|
|
# Verify initialization
|
|
self.assertEqual(self.tray_indicator.speech_engine, self.mock_speech_engine)
|
|
self.assertEqual(self.tray_indicator.text_injector, self.mock_text_injector)
|
|
|
|
# Verify callback was registered
|
|
self.mock_speech_engine.register_state_callback.assert_called_once()
|
|
|
|
# NOTE: We're manually setting shortcut_manager after initialization, so we don't
|
|
# verify register_toggle_callback or start being called
|
|
|
|
def test_toggle_recognition_from_idle(self):
|
|
"""Test toggling recognition state from IDLE."""
|
|
# Set state to IDLE
|
|
self.mock_speech_engine.state = self.RecognitionState.IDLE
|
|
|
|
# Call toggle method
|
|
self.tray_indicator._toggle_recognition()
|
|
|
|
# Verify start_recognition was called
|
|
self.mock_speech_engine.start_recognition.assert_called_once()
|
|
self.mock_speech_engine.stop_recognition.assert_not_called()
|
|
|
|
def test_toggle_recognition_from_listening(self):
|
|
"""Test toggling recognition state from LISTENING."""
|
|
# Set state to LISTENING
|
|
self.mock_speech_engine.state = self.RecognitionState.LISTENING
|
|
|
|
# Call toggle method
|
|
self.tray_indicator._toggle_recognition()
|
|
|
|
# Verify stop_recognition was called
|
|
self.mock_speech_engine.stop_recognition.assert_called_once()
|
|
self.mock_speech_engine.start_recognition.assert_not_called()
|
|
|
|
def test_toggle_recognition_from_processing(self):
|
|
"""Test toggling recognition state from PROCESSING."""
|
|
# Set state to PROCESSING
|
|
self.mock_speech_engine.state = self.RecognitionState.PROCESSING
|
|
|
|
# Call toggle method
|
|
self.tray_indicator._toggle_recognition()
|
|
|
|
# Verify stop_recognition was called
|
|
self.mock_speech_engine.stop_recognition.assert_called_once()
|
|
self.mock_speech_engine.start_recognition.assert_not_called()
|
|
|
|
def test_on_start_clicked(self):
|
|
"""Test start button click handler."""
|
|
# Reset mock
|
|
self.mock_speech_engine.start_recognition.reset_mock()
|
|
|
|
# Call the start handler
|
|
self.tray_indicator._on_start_clicked(None)
|
|
|
|
# Verify start_recognition was called
|
|
self.mock_speech_engine.start_recognition.assert_called_once()
|
|
|
|
def test_on_stop_clicked(self):
|
|
"""Test stop button click handler."""
|
|
# Reset mock
|
|
self.mock_speech_engine.stop_recognition.reset_mock()
|
|
|
|
# Call the stop handler
|
|
self.tray_indicator._on_stop_clicked(None)
|
|
|
|
# Verify stop_recognition was called
|
|
self.mock_speech_engine.stop_recognition.assert_called_once()
|
|
|
|
def test_on_recognition_state_changed(self):
|
|
"""Test state change callback."""
|
|
# Create patch for _update_ui to prevent any actual UI updates
|
|
with patch.object(self.tray_indicator, "_update_ui") as mock_update_ui:
|
|
# Override GLib.idle_add to execute functions directly
|
|
self.mock_glib_idle.side_effect = lambda func, *args: func(*args) or False
|
|
|
|
# Call state change callback with each state
|
|
for state in self.RecognitionState:
|
|
# Reset mock
|
|
mock_update_ui.reset_mock()
|
|
|
|
# Call callback
|
|
self.tray_indicator._on_recognition_state_changed(state)
|
|
|
|
# Verify update_ui was called with correct state
|
|
mock_update_ui.assert_called_once_with(state)
|
|
|
|
def test_quit(self):
|
|
"""Test quit functionality."""
|
|
# Patch Gtk.main_quit
|
|
with patch("gi.repository.Gtk.main_quit") as mock_main_quit:
|
|
# Make sure it doesn't actually try to quit GTK
|
|
mock_main_quit.side_effect = lambda: None
|
|
|
|
# Call quit method
|
|
self.tray_indicator._quit()
|
|
|
|
# Verify shortcuts manager stopped and GTK main loop exited
|
|
self.mock_ksm.stop.assert_called_once()
|
|
mock_main_quit.assert_called_once()
|
|
|
|
def test_signal_handler(self):
|
|
"""Test signal handler."""
|
|
# Create a patch for _quit
|
|
with patch.object(self.tray_indicator, "_quit") as mock_quit:
|
|
# Reset mock for idle_add to clear previous calls
|
|
self.mock_glib_idle.reset_mock()
|
|
|
|
# Call signal handler
|
|
self.tray_indicator._signal_handler(15, None) # 15 is SIGTERM
|
|
|
|
# Verify idle_add was called with _quit
|
|
self.mock_glib_idle.assert_called_once_with(mock_quit)
|
|
|
|
def test_run(self):
|
|
"""Test run method."""
|
|
# Patch signal.signal and Gtk.main
|
|
with patch("signal.signal") as mock_signal, patch("gi.repository.Gtk.main") as mock_main:
|
|
# Configure mock_main to return immediately instead of blocking
|
|
mock_main.side_effect = lambda: None
|
|
|
|
# Call run method
|
|
self.tray_indicator.run()
|
|
|
|
# Verify signal handlers were set and main loop started
|
|
self.assertEqual(mock_signal.call_count, 2) # SIGINT and SIGTERM
|
|
mock_main.assert_called_once()
|
|
|
|
def test_settings_callback(self):
|
|
"""Test settings callback."""
|
|
# Reset mocks
|
|
self.mock_settings_dialog_class.reset_mock()
|
|
self.mock_settings_dialog.reset_mock()
|
|
|
|
# Call the settings handler
|
|
self.tray_indicator._on_settings_clicked(None)
|
|
|
|
# Verify SettingsDialog was created with correct parameters
|
|
self.mock_settings_dialog_class.assert_called_once()
|
|
# Verify dialog was shown
|
|
self.mock_settings_dialog.show.assert_called_once()
|
|
|
|
def test_about_dialog(self):
|
|
"""Test about dialog creation."""
|
|
# Patch AboutDialog class and pixbuf
|
|
with patch("gi.repository.Gtk.AboutDialog") as mock_about_dialog_class, patch(
|
|
"gi.repository.GdkPixbuf.Pixbuf.new_from_file"
|
|
) as mock_pixbuf_new, patch(
|
|
"gi.repository.GdkPixbuf.Pixbuf.scale_simple"
|
|
) as mock_pixbuf_scale:
|
|
# Create mock for dialog instance
|
|
mock_about_dialog = MagicMock()
|
|
mock_about_dialog_class.return_value = mock_about_dialog
|
|
|
|
# Make sure dialog run method doesn't block
|
|
mock_about_dialog.run.side_effect = lambda: None
|
|
|
|
# Mock pixbuf creation to prevent actual file operations
|
|
mock_pixbuf = MagicMock()
|
|
mock_pixbuf_new.return_value = mock_pixbuf
|
|
mock_pixbuf_scale.return_value = mock_pixbuf
|
|
|
|
# Call about handler
|
|
self.tray_indicator._on_about_clicked(None)
|
|
|
|
# Verify dialog was configured and shown
|
|
mock_about_dialog.set_program_name.assert_called_with("Vocalinux")
|
|
mock_about_dialog.run.assert_called_once()
|
|
mock_about_dialog.destroy.assert_called_once()
|