diff --git a/emmental/sidebar/row.py b/emmental/sidebar/row.py index 640edd6..cd50b35 100644 --- a/emmental/sidebar/row.py +++ b/emmental/sidebar/row.py @@ -7,6 +7,7 @@ from .title import EditableTitle from .icon import Icon from .icon import Settable from .. import db +from .. import factory class BaseRow(Gtk.Box): @@ -155,3 +156,19 @@ class LibraryRow(BaseRow): case "delete": self.playlist.delete() case "scan": self.playlist.scan() case "stop": self.playlist.stop() + + +class TreeRow(factory.TreeRow): + """A factory Row used for displaying individual playlists.""" + + def do_bind(self) -> None: + """Bind a Playlist to this Row.""" + self.child.playlist = self.item + self.bind_and_set_property("name", "name") + self.bind_and_set_property("n-tracks", "count") + self.bind_n_children(self.item.children) + self.bind_active("active") + + def do_unbind(self) -> None: + """Unbind a Playlist from this Row.""" + self.child.playlist = None diff --git a/tests/sidebar/test_row.py b/tests/sidebar/test_row.py index b846ffd..2e440d8 100644 --- a/tests/sidebar/test_row.py +++ b/tests/sidebar/test_row.py @@ -6,6 +6,7 @@ import emmental.sidebar.icon import emmental.sidebar.row from gi.repository import Gio from gi.repository import Gtk +from gi.repository import Adw class TestBaseRow(unittest.TestCase): @@ -310,3 +311,44 @@ class TestLibraryRow(unittest.TestCase): self.assertTrue(self.row._progress.get_visible()) self.assertTrue(self.row._stop.get_visible()) self.assertFalse(self.row._scan.get_visible()) + + +class TestTreeRow(unittest.TestCase): + """Test the TreeRow widget used by the section factories.""" + + def setUp(self): + """Set up common variables.""" + self.playlist = emmental.db.playlist.Playlist(Gio.ListStore(), + 0, "Playlist Name") + self.treeitem = Gtk.TreeListRow() + self.treeitem.get_item = unittest.mock.Mock(return_value=self.playlist) + self.listitem = Gtk.ListItem() + self.listitem.get_item = unittest.mock.Mock(return_value=self.treeitem) + self.treerow = emmental.sidebar.row.TreeRow(self.listitem) + self.adw_bin = Adw.Bin(child=self.listitem.get_child()) + + def test_init(self): + """Test that the TreeRow was set up properly.""" + self.assertIsInstance(self.treerow, emmental.factory.TreeRow) + self.assertIsNone(self.treerow.child) + + def test_bind(self): + """Test binding a Playlist to the TreeRow.""" + self.treerow.child = emmental.sidebar.row.BaseRow() + self.playlist.table.update = unittest.mock.Mock(return_value=True) + self.playlist.active = True + self.playlist.n_tracks = 42 + self.playlist.children = Gtk.FilterListModel.new(Gio.ListStore(), None) + + self.treerow.bind() + self.assertEqual(self.treerow.child.playlist, self.playlist) + self.assertEqual(self.treerow.child.name, "Playlist Name") + self.assertEqual(self.treerow.child.count, 42) + self.assertEqual(self.treerow.n_children, 0) + self.assertTrue(self.treerow.active) + + self.playlist.children.get_model().append(Gtk.Label()) + self.assertEqual(self.treerow.n_children, 1) + + self.treerow.unbind() + self.assertIsNone(self.treerow.child.playlist)