File indexing completed on 2024-05-12 05:26:07

0001 /*
0002  * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #pragma once
0022 
0023 #include "sink_export.h"
0024 #include "applicationdomaintype.h"
0025 
0026 #include <memory>
0027 
0028 namespace Sink {
0029 namespace Test {
0030 /**
0031  * Initialize the test environment.
0032  * 
0033  * This makes use of QStandardPaths::setTestModeEnabled to avoid writing to the user directory,
0034  * and clears all data directories.
0035  */
0036 void SINK_EXPORT initTest();
0037 void SINK_EXPORT setTestModeEnabled(bool);
0038 bool SINK_EXPORT testModeEnabled();
0039 
0040 class SINK_EXPORT TestAccount {
0041 public:
0042     QByteArray identifier;
0043     static TestAccount registerAccount();
0044 
0045     template<typename DomainType>
0046     void SINK_EXPORT addEntity(const ApplicationDomain::ApplicationDomainType::Ptr &domainObject);
0047 
0048     template<typename DomainType>
0049     typename DomainType::Ptr SINK_EXPORT createEntity();
0050 
0051     template<typename DomainType>
0052     QList<ApplicationDomain::ApplicationDomainType::Ptr> SINK_EXPORT entities() const;
0053 
0054 private:
0055     TestAccount(){};
0056     TestAccount(const TestAccount &);
0057     TestAccount &operator=(const TestAccount &);
0058     QHash<QByteArray, QList<ApplicationDomain::ApplicationDomainType::Ptr> > mEntities;
0059     QHash<QByteArray, std::shared_ptr<void> > mFacades;
0060 };
0061 
0062 }
0063 }
0064 
0065 #define ASYNCCOMPARE(actual, expected) \
0066 do {\
0067     if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
0068         return KAsync::error<void>(1, "Comparison failed.");\
0069 } while (0)
0070 
0071 #define ASYNCVERIFY(statement) \
0072 do {\
0073     if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
0074         return KAsync::error<void>(1, "Verify failed.");\
0075 } while (0)
0076 
0077 #define VERIFYEXEC(statement) \
0078 do {\
0079     auto result = statement.exec(); \
0080     result.waitForFinished(); \
0081     if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\
0082         return;\
0083 } while (0)
0084 
0085 #define VERIFYEXEC_RET(statement, return) \
0086 do {\
0087     auto result = statement.exec(); \
0088     result.waitForFinished(); \
0089     if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\
0090         return #return;\
0091 } while (0)
0092 
0093 #define VERIFYEXEC_FAIL(statement) \
0094 do {\
0095     auto result = statement.exec(); \
0096     result.waitForFinished(); \
0097     if (!QTest::qVerify(result.errorCode(), #statement, "", __FILE__, __LINE__))\
0098         return;\
0099 } while (0)
0100 
0101 //qWait(1) seems to simply skip waiting at all.
0102 #define QUICK_TRY_VERIFY(statement) \
0103 do {\
0104     static int timeout = 2500; \
0105     int i = 0; \
0106     for (; i < timeout && !(statement); i++) { \
0107         QTest::qWait(2); \
0108     } \
0109     if (i >= timeout) { \
0110         qWarning() << "Timeout during QUICK_TRY_VERIFY"; \
0111     } \
0112 } while (0)