test: add comprehensive tests for UI dialog redesigns (#135)

* test: add comprehensive tests for UI dialog redesigns

- Add tests for AboutDialog (show_about_dialog function and class)
- Add tests for LoggingDialog (CSS, filtering, actions, toast notifications)
- Update SettingsDialog tests for instant-apply pattern (Close-only buttons)
- Use source code inspection to verify method existence with GTK mocks
- All 382 tests passing

* fix(test): rewrite UI dialog tests to use source inspection

Replace GTK mocking with source code inspection approach to fix
CI failures on Python 3.8 and 3.10. The new approach reads source
files directly and verifies method definitions, CSS classes, and
expected code patterns exist, avoiding Python version-specific
module caching issues with patched imports.
This commit is contained in:
Jatin K Malik
2026-02-05 14:51:36 -08:00
committed by GitHub
parent 192b089c97
commit c75893b2a4
3 changed files with 674 additions and 1 deletions
+198 -1
View File
@@ -4,18 +4,24 @@ Tests for the SettingsDialog.
Since SettingsDialog inherits from Gtk.Dialog which is mocked during tests,
we can't easily test the actual class methods. Instead, we test the core
logic that the settings dialog is supposed to execute.
UX Design Notes tested:
- Instant-apply pattern: settings apply immediately when changed
- Close-only button (no Apply/Cancel) consistent with GNOME Settings
"""
import sys
import time
import unittest
from unittest.mock import MagicMock, Mock
from unittest.mock import MagicMock, Mock, patch
# Mock GTK before importing anything that might use it
sys.modules["gi"] = MagicMock()
sys.modules["gi.repository"] = MagicMock()
sys.modules["gi.repository.Gtk"] = MagicMock()
sys.modules["gi.repository.GLib"] = MagicMock()
sys.modules["gi.repository.Gdk"] = MagicMock()
sys.modules["gi.repository.Pango"] = MagicMock()
from vocalinux.common_types import RecognitionState # noqa: E402
@@ -154,5 +160,196 @@ class TestSettingsDialog(unittest.TestCase):
mock_speech_engine.reconfigure.assert_called_once()
class TestSettingsDialogCSS(unittest.TestCase):
"""Test cases for SettingsDialog CSS styling."""
def setUp(self):
"""Set up test fixtures."""
# Clear any cached imports
if "vocalinux.ui.settings_dialog" in sys.modules:
del sys.modules["vocalinux.ui.settings_dialog"]
def test_settings_css_exists(self):
"""Test that SETTINGS_CSS constant is defined."""
from vocalinux.ui.settings_dialog import SETTINGS_CSS
self.assertIsInstance(SETTINGS_CSS, str)
def test_settings_css_has_dialog_class(self):
"""Test that CSS includes settings-dialog class."""
from vocalinux.ui.settings_dialog import SETTINGS_CSS
self.assertIn(".settings-dialog", SETTINGS_CSS)
def test_settings_css_has_preferences_group(self):
"""Test that CSS includes preferences-group class."""
from vocalinux.ui.settings_dialog import SETTINGS_CSS
self.assertIn(".preferences-group", SETTINGS_CSS)
def test_settings_css_has_preference_row(self):
"""Test that CSS includes preference-row class."""
from vocalinux.ui.settings_dialog import SETTINGS_CSS
self.assertIn(".preference-row", SETTINGS_CSS)
def test_settings_css_uses_theme_variables(self):
"""Test that CSS uses GTK theme variables."""
from vocalinux.ui.settings_dialog import SETTINGS_CSS
# Should use theme variables for proper light/dark mode support
self.assertIn("@theme_bg_color", SETTINGS_CSS)
self.assertIn("@theme_base_color", SETTINGS_CSS)
def test_settings_css_has_status_classes(self):
"""Test that CSS includes status indicator classes."""
from vocalinux.ui.settings_dialog import SETTINGS_CSS
self.assertIn(".status-success", SETTINGS_CSS)
self.assertIn(".status-warning", SETTINGS_CSS)
self.assertIn(".status-error", SETTINGS_CSS)
class TestSettingsDialogClasses(unittest.TestCase):
"""Test cases for SettingsDialog helper classes."""
def setUp(self):
"""Set up test fixtures."""
if "vocalinux.ui.settings_dialog" in sys.modules:
del sys.modules["vocalinux.ui.settings_dialog"]
def test_preferences_group_class_exists(self):
"""Test that PreferencesGroup class exists."""
from vocalinux.ui.settings_dialog import PreferencesGroup
self.assertTrue(callable(PreferencesGroup))
def test_preference_row_class_exists(self):
"""Test that PreferenceRow class exists."""
from vocalinux.ui.settings_dialog import PreferenceRow
self.assertTrue(callable(PreferenceRow))
def test_model_download_dialog_class_exists(self):
"""Test that ModelDownloadDialog class exists."""
from vocalinux.ui.settings_dialog import ModelDownloadDialog
self.assertTrue(callable(ModelDownloadDialog))
class TestSettingsDialogInstantApply(unittest.TestCase):
"""Test cases for instant-apply behavior (Close-only button pattern)."""
def setUp(self):
"""Set up test fixtures."""
if "vocalinux.ui.settings_dialog" in sys.modules:
del sys.modules["vocalinux.ui.settings_dialog"]
def test_settings_dialog_has_auto_apply_method(self):
"""Test that SettingsDialog has _auto_apply_settings method in source."""
import os
source_path = os.path.join(
os.path.dirname(__file__),
"..",
"src",
"vocalinux",
"ui",
"settings_dialog.py",
)
with open(source_path, "r") as f:
source_code = f.read()
self.assertIn("def _auto_apply_settings(self", source_code)
def test_settings_dialog_close_only_pattern(self):
"""Test that SettingsDialog uses Close-only button pattern (no Apply/Cancel)."""
import os
source_path = os.path.join(
os.path.dirname(__file__),
"..",
"src",
"vocalinux",
"ui",
"settings_dialog.py",
)
with open(source_path, "r") as f:
source_code = f.read()
# Check for Close button
self.assertIn("_Close", source_code)
# Should NOT have Apply or Cancel buttons (instant-apply pattern)
self.assertNotIn("_Apply", source_code)
self.assertNotIn("ResponseType.APPLY", source_code)
def test_settings_dialog_no_revert_settings(self):
"""Test that SettingsDialog does NOT have _revert_settings (removed)."""
from vocalinux.ui.settings_dialog import SettingsDialog
# _revert_settings was removed as part of Close-only button pattern
self.assertFalse(hasattr(SettingsDialog, "_revert_settings"))
def test_settings_dialog_no_show_applied_message(self):
"""Test that SettingsDialog does NOT have _show_settings_applied_message (removed)."""
from vocalinux.ui.settings_dialog import SettingsDialog
# _show_settings_applied_message was removed as part of instant-apply pattern
self.assertFalse(hasattr(SettingsDialog, "_show_settings_applied_message"))
class TestSettingsDialogHelperFunctions(unittest.TestCase):
"""Test cases for settings dialog helper functions."""
def setUp(self):
"""Set up test fixtures."""
if "vocalinux.ui.settings_dialog" in sys.modules:
del sys.modules["vocalinux.ui.settings_dialog"]
def test_format_size_function_exists(self):
"""Test that _format_size function exists."""
from vocalinux.ui.settings_dialog import _format_size
self.assertTrue(callable(_format_size))
def test_format_size_mb(self):
"""Test _format_size with MB values."""
from vocalinux.ui.settings_dialog import _format_size
self.assertEqual(_format_size(100), "100 MB")
self.assertEqual(_format_size(500), "500 MB")
def test_format_size_gb(self):
"""Test _format_size with GB values."""
from vocalinux.ui.settings_dialog import _format_size
self.assertEqual(_format_size(1000), "1.0 GB")
self.assertEqual(_format_size(2500), "2.5 GB")
def test_is_whisper_model_downloaded_function_exists(self):
"""Test that _is_whisper_model_downloaded function exists."""
from vocalinux.ui.settings_dialog import _is_whisper_model_downloaded
self.assertTrue(callable(_is_whisper_model_downloaded))
def test_is_vosk_model_downloaded_function_exists(self):
"""Test that _is_vosk_model_downloaded function exists."""
from vocalinux.ui.settings_dialog import _is_vosk_model_downloaded
self.assertTrue(callable(_is_vosk_model_downloaded))
def test_get_recommended_whisper_model_function_exists(self):
"""Test that _get_recommended_whisper_model function exists."""
from vocalinux.ui.settings_dialog import _get_recommended_whisper_model
self.assertTrue(callable(_get_recommended_whisper_model))
def test_get_recommended_vosk_model_function_exists(self):
"""Test that _get_recommended_vosk_model function exists."""
from vocalinux.ui.settings_dialog import _get_recommended_vosk_model
self.assertTrue(callable(_get_recommended_vosk_model))
if __name__ == "__main__":
unittest.main()