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

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  */
0007 
0008 #include "aktest.h"
0009 #include "fakeakonadiserver.h"
0010 #include "handler/searchhelper.h"
0011 
0012 #include <entities.h>
0013 
0014 #include <QTest>
0015 
0016 using namespace Akonadi::Server;
0017 
0018 Q_DECLARE_METATYPE(QList<qint64>)
0019 Q_DECLARE_METATYPE(QList<QString>)
0020 
0021 class SearchTest : public QObject
0022 {
0023     Q_OBJECT
0024 
0025     FakeAkonadiServer mAkonadi;
0026 
0027 public:
0028     SearchTest()
0029         : QObject()
0030     {
0031         mAkonadi.setPopulateDb(false);
0032         mAkonadi.init();
0033     }
0034 
0035     Collection createCollection(const Resource &res, const QString &name, const Collection &parent, const QStringList &mimetypes)
0036     {
0037         Collection col;
0038         col.setName(name);
0039         col.setResource(res);
0040         col.setParentId(parent.isValid() ? parent.id() : 0);
0041         col.insert();
0042         for (const QString &mimeType : mimetypes) {
0043             MimeType mt = MimeType::retrieveByName(mimeType);
0044             if (!mt.isValid()) {
0045                 mt = MimeType(mimeType);
0046                 mt.insert();
0047             }
0048             col.addMimeType(mt);
0049         }
0050         return col;
0051     }
0052 
0053 private Q_SLOTS:
0054     void testSearchHelperCollectionListing_data()
0055     {
0056         /*
0057         Fake Resource
0058           |- Col 1 (inode/directory)
0059           |  |- Col 2 (inode/directory, application/octet-stream)
0060           |  |  |- Col 3(application/octet-stream)
0061           |  |- Col 4 (text/plain)
0062           |- Col 5 (inode/directory, text/plain)
0063              |- Col 6 (inode/directory, application/octet-stream)
0064              |- Col 7 (inode/directory, text/plain)
0065                  |- Col 8 (inode/directory, application/octet-stream)
0066                     |- Col 9 (unique/mime-type)
0067         */
0068 
0069         Resource res(QStringLiteral("Test Resource"), false);
0070         res.insert();
0071 
0072         Collection col1 = createCollection(res, QStringLiteral("Col 1"), Collection(), QStringList() << QStringLiteral("inode/directory"));
0073         Collection col2 = createCollection(res,
0074                                            QStringLiteral("Col 2"),
0075                                            col1,
0076                                            QStringList() << QStringLiteral("inode/directory") << QStringLiteral("application/octet-stream"));
0077         Collection col3 = createCollection(res, QStringLiteral("Col 3"), col2, QStringList() << QStringLiteral("application/octet-stream"));
0078         Collection col4 = createCollection(res, QStringLiteral("Col 4"), col2, QStringList() << QStringLiteral("text/plain"));
0079         Collection col5 =
0080             createCollection(res, QStringLiteral("Col 5"), Collection(), QStringList() << QStringLiteral("inode/directory") << QStringLiteral("text/plain"));
0081         Collection col6 = createCollection(res,
0082                                            QStringLiteral("Col 6"),
0083                                            col5,
0084                                            QStringList() << QStringLiteral("inode/directory") << QStringLiteral("application/octet-stream"));
0085         Collection col7 =
0086             createCollection(res, QStringLiteral("Col 7"), col5, QStringList() << QStringLiteral("inode/directory") << QStringLiteral("text/plain"));
0087         Collection col8 = createCollection(res,
0088                                            QStringLiteral("Col 8"),
0089                                            col7,
0090                                            QStringList() << QStringLiteral("text/directory") << QStringLiteral("application/octet-stream"));
0091         Collection col9 = createCollection(res, QStringLiteral("Col 9"), col8, QStringList() << QStringLiteral("unique/mime-type"));
0092 
0093         QTest::addColumn<QList<qint64>>("ancestors");
0094         QTest::addColumn<QStringList>("mimetypes");
0095         QTest::addColumn<QList<qint64>>("expectedResults");
0096 
0097         QTest::newRow("") << (QList<qint64>() << 0) << (QStringList() << QStringLiteral("text/plain"))
0098                           << (QList<qint64>() << col4.id() << col5.id() << col7.id());
0099         QTest::newRow("") << (QList<qint64>() << 0) << (QStringList() << QStringLiteral("application/octet-stream"))
0100                           << (QList<qint64>() << col2.id() << col3.id() << col6.id() << col8.id());
0101         QTest::newRow("") << (QList<qint64>() << col1.id()) << (QStringList() << QStringLiteral("text/plain")) << (QList<qint64>() << col4.id());
0102         QTest::newRow("") << (QList<qint64>() << col1.id()) << (QStringList() << QStringLiteral("unique/mime-type")) << QList<qint64>();
0103         QTest::newRow("") << (QList<qint64>() << col2.id() << col7.id()) << (QStringList() << QStringLiteral("application/octet-stream"))
0104                           << (QList<qint64>() << col3.id() << col8.id());
0105     }
0106 
0107     void testSearchHelperCollectionListing()
0108     {
0109         QFETCH(QList<qint64>, ancestors);
0110         QFETCH(QStringList, mimetypes);
0111         QFETCH(QList<qint64>, expectedResults);
0112 
0113         QList<qint64> results = SearchHelper::matchSubcollectionsByMimeType(ancestors, mimetypes);
0114 
0115         std::sort(expectedResults.begin(), expectedResults.end());
0116         std::sort(results.begin(), results.end());
0117 
0118         QCOMPARE(results.size(), expectedResults.size());
0119         QCOMPARE(results, expectedResults);
0120     }
0121 };
0122 
0123 AKTEST_FAKESERVER_MAIN(SearchTest)
0124 
0125 #include "searchtest.moc"