gtk: LabelFactory improvements

I add a global SizeGroup to the LabelFactory so the first column of each
table I make can have the same width. I also add style classes that are
applied with specific item values, which will mostly be used by the
SummaryView labels.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-14 16:00:27 -04:00
parent e14667691a
commit d92b8148bf
2 changed files with 50 additions and 4 deletions

View File

@ -16,23 +16,34 @@ class TestLabelFactory(unittest.TestCase):
self.listitem.get_item = unittest.mock.Mock(return_value=self.testcase)
self.factory = xfstestsdb.gtk.row.LabelFactory("name")
self.group = xfstestsdb.gtk.row.LabelFactory.group
def test_init(self):
"""Test that the factory was initialized correctly."""
self.assertIsInstance(self.factory, Gtk.SignalListItemFactory)
self.assertEqual(self.factory.property, "name")
def test_size_group(self):
"""Test the label factory global size group."""
self.assertIsInstance(xfstestsdb.gtk.row.LabelFactory.group,
Gtk.SizeGroup)
self.assertEqual(xfstestsdb.gtk.row.LabelFactory.group.props.mode,
Gtk.SizeGroupMode.HORIZONTAL)
def test_setup(self):
"""Test that the factory implements the 'setup' signal."""
self.factory.emit("setup", self.listitem)
self.assertIsInstance(self.listitem.get_child(), Gtk.Label)
self.assertTrue(self.listitem.get_child().has_css_class("numeric"))
self.assertIn(self.listitem.get_child(), self.group.get_widgets())
def test_bind(self):
"""Test that the factory implements the 'bind' signal."""
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "test/case")
for style in xfstestsdb.gtk.row.STYLES.keys():
self.assertFalse(self.listitem.get_child().has_css_class(style))
def test_unbind(self):
"""Test that the factory implements the 'unbind' signal."""
@ -44,8 +55,28 @@ class TestLabelFactory(unittest.TestCase):
def test_teardown(self):
"""Test that the factory implements the 'teardown' signal."""
self.factory.emit("setup", self.listitem)
child = self.listitem.get_child()
self.factory.emit("teardown", self.listitem)
self.assertIsNone(self.listitem.get_child())
self.assertNotIn(child, self.group.get_widgets())
def test_styles(self):
"""Test the column text styles."""
self.assertDictEqual(xfstestsdb.gtk.row.STYLES,
{"passed": "success", "failed": "error",
"skipped": "warning", "time": "accent"})
for style in ["passed", "failed", "skipped", "time"]:
with self.subTest(style=style):
self.testcase.name = style
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
child = self.listitem.get_child()
expected = xfstestsdb.gtk.row.STYLES[style]
self.assertTrue(child.has_css_class(expected))
self.factory.emit("unbind", self.listitem)
self.assertFalse(child.has_css_class(expected))
class TestResultFactory(unittest.TestCase):

View File

@ -5,10 +5,15 @@ from gi.repository import GObject
from gi.repository import Gtk
STYLES = {"passed": "success", "failed": "error",
"skipped": "warning", "time": "accent"}
class LabelFactory(Gtk.SignalListItemFactory):
"""Create Gtk.Labels for each testcase."""
property = GObject.Property(type=str)
group = Gtk.SizeGroup()
def __init__(self, property: str):
"""Initialize our InscriptionFactory."""
@ -20,21 +25,31 @@ class LabelFactory(Gtk.SignalListItemFactory):
def do_setup(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Set up a ListItem child widget."""
listitem.set_child(Gtk.Label())
listitem.get_child().add_css_class("numeric")
child = Gtk.Label()
child.add_css_class("numeric")
listitem.set_child(child)
LabelFactory.group.add_widget(child)
def do_bind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Bind a ListItem to the child widget."""
text = listitem.get_item().get_property(self.property)
listitem.get_child().set_text(text)
child = listitem.get_child()
if style := STYLES.get(text):
child.add_css_class(style)
child.set_text(text)
def do_unbind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Unbind a ListItem from the child widget."""
listitem.get_child().set_text("")
child = listitem.get_child()
for style in STYLES.values():
child.remove_css_class(style)
child.set_text("")
def do_teardown(self, factory: typing.Self,
listitem: Gtk.ListItem) -> None:
"""Clean up a ListItem child widget."""
if (child := listitem.get_child()) is not None:
LabelFactory.group.remove_widget(child)
listitem.set_child(None)