1/* This file is part of the KDE libraries
2 Copyright (C) 2006 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#ifndef QTEST_KDE_H
20#define QTEST_KDE_H
21
22#include <QtTest/QtTest>
23#include <stdlib.h>
24#include <assert.h>
25#include <kaboutdata.h>
26#include <kcmdlineargs.h>
27#include <kcomponentdata.h>
28#include <kglobal.h>
29#include <kurl.h>
30#include <QtGui/QApplication>
31#include <QtCore/QEventLoop>
32#include <QtTest/QSignalSpy>
33
34namespace QTest
35{
36 /**
37 * Starts an event loop that runs until the given signal is received.
38 * Optionally the event loop can return earlier on a timeout.
39 *
40 * \param timeout the timeout in milliseconds
41 *
42 * \return \p true if the requested signal was received
43 * \p false on timeout
44 */
45 KDECORE_EXPORT bool kWaitForSignal(QObject *obj, const char *signal, int timeout = 0);
46} // namespace QTest
47
48// By default, unit tests get no gui.
49// Pass GUI if you use any GUI classes
50enum KDEMainFlag { NoGUI = 0, GUI = 1 }; // bitfield, next item is 2!
51Q_DECLARE_FLAGS(KDEMainFlags, KDEMainFlag)
52Q_DECLARE_OPERATORS_FOR_FLAGS(KDEMainFlags)
53/**
54 * \short QTEST_KDEMAIN variant with additional argument for the main component name
55 *
56 * This variant is useful for testing application classes which rely on the main
57 * component having a specific name (for instance to find xmlgui .rc files).
58 *
59 * This variant should not be needed in kdelibs's own unit tests.
60 *
61 * \param TestObject The class you use for testing.
62 * \param flags one of KDEMainFlag. This is passed to the QApplication constructor.
63 * \param componentName the name that will be given to the main component data.
64 *
65 * \see KDEMainFlag
66 * \see QTestLib
67 */
68#define QTEST_KDEMAIN_WITH_COMPONENTNAME(TestObject, flags, componentName) \
69int main(int argc, char *argv[]) \
70{ \
71 setenv("LC_ALL", "C", 1); \
72 assert( !QDir::homePath().isEmpty() ); \
73 setenv("KDEHOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test") ), 1); \
74 setenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/local") ), 1); \
75 setenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config") ), 1); \
76 setenv("KDE_SKIP_KDERC", "1", 1); \
77 unsetenv("KDE_COLOR_DEBUG"); \
78 QFile::remove(QDir::homePath() + QString::fromLatin1("/.kde-unit-test/share/config/qttestrc")); \
79 KAboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") ); \
80 KDEMainFlags mainFlags = flags; \
81 KComponentData cData(&aboutData); \
82 QApplication app( argc, argv, (mainFlags & GUI) != 0 ); \
83 app.setApplicationName( QLatin1String("qttest") ); \
84 qRegisterMetaType<KUrl>(); /*as done by kapplication*/ \
85 qRegisterMetaType<KUrl::List>(); \
86 TestObject tc; \
87 KGlobal::ref(); /* don't quit qeventloop after closing a mainwindow */ \
88 return QTest::qExec( &tc, argc, argv ); \
89}
90
91/**
92 * \short KDE Replacement for QTEST_MAIN from QTestLib
93 *
94 * This macro should be used for classes that need a KComponentData.
95 * So instead of writing QTEST_MAIN( TestClass ) you write
96 * QTEST_KDEMAIN( TestClass, GUI ).
97 *
98 * \param TestObject The class you use for testing.
99 * \param flags one of KDEMainFlag. This is passed to the QApplication constructor.
100 *
101 * \see KDEMainFlag
102 * \see QTestLib
103 */
104#define QTEST_KDEMAIN(TestObject, flags) QTEST_KDEMAIN_WITH_COMPONENTNAME(TestObject, flags, "qttest")
105
106/**
107 * \short KDE Replacement for QTEST_MAIN from QTestLib, for non-gui code.
108 *
109 * This macro should be used for classes that need a KComponentData.
110 * So instead of writing QTEST_MAIN( TestClass ) you write
111 * QTEST_KDEMAIN_CORE( TestClass ).
112 *
113 * \param TestObject The class you use for testing.
114 *
115 * \see KDEMainFlag
116 * \see QTestLib
117 * \since 4.3
118 */
119#define QTEST_KDEMAIN_CORE_WITH_COMPONENTNAME(TestObject, componentName) \
120int main(int argc, char *argv[]) \
121{ \
122 setenv("LC_ALL", "C", 1); \
123 assert( !QDir::homePath().isEmpty() ); \
124 setenv("KDEHOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test" )), 1); \
125 setenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/local") ), 1); \
126 setenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config") ), 1); \
127 setenv("KDE_SKIP_KDERC", "1", 1); \
128 unsetenv("KDE_COLOR_DEBUG"); \
129 QFile::remove(QDir::homePath() + QString::fromLatin1("/.kde-unit-test/share/config/qttestrc")); \
130 KAboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") ); \
131 KComponentData cData(&aboutData); \
132 QCoreApplication app( argc, argv ); \
133 app.setApplicationName( QLatin1String("qttest") ); \
134 qRegisterMetaType<KUrl>(); /*as done by kapplication*/ \
135 qRegisterMetaType<KUrl::List>(); \
136 TestObject tc; \
137 KGlobal::ref(); /* don't quit qeventloop after closing a mainwindow */ \
138 return QTest::qExec( &tc, argc, argv ); \
139}
140
141/**
142 * \short KDE Replacement for QTEST_MAIN from QTestLib, for non-gui code.
143 *
144 * This macro should be used for classes that need a KComponentData.
145 * So instead of writing QTEST_MAIN( TestClass ) you write
146 * QTEST_KDEMAIN_CORE( TestClass ).
147 *
148 * \param TestObject The class you use for testing.
149 *
150 * \see KDEMainFlag
151 * \see QTestLib
152 */
153#define QTEST_KDEMAIN_CORE(TestObject) QTEST_KDEMAIN_CORE_WITH_COMPONENTNAME(TestObject, "qttest")
154
155#endif /* QTEST_KDE_H */
156