Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Add tests for layout and labels using any non-null toolkits #846

Merged
merged 5 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
ievacerny marked this conversation as resolved.
Show resolved Hide resolved
try:
yield ui
finally:
ui.dispose()


# ######### Utility tools to test on both qt4 and wx


Expand Down
55 changes: 39 additions & 16 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 @@ -157,9 +158,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:
ievacerny marked this conversation as resolved.
Show resolved Hide resolved

# all labels
labels = ui.control.findChildren(qt.QtGui.QLabel)
Expand Down Expand Up @@ -197,23 +197,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 +222,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