File indexing completed on 2024-05-26 05:14:38

0001 /*
0002     SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "akapplication.h"
0011 #include "private/instance_p.h"
0012 
0013 #include <KCrash>
0014 
0015 #include <QTest>
0016 
0017 #define AKTEST_MAIN(TestObject)                                                                                                                                \
0018     int main(int argc, char **argv)                                                                                                                            \
0019     {                                                                                                                                                          \
0020         qputenv("XDG_DATA_HOME", ".local-unit-test/share");                                                                                                    \
0021         qputenv("XDG_CONFIG_HOME", ".config-unit-test");                                                                                                       \
0022         AkCoreApplication app(argc, argv);                                                                                                                     \
0023         KCrash::setDrKonqiEnabled(false);                                                                                                                      \
0024         app.parseCommandLine();                                                                                                                                \
0025         TestObject tc;                                                                                                                                         \
0026         return QTest::qExec(&tc, argc, argv);                                                                                                                  \
0027     }
0028 
0029 #define AKTEST_FAKESERVER_MAIN(TestObject)                                                                                                                     \
0030     int main(int argc, char **argv)                                                                                                                            \
0031     {                                                                                                                                                          \
0032         AkCoreApplication app(argc, argv);                                                                                                                     \
0033         KCrash::setDrKonqiEnabled(false);                                                                                                                      \
0034         app.addCommandLineOptions(QCommandLineOption(QStringLiteral("no-cleanup"), QStringLiteral("Don't clean up the temporary runtime environment")));       \
0035         app.parseCommandLine();                                                                                                                                \
0036         TestObject tc;                                                                                                                                         \
0037         return QTest::qExec(&tc, argc, argv);                                                                                                                  \
0038     }
0039 
0040 #define AKCOMPARE(actual, expected)                                                                                                                            \
0041     do {                                                                                                                                                       \
0042         if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))                                                                        \
0043             return false;                                                                                                                                      \
0044     } while (false)
0045 
0046 #define AKVERIFY(statement)                                                                                                                                    \
0047     do {                                                                                                                                                       \
0048         if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))                                                                                  \
0049             return false;                                                                                                                                      \
0050     } while (false)
0051 
0052 inline void akTestSetInstanceIdentifier(const QString &instanceId)
0053 {
0054     Akonadi::Instance::setIdentifier(instanceId);
0055 }
0056 
0057 #include "private/protocol_p.h"
0058 
0059 namespace QTest
0060 {
0061 template<>
0062 char *toString(const Akonadi::Protocol::ItemChangeNotificationPtr &msg)
0063 {
0064     return qstrdup(qPrintable(Akonadi::Protocol::debugString(msg)));
0065 }
0066 }
0067 
0068 namespace AkTest
0069 {
0070 enum NtfField {
0071     NtfType = (1 << 0),
0072     NtfOperation = (1 << 1),
0073     NtfSession = (1 << 2),
0074     NtfEntities = (1 << 3),
0075     NtfResource = (1 << 5),
0076     NtfCollection = (1 << 6),
0077     NtfDestResource = (1 << 7),
0078     NtfDestCollection = (1 << 8),
0079     NtfAddedFlags = (1 << 9),
0080     NtfRemovedFlags = (1 << 10),
0081     NtfAddedTags = (1 << 11),
0082     NtfRemovedTags = (1 << 12),
0083 
0084     NtfFlags = NtfAddedFlags | NtfRemovedTags,
0085     NtfTags = NtfAddedTags | NtfRemovedTags,
0086     NtfAll = NtfType | NtfOperation | NtfSession | NtfEntities | NtfResource | NtfCollection | NtfDestResource | NtfDestCollection | NtfFlags | NtfTags
0087 };
0088 using NtfFields = QFlags<NtfField>;
0089 
0090 bool compareNotifications(const Akonadi::Protocol::ItemChangeNotificationPtr &actual,
0091                           const Akonadi::Protocol::ItemChangeNotificationPtr &expected,
0092                           const NtfFields fields = NtfAll)
0093 {
0094     if (fields & NtfOperation) {
0095         AKCOMPARE(actual->operation(), expected->operation());
0096     }
0097     if (fields & NtfSession) {
0098         AKCOMPARE(actual->sessionId(), expected->sessionId());
0099     }
0100     if (fields & NtfEntities) {
0101         AKCOMPARE(actual->items(), expected->items());
0102     }
0103     if (fields & NtfResource) {
0104         AKCOMPARE(actual->resource(), expected->resource());
0105     }
0106     if (fields & NtfCollection) {
0107         AKCOMPARE(actual->parentCollection(), expected->parentCollection());
0108     }
0109     if (fields & NtfDestResource) {
0110         AKCOMPARE(actual->destinationResource(), expected->destinationResource());
0111     }
0112     if (fields & NtfDestCollection) {
0113         AKCOMPARE(actual->parentDestCollection(), expected->parentDestCollection());
0114     }
0115     if (fields & NtfAddedFlags) {
0116         AKCOMPARE(actual->addedFlags(), expected->addedFlags());
0117     }
0118     if (fields & NtfRemovedFlags) {
0119         AKCOMPARE(actual->removedFlags(), expected->removedFlags());
0120     }
0121     if (fields & NtfAddedTags) {
0122         AKCOMPARE(actual->addedTags(), expected->addedTags());
0123     }
0124     if (fields & NtfRemovedTags) {
0125         AKCOMPARE(actual->removedTags(), expected->removedTags());
0126     }
0127 
0128     return true;
0129 }
0130 }