playlist: Give Playlists a track-requested signal

We need to do some bookkeeping inside the Playlist before notifying the
Factory that a track has been requested.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-04-28 10:54:49 -04:00
parent 14bcef6e52
commit 55486c20c3
2 changed files with 38 additions and 0 deletions

View File

@ -139,6 +139,13 @@ class Playlist(model.TrackidModel):
if self.__playlist is not None:
self.__playlist.remove_track(track)
def request_track(self, track: db.tracks.Track) -> None:
"""Request a track for playback directly."""
if self.index(track) is not None:
if not self.can_go_next:
self.__picked.trackids = set()
self.emit("track-requested", track)
@GObject.Property(type=db.tracks.Track)
def current_track(self) -> db.tracks.Track | None:
"""Get the current Track of the Playlist."""
@ -217,3 +224,8 @@ class Playlist(model.TrackidModel):
def sort_order(self, new_order: str) -> None:
if self.__playlist is not None:
self.__playlist.sort_order = new_order
@GObject.Signal(arg_types=(db.tracks.Track,))
def track_requested(self, track: db.tracks.Track) -> None:
"""Signal that a track has been requested for playback."""
self.current_track = track

View File

@ -210,6 +210,32 @@ class TestPlaylist(tests.util.TestCase):
self.assertEqual(self.db_plist.current_trackid, self.track1.trackid)
notify.assert_called()
def test_request_track(self):
"""Test the Playlist request_track() function."""
requested = unittest.mock.Mock()
self.playlist.connect("track-requested", requested)
self.playlist.request_track(self.track1)
requested.assert_not_called()
self.playlist.playlist = self.db_plist
self.playlist.request_track(self.track1)
requested.assert_not_called()
self.db_plist.add_track(self.track1)
self.playlist.request_track(self.track1)
requested.assert_called_with(self.playlist, self.track1)
self.assertEqual(self.playlist.current_track, self.track1)
self.db_plist.add_track(self.track2)
self.playlist.request_track(self.track2)
self.assertFalse(self.playlist.can_go_next)
self.playlist.request_track(self.track1)
self.assertSetEqual(self.playlist._Playlist__picked.trackids,
{self.track1.trackid})
self.assertTrue(self.playlist.can_go_next)
def test_current_track(self):
"""Test the Playlist current-track property."""
self.assertIsNone(self.playlist.current_track)