File indexing completed on 2024-11-10 04:49:58

0001 /*
0002     SPDX-FileCopyrightText: 2013 Christian Mollekopf <mollekopf@kolabsys.com>
0003     SPDX-FileCopyrightText: 2017 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QObject>
0009 
0010 #include <KConfigGroup>
0011 #include <KSharedConfig>
0012 
0013 #include "favoritecollectionorderproxymodel.h"
0014 #include <Akonadi/ChangeRecorder>
0015 #include <Akonadi/Control>
0016 #include <Akonadi/EntityTreeModel>
0017 #include <Akonadi/FavoriteCollectionsModel>
0018 #include <akonadi/qtest_akonadi.h>
0019 
0020 using namespace Akonadi;
0021 using namespace MailCommon;
0022 
0023 class FavoriteProxyTest : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 private Q_SLOTS:
0028     void initTestCase();
0029     void testReordering();
0030 
0031 private:
0032     EntityTreeModel *createETM();
0033 };
0034 
0035 void FavoriteProxyTest::initTestCase()
0036 {
0037     AkonadiTest::checkTestIsIsolated();
0038     Akonadi::Control::start();
0039     AkonadiTest::setAllResourcesOffline();
0040 }
0041 
0042 static QModelIndex getIndex(const QString &string, EntityTreeModel *model)
0043 {
0044     QModelIndexList list = model->match(model->index(0, 0), Qt::DisplayRole, string, 1, Qt::MatchRecursive);
0045     if (list.isEmpty()) {
0046         return {};
0047     }
0048     return list.first();
0049 }
0050 
0051 /**
0052  * Since we have no sensible way to figure out if the model is fully populated,
0053  * we use the brute force approach.
0054  */
0055 static bool waitForPopulation(const QModelIndex &idx, EntityTreeModel *model, int count)
0056 {
0057     for (int i = 0; i < 500; i++) {
0058         if (model->rowCount(idx) >= count) {
0059             return true;
0060         }
0061         QTest::qWait(10);
0062     }
0063     return false;
0064 }
0065 
0066 EntityTreeModel *FavoriteProxyTest::createETM()
0067 {
0068     auto changeRecorder = new ChangeRecorder(this);
0069     changeRecorder->setCollectionMonitored(Collection::root());
0070     auto model = new EntityTreeModel(changeRecorder, this);
0071     model->setItemPopulationStrategy(Akonadi::EntityTreeModel::LazyPopulation);
0072     return model;
0073 }
0074 
0075 static const int numberOfRootCollections = 4;
0076 
0077 void FavoriteProxyTest::testReordering()
0078 {
0079     // GIVEN an ETM, a favorite proxy, and an EntityOrderProxyModel on top
0080     EntityTreeModel *model = createETM();
0081     QVERIFY(waitForPopulation(QModelIndex(), model, numberOfRootCollections));
0082 
0083     QList<Collection::Id> collectionIds;
0084     QStringList labels;
0085     QStringList order;
0086     for (const QString &folderName : {QStringLiteral("res2"), QStringLiteral("res3")}) {
0087         const QModelIndex index = getIndex(folderName, model);
0088         QVERIFY(index.isValid());
0089         const auto favoriteCollection = index.data(EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0090         QVERIFY(favoriteCollection.isValid());
0091         collectionIds.push_back(favoriteCollection.id());
0092         order.push_back(QLatin1Char('c') + QString::number(favoriteCollection.id()));
0093         labels << folderName;
0094     }
0095 
0096     KConfigGroup configGroup(KSharedConfig::openConfig(), QStringLiteral("favoritecollectionsmodeltest"));
0097     configGroup.writeEntry("FavoriteCollectionIds", collectionIds);
0098     configGroup.writeEntry("FavoriteCollectionLabels", labels);
0099     configGroup.writeEntry("0", order);
0100 
0101     auto favoriteModel = new FavoriteCollectionsModel(model, configGroup, this);
0102     QTRY_COMPARE(favoriteModel->rowCount(), 2);
0103 
0104     auto orderProxy = new FavoriteCollectionOrderProxyModel(this);
0105     orderProxy->setOrderConfig(configGroup);
0106     orderProxy->setSourceModel(favoriteModel);
0107     orderProxy->sort(0, Qt::AscendingOrder);
0108 
0109     QCOMPARE(orderProxy->rowCount(), 2);
0110 
0111     const QModelIndex firstRowIndex = orderProxy->index(0, 0);
0112     QVERIFY(firstRowIndex.isValid());
0113     QCOMPARE(firstRowIndex.data().toString(), QStringLiteral("res2"));
0114     // we can drop emails
0115     QVERIFY((orderProxy->flags(firstRowIndex) & Qt::ItemIsDropEnabled) != 0);
0116 }
0117 
0118 #include "favoritestest.moc"
0119 
0120 QTEST_AKONADIMAIN(FavoriteProxyTest)