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 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(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 = wx.TextCtrl(self.frame) textbox.SetEditable(False) with self.assertRaises(Disabled): _interaction_helpers.key_sequence_text_ctrl( textbox, command.KeySequence("abc"), 0)
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): helpers.key_sequence_qwidget(textbox, command.KeySequence("abc"), 0)
def test_key_sequence_with_backspace_unsupported(self): textbox = wx.TextCtrl(self.frame) with self.assertRaises(ValueError) as exception_context: _interaction_helpers.key_sequence_text_ctrl( textbox, command.KeySequence("\b"), 0) self.assertIn( "is currently not supported.", str(exception_context.exception), )
def test_key_sequence(self): # The insertion point is moved to the end textbox = wx.TextCtrl(self.frame) textbox.SetValue("123") handler = mock.Mock() textbox.Bind(wx.EVT_TEXT, handler) helpers.key_sequence_text_ctrl(textbox, command.KeySequence("abc"), 0) self.assertEqual(textbox.GetValue(), "123abc") self.assertEqual(handler.call_count, 3)
def test_simple_auto_set_update_text(self): foo = Foo() view = get_view(style="simple", auto_set=True) tester = UITester() with tester.create_ui(foo, dict(view=view)) as ui: with self.assertTraitChanges(foo, "name", count=3): name_field = tester.find_by_name(ui, "name") name_field.perform(command.KeySequence("NEW")) # with auto-set the displayed name should match the name trait display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "NEW") self.assertEqual(display_name, foo.name)
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_key_sequence_insert_point_qlineedit(self): textbox = QtGui.QLineEdit() textbox.setText("123") # when helpers.key_sequence_textbox( textbox, command.KeySequence("abc"), delay=0, ) # then self.assertEqual(textbox.text(), "123abc")
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_custom_auto_set_true_update_text(self): # the auto_set flag is disregard for custom editor. (not true on WX) foo = Foo() view = get_view(auto_set=True, style="custom") tester = UITester() with tester.create_ui(foo, dict(view=view)) as ui: with self.assertTraitChanges(foo, "name", count=3): name_field = tester.find_by_name(ui, "name") name_field.perform(command.KeySequence("NEW")) # with auto-set the displayed name should match the name trait display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "NEW") self.assertEqual(display_name, foo.name)
def test_key_sequence_unsupported_key(self): textbox = QtGui.QLineEdit() with self.assertRaises(ValueError) as exception_context: # QTest does not support this character. helpers.key_sequence_textbox( textbox, command.KeySequence(chr(31)), delay=0, ) self.assertIn( "is currently not supported.", str(exception_context.exception), )
def test_key_sequence_insert_point_qtextedit(self): # The default insertion point moved to the end to be consistent # with QLineEdit textbox = QtGui.QTextEdit() textbox.setText("123") # when helpers.key_sequence_textbox( textbox, command.KeySequence("abc"), delay=0, ) # then self.assertEqual(textbox.toPlainText(), "123abc")
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_sequence_backspace_character(self): # Qt does convert backspace character to the backspace key # But we disallow it for now to be consistent with wx. textbox = QtGui.QLineEdit() with self.assertRaises(ValueError) as exception_context: helpers.key_sequence_textbox( textbox, command.KeySequence("\b"), delay=0, ) self.assertIn( "is currently not supported.", str(exception_context.exception), )
def test_key_sequence_textbox_with_unicode(self): for code in range(32, 127): with self.subTest(code=code, word=chr(code)): textbox = QtGui.QLineEdit() change_slot = mock.Mock() textbox.textChanged.connect(change_slot) # when helpers.key_sequence_textbox( textbox, command.KeySequence(chr(code) * 3), delay=0, ) # then self.assertEqual(textbox.text(), chr(code) * 3) self.assertEqual(change_slot.call_count, 3)
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)
def test_key_sequence_with_unicode(self): handler = mock.Mock() textbox = wx.TextCtrl(self.frame) textbox.Bind(wx.EVT_TEXT, handler) # This range is supported by Qt for code in range(32, 127): with self.subTest(code=code, word=chr(code)): textbox.Clear() handler.reset_mock() # when _interaction_helpers.key_sequence_text_ctrl( textbox, command.KeySequence(chr(code) * 3), delay=0, ) # then self.assertEqual(textbox.Value, chr(code) * 3) self.assertEqual(handler.call_count, 3)
def test_custom_auto_set_false_do_not_update_wx(self): foo = Foo(name="") view = View( Item("name", editor=TextEditor(auto_set=False), style="custom"), Item("nickname", editor=TextEditor(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")) # with auto-set as False the displayed name should match what has # been typed not the trait itself, After moving to another textbox # it should match the name trait display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "") self.assertEqual(display_name, "NEW") tester.find_by_name(ui, "nickname").perform(command.MouseClick()) display_name = name_field.inspect(query.DisplayedText()) self.assertEqual(foo.name, "NEW") self.assertEqual(display_name, foo.name)