def test_key_sequence(self): # test on different Qwidget objects textboxes = [QtGui.QLineEdit(), QtGui.QTextEdit()] for i, textbox in enumerate(textboxes): change_slot = mock.Mock() textbox.textChanged.connect(change_slot) # when _interaction_helpers.key_sequence_qwidget( textbox, command.KeySequence("abc"), 0) # then if i == 0: self.assertEqual(textbox.text(), "abc") else: self.assertEqual(textbox.toPlainText(), "abc") # each keystroke fires a signal self.assertEqual(change_slot.call_count, 3) # for a QLabel, one can try a key sequence and nothing will happen textbox = QtGui.QLabel() _interaction_helpers.key_sequence_qwidget(textbox, command.KeySequence("abc"), 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 _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(change_slot) _interaction_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() _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(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_sequence_disabled(self): textbox = QtGui.QLineEdit() textbox.setEnabled(False) # this will fail, because one should not be allowed to set # cursor on the widget to type anything with self.assertRaises(Disabled): _interaction_helpers.key_sequence_qwidget( textbox, command.KeySequence("abc"), 0)
def register(registry): """Registry location and interaction handlers for EnumEditor. Parameters ---------- registry : InteractionRegistry """ _IndexedListEditor.register(registry) _IndexedRadioEditor.register(registry) _IndexedSimpleEditor.register(registry) simple_editor_text_handlers = [ ( KeyClick, ( lambda wrapper, interaction: _interaction_helpers.key_click_qwidget( control=wrapper._target.control, interaction=interaction, delay=wrapper.delay, ) ), ), ( KeySequence, ( lambda wrapper, interaction: _interaction_helpers.key_sequence_qwidget( control=wrapper._target.control, interaction=interaction, delay=wrapper.delay, ) ), ), ( DisplayedText, lambda wrapper, _: wrapper._target.control.currentText(), ), ( SelectedText, lambda wrapper, _: wrapper._target.control.currentText(), ), ] for interaction_class, handler in simple_editor_text_handlers: registry.register_interaction( target_class=SimpleEditor, interaction_class=interaction_class, handler=handler, ) registry.register_interaction( target_class=RadioEditor, interaction_class=SelectedText, handler=radio_selected_text_handler, ) registry.register_interaction( target_class=ListEditor, interaction_class=SelectedText, handler=lambda wrapper, _: wrapper._target.control.currentItem().text(), )