from PySide6.QtCore import QSettings from PySide6.QtWidgets import QGridLayout, QHBoxLayout, QPushButton, QWidget from aare.gui.widgets.title_label import TitleLabel # Unique title so the test never clashes with real panel settings. TITLE = "TitleLabelTestPanel" KEY = f"panel_collapsed/{TITLE}" def _remove_key(): QSettings("PSI", "AareGUI").remove(KEY) def _build_panel(qtbot): panel = QWidget() qtbot.addWidget(panel) grid = QGridLayout(panel) title = TitleLabel(TITLE, panel, collapsible=True) grid.addWidget(title, 0, 0, 1, 2) direct_child = QPushButton("direct", panel) grid.addWidget(direct_child, 1, 0) nested = QHBoxLayout() nested_child = QPushButton("nested", panel) nested.addWidget(nested_child) grid.addLayout(nested, 1, 1) return panel, title, direct_child, nested_child def test_starts_collapsed_by_default_and_toggle_persists(qtbot): _remove_key() try: _panel, title, direct_child, nested_child = _build_panel(qtbot) # Default is collapsed; applied deferred with a 0 ms timer (siblings # don't exist yet at TitleLabel construction). qtbot.waitUntil(lambda: direct_child.isHidden(), timeout=1000) assert nested_child.isHidden() assert not title.isHidden() assert title.toggle_button.text() == "+" title.toggle_collapsed() assert not direct_child.isHidden() assert not nested_child.isHidden() assert title.toggle_button.text() == "−" assert QSettings("PSI", "AareGUI").value(KEY, True, type=bool) is False title.toggle_collapsed() assert direct_child.isHidden() assert title.toggle_button.text() == "+" assert QSettings("PSI", "AareGUI").value(KEY, False, type=bool) is True finally: _remove_key() def test_saved_expanded_state_restored_on_construction(qtbot): QSettings("PSI", "AareGUI").setValue(KEY, False) try: _panel, title, direct_child, nested_child = _build_panel(qtbot) # A saved expanded state must override the collapsed default; give the # (absent) deferred collapse a chance to run before asserting. qtbot.wait(100) assert not direct_child.isHidden() assert not nested_child.isHidden() assert title.toggle_button.text() == "−" finally: _remove_key() def test_not_collapsible_by_default(qtbot): panel = QWidget() qtbot.addWidget(panel) grid = QGridLayout(panel) title = TitleLabel("Plain", panel) grid.addWidget(title, 0, 0) assert not hasattr(title, "toggle_button")