gtk: Add a SidebarFactory
This will be used to make Gtk.TreeExpander rows containing a Gtk.Label to display the XfstestsRuns on a given day. Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
parent
6fcb4eb5e7
commit
2352bc3512
|
@ -1,5 +1,6 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""Tests our row widgets and factories."""
|
||||
import datetime
|
||||
import unittest
|
||||
import xfstestsdb.gtk.row
|
||||
import tests.xunit
|
||||
|
@ -375,3 +376,67 @@ class TestSummaryFactory(unittest.TestCase):
|
|||
|
||||
self.factory.emit("unbind", self.listitem)
|
||||
self.assertFalse(self.listitem.get_child().has_css_class("accent"))
|
||||
|
||||
|
||||
class TestSidebarFactory(unittest.TestCase):
|
||||
"""Tests our Gtk.Factory to show Xfstsets runs in the sidebar."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.devlist = xfstestsdb.gtk.tree.DeviceRunsList("/dev/vda1")
|
||||
self.treeitem = Gtk.TreeListRow()
|
||||
self.treeitem.get_item = unittest.mock.Mock(return_value=self.devlist)
|
||||
self.listitem = Gtk.ListItem()
|
||||
self.listitem.get_item = unittest.mock.Mock(return_value=self.treeitem)
|
||||
|
||||
self.factory = xfstestsdb.gtk.row.SidebarFactory()
|
||||
|
||||
def test_init(self):
|
||||
"""Test that the factory was initialized correctly."""
|
||||
self.assertIsInstance(self.factory, Gtk.SignalListItemFactory)
|
||||
|
||||
def test_setup(self):
|
||||
"""Test that thefactory implements the 'setup' signal."""
|
||||
self.factory.emit("setup", self.listitem)
|
||||
expander = self.listitem.get_child()
|
||||
self.assertIsInstance(expander, Gtk.TreeExpander)
|
||||
self.assertIsInstance(expander.get_child(), Gtk.Label)
|
||||
self.assertEqual(expander.get_child().props.yalign, 0.75)
|
||||
self.assertTrue(expander.get_child().has_css_class("numeric"))
|
||||
|
||||
def test_bind_device_list(self):
|
||||
"""Test binding to a DeviceRunsList object."""
|
||||
self.factory.emit("setup", self.listitem)
|
||||
self.factory.emit("bind", self.listitem)
|
||||
self.assertEqual(self.listitem.get_child().get_child().get_text(),
|
||||
"/dev/vda1")
|
||||
self.assertEqual(self.listitem.get_child().get_list_row(),
|
||||
self.treeitem)
|
||||
self.assertFalse(self.listitem.props.selectable)
|
||||
|
||||
def test_bind_xfstests_run(self):
|
||||
"""Test binding to an XfstestsRun object."""
|
||||
now = datetime.datetime.now()
|
||||
self.devlist.add_run(1, now)
|
||||
self.treeitem.get_item.return_value = self.devlist[0]
|
||||
|
||||
self.factory.emit("setup", self.listitem)
|
||||
self.factory.emit("bind", self.listitem)
|
||||
self.assertEqual(self.listitem.get_child().get_child().get_text(),
|
||||
f"#1: {now.strftime('%T')}")
|
||||
self.assertEqual(self.listitem.get_child().get_list_row(),
|
||||
self.treeitem)
|
||||
self.assertTrue(self.listitem.props.selectable)
|
||||
|
||||
def test_unbind(self):
|
||||
"""Test that the factory implements the 'unbind' signal."""
|
||||
self.factory.emit("setup", self.listitem)
|
||||
self.factory.emit("bind", self.listitem)
|
||||
self.factory.emit("unbind", self.listitem)
|
||||
self.assertEqual(self.listitem.get_child().get_child().get_text(), "")
|
||||
|
||||
def test_teardown(self):
|
||||
"""Test that the factory implements the 'teardown' signal."""
|
||||
self.factory.emit("setup", self.listitem)
|
||||
self.factory.emit("teardown", self.listitem)
|
||||
self.assertIsNone(self.listitem.get_child())
|
||||
|
|
|
@ -4,6 +4,7 @@ import typing
|
|||
from gi.repository import GObject
|
||||
from gi.repository import Gtk
|
||||
from . import model
|
||||
from . import tree
|
||||
|
||||
|
||||
STYLES = {"passed": "success", "failed": "error",
|
||||
|
@ -181,3 +182,40 @@ class SummaryFactory(XunitFactory):
|
|||
def do_unbind(self, row: model.TestCase, child: Gtk.Inscription) -> None:
|
||||
"""Unbind a ListItem from the child widget."""
|
||||
child.remove_css_class(STYLES[row.name])
|
||||
|
||||
|
||||
class SidebarFactory(Gtk.SignalListItemFactory):
|
||||
"""Factory for making sidebar widgets."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize our InscriptionFactory."""
|
||||
super().__init__(*args, **kwargs)
|
||||
self.connect("setup", self.__setup)
|
||||
self.connect("bind", self.__bind)
|
||||
self.connect("unbind", self.__unbind)
|
||||
self.connect("teardown", self.__teardown)
|
||||
|
||||
def __setup(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
|
||||
"""Set up a ListItem child widget."""
|
||||
child = Gtk.Label(yalign=0.75)
|
||||
child.add_css_class("numeric")
|
||||
expander = Gtk.TreeExpander(child=child)
|
||||
listitem.set_child(expander)
|
||||
|
||||
def __bind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
|
||||
"""Bind a ListItem to the child widget."""
|
||||
treeitem = listitem.get_item()
|
||||
expander = listitem.get_child()
|
||||
expander.set_list_row(treeitem)
|
||||
|
||||
row = treeitem.get_item()
|
||||
child = expander.get_child()
|
||||
child.set_text(str(row))
|
||||
listitem.props.selectable = isinstance(row, tree.XfstestsRun)
|
||||
|
||||
def __unbind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
|
||||
"""Unbind a ListItem from the child widget."""
|
||||
listitem.get_child().get_child().set_text("")
|
||||
|
||||
def __teardown(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
|
||||
listitem.set_child(None)
|
||||
|
|
Loading…
Reference in New Issue