|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see guidata/LICENSE for details) |
| 5 | + |
| 6 | +""" |
| 7 | +Test dark/light theme switching |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | +from typing import Literal |
| 12 | + |
| 13 | +import pytest |
| 14 | +from guidata import qthelpers as qth |
| 15 | +from guidata.tests.widgets.test_theme import TestWidget as GuidataTestWidget |
| 16 | +from qtpy import QtWidgets as QW |
| 17 | + |
| 18 | +from plotpy.builder import make |
| 19 | +from plotpy.config import update_plotpy_color_mode |
| 20 | +from plotpy.plot import PlotOptions, PlotWidget |
| 21 | +from plotpy.tests import data as ptd |
| 22 | + |
| 23 | + |
| 24 | +class TestWidget(GuidataTestWidget): |
| 25 | + """Testing color mode switching for PlotPy and guidata widgets""" |
| 26 | + |
| 27 | + SIZE = (1400, 600) |
| 28 | + |
| 29 | + def __init__(self, default: Literal["light", "dark", "auto"] = qth.AUTO) -> None: |
| 30 | + self.plot_widget: PlotWidget | None = None |
| 31 | + super().__init__(default=default) |
| 32 | + |
| 33 | + def setup_widgets(self): |
| 34 | + """Setup widgets""" |
| 35 | + super().setup_widgets() |
| 36 | + options = PlotOptions(type="image", show_contrast=True) |
| 37 | + self.plot_widget = widget = PlotWidget(self, options=options) |
| 38 | + plot = self.plot_widget.get_plot() |
| 39 | + item = make.image(ptd.gen_image4(300, 200)) |
| 40 | + plot.add_item(item) |
| 41 | + plot.set_active_item(item, select=False) |
| 42 | + widget.setSizePolicy(QW.QSizePolicy.Expanding, QW.QSizePolicy.Expanding) |
| 43 | + self.grid_layout.addWidget(widget, 1, 2) |
| 44 | + |
| 45 | + def change_color_mode(self, mode: str) -> None: |
| 46 | + """Change color mode""" |
| 47 | + super().change_color_mode(mode) |
| 48 | + update_plotpy_color_mode() |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.skipif(reason="Not suitable for automated testing") |
| 52 | +def test_dark_light_themes( |
| 53 | + default: Literal["light", "dark", "auto"] | None = None, |
| 54 | +) -> None: |
| 55 | + """Test dark/light theme switching""" |
| 56 | + with qth.qt_app_context(exec_loop=True): |
| 57 | + widget = TestWidget(default=qth.AUTO if default is None else default) |
| 58 | + widget.show() |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + test_dark_light_themes(None if len(sys.argv) < 2 else sys.argv[1]) |
0 commit comments