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 "DiscoverBackendsFactory.h"
0009 #include <ApplicationAddonsModel.h>
0010 #include <Category/CategoryModel.h>
0011 #include <QAbstractItemModelTester>
0012 #include <ReviewsBackend/ReviewsModel.h>
0013 #include <ScreenshotsModel.h>
0014 #include <Transaction/TransactionModel.h>
0015 #include <UpdateModel/UpdateModel.h>
0016 #include <resources/AbstractBackendUpdater.h>
0017 #include <resources/ResourcesModel.h>
0018 #include <resources/ResourcesProxyModel.h>
0019 #include <resources/ResourcesUpdatesModel.h>
0020 
0021 #include <QSignalSpy>
0022 #include <QTest>
0023 
0024 QTEST_MAIN(DummyTest)
0025 
0026 AbstractResourcesBackend *backendByName(ResourcesModel *m, const QString &name)
0027 {
0028     const QVector<AbstractResourcesBackend *> backends = m->backends();
0029     for (AbstractResourcesBackend *backend : backends) {
0030         if (QString::fromLatin1(backend->metaObject()->className()) == name) {
0031             return backend;
0032         }
0033     }
0034     return nullptr;
0035 }
0036 
0037 DummyTest::DummyTest(QObject *parent)
0038     : QObject(parent)
0039 {
0040     DiscoverBackendsFactory::setRequestedBackends({QStringLiteral("dummy-backend")});
0041 
0042     m_model = new ResourcesModel(QStringLiteral("dummy-backend"), this);
0043     m_appBackend = backendByName(m_model, QStringLiteral("DummyBackend"));
0044 
0045     CategoryModel::global()->populateCategories();
0046 }
0047 
0048 void DummyTest::initTestCase()
0049 {
0050     QVERIFY(m_appBackend);
0051     while (m_appBackend->isFetching()) {
0052         QSignalSpy spy(m_appBackend, &AbstractResourcesBackend::fetchingChanged);
0053         QVERIFY(spy.wait());
0054     }
0055 }
0056 
0057 QVector<StreamResult> fetchResources(ResultsStream *stream)
0058 {
0059     QVector<StreamResult> ret;
0060     QObject::connect(stream, &ResultsStream::resourcesFound, stream, [&ret](const QVector<StreamResult> &res) {
0061         ret += res;
0062     });
0063     QSignalSpy spy(stream, &ResultsStream::destroyed);
0064     Q_ASSERT(spy.wait());
0065     return ret;
0066 }
0067 
0068 void DummyTest::testReadData()
0069 {
0070     const auto resources = fetchResources(m_appBackend->search({}));
0071 
0072     QCOMPARE(m_appBackend->property("startElements").toInt() * 2, resources.size());
0073     QBENCHMARK {
0074         for (StreamResult res : resources) {
0075             QVERIFY(!res.resource->name().isEmpty());
0076         }
0077     }
0078 }
0079 
0080 void DummyTest::testProxy()
0081 {
0082     ResourcesProxyModel pm;
0083     QSignalSpy spy(&pm, &ResourcesProxyModel::busyChanged);
0084     // QVERIFY(spy.wait());
0085     QVERIFY(!pm.isBusy());
0086 
0087     pm.setFiltersFromCategory(CategoryModel::global()->rootCategories().first());
0088     pm.componentComplete();
0089     QVERIFY(pm.isBusy());
0090     QVERIFY(spy.wait());
0091     QVERIFY(!pm.isBusy());
0092 
0093     QCOMPARE(pm.rowCount(), m_appBackend->property("startElements").toInt() * 2);
0094     pm.setSearch(QStringLiteral("techie"));
0095     QVERIFY(pm.isBusy());
0096     QVERIFY(spy.wait());
0097     QVERIFY(!pm.isBusy());
0098     QCOMPARE(0, pm.rowCount());
0099     QCOMPARE(pm.subcategories().count(), 7);
0100     pm.setSearch(QString());
0101     QVERIFY(pm.isBusy());
0102     QVERIFY(spy.wait());
0103     QVERIFY(!pm.isBusy());
0104     QCOMPARE(pm.rowCount(), m_appBackend->property("startElements").toInt() * 2);
0105 }
0106 
0107 void DummyTest::testProxySorting()
0108 {
0109     ResourcesProxyModel pm;
0110     QSignalSpy spy(&pm, &ResourcesProxyModel::busyChanged);
0111     // QVERIFY(spy.wait());
0112     QVERIFY(!pm.isBusy());
0113 
0114     pm.setFiltersFromCategory(CategoryModel::global()->rootCategories().first());
0115     pm.setSortOrder(Qt::DescendingOrder);
0116     pm.setSortRole(ResourcesProxyModel::RatingCountRole);
0117     pm.componentComplete();
0118     QVERIFY(pm.isBusy());
0119     QVERIFY(spy.wait());
0120     QVERIFY(!pm.isBusy());
0121 
0122     QCOMPARE(m_appBackend->property("startElements").toInt() * 2, pm.rowCount());
0123     QVariant lastRatingCount;
0124     for (int i = 0, rc = pm.rowCount(); i < rc; ++i) {
0125         const QModelIndex mi = pm.index(i, 0);
0126 
0127         const auto value = mi.data(pm.sortRole());
0128         QVERIFY(i == 0 || QVariant::compare(value, lastRatingCount) == QPartialOrdering::Less
0129                 || QVariant::compare(value, lastRatingCount) == QPartialOrdering::Equivalent);
0130         lastRatingCount = value;
0131     }
0132 }
0133 
0134 void DummyTest::testFetch()
0135 {
0136     const auto resources = fetchResources(m_appBackend->search({}));
0137     QCOMPARE(m_appBackend->property("startElements").toInt() * 2, resources.count());
0138 
0139     // fetches updates, adds new things
0140     m_appBackend->checkForUpdates();
0141     QSignalSpy spy(m_model, &ResourcesModel::allInitialized);
0142     QVERIFY(spy.wait(80000));
0143     auto resources2 = fetchResources(m_appBackend->search({}));
0144     QCOMPARE(m_appBackend->property("startElements").toInt() * 4, resources2.count());
0145 }
0146 
0147 void DummyTest::testSort()
0148 {
0149     ResourcesProxyModel pm;
0150 
0151     QCollator c;
0152     QBENCHMARK_ONCE {
0153         pm.setSortRole(ResourcesProxyModel::NameRole);
0154         pm.sort(0);
0155         QCOMPARE(pm.sortOrder(), Qt::AscendingOrder);
0156         QString last;
0157         for (int i = 0, count = pm.rowCount(); i < count; ++i) {
0158             const QString current = pm.index(i, 0).data(pm.sortRole()).toString();
0159             if (!last.isEmpty()) {
0160                 QCOMPARE(c.compare(last, current), -1);
0161             }
0162             last = current;
0163         }
0164     }
0165 
0166     QBENCHMARK_ONCE {
0167         pm.setSortRole(ResourcesProxyModel::SortableRatingRole);
0168         int last = -1;
0169         for (int i = 0, count = pm.rowCount(); i < count; ++i) {
0170             const int current = pm.index(i, 0).data(pm.sortRole()).toInt();
0171             QVERIFY(last <= current);
0172             last = current;
0173         }
0174     }
0175 }
0176 
0177 void DummyTest::testInstallAddons()
0178 {
0179     AbstractResourcesBackend::Filters filter;
0180     filter.resourceUrl = QUrl(QStringLiteral("dummy://Dummy.1"));
0181 
0182     const auto resources = fetchResources(m_appBackend->search(filter));
0183     QCOMPARE(resources.count(), 1);
0184     AbstractResource *res = resources.first().resource;
0185     QVERIFY(res);
0186 
0187     ApplicationAddonsModel m;
0188     new QAbstractItemModelTester(&m, &m);
0189     m.setApplication(res);
0190     QCOMPARE(m.rowCount(), res->addonsInformation().count());
0191     QCOMPARE(res->addonsInformation().at(0).isInstalled(), false);
0192 
0193     QString firstAddonName = m.data(m.index(0, 0)).toString();
0194     m.changeState(firstAddonName, true);
0195     QVERIFY(m.hasChanges());
0196 
0197     m.applyChanges();
0198     QSignalSpy sR(TransactionModel::global(), &TransactionModel::transactionRemoved);
0199     QVERIFY(sR.wait());
0200     QVERIFY(!m.hasChanges());
0201 
0202     QCOMPARE(m.data(m.index(0, 0)).toString(), firstAddonName);
0203     QCOMPARE(res->addonsInformation().at(0).name(), firstAddonName);
0204     QCOMPARE(res->addonsInformation().at(0).isInstalled(), true);
0205 
0206     m.changeState(m.data(m.index(1, 0)).toString(), true);
0207     QVERIFY(m.hasChanges());
0208     for (int i = 0, c = m.rowCount(); i < c; ++i) {
0209         const auto idx = m.index(i, 0);
0210         QCOMPARE(idx.data(Qt::CheckStateRole).toInt(), int(i <= 1 ? Qt::Checked : Qt::Unchecked));
0211         QVERIFY(!idx.data(ApplicationAddonsModel::PackageNameRole).toString().isEmpty());
0212     }
0213     m.discardChanges();
0214     QVERIFY(!m.hasChanges());
0215 }
0216 
0217 void DummyTest::testReviewsModel()
0218 {
0219     AbstractResourcesBackend::Filters filter;
0220     filter.resourceUrl = QUrl(QStringLiteral("dummy://Dummy.1"));
0221 
0222     const auto resources = fetchResources(m_appBackend->search(filter));
0223     QCOMPARE(resources.count(), 1);
0224     AbstractResource *res = resources.first().resource;
0225     QVERIFY(res);
0226 
0227     ReviewsModel m;
0228     new QAbstractItemModelTester(&m, &m);
0229     m.setResource(res);
0230     m.fetchMore();
0231 
0232     QVERIFY(m.rowCount() > 0);
0233 
0234     QCOMPARE(ReviewsModel::UserChoice(m.data(m.index(0, 0), ReviewsModel::UsefulChoice).toInt()), ReviewsModel::None);
0235     m.markUseful(0, true);
0236     QCOMPARE(ReviewsModel::UserChoice(m.data(m.index(0, 0), ReviewsModel::UsefulChoice).toInt()), ReviewsModel::Yes);
0237     m.markUseful(0, false);
0238     QCOMPARE(ReviewsModel::UserChoice(m.data(m.index(0, 0), ReviewsModel::UsefulChoice).toInt()), ReviewsModel::No);
0239 
0240     const auto resources2 = fetchResources(m_appBackend->search(filter));
0241     QCOMPARE(resources2.count(), 1);
0242     res = resources2.first().resource;
0243     m.setResource(res);
0244     m.fetchMore();
0245 
0246     QSignalSpy spy(&m, &ReviewsModel::rowsChanged);
0247     QVERIFY(m.rowCount() > 0);
0248 }
0249 
0250 void DummyTest::testUpdateModel()
0251 {
0252     const auto backend = m_model->backends().first();
0253 
0254     ResourcesUpdatesModel ruModel;
0255     new QAbstractItemModelTester(&ruModel, &ruModel);
0256     UpdateModel model;
0257     new QAbstractItemModelTester(&model, &model);
0258     model.setBackend(&ruModel);
0259 
0260     QCOMPARE(model.rowCount(), backend->property("startElements").toInt() * 2);
0261     QCOMPARE(model.hasUpdates(), true);
0262 }
0263 
0264 void DummyTest::testScreenshotsModel()
0265 {
0266     AbstractResourcesBackend::Filters filter;
0267     filter.resourceUrl = QUrl(QStringLiteral("dummy://Dummy.1"));
0268 
0269     ScreenshotsModel m;
0270     new QAbstractItemModelTester(&m, &m);
0271 
0272     const auto resources = fetchResources(m_appBackend->search(filter));
0273     QCOMPARE(resources.count(), 1);
0274     AbstractResource *res = resources.first().resource;
0275     QVERIFY(res);
0276     m.setResource(res);
0277     QCOMPARE(res, m.resource());
0278 
0279     int c = m.rowCount();
0280     for (int i = 0; i < c; ++i) {
0281         const auto idx = m.index(i, 0);
0282         QVERIFY(!idx.data(ScreenshotsModel::ThumbnailUrl).isNull());
0283         QVERIFY(!idx.data(ScreenshotsModel::ScreenshotUrl).isNull());
0284     }
0285 }
0286 
0287 // TODO test cancel transaction