162 lines
6.0 KiB
Python
162 lines
6.0 KiB
Python
"""Round 119: the Identify windows find the album art too.
|
|
|
|
- One song: the Identify window searches a cover for the album its grid
|
|
proposes, and searches again when the proposal changes.
|
|
- Embed ticks itself only when the song has no cover yet.
|
|
- A playlist batch gets a cover per album: one search at a time, a Cover
|
|
column, the selected song's album beside the table.
|
|
"""
|
|
import pytest
|
|
from PyQt6.QtCore import QBuffer, QByteArray, QIODevice, QObject, pyqtSignal
|
|
from PyQt6.QtGui import QImage
|
|
|
|
from lintunes.art_search import ArtCandidate
|
|
from lintunes.fingerprint import IdentifyCandidate
|
|
from lintunes.gui import art_picker
|
|
from lintunes.gui.identify_batch_dialog import IdentifyBatchDialog
|
|
from lintunes.gui.identify_dialog import IdentifyDialog
|
|
from lintunes.identify_batch import analyze
|
|
from lintunes.models.track import Track
|
|
|
|
|
|
class _Signals(QObject):
|
|
search_finished = pyqtSignal(object)
|
|
image_finished = pyqtSignal(object)
|
|
|
|
|
|
@pytest.fixture
|
|
def fetchers(monkeypatch):
|
|
made = []
|
|
|
|
class _Fetcher:
|
|
def __init__(self):
|
|
self._s = _Signals()
|
|
self.search_finished = self._s.search_finished
|
|
self.image_finished = self._s.image_finished
|
|
self.searched, self.fetched = [], []
|
|
made.append(self)
|
|
|
|
def search(self, artist, album):
|
|
self.searched.append((artist, album))
|
|
|
|
def fetch(self, candidate):
|
|
self.fetched.append(candidate)
|
|
monkeypatch.setattr(art_picker, "AlbumArtFetcher", _Fetcher)
|
|
return made
|
|
|
|
|
|
def _png():
|
|
image = QImage(4, 4, QImage.Format.Format_RGB32)
|
|
image.fill(0x00ff00)
|
|
data = QByteArray()
|
|
buf = QBuffer(data)
|
|
buf.open(QIODevice.OpenModeFlag.WriteOnly)
|
|
image.save(buf, "PNG")
|
|
return bytes(data)
|
|
|
|
|
|
def _c(name, album, artist="Digable Planets", score=0.9):
|
|
return IdentifyCandidate(score=score, source="acoustid", name=name,
|
|
artist=artist, album=album)
|
|
|
|
|
|
def _answer(fetcher, album="Reachin'"):
|
|
art = ArtCandidate("Digable Planets", album, "http://x/a.jpg")
|
|
fetcher.search_finished.emit({"candidates": [art]})
|
|
fetcher.image_finished.emit({"candidate": art, "image": _png(),
|
|
"mime": "image/png"})
|
|
|
|
|
|
def test_one_song_searches_the_proposed_album(qapp, fetchers):
|
|
track = Track(track_id=1, name="rebirth of slick [abc123]")
|
|
dialog = IdentifyDialog(track, [_c("Rebirth of Slick", "Reachin'"),
|
|
_c("Rebirth of Slick", "Best Of")],
|
|
find_art=True)
|
|
assert fetchers[-1].searched == [("Digable Planets", "Reachin'")]
|
|
_answer(fetchers[-1])
|
|
assert dialog.art._embed.isChecked()
|
|
dialog._on_accept()
|
|
assert dialog.selected_image == _png()
|
|
assert dialog.selected_mime == "image/png"
|
|
|
|
|
|
def test_another_candidate_searches_again_and_old_answers_are_ignored(
|
|
qapp, fetchers):
|
|
track = Track(track_id=1, name="x")
|
|
dialog = IdentifyDialog(track, [_c("Song", "Reachin'"),
|
|
_c("Song", "Best Of")], find_art=True)
|
|
first = fetchers[-1]
|
|
dialog.panel._combo.setCurrentIndex(1)
|
|
assert fetchers[-1].searched == [("Digable Planets", "Best Of")]
|
|
_answer(first) # the old search, late
|
|
assert dialog.art.image() is None
|
|
dialog._on_accept()
|
|
assert dialog.selected_image is None
|
|
|
|
|
|
def test_typing_an_album_searches_after_a_pause(qapp, fetchers):
|
|
track = Track(track_id=1, name="x")
|
|
dialog = IdentifyDialog(track, [_c("Song", "")], find_art=True)
|
|
assert fetchers == [] # nothing to search by
|
|
album = dialog.panel._editors["album"]
|
|
dialog.panel._checks["album"].setEnabled(True)
|
|
dialog.panel._checks["album"].setChecked(True)
|
|
album.setText("Blowout Comb")
|
|
assert fetchers == [] # still typing
|
|
dialog._art_timer.timeout.emit()
|
|
assert fetchers[-1].searched == [("Digable Planets", "Blowout Comb")]
|
|
|
|
|
|
def test_a_song_with_a_cover_isnt_ticked_to_replace_it(qapp, fetchers):
|
|
track = Track(track_id=1, name="x")
|
|
dialog = IdentifyDialog(track, [_c("Song", "Reachin'")], find_art=True,
|
|
embed_art=False)
|
|
_answer(fetchers[-1])
|
|
assert not dialog.art._embed.isChecked()
|
|
dialog._on_accept()
|
|
assert dialog.selected_image is None
|
|
|
|
|
|
def test_plain_identify_dialog_searches_nothing(qapp, fetchers):
|
|
dialog = IdentifyDialog(Track(track_id=1, name="x"),
|
|
[_c("Song", "Reachin'")])
|
|
assert dialog.art is None and fetchers == []
|
|
|
|
|
|
def test_a_playlist_gets_a_cover_per_album_one_search_at_a_time(
|
|
qapp, fetchers):
|
|
analysis = analyze([
|
|
(Track(track_id=1), [_c("A", "Reachin'")]),
|
|
(Track(track_id=2), [_c("B", "Blowout Comb")]),
|
|
(Track(track_id=3), [_c("C", "Reachin'")]),
|
|
(Track(track_id=4), []), # nothing to search by
|
|
])
|
|
dialog = IdentifyBatchDialog(analysis, None)
|
|
assert not analysis.is_album
|
|
assert [f.searched for f in fetchers] == [
|
|
[("Digable Planets", "Reachin'")]]
|
|
_answer(fetchers[0])
|
|
assert [f.searched for f in fetchers][1:] == [
|
|
[("Digable Planets", "Blowout Comb")]]
|
|
|
|
rows = {r.track.track_id: i for i, r in enumerate(dialog._rows)}
|
|
cover = dialog._cover_col
|
|
assert not dialog._table.item(rows[1], cover).icon().isNull()
|
|
assert not dialog._table.item(rows[3], cover).icon().isNull()
|
|
assert dialog._table.item(rows[2], cover).icon().isNull()
|
|
|
|
dialog._table.selectRow(rows[2])
|
|
assert dialog._cover_stack.currentWidget() is \
|
|
dialog._pickers[("digable planets", "blowout comb")]
|
|
dialog._table.selectRow(rows[4])
|
|
assert dialog._cover_stack.currentWidget() is dialog._no_album
|
|
|
|
[(art_rows, image, _mime)] = dialog.selected_art()
|
|
assert sorted(r.track.track_id for r in art_rows) == [1, 3]
|
|
assert image == _png()
|
|
|
|
# Unticking Embed takes the thumbnails away and the cover with them.
|
|
dialog._pickers[("digable planets", "reachin'")]._embed.setChecked(False)
|
|
assert dialog._table.item(rows[1], cover).icon().isNull()
|
|
assert dialog.selected_art() == []
|