from mopidy.models import Track, Playlist

class BaseCurrentPlaylistControllerTest(object):
    uris = []
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def populate_playlist(self):
        for uri in self.uris:
            self.backend.current_playlist.add(uri)

    def test_uri_set(self):
        self.assert_(len(self.uris) >= 3)

    def test_add(self):
        controller = self.backend.current_playlist

        for uri in self.uris:
            controller.add(uri)
            self.assertEqual(uri, controller.playlist.tracks[-1].uri)

    def test_add_at_position(self):
        controller = self.backend.current_playlist

        for uri in self.uris[:-1]:
            controller.add(uri, 0)
            self.assertEqual(uri, controller.playlist.tracks[0].uri)

        uri = self.uris[-1]

        controller.add(uri, len(self.uris)+2)
        self.assertEqual(uri, controller.playlist.tracks[-1].uri)

    def test_clear(self):
        self.populate_playlist()
        controller = self.backend.current_playlist

        controller.clear()

        self.assertEqual(len(controller.playlist.tracks), 0)

    def test_clear_when_playing(self):
        self.populate_playlist()
        controller = self.backend.current_playlist
        playback = self.backend.playback

        playback.play()

        self.assertEqual(playback.state, playback.PLAYING)

        controller.clear()

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

    def test_load(self):
        controller = self.backend.current_playlist

        new_playlist = Playlist()

        self.assertNotEqual(new_playlist, controller.playlist)

        controller.load(new_playlist)

        self.assertEqual(new_playlist, controller.playlist)

class BasePlaybackControllerTest(object):
    uris = []
    backend_class = None

    def setUp(self):
        self.backend = self.backend_class()

    def populate_playlist(self):
        for uri in self.uris:
            self.backend.current_playlist.add(uri)

    def test_play_with_empty_playlist(self):
        playback = self.backend.playback

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

        result = playback.play()

        self.assertEqual(result, False)
        self.assertEqual(playback.state, playback.STOPPED)

    def test_play(self):
        self.populate_playlist()
        playback = self.backend.playback

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

        result = playback.play()

        self.assertEqual(result, True)
        self.assertEqual(playback.state, playback.PLAYING)

    def test_next(self):
        self.populate_playlist()
        playback = self.backend.playback

        playback.play()

        old_position = playback.playlist_position
        old_uri = playback.current_track.uri

        playback.next()

        self.assertEqual(playback.playlist_position, old_position+1)
        self.assertNotEqual(playback.current_track.uri, old_uri)

@@ -36,16 +36,197 @@
 aylist%0A%0A
+def populate_playlist(func):%0A    def wrapper(self):%0A        for uri in self.uris:%0A            self.backend.current_playlist.add(uri)%0A%0A        return func(self)%0A%0A    return wrapper%0A%0A
 class Ba
@@ -376,123 +376,8 @@
 ()%0A%0A
-    def populate_playlist(self):%0A        for uri in self.uris:%0A            self.backend.current_playlist.add(uri)%0A%0A
     
@@ -1014,32 +1014,55 @@
 racks%5B-1%5D.uri)%0A%0A
+    @populate_playlist%0A
     def test_cle
@@ -1075,41 +1075,8 @@
 f):%0A
-        self.populate_playlist()%0A
     
@@ -1213,16 +1213,39 @@
 s), 0)%0A%0A
+    @populate_playlist%0A
     def 
@@ -1279,41 +1279,8 @@
 f):%0A
-        self.populate_playlist()%0A
     
@@ -1969,123 +1969,8 @@
 ()%0A%0A
-    def populate_playlist(self):%0A        for uri in self.uris:%0A            self.backend.current_playlist.add(uri)%0A%0A
     
@@ -2238,32 +2238,55 @@
 yback.STOPPED)%0A%0A
+    @populate_playlist%0A
     def test_pla
@@ -2298,41 +2298,8 @@
 f):%0A
-        self.populate_playlist()%0A
     
@@ -2525,24 +2525,47 @@
 k.PLAYING)%0A%0A
+    @populate_playlist%0A
     def test
@@ -2581,41 +2581,8 @@
 f):%0A
-        self.populate_playlist()%0A
     

Rewrite populate playlist to decorator