File indexing completed on 2024-11-17 04:55:36
0001 /* 0002 * SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "DummyTest.h" 0008 #include <ApplicationAddonsModel.h> 0009 #include <KFormat> 0010 #include <QAbstractItemModelTester> 0011 #include <ReviewsBackend/ReviewsModel.h> 0012 #include <Transaction/TransactionModel.h> 0013 #include <UpdateModel/UpdateModel.h> 0014 #include <resources/AbstractBackendUpdater.h> 0015 #include <resources/ResourcesModel.h> 0016 #include <resources/ResourcesProxyModel.h> 0017 #include <resources/ResourcesUpdatesModel.h> 0018 0019 #include <QSignalSpy> 0020 #include <QTest> 0021 0022 class UpdateDummyTest : public QObject 0023 { 0024 Q_OBJECT 0025 public: 0026 AbstractResourcesBackend *backendByName(ResourcesModel *m, const QString &name) 0027 { 0028 const QVector<AbstractResourcesBackend *> backends = m->backends(); 0029 for (AbstractResourcesBackend *backend : backends) { 0030 if (QLatin1String(backend->metaObject()->className()) == name) { 0031 return backend; 0032 } 0033 } 0034 return nullptr; 0035 } 0036 0037 UpdateDummyTest(QObject *parent = nullptr) 0038 : QObject(parent) 0039 { 0040 m_model = new ResourcesModel(QStringLiteral("dummy-backend"), this); 0041 m_appBackend = backendByName(m_model, QStringLiteral("DummyBackend")); 0042 } 0043 0044 private Q_SLOTS: 0045 void init() 0046 { 0047 QVERIFY(m_appBackend); 0048 while (m_appBackend->isFetching()) { 0049 QSignalSpy spy(m_appBackend, &AbstractResourcesBackend::fetchingChanged); 0050 QVERIFY(spy.wait()); 0051 } 0052 } 0053 0054 void testInformation() 0055 { 0056 ResourcesUpdatesModel *rum = new ResourcesUpdatesModel(this); 0057 new QAbstractItemModelTester(rum, rum); 0058 0059 UpdateModel *m = new UpdateModel(this); 0060 new QAbstractItemModelTester(m, m); 0061 m->setBackend(rum); 0062 0063 rum->prepare(); 0064 QSignalSpy spySetup(m_appBackend->backendUpdater(), &AbstractBackendUpdater::progressingChanged); 0065 QVERIFY(!m_appBackend->backendUpdater()->isProgressing() || spySetup.wait()); 0066 QCOMPARE(m_appBackend->updatesCount(), m_appBackend->property("startElements").toInt()); 0067 QCOMPARE(m->hasUpdates(), true); 0068 0069 QCOMPARE(m->index(0, 0).data(UpdateModel::ChangelogRole).toString(), QString{}); 0070 0071 QSignalSpy spy(m, &QAbstractItemModel::dataChanged); 0072 m->fetchUpdateDetails(0); 0073 QVERIFY(spy.count() || spy.wait()); 0074 QCOMPARE(spy.count(), 1); 0075 delete m; 0076 } 0077 0078 void testUpdate() 0079 { 0080 ResourcesUpdatesModel *rum = new ResourcesUpdatesModel(this); 0081 new QAbstractItemModelTester(rum, rum); 0082 0083 UpdateModel *m = new UpdateModel(this); 0084 new QAbstractItemModelTester(m, m); 0085 m->setBackend(rum); 0086 0087 rum->prepare(); 0088 QSignalSpy spySetup(m_appBackend->backendUpdater(), &AbstractBackendUpdater::progressingChanged); 0089 QVERIFY(!m_appBackend->backendUpdater()->isProgressing() || spySetup.wait()); 0090 QCOMPARE(m_appBackend->updatesCount(), m_appBackend->property("startElements").toInt()); 0091 QCOMPARE(m->hasUpdates(), true); 0092 0093 for (int i = 0, c = m->rowCount(); i < c; ++i) { 0094 const QModelIndex resourceIdx = m->index(i, 0); 0095 QVERIFY(resourceIdx.isValid()); 0096 0097 AbstractResource *res = qobject_cast<AbstractResource *>(resourceIdx.data(UpdateModel::ResourceRole).value<QObject *>()); 0098 QVERIFY(res); 0099 0100 QCOMPARE(Qt::CheckState(resourceIdx.data(Qt::CheckStateRole).toInt()), Qt::Checked); 0101 QVERIFY(m->setData(resourceIdx, int(Qt::Unchecked), Qt::CheckStateRole)); 0102 QCOMPARE(Qt::CheckState(resourceIdx.data(Qt::CheckStateRole).toInt()), Qt::Unchecked); 0103 QCOMPARE(resourceIdx.data(Qt::DisplayRole).toString(), res->name()); 0104 0105 if (i != 0) { 0106 QVERIFY(m->setData(resourceIdx, int(Qt::Checked), Qt::CheckStateRole)); 0107 } 0108 } 0109 0110 QSignalSpy spy(rum, &ResourcesUpdatesModel::progressingChanged); 0111 QVERIFY(!rum->isProgressing() || spy.wait()); 0112 QCOMPARE(rum->isProgressing(), false); 0113 0114 QCOMPARE(m_appBackend->updatesCount(), m->rowCount()); 0115 QCOMPARE(m->hasUpdates(), true); 0116 0117 rum->prepare(); 0118 0119 spy.clear(); 0120 QCOMPARE(rum->isProgressing(), false); 0121 rum->updateAll(); 0122 QVERIFY(spy.count() || spy.wait()); 0123 QCOMPARE(rum->isProgressing(), true); 0124 0125 QCOMPARE(TransactionModel::global()->rowCount(), 1); 0126 connect(TransactionModel::global(), &TransactionModel::progressChanged, this, []() { 0127 const int progress = TransactionModel::global()->progress(); 0128 static int lastProgress = -1; 0129 Q_ASSERT(progress >= lastProgress || (TransactionModel::global()->rowCount() == 0 && progress == 0)); 0130 lastProgress = progress; 0131 }); 0132 0133 QTest::qWait(20); 0134 QScopedPointer<ResourcesUpdatesModel> rum2(new ResourcesUpdatesModel(this)); 0135 new QAbstractItemModelTester(rum2.data(), rum2.data()); 0136 0137 QScopedPointer<UpdateModel> m2(new UpdateModel(this)); 0138 new QAbstractItemModelTester(m2.data(), m2.data()); 0139 m->setBackend(rum2.data()); 0140 0141 QCOMPARE(rum->isProgressing(), true); 0142 QVERIFY(spy.wait()); 0143 QCOMPARE(rum->isProgressing(), false); 0144 0145 QCOMPARE(m_appBackend->updatesCount(), 0); 0146 QCOMPARE(m->hasUpdates(), false); 0147 } 0148 0149 private: 0150 ResourcesModel *m_model; 0151 AbstractResourcesBackend *m_appBackend; 0152 }; 0153 0154 QTEST_MAIN(UpdateDummyTest) 0155 0156 #include "UpdateDummyTest.moc"