|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see guidata/LICENSE for details) |
| 5 | + |
| 6 | +""" |
| 7 | +Auto-apply functionality test |
| 8 | +
|
| 9 | +This test verifies that DictItem and FloatArrayItem editors automatically |
| 10 | +trigger the apply action when used within a DataSetEditGroupBox context. |
| 11 | +""" |
| 12 | + |
| 13 | +# guitest: show |
| 14 | + |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +import guidata.dataset as gds |
| 18 | +from guidata.dataset.qtwidgets import DataSetEditGroupBox |
| 19 | +from guidata.env import execenv |
| 20 | +from guidata.qthelpers import qt_app_context |
| 21 | + |
| 22 | + |
| 23 | +class AutoApplyDataSet(gds.DataSet): |
| 24 | + """Test dataset with DictItem and FloatArrayItem""" |
| 25 | + |
| 26 | + dictionary = gds.DictItem("Dictionary", default={"a": 1, "b": 2, "c": 3}) |
| 27 | + array = gds.FloatArrayItem("Array", default=np.array([[1, 2], [3, 4]])) |
| 28 | + string = gds.StringItem("String", default="test") |
| 29 | + |
| 30 | + |
| 31 | +class AutoApplySignalChecker: |
| 32 | + """Helper class to track signal emissions""" |
| 33 | + |
| 34 | + def __init__(self, groupbox: DataSetEditGroupBox): |
| 35 | + self.groupbox = groupbox |
| 36 | + self.signal_received = False |
| 37 | + groupbox.SIG_APPLY_BUTTON_CLICKED.connect(self.on_signal) |
| 38 | + |
| 39 | + def on_signal(self): |
| 40 | + """Signal handler""" |
| 41 | + self.signal_received = True |
| 42 | + execenv.print("Signal received: SIG_APPLY_BUTTON_CLICKED") |
| 43 | + |
| 44 | + def reset(self): |
| 45 | + """Reset the checker state""" |
| 46 | + self.signal_received = False |
| 47 | + |
| 48 | + |
| 49 | +def test_auto_apply_dictitem(): |
| 50 | + """Test that DictItem widget has auto-apply functionality""" |
| 51 | + with qt_app_context(): |
| 52 | + # Create the groupbox and signal checker |
| 53 | + groupbox = DataSetEditGroupBox("Test", AutoApplyDataSet) |
| 54 | + checker = AutoApplySignalChecker(groupbox) |
| 55 | + |
| 56 | + # Get the DictItem widget - widget.item is a DataItemVariable, |
| 57 | + # widget.item.item is the actual DataItem with the type/name info |
| 58 | + dict_widget = None |
| 59 | + for widget in groupbox.edit.widgets: |
| 60 | + if ( |
| 61 | + hasattr(widget, "item") |
| 62 | + and hasattr(widget.item, "item") |
| 63 | + and isinstance(widget.item.item, gds.DictItem) |
| 64 | + ): |
| 65 | + dict_widget = widget |
| 66 | + break |
| 67 | + |
| 68 | + assert dict_widget is not None, "DictItem widget not found" |
| 69 | + |
| 70 | + # Verify the widget has the _trigger_auto_apply method |
| 71 | + assert hasattr(dict_widget, "_trigger_auto_apply"), ( |
| 72 | + "DictItem widget missing _trigger_auto_apply method" |
| 73 | + ) |
| 74 | + |
| 75 | + # Call _trigger_auto_apply to simulate what the dictionary editor does |
| 76 | + dict_widget._trigger_auto_apply() |
| 77 | + |
| 78 | + # Process events to allow deferred execution |
| 79 | + from qtpy.QtWidgets import QApplication |
| 80 | + |
| 81 | + QApplication.processEvents() |
| 82 | + |
| 83 | + # Verify signal was received |
| 84 | + assert checker.signal_received, "Signal was not emitted after auto-apply" |
| 85 | + execenv.print("✓ DictItem auto-apply triggered signal") |
| 86 | + |
| 87 | + # Verify button is disabled after processing events |
| 88 | + assert not groupbox.apply_button.isEnabled(), ( |
| 89 | + "Apply button should be disabled after auto-apply" |
| 90 | + ) |
| 91 | + execenv.print("✓ Apply button is disabled after auto-apply") |
| 92 | + |
| 93 | + |
| 94 | +def test_auto_apply_floatarrayitem(): |
| 95 | + """Test that FloatArrayItem widget has auto-apply functionality""" |
| 96 | + with qt_app_context(): |
| 97 | + # Create the groupbox and signal checker |
| 98 | + groupbox = DataSetEditGroupBox("Test", AutoApplyDataSet) |
| 99 | + checker = AutoApplySignalChecker(groupbox) |
| 100 | + |
| 101 | + # Get the FloatArrayItem widget |
| 102 | + array_widget = None |
| 103 | + for widget in groupbox.edit.widgets: |
| 104 | + if ( |
| 105 | + hasattr(widget, "item") |
| 106 | + and hasattr(widget.item, "item") |
| 107 | + and isinstance(widget.item.item, gds.FloatArrayItem) |
| 108 | + ): |
| 109 | + array_widget = widget |
| 110 | + break |
| 111 | + |
| 112 | + assert array_widget is not None, "FloatArrayItem widget not found" |
| 113 | + |
| 114 | + # Verify the widget has the _trigger_auto_apply method |
| 115 | + assert hasattr(array_widget, "_trigger_auto_apply"), ( |
| 116 | + "FloatArrayItem widget missing _trigger_auto_apply method" |
| 117 | + ) |
| 118 | + |
| 119 | + # Call _trigger_auto_apply to simulate what the array editor does |
| 120 | + array_widget._trigger_auto_apply() |
| 121 | + |
| 122 | + # Process events to allow deferred execution |
| 123 | + from qtpy.QtWidgets import QApplication |
| 124 | + |
| 125 | + QApplication.processEvents() |
| 126 | + |
| 127 | + # Verify signal was received |
| 128 | + assert checker.signal_received, "Signal was not emitted after auto-apply" |
| 129 | + execenv.print("✓ FloatArrayItem auto-apply triggered signal") |
| 130 | + |
| 131 | + # Verify button is disabled after processing events |
| 132 | + assert not groupbox.apply_button.isEnabled(), ( |
| 133 | + "Apply button should be disabled after auto-apply" |
| 134 | + ) |
| 135 | + execenv.print("✓ Apply button is disabled after auto-apply") |
| 136 | + |
| 137 | + |
| 138 | +def test_auto_apply_widget_hierarchy(): |
| 139 | + """Test auto-apply works when DataSetEditGroupBox is in widget hierarchy""" |
| 140 | + with qt_app_context(): |
| 141 | + from qtpy.QtWidgets import QFrame, QStackedWidget, QTabWidget, QVBoxLayout |
| 142 | + |
| 143 | + # Create a complex widget hierarchy similar to DataLab's Properties panel |
| 144 | + tab_widget = QTabWidget() |
| 145 | + stacked = QStackedWidget() |
| 146 | + frame1 = QFrame() |
| 147 | + frame2 = QFrame() |
| 148 | + |
| 149 | + # Create the groupbox inside the hierarchy |
| 150 | + groupbox = DataSetEditGroupBox("Test", AutoApplyDataSet) |
| 151 | + checker = AutoApplySignalChecker(groupbox) |
| 152 | + |
| 153 | + # Build the hierarchy |
| 154 | + layout = QVBoxLayout() |
| 155 | + layout.addWidget(groupbox) |
| 156 | + frame2.setLayout(layout) |
| 157 | + frame1_layout = QVBoxLayout() |
| 158 | + frame1_layout.addWidget(frame2) |
| 159 | + frame1.setLayout(frame1_layout) |
| 160 | + stacked.addWidget(frame1) |
| 161 | + tab_widget.addTab(stacked, "Test Tab") |
| 162 | + |
| 163 | + # Get the widget and trigger auto-apply |
| 164 | + dict_widget = None |
| 165 | + for widget in groupbox.edit.widgets: |
| 166 | + if ( |
| 167 | + hasattr(widget, "item") |
| 168 | + and hasattr(widget.item, "item") |
| 169 | + and isinstance(widget.item.item, gds.DictItem) |
| 170 | + ): |
| 171 | + dict_widget = widget |
| 172 | + break |
| 173 | + |
| 174 | + assert dict_widget is not None, "DictItem widget not found" |
| 175 | + |
| 176 | + # Simulate dictionary update and auto-apply |
| 177 | + dict_widget._trigger_auto_apply() |
| 178 | + |
| 179 | + # Process events |
| 180 | + from qtpy.QtWidgets import QApplication |
| 181 | + |
| 182 | + QApplication.processEvents() |
| 183 | + |
| 184 | + # Verify it still works even with complex hierarchy |
| 185 | + assert checker.signal_received, "Signal was not emitted in complex hierarchy" |
| 186 | + assert not groupbox.apply_button.isEnabled(), ( |
| 187 | + "Apply button should be disabled after auto-apply" |
| 188 | + ) |
| 189 | + execenv.print("✓ Auto-apply works correctly in complex widget hierarchy") |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + # Run all tests |
| 194 | + test_auto_apply_dictitem() |
| 195 | + execenv.print("\n" + "=" * 80 + "\n") |
| 196 | + test_auto_apply_floatarrayitem() |
| 197 | + execenv.print("\n" + "=" * 80 + "\n") |
| 198 | + test_auto_apply_widget_hierarchy() |
| 199 | + execenv.print("\nAll tests passed!") |
0 commit comments