Skip to content

Commit

Permalink
Make sure all UI in tests are disposed (#865)
Browse files Browse the repository at this point in the history
* Dispose ui in test_visible_when_layout

* Wrap test with create_ui, and store_exceptions_on_all_threads

* Dispose ui and wrap it under store_exceptions... for test_ui

* Use create_ui in test_color_column

* Wrap tests with create_ui and store_exceptions_on_all_threads

* Dispose ui in test_actions

* Consistently dispose UI with create_ui in test_code_editor

* Skip two tests on wx that fail on its own

* Dispose UI in test_table_editor; these tests are failing on their own

* Remove skips; it was the developer's fault in not refreshing their devenv

* Dispose UI in test_csv_editor

* Make sure dispose must be called in test_date_editor

* Make sure dispose is called in test_date_range_editor

* Make sure dispose is called in test_datetime_editor

* Dispose ui in test_instance_editor

* Dispose ui in test_liststr_editor_selection

* Dispose ui in test_range_editor_spinner

* Dispose ui in test_range_editor_text

* Dispose ui in test_tree_editor

* Dispose UI in test_tuple_editor

* One more in test_data_frame_editor

* Remove additional store_exceptions_on_all_threads as they are orthogonal changes

* Experiment removing skips that were added due to test interactions

* Revert "Experiment removing skips that were added due to test interactions"

This reverts commit 7466c03.

* Try removing another workaround

* Revert "Try removing another workaround"

This reverts commit 090c75c.

* Revert inconsequential change to test_color_column

* Add close back just because it was there before - not sure if it is still needed.
  • Loading branch information
kitchoi committed Jul 8, 2020
1 parent 83bec81 commit 4e4f39a
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 245 deletions.
15 changes: 7 additions & 8 deletions traitsui/tests/editors/test_code_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


from traitsui.tests._tools import (
create_ui,
skip_if_not_qt4,
store_exceptions_on_all_threads,
)
Expand Down Expand Up @@ -60,10 +61,9 @@ def is_line_numbers_visible(ui):
return txt_ctrl.line_number_widget.isVisible()

def test_line_numbers_visibility(show=True):
with store_exceptions_on_all_threads():
code_model = CodeModel()
code_view = CodeView(model=code_model, show_line_numbers=show)
ui = code_view.edit_traits()
code_model = CodeModel()
code_view = CodeView(model=code_model, show_line_numbers=show)
with store_exceptions_on_all_threads(), create_ui(code_view) as ui:
self.assertEqual(is_line_numbers_visible(ui), show)
ui.control.close()

Expand All @@ -76,10 +76,9 @@ def test_code_editor_readonly(self):
"""
from pyface import qt

with store_exceptions_on_all_threads():
code_model = CodeModel()
code_view = CodeView(model=code_model, style="readonly")
ui = code_view.edit_traits()
code_model = CodeModel()
code_view = CodeView(model=code_model, style="readonly")
with store_exceptions_on_all_threads(), create_ui(code_view) as ui:
txt_ctrl = ui.control.findChild(qt.QtGui.QPlainTextEdit)
self.assertTrue(txt_ctrl.isReadOnly())

Expand Down
15 changes: 7 additions & 8 deletions traitsui/tests/editors/test_csv_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import traitsui.editors.csv_list_editor as csv_list_editor

from traitsui.tests._tools import (
create_ui,
is_current_backend_wx,
is_current_backend_qt4,
press_ok_button,
Expand Down Expand Up @@ -55,11 +56,10 @@ def test_csv_editor_disposal(self):
# its disposal, causing errors when the hooked data is accessed after
# the window is closed (Issue #48)

list_of_floats = ListOfFloats(data=[1, 2, 3])
csv_view = ListOfFloatsWithCSVEditor(model=list_of_floats)
try:
with store_exceptions_on_all_threads():
list_of_floats = ListOfFloats(data=[1, 2, 3])
csv_view = ListOfFloatsWithCSVEditor(model=list_of_floats)
ui = csv_view.edit_traits()
with store_exceptions_on_all_threads(), create_ui(csv_view) as ui:
press_ok_button(ui)

# raise an exception if still hooked
Expand All @@ -84,10 +84,9 @@ def _qt_get_text_value(ui):
txt_ctrl = ui.control.findChild(qt.QtGui.QLineEdit)
return txt_ctrl.text()

with store_exceptions_on_all_threads():
list_of_floats = ListOfFloats(data=[1.0])
csv_view = ListOfFloatsWithCSVEditor(model=list_of_floats)
ui = csv_view.edit_traits()
list_of_floats = ListOfFloats(data=[1.0])
csv_view = ListOfFloatsWithCSVEditor(model=list_of_floats)
with store_exceptions_on_all_threads(), create_ui(csv_view) as ui:

# add element to list, make sure that editor knows about it
list_of_floats.data.append(3.14)
Expand Down
2 changes: 1 addition & 1 deletion traitsui/tests/editors/test_date_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def test_custom_selected_color(self):
def launch_editor(self, view_factory):
foo = Foo()
ui = foo.edit_traits(view=view_factory())
editor, = ui._editors
try:
editor, = ui._editors
yield foo, editor
finally:
ui.dispose()
Expand Down
2 changes: 1 addition & 1 deletion traitsui/tests/editors/test_date_range_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def test_allow_no_range(self):
def launch_editor(self, view_factory):
foo = Foo()
ui = foo.edit_traits(view=view_factory())
editor, = ui._editors
try:
editor, = ui._editors
yield foo, editor
finally:
ui.dispose()
Expand Down
2 changes: 1 addition & 1 deletion traitsui/tests/editors/test_datetime_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ def test_datetime_editor_qt_datetime_out_of_bound(self):
@contextlib.contextmanager
def launch_editor(self, object, view):
ui = object.edit_traits(view=view)
editor, = ui._editors
try:
editor, = ui._editors
yield editor
finally:
ui.dispose()
11 changes: 5 additions & 6 deletions traitsui/tests/editors/test_instance_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from traitsui.item import Item
from traitsui.view import View
from traitsui.tests._tools import (
create_ui,
press_ok_button,
skip_if_not_qt4,
store_exceptions_on_all_threads,
Expand All @@ -24,9 +25,8 @@ class TestInstanceEditor(unittest.TestCase):

@skip_if_not_qt4
def test_simple_editor(self):
with store_exceptions_on_all_threads():
obj = NonmodalInstanceEditor()
ui = obj.edit_traits()
obj = NonmodalInstanceEditor()
with store_exceptions_on_all_threads(), create_ui(obj) as ui:
editor = ui.get_editors("inst")[0]

# make the dialog appear
Expand All @@ -40,9 +40,8 @@ def test_simple_editor(self):

@skip_if_not_qt4
def test_simple_editor_parent_closed(self):
with store_exceptions_on_all_threads():
obj = NonmodalInstanceEditor()
ui = obj.edit_traits()
obj = NonmodalInstanceEditor()
with store_exceptions_on_all_threads(), create_ui(obj) as ui:
editor = ui.get_editors("inst")[0]

# make the dialog appear
Expand Down
32 changes: 16 additions & 16 deletions traitsui/tests/editors/test_liststr_editor_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from traitsui.editors.list_str_editor import ListStrEditor

from traitsui.tests._tools import (
create_ui,
press_ok_button,
skip_if_not_qt4,
skip_if_not_wx,
Expand Down Expand Up @@ -97,12 +98,11 @@ class TestListStrEditorSelection(unittest.TestCase):
def test_wx_list_str_selected_index(self):
# behavior: when starting up, the

with store_exceptions_on_all_threads():
obj = ListStrEditorWithSelectedIndex(
values=["value1", "value2"], selected_index=1
)
ui = obj.edit_traits(view=single_select_view)

obj = ListStrEditorWithSelectedIndex(
values=["value1", "value2"], selected_index=1
)
with store_exceptions_on_all_threads(), \
create_ui(obj, dict(view=single_select_view)) as ui:
# the following is equivalent to setting the text in the text
# control, then pressing OK

Expand All @@ -123,11 +123,11 @@ def test_wx_list_str_selected_index(self):
def test_wx_list_str_multi_selected_index(self):
# behavior: when starting up, the

with store_exceptions_on_all_threads():
obj = ListStrEditorWithSelectedIndex(
values=["value1", "value2"], selected_indices=[1]
)
ui = obj.edit_traits(view=multi_select_view)
obj = ListStrEditorWithSelectedIndex(
values=["value1", "value2"], selected_indices=[1]
)
with store_exceptions_on_all_threads(), \
create_ui(obj, dict(view=multi_select_view)) as ui:

# the following is equivalent to setting the text in the text
# control, then pressing OK
Expand Down Expand Up @@ -162,13 +162,13 @@ def test_selection_listener_disconnected(self):
helper = EventLoopHelper(gui=GUI(), qt_app=qt_app)

# open the UI and run until the dialog is closed
ui = obj.edit_traits(view=single_select_item_view)
with helper.delete_widget(ui.control):
press_ok_button(ui)
with create_ui(obj, dict(view=single_select_item_view)) as ui:
with helper.delete_widget(ui.control):
press_ok_button(ui)

# now run again and change the selection
ui = obj.edit_traits(view=single_select_item_view)
with event_loop():
with create_ui(obj, dict(view=single_select_item_view)) as ui, \
event_loop():
editor = ui.get_editors("values")[0]

list_view = editor.list_view
Expand Down
16 changes: 7 additions & 9 deletions traitsui/tests/editors/test_range_editor_spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from traitsui.editors.range_editor import RangeEditor

from traitsui.tests._tools import (
create_ui,
press_ok_button,
skip_if_not_wx,
skip_if_not_qt4,
Expand Down Expand Up @@ -60,10 +61,9 @@ def test_wx_spin_control_editing_should_not_crash(self):
# Bug: when editing the text part of a spin control box, pressing
# the OK button raises an AttributeError on Mac OS X

num = NumberWithSpinnerEditor()
try:
with store_exceptions_on_all_threads():
num = NumberWithSpinnerEditor()
ui = num.edit_traits()
with store_exceptions_on_all_threads(), create_ui(num) as ui:

# the following is equivalent to clicking in the text control
# of the range editor, enter a number, and clicking ok without
Expand Down Expand Up @@ -100,9 +100,8 @@ def test_wx_spin_control_editing_does_not_update(self):
if wx.VERSION >= (3, 0):
return

with store_exceptions_on_all_threads():
num = NumberWithSpinnerEditor()
ui = num.edit_traits()
num = NumberWithSpinnerEditor()
with store_exceptions_on_all_threads(), create_ui(num) as ui:

# the following is equivalent to clicking in the text control of
# the range editor, enter a number, and clicking ok without
Expand Down Expand Up @@ -135,9 +134,8 @@ def test_qt_spin_control_editing(self):

from pyface import qt

with store_exceptions_on_all_threads():
num = NumberWithSpinnerEditor()
ui = num.edit_traits()
num = NumberWithSpinnerEditor()
with store_exceptions_on_all_threads(), create_ui(num) as ui:

# the following is equivalent to clicking in the text control of
# the range editor, enter a number, and clicking ok without
Expand Down
11 changes: 5 additions & 6 deletions traitsui/tests/editors/test_range_editor_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from traitsui.editors.range_editor import RangeEditor

from traitsui.tests._tools import (
create_ui,
press_ok_button,
skip_if_not_wx,
skip_if_not_qt4,
Expand Down Expand Up @@ -66,9 +67,8 @@ def test_wx_text_editing(self):
# the OK button should update the value of the HasTraits class
# (tests a bug where this fails with an AttributeError)

with store_exceptions_on_all_threads():
num = NumberWithRangeEditor()
ui = num.edit_traits()
num = NumberWithRangeEditor()
with store_exceptions_on_all_threads(), create_ui(num) as ui:

# the following is equivalent to setting the text in the text
# control, then pressing OK
Expand All @@ -88,9 +88,8 @@ def test_avoid_slider_feedback(self):
# should not be adjusted by the slider part of the range editor
from pyface import qt

with store_exceptions_on_all_threads():
num = FloatWithRangeEditor()
ui = num.edit_traits()
num = FloatWithRangeEditor()
with store_exceptions_on_all_threads(), create_ui(num) as ui:

# the following is equivalent to setting the text in the text
# control, then pressing OK
Expand Down
Loading

0 comments on commit 4e4f39a

Please sign in to comment.