1 | /* This file is based on qtest_kde.h from kdelibs |
2 | Copyright (C) 2006 David Faure <faure@kde.org> |
3 | Copyright (C) 2009 Volker Krause <vkrause@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License version 2 as published by the Free Software Foundation. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #ifndef QTEST_AKONADI_H |
21 | #define QTEST_AKONADI_H |
22 | |
23 | #include <qtest_kde.h> |
24 | |
25 | #include <akonadi/agentinstance.h> |
26 | #include <akonadi/agentmanager.h> |
27 | #include <klocale.h> |
28 | |
29 | /** |
30 | * \short Akonadi Replacement for QTEST_MAIN from QTestLib |
31 | * |
32 | * This macro should be used for classes that run inside the Akonadi Testrunner. |
33 | * So instead of writing QTEST_MAIN( TestClass ) you write |
34 | * QTEST_AKONADIMAIN( TestClass, GUI ). |
35 | * |
36 | * \param TestObject The class you use for testing. |
37 | * \param flags one of KDEMainFlag. This is passed to the QApplication constructor. |
38 | * |
39 | * \see KDEMainFlag |
40 | * \see QTestLib |
41 | * \see QTEST_KDEMAIN |
42 | */ |
43 | #define QTEST_AKONADIMAIN(TestObject, flags) \ |
44 | int main(int argc, char *argv[]) \ |
45 | { \ |
46 | setenv( "LC_ALL", "C", 1); \ |
47 | unsetenv( "KDE_COLOR_DEBUG" ); \ |
48 | KAboutData aboutData( QByteArray( "qttest" ), QByteArray(), ki18n( "KDE Test Program" ), QByteArray( "version" ) ); \ |
49 | KDEMainFlags mainFlags = flags; \ |
50 | KComponentData cData(&aboutData); \ |
51 | QApplication app( argc, argv, (mainFlags & GUI) != 0 ); \ |
52 | app.setApplicationName( QLatin1String( "qttest" ) ); \ |
53 | qRegisterMetaType<KUrl>(); /*as done by kapplication*/ \ |
54 | qRegisterMetaType<KUrl::List>(); \ |
55 | TestObject tc; \ |
56 | KGlobal::ref(); /* don't quit qeventloop after closing a mainwindow */ \ |
57 | return QTest::qExec( &tc, argc, argv ); \ |
58 | } |
59 | |
60 | namespace AkonadiTest { |
61 | /** |
62 | * Checks that the test is running in the proper test environment |
63 | */ |
64 | void checkTestIsIsolated() { |
65 | Q_ASSERT_X(!qgetenv("TESTRUNNER_DB_ENVIRONMENT" ).isEmpty(), |
66 | "AkonadiTest::checkTestIsIsolated" , |
67 | "This test must be run using ctest, in order to use the testrunner environment. Aborting, to avoid messing up your real akonadi" ); |
68 | } |
69 | |
70 | /** |
71 | * Switch all resources offline to reduce interference from them |
72 | */ |
73 | void setAllResourcesOffline() { |
74 | // switch all resources offline to reduce interference from them |
75 | foreach (Akonadi::AgentInstance agent, Akonadi::AgentManager::self()->instances()) { //krazy:exclude=foreach |
76 | agent.setIsOnline(false); |
77 | } |
78 | } |
79 | |
80 | } // namespace |
81 | |
82 | /** |
83 | * Runs an Akonadi::Job synchronously and aborts if the job failed. |
84 | * Similar to QVERIFY( job->exec() ) but includes the job error message |
85 | * in the output in case of a failure. |
86 | */ |
87 | #define AKVERIFYEXEC( job ) \ |
88 | QVERIFY2( job->exec(), job->errorString().toUtf8().constData() ) |
89 | |
90 | // Taken from Qt 5: |
91 | #if QT_VERSION < 0x050000 |
92 | |
93 | // Will try to wait for the expression to become true while allowing event processing |
94 | #define QTRY_VERIFY(__expr) \ |
95 | do { \ |
96 | const int __step = 50; \ |
97 | const int __timeout = 5000; \ |
98 | if ( !( __expr ) ) { \ |
99 | QTest::qWait( 0 ); \ |
100 | } \ |
101 | for ( int __i = 0; __i < __timeout && !( __expr ); __i += __step ) { \ |
102 | QTest::qWait( __step ); \ |
103 | } \ |
104 | QVERIFY( __expr ); \ |
105 | } while ( 0 ) |
106 | |
107 | // Will try to wait for the comparison to become successful while allowing event processing |
108 | #define QTRY_COMPARE(__expr, __expected) \ |
109 | do { \ |
110 | const int __step = 50; \ |
111 | const int __timeout = 5000; \ |
112 | if ( ( __expr ) != ( __expected ) ) { \ |
113 | QTest::qWait( 0 ); \ |
114 | } \ |
115 | for ( int __i = 0; __i < __timeout && ( ( __expr ) != ( __expected ) ); __i += __step ) { \ |
116 | QTest::qWait( __step ); \ |
117 | } \ |
118 | QCOMPARE( __expr, __expected ); \ |
119 | } while ( 0 ) |
120 | |
121 | #endif |
122 | |
123 | #endif |
124 | |