File indexing completed on 2024-11-10 04:40:18

0001 /*
0002     SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "storage/collectiontreecache.h"
0008 #include "aktest.h"
0009 #include "dbinitializer.h"
0010 #include "fakeakonadiserver.h"
0011 #include "private/scope_p.h"
0012 #include "storage/selectquerybuilder.h"
0013 
0014 #include <QObject>
0015 #include <QSignalSpy>
0016 #include <QTest>
0017 
0018 using namespace Akonadi;
0019 using namespace Akonadi::Server;
0020 
0021 class InspectableCollectionTreeCache : public CollectionTreeCache
0022 {
0023     Q_OBJECT
0024 public:
0025     InspectableCollectionTreeCache()
0026         : CollectionTreeCache()
0027         , mCachePopulated(0)
0028     {
0029     }
0030 
0031     bool waitForCachePopulated()
0032     {
0033         QSignalSpy spy(this, &InspectableCollectionTreeCache::cachePopulated);
0034         return mCachePopulated == 1 || spy.wait(5000);
0035     }
0036 
0037 Q_SIGNALS:
0038     void cachePopulated();
0039 
0040 protected:
0041     void init() override
0042     {
0043         CollectionTreeCache::init();
0044         mCachePopulated = 1;
0045         Q_EMIT cachePopulated();
0046     }
0047 
0048     void quit() override
0049     {
0050     }
0051 
0052 private:
0053     QAtomicInt mCachePopulated;
0054 };
0055 
0056 class CollectionTreeCacheTest : public QObject
0057 {
0058     Q_OBJECT
0059 
0060 public:
0061     CollectionTreeCacheTest()
0062     {
0063         try {
0064             FakeAkonadiServer::instance()->init();
0065         } catch (const FakeAkonadiServerException &e) {
0066             qWarning() << "Server exception: " << e.what();
0067             qFatal("Fake Akonadi Server failed to start up, aborting test");
0068         }
0069     }
0070 
0071     ~CollectionTreeCacheTest()
0072     {
0073         FakeAkonadiServer::instance()->quit();
0074     }
0075 
0076 private:
0077     void populateDb(DbInitializer &db)
0078     {
0079         // ResA
0080         //  |- Col A1
0081         //  |- Col A2
0082         //  |   |- Col A3
0083         //  |   |- Col A7
0084         //  |       |- Col A5
0085         //  |       |- Col A8
0086         //  |- Col A6
0087         //  |   |- Col A10
0088         //  |- Col A9
0089         auto res = db.createResource("TestResource");
0090         auto resA = db.createCollection("Res A", Collection());
0091         auto colA1 = db.createCollection("Col A1", resA);
0092         auto colA2 = db.createCollection("Col A2", resA);
0093         auto colA3 = db.createCollection("Col A3", colA2);
0094         auto colA5 = db.createCollection("Col A5", colA2);
0095         auto colA6 = db.createCollection("Col A6", resA);
0096         auto colA7 = db.createCollection("Col A7", colA2);
0097         auto colA8 = db.createCollection("Col A8", colA7);
0098         auto colA9 = db.createCollection("Col A9", resA);
0099         auto colA10 = db.createCollection("Col A10", colA6);
0100 
0101         // Move the collection to the final parent
0102         colA5.setParent(colA7);
0103         colA5.update();
0104     }
0105 
0106 private Q_SLOTS:
0107     void populateTest()
0108     {
0109         DbInitializer db;
0110         populateDb(db);
0111 
0112         InspectableCollectionTreeCache treeCache;
0113         QVERIFY(treeCache.waitForCachePopulated());
0114 
0115         auto allCols = treeCache.retrieveCollections(Scope(), std::numeric_limits<int>::max(), 1);
0116 
0117         SelectQueryBuilder<Collection> qb;
0118         QVERIFY(qb.exec());
0119         auto expCols = qb.result();
0120 
0121         const auto sort = [](const Collection &l, const Collection &r) {
0122             return l.id() < r.id();
0123         };
0124         std::sort(allCols.begin(), allCols.end(), sort);
0125         std::sort(expCols.begin(), expCols.end(), sort);
0126 
0127         QCOMPARE(allCols.size(), expCols.size());
0128         QCOMPARE(allCols, expCols);
0129     }
0130 };
0131 
0132 AKTEST_FAKESERVER_MAIN(CollectionTreeCacheTest)
0133 
0134 #include "collectiontreecachetest.moc"