Skip to content

Commit

Permalink
Avoid crashing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntoll committed Feb 5, 2020
1 parent dc8b26a commit 407becb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
17 changes: 12 additions & 5 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,7 @@ def __init__(
self.controller = controller
self.file = self.controller.get_file(file_uuid)
self.index = index
self.downloading = False

# Set styles
self.setObjectName('file_widget')
Expand Down Expand Up @@ -2004,18 +2005,22 @@ def eventFilter(self, obj, event):
if t == QEvent.MouseButtonPress:
if event.button() == Qt.LeftButton:
self._on_left_click()
if self.download_animation.state() != self.download_animation.Running:
if t == QEvent.HoverEnter or t == QEvent.HoverMove:
self.download_button.setIcon(load_icon('download_file_hover.svg'))
elif t == QEvent.HoverLeave:
self.download_button.setIcon(load_icon('download_file.svg'))
# HERE BE DRAGONS. Usually I'd wrap this in an if not self.download,
# but for reasons not entirely clear, this caused a crash. The
# following odd way of expressing the same conditional doesn't cause a
# crash. Go figure... :-/
if t == QEvent.HoverEnter or t == QEvent.HoverMove and not self.downloading:
self.download_button.setIcon(load_icon('download_file_hover.svg'))
elif t == QEvent.HoverLeave and not self.downloading:
self.download_button.setIcon(load_icon('download_file.svg'))
return QObject.event(obj, event)

@pyqtSlot(str)
def _on_file_downloaded(self, file_uuid: str) -> None:
if file_uuid == self.file.uuid:
self.file = self.controller.get_file(self.file.uuid)
if self.file.is_downloaded:
self.downloading = False
self.file_name.setText(self.file.filename)
self.download_button.hide()
self.no_file_name.hide()
Expand All @@ -2029,6 +2034,7 @@ def _on_file_missing(self, file_uuid: str) -> None:
if file_uuid == self.file.uuid:
self.file = self.controller.get_file(self.file.uuid)
if not self.file.is_downloaded:
self.downloading = False
self.download_animation.stop()
self.download_button.setText(_('DOWNLOAD'))
self.download_button.setIcon(load_icon('download_file.svg'))
Expand Down Expand Up @@ -2092,6 +2098,7 @@ def start_button_animation(self):
"""
Update the download button to the animated "downloading" state.
"""
self.downloading = True
self.download_animation.frameChanged.connect(self.set_button_animation_frame)
self.download_animation.start()
self.download_button.setText(_(" DOWNLOADING "))
Expand Down
36 changes: 35 additions & 1 deletion tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ def test_FileWidget_init_file_downloaded(mocker, source, session):
assert not fw.file_name.isHidden()


def test_FileWidget_event_handler(mocker, session, source):
def test_FileWidget_event_handler_left_click(mocker, session, source):
"""
Left click on filename should trigger an open.
"""
Expand All @@ -1474,6 +1474,40 @@ def test_FileWidget_event_handler(mocker, session, source):
fw._on_left_click.call_count == 1


def test_FileWidget_event_handler_hover(mocker, session, source):
"""
Hover events when the file isn't being downloaded should change the
widget's icon.
"""
file_ = factory.File(source=source['source'],
is_downloaded=False,
is_decrypted=None)
session.add(file_)
session.commit()

mock_get_file = mocker.MagicMock(return_value=file_)
mock_controller = mocker.MagicMock(get_file=mock_get_file)

fw = FileWidget(file_.uuid, mock_controller, mocker.MagicMock(), mocker.MagicMock(), 0)
fw.download_button = mocker.MagicMock()

# Hover enter
test_event = QEvent(QEvent.HoverEnter)
fw.eventFilter(fw, test_event)
assert fw.download_button.setIcon.call_count == 1
fw.download_button.setIcon.reset_mock()
# Hover move
test_event = QEvent(QEvent.HoverMove)
fw.eventFilter(fw, test_event)
assert fw.download_button.setIcon.call_count == 1
fw.download_button.setIcon.reset_mock()
# Hover leave
test_event = QEvent(QEvent.HoverLeave)
fw.eventFilter(fw, test_event)
assert fw.download_button.setIcon.call_count == 1
fw.download_button.setIcon.reset_mock()


def test_FileWidget_on_left_click_download(mocker, session, source):
"""
Left click on download when file is not downloaded should trigger
Expand Down

0 comments on commit 407becb

Please sign in to comment.