From cdae9541e96dab7d2f4d1add5352113c1b229ddc Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 11 Aug 2022 15:47:28 -0400 Subject: [PATCH] sidebar: Create a PlaylistTitle widget This widget sets the Title::subtitle property to a nicely formatted string based on the number set to the 'count' property. Signed-off-by: Anna Schumaker --- emmental/sidebar/title.py | 15 +++++++++++++++ tests/sidebar/test_title.py | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/emmental/sidebar/title.py b/emmental/sidebar/title.py index 4cb25ec..a99607b 100644 --- a/emmental/sidebar/title.py +++ b/emmental/sidebar/title.py @@ -31,3 +31,18 @@ class Title(Gtk.Box): self.append(self._title) self.append(self._subtitle) + + +class PlaylistTitle(Title): + """A title widget for displaying playlist size.""" + + count = GObject.Property(type=int) + + def __init__(self, title: str = "", **kwargs): + """Initialize a Playlist Title widget.""" + super().__init__(title=title, subtitle="-- tracks", **kwargs) + self.connect("notify::count", self.__update_subtitle) + + def __update_subtitle(self, title: Title, param) -> None: + s_count = "s" if self.count != 1 else "" + self.subtitle = f"{self.count} track{s_count}" diff --git a/tests/sidebar/test_title.py b/tests/sidebar/test_title.py index 6744a1a..cfde0c0 100644 --- a/tests/sidebar/test_title.py +++ b/tests/sidebar/test_title.py @@ -62,3 +62,28 @@ class TestTitle(unittest.TestCase): title2 = emmental.sidebar.title.Title(subtitle="Other Subtitle") self.assertEqual(title2._subtitle.get_text(), "Other Subtitle") + + +class TestPlaylistTitle(unittest.TestCase): + """Test our title widget configured for displaying playlist size.""" + + def setUp(self): + """Set up common variables.""" + self.title = emmental.sidebar.title.PlaylistTitle() + + def test_init(self): + """Test that the PlaylistTitle is configured correctly.""" + self.assertIsInstance(self.title, emmental.sidebar.title.Title) + self.assertEqual(self.title.subtitle, "-- tracks") + self.assertEqual(self.title.count, 0) + + def test_count(self): + """Test changing the count property.""" + self.title.count = 0 + self.assertEqual(self.title.subtitle, "0 tracks") + + self.title.count = 1 + self.assertEqual(self.title.subtitle, "1 track") + + self.title.count = 2 + self.assertEqual(self.title.subtitle, "2 tracks")