Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UITester support for TableEditor (#1707)
* initial qt implementation of table editor, updating some tests, and adding needed commands queries and locators * inital attempts at a wx implementation (all stille very ugly as tests are allowed failures already) * use UI Tester for all tests ui creation, disposal, and event processing * set all views to have sortable = false for consistency (should add a specific test for sorting) * update test_table_editor_select_row to use UI Tester to select row * use UI Tester for all single selections * first pass of a test for TableEditor_demo.py * flake8 * update some comments * remove start of wx support * require qt on test_TableEditor_demo.py * TableEditor tests all require qt * update a couple of tests to use Selected * use UITester to find editor and remove wx conditional block * More cases of using UITester to get an editor, and removing wx conditional blocks * typo and style * uncomment esc line in test * no longer need is_qt and is_wx, also couple more uses of UITester to find editor * remove redundant test * remove redundant view definition * add test using MouseDClick * add SelectedIndices query class * Docstring re-wording Co-authored-by: Poruri Sai Rahul <rporuri@enthought.com> * dont import from api internally * Add Cell and MouseDClick to the api module docstring * update SelectedIndices api * add news fragment * update Selected to also return list * upadte query object docstrings Co-authored-by: Poruri Sai Rahul <rporuri@enthought.com>
- Loading branch information
1 parent
e27a1a3
commit 360ca46
Showing
10 changed files
with
606 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add UITester support for qt TableEditor (#1707) |
51 changes: 51 additions & 0 deletions
51
traitsui/examples/demo/Standard_Editors/tests/test_TableEditor_demo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
This example demonstrates how to test interacting with a TableEditor. | ||
The GUI being tested is written in the demo under the same name (minus the | ||
preceding 'test') in the outer directory. | ||
""" | ||
|
||
import os | ||
import runpy | ||
import unittest | ||
|
||
|
||
from traitsui.testing.api import ( | ||
Cell, KeyClick, KeySequence, MouseClick, UITester | ||
) | ||
from traitsui.tests._tools import requires_toolkit, ToolkitName | ||
|
||
#: Filename of the demo script | ||
FILENAME = "TableEditor_demo.py" | ||
|
||
#: Path of the demo script | ||
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) | ||
|
||
|
||
class TestTableEditorDemo(unittest.TestCase): | ||
|
||
@requires_toolkit([ToolkitName.qt]) | ||
def test_list_editor_demo(self): | ||
demo = runpy.run_path(DEMO_PATH)["demo"] | ||
|
||
tester = UITester() | ||
with tester.create_ui(demo) as ui: | ||
employees_table = tester.find_by_name(ui, "employees") | ||
|
||
# clicking a cell enters edit mode and selects full text | ||
cell_21 = employees_table.locate(Cell(2, 1)) | ||
cell_21.perform(MouseClick()) | ||
cell_21.perform(KeySequence("Jones")) | ||
cell_21.perform(KeyClick("Enter")) | ||
|
||
self.assertEqual(demo.employees[0].last_name, 'Jones') | ||
|
||
# third column corresponds to Full Name property | ||
cell_32 = employees_table.locate(Cell(3, 2)) | ||
cell_32.perform(MouseClick()) | ||
|
||
|
||
# Run the test(s) | ||
unittest.TextTestRunner().run( | ||
unittest.TestLoader().loadTestsFromTestCase(TestTableEditorDemo) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
traitsui/testing/tester/_ui_tester_registry/qt4/_traitsui/table_editor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
from traitsui.qt4.table_editor import SimpleEditor | ||
|
||
from traitsui.testing.tester.command import ( | ||
MouseClick, | ||
MouseDClick, | ||
KeyClick, | ||
KeySequence, | ||
) | ||
from traitsui.testing.tester.locator import Cell | ||
from traitsui.testing.tester.query import ( | ||
DisplayedText, | ||
Selected, | ||
SelectedIndices, | ||
) | ||
from traitsui.testing.tester._ui_tester_registry._common_ui_targets import ( | ||
BaseSourceWithLocation | ||
) | ||
from traitsui.testing.tester._ui_tester_registry.qt4 import ( | ||
_interaction_helpers | ||
) | ||
|
||
|
||
def _query_table_editor_selected(wrapper, interaction): | ||
selected = wrapper._target.selected | ||
if not isinstance(selected, list): | ||
if selected is None: | ||
return [] | ||
else: | ||
return [selected] | ||
else: | ||
return selected | ||
|
||
|
||
def _query_table_editor_selected_indices(wrapper, interaction): | ||
selected_indices = wrapper._target.selected_indices | ||
if not isinstance(selected_indices, list): | ||
if selected_indices == -1: | ||
return [] | ||
else: | ||
return [selected_indices] | ||
else: | ||
return selected_indices | ||
|
||
|
||
class _SimpleEditorWithCell(BaseSourceWithLocation): | ||
source_class = SimpleEditor | ||
locator_class = Cell | ||
handlers = [ | ||
(MouseClick, lambda wrapper, _: wrapper._target._mouse_click( | ||
delay=wrapper.delay)), | ||
(KeyClick, lambda wrapper, interaction: wrapper._target._key_click( | ||
key=interaction.key, | ||
delay=wrapper.delay,)), | ||
( | ||
KeySequence, | ||
lambda wrapper, interaction: wrapper._target._key_sequence( | ||
sequence=interaction.sequence, | ||
delay=wrapper.delay, | ||
) | ||
), | ||
( | ||
DisplayedText, | ||
lambda wrapper, _: wrapper._target._get_displayed_text() | ||
), | ||
(MouseDClick, lambda wrapper, _: wrapper._target._mouse_dclick( | ||
delay=wrapper.delay,)), | ||
] | ||
|
||
def _get_model_view_index(self): | ||
table_view = self.source.table_view | ||
return dict( | ||
model=table_view.model(), | ||
view=table_view, | ||
index=table_view.model().index( | ||
self.location.row, self.location.column | ||
), | ||
) | ||
|
||
def _mouse_click(self, delay=0): | ||
_interaction_helpers.mouse_click_item_view( | ||
**self._get_model_view_index(), | ||
delay=delay, | ||
) | ||
|
||
def _mouse_dclick(self, delay=0): | ||
_interaction_helpers.mouse_dclick_item_view( | ||
**self._get_model_view_index(), | ||
delay=delay, | ||
) | ||
|
||
def _key_sequence(self, sequence, delay=0): | ||
_interaction_helpers.key_sequence_item_view( | ||
**self._get_model_view_index(), | ||
sequence=sequence, | ||
delay=delay, | ||
) | ||
|
||
def _key_click(self, key, delay=0): | ||
_interaction_helpers.key_click_item_view( | ||
**self._get_model_view_index(), | ||
key=key, | ||
delay=delay, | ||
) | ||
|
||
def _get_displayed_text(self): | ||
return _interaction_helpers.get_display_text_item_view( | ||
**self._get_model_view_index(), | ||
) | ||
|
||
|
||
def register(registry): | ||
""" Register interactions for the given registry. | ||
If there are any conflicts, an error will occur. | ||
Parameters | ||
---------- | ||
registry : TargetRegistry | ||
The registry being registered to. | ||
""" | ||
_SimpleEditorWithCell.register(registry) | ||
registry.register_interaction( | ||
target_class=SimpleEditor, | ||
interaction_class=Selected, | ||
handler=_query_table_editor_selected | ||
) | ||
registry.register_interaction( | ||
target_class=SimpleEditor, | ||
interaction_class=SelectedIndices, | ||
handler=_query_table_editor_selected_indices | ||
) |
Oops, something went wrong.