def get_thumbnail_from_video(self, **kwargs): """Run Video.get_thumbnail() This method uses mock to intercept the threading and idle_add calls and just runs the code in the current thread """ completion = mock.Mock() with mock.patch('mvc.video.idle_add') as mock_idle_add: with mock.patch('threading.Thread') as mock_thread: initial_rv = self.video.get_thumbnail(completion, **kwargs) if initial_rv is not None: # we already had a thumbnail and didn't have to do # anything synchrously return video.VideoFile(initial_rv) # We don't already have a thumbnail, so get_thumbnail() # created a thread to create it. Run the function for that # thread. mock_thread.call_args[1]['target']() self.assertEquals(mock_idle_add.call_count, 1) # At the end of the thread it uses add_idle() to call the # completion function. Run that now. mock_idle_add.call_args[0][0]() # Now when we call get_thumbnail() it should return # immediately with the thumbnail path = self.video.get_thumbnail(completion, **kwargs) self.assertNotEquals(path, None) return video.VideoFile(path)
def test_multiple_simultaneous_conversions(self): filename = os.path.join(self.temp_dir, 'webm-0.webm') shutil.copyfile(os.path.join(self.testdata_dir, 'webm-0.webm'), filename) shutil.copyfile(os.path.join(self.testdata_dir, 'webm-0.webm'), filename + '2') vf = video.VideoFile(filename) vf2 = video.VideoFile(filename + '2') c = self.manager.start_conversion(vf, self.converter) c2 = self.manager.start_conversion(vf2, self.converter) self.assertEqual(len(self.manager.in_progress), 2) self.spin(3) # if they're linear, it should take < 5s self.assertEqual(c.status, 'finished') self.assertEqual(c2.status, 'finished')
def test_limit_simultaneous_conversions(self): self.manager.simultaneous = 1 filename = os.path.join(self.temp_dir, 'webm-0.webm') shutil.copyfile(os.path.join(self.testdata_dir, 'webm-0.webm'), filename) shutil.copyfile(os.path.join(self.testdata_dir, 'webm-0.webm'), filename + '2') vf = video.VideoFile(filename) vf2 = video.VideoFile(filename + '2') c = self.manager.start_conversion(vf, self.converter) c2 = self.manager.start_conversion(vf2, self.converter) self.assertEqual(len(self.manager.in_progress), 1) self.assertEqual(len(self.manager.waiting), 1) self.spin(5) self.assertEqual(c.status, 'finished') self.assertEqual(c2.status, 'finished')
def test_get_thumbnail_audio(self): audio_path = os.path.join(self.testdata_dir, 'mp3-0.mp3') audio = video.VideoFile(audio_path) def complete(): pass self.assertEqual(audio.get_thumbnail(complete), None) self.assertEqual(audio.get_thumbnail(complete, 90, 70), None)
def test_stop(self): filename = os.path.join(self.temp_dir, 'webm-0.webm') shutil.copyfile(os.path.join(self.testdata_dir, 'webm-0.webm'), filename) vf = video.VideoFile(filename) c = self.manager.start_conversion(vf, self.converter) time.sleep(0.5) c.stop() self.spin(1) self.assertEqual(c.status, 'canceled') self.assertEqual(c.error, 'manually stopped')
def get_thumbnail_from_video(self, **kwargs): ev = threading.Event() def completion(): ev.set() path = self.video.get_thumbnail(completion, **kwargs) if not path: ev.wait(10) if not ev.is_set(): self.assertTrue(None, 'timed out generating thumbnail') path = self.video.get_thumbnail(completion, **kwargs) self.assertNotEqual(path, None, 'thumbnail not created') return video.VideoFile(path)
def start_conversion(self, filename, timeout=3): vf = video.VideoFile(filename) c = self.manager.start_conversion(vf, self.converter) # XXX HACK: for test harness change the output directory to be the # same as the input file (so we can nuke in one go) c.output_dir = os.path.dirname(filename) c.output = os.path.join(c.output_dir, self.converter.get_output_filename(vf)) c.listen(self.changed) self.assertTrue(self.manager.running) self.assertTrue(c in self.manager.in_progress) self.spin(timeout) self.assertFalse(self.manager.running) self.assertFalse(os.path.exists(c.temp_output)) return c
def generate_thumbnail(self, width, height): ev = threading.Event() def completion(path): ev.set() self.assertEqual(path, self.temp_path.name) video.get_thumbnail(self.video_path, width, height, self.temp_path.name, completion, skip=0) ev.wait(10) if not ev.is_set(): self.assertTrue(None, 'timed out generating thumbnail image') thumbnail = video.VideoFile(self.temp_path.name) return thumbnail
def generate_thumbnail(self, width, height): completion = mock.Mock() with mock.patch('mvc.video.idle_add') as mock_idle_add: with mock.patch('threading.Thread') as mock_thread: video.get_thumbnail(self.video_path, width, height, self.temp_path.name, completion, skip=0) # get_thumbnail() creates a thread to create the thumbnail. # Run the function for that thread now. mock_thread.call_args[1]['target']() self.assertEquals(mock_idle_add.call_count, 1) # At the end of the thread it uses add_idle() to call the # completion function. Run that now. mock_idle_add.call_args[0][0]() # Now when we call get_thumbnail() it should return # immediately with the thumbnail path = completion.call_args[0][0] self.assertNotEquals(path, None) return video.VideoFile(path)
def setUp(self): base.Test.setUp(self) self.video_path = os.path.join(self.testdata_dir, 'theora.ogv') self.video = video.VideoFile(self.video_path) self.video.thumbnails = {}
def start_conversion(self, filename, converter_id): self.startup() converter = self.converter_manager.get_by_id(converter_id) v = video.VideoFile(filename) return self.conversion_manager.start_conversion(v, converter)