2
* This file is part of system-settings
4
* Copyright (C) 2016 Canonical Ltd.
6
* This program is free software: you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 3, as published
8
* by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranties of
12
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13
* PURPOSE. See the GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "systemimage.h"
20
#include "mocks/system-image-dbus/fakesystemimagedbus.h"
22
#include <libqtdbusmock/MethodCall.h>
27
class TstSystemImage: public QObject
33
QVariantMap parameters;
34
parameters["device"] = "test";
35
parameters["auto_download"] = 0;
36
parameters["channel"] = "testchannel";
37
parameters["build_number"] = 10;
38
parameters["target_build_number"] = 42;
39
parameters["version_detail"] =
40
"foo=bar,tag=OTA-100,ubuntu=101,device=102,custom=103";
42
m_siMock = new FakeSystemImageDbus(parameters);
43
m_dbus = new QDBusConnection(m_siMock->dbus());
44
m_mock = new QDBusInterface(SI_SERVICE,
46
"org.freedesktop.DBus.Mock",
48
m_methodSpy = new QSignalSpy(
49
m_mock, SIGNAL(MethodCalled(const QString &, const QVariantList &))
51
m_systemImage = new QSystemImage(*m_dbus);
53
/* The following connections help us test DBus signals that are not
54
mockable. See https://github.com/martinpitt/python-dbusmock/issues/23
57
m_siMock, SIGNAL(mockAvailableStatusChanged(
58
bool, bool, QString, int, QString, QString
60
m_systemImage, SLOT(availableStatusChanged(
61
bool, bool, QString, int, QString, QString
64
connect(m_siMock, SIGNAL(mockSettingChanged(QString, QString)),
65
m_systemImage, SLOT(settingsChanged(QString, QString)));
69
QSignalSpy destroyedSpy(m_systemImage, SIGNAL(destroyed(QObject*)));
70
m_systemImage->deleteLater();
71
QTRY_COMPARE(destroyedSpy.count(), 1);
75
void testDetailedVersionDetails()
78
details["foo"] = "bar";
79
details["tag"] = "OTA-100";
80
details["ubuntu"] = "101";
81
details["device"] = "102";
82
details["custom"] = "103";
83
QCOMPARE(m_systemImage->detailedVersionDetails(), details);
87
QCOMPARE(m_systemImage->versionTag(), QString("OTA-100"));
89
void testAvailableStatus_data()
91
QTest::addColumn<bool>("isAvailable");
92
QTest::addColumn<bool>("downloading");
93
QTest::addColumn<QString>("availableVersion");
94
QTest::addColumn<int>("updateSize");
95
QTest::addColumn<QString>("lastUpdateDate");
96
QTest::addColumn<QString>("errorReason");
97
QTest::addColumn<int>("targetBuildNumber");
99
QString updateDate("2015-01-01T20:00:00");
101
QTest::newRow("available, downloading") << true << true << "50" << 1000 << updateDate << "" << 50;
102
QTest::newRow("available, paused") << true << false << "50" << 1000 << updateDate << "paused" << 50;
104
// Target build number will be the one from Information call (at creation).
105
QTest::newRow("not available") << false << false << "" << 0 << updateDate << "" << 42;
107
void testAvailableStatus()
109
QFETCH(bool, isAvailable);
110
QFETCH(bool, downloading);
111
QFETCH(QString, availableVersion);
112
QFETCH(int, updateSize);
113
QFETCH(QString, lastUpdateDate);
114
QFETCH(QString, errorReason);
115
QFETCH(int, targetBuildNumber);
117
Q_EMIT m_siMock->mockAvailableStatusChanged(
118
isAvailable, downloading, availableVersion, updateSize,
119
lastUpdateDate, errorReason
122
QCOMPARE(m_systemImage->updateAvailable(), isAvailable);
123
QCOMPARE(m_systemImage->downloading(), downloading);
124
QCOMPARE(m_systemImage->targetBuildNumber(), targetBuildNumber);
125
QCOMPARE(m_systemImage->updateSize(), updateSize);
126
QCOMPARE(m_systemImage->errorReason(), errorReason);
128
void testDeviceName()
130
QCOMPARE(m_systemImage->deviceName(), QString("test"));
132
void testDownloadMode()
134
QCOMPARE(m_systemImage->downloadMode(), 0);
136
void testSetDownloadMode_data()
138
QTest::addColumn<int>("downloadMode");
139
QTest::addColumn<int>("targetDownloadMode");
140
QTest::newRow("never") << 0 << 0;
141
QTest::newRow("wifi") << 1 << 1;
142
QTest::newRow("always") << 2 << 2;
144
// We expect these not not change the value (default is 0 in this test)
145
QTest::newRow("too high") << 3 << 0;
146
QTest::newRow("too low") << -1 << 0;
148
void testSetDownloadMode()
150
QFETCH(int, downloadMode);
151
QFETCH(int, targetDownloadMode);
153
m_systemImage->setDownloadMode(downloadMode);
154
QCOMPARE(m_systemImage->downloadMode(), targetDownloadMode);
156
void testSettingsChangedRemotely()
158
Q_EMIT m_siMock->mockSettingChanged("auto_download", "2");
159
QCOMPARE(m_systemImage->downloadMode(), 2);
161
Q_EMIT m_siMock->mockSettingChanged("failures_before_warning", "80");
162
QCOMPARE(m_systemImage->failuresBeforeWarning(), 80);
164
void testChannelName()
166
QCOMPARE(m_systemImage->channelName(), QString("testchannel"));
168
void testBuildNumbers()
170
QCOMPARE(m_systemImage->currentUbuntuBuildNumber(), QString("101"));
171
QCOMPARE(m_systemImage->currentDeviceBuildNumber(), QString("102"));
172
QCOMPARE(m_systemImage->currentCustomBuildNumber(), QString("103"));
174
void testCheckForUpdate()
176
m_systemImage->checkForUpdate();
177
QVERIFY(m_methodSpy->wait());
178
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "CheckForUpdate");
179
QVERIFY2(reply.isValid(), "Method was not called correctly.");
180
QCOMPARE(reply.value().size(), 1); // Called once.
182
void testDownloadUpdate()
184
m_systemImage->downloadUpdate();
185
QVERIFY(m_methodSpy->wait());
186
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "DownloadUpdate");
187
QVERIFY2(reply.isValid(), "Method was not called correctly.");
188
QCOMPARE(reply.value().size(), 1); // Called once.
190
void testForceAllowGSMDownload()
192
m_systemImage->forceAllowGSMDownload();
193
QVERIFY(m_methodSpy->wait());
194
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "ForceAllowGSMDownload");
195
QVERIFY2(reply.isValid(), "Method was not called correctly.");
196
QCOMPARE(reply.value().size(), 1); // Called once.
198
void testApplyUpdate()
200
m_systemImage->applyUpdate();
201
QVERIFY(m_methodSpy->wait());
202
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "ApplyUpdate");
203
QVERIFY2(reply.isValid(), "Method was not called correctly.");
204
QCOMPARE(reply.value().size(), 1); // Called once.
206
void testCancelUpdate()
208
QString reason = m_systemImage->cancelUpdate();
209
QVERIFY(m_methodSpy->wait());
210
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "CancelUpdate");
211
QVERIFY2(reply.isValid(), "Method was not called correctly.");
212
QCOMPARE(reply.value().size(), 1); // Called once.
214
// cancelUpdate should also return an empty string (i.e. no error)
215
QCOMPARE(reason, QString(""));
217
void testPauseDownload()
219
QString reason = m_systemImage->pauseDownload();
220
QVERIFY(m_methodSpy->wait());
221
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "PauseDownload");
222
QVERIFY2(reply.isValid(), "Method was not called correctly.");
224
// pauseDownload should also return an empty string (i.e. no error)
225
QCOMPARE(reason, QString(""));
227
void testProductionReset()
229
m_systemImage->productionReset();
230
QVERIFY(m_methodSpy->wait());
231
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "ProductionReset");
232
QVERIFY2(reply.isValid(), "Method was not called correctly.");
233
QCOMPARE(reply.value().size(), 1); // Called once.
235
void testFactoryReset()
237
m_systemImage->factoryReset();
238
QVERIFY(m_methodSpy->wait());
239
QDBusReply<QList<MethodCall>> reply = m_mock->call("GetMethodCalls", "FactoryReset");
240
QVERIFY2(reply.isValid(), "Method was not called correctly.");
241
QCOMPARE(reply.value().size(), 1); // Called once.
243
void testCheckTarget()
245
// Should return true, see init().
246
QVERIFY(m_systemImage->checkTarget());
248
void testCheckingForUpdates()
250
QSignalSpy checkingChangedSpy(
251
m_systemImage, SIGNAL(checkingForUpdatesChanged())
255
m_systemImage->checkForUpdate();
256
QTRY_COMPARE(checkingChangedSpy.count(), 1);
257
QVERIFY(m_systemImage->checkingForUpdates());
260
Q_EMIT m_siMock->mockAvailableStatusChanged(
261
false, false, "", 0, "", "" // doesn't matter
263
QTRY_COMPARE(checkingChangedSpy.count(), 2);
264
QVERIFY(!m_systemImage->checkingForUpdates());
266
void testFailuresBeforeWarning()
269
QCOMPARE(m_systemImage->failuresBeforeWarning(), 3);
272
QSignalSpy *m_methodSpy;
273
FakeSystemImageDbus *m_siMock = nullptr;
274
QDBusInterface *m_mock = nullptr;
275
QSystemImage *m_systemImage = nullptr;
276
QDBusConnection *m_dbus = nullptr;
279
QTEST_GUILESS_MAIN(TstSystemImage)
280
#include "tst_systemimage.moc"