257 lines
10 KiB
Python
257 lines
10 KiB
Python
"""Round 101: dark mode, a tint per mode, and the toolbar configurator."""
|
|
from PyQt6.QtGui import QColor, QPalette
|
|
from PyQt6.QtWidgets import QApplication
|
|
|
|
from lintunes import theme
|
|
from lintunes.preferences import Preferences
|
|
|
|
|
|
def _luma(color: QColor) -> float:
|
|
return 0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()
|
|
|
|
|
|
def test_dark_mode_turns_every_surface_dark_and_text_light(qapp, tmp_path):
|
|
prefs = Preferences(tmp_path)
|
|
prefs.set("dark_mode", True)
|
|
try:
|
|
theme.apply_theme(qapp, prefs)
|
|
palette = QApplication.palette()
|
|
for role in (QPalette.ColorRole.Window, QPalette.ColorRole.Base,
|
|
QPalette.ColorRole.AlternateBase, QPalette.ColorRole.Button):
|
|
assert _luma(palette.color(role)) < 90, role
|
|
for role in (QPalette.ColorRole.Text, QPalette.ColorRole.WindowText,
|
|
QPalette.ColorRole.ButtonText):
|
|
assert _luma(palette.color(role)) > 180, role
|
|
assert palette.color(QPalette.ColorRole.Highlight) == theme.highlight_color(prefs)
|
|
assert theme.is_dark()
|
|
finally:
|
|
theme.apply_theme(qapp, Preferences(tmp_path / "light"))
|
|
assert not theme.is_dark()
|
|
|
|
|
|
def test_each_mode_keeps_its_own_grays(tmp_path):
|
|
prefs = Preferences(tmp_path)
|
|
prefs.set("color_background", 200)
|
|
prefs.set("dark_color_background", 20)
|
|
assert theme.gray_value(prefs, "color_background") == 200
|
|
prefs.set("dark_mode", True)
|
|
assert theme.gray_value(prefs, "color_background") == 20
|
|
assert theme.gray_value(prefs, "color_stripe") == theme.DARK_DEFAULTS["color_stripe"]
|
|
assert theme.gray_value(prefs, "color_background", "light") == 200
|
|
|
|
|
|
def test_a_tint_keeps_the_gray_lightness(tmp_path):
|
|
prefs = Preferences(tmp_path)
|
|
prefs.set("dark_mode", True)
|
|
prefs.set("dark_tint_color", "#ff0000")
|
|
prefs.set("dark_tint_level", 50)
|
|
color = theme.surface(prefs, "color_background")
|
|
plain = theme.gray(theme.gray_value(prefs, "color_background"))
|
|
assert abs(color.lightnessF() - plain.lightnessF()) < 0.01
|
|
assert color.red() > color.green() == color.blue()
|
|
prefs.set("dark_tint_level", 0)
|
|
assert theme.surface(prefs, "color_background") == plain
|
|
|
|
|
|
def test_untouched_light_mode_keeps_the_base_palette(qapp, tmp_path):
|
|
theme.apply_theme(qapp, Preferences(tmp_path))
|
|
assert (QApplication.palette().color(QPalette.ColorRole.Window)
|
|
== theme._base().color(QPalette.ColorRole.Window))
|
|
|
|
|
|
def _dialog(tmp_path):
|
|
from lintunes.gui.preferences_dialog import PreferencesDialog
|
|
from tests.test_round40 import _FakeLastFm
|
|
prefs = Preferences(tmp_path)
|
|
return prefs, PreferencesDialog(prefs, _FakeLastFm())
|
|
|
|
|
|
def test_switching_the_edited_mode_moves_the_sliders(qapp, tmp_path):
|
|
prefs, dialog = _dialog(tmp_path)
|
|
slider = dialog._color_sliders["color_background"]
|
|
slider.setValue(222)
|
|
assert prefs.get("color_background") == 222
|
|
dialog._color_mode_buttons["dark"].setChecked(True)
|
|
assert slider.value() == theme.DARK_DEFAULTS["color_background"]
|
|
slider.setValue(11)
|
|
assert prefs.get("dark_color_background") == 11
|
|
assert prefs.get("color_background") == 222 # light untouched
|
|
dialog._color_mode_buttons["light"].setChecked(True)
|
|
assert slider.value() == 222
|
|
dialog._tint_slider.setValue(40)
|
|
assert prefs.get("tint_level") == 40
|
|
assert prefs.get("dark_tint_level") == 0
|
|
dialog.deleteLater()
|
|
|
|
|
|
def test_the_dark_mode_box_follows_the_pref(qapp, tmp_path):
|
|
prefs, dialog = _dialog(tmp_path)
|
|
dialog._dark_check.setChecked(True)
|
|
assert prefs.get("dark_mode") is True
|
|
assert dialog._color_mode_buttons["dark"].isChecked()
|
|
prefs.set("dark_mode", False) # e.g. the toolbar's button
|
|
assert not dialog._dark_check.isChecked()
|
|
dialog.deleteLater()
|
|
theme.apply_theme(qapp, Preferences(tmp_path / "light"))
|
|
|
|
|
|
# ---- the toolbar configurator ----
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
from PyQt6.QtCore import QPoint, QPointF, Qt
|
|
from PyQt6.QtGui import QMouseEvent
|
|
from PyQt6.QtCore import QEvent
|
|
|
|
from lintunes.gui import toolbar
|
|
from lintunes.gui.toolbar import Slot, lay_out
|
|
|
|
|
|
def test_normalize_keeps_what_must_stay_and_drops_junk():
|
|
rows = toolbar.normalize([[{"id": "shuffle"}, {"id": "shuffle"}, {"id": "nope"},
|
|
{"id": "library"}, "space:1"], []])
|
|
assert [e["id"] for e in rows[0]] == ["shuffle", "space:1"]
|
|
# library may only sit in row 1, and it and the view can't be removed
|
|
assert {e["id"] for e in rows[1]} == {"library", "view"}
|
|
assert toolbar.normalize("garbage") == toolbar.default_config()
|
|
|
|
|
|
def test_move_remove_and_sizes():
|
|
rows = toolbar.default_config()
|
|
rows = toolbar.move(rows, "dj", 0, 0)
|
|
assert rows[0][0]["id"] == "dj"
|
|
rows = toolbar.move(rows, "theme", 1, 2)
|
|
assert [e["id"] for e in rows[1]][:3] == ["library", "view", "theme"]
|
|
assert toolbar.move(rows, "library", 0, 0) == rows # row 1 only
|
|
assert toolbar.remove(rows, "view") == rows # required
|
|
rows = toolbar.remove(rows, "theme")
|
|
assert toolbar.find(rows, "theme") is None
|
|
rows = toolbar.set_size(rows, "visualizer", "exact", 200)
|
|
entry = rows[0][toolbar.find(rows, "visualizer")[1]]
|
|
assert entry == {"id": "visualizer", "size": "exact", "width": 200}
|
|
rows = toolbar.set_size(rows, "visualizer", "fill")
|
|
assert "width" not in rows[0][toolbar.find(rows, "visualizer")[1]]
|
|
|
|
|
|
def test_spacers_are_numbered_and_always_offered():
|
|
rows = toolbar.default_config()
|
|
first = toolbar.new_instance(rows, "space")
|
|
rows = toolbar.move(rows, first, 0, 1)
|
|
assert first == "space:1"
|
|
assert toolbar.new_instance(rows, "space") == "space:2"
|
|
assert "space" in toolbar.palette_ids(rows)
|
|
assert "theme" in toolbar.palette_ids(rows)
|
|
assert "shuffle" not in toolbar.palette_ids(rows)
|
|
|
|
|
|
def test_lay_out_knolls_and_shares_fill():
|
|
spots = lay_out([Slot("a", width=50), Slot("b", fill=True), Slot("c", width=30),
|
|
Slot("d", fill=True)], 400, 10, 10)
|
|
assert spots["a"] == (10, 50)
|
|
# 400 - 2*10 margins - 80 fixed - 3*10 gaps = 270, split 135/135
|
|
assert spots["b"] == (70, 135) and spots["d"][1] == 135
|
|
assert spots["d"][0] + spots["d"][1] == 390
|
|
|
|
|
|
def test_lay_out_anchors_and_falls_back():
|
|
spots = lay_out([Slot("lib", anchor=(8, 200)), Slot("view", fill=True, start=208),
|
|
Slot("seek", anchor=(500, 900))], 1000, 8, 6)
|
|
assert spots["lib"] == (8, 192)
|
|
assert spots["view"] == (208, 500 - 6 - 208)
|
|
assert spots["seek"] == (500, 400)
|
|
# an anchor the items before it overrun is just a fixed slot after them
|
|
spots = lay_out([Slot("big", width=600), Slot("seek", anchor=(100, 300))], 1000, 0, 10)
|
|
assert spots["seek"] == (610, 200)
|
|
|
|
|
|
def _bar(tmp_path, rows=None):
|
|
from lintunes.gui.transport import TransportBar
|
|
player = MagicMock()
|
|
player.repeat_mode.return_value = "off"
|
|
prefs = Preferences(tmp_path)
|
|
if rows is not None:
|
|
prefs.set("toolbar", rows)
|
|
bar = TransportBar(player, MagicMock(), prefs)
|
|
bar.resize(1280, 140)
|
|
bar.show()
|
|
QApplication.processEvents()
|
|
return bar, prefs
|
|
|
|
|
|
def test_dropping_an_item_in_adds_it_and_saves(qapp, tmp_path):
|
|
bar, prefs = _bar(tmp_path)
|
|
assert not bar._theme_btn.parentWidget().isVisible()
|
|
bar.drop_item("theme", 0, 3)
|
|
QApplication.processEvents()
|
|
assert [e["id"] for e in prefs.get("toolbar")[0]][3] == "theme"
|
|
box = bar._theme_btn.parentWidget()
|
|
assert box.isVisible() and box.parentWidget() is bar._top_row
|
|
shuffle = bar._shuffle_btn.parentWidget().geometry()
|
|
assert box.geometry().left() > shuffle.right()
|
|
bar.remove_item("theme")
|
|
assert not box.isVisible()
|
|
|
|
|
|
def test_the_theme_button_flips_dark_mode(qapp, tmp_path):
|
|
bar, prefs = _bar(tmp_path)
|
|
bar._theme_btn.click()
|
|
assert prefs.get("dark_mode") is True
|
|
bar._theme_btn.click()
|
|
assert prefs.get("dark_mode") is False
|
|
|
|
|
|
def test_an_exact_width_and_fill(qapp, tmp_path):
|
|
bar, prefs = _bar(tmp_path)
|
|
bar._save_toolbar(toolbar.set_size(bar._rows, "visualizer", "exact", 300))
|
|
assert bar._visualizer.width() == 300
|
|
panel = bar._now_playing.width()
|
|
bar._save_toolbar(toolbar.set_size(bar._rows, "volume", "fill"))
|
|
# two fills now share what the fixed items leave
|
|
assert abs(bar._volume_slider.width() - bar._now_playing.width()) <= 1
|
|
assert bar._now_playing.width() < panel
|
|
|
|
|
|
def _mouse(widget, kind, pos, button=Qt.MouseButton.LeftButton):
|
|
event = QMouseEvent(kind, QPointF(pos), QPointF(widget.mapToGlobal(pos)), button,
|
|
button if kind != QEvent.Type.MouseButtonRelease
|
|
else Qt.MouseButton.NoButton,
|
|
Qt.KeyboardModifier.NoModifier)
|
|
QApplication.sendEvent(widget, event)
|
|
|
|
|
|
def test_customize_mode_resizes_by_the_handle(qapp, tmp_path):
|
|
bar, prefs = _bar(tmp_path)
|
|
bar.set_customizing(True)
|
|
QApplication.processEvents()
|
|
assert bar.is_customizing() and len(bar._overlays) == 2
|
|
overlay = bar._overlays[0]
|
|
rect = bar._rects[0]["visualizer"]
|
|
start = QPoint(rect.right() - 1, rect.center().y())
|
|
_mouse(overlay, QEvent.Type.MouseButtonPress, start)
|
|
_mouse(overlay, QEvent.Type.MouseMove, start + QPoint(40, 0))
|
|
_mouse(overlay, QEvent.Type.MouseButtonRelease, start + QPoint(40, 0))
|
|
entry = prefs.get("toolbar")[0][toolbar.find(bar._rows, "visualizer")[1]]
|
|
assert entry["size"] == "exact" and entry["width"] == rect.width() + 40
|
|
assert bar._visualizer.width() == rect.width() + 40
|
|
# the overlay takes the clicks, so a button under it isn't pressed
|
|
played = []
|
|
bar.play_clicked.connect(lambda: played.append(1))
|
|
play = bar._play_btn.mapTo(bar._top_row, bar._play_btn.rect().center())
|
|
_mouse(overlay, QEvent.Type.MouseButtonPress, play)
|
|
_mouse(overlay, QEvent.Type.MouseButtonRelease, play)
|
|
assert played == []
|
|
bar.set_customizing(False)
|
|
QApplication.processEvents()
|
|
assert bar._overlays == [] and bar._panel is None
|
|
|
|
|
|
def test_restore_defaults(qapp, tmp_path):
|
|
rows = toolbar.remove(toolbar.default_config(), "shuffle")
|
|
bar, prefs = _bar(tmp_path, rows)
|
|
assert not bar._shuffle_btn.parentWidget().isVisible()
|
|
bar.set_customizing(True)
|
|
bar._panel.restore.emit()
|
|
assert prefs.get("toolbar") == toolbar.default_config()
|
|
assert bar._shuffle_btn.parentWidget().isVisible()
|
|
bar.set_customizing(False)
|