Skip to content

Commit

Permalink
Merge pull request #846 from enthought/maint-test-non-null-toolkit-la…
Browse files Browse the repository at this point in the history
…yout-labels

TST: Add tests for layout and labels using any non-null toolkits
  • Loading branch information
kitchoi authored Jun 1, 2020
2 parents 577137e + 5217ebf commit 4398b3d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 29 deletions.
24 changes: 24 additions & 0 deletions traitsui/tests/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
62 changes: 42 additions & 20 deletions traitsui/tests/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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]

Expand All @@ -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()
Expand Down
54 changes: 45 additions & 9 deletions traitsui/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 4398b3d

Please sign in to comment.