Skip to content

Commit

Permalink
Resurrect location validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kitchoi committed Aug 7, 2020
1 parent 9362617 commit 5e0a9cd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
12 changes: 9 additions & 3 deletions traitsui/examples/demo/Advanced/List_editors_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
84 changes: 72 additions & 12 deletions traitsui/testing/qt4/default_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand All @@ -591,12 +640,23 @@ 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(
editor_class=TabbedFoldGroupEditor,
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
2 changes: 0 additions & 2 deletions traitsui/testing/qt4/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 1 addition & 4 deletions traitsui/testing/ui_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5e0a9cd

Please sign in to comment.