| 1 |
# Unit tests for preferences_grid_base.py. |
| 2 |
# |
| 3 |
# Copyright 2026 Igalia, S.L. |
| 4 |
# Author: Joanmarie Diggs <[email protected]> |
| 5 |
# |
| 6 |
# This library is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU Lesser General Public |
| 8 |
# License as published by the Free Software Foundation; either |
| 9 |
# version 2.1 of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This library is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with this library; if not, write to the |
| 18 |
# Free Software Foundation, Inc., Franklin Street, Fifth Floor, |
| 19 |
# Boston MA 02110-1301 USA. |
| 20 |
|
| 21 |
# pylint: disable=wrong-import-position |
| 22 |
# pylint: disable=protected-access |
| 23 |
# pylint: disable=import-outside-toplevel |
| 24 |
|
| 25 |
"""Unit tests for preferences_grid_base.py.""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
from typing import Any |
| 30 |
|
| 31 |
import gi |
| 32 |
import pytest |
| 33 |
|
| 34 |
gi.require_version("Gtk", "3.0") |
| 35 |
from gi.repository import Gtk |
| 36 |
|
| 37 |
from orca.preferences_grid_base import ( |
| 38 |
AutoPreferencesGrid, |
| 39 |
BooleanPreferenceControl, |
| 40 |
CategoryListBoxRow, |
| 41 |
CommandListBoxRow, |
| 42 |
ControlType, |
| 43 |
EnumPreferenceControl, |
| 44 |
FloatRangePreferenceControl, |
| 45 |
FocusManagedListBox, |
| 46 |
IntRangePreferenceControl, |
| 47 |
PreferencesGridBase, |
| 48 |
RadioButtonWithActions, |
| 49 |
SelectionPreferenceControl, |
| 50 |
StackedPreferencesHelper, |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.unit |
| 55 |
class TestPreferenceControlDataclasses: |
| 56 |
"""Test preference control dataclass creation and properties.""" |
| 57 |
|
| 58 |
def test_boolean_preference_control_creation(self) -> None: |
| 59 |
"""Test BooleanPreferenceControl can be created with required fields.""" |
| 60 |
|
| 61 |
getter_called: list[bool] = [False] |
| 62 |
setter_value: list[bool | None] = [None] |
| 63 |
|
| 64 |
def getter() -> bool: |
| 65 |
getter_called[0] = True |
| 66 |
return True |
| 67 |
|
| 68 |
def setter(value: bool) -> bool: |
| 69 |
setter_value[0] = value |
| 70 |
return True |
| 71 |
|
| 72 |
control = BooleanPreferenceControl( |
| 73 |
label="Test Label", |
| 74 |
getter=getter, |
| 75 |
setter=setter, |
| 76 |
prefs_key="testKey", |
| 77 |
) |
| 78 |
|
| 79 |
assert control.label == "Test Label" |
| 80 |
assert control.prefs_key == "testKey" |
| 81 |
assert control.getter() is True |
| 82 |
assert getter_called[0] is True |
| 83 |
control.setter(False) |
| 84 |
assert setter_value[0] is False |
| 85 |
|
| 86 |
def test_boolean_preference_control_optional_fields(self) -> None: |
| 87 |
"""Test BooleanPreferenceControl optional fields have correct defaults.""" |
| 88 |
|
| 89 |
control = BooleanPreferenceControl(label="Test", getter=lambda: True, setter=lambda x: True) |
| 90 |
|
| 91 |
assert control.prefs_key is None |
| 92 |
assert control.member_of is None |
| 93 |
assert control.determine_sensitivity is None |
| 94 |
assert control.apply_immediately is True |
| 95 |
|
| 96 |
def test_int_range_preference_control_creation(self) -> None: |
| 97 |
"""Test IntRangePreferenceControl can be created with all fields.""" |
| 98 |
|
| 99 |
control = IntRangePreferenceControl( |
| 100 |
label="Volume", |
| 101 |
minimum=0, |
| 102 |
maximum=100, |
| 103 |
getter=lambda: 50, |
| 104 |
setter=lambda x: True, |
| 105 |
prefs_key="volume", |
| 106 |
) |
| 107 |
|
| 108 |
assert control.label == "Volume" |
| 109 |
assert control.minimum == 0 |
| 110 |
assert control.maximum == 100 |
| 111 |
assert control.getter() == 50 |
| 112 |
|
| 113 |
def test_float_range_preference_control_creation(self) -> None: |
| 114 |
"""Test FloatRangePreferenceControl can be created with all fields.""" |
| 115 |
|
| 116 |
control = FloatRangePreferenceControl( |
| 117 |
label="Rate", |
| 118 |
minimum=0.0, |
| 119 |
maximum=1.0, |
| 120 |
getter=lambda: 0.5, |
| 121 |
setter=lambda x: True, |
| 122 |
) |
| 123 |
|
| 124 |
assert control.label == "Rate" |
| 125 |
assert control.minimum == 0.0 |
| 126 |
assert control.maximum == 1.0 |
| 127 |
assert control.getter() == 0.5 |
| 128 |
|
| 129 |
def test_enum_preference_control_creation(self) -> None: |
| 130 |
"""Test EnumPreferenceControl can be created with options and values.""" |
| 131 |
|
| 132 |
control = EnumPreferenceControl( |
| 133 |
label="Style", |
| 134 |
options=["Option A", "Option B", "Option C"], |
| 135 |
values=[1, 2, 3], |
| 136 |
getter=lambda: 2, |
| 137 |
setter=lambda x: True, |
| 138 |
) |
| 139 |
|
| 140 |
assert control.label == "Style" |
| 141 |
assert len(control.options) == 3 |
| 142 |
assert control.values == [1, 2, 3] |
| 143 |
assert control.getter() == 2 |
| 144 |
|
| 145 |
def test_selection_preference_control_creation(self) -> None: |
| 146 |
"""Test SelectionPreferenceControl can be created with actions callback.""" |
| 147 |
|
| 148 |
def get_actions(_value: Any) -> list[tuple[str, str, Any]]: |
| 149 |
return [("Edit", "edit-symbolic", lambda: None)] |
| 150 |
|
| 151 |
control = SelectionPreferenceControl( |
| 152 |
label="Profile", |
| 153 |
options=["Default", "Custom"], |
| 154 |
getter=lambda: "Default", |
| 155 |
setter=lambda x: True, |
| 156 |
get_actions_for_option=get_actions, |
| 157 |
) |
| 158 |
|
| 159 |
assert control.label == "Profile" |
| 160 |
assert len(control.options) == 2 |
| 161 |
assert control.get_actions_for_option is not None |
| 162 |
actions = control.get_actions_for_option("Default") |
| 163 |
assert len(actions) == 1 |
| 164 |
assert actions[0][0] == "Edit" |
| 165 |
|
| 166 |
|
| 167 |
@pytest.mark.unit |
| 168 |
class TestHelperClasses: |
| 169 |
"""Test helper classes in preferences_grid_base.""" |
| 170 |
|
| 171 |
def test_category_list_box_row_stores_category(self) -> None: |
| 172 |
"""Test CategoryListBoxRow stores and returns category identifier.""" |
| 173 |
|
| 174 |
row = CategoryListBoxRow("speech_settings") |
| 175 |
assert row.category == "speech_settings" |
| 176 |
|
| 177 |
def test_category_list_box_row_is_gtk_list_box_row(self) -> None: |
| 178 |
"""Test CategoryListBoxRow inherits from Gtk.ListBoxRow.""" |
| 179 |
|
| 180 |
row = CategoryListBoxRow("test") |
| 181 |
assert isinstance(row, Gtk.ListBoxRow) |
| 182 |
|
| 183 |
def test_command_list_box_row_properties(self) -> None: |
| 184 |
"""Test CommandListBoxRow property getters and setters.""" |
| 185 |
|
| 186 |
row = CommandListBoxRow() |
| 187 |
assert row.command is None |
| 188 |
assert row.vbox is None |
| 189 |
assert row.binding_label is None |
| 190 |
|
| 191 |
mock_command = object() |
| 192 |
mock_vbox = Gtk.Box() |
| 193 |
mock_label = Gtk.Label() |
| 194 |
|
| 195 |
row.command = mock_command |
| 196 |
row.vbox = mock_vbox |
| 197 |
row.binding_label = mock_label |
| 198 |
|
| 199 |
assert row.command is mock_command |
| 200 |
assert row.vbox is mock_vbox |
| 201 |
assert row.binding_label is mock_label |
| 202 |
|
| 203 |
def test_radio_button_with_actions_stores_buttons(self) -> None: |
| 204 |
"""Test RadioButtonWithActions stores action buttons list.""" |
| 205 |
|
| 206 |
radio = RadioButtonWithActions(label="Test Option") |
| 207 |
assert not radio.action_buttons |
| 208 |
|
| 209 |
button1 = Gtk.Button() |
| 210 |
button2 = Gtk.Button() |
| 211 |
radio.action_buttons = [button1, button2] |
| 212 |
|
| 213 |
assert len(radio.action_buttons) == 2 |
| 214 |
assert radio.action_buttons[0] is button1 |
| 215 |
|
| 216 |
def test_radio_button_with_actions_is_gtk_radio_button(self) -> None: |
| 217 |
"""Test RadioButtonWithActions inherits from Gtk.RadioButton.""" |
| 218 |
|
| 219 |
radio = RadioButtonWithActions(label="Test") |
| 220 |
assert isinstance(radio, Gtk.RadioButton) |
| 221 |
|
| 222 |
def test_stacked_preferences_helper_initial_state(self) -> None: |
| 223 |
"""Test StackedPreferencesHelper initializes with None values.""" |
| 224 |
|
| 225 |
helper = StackedPreferencesHelper() |
| 226 |
assert helper.stack is None |
| 227 |
assert helper.categories_listbox is None |
| 228 |
assert helper.detail_listbox is None |
| 229 |
assert not helper.disable_widgets |
| 230 |
assert helper.on_category_activated_callback is None |
| 231 |
|
| 232 |
def test_stacked_preferences_helper_register_widgets(self) -> None: |
| 233 |
"""Test StackedPreferencesHelper can register widgets to disable.""" |
| 234 |
|
| 235 |
helper = StackedPreferencesHelper() |
| 236 |
button1 = Gtk.Button() |
| 237 |
button2 = Gtk.Button() |
| 238 |
|
| 239 |
helper.register_disable_widgets(button1, button2) |
| 240 |
assert len(helper.disable_widgets) == 2 |
| 241 |
assert button1 in helper.disable_widgets |
| 242 |
|
| 243 |
def test_stacked_preferences_helper_show_categories(self) -> None: |
| 244 |
"""Test StackedPreferencesHelper show_categories enables registered widgets.""" |
| 245 |
|
| 246 |
helper = StackedPreferencesHelper() |
| 247 |
helper.stack = Gtk.Stack() |
| 248 |
cat_label = Gtk.Label(label="Categories") |
| 249 |
det_label = Gtk.Label(label="Detail") |
| 250 |
helper.stack.add_named(cat_label, "categories") |
| 251 |
helper.stack.add_named(det_label, "detail") |
| 252 |
|
| 253 |
# Show the stack to ensure children are realized |
| 254 |
helper.stack.show_all() # pylint: disable=no-member |
| 255 |
helper.stack.set_visible_child_name("detail") |
| 256 |
|
| 257 |
button = Gtk.Button() |
| 258 |
button.set_sensitive(False) |
| 259 |
helper.register_disable_widgets(button) |
| 260 |
|
| 261 |
helper.show_categories() |
| 262 |
assert button.get_sensitive() is True |
| 263 |
# Verify the visible child is the categories child |
| 264 |
assert helper.stack.get_visible_child() is cat_label |
| 265 |
|
| 266 |
def test_stacked_preferences_helper_show_detail(self) -> None: |
| 267 |
"""Test StackedPreferencesHelper show_detail disables registered widgets.""" |
| 268 |
|
| 269 |
helper = StackedPreferencesHelper() |
| 270 |
helper.stack = Gtk.Stack() |
| 271 |
cat_label = Gtk.Label(label="Categories") |
| 272 |
det_label = Gtk.Label(label="Detail") |
| 273 |
helper.stack.add_named(cat_label, "categories") |
| 274 |
helper.stack.add_named(det_label, "detail") |
| 275 |
|
| 276 |
# Show the stack to ensure children are realized |
| 277 |
helper.stack.show_all() # pylint: disable=no-member |
| 278 |
helper.stack.set_visible_child_name("categories") |
| 279 |
|
| 280 |
button = Gtk.Button() |
| 281 |
button.set_sensitive(True) |
| 282 |
helper.register_disable_widgets(button) |
| 283 |
|
| 284 |
helper.show_detail() |
| 285 |
assert button.get_sensitive() is False |
| 286 |
# Verify the visible child is the detail child |
| 287 |
assert helper.stack.get_visible_child() is det_label |
| 288 |
|
| 289 |
|
| 290 |
@pytest.mark.unit |
| 291 |
class TestFocusManagedListBox: |
| 292 |
"""Test FocusManagedListBox focus management.""" |
| 293 |
|
| 294 |
def test_focus_managed_listbox_initial_state(self) -> None: |
| 295 |
"""Test FocusManagedListBox initializes with correct settings.""" |
| 296 |
|
| 297 |
listbox = FocusManagedListBox() |
| 298 |
assert listbox.get_selection_mode() == Gtk.SelectionMode.NONE |
| 299 |
assert listbox.get_can_focus() is False |
| 300 |
|
| 301 |
def test_focus_managed_listbox_add_row_with_widget(self) -> None: |
| 302 |
"""Test FocusManagedListBox tracks added rows and widgets.""" |
| 303 |
|
| 304 |
listbox = FocusManagedListBox() |
| 305 |
row = Gtk.ListBoxRow() |
| 306 |
switch = Gtk.Switch() |
| 307 |
|
| 308 |
listbox.add_row_with_widget(row, switch) |
| 309 |
|
| 310 |
assert len(listbox._widgets) == 1 |
| 311 |
assert len(listbox._rows) == 1 |
| 312 |
assert listbox._widgets[0] is switch |
| 313 |
assert listbox._rows[0] is row |
| 314 |
|
| 315 |
def test_focus_managed_listbox_get_last_row(self) -> None: |
| 316 |
"""Test FocusManagedListBox.get_last_row returns last added row.""" |
| 317 |
|
| 318 |
listbox = FocusManagedListBox() |
| 319 |
assert listbox.get_last_row() is None |
| 320 |
|
| 321 |
row1 = Gtk.ListBoxRow() |
| 322 |
row2 = Gtk.ListBoxRow() |
| 323 |
listbox.add_row_with_widget(row1, Gtk.Switch()) |
| 324 |
listbox.add_row_with_widget(row2, Gtk.Switch()) |
| 325 |
|
| 326 |
assert listbox.get_last_row() is row2 |
| 327 |
|
| 328 |
def test_focus_managed_listbox_multiple_rows(self) -> None: |
| 329 |
"""Test FocusManagedListBox handles multiple rows correctly.""" |
| 330 |
|
| 331 |
listbox = FocusManagedListBox() |
| 332 |
|
| 333 |
for _i in range(5): |
| 334 |
row = Gtk.ListBoxRow() |
| 335 |
switch = Gtk.Switch() |
| 336 |
listbox.add_row_with_widget(row, switch) |
| 337 |
|
| 338 |
assert len(listbox._widgets) == 5 |
| 339 |
assert len(listbox._rows) == 5 |
| 340 |
|
| 341 |
|
| 342 |
@pytest.mark.unit |
| 343 |
class TestPreferencesGridBase: |
| 344 |
"""Test PreferencesGridBase UI helper methods.""" |
| 345 |
|
| 346 |
def test_preferences_grid_base_initialization(self) -> None: |
| 347 |
"""Test PreferencesGridBase initializes with correct properties.""" |
| 348 |
|
| 349 |
grid = PreferencesGridBase("Test Tab") |
| 350 |
assert grid._tab_label == "Test Tab" |
| 351 |
assert grid._has_unsaved_changes is False |
| 352 |
assert grid.get_border_width() == 24 # pylint: disable=no-member |
| 353 |
|
| 354 |
def test_preferences_grid_base_get_label(self) -> None: |
| 355 |
"""Test PreferencesGridBase.get_label returns Gtk.Label.""" |
| 356 |
|
| 357 |
grid = PreferencesGridBase("My Tab") |
| 358 |
label = grid.get_label() |
| 359 |
|
| 360 |
assert isinstance(label, Gtk.Label) |
| 361 |
assert label.get_text() == "My Tab" |
| 362 |
|
| 363 |
def test_preferences_grid_base_has_changes(self) -> None: |
| 364 |
"""Test PreferencesGridBase.has_changes tracks unsaved changes.""" |
| 365 |
|
| 366 |
grid = PreferencesGridBase("Test") |
| 367 |
assert grid.has_changes() is False |
| 368 |
|
| 369 |
grid._has_unsaved_changes = True |
| 370 |
assert grid.has_changes() is True |
| 371 |
|
| 372 |
def test_create_frame_returns_frame_and_grid(self) -> None: |
| 373 |
"""Test _create_frame returns Gtk.Frame and content Gtk.Grid.""" |
| 374 |
|
| 375 |
grid = PreferencesGridBase("Test") |
| 376 |
frame, content = grid._create_frame("Section Label") |
| 377 |
|
| 378 |
assert isinstance(frame, Gtk.Frame) |
| 379 |
assert isinstance(content, Gtk.Grid) |
| 380 |
assert frame.get_child() is content |
| 381 |
|
| 382 |
def test_create_label_with_mnemonic(self) -> None: |
| 383 |
"""Test _create_label creates label with mnemonic support.""" |
| 384 |
|
| 385 |
grid = PreferencesGridBase("Test") |
| 386 |
label = grid._create_label("_Test Label") |
| 387 |
|
| 388 |
assert isinstance(label, Gtk.Label) |
| 389 |
# Label should use underline for mnemonic |
| 390 |
assert label.get_use_underline() is True |
| 391 |
|
| 392 |
def test_create_switch_row(self) -> None: |
| 393 |
"""Test _create_switch_row creates row with label and switch.""" |
| 394 |
|
| 395 |
grid = PreferencesGridBase("Test") |
| 396 |
handler_called = [False] |
| 397 |
|
| 398 |
def handler(_switch, _param): |
| 399 |
handler_called[0] = True |
| 400 |
|
| 401 |
row, switch, label = grid._create_switch_row("Enable Feature", handler, state=True) |
| 402 |
|
| 403 |
assert isinstance(row, Gtk.ListBoxRow) |
| 404 |
assert isinstance(switch, Gtk.Switch) |
| 405 |
assert isinstance(label, Gtk.Label) |
| 406 |
assert switch.get_active() is True |
| 407 |
|
| 408 |
def test_create_slider_row(self) -> None: |
| 409 |
"""Test _create_slider_row creates row with label and scale.""" |
| 410 |
|
| 411 |
grid = PreferencesGridBase("Test") |
| 412 |
adjustment = Gtk.Adjustment( |
| 413 |
value=50, |
| 414 |
lower=0, |
| 415 |
upper=100, |
| 416 |
step_increment=1, |
| 417 |
page_increment=10, |
| 418 |
) |
| 419 |
|
| 420 |
row, scale, label = grid._create_slider_row("Volume", adjustment) |
| 421 |
|
| 422 |
assert isinstance(row, Gtk.ListBoxRow) |
| 423 |
assert isinstance(scale, Gtk.Scale) |
| 424 |
assert isinstance(label, Gtk.Label) |
| 425 |
assert scale.get_value() == 50 |
| 426 |
|
| 427 |
def test_create_info_listbox(self) -> None: |
| 428 |
"""Test _create_info_listbox creates listbox with info message.""" |
| 429 |
|
| 430 |
grid = PreferencesGridBase("Test") |
| 431 |
listbox = grid._create_info_listbox("This is an informational message.") |
| 432 |
|
| 433 |
assert isinstance(listbox, Gtk.ListBox) |
| 434 |
assert listbox.get_selection_mode() == Gtk.SelectionMode.NONE |
| 435 |
|
| 436 |
def test_create_scrolled_window(self) -> None: |
| 437 |
"""Test _create_scrolled_window wraps widget in scrolled container.""" |
| 438 |
|
| 439 |
grid = PreferencesGridBase("Test") |
| 440 |
label = Gtk.Label(label="Content") |
| 441 |
scrolled = grid._create_scrolled_window(label) |
| 442 |
|
| 443 |
assert isinstance(scrolled, Gtk.ScrolledWindow) |
| 444 |
assert scrolled.get_hexpand() is True |
| 445 |
assert scrolled.get_vexpand() is True |
| 446 |
|
| 447 |
|
| 448 |
@pytest.mark.unit |
| 449 |
class TestAutoPreferencesGrid: |
| 450 |
"""Test AutoPreferencesGrid automatic UI building.""" |
| 451 |
|
| 452 |
def _create_test_grid(self) -> tuple: |
| 453 |
"""Create a test grid with various controls for testing.""" |
| 454 |
|
| 455 |
values: dict[str, Any] = { |
| 456 |
"bool_setting": True, |
| 457 |
"int_setting": 5, |
| 458 |
"float_setting": 0.5, |
| 459 |
"enum_setting": "Option B", |
| 460 |
} |
| 461 |
|
| 462 |
def set_bool(x: bool) -> bool: |
| 463 |
values["bool_setting"] = x |
| 464 |
return True |
| 465 |
|
| 466 |
def set_int(x: int) -> bool: |
| 467 |
values["int_setting"] = x |
| 468 |
return True |
| 469 |
|
| 470 |
def set_float(x: float) -> bool: |
| 471 |
values["float_setting"] = x |
| 472 |
return True |
| 473 |
|
| 474 |
def set_enum(x: str) -> bool: |
| 475 |
values["enum_setting"] = x |
| 476 |
return True |
| 477 |
|
| 478 |
controls: list[ControlType] = [ |
| 479 |
BooleanPreferenceControl( |
| 480 |
label="Enable Feature", |
| 481 |
getter=lambda: values["bool_setting"], |
| 482 |
setter=set_bool, |
| 483 |
prefs_key="boolSetting", |
| 484 |
), |
| 485 |
IntRangePreferenceControl( |
| 486 |
label="Count", |
| 487 |
minimum=0, |
| 488 |
maximum=10, |
| 489 |
getter=lambda: values["int_setting"], |
| 490 |
setter=set_int, |
| 491 |
prefs_key="intSetting", |
| 492 |
), |
| 493 |
FloatRangePreferenceControl( |
| 494 |
label="Rate", |
| 495 |
minimum=0.0, |
| 496 |
maximum=1.0, |
| 497 |
getter=lambda: values["float_setting"], |
| 498 |
setter=set_float, |
| 499 |
prefs_key="floatSetting", |
| 500 |
), |
| 501 |
EnumPreferenceControl( |
| 502 |
label="Style", |
| 503 |
options=["Option A", "Option B", "Option C"], |
| 504 |
getter=lambda: values["enum_setting"], |
| 505 |
setter=set_enum, |
| 506 |
prefs_key="enumSetting", |
| 507 |
), |
| 508 |
] |
| 509 |
|
| 510 |
grid = AutoPreferencesGrid("Test Tab", controls) |
| 511 |
return grid, values, controls |
| 512 |
|
| 513 |
def test_auto_preferences_grid_builds_widgets(self) -> None: |
| 514 |
"""Test AutoPreferencesGrid creates widgets for all controls.""" |
| 515 |
|
| 516 |
grid, _values, controls = self._create_test_grid() |
| 517 |
|
| 518 |
assert len(grid._widgets) == len(controls) |
| 519 |
assert len(grid._controls) == len(controls) |
| 520 |
|
| 521 |
def test_auto_preferences_grid_boolean_control_creates_switch(self) -> None: |
| 522 |
"""Test AutoPreferencesGrid creates Switch for boolean control.""" |
| 523 |
|
| 524 |
grid, _values, _controls = self._create_test_grid() |
| 525 |
|
| 526 |
# First control is boolean, should create a Switch |
| 527 |
widget = grid.get_widget(0) |
| 528 |
assert isinstance(widget, Gtk.Switch) |
| 529 |
assert widget.get_active() is True |
| 530 |
|
| 531 |
def test_auto_preferences_grid_int_range_creates_spinbutton(self) -> None: |
| 532 |
"""Test AutoPreferencesGrid creates SpinButton for int range control.""" |
| 533 |
|
| 534 |
grid, _values, _controls = self._create_test_grid() |
| 535 |
|
| 536 |
widget = grid.get_widget(1) |
| 537 |
assert isinstance(widget, Gtk.SpinButton) |
| 538 |
assert int(widget.get_value()) == 5 |
| 539 |
|
| 540 |
def test_auto_preferences_grid_float_range_creates_scale(self) -> None: |
| 541 |
"""Test AutoPreferencesGrid creates Scale for float range control.""" |
| 542 |
|
| 543 |
grid, _values, _controls = self._create_test_grid() |
| 544 |
|
| 545 |
widget = grid.get_widget(2) |
| 546 |
assert isinstance(widget, Gtk.Scale) |
| 547 |
assert widget.get_value() == pytest.approx(0.5, abs=0.01) |
| 548 |
|
| 549 |
def test_auto_preferences_grid_enum_creates_combobox(self) -> None: |
| 550 |
"""Test AutoPreferencesGrid creates ComboBoxText for enum control.""" |
| 551 |
|
| 552 |
grid, _values, _controls = self._create_test_grid() |
| 553 |
|
| 554 |
widget = grid.get_widget(3) |
| 555 |
assert isinstance(widget, Gtk.ComboBoxText) |
| 556 |
# Option B is at index 1 |
| 557 |
assert widget.get_active() == 1 |
| 558 |
|
| 559 |
def test_auto_preferences_grid_refresh_updates_widgets(self) -> None: |
| 560 |
"""Test AutoPreferencesGrid.refresh updates widget values from getters.""" |
| 561 |
|
| 562 |
current_value = [True] |
| 563 |
|
| 564 |
controls = [ |
| 565 |
BooleanPreferenceControl( |
| 566 |
label="Test", |
| 567 |
getter=lambda: current_value[0], |
| 568 |
setter=lambda x: True, |
| 569 |
), |
| 570 |
] |
| 571 |
|
| 572 |
grid = AutoPreferencesGrid("Test", controls) |
| 573 |
switch = grid.get_widget(0) |
| 574 |
assert switch and switch.get_active() |
| 575 |
|
| 576 |
current_value[0] = False |
| 577 |
grid.refresh() |
| 578 |
assert switch and not switch.get_active() |
| 579 |
|
| 580 |
def test_auto_preferences_grid_save_settings_calls_setters(self) -> None: |
| 581 |
"""Test AutoPreferencesGrid.save_settings calls all setters.""" |
| 582 |
|
| 583 |
grid, values, _controls = self._create_test_grid() |
| 584 |
|
| 585 |
switch = grid.get_widget(0) |
| 586 |
switch.set_active(False) |
| 587 |
spin = grid.get_widget(1) |
| 588 |
spin.set_value(8) |
| 589 |
result = grid.save_settings() |
| 590 |
|
| 591 |
assert values["bool_setting"] is False |
| 592 |
assert values["int_setting"] == 8 |
| 593 |
assert "boolSetting" in result |
| 594 |
assert result["boolSetting"] is False |
| 595 |
|
| 596 |
def test_auto_preferences_grid_save_returns_prefs_dict(self) -> None: |
| 597 |
"""Test AutoPreferencesGrid.save_settings returns dict with prefs_keys.""" |
| 598 |
|
| 599 |
grid, _values, _controls = self._create_test_grid() |
| 600 |
|
| 601 |
result = grid.save_settings() |
| 602 |
|
| 603 |
assert "boolSetting" in result |
| 604 |
assert "intSetting" in result |
| 605 |
assert "floatSetting" in result |
| 606 |
assert "enumSetting" in result |
| 607 |
|
| 608 |
def test_auto_preferences_grid_reload_clears_changes_flag(self) -> None: |
| 609 |
"""Test AutoPreferencesGrid.reload clears unsaved changes flag.""" |
| 610 |
|
| 611 |
grid, _values, _controls = self._create_test_grid() |
| 612 |
|
| 613 |
grid._has_unsaved_changes = True |
| 614 |
grid.reload() |
| 615 |
|
| 616 |
assert grid._has_unsaved_changes is False |
| 617 |
|
| 618 |
def test_auto_preferences_grid_get_widget_returns_none_for_invalid_index(self) -> None: |
| 619 |
"""Test AutoPreferencesGrid.get_widget returns None for out-of-range index.""" |
| 620 |
|
| 621 |
grid, _values, controls = self._create_test_grid() |
| 622 |
|
| 623 |
assert grid.get_widget(-1) is None |
| 624 |
assert grid.get_widget(len(controls)) is None |
| 625 |
assert grid.get_widget(100) is None |
| 626 |
|
| 627 |
def test_auto_preferences_grid_get_widget_for_control(self) -> None: |
| 628 |
"""Test AutoPreferencesGrid.get_widget_for_control finds correct widget.""" |
| 629 |
|
| 630 |
grid, _values, controls = self._create_test_grid() |
| 631 |
|
| 632 |
widget = grid.get_widget_for_control(controls[0]) |
| 633 |
assert widget is not None |
| 634 |
assert isinstance(widget, Gtk.Switch) |
| 635 |
|
| 636 |
# Non-existent control should return None |
| 637 |
other_control = BooleanPreferenceControl( |
| 638 |
label="Other", |
| 639 |
getter=lambda: True, |
| 640 |
setter=lambda x: True, |
| 641 |
) |
| 642 |
assert grid.get_widget_for_control(other_control) is None |
| 643 |
|
| 644 |
def test_auto_preferences_grid_with_grouped_controls(self) -> None: |
| 645 |
"""Test AutoPreferencesGrid groups controls by member_of field.""" |
| 646 |
|
| 647 |
controls = [ |
| 648 |
BooleanPreferenceControl(label="Ungrouped", getter=lambda: True, setter=lambda x: True), |
| 649 |
BooleanPreferenceControl( |
| 650 |
label="Group A Item 1", |
| 651 |
getter=lambda: True, |
| 652 |
setter=lambda x: True, |
| 653 |
member_of="Group A", |
| 654 |
), |
| 655 |
BooleanPreferenceControl( |
| 656 |
label="Group A Item 2", |
| 657 |
getter=lambda: False, |
| 658 |
setter=lambda x: True, |
| 659 |
member_of="Group A", |
| 660 |
), |
| 661 |
BooleanPreferenceControl( |
| 662 |
label="Group B Item", |
| 663 |
getter=lambda: True, |
| 664 |
setter=lambda x: True, |
| 665 |
member_of="Group B", |
| 666 |
), |
| 667 |
] |
| 668 |
|
| 669 |
grid = AutoPreferencesGrid("Test", controls) |
| 670 |
|
| 671 |
assert "Group A" in grid._group_labels |
| 672 |
assert "Group B" in grid._group_labels |
| 673 |
assert len(grid._widgets) == 4 |
| 674 |
|
| 675 |
def test_auto_preferences_grid_sensitivity_callback(self) -> None: |
| 676 |
"""Test AutoPreferencesGrid updates sensitivity based on callback.""" |
| 677 |
|
| 678 |
primary_enabled = [True] |
| 679 |
|
| 680 |
def set_primary(x: bool) -> bool: |
| 681 |
primary_enabled[0] = x |
| 682 |
return True |
| 683 |
|
| 684 |
controls = [ |
| 685 |
BooleanPreferenceControl( |
| 686 |
label="Primary", |
| 687 |
getter=lambda: primary_enabled[0], |
| 688 |
setter=set_primary, |
| 689 |
apply_immediately=True, |
| 690 |
), |
| 691 |
BooleanPreferenceControl( |
| 692 |
label="Dependent", |
| 693 |
getter=lambda: True, |
| 694 |
setter=lambda x: True, |
| 695 |
determine_sensitivity=lambda: primary_enabled[0], |
| 696 |
), |
| 697 |
] |
| 698 |
|
| 699 |
grid = AutoPreferencesGrid("Test", controls) |
| 700 |
|
| 701 |
primary_switch = grid.get_widget(0) |
| 702 |
dependent_switch = grid.get_widget(1) |
| 703 |
|
| 704 |
assert dependent_switch and dependent_switch.get_sensitive() |
| 705 |
|
| 706 |
grid._initializing = False # Allow change handlers to run |
| 707 |
assert primary_switch |
| 708 |
primary_switch.set_active(False) |
| 709 |
|
| 710 |
assert dependent_switch and not dependent_switch.get_sensitive() |
| 711 |
|
| 712 |
def test_auto_preferences_grid_with_info_message(self) -> None: |
| 713 |
"""Test AutoPreferencesGrid displays info message when provided.""" |
| 714 |
|
| 715 |
controls = [ |
| 716 |
BooleanPreferenceControl(label="Test", getter=lambda: True, setter=lambda x: True), |
| 717 |
] |
| 718 |
|
| 719 |
grid = AutoPreferencesGrid("Test", controls, info_message="This is helpful information.") |
| 720 |
|
| 721 |
assert grid._info_listbox is not None |
| 722 |
|
| 723 |
|
| 724 |
@pytest.mark.unit |
| 725 |
class TestAutoPreferencesGridSelectionControl: |
| 726 |
"""Test AutoPreferencesGrid with SelectionPreferenceControl.""" |
| 727 |
|
| 728 |
def test_selection_control_creates_combobox_without_actions(self) -> None: |
| 729 |
"""Test SelectionPreferenceControl without actions creates ComboBoxText.""" |
| 730 |
|
| 731 |
controls = [ |
| 732 |
SelectionPreferenceControl( |
| 733 |
label="Choose", |
| 734 |
options=["A", "B", "C"], |
| 735 |
getter=lambda: "B", |
| 736 |
setter=lambda x: True, |
| 737 |
), |
| 738 |
] |
| 739 |
|
| 740 |
grid = AutoPreferencesGrid("Test", controls) |
| 741 |
widget = grid.get_widget(0) |
| 742 |
|
| 743 |
assert isinstance(widget, Gtk.ComboBoxText) |
| 744 |
assert widget.get_active() == 1 |
| 745 |
|
| 746 |
def test_selection_control_creates_radio_buttons_with_actions(self) -> None: |
| 747 |
"""Test SelectionPreferenceControl with actions creates RadioButtons.""" |
| 748 |
|
| 749 |
def get_actions(_value): |
| 750 |
return [("Edit", "edit-symbolic", lambda: None)] |
| 751 |
|
| 752 |
controls = [ |
| 753 |
SelectionPreferenceControl( |
| 754 |
label="Profile", |
| 755 |
options=["Default", "Custom"], |
| 756 |
getter=lambda: "Default", |
| 757 |
setter=lambda x: True, |
| 758 |
get_actions_for_option=get_actions, |
| 759 |
), |
| 760 |
] |
| 761 |
|
| 762 |
grid = AutoPreferencesGrid("Test", controls) |
| 763 |
widget = grid.get_widget(0) |
| 764 |
|
| 765 |
assert isinstance(widget, RadioButtonWithActions) |
| 766 |
|
| 767 |
def test_selection_control_with_values_mapping(self) -> None: |
| 768 |
"""Test SelectionPreferenceControl maps display options to values.""" |
| 769 |
|
| 770 |
current_value = [100] |
| 771 |
|
| 772 |
def set_level(x: int) -> bool: |
| 773 |
current_value[0] = x |
| 774 |
return True |
| 775 |
|
| 776 |
controls = [ |
| 777 |
SelectionPreferenceControl( |
| 778 |
label="Level", |
| 779 |
options=["Low", "Medium", "High"], |
| 780 |
values=[50, 100, 150], |
| 781 |
getter=lambda: current_value[0], |
| 782 |
setter=set_level, |
| 783 |
prefs_key="level", |
| 784 |
), |
| 785 |
] |
| 786 |
|
| 787 |
grid = AutoPreferencesGrid("Test", controls) |
| 788 |
widget = grid.get_widget(0) |
| 789 |
|
| 790 |
assert widget and widget.get_active() == 1 |
| 791 |
|
| 792 |
widget.set_active(2) |
| 793 |
result = grid.save_settings() |
| 794 |
|
| 795 |
assert current_value[0] == 150 |
| 796 |
assert result["level"] == 150 |
| 797 |
|
| 798 |
|
| 799 |
@pytest.mark.unit |
| 800 |
class TestPreferencesGridBaseMultiPageStack: |
| 801 |
"""Test PreferencesGridBase multi-page stack functionality.""" |
| 802 |
|
| 803 |
def test_create_multi_page_stack_basic(self) -> None: |
| 804 |
"""Test _create_multi_page_stack creates stack with categories.""" |
| 805 |
|
| 806 |
class TestGrid(PreferencesGridBase): |
| 807 |
"""Test grid.""" |
| 808 |
|
| 809 |
def __init__(self): |
| 810 |
super().__init__("Test") |
| 811 |
self._initializing = True |
| 812 |
|
| 813 |
child1 = PreferencesGridBase("Child 1") |
| 814 |
child2 = PreferencesGridBase("Child 2") |
| 815 |
|
| 816 |
enable_listbox, stack, categories = self._create_multi_page_stack( |
| 817 |
enable_label="Enable", |
| 818 |
enable_getter=lambda: True, |
| 819 |
enable_setter=lambda x: True, |
| 820 |
categories=[ |
| 821 |
("General", "general", child1), |
| 822 |
("Advanced", "advanced", child2), |
| 823 |
], |
| 824 |
main_title="Test Settings", |
| 825 |
) |
| 826 |
|
| 827 |
self.my_enable_listbox = enable_listbox |
| 828 |
self.my_stack = stack |
| 829 |
self.my_categories = categories |
| 830 |
self._initializing = False |
| 831 |
|
| 832 |
grid = TestGrid() |
| 833 |
|
| 834 |
assert grid.my_stack is not None |
| 835 |
assert grid.my_categories is not None |
| 836 |
assert grid.my_enable_listbox is not None |
| 837 |
|
| 838 |
def test_create_multi_page_stack_without_enable(self) -> None: |
| 839 |
"""Test _create_multi_page_stack without enable switch.""" |
| 840 |
|
| 841 |
class TestGrid(PreferencesGridBase): |
| 842 |
"""Test grid.""" |
| 843 |
|
| 844 |
def __init__(self): |
| 845 |
super().__init__("Test") |
| 846 |
self._initializing = True |
| 847 |
|
| 848 |
child = PreferencesGridBase("Child") |
| 849 |
|
| 850 |
enable_listbox, stack, _categories = self._create_multi_page_stack( |
| 851 |
enable_label=None, |
| 852 |
enable_getter=None, |
| 853 |
enable_setter=None, |
| 854 |
categories=[("General", "general", child)], |
| 855 |
main_title="Test", |
| 856 |
) |
| 857 |
|
| 858 |
self.my_enable_listbox = enable_listbox |
| 859 |
self.my_stack = stack |
| 860 |
self._initializing = False |
| 861 |
|
| 862 |
grid = TestGrid() |
| 863 |
|
| 864 |
assert grid.my_enable_listbox is None |
| 865 |
assert grid.my_stack is not None |
| 866 |
|
| 867 |
|
| 868 |
@pytest.mark.unit |
| 869 |
class TestPreferencesGridBaseStackedPreferences: |
| 870 |
"""Test PreferencesGridBase stacked drill-down preferences.""" |
| 871 |
|
| 872 |
def test_create_stacked_preferences(self) -> None: |
| 873 |
"""Test _create_stacked_preferences creates stack with categories and detail.""" |
| 874 |
|
| 875 |
class TestGrid(PreferencesGridBase): |
| 876 |
"""Test grid.""" |
| 877 |
|
| 878 |
def __init__(self): |
| 879 |
super().__init__("Test") |
| 880 |
self.activated_row = None |
| 881 |
|
| 882 |
stack, categories, detail = self._create_stacked_preferences( |
| 883 |
on_category_activated=self._on_category, |
| 884 |
on_detail_row_activated=None, |
| 885 |
) |
| 886 |
self.my_stack = stack |
| 887 |
self.my_categories = categories |
| 888 |
self.my_detail = detail |
| 889 |
|
| 890 |
def _on_category(self, row): |
| 891 |
self.activated_row = row |
| 892 |
|
| 893 |
grid = TestGrid() |
| 894 |
|
| 895 |
assert grid.my_stack is not None |
| 896 |
assert grid.my_categories is not None |
| 897 |
assert grid.my_detail is not None |
| 898 |
assert isinstance(grid.my_stack, Gtk.Stack) |
| 899 |
assert isinstance(grid.my_categories, Gtk.ListBox) |
| 900 |
assert isinstance(grid.my_detail, Gtk.ListBox) |
| 901 |
|
| 902 |
def test_add_stack_category_row(self) -> None: |
| 903 |
"""Test _add_stack_category_row adds category with correct properties.""" |
| 904 |
|
| 905 |
class TestGrid(PreferencesGridBase): |
| 906 |
"""Test grid.""" |
| 907 |
|
| 908 |
def __init__(self): |
| 909 |
super().__init__("Test") |
| 910 |
_stack, categories, _detail = self._create_stacked_preferences( |
| 911 |
on_category_activated=lambda r: None, |
| 912 |
) |
| 913 |
self.my_categories = categories |
| 914 |
|
| 915 |
grid = TestGrid() |
| 916 |
|
| 917 |
row = grid._add_stack_category_row(grid.my_categories, "Speech Settings", category="speech") |
| 918 |
|
| 919 |
assert isinstance(row, CategoryListBoxRow) |
| 920 |
assert row.category == "speech" |
| 921 |
|
| 922 |
def test_add_stack_category_row_chevron_accessible_role(self) -> None: |
| 923 |
"""Test _add_stack_category_row chevron has PUSH_BUTTON accessible role.""" |
| 924 |
|
| 925 |
gi.require_version("Atk", "1.0") |
| 926 |
from gi.repository import Atk |
| 927 |
|
| 928 |
class TestGrid(PreferencesGridBase): |
| 929 |
"""Test grid.""" |
| 930 |
|
| 931 |
def __init__(self): |
| 932 |
super().__init__("Test") |
| 933 |
_stack, categories, _detail = self._create_stacked_preferences( |
| 934 |
on_category_activated=lambda r: None, |
| 935 |
) |
| 936 |
self.my_categories = categories |
| 937 |
|
| 938 |
grid = TestGrid() |
| 939 |
row = grid._add_stack_category_row(grid.my_categories, "Test Category", category="test") |
| 940 |
|
| 941 |
# Find the chevron image in the row's hbox |
| 942 |
hbox = row.get_child() |
| 943 |
chevron = None |
| 944 |
for child in hbox.get_children(): |
| 945 |
if isinstance(child, Gtk.Image): |
| 946 |
chevron = child |
| 947 |
break |
| 948 |
|
| 949 |
assert chevron is not None, "Chevron image not found in category row" |
| 950 |
|
| 951 |
chevron_accessible = chevron.get_accessible() |
| 952 |
assert chevron_accessible is not None |
| 953 |
assert chevron_accessible.get_role() == Atk.Role.BUTTON |
| 954 |
assert chevron_accessible.get_name() == "" |
| 955 |
|
| 956 |
def test_show_stack_categories_and_detail(self) -> None: |
| 957 |
"""Test _show_stack_categories and _show_stack_detail switch views.""" |
| 958 |
|
| 959 |
class TestGrid(PreferencesGridBase): |
| 960 |
"""Test grid.""" |
| 961 |
|
| 962 |
def __init__(self): |
| 963 |
super().__init__("Test") |
| 964 |
stack, categories, detail = self._create_stacked_preferences( |
| 965 |
on_category_activated=lambda r: None, |
| 966 |
) |
| 967 |
self.my_stack = stack |
| 968 |
self.my_categories = categories |
| 969 |
self.my_detail = detail |
| 970 |
|
| 971 |
grid = TestGrid() |
| 972 |
|
| 973 |
grid.my_stack.show_all() # pylint: disable=no-member |
| 974 |
|
| 975 |
cat_scrolled = grid.my_stack.get_child_by_name("categories") |
| 976 |
det_scrolled = grid.my_stack.get_child_by_name("detail") |
| 977 |
|
| 978 |
grid._show_stack_detail() |
| 979 |
assert grid.my_stack.get_visible_child() is det_scrolled |
| 980 |
|
| 981 |
grid._show_stack_categories() |
| 982 |
assert grid.my_stack.get_visible_child() is cat_scrolled |