"""Round 110: the now-playing panel is a Go to Current Song button.""" from PyQt6.QtCore import QEvent, QPointF, Qt from PyQt6.QtGui import QEnterEvent, QMouseEvent from lintunes.gui.transport import NowPlayingPanel def _mouse(kind, x=5, y=5): return QMouseEvent(kind, QPointF(x, y), QPointF(x, y), Qt.MouseButton.LeftButton, Qt.MouseButton.LeftButton if kind != QEvent.Type.MouseButtonRelease else Qt.MouseButton.NoButton, Qt.KeyboardModifier.NoModifier) def _enter(panel): panel.enterEvent(QEnterEvent(QPointF(5, 5), QPointF(5, 5), QPointF(5, 5))) def test_hover_embosses_and_a_click_goes_to_the_song(qapp): panel = NowPlayingPanel() panel.resize(200, 60) clicks = [] panel.clicked.connect(lambda: clicks.append(1)) panel.set_clickable(True) assert not panel.property("face") _enter(panel) assert panel.property("face") == "hover" panel.mousePressEvent(_mouse(QEvent.Type.MouseButtonPress)) assert panel.property("face") == "down" panel.mouseReleaseEvent(_mouse(QEvent.Type.MouseButtonRelease)) assert clicks == [1] and panel.property("face") == "hover" panel.leaveEvent(QEvent(QEvent.Type.Leave)) assert not panel.property("face") def test_released_outside_is_no_click(qapp): panel = NowPlayingPanel() panel.resize(200, 60) clicks = [] panel.clicked.connect(lambda: clicks.append(1)) panel.set_clickable(True) _enter(panel) panel.mousePressEvent(_mouse(QEvent.Type.MouseButtonPress)) panel.mouseMoveEvent(_mouse(QEvent.Type.MouseMove, 400, 5)) assert not panel.property("face") panel.mouseReleaseEvent(_mouse(QEvent.Type.MouseButtonRelease, 400, 5)) assert clicks == [] def test_nothing_playing_is_just_a_panel(qapp): panel = NowPlayingPanel() clicks = [] panel.clicked.connect(lambda: clicks.append(1)) _enter(panel) panel.mousePressEvent(_mouse(QEvent.Type.MouseButtonPress)) panel.mouseReleaseEvent(_mouse(QEvent.Type.MouseButtonRelease)) assert clicks == [] and not panel.property("face") def test_window_wires_it_to_go_to_current_song(qapp, tmp_path, monkeypatch): from lintunes.gui.main_window import MainWindow from lintunes.library_manager import LibraryManager from lintunes.models.library import Library from lintunes.models.track import Track from lintunes.preferences import Preferences library = Library() library.tracks[1] = Track(track_id=1, name="Song", artist="A") manager = LibraryManager(library, tmp_path / "data") went = [] monkeypatch.setattr(MainWindow, "_go_to_current_song", lambda self: went.append(1)) window = MainWindow(manager, Preferences(tmp_path / "data")) try: window._transport.now_playing_clicked.emit() assert went == [1] panel = window._transport._now_playing window._transport._on_track_changed(library.tracks[1]) assert panel._clickable window._transport.set_dj_mode(True) assert not panel._clickable window._transport.set_dj_mode(False) assert panel._clickable finally: window.close() def test_version_button_shows_a_face_on_hover_only_once_an_update_is_ready(qapp): from PyQt6.QtCore import QObject, pyqtSignal from lintunes.gui.version_button import VersionButton class Updater(QObject): update_available = pyqtSignal(int) update_failed = pyqtSignal(str) def short_hash(self): return "" button = VersionButton(Updater()) assert ":hover" not in button.styleSheet() # an inert readout button._on_update_available(3) assert ":hover" in button.styleSheet() and ":pressed" in button.styleSheet() button.refresh_theme() # a theme switch keeps it assert ":hover" in button.styleSheet() def test_cancel_transfer_is_always_a_button(qapp): from lintunes.gui.main_window import _CancelSyncButton button = _CancelSyncButton() assert not button.isFlat() assert "border: none" not in button.styleSheet() # ---- the Speakers and Identify toolbar items ---- def _bar(tmp_path): from test_round101 import _bar as make return make(tmp_path) def test_speakers_and_identify_are_offered_but_not_shown(qapp, tmp_path): from lintunes.gui import toolbar rows = toolbar.default_config() for item in ("speakers", "identify"): assert toolbar.find(rows, item) is None assert item in toolbar.palette_ids(rows) def test_they_can_be_dropped_in(qapp, tmp_path): from PyQt6.QtWidgets import QApplication bar, prefs = _bar(tmp_path) bar.drop_item("speakers", 0, 2) bar.drop_item("identify", 1, 3) QApplication.processEvents() assert bar._speakers_btn.parentWidget().isVisible() assert bar._identify_btn.parentWidget().isVisible() clicked = [] bar.identify_clicked.connect(lambda: clicked.append("identify")) bar.speakers_clicked.connect(lambda button: clicked.append(button)) bar._identify_btn.click() bar._speakers_btn.click() assert clicked == ["identify", bar._speakers_btn] assert not bar._speakers_btn.isChecked() # a click doesn't light it bar.set_speakers_active(True, "Kitchen") assert bar._speakers_btn.isChecked() bar._speakers_btn.click() assert bar._speakers_btn.isChecked() # nor put it out bar.set_speakers_active(False) assert not bar._speakers_btn.isChecked() def test_identify_takes_the_playing_song_else_the_selection(qapp, tmp_path, monkeypatch): from lintunes.gui.main_window import MainWindow from lintunes.library_manager import LibraryManager from lintunes.models.library import Library from lintunes.models.track import Track from lintunes.preferences import Preferences library = Library() for tid in (1, 2): library.tracks[tid] = Track(track_id=tid, name=f"S{tid}") asked = [] monkeypatch.setattr(MainWindow, "_identify_tracks", lambda self, ids: asked.append(ids)) window = MainWindow(LibraryManager(library, tmp_path / "data"), Preferences(tmp_path / "data")) try: table = window._content.currentWidget().table monkeypatch.setattr(table, "selected_track_ids", lambda: [2]) window._identify_from_toolbar() window.player._current_track = library.tracks[1] window._identify_from_toolbar() assert asked == [[2], [1]] finally: window.player._current_track = None window.close() def test_the_speaker_menu_lights_the_button(qapp, tmp_path, monkeypatch): from lintunes.gui.main_window import MainWindow from lintunes.library_manager import LibraryManager from lintunes.models.library import Library from lintunes.preferences import Preferences window = MainWindow(LibraryManager(Library(), tmp_path / "data"), Preferences(tmp_path / "data")) try: monkeypatch.setattr(window._airplay, "is_connected", lambda: True) monkeypatch.setattr(window._airplay, "device_name", lambda: "Den") window._airplay.connected.emit("Den") assert window._transport._speakers_btn.isChecked() assert "Den" in window._transport._speakers_btn.toolTip() monkeypatch.setattr(window._airplay, "is_connected", lambda: False) window._airplay.disconnected.emit("") assert not window._transport._speakers_btn.isChecked() finally: window.close()