Skip to content

Commit ab0b718

Browse files
authored
Clean up dark/light tests (#14079)
1 parent 4c3c068 commit ab0b718

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

mne/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ def browser_backend(request, garbage_collect, monkeypatch):
737737
with use_browser_backend(backend_name) as backend:
738738
backend._close_all()
739739
monkeypatch.setenv("MNE_BROWSE_RAW_SIZE", "10,10")
740+
# Unify theme across dev setups (regardless of current light/dark mode)
741+
monkeypatch.setenv("MNE_BROWSER_THEME", "light")
740742
yield backend
741743
backend._close_all()
742744
if backend_name == "qt":
@@ -1079,6 +1081,7 @@ def options_3d():
10791081
"MNE_3D_OPTION_ANTIALIAS": "false",
10801082
"MNE_3D_OPTION_DEPTH_PEELING": "false",
10811083
"MNE_3D_OPTION_SMOOTH_SHADING": "false",
1084+
"MNE_3D_OPTION_THEME": "light", # unify colors across setups
10821085
},
10831086
):
10841087
yield

mne/gui/tests/test_gui_api.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
def test_gui_api_notebook(renderer_notebook, nbexec, *, backend="qt"):
1616
"""Test GUI API."""
1717
import contextlib
18+
import os
1819
import warnings
20+
from unittest import mock
1921

2022
import mne
2123

@@ -31,8 +33,14 @@ def test_gui_api_notebook(renderer_notebook, nbexec, *, backend="qt"):
3133
mne.viz.set_3d_backend("notebook")
3234
renderer = mne.viz.backends.renderer._get_renderer(size=(300, 300))
3335

34-
# theme
35-
with warnings.catch_warnings(record=True) as w:
36+
# theme -- drop the MNE_3D_OPTION_THEME that the options_3d fixture pins to
37+
# "light" (it takes precedence via get_config), so the bad path is actually
38+
# used and warns.
39+
with (
40+
mock.patch.dict(os.environ),
41+
warnings.catch_warnings(record=True) as w,
42+
):
43+
os.environ.pop("MNE_3D_OPTION_THEME", None)
3644
warnings.simplefilter("always")
3745
renderer._window_set_theme("/does/not/exist")
3846
if backend == "qt":

mne/viz/backends/tests/test_utils.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
# License: BSD-3-Clause
33
# Copyright the MNE-Python contributors.
44

5-
import platform
65
from colorsys import rgb_to_hls
76

87
import numpy as np
98
import pytest
109

1110
from mne import create_info
1211
from mne.io import RawArray
13-
from mne.utils import _check_qt_version
1412
from mne.viz.backends._utils import (
1513
_check_color,
1614
_get_colormap_from_array,
@@ -50,37 +48,36 @@ def test_check_color():
5048
_check_color(None)
5149

5250

51+
def _assert_correct_darkness(widget, want_dark):
52+
__tracebackhide__ = True # noqa
53+
# The override propagates to children, so both palette and pixels should match.
54+
bgcolor = widget.palette().color(widget.backgroundRole()).getRgbF()[:3]
55+
dark = rgb_to_hls(*bgcolor)[1] < 0.5
56+
assert dark == want_dark, f"{widget} palette dark={dark} want_dark={want_dark}"
57+
colors = _pixmap_to_ndarray(widget.grab())[:, :, :3]
58+
dark = colors.mean() < 0.5
59+
assert dark == want_dark, f"{widget} pixmap dark={dark} want_dark={want_dark}"
60+
61+
5362
@pytest.mark.pgtest
5463
@pytest.mark.parametrize("theme", ("auto", "light", "dark"))
5564
def test_theme_colors(pg_backend, theme, monkeypatch, tmp_path):
5665
"""Test that theme colors propagate properly."""
5766
darkdetect = pytest.importorskip("darkdetect")
5867
monkeypatch.setenv("_MNE_FAKE_HOME_DIR", str(tmp_path))
5968
monkeypatch.delenv("MNE_BROWSER_THEME", raising=False)
60-
# make it seem like the system is always in light mode
61-
monkeypatch.setattr(darkdetect, "theme", lambda: "light")
69+
# A qdarkstyle stylesheet is only applied when the requested theme differs from
70+
# the system, so fake the system as the opposite of the request
71+
if theme == "auto":
72+
want_dark = (darkdetect.theme() or "light").lower() == "dark"
73+
else:
74+
want_dark = theme == "dark"
75+
fake_system = "light" if want_dark else "dark"
76+
monkeypatch.setattr(darkdetect, "theme", lambda: fake_system)
6277
raw = RawArray(np.zeros((1, 1000)), create_info(1, 1000.0, "eeg"))
63-
_, api = _check_qt_version(return_api=True)
6478
fig = raw.plot(theme=theme)
6579
is_dark = _qt_is_dark(fig)
66-
# on Darwin these checks get complicated, so don't bother for now
67-
if platform.system() == "Darwin":
68-
pytest.skip("Problems on macOS")
69-
if theme == "dark":
70-
assert is_dark, theme
71-
elif theme == "light":
72-
assert not is_dark, theme
73-
74-
def assert_correct_darkness(widget, want_dark):
75-
__tracebackhide__ = True # noqa
76-
# This should work, but it just picks up the parent in the errant case!
77-
bgcolor = widget.palette().color(widget.backgroundRole()).getRgbF()[:3]
78-
dark = rgb_to_hls(*bgcolor)[1] < 0.5
79-
assert dark == want_dark, f"{widget} dark={dark} want_dark={want_dark}"
80-
# ... so we use a more direct test
81-
colors = _pixmap_to_ndarray(widget.grab())[:, :, :3]
82-
dark = colors.mean() < 0.5
83-
assert dark == want_dark, f"{widget} dark={dark} want_dark={want_dark}"
80+
assert is_dark == want_dark, theme
8481

8582
for widget in (fig.mne.toolbar, fig.statusBar()):
86-
assert_correct_darkness(widget, is_dark)
83+
_assert_correct_darkness(widget, is_dark)

0 commit comments

Comments
 (0)