Skip to content

Commit

Permalink
Bug 1177001 - [gui] check for a new available version and warn the user
Browse files Browse the repository at this point in the history
  • Loading branch information
parkouss committed Sep 7, 2015
1 parent c6d619c commit 686c455
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
53 changes: 53 additions & 0 deletions gui/mozregui/check_release.py
@@ -0,0 +1,53 @@
from PyQt4.QtCore import QObject, QThread, pyqtSlot as Slot, Qt, QUrl
from PyQt4.QtGui import QLabel, QDesktopServices
from mozregression.network import retry_get
from mozregui import __version__


class CheckReleaseThread(QThread):
GITHUB_LATEST_RELEASE_URL = (
"https://api.github.com/repos/mozilla/mozregression/releases/latest"
)

def __init__(self):
QThread.__init__(self)
self.tag_name = None
self.release_url = None

def run(self):
data = retry_get(self.GITHUB_LATEST_RELEASE_URL).json()
self.tag_name = data['tag_name']
self.release_url = data['html_url']


class CheckRelease(QObject):
def __init__(self, mainwindow):
QObject.__init__(self, mainwindow)
self.mainwindow = mainwindow
self.thread = CheckReleaseThread()
self.thread.finished.connect(self.on_release_found)
lbl = QLabel()
lbl.setTextFormat(Qt.RichText)
lbl.setTextInteractionFlags(Qt.TextBrowserInteraction)
lbl.linkActivated.connect(self.label_clicked)
self.label = lbl

def check(self):
self.thread.start()

@Slot()
def on_release_found(self):
release_name = self.thread.tag_name.replace('gui-', '')
if release_name == __version__:
return

self.label.setText(
'There is a new release available! Download the new'
' <a href="%s">release %s</a>.'
% (self.thread.release_url, release_name))
self.mainwindow.ui.status_bar.addWidget(self.label)

@Slot(str)
def label_clicked(self, link):
QDesktopServices.openUrl(QUrl(link))
self.mainwindow.ui.status_bar.removeWidget(self.label)
3 changes: 3 additions & 0 deletions gui/mozregui/main.py
Expand Up @@ -19,6 +19,7 @@
from mozregui.global_prefs import change_prefs_dialog
from mozregui.log_report import LogModel
from mozregui.report_delegate import ReportItemDelegate
from mozregui.check_release import CheckRelease


ABOUT_TEXT = """\
Expand Down Expand Up @@ -127,6 +128,8 @@ def main():
win = MainWindow()
app.aboutToQuit.connect(win.bisect_runner.stop)
app.aboutToQuit.connect(win.clear)
release_checker = CheckRelease(win)
release_checker.check()
log_model.log.connect(win.ui.log_view.on_log_received)
win.show()
win.start_bisection_wizard()
Expand Down
1 change: 1 addition & 0 deletions gui/mozregui/ui/mainwindow.ui
Expand Up @@ -160,6 +160,7 @@
</layout>
</widget>
</widget>
<widget class="QStatusBar" name="status_bar"/>
<action name="actionStart_a_new_bisection">
<property name="checkable">
<bool>false</bool>
Expand Down
59 changes: 59 additions & 0 deletions gui/tests/test_check_release.py
@@ -0,0 +1,59 @@
import pytest

from mozregui import __version__
from mozregui.main import MainWindow
from mozregui.check_release import CheckRelease, QLabel, QUrl


@pytest.yield_fixture
def mainwindow(qtbot):
main = MainWindow()
qtbot.addWidget(main)
yield main
main.clear()


def test_check_release(qtbot, mocker, mainwindow):
retry_get = mocker.patch("mozregui.check_release.retry_get")
retry_get.return_value = mocker.Mock(
json=lambda *a: {
'tag_name': '0.0',
'html_url': 'url'
}
)
status_bar = mainwindow.ui.status_bar
assert status_bar.findChild(QLabel, '') is None

checker = CheckRelease(mainwindow)
with qtbot.waitSignal(checker.thread.finished, raising=True):
checker.check()

lbl = status_bar.findChild(QLabel, '')
assert lbl
assert "There is a new release available!" in str(lbl.text())
assert '0.0' in str(lbl.text())

# simulate click on the link
open_url = mocker.patch("mozregui.check_release.QDesktopServices.openUrl")
checker.label_clicked("http://url")

open_url.assert_called_once_with(QUrl("http://url"))
assert not lbl.isVisible()


def test_check_release_no_update(qtbot, mocker, mainwindow):
retry_get = mocker.patch("mozregui.check_release.retry_get")
retry_get.return_value = mocker.Mock(
json=lambda *a: {
'tag_name': 'gui-' + __version__,
'html_url': 'url'
}
)
status_bar = mainwindow.ui.status_bar
assert status_bar.findChild(QLabel, '') is None

checker = CheckRelease(mainwindow)
with qtbot.waitSignal(checker.thread.finished, raising=True):
checker.check()

assert status_bar.findChild(QLabel, '') is None

0 comments on commit 686c455

Please sign in to comment.