# Copyright 2023 (c) Anna Schumaker. """Tests our row widgets and factories.""" import datetime import unittest import xfstestsdb.gtk.row import tests.xunit from gi.repository import Gtk from gi.repository import Adw class TestFactory(unittest.TestCase): """Tests our base Gtk.Factory to make Gtk.Inscriptions.""" def setUp(self): """Set up common variables.""" self.xunitrow = xfstestsdb.gtk.model.XunitRow("xunit/row") self.listitem = Gtk.ListItem() self.listitem.get_item = unittest.mock.Mock(return_value=self.xunitrow) self.factory = xfstestsdb.gtk.row.Factory() def test_init(self): """Test that the Factory was initialized correctly.""" self.assertIsInstance(self.factory, Gtk.SignalListItemFactory) def test_setup(self): """Test that the factory implements the 'setup' signal.""" with unittest.mock.patch.object(self.factory, "do_setup") as mock_setup: self.factory.emit("setup", self.listitem) self.assertIsInstance(self.listitem.get_child(), Gtk.Inscription) self.assertEqual(self.listitem.get_child().props.xalign, 0.5) self.assertEqual(self.listitem.get_child().props.nat_chars, 18) self.assertEqual(self.listitem.get_child().props.text_overflow, Gtk.InscriptionOverflow.ELLIPSIZE_END) self.assertTrue(self.listitem.get_child().has_css_class("numeric")) mock_setup.assert_called_with(self.listitem.get_child()) def test_bind(self): """Test that the factory implements the 'bind' signal.""" with unittest.mock.patch.object(self.factory, "do_bind") as mock_bind: self.factory.emit("setup", self.listitem) self.factory.emit("bind", self.listitem) mock_bind.assert_called_with(self.xunitrow, self.listitem.get_child()) def test_unbind(self): """Test that the factory implements the 'unbind' signal.""" with unittest.mock.patch.object(self.factory, "do_unbind") as mock_unbind: self.factory.emit("setup", self.listitem) self.factory.emit("bind", self.listitem) self.listitem.get_child().set_text("text") self.factory.emit("unbind", self.listitem) self.assertIsNone(self.listitem.get_child().get_text()) mock_unbind.assert_called_with(self.xunitrow, self.listitem.get_child()) def test_teardown(self): """Test that the factory implements the 'teardown' signal.""" with unittest.mock.patch.object(self.factory, "do_teardown") as mock_teardown: self.factory.emit("setup", self.listitem) child = self.listitem.get_child() self.factory.emit("teardown", self.listitem) self.assertIsNone(self.listitem.get_child()) mock_teardown.assert_called_with(child) class TestLabelFactory(unittest.TestCase): """Tests our Gtk.Factory to make Gtk.Labels.""" def setUp(self): """Set up common variables.""" self.testcase = xfstestsdb.gtk.model.TestCase("test/case") self.listitem = Gtk.ListItem() self.listitem.get_item = unittest.mock.Mock(return_value=self.testcase) self.factory = xfstestsdb.gtk.row.LabelFactory(property="name") self.group = xfstestsdb.gtk.row.LabelFactory.group def test_init(self): """Test that the factory was initialized correctly.""" self.assertIsInstance(self.factory, xfstestsdb.gtk.row.Factory) 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.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.""" self.factory.emit("setup", self.listitem) self.factory.emit("bind", self.listitem) self.factory.emit("unbind", self.listitem) self.assertIsNone(self.listitem.get_child().get_text()) 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.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 TestEnvironmentFactory(unittest.TestCase): """Tests our Gtk.Factory to show environment properties.""" def setUp(self): """Set up common variables.""" self.xfstestsdb = xfstestsdb.Command() with unittest.mock.patch("sys.stdout"): self.xfstestsdb.run(["new", "/dev/vda1"]) self.xfstestsdb.run(["xunit", "read", "--name", "xunit-1", "1", str(tests.xunit.XUNIT_1)]) self.xfstestsdb.run(["xunit", "read", "--name", "xunit-2", "1", str(tests.xunit.XUNIT_1)]) self.props = xfstestsdb.gtk.model.PropertyList(self.xfstestsdb.sql, 1) self.listitem = Gtk.ListItem() self.listitem.get_item = unittest.mock.Mock(return_value=self.props) self.factory = xfstestsdb.gtk.row.EnvironmentFactory(property="FSTYP") def test_init(self): """Test that the factory was initialized correctly.""" self.assertIsInstance(self.factory, xfstestsdb.gtk.row.Factory) self.assertEqual(self.factory.property, "FSTYP") def test_bind(self): """Test binding to a property.""" self.factory.emit("setup", self.listitem) self.factory.emit("bind", self.listitem) self.assertEqual(self.listitem.get_child().get_xalign(), 0) self.assertEqual(self.listitem.get_child().get_text(), "myfs") self.assertEqual(self.listitem.get_child().get_tooltip_text(), "myfs") class TestXunitFactory(unittest.TestCase): """Tests our XunitFactory base class.""" def setUp(self): """Set up common variables.""" self.factory = xfstestsdb.gtk.row.XunitFactory(xunit="xunit-1") def test_init(self): """Test that the factory was initialized correctly.""" self.assertIsInstance(self.factory, xfstestsdb.gtk.row.Factory) self.assertEqual(self.factory.xunit, "xunit-1") class TestPropertyFactory(unittest.TestCase): """Tests our Gtk.Factory to show xunit properties.""" def setUp(self): """Set up common variables.""" self.property = xfstestsdb.gtk.model.Property("property") self.listitem = Gtk.ListItem() self.listitem.get_item = unittest.mock.Mock(return_value=self.property) self.factory = xfstestsdb.gtk.row.PropertyFactory(xunit="xunit-1") def test_init(self): """Test that the factory was initialized correctly.""" self.assertIsInstance(self.factory, xfstestsdb.gtk.row.XunitFactory) def test_bind(self): """Test binding to a property.""" self.property.add_xunit("xunit-1", "property", "value") self.factory.emit("setup", self.listitem) self.factory.emit("bind", self.listitem) self.assertEqual(self.listitem.get_child().get_text(), "value") self.assertEqual(self.listitem.get_child().get_tooltip_text(), "value") class TestResultFactory(unittest.TestCase): """Tests our Gtk.Factory to show test results.""" def setup_parent(self, factory: xfstestsdb.gtk.row.ResultFactory, listitem: Gtk.ListItem) -> None: """Set the child widget's parent for custom styling.""" self.parent.set_child(listitem.get_child()) def setUp(self): """Set up common variables.""" self.testcase = xfstestsdb.gtk.model.TestCase("test/case") self.parent = Adw.Bin() self.listitem = Gtk.ListItem() self.listitem.get_item = unittest.mock.Mock(return_value=self.testcase) self.factory = xfstestsdb.gtk.row.ResultFactory(xunit="xunit-1") self.factory.connect("setup", self.setup_parent) def test_init(self): """Test that the factory was initialized correctly.""" self.assertIsInstance(self.factory, xfstestsdb.gtk.row.XunitFactory) def test_setup_click(self): """Test that we setup a GestureClick on the child widget.""" self.factory.emit("setup", self.listitem) child = self.listitem.get_child() click = getattr(child, "click") self.assertIsInstance(click, Gtk.GestureClick) self.assertIn(click, child.observe_controllers()) self.factory.emit("teardown", self.listitem) self.assertIsNone(getattr(child, "click")) self.assertNotIn(click, child.observe_controllers()) def test_bind_passed(self): """Test binding to a passing test.""" show_messages = unittest.mock.Mock() self.testcase.add_xunit("xunit-1", "passed", 3, "", None, None) self.factory.connect("show-messages", show_messages) self.factory.emit("setup", self.listitem) child = self.listitem.get_child() self.factory.emit("bind", self.listitem) self.assertEqual(child.get_text(), "3 seconds") self.assertIsNone(child.get_tooltip_text()) self.assertTrue(self.parent.has_css_class("passed")) child.click.emit("released", 1, 0, 0) show_messages.assert_not_called() self.factory.emit("unbind", self.listitem) self.assertFalse(self.parent.has_css_class("passed")) def test_bind_skipped(self): """Test binding to a skipped test.""" show_messages = unittest.mock.Mock() self.testcase.add_xunit("xunit-1", "skipped", 0, "test skipped for ... reasons", None, None) self.factory.connect("show-messages", show_messages) self.factory.emit("setup", self.listitem) child = self.listitem.get_child() self.factory.emit("bind", self.listitem) self.assertEqual(child.get_text(), "skipped") self.assertEqual(child.get_tooltip_text(), "test skipped for ... reasons") self.assertTrue(self.parent.has_css_class("skipped")) child.click.emit("released", 1, 0, 0) show_messages.assert_not_called() self.factory.emit("unbind", self.listitem) self.assertFalse(self.parent.has_css_class("skipped")) def test_bind_failed(self): """Test binding to a failed test.""" show_messages = unittest.mock.Mock() self.testcase.add_xunit("xunit-1", "failure", 8, "- failed. see output", "stdout message", "stderr message") self.factory.connect("show-messages", show_messages) self.factory.emit("setup", self.listitem) child = self.listitem.get_child() self.factory.emit("bind", self.listitem) self.assertEqual(child.get_text(), "failure") self.assertEqual(child.get_tooltip_text(), "failed. see output") self.assertTrue(self.parent.has_css_class("failure")) child.click.emit("released", 1, 0, 0) show_messages.assert_called_with(self.factory, "test/case", "xunit-1", "stdout message", "stderr message") self.factory.emit("unbind", self.listitem) self.assertFalse(self.parent.has_css_class("failure")) child.click.emit("released", 1, 0, 0) show_messages.assert_called_once() def test_bind_missing(self): """Test binding to a missing test.""" self.factory.emit("setup", self.listitem) self.factory.emit("bind", self.listitem) self.assertIsNone(self.listitem.get_child().get_text()) self.factory.emit("unbind", self.listitem) 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="xunit-1") def test_init(self): """Test that the factory was initialized correctly.""" self.assertIsInstance(self.factory, xfstestsdb.gtk.row.XunitFactory) 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.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.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.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.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())