"""Round 91: the playlist frame's rounded corners, the empty art panel as a button under the cursor, an art window with no background, and cliairplay as the fallback AirPlay sender.""" import asyncio import hashlib from types import SimpleNamespace from unittest.mock import MagicMock, patch import pytest from PyQt6.QtCore import QEvent, QPointF, QSize, Qt from PyQt6.QtGui import QColor, QEnterEvent, QMouseEvent, QPixmap from PyQt6.QtWidgets import QLabel from tests.test_round49 import _FakePlayer # ---- the playlist frame ---- class TestRoundedFrame: def test_corners_are_window_color_and_clicks_reach_the_list(self, qapp): from lintunes.gui.sidebar import RoundedFrame, frame_color frame = RoundedFrame(8) inside = QLabel(frame) inside.setAutoFillBackground(True) palette = inside.palette() palette.setColor(inside.backgroundRole(), QColor("red")) inside.setPalette(palette) inside.setGeometry(0, 0, 100, 100) frame.resize(100, 100) frame.show() qapp.processEvents() image = frame.grab().toImage() window = frame.palette().color(frame.backgroundRole()) assert QColor(image.pixel(0, 0)) == QColor(window.name()) assert QColor(image.pixel(50, 50)) == QColor("red") edge = QColor(image.pixel(50, 0)) assert abs(edge.red() - frame_color(frame.palette()).red()) < 12 assert frame._overlay.testAttribute( Qt.WidgetAttribute.WA_TransparentForMouseEvents) frame.close() # ---- the empty art panel ---- def _event(kind, button=Qt.MouseButton.LeftButton, x=50, y=50): return QMouseEvent(kind, QPointF(x, y), QPointF(x, y), button, button, Qt.KeyboardModifier.NoModifier) class TestEmptyArtButton: def _art(self, track=True): from lintunes.gui.sidebar import SidebarArt art = SidebarArt(_FakePlayer()) art.resize(100, 106) art._track = SimpleNamespace(track_id=1) if track else None return art def _middle(self, art): return art.grab().toImage().pixelColor(50, 60) def test_pops_up_on_hover_and_sinks_while_held(self, qapp): art = self._art() resting = self._middle(art) art.enterEvent(QEnterEvent(QPointF(50, 50), QPointF(50, 50), QPointF(50, 50))) hovered = self._middle(art) art.mousePressEvent(_event(QEvent.Type.MouseButtonPress)) held = self._middle(art) assert hovered != resting and held != hovered assert held.lightness() < hovered.lightness() art.mouseReleaseEvent(_event(QEvent.Type.MouseButtonRelease)) assert self._middle(art) == hovered art.leaveEvent(QEvent(QEvent.Type.Leave)) assert self._middle(art) == resting def test_no_track_or_real_art_is_never_a_button(self, qapp): art = self._art(track=False) resting = self._middle(art) art.enterEvent(QEnterEvent(QPointF(50, 50), QPointF(50, 50), QPointF(50, 50))) assert self._middle(art) == resting assert not art._is_button() art._track = SimpleNamespace(track_id=1) art._pixmap = QPixmap(10, 10) assert not art._is_button() # ---- the big art window ---- class TestArtShape: def test_the_dimension_that_moved_leads(self): from lintunes.gui.art_window import reshaped art = QSize(500, 500) assert reshaped(art, QSize(700, 520), QSize(500, 500)) == QSize(700, 700) assert reshaped(art, QSize(510, 300), QSize(500, 500)) == QSize(300, 300) assert reshaped(QSize(200, 100), QSize(400, 400), None) == QSize(400, 200) def test_stays_on_the_screen(self): from lintunes.gui.art_window import reshaped assert reshaped(QSize(500, 500), QSize(1500, 900), QSize(500, 500), QSize(1920, 1000)) == QSize(1000, 1000) def test_never_paints_a_background(self, qapp): from lintunes.gui.art_window import ArtWindow window = ArtWindow() art = QPixmap(100, 100) art.fill(QColor("red")) window._pixmap = art window.resize(300, 200) # the wrong shape, before the snap image = window.grab().toImage() for x, y in ((0, 0), (299, 199), (5, 100), (295, 100)): assert image.pixelColor(x, y) == QColor("red") # ---- cliairplay ---- class TestCliBinary: def test_a_checksum_mismatch_is_never_installed(self, tmp_path, monkeypatch): from lintunes.airplay import cli monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path)) monkeypatch.setattr(cli, "_arch", lambda: "x86_64") with pytest.raises(cli.CliUnavailable): cli.ensure_binary(fetch=lambda url: b"not the binary") assert not any(tmp_path.rglob("cliairplay-*")) def test_a_matching_download_is_installed_executable(self, tmp_path, monkeypatch): from lintunes.airplay import cli data = b"pretend binary" monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path)) monkeypatch.setattr(cli, "_arch", lambda: "x86_64") monkeypatch.setitem(cli._SHA256, "x86_64", hashlib.sha256(data).hexdigest()) urls = [] path = cli.ensure_binary(fetch=lambda url: urls.append(url) or data) assert path.read_bytes() == data and cli.installed() == path assert urls == [cli._RELEASE + "cliairplay-linux-x86_64"] assert cli.ensure_binary(fetch=lambda url: 1 / 0) == path # cached def test_status_lines(self): from lintunes.airplay import cli assert cli.parse_status("[STATUS] playing elapsed_ms=1203") == ( "playing", {"elapsed_ms": "1203"}) assert cli.parse_status('[STATUS] error code=x detail="a b"') == ( "error", {"code": "x", "detail": "a b"}) assert cli.parse_status("[12:00] something") is None def test_args_carry_the_raop_txt(self): from lintunes.airplay import cli args = cli.build_args("/bin/cli", "10.0.0.9", 7000, {"et": "0,4", "md": "0,1,2", "ft": "0x1,0x2"}, {}, "/tmp/p", 50) assert args[0] == "/bin/cli" and args[-1] == "10.0.0.9" assert args[args.index("--et") + 1] == "0,4" assert args[args.index("--txt") + 1] == "ft=0x1,0x2" assert args[args.index("--volume") + 1] == "50" class TestFallback: def _controller(self): from lintunes.airplay.controller import AirPlayController controller = AirPlayController(MagicMock()) return controller def _conf(self): from pyatv.const import Protocol raop = SimpleNamespace(port=7000, properties={"am": "WiiM"}, protocol=Protocol.RAOP) conf = MagicMock(address="10.0.0.9") conf.get_service = lambda p: raop if p == Protocol.RAOP else None return conf def _connect(self, controller, config, native): from lintunes.airplay import cli, controller as module device = SimpleNamespace(name="tiat", host="10.0.0.9", uuid="x") with patch.object(module, "load_config", return_value=config), \ patch.object(type(controller), "_connect_native", native), \ patch.object(cli, "supported", return_value=True), \ patch.object(cli, "ensure_binary", return_value="/bin/cli"): return asyncio.run(controller._connect(self._conf(), device)) def test_a_native_failure_falls_back_to_cliairplay(self, qapp): async def native(*args): raise ConnectionError("refused") result = self._connect(self._controller(), {}, native) assert result["cli"] == "/bin/cli" assert result["target"]["host"] == "10.0.0.9" assert result["target"]["port"] == 7000 def test_native_when_it_connects_and_cli_when_asked(self, qapp): async def native(*args): return "native device" controller = self._controller() assert self._connect(controller, {}, native) == "native device" assert "cli" in self._connect(controller, {"airplay_backend": "cli"}, native) def test_native_only_reports_the_failure(self, qapp): async def native(*args): raise ConnectionError("refused") with pytest.raises(ConnectionError): self._connect(self._controller(), {"airplay_backend": "native"}, native)