Skip to content

Commit

Permalink
add tabbar for model's subresources
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Jan 2, 2020
1 parent d848714 commit 0281181
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
Binary file added data/test.m4a
Binary file not shown.
49 changes: 48 additions & 1 deletion feeluown/widgets/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,54 @@

from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout, QVBoxLayout, \
QSpacerItem, QScrollArea, QFrame
QSpacerItem, QScrollArea, QFrame, QTabBar


class TabBar(QTabBar):
song = '歌曲'
artist = '歌手'
album = '专辑'
contributed_works = '参与作品'

show_songs_needed = pyqtSignal()
show_artists_needed = pyqtSignal()
show_albums_needed = pyqtSignal()
show_contributed_works_needed = pyqtSignal()

def __init__(self, parent=None):
super().__init__(parent=parent)

self.tabBarClicked.connect(self.on_index_changed)

def use(self, *tabs):
i = self.count() - 1
while i >= 0:
self.removeTab(i)
i = i - 1
for tab in tabs:
self.addTab(tab)

def artist_mode(self):
self.use(TabBar.song,
TabBar.album,
TabBar.contributed_works)

def library_mode(self):
self.use(TabBar.song,
TabBar.artist,
TabBar.album,
TabBar.contributed_works)

def on_index_changed(self, index):
text = self.tabText(index)
if text == self.song:
self.show_songs_needed.emit()
elif text == self.artist:
self.show_artists_needed.emit()
elif text == self.album:
self.show_albums_needed.emit()
else:
self.show_contributed_works_needed.emit()


class DescriptionContainer(QScrollArea):
Expand Down
19 changes: 19 additions & 0 deletions tests/feeluown/widgets/test_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from tests.helpers import cannot_run_qt_test
from feeluown.widgets.meta import TabBar


# TODO: use xvfb in travis env
# example: https://github.com/pytest-dev/pytest-qt/blob/master/.travis.yml
@pytest.mark.skipif(cannot_run_qt_test, reason='this is a qt testcase')
def test_tabbar(qtbot):
tabbar = TabBar()
qtbot.addWidget(tabbar)
tabbar.artist_mode()
with qtbot.waitSignal(tabbar.show_albums_needed):
tabbar.tabBarClicked.emit(1)

tabbar.library_mode()
with qtbot.waitSignal(tabbar.show_artists_needed):
tabbar.tabBarClicked.emit(1)
6 changes: 3 additions & 3 deletions tests/feeluown/widgets/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

from tests.helpers import is_travis_env
from feeluown.widgets.meta import TableMetaWidget
from feeluown.widgets.table_toolbar import SongsTableToolbar


# TODO: use xvfb in travis env
# example: https://github.com/pytest-dev/pytest-qt/blob/master/.travis.yml
@pytest.mark.skipif(is_travis_env, reason="travis env has no display")
def test_table_meta(qtbot):
widget = TableMetaWidget()
widget = TableMetaWidget(SongsTableToolbar())
qtbot.addWidget(widget)
widget.title = '我喜欢的音乐'
widget.subtitle = '嘿嘿'
widget.creator = 'cosven'
widget.updated_at = time.time()
widget.desc = "<pre><code>print('hello world')</code><pre>"

qtbot.addWidget(widget)
3 changes: 3 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@


is_travis_env = os.environ.get('TEST_ENV') == 'travis'

cannot_play_audio = is_travis_env
cannot_run_qt_test = is_travis_env
18 changes: 9 additions & 9 deletions tests/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from unittest import TestCase, skipIf, mock

from fuocore.player import MpvPlayer, Playlist, PlaybackMode, State
from .helpers import is_travis_env
from .helpers import cannot_play_audio


MP3_URL = os.path.join(os.path.dirname(__file__),
'../data/sample.mp3')
'../data/test.m4a')
MPV_SLEEP_SECOND = 0.1 # 留给 MPV 反应的时间


Expand Down Expand Up @@ -38,25 +38,25 @@ def tearDown(self):
self.player.stop()
self.player.shutdown()

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

@skipIf(os.environ.get('TEST_ENV') == 'travis', '')
@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(os.environ.get('TEST_ENV') == 'travis', '')
@skipIf(cannot_play_audio, '')
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', '')
@skipIf(cannot_play_audio, '')
def test_replay(self):
song = FakeValidSongModel()
self.player.play_song(song)
Expand All @@ -66,7 +66,7 @@ def test_replay(self):
time.sleep(MPV_SLEEP_SECOND)
self.assertTrue(self.player.position < 10)

@skipIf(os.environ.get('TEST_ENV') == 'travis', '')
@skipIf(cannot_play_audio, '')
def test_play_pause_toggle_resume_stop(self):
self.player.play(MP3_URL)
time.sleep(MPV_SLEEP_SECOND)
Expand All @@ -79,7 +79,7 @@ def test_play_pause_toggle_resume_stop(self):
self.player.stop()
self.assertEqual(self.player.state, State.stopped)

@skipIf(os.environ.get('TEST_ENV') == 'travis', '')
@skipIf(cannot_play_audio, '')
def test_set_volume(self):
self.player.volume = 30
self.assertEqual(self.player.volume, 30)
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_change_song(self, _):
self.player.play_previous()
self.assertTrue(playlist.current_song, s1)

@skipIf(os.environ.get('TEST_ENV') == 'travis', '')
@skipIf(cannot_play_audio, '')
@mock.patch.object(MpvPlayer, 'play')
def test_remove_current_song_2(self, mock_play):
"""当播放列表只有一首歌时,移除它"""
Expand Down

0 comments on commit 0281181

Please sign in to comment.