emmental/tests/test_factory.py
Anna Schumaker 236a1e60c2 factory: Create a LabelRow and LabelFactory
The LabelRow is an implementation of the ListRow for the common case of
displaying text to the user. It has some convenience properties for
setting the xalign property and adding the "numeric" class to the Gtk.Label.

The LabelFactory creates LabelRows.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2023-04-12 10:41:42 -04:00

176 lines
6.8 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Tests our Factory implementation."""
import unittest
import unittest.mock
import emmental.factory
from gi.repository import Gtk
from gi.repository import GObject
class TestListRow(unittest.TestCase):
"""Test the ListRow object."""
def setUp(self):
"""Set up common variables."""
self.item = Gtk.Label(label="Test")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.item)
self.row = emmental.factory.ListRow(self.listitem, child=Gtk.Label())
self.row.do_bind = unittest.mock.Mock(wraps=self.row.do_bind)
self.row.do_unbind = unittest.mock.Mock(wraps=self.row.do_unbind)
def test_init(self):
"""Test that the ListRow is set up properly."""
self.assertIsInstance(self.row, GObject.GObject)
self.assertIsInstance(self.listitem.get_child(), Gtk.Label)
self.assertListEqual(self.row.bindings, [])
self.assertEqual(self.row.listitem, self.listitem)
self.assertEqual(self.row.child, self.listitem.get_child())
self.assertEqual(self.row.item, self.item)
def test_bind(self):
"""Test calling bind() on the ListRow."""
self.row.bind()
self.row.do_bind.assert_called()
def test_bind_and_set_property(self):
"""Test the ListRow bind_property() function."""
self.row.bind_and_set_property("label", "label")
self.assertEqual(self.row.child.get_text(), "Test")
self.assertEqual(len(self.row.bindings), 1)
self.assertIsInstance(self.row.bindings[0], GObject.Binding)
self.item.set_text("Text 2")
self.assertEqual(self.row.child.get_text(), "Text 2")
self.row.child.set_text("Other Text")
self.assertEqual(self.item.get_text(), "Text 2")
def test_bind_bidirectional(self):
"""Test bidirectional bindings."""
self.row.bind_and_set_property("label", "label", bidirectional=True)
self.assertEqual(self.row.child.get_text(), "Test")
self.row.child.set_text("Other Text")
self.assertEqual(self.item.get_text(), "Other Text")
def test_bind_invert_boolean(self):
"""Test invert boolean bindings."""
self.row.bind_and_set_property("sensitive", "sensitive",
invert_boolean=True)
self.assertFalse(self.row.child.get_sensitive())
self.item.set_sensitive(False)
self.assertTrue(self.row.child.get_sensitive())
def test_unbind(self):
"""Test unbinding a ListRow."""
self.row.unbind()
self.row.do_unbind.assert_called()
self.assertEqual(len(self.row.bindings), 0)
class TestInscriptionRow(unittest.TestCase):
"""Test our pre-configured InscriptionRow."""
def setUp(self):
"""Set up common variables."""
self.item = Gtk.Inscription(text="Test")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.item)
def test_inscription_row(self):
"""Test that the LabelRow works as expected."""
row = emmental.factory.InscriptionRow(self.listitem,
item_property="text")
self.assertIsInstance(row, emmental.factory.ListRow)
self.assertIsInstance(row.child, Gtk.Inscription)
self.assertEqual(row.item_property, "text")
self.assertEqual(row.child.get_xalign(), 0.0)
self.assertFalse(row.child.has_css_class("numeric"))
row.bind()
self.assertEqual(row.child.get_text(), "Test")
def test_numeric_inscription(self):
"""Test that we can create numeric Labels."""
row = emmental.factory.InscriptionRow(self.listitem,
item_property="text",
xalign=1.0, numeric=True)
self.assertEqual(row.child.get_xalign(), 1.0)
self.assertTrue(row.child.has_css_class("numeric"))
class TestFactory(unittest.TestCase):
"""Test a Factory."""
def setUp(self):
"""Set up common variables."""
self.item = Gtk.Label(label="Text")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.item)
self.factory = emmental.factory.Factory(
row_type=emmental.factory.ListRow)
def test_init(self):
"""Test that the ListFactory is set up properly."""
self.assertIsInstance(self.factory, Gtk.SignalListItemFactory)
self.assertEqual(self.factory.row_type, emmental.factory.ListRow)
def test_setup(self):
"""Test the setup signal."""
self.factory.emit("setup", self.listitem)
self.assertIsInstance(self.listitem.listrow,
emmental.factory.ListRow)
def test_bind(self):
"""Test the bind signal."""
self.factory.emit("setup", self.listitem)
self.listitem.listrow.bind = unittest.mock.Mock()
self.factory.emit("bind", self.listitem)
self.listitem.listrow.bind.assert_called()
def test_unbind(self):
"""Test the unbind signal."""
self.factory.emit("setup", self.listitem)
self.listitem.listrow.unbind = unittest.mock.Mock()
self.factory.emit("unbind", self.listitem)
self.listitem.listrow.unbind.assert_called()
def test_teardown(self):
"""Test the teardown signal."""
self.factory.emit("setup", self.listitem)
self.factory.emit("teardown", self.listitem)
self.assertIsNone(self.listitem.get_child())
self.assertIsNone(self.listitem.listrow)
class TestInscriptionFactory(unittest.TestCase):
"""Test an Inscription Factory."""
def setUp(self):
"""Set up common variables."""
self.item = Gtk.Inscription(text="Text")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.item)
def test_init(self):
"""Test a basic LabelFactory."""
factory = emmental.factory.InscriptionFactory(item_property="text",
xalign=1.0, numeric=True)
self.assertIsInstance(factory, emmental.factory.Factory)
self.assertEqual(factory.row_type, emmental.factory.InscriptionRow)
factory.emit("setup", self.listitem)
self.assertIsInstance(self.listitem.listrow,
emmental.factory.InscriptionRow)
self.assertEqual(self.listitem.get_child().get_xalign(), 1.0)
self.assertTrue(self.listitem.get_child().has_css_class("numeric"))
def test_script_type(self):
"""Test a LabelFactory that makes other types of LabelRows."""
factory = emmental.factory.InscriptionFactory(
script_type=Gtk.EditableLabel, item_property="label")
self.assertEqual(factory.row_type, Gtk.EditableLabel)