Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions traitsui/testing/tester/wx/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ def key_click_text_ctrl(control, interaction, delay):
control.ProcessEvent(event)
elif interaction.key == "Backspace":
wx.MilliSleep(delay)
pos = control.GetInsertionPoint()
control.Remove(max(0, pos - 1), pos)
if control.GetStringSelection():
control.Remove(*control.GetSelection())
else:
pos = control.GetInsertionPoint()
control.Remove(max(0, pos - 1), pos)
else:
check_key_compat(interaction.key)
wx.MilliSleep(delay)
Expand Down
29 changes: 29 additions & 0 deletions traitsui/testing/tester/wx/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,40 @@ def test_key_sequence_disabled(self):

def test_key_click(self):
textbox = wx.TextCtrl(self.frame)
handler = mock.Mock()
textbox.Bind(wx.EVT_TEXT, handler)

helpers.key_click_text_ctrl(textbox, command.KeyClick("A"), 0)

self.assertEqual(textbox.Value, "A")
self.assertEqual(handler.call_count, 1)

def test_key_click_backspace(self):
textbox = wx.TextCtrl(self.frame)
textbox.SetValue("A")
handler = mock.Mock()
textbox.Bind(wx.EVT_TEXT, handler)

helpers.key_click_text_ctrl(textbox, command.KeyClick("Backspace"), 0)

self.assertEqual(textbox.Value, "")
self.assertEqual(handler.call_count, 1)

def test_key_click_backspace_with_selection(self):
textbox = wx.TextCtrl(self.frame)
textbox.SetFocus()
textbox.SetValue("ABCDE")
textbox.SetSelection(0, 4)
# sanity check
self.assertEqual(textbox.GetStringSelection(), "ABCD")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pedantic: This is not really a test objective so if the setup is not what we expect, we should raise a different exception to cause the test to "error", not to "fail". I am not a purist and the comment explains the intent of this assertion already so I am going to leave it here. Happy to remove it too if others find this confusing.


handler = mock.Mock()
textbox.Bind(wx.EVT_TEXT, handler)

helpers.key_click_text_ctrl(textbox, command.KeyClick("Backspace"), 0)

self.assertEqual(textbox.Value, "E")
self.assertEqual(handler.call_count, 1)

def test_key_click_disabled(self):
textbox = wx.TextCtrl(self.frame)
Expand Down