Exemplo n.º 1
0
    def test_create_stored_playlist_overwrites_old_disk_contents(self):
        track = Track(uri=generate_song(1))
        self.core.tracklist.add(tracks=[track])
        playlist_path = os.path.join(self.playlists_dir, 'test.json')
        self.core.playlists.create_stored('test')
        with open(playlist_path, 'r') as fp:
            playlist = json.load(fp, object_hook=models.model_json_decoder)
            self.assertEqual(track, playlist.tracks[0])

        self.core.tracklist.clear()
        new_track = Track(uri=generate_song(2))
        self.core.tracklist.add(tracks=[new_track])
        self.core.playlists.create_stored('test')
        with open(playlist_path, 'r') as fp:
            playlist = json.load(fp, object_hook=models.model_json_decoder)
            self.assertEqual(new_track, playlist.tracks[0])
Exemplo n.º 2
0
 def test_create_stored_playlist_contents_is_written_to_disk(self):
     track = Track(uri=generate_song(1))
     self.core.tracklist.add(tracks=[track])
     playlist_path = os.path.join(self.playlists_dir, 'test.json')
     self.core.playlists.create_stored('test')
     with open(playlist_path, 'r') as fp:
         playlist = json.load(fp, object_hook=models.model_json_decoder)
         self.assertEqual(len(playlist.tracks), 1)
         self.assertEqual(track, playlist.tracks[0])
Exemplo n.º 3
0
    def test_stored_playlists_are_loaded_at_startup(self):
        track = Track(uri=generate_song(1))
        self.core.tracklist.add(tracks=[track])
        self.core.playlists.create_stored('test')

        backend = self.backend_class(config=self.config, audio=self.audio)

        self.assert_(backend.playlists.playlists)
        self.assertEqual('test', backend.playlists.playlists[0].name)
        self.assertEqual(track, backend.playlists.playlists[0].tracks[0])
Exemplo n.º 4
0
    def test_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1))
        playlist = self.core.playlists.create('test')
        playlist_path = os.path.join(self.playlists_dir, 'test.m3u')
        playlist = playlist.copy(tracks=[track])
        playlist = self.core.playlists.save(playlist)

        with open(playlist_path) as playlist_file:
            contents = playlist_file.read()

        self.assertEqual(track.uri, contents.strip())
Exemplo n.º 5
0
    def test_extended_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1), name='Test', length=60000)
        playlist = self.core.playlists.create('test')
        playlist_path = os.path.join(self.playlists_dir, 'test.m3u')
        playlist = playlist.copy(tracks=[track])
        playlist = self.core.playlists.save(playlist)

        with open(playlist_path) as playlist_file:
            contents = playlist_file.read().splitlines()

        self.assertEqual(contents, ['#EXTM3U', '#EXTINF:60,Test', track.uri])
Exemplo n.º 6
0
    def test_extended_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1), name='Test', length=60000)
        playlist = self.core.playlists.create('test')
        playlist_path = os.path.join(self.playlists_dir, 'test.m3u')
        playlist = playlist.copy(tracks=[track])
        playlist = self.core.playlists.save(playlist)

        with open(playlist_path) as playlist_file:
            contents = playlist_file.read().splitlines()

        self.assertEqual(contents, ['#EXTM3U', '#EXTINF:60,Test', track.uri])
Exemplo n.º 7
0
    def test_playlist_contents_is_written_to_disk(self):
        track = Track(uri=generate_song(1))
        playlist = self.core.playlists.create('test')
        playlist_path = os.path.join(self.playlists_dir, 'test.m3u')
        playlist = playlist.copy(tracks=[track])
        playlist = self.core.playlists.save(playlist)

        with open(playlist_path) as playlist_file:
            contents = playlist_file.read()

        self.assertEqual(track.uri, contents.strip())
Exemplo n.º 8
0
class LocalPlaybackProviderTest(unittest.TestCase):
    config = {
        'core': {
            'data_dir': path_to_data_dir(''),
            'max_tracklist_length': 10000,
        },
        'local': {
            'media_dir': path_to_data_dir(''),
            'library': 'json',
        }
    }

    # We need four tracks so that our shuffled track tests behave nicely with
    # reversed as a fake shuffle. Ensuring that shuffled order is [4,3,2,1] and
    # normal order [1,2,3,4] which means next_track != next_track_with_random
    tracks = [Track(uri=generate_song(i), length=4464) for i in (1, 2, 3, 4)]

    def add_track(self, uri):
        track = Track(uri=uri, length=4464)
        self.tracklist.add([track])

    def trigger_end_of_track(self):
        self.playback._on_end_of_track()

    def run(self, result=None):
        with deprecation.ignore('core.tracklist.add:tracks_arg'):
            return super(LocalPlaybackProviderTest, self).run(result)

    def setUp(self):  # noqa: N802
        self.audio = dummy_audio.create_proxy()
        self.backend = actor.LocalBackend.start(config=self.config,
                                                audio=self.audio).proxy()
        self.core = core.Core(self.config, backends=[self.backend])
        self.playback = self.core.playback
        self.tracklist = self.core.tracklist

        assert len(self.tracks) >= 3, \
            'Need at least three tracks to run tests.'
        assert self.tracks[0].length >= 2000, \
            'First song needs to be at least 2000 miliseconds'

    def tearDown(self):  # noqa: N802
        pykka.ActorRegistry.stop_all()

    def test_uri_scheme(self):
        self.assertNotIn('file', self.core.uri_schemes)
        self.assertIn('local', self.core.uri_schemes)

    def test_play_mp3(self):
        self.add_track('local:track:blank.mp3')
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    def test_play_ogg(self):
        self.add_track('local:track:blank.ogg')
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    def test_play_flac(self):
        self.add_track('local:track:blank.flac')
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    def test_play_uri_with_non_ascii_bytes(self):
        # Regression test: If trying to do .split(u':') on a bytestring, the
        # string will be decoded from ASCII to Unicode, which will crash on
        # non-ASCII strings, like the bytestring the following URI decodes to.
        self.add_track('local:track:12%20Doin%E2%80%99%20It%20Right.flac')
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    def test_initial_state_is_stopped(self):
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    def test_play_with_empty_playlist(self):
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    def test_play_with_empty_playlist_return_value(self):
        self.assertEqual(self.playback.play(), None)

    @populate_tracklist
    def test_play_state(self):
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_play_return_value(self):
        self.assertEqual(self.playback.play(), None)

    @populate_tracklist
    def test_play_track_state(self):
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.playback.play(self.tracklist.tl_tracks[-1])
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_play_track_return_value(self):
        self.assertEqual(self.playback.play(self.tracklist.tl_tracks[-1]),
                         None)

    @populate_tracklist
    def test_play_when_playing(self):
        self.playback.play()
        track = self.playback.current_track
        self.playback.play()
        self.assertEqual(track, self.playback.current_track)

    @populate_tracklist
    def test_play_when_paused(self):
        self.playback.play()
        track = self.playback.current_track
        self.playback.pause()
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.assertEqual(track, self.playback.current_track)

    @populate_tracklist
    def test_play_when_pause_after_next(self):
        self.playback.play()
        self.playback.next()
        self.playback.next()
        track = self.playback.current_track
        self.playback.pause()
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.assertEqual(track, self.playback.current_track)

    @populate_tracklist
    def test_play_sets_current_track(self):
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])

    @populate_tracklist
    def test_play_track_sets_current_track(self):
        self.playback.play(self.tracklist.tl_tracks[-1])
        self.assertEqual(self.playback.current_track, self.tracks[-1])

    @populate_tracklist
    def test_play_skips_to_next_track_on_failure(self):
        # If backend's play() returns False, it is a failure.
        return_values = [True, False]
        self.backend.playback.play = lambda: return_values.pop()
        self.playback.play()
        self.assertNotEqual(self.playback.current_track, self.tracks[0])
        self.assertEqual(self.playback.current_track, self.tracks[1])

    @populate_tracklist
    def test_current_track_after_completed_playlist(self):
        self.playback.play(self.tracklist.tl_tracks[-1])
        self.trigger_end_of_track()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.assertEqual(self.playback.current_track, None)

        self.playback.play(self.tracklist.tl_tracks[-1])
        self.playback.next()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.assertEqual(self.playback.current_track, None)

    @populate_tracklist
    def test_previous(self):
        self.playback.play()
        self.playback.next()
        self.playback.previous()
        self.assertEqual(self.playback.current_track, self.tracks[0])

    @populate_tracklist
    def test_previous_more(self):
        self.playback.play()  # At track 0
        self.playback.next()  # At track 1
        self.playback.next()  # At track 2
        self.playback.previous()  # At track 1
        self.assertEqual(self.playback.current_track, self.tracks[1])

    @populate_tracklist
    def test_previous_return_value(self):
        self.playback.play()
        self.playback.next()
        self.assertEqual(self.playback.previous(), None)

    @populate_tracklist
    def test_previous_does_not_trigger_playback(self):
        self.playback.play()
        self.playback.next()
        self.playback.stop()
        self.playback.previous()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_previous_at_start_of_playlist(self):
        self.playback.previous()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.assertEqual(self.playback.current_track, None)

    def test_previous_for_empty_playlist(self):
        self.playback.previous()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.assertEqual(self.playback.current_track, None)

    @populate_tracklist
    def test_previous_skips_to_previous_track_on_failure(self):
        # If backend's play() returns False, it is a failure.
        return_values = [True, False, True]
        self.backend.playback.play = lambda: return_values.pop()
        self.playback.play(self.tracklist.tl_tracks[2])
        self.assertEqual(self.playback.current_track, self.tracks[2])
        self.playback.previous()
        self.assertNotEqual(self.playback.current_track, self.tracks[1])
        self.assertEqual(self.playback.current_track, self.tracks[0])

    @populate_tracklist
    def test_next(self):
        self.playback.play()

        tl_track = self.playback.current_tl_track
        old_position = self.tracklist.index(tl_track)
        old_uri = tl_track.track.uri

        self.playback.next()

        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.index(tl_track), old_position + 1)
        self.assertNotEqual(self.playback.current_track.uri, old_uri)

    @populate_tracklist
    def test_next_return_value(self):
        self.playback.play()
        self.assertEqual(self.playback.next(), None)

    @populate_tracklist
    def test_next_does_not_trigger_playback(self):
        self.playback.next()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_next_at_end_of_playlist(self):
        self.playback.play()

        for i, track in enumerate(self.tracks):
            self.assertEqual(self.playback.state, PlaybackState.PLAYING)
            self.assertEqual(self.playback.current_track, track)
            tl_track = self.playback.current_tl_track
            self.assertEqual(self.tracklist.index(tl_track), i)

            self.playback.next()

        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_next_until_end_of_playlist_and_play_from_start(self):
        self.playback.play()

        for _ in self.tracks:
            self.playback.next()

        self.assertEqual(self.playback.current_track, None)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.assertEqual(self.playback.current_track, self.tracks[0])

    def test_next_for_empty_playlist(self):
        self.playback.next()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_next_skips_to_next_track_on_failure(self):
        # If backend's play() returns False, it is a failure.
        return_values = [True, False, True]
        self.backend.playback.play = lambda: return_values.pop()
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])
        self.playback.next()
        self.assertNotEqual(self.playback.current_track, self.tracks[1])
        self.assertEqual(self.playback.current_track, self.tracks[2])

    @populate_tracklist
    def test_next_track_before_play(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[0])

    @populate_tracklist
    def test_next_track_during_play(self):
        self.playback.play()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[1])

    @populate_tracklist
    def test_next_track_after_previous(self):
        self.playback.play()
        self.playback.next()
        self.playback.previous()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[1])

    def test_next_track_empty_playlist(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track), None)

    @populate_tracklist
    def test_next_track_at_end_of_playlist(self):
        self.playback.play()
        for _ in self.tracklist.tl_tracks[1:]:
            self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track), None)

    @populate_tracklist
    def test_next_track_at_end_of_playlist_with_repeat(self):
        self.tracklist.repeat = True
        self.playback.play()
        for _ in self.tracks[1:]:
            self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[0])

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_next_track_with_random(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        current_tl_track = self.playback.current_tl_track
        next_tl_track = self.tracklist.next_track(current_tl_track)
        self.assertEqual(next_tl_track, self.tl_tracks[-1])

    @populate_tracklist
    def test_next_with_consume(self):
        self.tracklist.consume = True
        self.playback.play()
        self.playback.next()
        self.assertNotIn(self.tracks[0], self.tracklist.tracks)

    @populate_tracklist
    def test_next_with_single_and_repeat(self):
        self.tracklist.single = True
        self.tracklist.repeat = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])
        self.playback.next()
        self.assertEqual(self.playback.current_track, self.tracks[1])

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_next_with_random(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[-1])
        self.playback.next()
        self.assertEqual(self.playback.current_track, self.tracks[-2])

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_next_track_with_random_after_append_playlist(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        current_tl_track = self.playback.current_tl_track

        expected_tl_track = self.tracklist.tl_tracks[-1]
        next_tl_track = self.tracklist.next_track(current_tl_track)

        # Baseline checking that first next_track is last tl track per our fake
        # shuffle.
        self.assertEqual(next_tl_track, expected_tl_track)

        self.tracklist.add(self.tracks[:1])

        old_next_tl_track = next_tl_track
        expected_tl_track = self.tracklist.tl_tracks[-1]
        next_tl_track = self.tracklist.next_track(current_tl_track)

        # Verify that first next track has changed since we added to the
        # playlist.
        self.assertEqual(next_tl_track, expected_tl_track)
        self.assertNotEqual(next_tl_track, old_next_tl_track)

    @populate_tracklist
    def test_end_of_track(self):
        self.playback.play()

        tl_track = self.playback.current_tl_track
        old_position = self.tracklist.index(tl_track)
        old_uri = tl_track.track.uri

        self.trigger_end_of_track()

        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.index(tl_track), old_position + 1)
        self.assertNotEqual(self.playback.current_track.uri, old_uri)

    @populate_tracklist
    def test_end_of_track_return_value(self):
        self.playback.play()
        self.assertEqual(self.trigger_end_of_track(), None)

    @populate_tracklist
    def test_end_of_track_does_not_trigger_playback(self):
        self.trigger_end_of_track()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_end_of_track_at_end_of_playlist(self):
        self.playback.play()

        for i, track in enumerate(self.tracks):
            self.assertEqual(self.playback.state, PlaybackState.PLAYING)
            self.assertEqual(self.playback.current_track, track)
            tl_track = self.playback.current_tl_track
            self.assertEqual(self.tracklist.index(tl_track), i)

            self.trigger_end_of_track()

        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_end_of_track_until_end_of_playlist_and_play_from_start(self):
        self.playback.play()

        for _ in self.tracks:
            self.trigger_end_of_track()

        self.assertEqual(self.playback.current_track, None)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.assertEqual(self.playback.current_track, self.tracks[0])

    def test_end_of_track_for_empty_playlist(self):
        self.trigger_end_of_track()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_end_of_track_skips_to_next_track_on_failure(self):
        # If backend's play() returns False, it is a failure.
        return_values = [True, False, True]
        self.backend.playback.play = lambda: return_values.pop()
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])
        self.trigger_end_of_track()
        self.assertNotEqual(self.playback.current_track, self.tracks[1])
        self.assertEqual(self.playback.current_track, self.tracks[2])

    @populate_tracklist
    def test_end_of_track_track_before_play(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[0])

    @populate_tracklist
    def test_end_of_track_track_during_play(self):
        self.playback.play()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[1])

    @populate_tracklist
    def test_end_of_track_track_after_previous(self):
        self.playback.play()
        self.trigger_end_of_track()
        self.playback.previous()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[1])

    def test_end_of_track_track_empty_playlist(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track), None)

    @populate_tracklist
    def test_end_of_track_track_at_end_of_playlist(self):
        self.playback.play()
        for _ in self.tracklist.tl_tracks[1:]:
            self.trigger_end_of_track()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track), None)

    @populate_tracklist
    def test_end_of_track_track_at_end_of_playlist_with_repeat(self):
        self.tracklist.repeat = True
        self.playback.play()
        for _ in self.tracks[1:]:
            self.trigger_end_of_track()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[0])

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_end_of_track_track_with_random(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track),
                         self.tl_tracks[-1])

    @populate_tracklist
    def test_end_of_track_with_consume(self):
        self.tracklist.consume = True
        self.playback.play()
        self.trigger_end_of_track()
        self.assertNotIn(self.tracks[0], self.tracklist.tracks)

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_end_of_track_with_random(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[-1])
        self.trigger_end_of_track()
        self.assertEqual(self.playback.current_track, self.tracks[-2])

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_end_of_track_track_with_random_after_append_playlist(
            self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        current_tl_track = self.playback.current_tl_track

        expected_tl_track = self.tracklist.tl_tracks[-1]
        eot_tl_track = self.tracklist.eot_track(current_tl_track)

        # Baseline checking that first eot_track is last tl track per our fake
        # shuffle.
        self.assertEqual(eot_tl_track, expected_tl_track)

        self.tracklist.add(self.tracks[:1])

        old_eot_tl_track = eot_tl_track
        expected_tl_track = self.tracklist.tl_tracks[-1]
        eot_tl_track = self.tracklist.eot_track(current_tl_track)

        # Verify that first next track has changed since we added to the
        # playlist.
        self.assertEqual(eot_tl_track, expected_tl_track)
        self.assertNotEqual(eot_tl_track, old_eot_tl_track)

    @populate_tracklist
    def test_previous_track_before_play(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.previous_track(tl_track), None)

    @populate_tracklist
    def test_previous_track_after_play(self):
        self.playback.play()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.previous_track(tl_track), None)

    @populate_tracklist
    def test_previous_track_after_next(self):
        self.playback.play()
        self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.previous_track(tl_track),
                         self.tl_tracks[0])

    @populate_tracklist
    def test_previous_track_after_previous(self):
        self.playback.play()  # At track 0
        self.playback.next()  # At track 1
        self.playback.next()  # At track 2
        self.playback.previous()  # At track 1
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.previous_track(tl_track),
                         self.tl_tracks[0])

    def test_previous_track_empty_playlist(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.previous_track(tl_track), None)

    @populate_tracklist
    def test_previous_track_with_consume(self):
        self.tracklist.consume = True
        for _ in self.tracks:
            self.playback.next()
            tl_track = self.playback.current_tl_track
            self.assertEqual(self.tracklist.previous_track(tl_track),
                             self.playback.current_tl_track)

    @populate_tracklist
    def test_previous_track_with_random(self):
        self.tracklist.random = True
        for _ in self.tracks:
            self.playback.next()
            tl_track = self.playback.current_tl_track
            self.assertEqual(self.tracklist.previous_track(tl_track),
                             self.playback.current_tl_track)

    @populate_tracklist
    def test_initial_current_track(self):
        self.assertEqual(self.playback.current_track, None)

    @populate_tracklist
    def test_current_track_during_play(self):
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])

    @populate_tracklist
    def test_current_track_after_next(self):
        self.playback.play()
        self.playback.next()
        self.assertEqual(self.playback.current_track, self.tracks[1])

    @populate_tracklist
    def test_initial_tracklist_position(self):
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.index(tl_track), None)

    @populate_tracklist
    def test_tracklist_position_during_play(self):
        self.playback.play()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.index(tl_track), 0)

    @populate_tracklist
    def test_tracklist_position_after_next(self):
        self.playback.play()
        self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.index(tl_track), 1)

    @populate_tracklist
    def test_tracklist_position_at_end_of_playlist(self):
        self.playback.play(self.tracklist.tl_tracks[-1])
        self.trigger_end_of_track()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.index(tl_track), None)

    def test_on_tracklist_change_gets_called(self):
        callback = self.playback._on_tracklist_change

        def wrapper():
            wrapper.called = True
            return callback()

        wrapper.called = False

        self.playback._on_tracklist_change = wrapper
        self.tracklist.add([Track()])

        self.assert_(wrapper.called)

    @unittest.SkipTest  # Blocks for 10ms
    @populate_tracklist
    def test_end_of_track_callback_gets_called(self):
        self.playback.play()
        result = self.playback.seek(self.tracks[0].length - 10)
        self.assertTrue(result, 'Seek failed')
        message = self.core_queue.get(True, 1)
        self.assertEqual('end_of_track', message['command'])

    @populate_tracklist
    def test_on_tracklist_change_when_playing(self):
        self.playback.play()
        current_track = self.playback.current_track
        self.tracklist.add([self.tracks[2]])
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.assertEqual(self.playback.current_track, current_track)

    @populate_tracklist
    def test_on_tracklist_change_when_stopped(self):
        self.tracklist.add([self.tracks[2]])
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.assertEqual(self.playback.current_track, None)

    @populate_tracklist
    def test_on_tracklist_change_when_paused(self):
        self.playback.play()
        self.playback.pause()
        current_track = self.playback.current_track
        self.tracklist.add([self.tracks[2]])
        self.assertEqual(self.playback.state, PlaybackState.PAUSED)
        self.assertEqual(self.playback.current_track, current_track)

    @populate_tracklist
    def test_pause_when_stopped(self):
        self.playback.pause()
        self.assertEqual(self.playback.state, PlaybackState.PAUSED)

    @populate_tracklist
    def test_pause_when_playing(self):
        self.playback.play()
        self.playback.pause()
        self.assertEqual(self.playback.state, PlaybackState.PAUSED)

    @populate_tracklist
    def test_pause_when_paused(self):
        self.playback.play()
        self.playback.pause()
        self.playback.pause()
        self.assertEqual(self.playback.state, PlaybackState.PAUSED)

    @populate_tracklist
    def test_pause_return_value(self):
        self.playback.play()
        self.assertEqual(self.playback.pause(), None)

    @populate_tracklist
    def test_resume_when_stopped(self):
        self.playback.resume()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_resume_when_playing(self):
        self.playback.play()
        self.playback.resume()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_resume_when_paused(self):
        self.playback.play()
        self.playback.pause()
        self.playback.resume()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_resume_return_value(self):
        self.playback.play()
        self.playback.pause()
        self.assertEqual(self.playback.resume(), None)

    @unittest.SkipTest  # Uses sleep and might not work with LocalBackend
    @populate_tracklist
    def test_resume_continues_from_right_position(self):
        self.playback.play()
        time.sleep(0.2)
        self.playback.pause()
        self.playback.resume()
        self.assertNotEqual(self.playback.time_position, 0)

    @populate_tracklist
    def test_seek_when_stopped(self):
        result = self.playback.seek(1000)
        self.assert_(result, 'Seek return value was %s' % result)

    @populate_tracklist
    def test_seek_when_stopped_updates_position(self):
        self.playback.seek(1000)
        position = self.playback.time_position
        self.assertGreaterEqual(position, 990)

    def test_seek_on_empty_playlist(self):
        self.assertFalse(self.playback.seek(0))

    def test_seek_on_empty_playlist_updates_position(self):
        self.playback.seek(0)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_seek_when_stopped_triggers_play(self):
        self.playback.seek(0)
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_seek_when_playing(self):
        self.playback.play()
        result = self.playback.seek(self.tracks[0].length - 1000)
        self.assert_(result, 'Seek return value was %s' % result)

    @populate_tracklist
    def test_seek_when_playing_updates_position(self):
        length = self.tracklist.tracks[0].length
        self.playback.play()
        self.playback.seek(length - 1000)
        position = self.playback.time_position
        self.assertGreaterEqual(position, length - 1010)

    @populate_tracklist
    def test_seek_when_paused(self):
        self.playback.play()
        self.playback.pause()
        result = self.playback.seek(self.tracks[0].length - 1000)
        self.assert_(result, 'Seek return value was %s' % result)
        self.assertEqual(self.playback.state, PlaybackState.PAUSED)

    @populate_tracklist
    def test_seek_when_paused_updates_position(self):
        length = self.tracklist.tracks[0].length
        self.playback.play()
        self.playback.pause()
        self.playback.seek(length - 1000)
        position = self.playback.time_position
        self.assertGreaterEqual(position, length - 1010)

    @unittest.SkipTest
    @populate_tracklist
    def test_seek_beyond_end_of_song(self):
        # FIXME need to decide return value
        self.playback.play()
        result = self.playback.seek(self.tracks[0].length * 100)
        self.assert_(not result, 'Seek return value was %s' % result)

    @populate_tracklist
    def test_seek_beyond_end_of_song_jumps_to_next_song(self):
        self.playback.play()
        self.playback.seek(self.tracks[0].length * 100)
        self.assertEqual(self.playback.current_track, self.tracks[1])

    @populate_tracklist
    def test_seek_beyond_end_of_song_for_last_track(self):
        self.playback.play(self.tracklist.tl_tracks[-1])
        self.playback.seek(self.tracklist.tracks[-1].length * 100)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_stop_when_stopped(self):
        self.playback.stop()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_stop_when_playing(self):
        self.playback.play()
        self.playback.stop()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_stop_when_paused(self):
        self.playback.play()
        self.playback.pause()
        self.playback.stop()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    def test_stop_return_value(self):
        self.playback.play()
        self.assertEqual(self.playback.stop(), None)

    def test_time_position_when_stopped(self):
        future = mock.Mock()
        future.get = mock.Mock(return_value=0)
        self.audio.get_position = mock.Mock(return_value=future)

        self.assertEqual(self.playback.time_position, 0)

    @populate_tracklist
    def test_time_position_when_stopped_with_playlist(self):
        future = mock.Mock()
        future.get = mock.Mock(return_value=0)
        self.audio.get_position = mock.Mock(return_value=future)

        self.assertEqual(self.playback.time_position, 0)

    @unittest.SkipTest  # Uses sleep and does might not work with LocalBackend
    @populate_tracklist
    def test_time_position_when_playing(self):
        self.playback.play()
        first = self.playback.time_position
        time.sleep(1)
        second = self.playback.time_position
        self.assertGreater(second, first)

    @unittest.SkipTest  # Uses sleep
    @populate_tracklist
    def test_time_position_when_paused(self):
        self.playback.play()
        time.sleep(0.2)
        self.playback.pause()
        time.sleep(0.2)
        first = self.playback.time_position
        second = self.playback.time_position
        self.assertEqual(first, second)

    @populate_tracklist
    def test_play_with_consume(self):
        self.tracklist.consume = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])

    @populate_tracklist
    def test_playlist_is_empty_after_all_tracks_are_played_with_consume(self):
        self.tracklist.consume = True
        self.playback.play()
        for _ in range(len(self.tracklist.tracks)):
            self.trigger_end_of_track()
        self.assertEqual(len(self.tracklist.tracks), 0)

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_play_with_random(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[-1])

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_previous_with_random(self, shuffle_mock):
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        self.tracklist.random = True
        self.playback.play()
        self.playback.next()
        current_track = self.playback.current_track
        self.playback.previous()
        self.assertEqual(self.playback.current_track, current_track)

    @populate_tracklist
    def test_end_of_song_starts_next_track(self):
        self.playback.play()
        self.trigger_end_of_track()
        self.assertEqual(self.playback.current_track, self.tracks[1])

    @populate_tracklist
    def test_end_of_song_with_single_and_repeat_starts_same(self):
        self.tracklist.single = True
        self.tracklist.repeat = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])
        self.trigger_end_of_track()
        self.assertEqual(self.playback.current_track, self.tracks[0])

    @populate_tracklist
    def test_end_of_song_with_single_random_and_repeat_starts_same(self):
        self.tracklist.single = True
        self.tracklist.repeat = True
        self.tracklist.random = True
        self.playback.play()
        current_track = self.playback.current_track
        self.trigger_end_of_track()
        self.assertEqual(self.playback.current_track, current_track)

    @populate_tracklist
    def test_end_of_song_with_single_stops(self):
        self.tracklist.single = True
        self.playback.play()
        self.assertEqual(self.playback.current_track, self.tracks[0])
        self.trigger_end_of_track()
        self.assertEqual(self.playback.current_track, None)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_end_of_song_with_single_and_random_stops(self):
        self.tracklist.single = True
        self.tracklist.random = True
        self.playback.play()
        self.trigger_end_of_track()
        self.assertEqual(self.playback.current_track, None)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    @populate_tracklist
    def test_end_of_playlist_stops(self):
        self.playback.play(self.tracklist.tl_tracks[-1])
        self.trigger_end_of_track()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    def test_repeat_off_by_default(self):
        self.assertEqual(self.tracklist.repeat, False)

    def test_random_off_by_default(self):
        self.assertEqual(self.tracklist.random, False)

    def test_consume_off_by_default(self):
        self.assertEqual(self.tracklist.consume, False)

    @populate_tracklist
    def test_random_until_end_of_playlist(self):
        self.tracklist.random = True
        self.playback.play()
        for _ in self.tracks[1:]:
            self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.next_track(tl_track), None)

    @populate_tracklist
    def test_random_with_eot_until_end_of_playlist(self):
        self.tracklist.random = True
        self.playback.play()
        for _ in self.tracks[1:]:
            self.trigger_end_of_track()
        tl_track = self.playback.current_tl_track
        self.assertEqual(self.tracklist.eot_track(tl_track), None)

    @populate_tracklist
    def test_random_until_end_of_playlist_and_play_from_start(self):
        self.tracklist.random = True
        self.playback.play()
        for _ in self.tracks:
            self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertNotEqual(self.tracklist.next_track(tl_track), None)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_random_with_eot_until_end_of_playlist_and_play_from_start(self):
        self.tracklist.random = True
        self.playback.play()
        for _ in self.tracks:
            self.trigger_end_of_track()
        tl_track = self.playback.current_tl_track
        self.assertNotEqual(self.tracklist.eot_track(tl_track), None)
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)

    @populate_tracklist
    def test_random_until_end_of_playlist_with_repeat(self):
        self.tracklist.repeat = True
        self.tracklist.random = True
        self.playback.play()
        for _ in self.tracks[1:]:
            self.playback.next()
        tl_track = self.playback.current_tl_track
        self.assertNotEqual(self.tracklist.next_track(tl_track), None)

    @populate_tracklist
    def test_played_track_during_random_not_played_again(self):
        self.tracklist.random = True
        self.playback.play()
        played = []
        for _ in self.tracks:
            self.assertNotIn(self.playback.current_track, played)
            played.append(self.playback.current_track)
            self.playback.next()

    @populate_tracklist
    @mock.patch('random.shuffle')
    def test_play_track_then_enable_random(self, shuffle_mock):
        # Covers underlying issue IssueGH17RegressionTest tests for.
        shuffle_mock.side_effect = lambda tracks: tracks.reverse()

        expected = self.tl_tracks[::-1] + [None]
        actual = []

        self.playback.play()
        self.tracklist.random = True
        while self.playback.state != PlaybackState.STOPPED:
            self.playback.next()
            actual.append(self.playback.current_tl_track)
        self.assertEqual(actual, expected)

    @populate_tracklist
    def test_playing_track_that_isnt_in_playlist(self):
        with self.assertRaises(AssertionError):
            self.playback.play(TlTrack(17, Track()))
Exemplo n.º 9
0
class LocalTracklistProviderTest(unittest.TestCase):
    config = {
        'core': {
            'data_dir': path_to_data_dir(''),
            'max_tracklist_length': 10000
        },
        'local': {
            'media_dir': path_to_data_dir(''),
            'playlists_dir': b'',
            'library': 'json',
        }
    }
    tracks = [Track(uri=generate_song(i), length=4464) for i in range(1, 4)]

    def run(self, result=None):
        with deprecation.ignore('core.tracklist.add:tracks_arg'):
            return super(LocalTracklistProviderTest, self).run(result)

    def setUp(self):  # noqa: N802
        self.audio = dummy_audio.create_proxy()
        self.backend = actor.LocalBackend.start(config=self.config,
                                                audio=self.audio).proxy()
        self.core = core.Core.start(audio=self.audio,
                                    backends=[self.backend],
                                    config=self.config).proxy()
        self.controller = self.core.tracklist
        self.playback = self.core.playback

        assert len(self.tracks) == 3, 'Need three tracks to run tests.'

    def tearDown(self):  # noqa: N802
        pykka.ActorRegistry.stop_all()

    def assert_state_is(self, state):
        self.assertEqual(self.playback.get_state().get(), state)

    def assert_current_track_is(self, track):
        self.assertEqual(self.playback.get_current_track().get(), track)

    def test_length(self):
        self.assertEqual(0, len(self.controller.get_tl_tracks().get()))
        self.assertEqual(0, self.controller.get_length().get())
        self.controller.add(self.tracks)
        self.assertEqual(3, len(self.controller.get_tl_tracks().get()))
        self.assertEqual(3, self.controller.get_length().get())

    def test_add(self):
        for track in self.tracks:
            added = self.controller.add([track]).get()
            tracks = self.controller.get_tracks().get()
            tl_tracks = self.controller.get_tl_tracks().get()

            self.assertEqual(track, tracks[-1])
            self.assertEqual(added[0], tl_tracks[-1])
            self.assertEqual(track, added[0].track)

    def test_add_at_position(self):
        for track in self.tracks[:-1]:
            added = self.controller.add([track], 0).get()
            tracks = self.controller.get_tracks().get()
            tl_tracks = self.controller.get_tl_tracks().get()

            self.assertEqual(track, tracks[0])
            self.assertEqual(added[0], tl_tracks[0])
            self.assertEqual(track, added[0].track)

    @populate_tracklist
    def test_add_at_position_outside_of_playlist(self):
        for track in self.tracks:
            added = self.controller.add([track], len(self.tracks) + 2).get()
            tracks = self.controller.get_tracks().get()
            tl_tracks = self.controller.get_tl_tracks().get()

            self.assertEqual(track, tracks[-1])
            self.assertEqual(added[0], tl_tracks[-1])
            self.assertEqual(track, added[0].track)

    @populate_tracklist
    def test_filter_by_tlid(self):
        tl_track = self.controller.get_tl_tracks().get()[1]
        result = self.controller.filter({'tlid': [tl_track.tlid]}).get()
        self.assertEqual([tl_track], result)

    @populate_tracklist
    def test_filter_by_uri(self):
        tl_track = self.controller.get_tl_tracks().get()[1]
        result = self.controller.filter({'uri': [tl_track.track.uri]}).get()
        self.assertEqual([tl_track], result)

    @populate_tracklist
    def test_filter_by_uri_returns_nothing_for_invalid_uri(self):
        self.assertEqual([], self.controller.filter({'uri': ['foobar']}).get())

    def test_filter_by_uri_returns_single_match(self):
        t = Track(uri='a')
        self.controller.add([Track(uri='z'), t, Track(uri='y')])

        result = self.controller.filter({'uri': ['a']}).get()
        self.assertEqual(t, result[0].track)

    def test_filter_by_uri_returns_multiple_matches(self):
        track = Track(uri='a')
        self.controller.add([Track(uri='z'), track, track])
        tl_tracks = self.controller.filter({'uri': ['a']}).get()
        self.assertEqual(track, tl_tracks[0].track)
        self.assertEqual(track, tl_tracks[1].track)

    def test_filter_by_uri_returns_nothing_if_no_match(self):
        self.controller.playlist = Playlist(
            tracks=[Track(uri='z'), Track(uri='y')])
        self.assertEqual([], self.controller.filter({'uri': ['a']}).get())

    def test_filter_by_multiple_criteria_returns_elements_matching_all(self):
        t1 = Track(uri='a', name='x')
        t2 = Track(uri='b', name='x')
        t3 = Track(uri='b', name='y')
        self.controller.add([t1, t2, t3])

        result1 = self.controller.filter({'uri': ['a'], 'name': ['x']}).get()
        self.assertEqual(t1, result1[0].track)

        result2 = self.controller.filter({'uri': ['b'], 'name': ['x']}).get()
        self.assertEqual(t2, result2[0].track)

        result3 = self.controller.filter({'uri': ['b'], 'name': ['y']}).get()
        self.assertEqual(t3, result3[0].track)

    def test_filter_by_criteria_that_is_not_present_in_all_elements(self):
        track1 = Track()
        track2 = Track(uri='b')
        track3 = Track()

        self.controller.add([track1, track2, track3])
        result = self.controller.filter({'uri': ['b']}).get()
        self.assertEqual(track2, result[0].track)

    @populate_tracklist
    def test_clear(self):
        self.controller.clear().get()
        self.assertEqual(len(self.controller.get_tracks().get()), 0)

    def test_clear_empty_playlist(self):
        self.controller.clear().get()
        self.assertEqual(len(self.controller.get_tracks().get()), 0)

    @populate_tracklist
    def test_clear_when_playing(self):
        self.playback.play().get()
        self.assert_state_is(PlaybackState.PLAYING)
        self.controller.clear().get()
        self.assert_state_is(PlaybackState.STOPPED)

    def test_add_appends_to_the_tracklist(self):
        self.controller.add([Track(uri='a'), Track(uri='b')])

        tracks = self.controller.get_tracks().get()
        self.assertEqual(len(tracks), 2)

        self.controller.add([Track(uri='c'), Track(uri='d')])

        tracks = self.controller.get_tracks().get()
        self.assertEqual(len(tracks), 4)
        self.assertEqual(tracks[0].uri, 'a')
        self.assertEqual(tracks[1].uri, 'b')
        self.assertEqual(tracks[2].uri, 'c')
        self.assertEqual(tracks[3].uri, 'd')

    def test_add_does_not_reset_version(self):
        version = self.controller.get_version().get()
        self.controller.add([])
        self.assertEqual(self.controller.get_version().get(), version)

    @populate_tracklist
    def test_add_preserves_playing_state(self):
        self.playback.play().get()

        track = self.playback.get_current_track().get()
        tracks = self.controller.get_tracks().get()
        self.controller.add(tracks[1:2]).get()

        self.assert_state_is(PlaybackState.PLAYING)
        self.assert_current_track_is(track)

    @populate_tracklist
    def test_add_preserves_stopped_state(self):
        tracks = self.controller.get_tracks().get()
        self.controller.add(tracks[1:2]).get()

        self.assert_state_is(PlaybackState.STOPPED)
        self.assert_current_track_is(None)

    @populate_tracklist
    def test_add_returns_the_tl_tracks_that_was_added(self):
        tracks = self.controller.get_tracks().get()

        added = self.controller.add(tracks[1:2]).get()
        tracks = self.controller.get_tracks().get()
        self.assertEqual(added[0].track, tracks[1])

    @populate_tracklist
    def test_move_single(self):
        self.controller.move(0, 0, 2)

        tracks = self.controller.get_tracks().get()
        self.assertEqual(tracks[2], self.tracks[0])

    @populate_tracklist
    def test_move_group(self):
        self.controller.move(0, 2, 1)

        tracks = self.controller.get_tracks().get()
        self.assertEqual(tracks[1], self.tracks[0])
        self.assertEqual(tracks[2], self.tracks[1])

    @populate_tracklist
    def test_moving_track_outside_of_playlist(self):
        num_tracks = len(self.controller.get_tracks().get())
        with self.assertRaises(AssertionError):
            self.controller.move(0, 0, num_tracks + 5).get()

    @populate_tracklist
    def test_move_group_outside_of_playlist(self):
        num_tracks = len(self.controller.get_tracks().get())
        with self.assertRaises(AssertionError):
            self.controller.move(0, 2, num_tracks + 5).get()

    @populate_tracklist
    def test_move_group_out_of_range(self):
        num_tracks = len(self.controller.get_tracks().get())
        with self.assertRaises(AssertionError):
            self.controller.move(num_tracks + 2, num_tracks + 3, 0).get()

    @populate_tracklist
    def test_move_group_invalid_group(self):
        with self.assertRaises(AssertionError):
            self.controller.move(2, 1, 0).get()

    def test_tracks_attribute_is_immutable(self):
        tracks1 = self.controller.tracks.get()
        tracks2 = self.controller.tracks.get()
        self.assertNotEqual(id(tracks1), id(tracks2))

    @populate_tracklist
    def test_remove(self):
        track1 = self.controller.get_tracks().get()[1]
        track2 = self.controller.get_tracks().get()[2]
        version = self.controller.get_version().get()
        self.controller.remove({'uri': [track1.uri]})
        self.assertLess(version, self.controller.get_version().get())
        self.assertNotIn(track1, self.controller.get_tracks().get())
        self.assertEqual(track2, self.controller.get_tracks().get()[1])

    @populate_tracklist
    def test_removing_track_that_does_not_exist_does_nothing(self):
        self.controller.remove({'uri': ['/nonexistant']}).get()

    def test_removing_from_empty_playlist_does_nothing(self):
        self.controller.remove({'uri': ['/nonexistant']}).get()

    @populate_tracklist
    def test_remove_lists(self):
        version = self.controller.get_version().get()
        tracks = self.controller.get_tracks().get()
        track0 = tracks[0]
        track1 = tracks[1]
        track2 = tracks[2]

        self.controller.remove({'uri': [track0.uri, track2.uri]})

        tracks = self.controller.get_tracks().get()
        self.assertLess(version, self.controller.get_version().get())
        self.assertNotIn(track0, tracks)
        self.assertNotIn(track2, tracks)
        self.assertEqual(track1, tracks[0])

    @populate_tracklist
    def test_shuffle(self):
        random.seed(1)
        self.controller.shuffle()

        shuffled_tracks = self.controller.get_tracks().get()

        self.assertNotEqual(self.tracks, shuffled_tracks)
        self.assertEqual(set(self.tracks), set(shuffled_tracks))

    @populate_tracklist
    def test_shuffle_subset(self):
        random.seed(1)
        self.controller.shuffle(1, 3)

        shuffled_tracks = self.controller.get_tracks().get()

        self.assertNotEqual(self.tracks, shuffled_tracks)
        self.assertEqual(self.tracks[0], shuffled_tracks[0])
        self.assertEqual(set(self.tracks), set(shuffled_tracks))

    @populate_tracklist
    def test_shuffle_invalid_subset(self):
        with self.assertRaises(AssertionError):
            self.controller.shuffle(3, 1).get()

    @populate_tracklist
    def test_shuffle_superset(self):
        num_tracks = len(self.controller.get_tracks().get())
        with self.assertRaises(AssertionError):
            self.controller.shuffle(1, num_tracks + 5).get()

    @populate_tracklist
    def test_shuffle_open_subset(self):
        random.seed(1)
        self.controller.shuffle(1)

        shuffled_tracks = self.controller.get_tracks().get()

        self.assertNotEqual(self.tracks, shuffled_tracks)
        self.assertEqual(self.tracks[0], shuffled_tracks[0])
        self.assertEqual(set(self.tracks), set(shuffled_tracks))

    @populate_tracklist
    def test_slice_returns_a_subset_of_tracks(self):
        track_slice = self.controller.slice(1, 3).get()
        self.assertEqual(2, len(track_slice))
        self.assertEqual(self.tracks[1], track_slice[0].track)
        self.assertEqual(self.tracks[2], track_slice[1].track)

    @populate_tracklist
    def test_slice_returns_empty_list_if_indexes_outside_tracks_list(self):
        self.assertEqual(0, len(self.controller.slice(7, 8).get()))
        self.assertEqual(0, len(self.controller.slice(-1, 1).get()))

    def test_version_does_not_change_when_adding_nothing(self):
        version = self.controller.get_version().get()
        self.controller.add([])
        self.assertEqual(version, self.controller.get_version().get())

    def test_version_increases_when_adding_something(self):
        version = self.controller.get_version().get()
        self.controller.add([Track()])
        self.assertLess(version, self.controller.get_version().get())
Exemplo n.º 10
0
class LocalTracklistProviderTest(unittest.TestCase):
    config = {
        'local': {
            'media_dir': path_to_data_dir(''),
            'data_dir': path_to_data_dir(''),
            'playlists_dir': b'',
            'library': 'json',
        }
    }
    tracks = [
        Track(uri=generate_song(i), length=4464) for i in range(1, 4)]

    def setUp(self):
        self.audio = audio.DummyAudio.start().proxy()
        self.backend = actor.LocalBackend.start(
            config=self.config, audio=self.audio).proxy()
        self.core = core.Core(audio=self.audio, backends=[self.backend])
        self.controller = self.core.tracklist
        self.playback = self.core.playback

        assert len(self.tracks) == 3, 'Need three tracks to run tests.'

    def tearDown(self):
        pykka.ActorRegistry.stop_all()

    def test_length(self):
        self.assertEqual(0, len(self.controller.tl_tracks))
        self.assertEqual(0, self.controller.length)
        self.controller.add(self.tracks)
        self.assertEqual(3, len(self.controller.tl_tracks))
        self.assertEqual(3, self.controller.length)

    def test_add(self):
        for track in self.tracks:
            tl_tracks = self.controller.add([track])
            self.assertEqual(track, self.controller.tracks[-1])
            self.assertEqual(tl_tracks[0], self.controller.tl_tracks[-1])
            self.assertEqual(track, tl_tracks[0].track)

    def test_add_at_position(self):
        for track in self.tracks[:-1]:
            tl_tracks = self.controller.add([track], 0)
            self.assertEqual(track, self.controller.tracks[0])
            self.assertEqual(tl_tracks[0], self.controller.tl_tracks[0])
            self.assertEqual(track, tl_tracks[0].track)

    @populate_tracklist
    def test_add_at_position_outside_of_playlist(self):
        for track in self.tracks:
            tl_tracks = self.controller.add([track], len(self.tracks) + 2)
            self.assertEqual(track, self.controller.tracks[-1])
            self.assertEqual(tl_tracks[0], self.controller.tl_tracks[-1])
            self.assertEqual(track, tl_tracks[0].track)

    @populate_tracklist
    def test_filter_by_tlid(self):
        tl_track = self.controller.tl_tracks[1]
        self.assertEqual(
            [tl_track], self.controller.filter(tlid=[tl_track.tlid]))

    @populate_tracklist
    def test_filter_by_uri(self):
        tl_track = self.controller.tl_tracks[1]
        self.assertEqual(
            [tl_track], self.controller.filter(uri=[tl_track.track.uri]))

    @populate_tracklist
    def test_filter_by_uri_returns_nothing_for_invalid_uri(self):
        self.assertEqual([], self.controller.filter(uri=['foobar']))

    def test_filter_by_uri_returns_single_match(self):
        track = Track(uri='a')
        self.controller.add([Track(uri='z'), track, Track(uri='y')])
        self.assertEqual(track, self.controller.filter(uri=['a'])[0].track)

    def test_filter_by_uri_returns_multiple_matches(self):
        track = Track(uri='a')
        self.controller.add([Track(uri='z'), track, track])
        tl_tracks = self.controller.filter(uri=['a'])
        self.assertEqual(track, tl_tracks[0].track)
        self.assertEqual(track, tl_tracks[1].track)

    def test_filter_by_uri_returns_nothing_if_no_match(self):
        self.controller.playlist = Playlist(
            tracks=[Track(uri=['z']), Track(uri=['y'])])
        self.assertEqual([], self.controller.filter(uri=['a']))

    def test_filter_by_multiple_criteria_returns_elements_matching_all(self):
        track1 = Track(uri='a', name='x')
        track2 = Track(uri='b', name='x')
        track3 = Track(uri='b', name='y')
        self.controller.add([track1, track2, track3])
        self.assertEqual(
            track1, self.controller.filter(uri=['a'], name=['x'])[0].track)
        self.assertEqual(
            track2, self.controller.filter(uri=['b'], name=['x'])[0].track)
        self.assertEqual(
            track3, self.controller.filter(uri=['b'], name=['y'])[0].track)

    def test_filter_by_criteria_that_is_not_present_in_all_elements(self):
        track1 = Track()
        track2 = Track(uri='b')
        track3 = Track()
        self.controller.add([track1, track2, track3])
        self.assertEqual(track2, self.controller.filter(uri=['b'])[0].track)

    @populate_tracklist
    def test_clear(self):
        self.controller.clear()
        self.assertEqual(len(self.controller.tracks), 0)

    def test_clear_empty_playlist(self):
        self.controller.clear()
        self.assertEqual(len(self.controller.tracks), 0)

    @populate_tracklist
    def test_clear_when_playing(self):
        self.playback.play()
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.controller.clear()
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)

    def test_add_appends_to_the_tracklist(self):
        self.controller.add([Track(uri='a'), Track(uri='b')])
        self.assertEqual(len(self.controller.tracks), 2)
        self.controller.add([Track(uri='c'), Track(uri='d')])
        self.assertEqual(len(self.controller.tracks), 4)
        self.assertEqual(self.controller.tracks[0].uri, 'a')
        self.assertEqual(self.controller.tracks[1].uri, 'b')
        self.assertEqual(self.controller.tracks[2].uri, 'c')
        self.assertEqual(self.controller.tracks[3].uri, 'd')

    def test_add_does_not_reset_version(self):
        version = self.controller.version
        self.controller.add([])
        self.assertEqual(self.controller.version, version)

    @populate_tracklist
    def test_add_preserves_playing_state(self):
        self.playback.play()
        track = self.playback.current_track
        self.controller.add(self.controller.tracks[1:2])
        self.assertEqual(self.playback.state, PlaybackState.PLAYING)
        self.assertEqual(self.playback.current_track, track)

    @populate_tracklist
    def test_add_preserves_stopped_state(self):
        self.controller.add(self.controller.tracks[1:2])
        self.assertEqual(self.playback.state, PlaybackState.STOPPED)
        self.assertEqual(self.playback.current_track, None)

    @populate_tracklist
    def test_add_returns_the_tl_tracks_that_was_added(self):
        tl_tracks = self.controller.add(self.controller.tracks[1:2])
        self.assertEqual(tl_tracks[0].track, self.controller.tracks[1])

    def test_index_returns_index_of_track(self):
        tl_tracks = self.controller.add(self.tracks)
        self.assertEqual(0, self.controller.index(tl_tracks[0]))
        self.assertEqual(1, self.controller.index(tl_tracks[1]))
        self.assertEqual(2, self.controller.index(tl_tracks[2]))

    def test_index_returns_none_if_item_not_found(self):
        tl_track = TlTrack(0, Track())
        self.assertEqual(self.controller.index(tl_track), None)

    @populate_tracklist
    def test_move_single(self):
        self.controller.move(0, 0, 2)

        tracks = self.controller.tracks
        self.assertEqual(tracks[2], self.tracks[0])

    @populate_tracklist
    def test_move_group(self):
        self.controller.move(0, 2, 1)

        tracks = self.controller.tracks
        self.assertEqual(tracks[1], self.tracks[0])
        self.assertEqual(tracks[2], self.tracks[1])

    @populate_tracklist
    def test_moving_track_outside_of_playlist(self):
        tracks = len(self.controller.tracks)
        test = lambda: self.controller.move(0, 0, tracks + 5)
        self.assertRaises(AssertionError, test)

    @populate_tracklist
    def test_move_group_outside_of_playlist(self):
        tracks = len(self.controller.tracks)
        test = lambda: self.controller.move(0, 2, tracks + 5)
        self.assertRaises(AssertionError, test)

    @populate_tracklist
    def test_move_group_out_of_range(self):
        tracks = len(self.controller.tracks)
        test = lambda: self.controller.move(tracks + 2, tracks + 3, 0)
        self.assertRaises(AssertionError, test)

    @populate_tracklist
    def test_move_group_invalid_group(self):
        test = lambda: self.controller.move(2, 1, 0)
        self.assertRaises(AssertionError, test)

    def test_tracks_attribute_is_immutable(self):
        tracks1 = self.controller.tracks
        tracks2 = self.controller.tracks
        self.assertNotEqual(id(tracks1), id(tracks2))

    @populate_tracklist
    def test_remove(self):
        track1 = self.controller.tracks[1]
        track2 = self.controller.tracks[2]
        version = self.controller.version
        self.controller.remove(uri=[track1.uri])
        self.assertLess(version, self.controller.version)
        self.assertNotIn(track1, self.controller.tracks)
        self.assertEqual(track2, self.controller.tracks[1])

    @populate_tracklist
    def test_removing_track_that_does_not_exist_does_nothing(self):
        self.controller.remove(uri=['/nonexistant'])

    def test_removing_from_empty_playlist_does_nothing(self):
        self.controller.remove(uri=['/nonexistant'])

    @populate_tracklist
    def test_remove_lists(self):
        track0 = self.controller.tracks[0]
        track1 = self.controller.tracks[1]
        track2 = self.controller.tracks[2]
        version = self.controller.version
        self.controller.remove(uri=[track0.uri, track2.uri])
        self.assertLess(version, self.controller.version)
        self.assertNotIn(track0, self.controller.tracks)
        self.assertNotIn(track2, self.controller.tracks)
        self.assertEqual(track1, self.controller.tracks[0])

    @populate_tracklist
    def test_shuffle(self):
        random.seed(1)
        self.controller.shuffle()

        shuffled_tracks = self.controller.tracks

        self.assertNotEqual(self.tracks, shuffled_tracks)
        self.assertEqual(set(self.tracks), set(shuffled_tracks))

    @populate_tracklist
    def test_shuffle_subset(self):
        random.seed(1)
        self.controller.shuffle(1, 3)

        shuffled_tracks = self.controller.tracks

        self.assertNotEqual(self.tracks, shuffled_tracks)
        self.assertEqual(self.tracks[0], shuffled_tracks[0])
        self.assertEqual(set(self.tracks), set(shuffled_tracks))

    @populate_tracklist
    def test_shuffle_invalid_subset(self):
        test = lambda: self.controller.shuffle(3, 1)
        self.assertRaises(AssertionError, test)

    @populate_tracklist
    def test_shuffle_superset(self):
        tracks = len(self.controller.tracks)
        test = lambda: self.controller.shuffle(1, tracks + 5)
        self.assertRaises(AssertionError, test)

    @populate_tracklist
    def test_shuffle_open_subset(self):
        random.seed(1)
        self.controller.shuffle(1)

        shuffled_tracks = self.controller.tracks

        self.assertNotEqual(self.tracks, shuffled_tracks)
        self.assertEqual(self.tracks[0], shuffled_tracks[0])
        self.assertEqual(set(self.tracks), set(shuffled_tracks))

    @populate_tracklist
    def test_slice_returns_a_subset_of_tracks(self):
        track_slice = self.controller.slice(1, 3)
        self.assertEqual(2, len(track_slice))
        self.assertEqual(self.tracks[1], track_slice[0].track)
        self.assertEqual(self.tracks[2], track_slice[1].track)

    @populate_tracklist
    def test_slice_returns_empty_list_if_indexes_outside_tracks_list(self):
        self.assertEqual(0, len(self.controller.slice(7, 8)))
        self.assertEqual(0, len(self.controller.slice(-1, 1)))

    def test_version_does_not_change_when_adding_nothing(self):
        version = self.controller.version
        self.controller.add([])
        self.assertEquals(version, self.controller.version)

    def test_version_increases_when_adding_something(self):
        version = self.controller.version
        self.controller.add([Track()])
        self.assertLess(version, self.controller.version)