示例#1
0
class TestPlayer(TestCase):

    def setUp(self):
        self.player = MpvPlayer()
        self.player.volume = 0
        self.player.initialize()

    def tearDown(self):
        self.player.stop()
        self.player.shutdown()

    @skipIf(is_travis_env, '')
    def test_play(self):
        self.player.play(MP3_URL)
        self.player.stop()

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    def test_duration(self):
        # This may failed?
        self.player.play(MP3_URL)
        time.sleep(MPV_SLEEP_SECOND)
        self.assertIsNotNone(self.player.duration)

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    def test_seek(self):
        self.player.play(MP3_URL)
        time.sleep(MPV_SLEEP_SECOND)
        self.player.position = 100

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    def test_replay(self):
        song = FakeValidSongModel()
        self.player.play_song(song)
        time.sleep(MPV_SLEEP_SECOND)
        self.player.position = 100
        self.player.replay()
        time.sleep(MPV_SLEEP_SECOND)
        self.assertTrue(self.player.position < 10)

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    def test_play_pause_toggle_resume_stop(self):
        self.player.play(MP3_URL)
        time.sleep(MPV_SLEEP_SECOND)
        self.player.toggle()
        self.assertEqual(self.player.state, State.paused)
        self.player.resume()
        self.assertEqual(self.player.state, State.playing)
        self.player.pause()
        self.assertEqual(self.player.state, State.paused)
        self.player.stop()
        self.assertEqual(self.player.state, State.stopped)

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    def test_set_volume(self):
        self.player.volume = 30
        self.assertEqual(self.player.volume, 30)
示例#2
0
class TestPlayerAndPlaylist(TestCase):

    def setUp(self):
        self.player = MpvPlayer()
        self.player.initialize()

    def tearDown(self):
        self.player.stop()
        self.player.shutdown()

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    @mock.patch.object(MpvPlayer, 'play')
    def test_change_song(self, _):
        s1 = FakeValidSongModel()
        s2 = FakeValidSongModel()

        playlist = self.player.playlist
        playlist.add(s1)
        playlist.add(s2)

        self.player.play_song(s1)
        self.player.play_next()
        self.assertTrue(playlist.current_song, s2)

        self.player.play_previous()
        self.assertTrue(playlist.current_song, s1)

    @skipIf(os.environ.get('TEST_ENV') == 'travis', '')
    @mock.patch.object(MpvPlayer, 'play')
    def test_remove_current_song_2(self, mock_play):
        """当播放列表只有一首歌时,移除它"""
        s1 = FakeValidSongModel()
        self.player.playlist.current_song = s1
        time.sleep(MPV_SLEEP_SECOND)  # 让 Mpv 真正的开始播放
        self.player.playlist.remove(s1)
        self.assertEqual(len(self.player.playlist), 0)
        self.assertEqual(self.player.state, State.stopped)
示例#3
0
class TestPlayer(TestCase):
    def setUp(self):
        self.player = MpvPlayer()
        self.player.volume = 0
        self.player.initialize()

    def tearDown(self):
        self.player.stop()
        self.player.shutdown()

    @skipIf(cannot_play_audio, '')
    def test_play(self):
        self.player.play(MP3_URL)
        self.player.stop()

    @skipIf(cannot_play_audio, '')
    def test_duration(self):
        # This may failed?
        self.player.play(MP3_URL)
        time.sleep(MPV_SLEEP_SECOND)
        self.assertIsNotNone(self.player.duration)

    @skipIf(cannot_play_audio, '')
    def test_seek(self):
        self.player.play(MP3_URL)
        time.sleep(MPV_SLEEP_SECOND)
        self.player.position = 100

    @skipIf(cannot_play_audio, '')
    def test_replay(self):
        song = FakeValidSongModel()
        self.player.play_song(song)
        time.sleep(MPV_SLEEP_SECOND)
        self.player.position = 100
        self.player.replay()
        time.sleep(MPV_SLEEP_SECOND)
        self.assertTrue(self.player.position < 10)

    @skipIf(cannot_play_audio, '')
    def test_play_pause_toggle_resume_stop(self):
        self.player.play(MP3_URL)
        time.sleep(MPV_SLEEP_SECOND)
        self.player.toggle()
        self.assertEqual(self.player.state, State.paused)
        self.player.resume()
        self.assertEqual(self.player.state, State.playing)
        self.player.pause()
        self.assertEqual(self.player.state, State.paused)
        self.player.stop()
        self.assertEqual(self.player.state, State.stopped)

    @skipIf(cannot_play_audio, '')
    def test_set_volume(self):
        cb = mock.Mock()
        self.player.volume_changed.connect(cb)
        self.player.volume = 30
        self.assertEqual(self.player.volume, 30)
        cb.assert_called_once_with(30)

    @mock.patch('fuocore.mpvplayer._mpv_set_option_string')
    def test_play_media_with_http_headers(self, mock_set_option_string):
        media = Media('http://xxx', http_headers={'referer': 'http://xxx'})
        self.player.play(media)
        assert mock_set_option_string.called
        self.player.stop()