def METHOD_NAME(mocker, modal_dialog, key):
    modal_dialog.cancel_button.click = mocker.MagicMock()
    modal_dialog.cancel_button.hasFocus = mocker.MagicMock(return_value=True)
    event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier)
    modal_dialog.keyPressEvent(event)
    modal_dialog.cancel_button.click.assert_called_once_with()