Skip to content

Commit

Permalink
Add unit test for downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Feb 3, 2022
1 parent c49429d commit 57484ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/gui/updater/ocupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ QString OCUpdater::statusString() const
}
}

int OCUpdater::downloadState() const
OCUpdater::DownloadState OCUpdater::downloadState() const
{
return _state;
}
Expand Down Expand Up @@ -360,12 +360,12 @@ void NSISUpdater::versionInfoArrived(const UpdateInfo &info)
qCInfo(lcUpdater) << "Client is on latest version!";
setDownloadState(UpToDate);
} else {
QString url = info.downloadUrl();
const QString url = info.downloadUrl();
if (url.isEmpty()) {
showNoUrlDialog(info);
} else {
_targetFile = ConfigFile::configPath() + url.mid(url.lastIndexOf('/') + 1);
if (QFile(_targetFile).exists()) {
if (QFile::exists(_targetFile)) {
setDownloadState(DownloadComplete);
} else {
auto request = QNetworkRequest(QUrl(url));
Expand Down
8 changes: 6 additions & 2 deletions src/gui/updater/ocupdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class OCUpdater : public Updater
DownloadFailed,
DownloadTimedOut,
UpdateOnlyAvailableThroughSystem };
Q_ENUM(DownloadState);

explicit OCUpdater(const QUrl &url);

void setUpdateUrl(const QUrl &url);
Expand All @@ -106,7 +108,7 @@ class OCUpdater : public Updater
void checkForUpdate() override;

QString statusString() const;
int downloadState() const;
DownloadState downloadState() const;
void setDownloadState(DownloadState state);

signals:
Expand Down Expand Up @@ -134,7 +136,7 @@ private slots:

private:
QUrl _updateUrl;
int _state;
DownloadState _state;
QNetworkAccessManager *_accessManager;
QTimer *_timeoutWatchdog; /** Timer to guard the timeout of an individual network request */
UpdateInfo _updateInfo;
Expand Down Expand Up @@ -162,6 +164,8 @@ private slots:
void versionInfoArrived(const UpdateInfo &info) override;
QScopedPointer<QTemporaryFile> _file;
QString _targetFile;

friend class TestUpdater;
};

/**
Expand Down
60 changes: 60 additions & 0 deletions test/testupdater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This software is in the public domain, furnished "as is", without technical
support, and with no warranty, express or implied, as to its usefulness for
any purpose.
*/

#include <QtTest>

#include "updater/updater.h"
#include "updater/ocupdater.h"

namespace OCC {

class TestUpdater : public QObject
{
Q_OBJECT

private slots:
void testVersionToInt()
{
auto lowVersion = Updater::Helper::versionToInt(1, 2, 80, 3000);
QCOMPARE(Updater::Helper::stringVersionToInt("1.2.80.3000"), lowVersion);

auto highVersion = Updater::Helper::versionToInt(999, 2, 80, 3000);
auto currVersion = Updater::Helper::currentVersionToInt();
QVERIFY(currVersion > 0);
QVERIFY(currVersion > lowVersion);
QVERIFY(currVersion < highVersion);
}

void testDownload_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<OCUpdater::DownloadState>("result");
// a redirect to attic
QTest::newRow("redirect") << "https://download.owncloud.com/desktop/stable/ownCloud-2.2.4.6408-setup.exe" << OCUpdater::DownloadComplete;
QTest::newRow("broken url") << "https://&" << OCUpdater::DownloadFailed;
}

void testDownload()
{
QFETCH(QString, url);
QFETCH(OCUpdater::DownloadState, result);
UpdateInfo info;
info.setDownloadUrl(url);
info.setVersionString("ownCloud 2.2.4 (build 6408)");
// esnure we do the update
info.setVersion("100.2.4.6408");
auto *updater = new NSISUpdater({});
QSignalSpy downloadSpy(updater, &NSISUpdater::slotDownloadFinished);
updater->versionInfoArrived(info);
downloadSpy.wait();
QCOMPARE(updater->downloadState(), result);
updater->deleteLater();
}
};
}

QTEST_MAIN(OCC::TestUpdater)
#include "testupdater.moc"

0 comments on commit 57484ad

Please sign in to comment.