Changeset View
Changeset View
Standalone View
Standalone View
libdiscover/backends/FwupdBackend/tests/UpdateFwupdTest.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License as * | ||||
6 | * published by the Free Software Foundation; either version 2 of * | ||||
7 | * the License or (at your option) version 3 or any later version * | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved * | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy * | ||||
10 | * defined in Section 14 of version 3 of the license. * | ||||
11 | * * | ||||
12 | * This program is distributed in the hope that it will be useful, * | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
15 | * GNU General Public License for more details. * | ||||
16 | * * | ||||
17 | * You should have received a copy of the GNU General Public License * | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||||
19 | ***************************************************************************/ | ||||
20 | | ||||
21 | #include "FwupdTest.h" | ||||
22 | #include <tests/modeltest.h> | ||||
23 | #include <KFormat> | ||||
24 | #include <resources/ResourcesModel.h> | ||||
25 | #include <resources/ResourcesProxyModel.h> | ||||
26 | #include <resources/AbstractBackendUpdater.h> | ||||
27 | #include <ApplicationAddonsModel.h> | ||||
28 | #include <ReviewsBackend/ReviewsModel.h> | ||||
29 | #include <UpdateModel/UpdateModel.h> | ||||
30 | #include <resources/ResourcesUpdatesModel.h> | ||||
31 | #include <Transaction/TransactionModel.h> | ||||
32 | | ||||
33 | #include <qtest.h> | ||||
34 | #include <QtTest> | ||||
35 | #include <QAction> | ||||
36 | | ||||
37 | class UpdateFwupdTest | ||||
38 | : public QObject | ||||
39 | { | ||||
40 | Q_OBJECT | ||||
41 | public: | ||||
42 | AbstractResourcesBackend* backendByName(ResourcesModel* m, const QString& name) | ||||
43 | { | ||||
44 | QVector<AbstractResourcesBackend*> backends = m->backends(); | ||||
45 | foreach(AbstractResourcesBackend* backend, backends) { | ||||
46 | if(QLatin1String(backend->metaObject()->className()) == name) { | ||||
47 | return backend; | ||||
48 | } | ||||
49 | } | ||||
50 | return nullptr; | ||||
51 | } | ||||
52 | | ||||
53 | UpdateFwupdTest(QObject* parent = nullptr): QObject(parent) | ||||
54 | { | ||||
55 | m_model = new ResourcesModel(QStringLiteral("fwupd-backend"), this); | ||||
56 | m_appBackend = backendByName(m_model, QStringLiteral("FwupdBackend")); | ||||
57 | } | ||||
58 | | ||||
59 | private Q_SLOTS: | ||||
60 | void init() | ||||
61 | { | ||||
62 | QVERIFY(m_appBackend); | ||||
63 | while(m_appBackend->isFetching()) { | ||||
64 | QSignalSpy spy(m_appBackend, &AbstractResourcesBackend::fetchingChanged); | ||||
65 | QVERIFY(spy.wait()); | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | void testInformation() | ||||
70 | { | ||||
71 | ResourcesUpdatesModel* rum = new ResourcesUpdatesModel(this); | ||||
72 | new ModelTest(rum, rum); | ||||
73 | | ||||
74 | UpdateModel* m = new UpdateModel(this); | ||||
75 | new ModelTest(m, m); | ||||
76 | m->setBackend(rum); | ||||
77 | | ||||
78 | rum->prepare(); | ||||
79 | QSignalSpy spySetup(m_appBackend->backendUpdater(), &AbstractBackendUpdater::progressingChanged); | ||||
80 | QVERIFY(!m_appBackend->backendUpdater()->isProgressing() || spySetup.wait()); | ||||
81 | QCOMPARE(m_appBackend->updatesCount(), m_appBackend->property("startElements").toInt()*2/3); | ||||
82 | QCOMPARE(m->hasUpdates(), true); | ||||
83 | | ||||
84 | QCOMPARE(m->index(0,0).data(UpdateModel::ChangelogRole).toString(), {}); | ||||
85 | | ||||
86 | QSignalSpy spy(m, &QAbstractItemModel::dataChanged); | ||||
87 | m->fetchChangelog(0); | ||||
88 | QVERIFY(spy.count() || spy.wait()); | ||||
89 | QCOMPARE(spy.count(), 1); | ||||
90 | delete m; | ||||
91 | } | ||||
92 | | ||||
93 | void testUpdate() | ||||
94 | { | ||||
95 | ResourcesUpdatesModel* rum = new ResourcesUpdatesModel(this); | ||||
96 | new ModelTest(rum, rum); | ||||
97 | | ||||
98 | UpdateModel* m = new UpdateModel(this); | ||||
99 | new ModelTest(m, m); | ||||
100 | m->setBackend(rum); | ||||
101 | | ||||
102 | rum->prepare(); | ||||
103 | QSignalSpy spySetup(m_appBackend->backendUpdater(), &AbstractBackendUpdater::progressingChanged); | ||||
104 | QVERIFY(!m_appBackend->backendUpdater()->isProgressing() || spySetup.wait()); | ||||
105 | QCOMPARE(m_appBackend->updatesCount(), m_appBackend->property("startElements").toInt()*2/3); | ||||
106 | QCOMPARE(m->hasUpdates(), true); | ||||
107 | | ||||
108 | for(int i=0, c=m->rowCount(); i<c; ++i) { | ||||
109 | const QModelIndex resourceIdx = m->index(i,0); | ||||
110 | QVERIFY(resourceIdx.isValid()); | ||||
111 | | ||||
112 | AbstractResource* res = qobject_cast<AbstractResource*>(resourceIdx.data(UpdateModel::ResourceRole).value<QObject*>()); | ||||
113 | QVERIFY(res); | ||||
114 | | ||||
115 | QCOMPARE(Qt::CheckState(resourceIdx.data(Qt::CheckStateRole).toInt()), Qt::Checked); | ||||
116 | QVERIFY(m->setData(resourceIdx, int(Qt::Unchecked), Qt::CheckStateRole)); | ||||
117 | QCOMPARE(Qt::CheckState(resourceIdx.data(Qt::CheckStateRole).toInt()), Qt::Unchecked); | ||||
118 | QCOMPARE(resourceIdx.data(Qt::DisplayRole).toString(), res->name()); | ||||
119 | | ||||
120 | if (i!=0) { | ||||
121 | QVERIFY(m->setData(resourceIdx, int(Qt::Checked), Qt::CheckStateRole)); | ||||
122 | } | ||||
123 | } | ||||
124 | | ||||
125 | QSignalSpy spy(rum, &ResourcesUpdatesModel::progressingChanged); | ||||
126 | QVERIFY(!rum->isProgressing() || spy.wait()); | ||||
127 | QCOMPARE(rum->isProgressing(), false); | ||||
128 | | ||||
129 | QCOMPARE(m_appBackend->updatesCount(), m->rowCount()); | ||||
130 | QCOMPARE(m->hasUpdates(), true); | ||||
131 | | ||||
132 | rum->prepare(); | ||||
133 | | ||||
134 | spy.clear(); | ||||
135 | QCOMPARE(rum->isProgressing(), false); | ||||
136 | rum->updateAll(); | ||||
137 | QVERIFY(spy.count() || spy.wait()); | ||||
138 | QCOMPARE(rum->isProgressing(), true); | ||||
139 | | ||||
140 | QCOMPARE(TransactionModel::global()->rowCount(), 1); | ||||
141 | connect(TransactionModel::global(), &TransactionModel::progressChanged, this, []() { | ||||
142 | const int progress = TransactionModel::global()->progress(); | ||||
143 | static int lastProgress = -1; | ||||
144 | Q_ASSERT(progress >= lastProgress || (TransactionModel::global()->rowCount() == 0 && progress == 0)); | ||||
145 | lastProgress = progress; | ||||
146 | }); | ||||
147 | | ||||
148 | QTest::qWait(20); | ||||
149 | QScopedPointer<ResourcesUpdatesModel> rum2(new ResourcesUpdatesModel(this)); | ||||
150 | new ModelTest(rum2.data(), rum2.data()); | ||||
151 | | ||||
152 | QScopedPointer<UpdateModel> m2(new UpdateModel(this)); | ||||
153 | new ModelTest(m2.data(), m2.data()); | ||||
154 | m->setBackend(rum2.data()); | ||||
155 | | ||||
156 | QCOMPARE(rum->isProgressing(), true); | ||||
157 | QVERIFY(spy.wait()); | ||||
158 | QCOMPARE(rum->isProgressing(), false); | ||||
159 | | ||||
160 | QCOMPARE(m_appBackend->updatesCount(), 0); | ||||
161 | QCOMPARE(m->hasUpdates(), false); | ||||
162 | } | ||||
163 | | ||||
164 | private: | ||||
165 | ResourcesModel* m_model; | ||||
166 | AbstractResourcesBackend* m_appBackend; | ||||
167 | }; | ||||
168 | | ||||
169 | QTEST_MAIN(UpdateFwupdTest) | ||||
170 | | ||||
171 | #include "UpdateFwupdTest.moc" |