Skip to content

Commit

Permalink
UI Tester - add IsEnabled query class and implementations for Button …
Browse files Browse the repository at this point in the history
…Editors (#1320)

* adding IsEnabled Query class and implementations for button editors

* adding to tests to exercise use of IsEditable

* Apply suggestions from code review

Co-authored-by: Poruri Sai Rahul <rporuri@enthought.com>

Co-authored-by: Poruri Sai Rahul <rporuri@enthought.com>
  • Loading branch information
aaronayres35 and Poruri Sai Rahul authored Oct 8, 2020
1 parent 1aea5f5 commit b855634
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions traitsui/testing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
from .tester.query import (
DisplayedText,
IsChecked,
IsEnabled,
SelectedText
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# Thanks for using Enthought open source!
#
from traitsui.testing.api import DisplayedText, MouseClick
from traitsui.testing.api import DisplayedText, IsEnabled, MouseClick
from traitsui.testing.tester._ui_tester_registry.qt4 import (
_interaction_helpers
)
Expand All @@ -31,7 +31,8 @@ def register(registry):
lambda wrapper, _: _interaction_helpers.mouse_click_qwidget(
wrapper._target.control, wrapper.delay)),
(DisplayedText,
lambda wrapper, _: wrapper._target.control.text())
lambda wrapper, _: wrapper._target.control.text()),
(IsEnabled, lambda wrapper, _: wrapper._target.control.isEnabled()),
]

for target_class in [SimpleEditor, CustomEditor]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Thanks for using Enthought open source!
#
from traitsui.qt4.ui_base import ButtonEditor
from traitsui.testing.api import DisplayedText, MouseClick
from traitsui.testing.api import DisplayedText, IsEnabled, MouseClick
from traitsui.testing.tester._ui_tester_registry.qt4 import (
_interaction_helpers
)
Expand All @@ -30,7 +30,8 @@ def register(registry):
(lambda wrapper, _: _interaction_helpers.mouse_click_qwidget(
wrapper._target.control, wrapper.delay))),
(DisplayedText,
lambda wrapper, _: wrapper._target.control.text())
lambda wrapper, _: wrapper._target.control.text()),
(IsEnabled, lambda wrapper, _: wrapper._target.control.isEnabled()),
]
for interaction_class, handler in handlers:
registry.register_interaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import wx

from traitsui.wx.button_editor import SimpleEditor, CustomEditor
from traitsui.testing.api import DisplayedText, MouseClick
from traitsui.testing.api import DisplayedText, IsEnabled, MouseClick
from traitsui.testing.tester._ui_tester_registry.wx import _interaction_helpers


Expand Down Expand Up @@ -68,6 +68,12 @@ def register(registry):
handler=lambda wrapper, _: wrapper._target.control.GetLabel()
)

registry.register_interaction(
target_class=SimpleEditor,
interaction_class=IsEnabled,
handler=lambda wrapper, _: wrapper._target.control.IsEnabled()
)

registry.register_interaction(
target_class=CustomEditor,
interaction_class=MouseClick,
Expand All @@ -79,3 +85,9 @@ def register(registry):
interaction_class=DisplayedText,
handler=lambda wrapper, _: wrapper._target.control.GetLabel()
)

registry.register_interaction(
target_class=CustomEditor,
interaction_class=IsEnabled,
handler=lambda wrapper, _: wrapper._target.control.IsEnabled()
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Thanks for using Enthought open source!
#
from traitsui.wx.ui_base import ButtonEditor
from traitsui.testing.api import DisplayedText, MouseClick
from traitsui.testing.api import DisplayedText, IsEnabled, MouseClick
from traitsui.testing.tester._ui_tester_registry.wx import _interaction_helpers


Expand All @@ -36,3 +36,9 @@ def register(registry):
interaction_class=DisplayedText,
handler=lambda wrapper, _: wrapper._target.control.GetLabel()
)

registry.register_interaction(
target_class=ButtonEditor,
interaction_class=IsEnabled,
handler=lambda wrapper, _: wrapper._target.control.IsEnabled()
)
9 changes: 9 additions & 0 deletions traitsui/testing/tester/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ class IsChecked:
Implementations should return True if checked and False if not.
"""
pass


class IsEnabled:
""" An object representing an interaction to obtain whether a widget is
enabled or not.
Implementations should return True if enabled and False if not.
"""
pass
3 changes: 3 additions & 0 deletions traitsui/tests/editors/test_button_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from traitsui.testing.api import (
DisplayedText,
IsEnabled,
MouseClick,
UITester
)
Expand Down Expand Up @@ -116,12 +117,14 @@ def check_button_disabled(self, style):
tester = UITester()
with tester.create_ui(button_text_edit, dict(view=view)) as ui:
button = tester.find_by_name(ui, "play_button")
self.assertFalse(button.inspect(IsEnabled()))

with self.assertTraitDoesNotChange(
button_text_edit, "play_button"):
button.perform(MouseClick())

button_text_edit.button_enabled = True
self.assertTrue(button.inspect(IsEnabled()))
with self.assertTraitChanges(
button_text_edit, "play_button", count=1):
button.perform(MouseClick())
Expand Down
13 changes: 7 additions & 6 deletions traitsui/tests/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import traitsui
from traitsui.basic_editor_factory import BasicEditorFactory
from traitsui.api import Group, Item, spring, View
from traitsui.testing.tester import command, query
from traitsui.testing.tester.ui_tester import UITester
from traitsui.testing.api import DisplayedText, IsEnabled, MouseClick, UITester
from traitsui.tests._tools import (
BaseTestMixin,
count_calls,
Expand Down Expand Up @@ -203,8 +202,9 @@ def test_destroy_after_ok_wx(self):

# press the OK button and close the dialog
ok_button = tester.find_by_id(ui, "OK")
self.assertEqual(ok_button.inspect(query.DisplayedText()), "OK")
ok_button.perform(command.MouseClick())
self.assertEqual(ok_button.inspect(DisplayedText()), "OK")
self.assertTrue(ok_button.inspect(IsEnabled()))
ok_button.perform(MouseClick())

self.assertIsNone(ui.control)
self.assertEqual(control.Destroy._n_calls, 1)
Expand All @@ -226,8 +226,9 @@ def test_destroy_after_ok_qt(self):

# press the OK button and close the dialog
ok_button = tester.find_by_id(ui, "OK")
self.assertEqual(ok_button.inspect(query.DisplayedText()), "OK")
ok_button.perform(command.MouseClick())
self.assertEqual(ok_button.inspect(DisplayedText()), "OK")
self.assertTrue(ok_button.inspect(IsEnabled()))
ok_button.perform(MouseClick())

self.assertIsNone(ui.control)
self.assertEqual(control.deleteLater._n_calls, 1)
Expand Down

0 comments on commit b855634

Please sign in to comment.