diff --git a/traitsui/tests/_tools.py b/traitsui/tests/_tools.py index 8fbac4a6a..19fbb9a9f 100644 --- a/traitsui/tests/_tools.py +++ b/traitsui/tests/_tools.py @@ -131,6 +131,30 @@ def filter_tests(test_suite, exclusion_pattern): return filtered_test_suite +@contextmanager +def create_ui(object, ui_kwargs=None): + """ Context manager for creating a UI and then dispose it when exiting + the context. + + Parameters + ---------- + object : HasTraits + An object from which ``edit_traits`` can be called to create a UI + ui_kwargs : dict or None + Keyword arguments to be provided to ``edit_traits``. + + Yields + ------ + ui: UI + """ + ui_kwargs = {} if ui_kwargs is None else ui_kwargs + ui = object.edit_traits(**ui_kwargs) + try: + yield ui + finally: + ui.dispose() + + # ######### Utility tools to test on both qt4 and wx diff --git a/traitsui/tests/test_labels.py b/traitsui/tests/test_labels.py index 35d7d9520..35c01b957 100644 --- a/traitsui/tests/test_labels.py +++ b/traitsui/tests/test_labels.py @@ -25,6 +25,7 @@ from traitsui.group import VGroup, HGroup from traitsui.tests._tools import ( + create_ui, is_control_enabled, is_current_backend_qt4, skip_if_not_qt4, @@ -132,10 +133,9 @@ def test_qt_show_labels_right_without_colon(self): # that are shown to the *right* of the corresponding elements from pyface import qt - - with store_exceptions_on_all_threads(): - dialog = ShowRightLabelsDialog() - ui = dialog.edit_traits() + dialog = ShowRightLabelsDialog() + with store_exceptions_on_all_threads(), \ + create_ui(dialog) as ui: # get reference to label objects labels = ui.control.findChildren(qt.QtGui.QLabel) @@ -157,9 +157,8 @@ def _test_qt_labels_right_resizing(self, dialog_class): from pyface import qt - with store_exceptions_on_all_threads(): - dialog = dialog_class() - ui = dialog.edit_traits() + with store_exceptions_on_all_threads(), \ + create_ui(dialog_class()) as ui: # all labels labels = ui.control.findChildren(qt.QtGui.QLabel) @@ -197,23 +196,13 @@ def test_qt_labels_right_resizing_vertical(self): def test_qt_labels_right_resizing_horizontal(self): self._test_qt_labels_right_resizing(HResizeTestDialog) - @skip_if_not_qt4 - def test_qt_no_labels_on_the_right_bug(self): - # Bug: If one set show_left=False, show_label=False on a non-resizable - # item like a checkbox, the Qt backend tried to set the label's size - # policy and failed because label=None. - - with store_exceptions_on_all_threads(): - dialog = NoLabelResizeTestDialog() - ui = dialog.edit_traits() - @skip_if_null def test_labels_enabled_when(self): # Behaviour: label should enable/disable along with editor - with store_exceptions_on_all_threads(): - dialog = EnableWhenDialog() - ui = dialog.edit_traits() + dialog = EnableWhenDialog() + with store_exceptions_on_all_threads(), \ + create_ui(dialog) as ui: labelled_editor = ui.get_editors("labelled_item")[0] @@ -232,6 +221,39 @@ def test_labels_enabled_when(self): ui.dispose() +@skip_if_null +class TestAnyToolkit(unittest.TestCase): + """ Toolkit-agnostic tests for labels with different orientations.""" + + def test_group_show_right_labels(self): + with store_exceptions_on_all_threads(), \ + create_ui(ShowRightLabelsDialog()): + pass + + def test_horizontal_resizable_and_labels(self): + with store_exceptions_on_all_threads(), \ + create_ui(HResizeTestDialog()): + pass + + def test_all_resizable_with_labels(self): + with store_exceptions_on_all_threads(), \ + create_ui(VResizeTestDialog()): + pass + + def test_show_right_with_no_label(self): + # Bug: If one set show_left=False, show_label=False on a non-resizable + # item like a checkbox, the Qt backend tried to set the label's size + # policy and failed because label=None. + with store_exceptions_on_all_threads(), \ + create_ui(NoLabelResizeTestDialog()): + pass + + def test_enable_when_flag(self): + with store_exceptions_on_all_threads(), \ + create_ui(EnableWhenDialog()): + pass + + if __name__ == "__main__": # Execute from command line for manual testing vw = HResizeTestDialog() diff --git a/traitsui/tests/test_layout.py b/traitsui/tests/test_layout.py index 33df35efc..1a5103c39 100644 --- a/traitsui/tests/test_layout.py +++ b/traitsui/tests/test_layout.py @@ -27,7 +27,9 @@ from traitsui.group import HGroup, VGroup from traitsui.tests._tools import ( + create_ui, skip_if_not_qt4, + skip_if_null, store_exceptions_on_all_threads, ) @@ -37,6 +39,13 @@ _TXT_WIDTH = 100 +class MultipleTrait(HasTraits): + """ An object with multiple traits to test layout and alignments.""" + + txt1 = Str("text1") + txt2 = Str("text2") + + class VResizeDialog(HasTraits): txt = Str("hallo") @@ -71,11 +80,10 @@ def test_qt_resizable_in_vgroup(self): from pyface import qt - with store_exceptions_on_all_threads(): - dialog = VResizeDialog() - ui = dialog.edit_traits() - - text = ui.control.findChild(qt.QtGui.QLineEdit) + with store_exceptions_on_all_threads(), \ + create_ui(VResizeDialog()) as ui: + editor, = ui.get_editors("txt") + text = editor.control # horizontal size should be large self.assertGreater(text.width(), _DIALOG_WIDTH - 100) @@ -91,11 +99,11 @@ def test_qt_resizable_in_hgroup(self): from pyface import qt - with store_exceptions_on_all_threads(): - dialog = HResizeDialog() - ui = dialog.edit_traits() + with store_exceptions_on_all_threads(), \ + create_ui(HResizeDialog()) as ui: - text = ui.control.findChild(qt.QtGui.QLineEdit) + editor, = ui.get_editors("txt") + text = editor.control # vertical size should be large self.assertGreater(text.height(), _DIALOG_HEIGHT - 100) @@ -106,6 +114,34 @@ def test_qt_resizable_in_hgroup(self): # self.assertLess(text.width(), _TXT_WIDTH+100) +@skip_if_null +class TestOrientation(unittest.TestCase): + """ Toolkit-agnostic tests on the layout orientations.""" + + def test_vertical_layout(self): + view = View( + VGroup( + Item("txt1"), + Item("txt2"), + ) + ) + with store_exceptions_on_all_threads(), \ + create_ui(MultipleTrait(), ui_kwargs=dict(view=view)): + pass + + def test_horizontal_layout(self): + # layout + view = View( + HGroup( + Item("txt1"), + Item("txt2"), + ) + ) + with store_exceptions_on_all_threads(), \ + create_ui(MultipleTrait(), ui_kwargs=dict(view=view)): + pass + + if __name__ == "__main__": # Execute from command line for manual testing vw = VResizeDialog()