gtk: Create a SummaryFactory

This is a Gtk.ListItemFactory that creates a Label widget and binds
xunit summaries to it. I use the summary field name to apply an
appropriate CSS class to each label.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-14 15:20:38 -04:00
parent d92b8148bf
commit 8a5bc1527a
2 changed files with 118 additions and 0 deletions

View File

@ -165,3 +165,83 @@ class TestResultFactory(unittest.TestCase):
self.factory.emit("setup", self.listitem)
self.factory.emit("teardown", self.listitem)
self.assertIsNone(self.listitem.get_child())
class TestSummaryFactory(unittest.TestCase):
"""Tests our Gtk.Factory to show Xfstests results summaries."""
def setUp(self):
"""Set up common variables."""
self.summary = xfstestsdb.gtk.model.Summary("passed")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.summary)
self.factory = xfstestsdb.gtk.row.SummaryFactory("xunit-1")
def test_init(self):
"""Test that the factory was initialized correctly."""
self.assertIsInstance(self.factory, Gtk.SignalListItemFactory)
self.assertEqual(self.factory.xunit, "xunit-1")
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"))
def test_bind_passed(self):
"""Test binding to the passed tests summary."""
self.summary.add_xunit("xunit-1", 1, "testcase")
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "1 testcase")
self.assertTrue(self.listitem.get_child().has_css_class("success"))
self.factory.emit("unbind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "")
self.assertFalse(self.listitem.get_child().has_css_class("success"))
def test_bind_failed(self):
"""Test binding to the failed tests summary."""
self.summary.name = "failed"
self.summary.add_xunit("xunit-1", 1, "testcase")
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "1 testcase")
self.assertTrue(self.listitem.get_child().has_css_class("error"))
self.factory.emit("unbind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "")
self.assertFalse(self.listitem.get_child().has_css_class("error"))
def test_bind_skipped(self):
"""Test binding to the skipped tests summary."""
self.summary.name = "skipped"
self.summary.add_xunit("xunit-1", 1, "testcase")
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "1 testcase")
self.assertTrue(self.listitem.get_child().has_css_class("warning"))
self.factory.emit("unbind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "")
self.assertFalse(self.listitem.get_child().has_css_class("warning"))
def test_bind_time(self):
"""Test binding to the time summary."""
self.summary.name = "time"
self.summary.add_xunit("xunit-1", 1, "second")
self.factory.emit("setup", self.listitem)
self.factory.emit("bind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "1 second")
self.assertTrue(self.listitem.get_child().has_css_class("accent"))
self.factory.emit("unbind", self.listitem)
self.assertEqual(self.listitem.get_child().get_text(), "")
self.assertFalse(self.listitem.get_child().has_css_class("accent"))
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())

View File

@ -95,3 +95,41 @@ class ResultFactory(Gtk.SignalListItemFactory):
listitem: Gtk.ListItem) -> None:
"""Clean up a ListItem child widget."""
listitem.set_child(None)
class SummaryFactory(Gtk.SignalListItemFactory):
"""Factory for making test summary widgets."""
xunit = GObject.Property(type=str)
def __init__(self, xunit: str):
"""Initialize our ResultFactory."""
super().__init__(xunit=xunit)
self.connect("setup", self.do_setup)
self.connect("bind", self.do_bind)
self.connect("unbind", self.do_unbind)
self.connect("teardown", self.do_teardown)
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")
def do_bind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Bind a ListItem to the child widget."""
summary = listitem.get_item()
result = summary[self.xunit]
child = listitem.get_child()
child.set_text(str(result))
child.add_css_class(STYLES[summary.name])
def do_unbind(self, factory: typing.Self, listitem: Gtk.ListItem) -> None:
"""Unbind a ListItem from the child widget."""
child = listitem.get_child()
child.set_text("")
child.remove_css_class(STYLES[listitem.get_item().name])
def do_teardown(self, factory: typing.Self,
listitem: Gtk.ListItem) -> None:
"""Clean up a ListItem child widget."""
listitem.set_child(None)