From 9c73bf9e97a19c39230b1289b458e1d1a3d33bfc Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Sat, 27 Oct 2018 16:12:15 -0700 Subject: [PATCH] Add tests for selection persisting - Remove unneeded override of qt home method --- qutebrowser/misc/miscwidgets.py | 7 ------- tests/unit/misc/test_miscwidgets.py | 23 +++++++++++++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/qutebrowser/misc/miscwidgets.py b/qutebrowser/misc/miscwidgets.py index 36dad4f41..085b2fdd1 100644 --- a/qutebrowser/misc/miscwidgets.py +++ b/qutebrowser/misc/miscwidgets.py @@ -105,13 +105,6 @@ class CommandLineEdit(QLineEdit): self._validator.prompt = text self._promptlen = len(text) - def home(self, mark): - """Override home so it works properly with our cursor restriction.""" - oldpos = self.cursorPosition() - self.setCursorPosition(self._promptlen) - if mark: - self.setSelection(self._promptlen, oldpos - self._promptlen) - class _CommandValidator(QValidator): diff --git a/tests/unit/misc/test_miscwidgets.py b/tests/unit/misc/test_miscwidgets.py index d6b099418..551b466e2 100644 --- a/tests/unit/misc/test_miscwidgets.py +++ b/tests/unit/misc/test_miscwidgets.py @@ -59,8 +59,8 @@ class TestCommandLineEdit: assert cmd_edit.text() == ':hello' assert cmd_edit.cursorPosition() == len(':hello') - cmd_edit.home(mark=True) - assert cmd_edit.cursorPosition() == len(':hello') + cmd_edit.home(True) + assert cmd_edit.cursorPosition() == len(':') qtbot.keyClick(cmd_edit, Qt.Key_Delete) assert cmd_edit.text() == ':' qtbot.keyClick(cmd_edit, Qt.Key_Backspace) @@ -74,6 +74,25 @@ class TestCommandLineEdit: qtbot.keyClicks(cmd_edit, '$hello') assert cmd_edit.text() == '' + def test_selection_home(self, qtbot, cmd_edit): + """Test selection persisting when pressing home.""" + qtbot.keyClicks(cmd_edit, ':hello') + assert cmd_edit.text() == ':hello' + assert cmd_edit.cursorPosition() == len(':hello') + cmd_edit.home(True) + assert cmd_edit.cursorPosition() == len(':') + assert cmd_edit.selectionStart() == len(':') + + def test_selection_backspace(self, qtbot, cmd_edit): + """Test selection persisting when backspacing to the first char.""" + qtbot.keyClicks(cmd_edit, ':hello') + assert cmd_edit.text() == ':hello' + assert cmd_edit.cursorPosition() == len(':hello') + for _ in range(len(':hello')): + qtbot.keyClick(cmd_edit, Qt.Key_Left, modifier=Qt.ShiftModifier) + assert cmd_edit.cursorPosition() == len(':') + assert cmd_edit.selectionStart() == len(':') + class WrappedWidget(QWidget):