Package elisa :: Package plugins :: Package bad :: Package daap_plugin :: Package tests :: Module test_daap_media
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.daap_plugin.tests.test_daap_media

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2007-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  from elisa.core.tests.test_media_provider import TestMediaProvider 
 18  from elisa.core.media_uri import MediaUri 
 19  from elisa.core.component import InitializeFailure 
 20  from elisa.extern.natural_sort import natcasecmp 
 21  import gst 
 22  from twisted.internet import defer 
 23   
 24  try: 
 25      from elisa.plugins.bad.daap_plugin.daap_media import DaapSource, DaapMedia 
 26      skip_test = False 
 27  except ImportError, x: 
 28      # we don't have the dependencies to test this 
 29      skip_test = str(x) 
30 - class DaapMedia(object):
31 - def _get_library(*args, **kw):
32 pass
33
34 - def _build_library(*args, **kw):
35 pass
36
37 -class TestDaapTrack(object):
38 - def __init__(self, artist, album, track_id, track_name):
39 self.artist = artist 40 self.album = album 41 self.id = track_id 42 self.name = track_name
43
44 -class TestDaapMediaProvider(DaapMedia):
45 - def __init__(self, *args, **kw):
46 super(TestDaapMediaProvider, self).__init__(*args, **kw) 47 48 self.n_artists = 10 49 self.n_albums_per_artist = 2 50 self.n_tracks_per_album = 10 51 52 # create the tracks, put artists and tracks in reverse to check that 53 # natural sorting works in the provider 54 self.tracks = [] 55 for x in range(self.n_artists, 0, -1): 56 for y in range(self.n_albums_per_artist): 57 for z in range(self.n_tracks_per_album, 0, -1): 58 artist = unicode('artist-%d' % x, 'utf-8') 59 album = 'album-%d' % y 60 track_name = 'artist-%d-album-%d-track-name-%d' % (x, y, z) 61 self.tracks.append(TestDaapTrack(artist, album, z, track_name)) 62 63 self.test_lib = self._build_library(self.tracks)
64
65 - def _initialize_avahi(self):
66 # override this so we don't need a daap server to test the media 67 # provider 68 pass
69
70 - def _get_library(self, uri):
71 # get our test library 72 return self.test_lib
73
74 -class TestDaapMedia(TestMediaProvider):
75 component_class = TestDaapMediaProvider 76 77 if skip_test: 78 skip = skip_test 79
80 - def get_valid_uris(self):
81 return [MediaUri("daap:///")]
82
83 - def test_uri(self):
84 uri = 'daap://ciao' 85 src = gst.element_make_from_uri(gst.URI_SRC, uri) 86 self.failUnless(type(src) == DaapSource) 87 self.failUnlessEquals(src.get_uri(), uri) 88 89 uri = 'daap://miao' 90 self.failUnless(src.set_uri(uri)) 91 self.failUnlessEquals(src.get_uri(), uri) 92 93 uri = 'ciao://bao' 94 self.failUnlessFalse(src.set_uri(uri))
95
96 - def test_is_directory(self):
97 uri = MediaUri('daap://ciao') 98 track = MediaUri('daap://ciao?artist=asd&album=asd&id=1') 99 100 def expected(result, expected_result): 101 self.failIf(result != expected_result)
102 103 d1 = self.component.is_directory(uri) 104 d1.addCallback(expected, True) 105 106 d2 = self.component.is_directory(track) 107 d2.addCallback(expected, False) 108 109 return defer.DeferredList([d1, d2])
110
111 - def test_get_direct_children(self):
112 # get the artists 113 d1 = self.component.get_direct_children(MediaUri('daap://host:1234'), 114 []) 115 d1.addCallback(lambda c: self.failIf(len(c)!= self.component.n_artists)) 116 117 # get the albums of the first artist 118 artist_uri = MediaUri("daap://host:1234/?artist=artist-1") 119 albums = [] 120 d2 = self.component.get_direct_children(artist_uri, albums) 121 d2.addCallback(lambda a: self.failIf(len(a) != self.component.n_albums_per_artist)) 122 123 # get the tracks in the first album of the first artist 124 album_uri = MediaUri("daap://host:1234/?album=album-0&artist=artist-1") 125 tracks = [] 126 d3 = self.component.get_direct_children(album_uri, tracks) 127 d3.addCallback(lambda t: self.failIf(len(t) != self.component.n_tracks_per_album)) 128 129 # tracks have no children 130 track_uri = MediaUri("daap://host:1234/?album=album-0&track=artist-1-album-0-track-name-1&id=1&artist=artist-1") 131 empty = [] 132 d4 = self.component.get_direct_children(track_uri, empty) 133 d4.addCallback(lambda e: self.failIf(len(e) != 0)) 134 135 return defer.DeferredList([d1, d2, d3, d4])
136
137 - def test_next_location(self):
138 root = MediaUri('daap://host:1234') 139 # get the tracks and sort them 140 tracks = self.component.tracks[:] 141 tracks.sort(cmp=natcasecmp, key=lambda track: track.name) 142 143 def uri_from_track(track): 144 expected_uri = MediaUri('daap://host:1234') 145 expected_uri.set_param('artist', track.artist) 146 expected_uri.set_param('album', track.album) 147 expected_uri.set_param('track', track.name) 148 expected_uri.set_param('id', track.id) 149 150 return expected_uri 151 152 # now check that the media provider returns them in the order we expect 153 uri = root 154 expected_uri = None 155 n_tracks = 0 156 while True: 157 try: 158 track = tracks[n_tracks] 159 except IndexError: 160 track = None 161 162 uri = self.component.blocking_next_location(uri, root=root) 163 if track is None: 164 self.assertEqual(uri, None) 165 break 166 else: 167 expected_uri = uri_from_track(track) 168 self.assertEqual(uri, expected_uri) 169 170 n_tracks += 1 171 172 # the same, rooted at one artist 173 artist_1_tracks = [track for track in tracks 174 if track.artist == 'artist-1'] 175 root.set_param('artist', 'artist-1') 176 uri = root 177 n_tracks = 0 178 while True: 179 try: 180 track = artist_1_tracks[n_tracks] 181 except IndexError: 182 track = None 183 184 uri = self.component.blocking_next_location(uri, root=root) 185 if track is None: 186 self.assertEqual(uri, None) 187 break 188 else: 189 expected_uri = uri_from_track(track) 190 self.assertEqual(uri, expected_uri) 191 192 n_tracks += 1 193 194 # rooted at one album 195 artist_1_tracks = [track for track in tracks 196 if track.artist == 'artist-1' and track.album == 'album-0'] 197 root.set_param('artist', 'artist-1') 198 root.set_param('album', 'album-0') 199 uri = root 200 n_tracks = 0 201 while True: 202 try: 203 track = artist_1_tracks[n_tracks] 204 except IndexError: 205 track = None 206 207 uri = self.component.blocking_next_location(uri, root=root) 208 if track is None: 209 self.assertEqual(uri, None) 210 break 211 else: 212 expected_uri = uri_from_track(track) 213 self.assertEqual(uri, expected_uri) 214 215 n_tracks += 1 216 217 test_next_location.skip = "Refactorize and don't use blocking API" 218
219 - def test_has_children_with_types(self):
220 221 def expect(result, expected_result): 222 self.failIf(result != expected_result)
223 224 types = ['audio', 'video'] 225 uri = MediaUri('daap://host:1234') 226 d1 = self.component.has_children_with_types(uri, types) 227 d1.addCallback(expect, True) 228 229 types = ['directory', 'video'] 230 d2 = self.component.has_children_with_types(uri, types) 231 d2.addCallback(expect, True) 232 233 types = ['video'] 234 d3 = self.component.has_children_with_types(uri, types) 235 d3.addCallback(expect, False) 236 237 uri = MediaUri('daap://host:1234?id=something') 238 types = ['audio'] 239 d4 = self.component.has_children_with_types(uri, types) 240 d4.addCallback(expect, False) 241 242 return defer.DeferredList([d1, d2, d3, d4]) 243