124 lines
4.6 KiB
Python
124 lines
4.6 KiB
Python
"""Round 107: opening DJ mode with both decks empty doesn't stop the song —
|
|
Deck A loads it and takes over without a gap, and closing DJ mode goes back
|
|
to where the main player left off."""
|
|
|
|
import pytest
|
|
|
|
from lintunes.dj import analysis, stems
|
|
from lintunes.dj.audio import RATE, Source
|
|
from lintunes.player import Player
|
|
from tests.test_round93 import _tone
|
|
|
|
|
|
@pytest.fixture
|
|
def window(qapp, tmp_path, monkeypatch):
|
|
from lintunes.gui import dj_shelf
|
|
from lintunes.gui.main_window import MainWindow
|
|
from lintunes.library_manager import LibraryManager
|
|
from lintunes.models import Track
|
|
from lintunes.models.library import Library
|
|
from lintunes.preferences import Preferences
|
|
monkeypatch.setattr("lintunes.gui.main_window.load_config", lambda: {})
|
|
monkeypatch.setattr("lintunes.gui.main_window.save_config", lambda c: None)
|
|
monkeypatch.setattr(dj_shelf.DeckLoader, "request", lambda *a, **k: None)
|
|
monkeypatch.setattr(stems, "available", lambda: False)
|
|
monkeypatch.setattr(dj_shelf.tagging, "read_embedded_artwork", lambda p: None)
|
|
library = Library()
|
|
for tid in (1, 2):
|
|
library.tracks[tid] = Track(track_id=tid, name=f"Song {tid}", artist="Test",
|
|
location=f"/x{tid}.mp3", bpm=120)
|
|
made = MainWindow(LibraryManager(library, tmp_path), Preferences(tmp_path))
|
|
# A main player "playing" song 1, 42 s in.
|
|
state = {"track": library.tracks[1], "playing": True, "ms": 42_000, "seeks": []}
|
|
monkeypatch.setattr(Player, "current_track", property(lambda self: state["track"]))
|
|
player = made.player
|
|
player.is_playing = lambda: state["playing"]
|
|
player.position_ms = lambda: state["ms"]
|
|
player.pause = lambda: state.update(playing=False)
|
|
player.play = lambda: state.update(playing=True)
|
|
player.seek = lambda ms: state["seeks"].append(ms)
|
|
made.state = state
|
|
made.library = library
|
|
yield made
|
|
made.close()
|
|
|
|
|
|
def _finish_loading(window, track):
|
|
from lintunes.dj.loader import Loaded
|
|
samples = _tone(seconds=60.0)
|
|
window._dj._on_loader_result("A", Loaded(
|
|
track_id=track.track_id, source=Source(samples),
|
|
peaks=analysis.peaks(samples, RATE), bpm=120.0, offset=0.0, detected=False))
|
|
|
|
|
|
def _close(window, qapp):
|
|
window.set_dj_mode(False)
|
|
window._dj_anim.stop()
|
|
window._dj_closed()
|
|
|
|
|
|
def test_empty_decks_take_the_playing_song_over(window, qapp):
|
|
state = window.state
|
|
window.set_dj_mode(True)
|
|
shelf = window._dj
|
|
a = shelf.views[0]
|
|
assert state["playing"], "the song keeps playing while Deck A loads"
|
|
assert window.player.suspended
|
|
assert a.track is state["track"]
|
|
assert shelf.crossfader.value() == 0 # Deck A alone
|
|
_finish_loading(window, state["track"])
|
|
assert a.deck.playing
|
|
assert a.deck.position / RATE == pytest.approx(42.0 + shelf.latency_ms() / 1000, abs=0.01)
|
|
qapp.processEvents() # the latency passes
|
|
assert not state["playing"]
|
|
assert window._dj_pin == (1, 42_000, True)
|
|
_close(window, qapp)
|
|
assert state["seeks"] == [42_000] and state["playing"]
|
|
assert window._dj_pin is None
|
|
|
|
|
|
def test_a_paused_song_is_cued_and_stays_paused(window, qapp):
|
|
state = window.state
|
|
state["playing"] = False
|
|
window.set_dj_mode(True)
|
|
a = window._dj.views[0]
|
|
_finish_loading(window, state["track"])
|
|
assert not a.deck.playing
|
|
assert a.deck.position / RATE == pytest.approx(42.0, abs=0.01)
|
|
_close(window, qapp)
|
|
assert not state["playing"]
|
|
|
|
|
|
def test_closing_before_the_deck_loads_leaves_the_song_playing(window, qapp):
|
|
state = window.state
|
|
window.set_dj_mode(True)
|
|
_close(window, qapp)
|
|
assert state["playing"] and not window.player.suspended
|
|
assert window._dj.views[0].handoff is None
|
|
assert window._dj_pin is None
|
|
|
|
|
|
def test_a_loaded_deck_means_the_song_just_stops(window, qapp):
|
|
state = window.state
|
|
window.set_dj_mode(True)
|
|
_finish_loading(window, state["track"])
|
|
qapp.processEvents()
|
|
_close(window, qapp)
|
|
window._dj.load(window._dj.views[1], window.library.tracks[2])
|
|
window._dj.views[0].track = None # only Deck B holds something now
|
|
state["playing"] = True
|
|
window.set_dj_mode(True)
|
|
assert not state["playing"]
|
|
assert window._dj.views[0].track is None # nothing loaded for it
|
|
_close(window, qapp)
|
|
assert not state["playing"]
|
|
|
|
|
|
def test_a_failed_load_stops_the_song(window, qapp):
|
|
state = window.state
|
|
window.set_dj_mode(True)
|
|
window._dj._on_loader_failed("A", 1, "no decoder")
|
|
assert not state["playing"]
|
|
_close(window, qapp)
|
|
assert not state["playing"]
|