Package elisa :: Package plugins :: Package base :: Package tests :: Module test_local_media
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.base.tests.test_local_media

  1   
  2  from elisa.core import plugin 
  3  from elisa.core.tests.test_media_provider import TestMediaProvider 
  4  from elisa.base_components.media_provider import NotifyEvent 
  5  from elisa.plugins.base import local_media 
  6  from elisa.core.media_uri import MediaUri 
  7  from elisa.core.utils import misc, sorting 
  8  import os, platform 
  9  from twisted.internet import defer 
 10  import pkg_resources 
 11  import shutil 
 12   
 13  if platform.system() == 'Linux': 
 14      from elisa.extern.coherence.inotify import INotify 
 15   
16 -class DummyPlugin(plugin.Plugin):
17 name = 'dummy' 18 components = { 19 'gst_metadata': 20 {'path': 'elisa.plugins.good.gstreamer_plugin.gst_metadata:GstMetadata'} 21 }
22
23 -class TestLocalMedia(TestMediaProvider):
24 component_class = local_media.LocalMedia 25
26 - def __init__(self, methodName='runTest'):
27 TestMediaProvider.__init__(self, methodName=methodName) 28 29 if platform.system() != 'Windows': 30 try: 31 i = INotify() 32 i.release() 33 except Exception, error: 34 self.skip = error 35 36 # use a well known list of files so that if someone adds a file in data/ 37 # to use it in another test this doesn't break 38 # key is a file in the source distribution, value a list of paths 39 # relative to the working directory where to copy the file 40 self.data_files = { 41 'audio.ogg': ['audio.ogg', 'first_level/audio.ogg', 42 'first_level/second_level/audio.ogg'], 43 'video.ogg': ['video.ogg', 'first_level/second_level/video.ogg'], 44 'audio.mp3': ['audio.mp3', 'first_level/audio.mp3'] 45 }
46
47 - def setUp(self):
48 package_path = 'elisa.plugins.good.gstreamer_plugin.tests' 49 # we are already in _trial_temp/ at this point 50 self.working_data_dir = os.path.abspath('local_media_data') 51 52 # setup a clean data dir for each test 53 self.uris = [] 54 for filename, working_paths in self.data_files.iteritems(): 55 for path in working_paths: 56 working_filename = os.path.join(self.working_data_dir, path) 57 pkg_resources.ensure_directory(working_filename) 58 working_uri = MediaUri('file://' + working_filename) 59 self.uris.append(working_uri) 60 61 # copy our data to the trial working dir 62 fi = pkg_resources.resource_stream(package_path, 63 os.path.join('data', filename)) 64 fo = file(working_filename, 'w') 65 fo.write(fi.read()) 66 fo.close() 67 68 # chain up the parent that sets stuff in common.application 69 super(TestLocalMedia, self).setUp() 70 71 from elisa.core.common import application 72 application.plugin_registry.register_plugin(DummyPlugin) 73 self.gst_metadata = application.plugin_registry.create_component('dummy:gst_metadata') 74 application.metadata_manager.register_component(self.gst_metadata)
75
76 - def tearDown(self):
77 if platform.system() != 'Windows': 78 try: 79 i = INotify() 80 except Exception, error: 81 # no need to skip from here 82 pass 83 else: 84 i.release() 85 86 # clean the data dir at the end of each test 87 shutil.rmtree(self.working_data_dir) 88 89 # clean the component 90 self.gst_metadata.clean() 91 TestMediaProvider.tearDown(self)
92
93 - def get_data_uris(self, root, deep=False):
94 uris = [] 95 for path, dirs, files in os.walk(root): 96 for child in dirs + files: 97 uris.append(self.get_data_uri(child)) 98 99 if not deep: 100 break 101 102 return uris
103
104 - def get_valid_uris(self):
105 return self.uris
106
107 - def get_data_uri(self, filename=''):
108 return MediaUri('file://' + os.path.join(self.working_data_dir, filename))
109
110 - def _touch(self, path):
111 f = open(path.path, 'w') 112 f.write('woo') 113 f.close()
114
115 - def test_get_direct_children(self):
116 # get the root uri 117 uri = self.get_data_uri() 118 119 def get_direct_children_done(children, root): 120 expected = self.get_data_uris(root.path) 121 self.assertEqual(len(children), len(expected)) 122 123 children_uris = [i[0] for i in children] 124 # my professor of algorithms would be proud of me here 125 for uri in expected: 126 assert uri in children_uris
127 128 children = [] 129 dfr = self.component.get_direct_children(uri, children) 130 dfr.addCallback(get_direct_children_done, uri) 131 132 return dfr
133
134 - def test_monitor_uri(self):
135 136 dfr = defer.Deferred() 137 uri = self.get_data_uri() 138 foo_txt = self.get_data_uri('foo.txt') 139 140 def cb(u1, metadata, evt, u): 141 self.assertEquals(u, uri) 142 self.assertEquals(evt, NotifyEvent.ADDED) 143 self.assertEquals(u1, foo_txt) 144 self.assertEquals(metadata, {}) 145 dfr.callback(True)
146 147 self.component.monitor_uri(uri, cb) 148 self._touch(foo_txt) 149 150 dfr.addCallback(lambda r: self.component.unmonitor_uri(uri)) 151 return dfr 152 153 if platform.system() == 'Windows': 154 test_monitor_uri.skip = "Skipped under windows, to fix" 155
156 - def test_copy(self):
157 # TestMediaProvider.test_copy(self) 158 159 orig_uri = self.get_data_uri('audio.ogg') 160 dest_uri = self.get_data_uri('audio_copy.ogg') 161 162 self.component.copy(orig_uri, dest_uri) 163 self.failUnless(os.path.exists(dest_uri.path))
164 165 if platform.system() == 'Windows': 166 test_copy.skip = "Skipped under windows, to fix" 167
168 - def test_move(self):
169 # TestMediaProvider.test_move(self) 170 171 orig_uri = self.get_data_uri('audio.ogg') 172 dest_uri = self.get_data_uri('audio_moved.ogg') 173 174 self.component.move(orig_uri, dest_uri) 175 self.failIf(os.path.exists(orig_uri.path)) 176 self.failUnless(os.path.exists(dest_uri.path))
177 178 if platform.system() == 'Windows': 179 test_move.skip = "Skipped under windows, to fix" 180
181 - def test_delete(self):
182 # TestMediaProvider.test_delete(self) 183 184 orig_uri = self.get_data_uri('audio.ogg') 185 self.component.delete(orig_uri) 186 self.failIf(os.path.exists(orig_uri.path))
187 188 if platform.system() == 'Windows': 189 test_delete.skip = "Skipped under windows, to fix" 190
191 - def test_next_location(self):
192 ret_list = [] 193 def next_location_done(result, uris, root, index): 194 if result is None: 195 self.assertEquals(len(uris), len(ret_list)) 196 197 return 198 199 self.assertEqual(result, uris[index]) 200 ret_list.append(result) 201 202 dfr = self.component.next_location(result, root) 203 dfr.addCallback(next_location_done, uris, root, index + 1) 204 205 return dfr
206 207 expected = ['first_level', 'first_level/second_level', 208 'first_level/second_level/audio.ogg', 209 'first_level/second_level/video.ogg', 210 'first_level/audio.mp3', 'first_level/audio.ogg', 211 'audio.mp3', 'audio.ogg', 'video.ogg'] 212 213 expected = [self.get_data_uri(i) for i in expected] 214 215 root = self.get_data_uri() 216 uris = [str(uri) for uri in self.get_data_uris(root.path)] 217 sorting.natural_sort(uris) 218 219 dfr = self.component.next_location(root) 220 dfr.addCallback(next_location_done, expected, root, 0) 221 222 return dfr 223 224 if platform.system() == 'Windows': 225 test_next_location.skip = "Skipped under windows, to fix" 226
227 - def test_get_media_type(self):
228 # TestMediaProvider.test_get_media_type(self) 229 230 def get_media_type_done(media_type, 231 expected_file_type, expected_mime_types): 232 self.assertEqual(media_type['file_type'], expected_file_type) 233 self.failUnless(media_type['mime_type'] in expected_mime_types)
234 235 uri = self.get_data_uri('audio.ogg') 236 audio_ogg_dfr = self.component.get_media_type(uri) 237 audio_ogg_dfr.addCallback(get_media_type_done, 238 'audio', ['audio/x-vorbis']) 239 240 uri = self.get_data_uri('video.ogg') 241 video_ogg_dfr = self.component.get_media_type(uri) 242 video_ogg_dfr.addCallback(get_media_type_done, 243 'video', ['video/x-theora']) 244 245 # FIXME: two of the build bots don't have mad, reenable this when we 246 # implement missing plugin reporting in gst_metadata 247 # uri = self.get_data_uri('audio.mp3') 248 # audio_mp3_dfr = self.component.get_media_type(uri) 249 # audio_mp3_dfr.addCallback(get_media_type_done, 250 # 'audio', ['application/x-id3', 'audio/mpeg']) 251 audio_mp3_dfr = defer.succeed(None) 252 253 dfr_list = defer.DeferredList([audio_ogg_dfr, video_ogg_dfr, 254 audio_mp3_dfr], fireOnOneErrback=True) 255 256 return dfr_list 257 258 test_get_media_type.skip = "Skipped under windows due to glib2 reactor issues with unit test and GIL release issues on Rawhide" 259