Update existing listener events to reflect that they only notify about track changes

This commit is contained in:
Thomas Adamcik 2011-07-19 02:29:40 +02:00
parent e919dcf627
commit 215ed61b0b
5 changed files with 32 additions and 33 deletions

View File

@ -326,7 +326,7 @@ class PlaybackController(object):
original_cp_track = self.current_cp_track
if self.cp_track_at_eot:
self._trigger_stopped_playing_event()
self._trigger_track_playback_ended()
self.play(self.cp_track_at_eot)
else:
self.stop(clear_current_track=True)
@ -354,7 +354,7 @@ class PlaybackController(object):
return
if self.cp_track_at_next:
self._trigger_stopped_playing_event()
self._trigger_track_playback_ended()
self.play(self.cp_track_at_next)
else:
self.stop(clear_current_track=True)
@ -402,7 +402,7 @@ class PlaybackController(object):
if self.random and self.current_cp_track in self._shuffled:
self._shuffled.remove(self.current_cp_track)
self._trigger_started_playing_event()
self._trigger_track_playback_started()
def previous(self):
"""Play the previous track."""
@ -410,7 +410,7 @@ class PlaybackController(object):
return
if self.state == self.STOPPED:
return
self._trigger_stopped_playing_event()
self._trigger_track_playback_ended()
self.play(self.cp_track_at_previous, on_error_step=-1)
def resume(self):
@ -454,25 +454,24 @@ class PlaybackController(object):
:type clear_current_track: boolean
"""
if self.state != self.STOPPED:
self._trigger_stopped_playing_event()
if self.provider.stop():
self._trigger_track_playback_ended()
self.state = self.STOPPED
if clear_current_track:
self.current_cp_track = None
def _trigger_started_playing_event(self):
logger.debug(u'Triggering started playing event')
def _trigger_track_playback_started(self):
logger.debug(u'Triggering track playback started event')
if self.current_track is None:
return
BackendListener.send('started_playing',
BackendListener.send('track_playback_started',
track=self.current_track)
def _trigger_stopped_playing_event(self):
# TODO Test that this is called on next/prev/end-of-track
logger.debug(u'Triggering stopped playing event')
def _trigger_track_playback_ended(self):
logger.debug(u'Triggering track playback ended event')
if self.current_track is None:
return
BackendListener.send('stopped_playing',
BackendListener.send('track_playback_ended',
track=self.current_track,
time_position=self.time_position)

View File

@ -57,7 +57,7 @@ class LastfmFrontend(ThreadingActor, BackendListener):
logger.error(u'Error during Last.fm setup: %s', e)
self.stop()
def started_playing(self, track):
def track_playback_started(self, track):
artists = ', '.join([a.name for a in track.artists])
duration = track.length and track.length // 1000 or 0
self.last_start_time = int(time.time())
@ -74,7 +74,7 @@ class LastfmFrontend(ThreadingActor, BackendListener):
pylast.MalformedResponseError, pylast.WSError) as e:
logger.warning(u'Error submitting playing track to Last.fm: %s', e)
def stopped_playing(self, track, time_position):
def track_playback_ended(self, track, time_position):
artists = ', '.join([a.name for a in track.artists])
duration = track.length and track.length // 1000 or 0
time_position = time_position // 1000

View File

@ -21,7 +21,7 @@ class BackendListener(object):
'kwargs': kwargs
}, target_class=BackendListener)
def started_playing(self, track):
def track_playback_started(self, track):
"""
Called whenever a new track starts playing.
@ -32,9 +32,9 @@ class BackendListener(object):
"""
pass
def stopped_playing(self, track, time_position):
def track_playback_ended(self, track, time_position):
"""
Called whenever playback is stopped.
Called whenever playback of a track ends.
*MAY* be implemented by actor.

View File

@ -11,8 +11,8 @@ from mopidy.models import Track
class BackendEventsTest(unittest.TestCase):
def setUp(self):
self.events = {
'started_playing': threading.Event(),
'stopped_playing': threading.Event(),
'track_playback_started': threading.Event(),
'track_playback_ended': threading.Event(),
}
self.backend = DummyBackend.start().proxy()
self.listener = DummyBackendListener.start(self.events).proxy()
@ -20,26 +20,26 @@ class BackendEventsTest(unittest.TestCase):
def tearDown(self):
ActorRegistry.stop_all()
def test_play_sends_started_playing_event(self):
def test_play_sends_track_playback_started_event(self):
self.backend.current_playlist.add([Track(uri='a')])
self.backend.playback.play()
self.events['started_playing'].wait(timeout=1)
self.assertTrue(self.events['started_playing'].is_set())
self.events['track_playback_started'].wait(timeout=1)
self.assertTrue(self.events['track_playback_started'].is_set())
def test_stop_sends_stopped_playing_event(self):
def test_stop_sends_track_playback_ended_event(self):
self.backend.current_playlist.add([Track(uri='a')])
self.backend.playback.play()
self.backend.playback.stop()
self.events['stopped_playing'].wait(timeout=1)
self.assertTrue(self.events['stopped_playing'].is_set())
self.events['track_playback_ended'].wait(timeout=1)
self.assertTrue(self.events['track_playback_ended'].is_set())
class DummyBackendListener(ThreadingActor, BackendListener):
def __init__(self, events):
self.events = events
def started_playing(self, track):
self.events['started_playing'].set()
def track_playback_started(self, track):
self.events['track_playback_started'].set()
def stopped_playing(self, track, time_position):
self.events['stopped_playing'].set()
def track_playback_ended(self, track, time_position):
self.events['track_playback_ended'].set()

View File

@ -7,8 +7,8 @@ class BackendListenerTest(unittest.TestCase):
def setUp(self):
self.listener = BackendListener()
def test_listener_has_default_impl_for_the_started_playing_event(self):
self.listener.started_playing(Track())
def test_listener_has_default_impl_for_the_track_playback_started(self):
self.listener.track_playback_started(Track())
def test_listener_has_default_impl_for_the_stopped_playing_event(self):
self.listener.stopped_playing(Track(), 0)
def test_listener_has_default_impl_for_the_track_playback_ended(self):
self.listener.track_playback_ended(Track(), 0)