From c8bc52b4c6b0fdd904b4f7e5e8c9b71d052e0360 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Mon, 20 Jun 2011 13:43:20 +0300 Subject: [PATCH] Test and implement all direct checks of CanControl==true before doing the designated action --- mopidy/frontends/mpris.py | 12 ++++++++ .../frontends/mpris/player_interface_test.py | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/mopidy/frontends/mpris.py b/mopidy/frontends/mpris.py index 5232a385..a515412c 100644 --- a/mopidy/frontends/mpris.py +++ b/mopidy/frontends/mpris.py @@ -266,6 +266,9 @@ class MprisObject(dbus.service.Object): @dbus.service.method(dbus_interface=PLAYER_IFACE) def Stop(self): logger.debug(u'%s.Stop called', PLAYER_IFACE) + if not self.get_CanControl(): + logger.debug(u'%s.Stop not allowed', PLAYER_IFACE) + return # TODO Raise error self.backend.playback.stop().get() @dbus.service.method(dbus_interface=PLAYER_IFACE) @@ -345,6 +348,9 @@ class MprisObject(dbus.service.Object): return 'Playlist' def set_LoopStatus(self, value): + if not self.get_CanControl(): + logger.debug(u'Setting %s.LoopStatus not allowed', PLAYER_IFACE) + return # TODO Raise error if value == 'None': self.backend.playback.repeat = False self.backend.playback.single = False @@ -363,6 +369,9 @@ class MprisObject(dbus.service.Object): return self.backend.playback.shuffle.get() def set_Shuffle(self, value): + if not self.get_CanControl(): + logger.debug(u'Setting %s.Shuffle not allowed', PLAYER_IFACE) + return # TODO Raise error if value: self.backend.playback.shuffle = True else: @@ -374,6 +383,9 @@ class MprisObject(dbus.service.Object): return volume / 100.0 def set_Volume(self, value): + if not self.get_CanControl(): + logger.debug(u'Setting %s.Volume not allowed', PLAYER_IFACE) + return # TODO Raise error if value is None: return elif value < 0: diff --git a/tests/frontends/mpris/player_interface_test.py b/tests/frontends/mpris/player_interface_test.py index 32562fca..26e46184 100644 --- a/tests/frontends/mpris/player_interface_test.py +++ b/tests/frontends/mpris/player_interface_test.py @@ -56,6 +56,14 @@ class PlayerInterfaceTest(unittest.TestCase): result = self.mpris.Get(mpris.PLAYER_IFACE, 'LoopStatus') self.assertEqual('Playlist', result) + def test_set_loop_status_is_ignored_if_can_control_is_false(self): + self.mpris.get_CanControl = lambda *_: False + self.backend.playback.repeat = True + self.backend.playback.single = True + self.mpris.Set(mpris.PLAYER_IFACE, 'LoopStatus', 'None') + self.assertEquals(self.backend.playback.repeat.get(), True) + self.assertEquals(self.backend.playback.single.get(), True) + def test_set_loop_status_to_none_unsets_repeat_and_single(self): self.mpris.Set(mpris.PLAYER_IFACE, 'LoopStatus', 'None') self.assertEquals(self.backend.playback.repeat.get(), False) @@ -98,6 +106,12 @@ class PlayerInterfaceTest(unittest.TestCase): result = self.mpris.Get(mpris.PLAYER_IFACE, 'Shuffle') self.assertFalse(result) + def test_set_shuffle_is_ignored_if_can_control_is_false(self): + self.mpris.get_CanControl = lambda *_: False + self.backend.playback.shuffle = False + result = self.mpris.Set(mpris.PLAYER_IFACE, 'Shuffle', True) + self.assertFalse(self.backend.playback.shuffle.get()) + def test_set_shuffle_to_true_activates_shuffle_mode(self): self.backend.playback.shuffle = False self.assertFalse(self.backend.playback.shuffle.get()) @@ -123,6 +137,12 @@ class PlayerInterfaceTest(unittest.TestCase): result = self.mpris.Get(mpris.PLAYER_IFACE, 'Volume') self.assertEquals(result, 1) + def test_set_volume_is_ignored_if_can_control_is_false(self): + self.mpris.get_CanControl = lambda *_: False + self.mixer.volume = 0 + self.mpris.Set(mpris.PLAYER_IFACE, 'Volume', 1.0) + self.assertEquals(self.mixer.volume.get(), 0) + def test_set_volume_to_one_should_set_mixer_volume_to_100(self): self.mpris.Set(mpris.PLAYER_IFACE, 'Volume', 1.0) self.assertEquals(self.mixer.volume.get(), 100) @@ -282,6 +302,14 @@ class PlayerInterfaceTest(unittest.TestCase): self.mpris.PlayPause() self.assertEquals(self.backend.playback.state.get(), PLAYING) + def test_stop_is_ignored_if_can_control_is_false(self): + self.mpris.get_CanControl = lambda *_: False + self.backend.current_playlist.append([Track(uri='a'), Track(uri='b')]) + self.backend.playback.play() + self.assertEquals(self.backend.playback.state.get(), PLAYING) + self.mpris.Stop() + self.assertEquals(self.backend.playback.state.get(), PLAYING) + def test_stop_when_playing_should_stop_playback(self): self.backend.current_playlist.append([Track(uri='a'), Track(uri='b')]) self.backend.playback.play()