Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1177001 - [gui] check for a new available version and warn the user
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |