|
11 | 11 |
|
12 | 12 | # guitest: show |
13 | 13 |
|
| 14 | +import pytest |
14 | 15 | import qtpy.QtCore as QC |
15 | 16 | import qtpy.QtGui as QG |
16 | 17 | from guidata.env import execenv |
17 | 18 | from guidata.qthelpers import exec_dialog, qt_app_context |
18 | 19 |
|
19 | | -from plotpy.mathutils.colormap import ALL_COLORMAPS |
| 20 | +from plotpy.mathutils.colormap import ALL_COLORMAPS, delete_cmap, get_cmap |
20 | 21 | from plotpy.widgets.colormap.manager import ColorMapManager |
21 | 22 | from plotpy.widgets.colormap.widget import EditableColormap |
22 | 23 |
|
23 | 24 |
|
24 | | -def test_colormap_manager() -> None: |
| 25 | +@pytest.fixture |
| 26 | +def test_cmap(cmap_name="Kinda Viridis"): |
| 27 | + cmap = EditableColormap( |
| 28 | + [QG.QColor(QC.Qt.GlobalColor.blue), QG.QColor(QC.Qt.GlobalColor.yellow)], |
| 29 | + name=cmap_name, |
| 30 | + ) |
| 31 | + yield cmap |
| 32 | + |
| 33 | + delete_cmap(get_cmap(cmap_name)) |
| 34 | + |
| 35 | + |
| 36 | +def test_colormap_manager(test_cmap: EditableColormap) -> None: |
25 | 37 | """Test the colormap manager widget.""" |
26 | | - with qt_app_context(): |
| 38 | + with qt_app_context(exec_loop=False): |
27 | 39 | red = QG.QColor(QC.Qt.GlobalColor.red) |
28 | | - blue = QG.QColor(QC.Qt.GlobalColor.blue) |
29 | | - yellow = QG.QColor(QC.Qt.GlobalColor.yellow) |
30 | | - cmap = EditableColormap(blue, yellow, name="kinda_viridis") |
31 | | - ALL_COLORMAPS["kinda_viridis"] = cmap |
32 | | - dlg = ColorMapManager(None, active_colormap="YlGn") |
33 | | - dlg.colormap_editor.colormap_widget.add_handle_at_relative_pos(0.5, red) |
34 | | - dlg.get_colormap() |
35 | | - dlg.colormap_editor.update_colormap_widget() |
36 | | - dlg.colormap_editor.update_current_dataset() |
37 | | - result = exec_dialog(dlg) |
| 40 | + cmap_editor = ColorMapManager(None, active_colormap="YlGn") |
| 41 | + cmap_editor.show() |
| 42 | + |
| 43 | + with execenv.context(accept_dialogs=True): |
| 44 | + cmap_editor.save_colormap(test_cmap) |
| 45 | + cmap_editor.colormap_editor.colormap_widget.add_handle_at_relative_pos(0.5, red) |
| 46 | + cmap_editor.get_colormap() |
| 47 | + cmap_editor.colormap_editor.update_colormap_widget() |
| 48 | + cmap_editor.colormap_editor.update_current_dataset() |
| 49 | + |
| 50 | + # set the colormap to last one |
| 51 | + with execenv.context(accept_dialogs=True): |
| 52 | + cmap_editor.remove_colormap() |
| 53 | + |
| 54 | + result = exec_dialog(cmap_editor) |
38 | 55 | execenv.print("Dialog result:", result) |
39 | | - cmap = dlg.get_colormap() |
| 56 | + cmap = cmap_editor.get_colormap() |
40 | 57 | execenv.print("Selected colormap:", None if cmap is None else cmap.name) |
| 58 | + delete_cmap(test_cmap) |
41 | 59 |
|
42 | 60 |
|
43 | 61 | if __name__ == "__main__": |
44 | | - test_colormap_manager() |
| 62 | + test_name = "Kinda Viridis" |
| 63 | + new_colormap = EditableColormap( |
| 64 | + [QG.QColor(QC.Qt.GlobalColor.blue), QG.QColor(QC.Qt.GlobalColor.yellow)], |
| 65 | + name=test_name, |
| 66 | + ) |
| 67 | + test_colormap_manager(new_colormap) |
| 68 | + delete_cmap(get_cmap(test_name)) |
0 commit comments