File indexing completed on 2024-11-17 04:55:40
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 "KNSBackendTest.h" 0008 #include "utils.h" 0009 #include <Category/Category.h> 0010 #include <Category/CategoryModel.h> 0011 #include <DiscoverBackendsFactory.h> 0012 #include <KNSBackend.h> 0013 #include <QStandardPaths> 0014 #include <ReviewsBackend/AbstractReviewsBackend.h> 0015 #include <ReviewsBackend/Rating.h> 0016 #include <ReviewsBackend/Review.h> 0017 #include <resources/AbstractBackendUpdater.h> 0018 #include <resources/AbstractResource.h> 0019 #include <resources/ResourcesModel.h> 0020 0021 #include <QSignalSpy> 0022 #include <QTest> 0023 0024 QTEST_MAIN(KNSBackendTest) 0025 0026 static QString s_knsrcName = QStringLiteral("ksplash.knsrc"); 0027 0028 KNSBackendTest::KNSBackendTest(QObject *parent) 0029 : QObject(parent) 0030 , m_r(nullptr) 0031 { 0032 QStandardPaths::setTestModeEnabled(true); 0033 QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).removeRecursively(); 0034 0035 ResourcesModel *model = new ResourcesModel(QStringLiteral("kns-backend"), this); 0036 Q_ASSERT(!model->backends().isEmpty()); 0037 auto findTestBackend = [](AbstractResourcesBackend *backend) { 0038 return backend->name() == QLatin1String("ksplash.knsrc"); 0039 }; 0040 m_backend = kFilter<QVector<AbstractResourcesBackend *>>(model->backends(), findTestBackend).value(0); 0041 0042 if (!m_backend || !m_backend->isValid()) { 0043 qWarning() << "couldn't run the test"; 0044 exit(0); 0045 } 0046 0047 connect(m_backend->reviewsBackend(), &AbstractReviewsBackend::reviewsReady, this, &KNSBackendTest::reviewsArrived); 0048 } 0049 0050 QVector<AbstractResource *> KNSBackendTest::getResources(ResultsStream *stream, bool canBeEmpty) 0051 { 0052 Q_ASSERT(stream); 0053 Q_ASSERT(stream->objectName() != QLatin1String("KNS-void")); 0054 QSignalSpy spyResources(stream, &ResultsStream::destroyed); 0055 QVector<AbstractResource *> resources; 0056 connect(stream, &ResultsStream::resourcesFound, this, [&resources, stream](const QVector<StreamResult> &res) { 0057 resources += kTransform<QVector<AbstractResource *>>(res, [](auto result) { 0058 return result.resource; 0059 }); 0060 Q_EMIT stream->fetchMore(); 0061 }); 0062 bool waited = spyResources.wait(10000); 0063 if (!waited) { 0064 if (auto x = qobject_cast<AggregatedResultsStream *>(stream)) 0065 qDebug() << "waited" << x->streams(); 0066 } 0067 Q_ASSERT(waited); 0068 Q_ASSERT(!resources.isEmpty() || canBeEmpty); 0069 return resources; 0070 } 0071 0072 QVector<AbstractResource *> KNSBackendTest::getAllResources(AbstractResourcesBackend *backend) 0073 { 0074 AbstractResourcesBackend::Filters f; 0075 if (CategoryModel::global()->rootCategories().isEmpty()) 0076 CategoryModel::global()->populateCategories(); 0077 f.category = CategoryModel::global()->rootCategories().constFirst(); 0078 return getResources(backend->search(f)); 0079 } 0080 0081 void KNSBackendTest::testRetrieval() 0082 { 0083 QVERIFY(m_backend->backendUpdater()); 0084 QCOMPARE(m_backend->updatesCount(), m_backend->backendUpdater()->toUpdate().count()); 0085 0086 QSignalSpy spy(m_backend, &AbstractResourcesBackend::fetchingChanged); 0087 QVERIFY(!m_backend->isFetching() || spy.wait()); 0088 0089 const auto resources = getAllResources(m_backend); 0090 for (AbstractResource *res : resources) { 0091 QVERIFY(!res->name().isEmpty()); 0092 QVERIFY(!res->categories().isEmpty()); 0093 QVERIFY(!res->origin().isEmpty()); 0094 QVERIFY(!res->icon().isNull()); 0095 // QVERIFY(!res->comment().isEmpty()); 0096 // QVERIFY(!res->longDescription().isEmpty()); 0097 // QVERIFY(!res->license().isEmpty()); 0098 QVERIFY(res->homepage().isValid() && !res->homepage().isEmpty()); 0099 QVERIFY(res->state() > AbstractResource::Broken); 0100 QVERIFY(res->addonsInformation().isEmpty()); 0101 0102 QSignalSpy spy(res, &AbstractResource::screenshotsFetched); 0103 res->fetchScreenshots(); 0104 QVERIFY(spy.count() || spy.wait()); 0105 0106 QSignalSpy spy1(res, &AbstractResource::changelogFetched); 0107 res->fetchChangelog(); 0108 QVERIFY(spy1.count() || spy1.wait()); 0109 } 0110 } 0111 0112 void KNSBackendTest::testReviews() 0113 { 0114 const QVector<AbstractResource *> resources = getAllResources(m_backend); 0115 AbstractReviewsBackend *rev = m_backend->reviewsBackend(); 0116 QVERIFY(!rev->hasCredentials()); 0117 for (AbstractResource *res : resources) { 0118 Rating *r = rev->ratingForApplication(res); 0119 QVERIFY(r); 0120 QCOMPARE(r->packageName(), res->packageName()); 0121 QVERIFY(r->rating() > 0 && r->rating() <= 10); 0122 } 0123 0124 auto res = resources.first(); 0125 QSignalSpy spy(rev, &AbstractReviewsBackend::reviewsReady); 0126 rev->fetchReviews(res); 0127 QVERIFY(spy.count() || spy.wait()); 0128 } 0129 0130 void KNSBackendTest::reviewsArrived(AbstractResource *r, const QVector<ReviewPtr> &revs) 0131 { 0132 m_r = r; 0133 m_revs = revs; 0134 } 0135 0136 void KNSBackendTest::testResourceByUrl() 0137 { 0138 AbstractResourcesBackend::Filters f; 0139 f.resourceUrl = QUrl(QLatin1String("kns://") + m_backend->name() + QLatin1String("/api.kde-look.org/1136471")); 0140 const QVector<AbstractResource *> resources = getResources(m_backend->search(f)); 0141 const QVector<QUrl> res = kTransform<QVector<QUrl>>(resources, [](AbstractResource *res) { 0142 return res->url(); 0143 }); 0144 QCOMPARE(res.count(), 1); 0145 QCOMPARE(f.resourceUrl, res.constFirst()); 0146 0147 auto resource = resources.constFirst(); 0148 QVERIFY(!resource->isInstalled()); // Make sure .qttest is clean before running the test 0149 0150 QSignalSpy spy(resource, &AbstractResource::stateChanged); 0151 auto b = resource->backend(); 0152 b->installApplication(resource); 0153 QVERIFY(spy.wait()); 0154 b->removeApplication(resource); 0155 QVERIFY(spy.wait()); 0156 QCOMPARE(spy.count(), 2); 0157 QVERIFY(!resource->isInstalled()); 0158 } 0159 0160 void KNSBackendTest::testResourceByUrlResourcesModel() 0161 { 0162 AbstractResourcesBackend::Filters filter; 0163 filter.resourceUrl = QUrl(QStringLiteral("kns://plasmoids.knsrc/store.kde.org/1169537")); // Wrong domain 0164 0165 auto resources = getResources(ResourcesModel::global()->search(filter), true); 0166 const QVector<QUrl> res = kTransform<QVector<QUrl>>(resources, [](AbstractResource *res) { 0167 return res->url(); 0168 }); 0169 QCOMPARE(res.count(), 0); 0170 } 0171 0172 #include "moc_KNSBackendTest.cpp"