From 5e0a9cd5ea7d29b483af107e42a3e7989f538b56 Mon Sep 17 00:00:00 2001 From: Kit Yan Choi Date: Fri, 7 Aug 2020 19:40:57 +0100 Subject: [PATCH] Resurrect location validation --- .../demo/Advanced/List_editors_demo.py | 12 ++- traitsui/testing/qt4/default_registry.py | 84 ++++++++++++++++--- traitsui/testing/qt4/helpers.py | 2 - traitsui/testing/ui_tester.py | 5 +- 4 files changed, 82 insertions(+), 21 deletions(-) diff --git a/traitsui/examples/demo/Advanced/List_editors_demo.py b/traitsui/examples/demo/Advanced/List_editors_demo.py index 840ec47b9..0f4f5cc0a 100644 --- a/traitsui/examples/demo/Advanced/List_editors_demo.py +++ b/traitsui/examples/demo/Advanced/List_editors_demo.py @@ -125,12 +125,18 @@ class ListTraitTest(HasStrictTraits): # Run the demo (if invoked from the command line): if __name__ == '__main__': from traitsui.testing.api import UITester, locator, command - tester = UITester(delay=300) + tester = UITester(delay=200) with tester.create_ui(demo) as ui: main_tab = tester.find_by_id(ui, "splitter") main_tab.locate(locator.Index(1)).perform(command.MouseClick()) - list_ = tester.find_by_id(ui, "list").locate(locator.Index(3)) - list_.find_by_name("name").perform(command.KeySequence("\b\b\b\bDavid")) + + item = tester.find_by_id(ui, "list").locate(locator.Index(7)) + item.find_by_name("name").perform( + command.KeySequence("\b\b\b\b\b\bDavid") + ) + + item = tester.find_by_id(ui, "list").locate(locator.Index(1)) + item.find_by_name("name").perform(command.KeySequence("\b\b\b\bSimon")) main_tab.locate(locator.Index(2)).perform(command.MouseClick()) notebook = tester.find_by_id(ui, "notebook") diff --git a/traitsui/testing/qt4/default_registry.py b/traitsui/testing/qt4/default_registry.py index 97a67e492..d088b4d62 100644 --- a/traitsui/testing/qt4/default_registry.py +++ b/traitsui/testing/qt4/default_registry.py @@ -116,12 +116,25 @@ def mouse_click_index_with_layout(interactor, action): ) -def nested_ui_index_with_custom_list_editor(interactor, action): - index = interactor.location.index +def _get_grid_layout_item(interactor, location): + index = location.index row, column = divmod(index, interactor.editor.factory.columns) grid_layout = interactor.editor._list_pane.layout() - widget = grid_layout.itemAtPosition(row, column).widget() - return widget._editor._ui + item = grid_layout.itemAtPosition(row, column) + if item is None: + raise IndexError(index) + return item + + +def validate_location_with_custom_list_editor(interactor, action): + item = _get_grid_layout_item(interactor, action.location) + if interactor.editor.scrollable: + interactor.editor.control.ensureWidgetVisible(item.widget()) + + +def nested_ui_index_with_custom_list_editor(interactor, action): + item = _get_grid_layout_item(interactor, interactor.location) + return item.widget()._editor._ui def mouse_click_index_with_combobox(interactor, action): @@ -310,6 +323,13 @@ def get_displayed_text_with_table_view(interactor, action): ) +def validate_location_notebook_editor(interactor, action): + widget = interactor.editor.control + index = action.location.index + if widget.widget(index) is None: + raise IndexError(index) + + def mouse_click_notebook_editor(interactor, action): """ Implementation for clicking an item in a ListEditor with notebook. """ @@ -318,6 +338,13 @@ def mouse_click_notebook_editor(interactor, action): helpers.mouse_click_tab_index(tabbed_widget, index, delay=interactor.delay) +def validate_location_tabbed_group_editor(interactor, action): + widget = interactor.editor.control + index = action.location.index + if isinstance(widget, QtGui.QTabWidget) and widget.widget(index) is None: + raise IndexError(index) + + def mouse_click_tabbed_group_editor(interactor, action): widget = interactor.editor.control index = interactor.location.index @@ -330,19 +357,27 @@ def mouse_click_tabbed_group_editor(interactor, action): ) -def _get_tree_view_index(interactor, action): - tree_view = interactor.editor._tree - i_column = interactor.location.column - i_rows = iter(interactor.location.row) +def _get_tree_view_index(tree_view, location): + i_column = location.column + i_rows = iter(location.row) item = tree_view.topLevelItem(next(i_rows)) for i_row in i_rows: item = item.child(i_row) return tree_view.indexFromItem(item, i_column) +def locate_item_simple_tree_editor(interactor, action): + tree_view = interactor.editor._tree + location = action.location + q_model_index = _get_tree_view_index(tree_view, location) + if not q_model_index.isValid(): + raise IndexError("Item is invalid.") + + def mouse_click_simple_tree_editor(interactor, action): tree_view = interactor.editor._tree - q_model_index = _get_tree_view_index(interactor, action) + location = interactor.location + q_model_index = _get_tree_view_index(tree_view, location) if not q_model_index.isValid(): raise IndexError("Item is invalid.") tree_view.scrollTo(q_model_index) @@ -358,7 +393,8 @@ def mouse_click_simple_tree_editor(interactor, action): def mouse_dclick_simple_tree_editor(interactor, action): tree_view = interactor.editor._tree - q_model_index = _get_tree_view_index(interactor, action) + location = interactor.location + q_model_index = _get_tree_view_index(tree_view, location) if not q_model_index.isValid(): raise IndexError("Item is invalid.") tree_view.scrollTo(q_model_index) @@ -374,9 +410,10 @@ def mouse_dclick_simple_tree_editor(interactor, action): def get_display_text_simple_tree_editor(interactor, action): tree_view = interactor.editor._tree - q_model_index = _get_tree_view_index(interactor, action) + location = interactor.location + q_model_index = _get_tree_view_index(tree_view, location) item = tree_view.itemFromIndex(q_model_index) - return item.text(interactor.location.column) + return item.text(location.column) def nested_ui_simple_tree_editor(interactor, action): @@ -571,6 +608,12 @@ def get_default_registry(): action_class=query.DisplayedText, handler=get_display_text_simple_tree_editor, ) + registry.register_with_factory( + factory=TreeEditor(), + style="simple", + action_class=command.ValidateLocation, + handler=locate_item_simple_tree_editor, + ) # ListEditor registry.register_with_factory( @@ -579,6 +622,12 @@ def get_default_registry(): action_class=query.NestedUI, handler=nested_ui_index_with_custom_list_editor, ) + registry.register_with_factory( + factory=ListEditor(), + style="custom", + action_class=command.ValidateLocation, + handler=validate_location_with_custom_list_editor, + ) registry.register_with_factory( factory=ListEditor(use_notebook=True), style="custom", @@ -591,6 +640,12 @@ def get_default_registry(): action_class=query.NestedUI, handler=list_editor_notebook_get_ui, ) + registry.register_with_factory( + factory=ListEditor(use_notebook=True), + style="custom", + action_class=command.ValidateLocation, + handler=validate_location_notebook_editor, + ) # Tabbed in the UI registry.register( @@ -598,5 +653,10 @@ def get_default_registry(): action_class=command.MouseClick, handler=mouse_click_tabbed_group_editor, ) + registry.register( + editor_class=TabbedFoldGroupEditor, + action_class=command.ValidateLocation, + handler=validate_location_tabbed_group_editor, + ) return registry diff --git a/traitsui/testing/qt4/helpers.py b/traitsui/testing/qt4/helpers.py index a7a213bce..a9ab3519c 100644 --- a/traitsui/testing/qt4/helpers.py +++ b/traitsui/testing/qt4/helpers.py @@ -168,8 +168,6 @@ def get_display_text_item_view(model, row, column): def mouse_click_tab_index(tab_widget, index, delay=0): - if tab_widget.widget(index) is None: - raise IndexError(index) tabbar = tab_widget.tabBar() rect = tabbar.tabRect(index) QTest.mouseClick( diff --git a/traitsui/testing/ui_tester.py b/traitsui/testing/ui_tester.py index f95e99fe9..b16355618 100644 --- a/traitsui/testing/ui_tester.py +++ b/traitsui/testing/ui_tester.py @@ -309,10 +309,7 @@ def locate(self, location): ---------- location : Location """ - try: - self.perform(command.ValidateLocation(location)) - except ActionNotSupported: - pass + self.perform(command.ValidateLocation(location)) return UserInteractor( editor=self.editor, registries=self.registries,