def test_key_click(self): textbox = QtGui.QLineEdit() change_slot = mock.Mock() textbox.editingFinished.connect(lambda: change_slot()) # sanity check on editingFinished signal _interaction_helpers.key_sequence_qwidget(textbox, command.KeySequence("abc"), 0) self.assertEqual(change_slot.call_count, 0) _interaction_helpers.key_click_qwidget(textbox, command.KeyClick("Enter"), 0) self.assertEqual(change_slot.call_count, 1) # test on a different Qwidget object - QtGui.QTextEdit() textbox = QtGui.QTextEdit() change_slot = mock.Mock() # Now "Enter" should not finish editing, but instead go to next line textbox.textChanged.connect(lambda: change_slot()) _interaction_helpers.key_click_qwidget(textbox, command.KeyClick("Enter"), 0) # The textChanged event appears to be fired twice instead of once # on Windows/PySide6, for reasons as yet undetermined. But for our # purposes it's good enough that it's fired at all. # xref: enthought/traitsui#1895 change_slot.assert_called() self.assertEqual(textbox.toPlainText(), "\n") # for a QLabel, one can try a key click and nothing will happen textbox = QtGui.QLabel() _interaction_helpers.key_click_qwidget(textbox, command.KeyClick("A"), 0) self.assertEqual(textbox.text(), "")
def test_key_click(self): textbox = QtGui.QLineEdit() change_slot = mock.Mock() textbox.editingFinished.connect(change_slot) # sanity check on editingFinished signal helpers.key_sequence_qwidget(textbox, command.KeySequence("abc"), 0) self.assertEqual(change_slot.call_count, 0) helpers.key_click_qwidget(textbox, command.KeyClick("Enter"), 0) self.assertEqual(change_slot.call_count, 1) # test on a different Qwidget object - QtGui.QTextEdit() textbox = QtGui.QTextEdit() change_slot = mock.Mock() # Now "Enter" should not finish editing, but instead go to next line textbox.textChanged.connect(change_slot) helpers.key_click_qwidget(textbox, command.KeyClick("Enter"), 0) self.assertEqual(change_slot.call_count, 1) self.assertEqual(textbox.toPlainText(), "\n") # for a QLabel, one can try a key click and nothing will happen textbox = QtGui.QLabel() helpers.key_click_qwidget(textbox, command.KeyClick("A"), 0) self.assertEqual(textbox.text(), "")
def test_key_click_end(self): textbox = wx.TextCtrl(self.frame) textbox.SetValue("ABCDE") textbox.SetInsertionPoint(0) # sanity check self.assertEqual(textbox.GetInsertionPoint(), 0) _interaction_helpers.key_click_text_entry(textbox, command.KeyClick("End"), 0) _interaction_helpers.key_click_text_entry(textbox, command.KeyClick("F"), 0) self.assertEqual(textbox.Value, "ABCDEF")
def test_wx_text_editing(self): # behavior: when editing the text part of a spin control box, pressing # the OK button should update the value of the HasTraits class # (tests a bug where this fails with an AttributeError) num = NumberWithRangeEditor() tester = UITester() with tester.create_ui(num) as ui: # the following is equivalent to setting the text in the text # control, then pressing OK text = tester.find_by_name(ui, "number") text.perform(command.KeyClick("1")) text.perform(command.KeyClick("Enter")) # the number traits should be between 3 and 8 self.assertTrue(3 <= num.number <= 8)
def test_key_click_disabled(self): textbox = wx.TextCtrl(self.frame) textbox.SetEditable(False) with self.assertRaises(Disabled): _interaction_helpers.key_click_text_entry( textbox, command.KeyClick("Enter"), 0)
def check_set_with_text_after_empty(self, mode): model = RangeModel() view = View( Item("value", editor=RangeEditor(low=1, high=12, mode=mode))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: number_field = tester.find_by_name(ui, "value") text = number_field.locate(locator.WidgetType.textbox) # Delete all contents of textbox for _ in range(5): text.perform(command.KeyClick("Backspace")) text.perform(command.KeySequence("11")) text.perform(command.KeyClick("Enter")) displayed = text.inspect(query.DisplayedText()) self.assertEqual(model.value, 11) self.assertEqual(displayed, str(model.value))
def test_key_click_slider_helpful_err(self): slider = wx.Slider() with self.assertRaises(ValueError) as exc: _interaction_helpers.key_click_slider(slider, command.KeyClick("Enter"), 0) self.assertIn( "['Down', 'Left', 'Page Down', 'Page Up', 'Right', 'Up']", str(exc.exception))
def test_key_click_disabled(self): textbox = QtGui.QLineEdit() textbox.setEnabled(False) change_slot = mock.Mock() textbox.editingFinished.connect(change_slot) with self.assertRaises(Disabled): helpers.key_click_qwidget(textbox, command.KeyClick("Enter"), 0) self.assertEqual(change_slot.call_count, 0)
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) _interaction_helpers.key_click_text_entry( textbox, command.KeyClick("Backspace"), 0) self.assertEqual(textbox.Value, "") self.assertEqual(handler.call_count, 1)
def check_set_with_text_valid(self, mode): model = RangeModel() view = View( Item("value", editor=RangeEditor(low=1, high=12, mode=mode))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: # sanity check self.assertEqual(model.value, 1) number_field = tester.find_by_name(ui, "value") text = number_field.locate(locator.WidgetType.textbox) if is_windows and is_wx() and mode == 'text': # For RangeTextEditor on wx and windows, the textbox # automatically gets focus and the full content is selected. # Insertion point is moved to keep the test consistent text.target.textbox.SetInsertionPointEnd() text.perform(command.KeyClick("0")) text.perform(command.KeyClick("Enter")) displayed = text.inspect(query.DisplayedText()) self.assertEqual(model.value, 10) self.assertEqual(displayed, str(model.value))
def test_custom_auto_set_false_update_text(self): # the auto_set flag is disregard for custom editor. (not true on WX) foo = Foo() view = get_view(auto_set=False, style="custom") tester = UITester() with tester.create_ui(foo, dict(view=view)) as ui: name_field = tester.find_by_name(ui, "name") name_field.perform(command.KeySequence("NEW")) name_field.perform(command.KeyClick("Enter")) display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "NEW\n") self.assertEqual(display_name, foo.name)
def test_readonly_editor(self): foo = Foo(name="A name") view = get_view(style="readonly", auto_set=True) tester = UITester() with tester.create_ui(foo, dict(view=view)) as ui: name_field = tester.find_by_name(ui, "name") with self.assertRaises(InteractionNotSupported): name_field.perform(command.KeySequence("NEW")) # Trying to type should do nothing with self.assertRaises(InteractionNotSupported): name_field.perform(command.KeyClick("Space")) display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(display_name, "A name")
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") 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_simple_auto_set_false_do_not_update_qt(self): foo = Foo(name="") view = get_view(style="simple", auto_set=False) tester = UITester() with tester.create_ui(foo, dict(view=view)) as ui: name_field = tester.find_by_name(ui, "name") name_field.perform(command.KeySequence("NEW")) # with auto-set as False the displayed name should match what has # been typed not the trait itself, After "Enter" is pressed it # should match the name trait display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "") self.assertEqual(display_name, "NEW") name_field.perform(command.KeyClick("Enter")) display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "NEW") self.assertEqual(display_name, foo.name)
def test_locate_element_and_edit(self): # varying the number of columns in the view tests the logic for # getting the correct nested ui for col in range(1, 5): obj = ListTraitTest(people=get_people(), num_columns=col) tester = UITester() with tester.create_ui(obj) as ui: # sanity check self.assertEqual(obj.people[7].name, "Fields") people_list = tester.find_by_name(ui, "people") item = people_list.locate(locator.Index(7)) name_field = item.find_by_name("name") for _ in range(6): name_field.perform(command.KeyClick("Backspace")) name_field.perform(command.KeySequence("David")) displayed = name_field.inspect(query.DisplayedText()) self.assertEqual(obj.people[7].name, "David") self.assertEqual(displayed, obj.people[7].name)