Skip to content

Commit

Permalink
allow cancel and continue on enter when focused
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Mar 3, 2020
1 parent a9540d2 commit 8cbecab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
6 changes: 5 additions & 1 deletion securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,6 @@ def __init__(self):
button_layout = QVBoxLayout()
window_buttons.setLayout(button_layout)
self.cancel_button = QPushButton(_('CANCEL'))
self.cancel_button.setAutoDefault(False)
self.cancel_button.clicked.connect(self.close)
self.continue_button = QPushButton(_('CONTINUE'))
self.continue_button.setObjectName('primary_button')
Expand Down Expand Up @@ -2291,7 +2290,12 @@ def keyPressEvent(self, event: QKeyEvent):
# window in Qubes), the default behavior is to close the dialog when the Enter or Return
# key is clicked, which we override here.
if (event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return):
if self.cancel_button.hasFocus():
self.cancel_button.click()
else:
self.continue_button.click()
return

super().keyPressEvent(event)

def close(self):
Expand Down
33 changes: 30 additions & 3 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from PyQt5.QtGui import QFocusEvent, QKeyEvent, QMovie
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QMessageBox, QMainWindow, \
QLineEdit, QDialog
QLineEdit
from sqlalchemy.orm import scoped_session, sessionmaker

from securedrop_client import db, logic
Expand Down Expand Up @@ -1888,21 +1888,48 @@ def test_FramelessDialog_keyPressEvent_does_not_close_on_enter_or_return(mocker,
'securedrop_client.gui.widgets.QApplication.activeWindow', return_value=QMainWindow())
dialog = FramelessDialog()
dialog.close = mocker.MagicMock()
event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)

event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)
dialog.keyPressEvent(event)

dialog.close.assert_not_called()


@pytest.mark.parametrize("key", [Qt.Key_Enter, Qt.Key_Return])
def test_FramelessDialog_keyPressEvent_cancel_on_enter_when_focused(mocker, key):
mocker.patch(
'securedrop_client.gui.widgets.QApplication.activeWindow', return_value=QMainWindow())
dialog = FramelessDialog()
dialog.cancel_button.click = mocker.MagicMock()
dialog.cancel_button.hasFocus = mocker.MagicMock(return_value=True)

event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)
dialog.keyPressEvent(event)

dialog.cancel_button.click.assert_called_once_with()


@pytest.mark.parametrize("key", [Qt.Key_Enter, Qt.Key_Return])
def test_FramelessDialog_keyPressEvent_continue_on_enter(mocker, key):
mocker.patch(
'securedrop_client.gui.widgets.QApplication.activeWindow', return_value=QMainWindow())
dialog = FramelessDialog()
dialog.continue_button.click = mocker.MagicMock()

event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)
dialog.keyPressEvent(event)

dialog.continue_button.click.assert_called_once_with()


@pytest.mark.parametrize("key", [Qt.Key_Alt, Qt.Key_A])
def test_FramelessDialog_keyPressEvent_does_not_close_for_other_keys(mocker, key):
mocker.patch(
'securedrop_client.gui.widgets.QApplication.activeWindow', return_value=QMainWindow())
dialog = FramelessDialog()
dialog.close = mocker.MagicMock()
event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)

event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)
dialog.keyPressEvent(event)

dialog.close.assert_not_called()
Expand Down

0 comments on commit 8cbecab

Please sign in to comment.