From b855634ec98690b84228c7c7cdcd66b1ebe8b2ba Mon Sep 17 00:00:00 2001 From: aaronayres35 <36972686+aaronayres35@users.noreply.github.com> Date: Thu, 8 Oct 2020 11:09:42 -0500 Subject: [PATCH] UI Tester - add IsEnabled query class and implementations for Button 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 Co-authored-by: Poruri Sai Rahul --- traitsui/testing/api.py | 1 + .../qt4/_traitsui/button_editor.py | 5 +++-- .../_ui_tester_registry/qt4/_traitsui/ui_base.py | 5 +++-- .../wx/_traitsui/button_editor.py | 14 +++++++++++++- .../_ui_tester_registry/wx/_traitsui/ui_base.py | 8 +++++++- traitsui/testing/tester/query.py | 9 +++++++++ traitsui/tests/editors/test_button_editor.py | 3 +++ traitsui/tests/test_ui.py | 13 +++++++------ 8 files changed, 46 insertions(+), 12 deletions(-) diff --git a/traitsui/testing/api.py b/traitsui/testing/api.py index fcb2a2cf3..469a9f8cc 100644 --- a/traitsui/testing/api.py +++ b/traitsui/testing/api.py @@ -83,6 +83,7 @@ from .tester.query import ( DisplayedText, IsChecked, + IsEnabled, SelectedText ) diff --git a/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/button_editor.py b/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/button_editor.py index c13964548..1b406cd5d 100644 --- a/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/button_editor.py +++ b/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/button_editor.py @@ -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 ) @@ -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]: diff --git a/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/ui_base.py b/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/ui_base.py index e9bf8b639..133d82b0d 100644 --- a/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/ui_base.py +++ b/traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/ui_base.py @@ -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 ) @@ -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( diff --git a/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/button_editor.py b/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/button_editor.py index b2c95da93..4cd85da79 100644 --- a/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/button_editor.py +++ b/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/button_editor.py @@ -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 @@ -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, @@ -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() + ) diff --git a/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/ui_base.py b/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/ui_base.py index 6c478602b..8700fcc00 100644 --- a/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/ui_base.py +++ b/traitsui/testing/tester/_ui_tester_registry/wx/_traitsui/ui_base.py @@ -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 @@ -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() + ) diff --git a/traitsui/testing/tester/query.py b/traitsui/testing/tester/query.py index 6306bab3c..c46219cd1 100644 --- a/traitsui/testing/tester/query.py +++ b/traitsui/testing/tester/query.py @@ -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 diff --git a/traitsui/tests/editors/test_button_editor.py b/traitsui/tests/editors/test_button_editor.py index 445f4f033..5a5e0327f 100644 --- a/traitsui/tests/editors/test_button_editor.py +++ b/traitsui/tests/editors/test_button_editor.py @@ -11,6 +11,7 @@ ) from traitsui.testing.api import ( DisplayedText, + IsEnabled, MouseClick, UITester ) @@ -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()) diff --git a/traitsui/tests/test_ui.py b/traitsui/tests/test_ui.py index 5d101b9e0..48d5113bf 100644 --- a/traitsui/tests/test_ui.py +++ b/traitsui/tests/test_ui.py @@ -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, @@ -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) @@ -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)