File indexing completed on 2024-04-14 14:20:11

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 2006 David Faure <faure@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License version 2 as published by the Free Software Foundation.
0007 
0008    This library is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    Library General Public License for more details.
0012 
0013    You should have received a copy of the GNU Library General Public License
0014    along with this library; see the file COPYING.LIB.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #ifndef QTEST_KDE_H
0020 #define QTEST_KDE_H
0021 
0022 #include <kdelibs4support_export.h>
0023 #include <QTest>
0024 #include <stdlib.h>
0025 #include <assert.h>
0026 #include <k4aboutdata.h>
0027 #include <klocalizedstring.h>
0028 #include <kcmdlineargs.h>
0029 #include <kcomponentdata.h>
0030 #include "kglobal.h"
0031 #include <kurl.h>
0032 #include <QApplication>
0033 #include <QEventLoop>
0034 #include <QSignalSpy>
0035 
0036 /** @file */ // Trigger doxygen coverage of global objects in the file, like macros
0037 
0038 /// The QTest namespace.
0039 namespace QTest
0040 {
0041 /**
0042  * Starts an event loop that runs until the given signal is received.
0043  * Optionally the event loop can return earlier on a timeout.
0044  *
0045  * \param timeout the timeout in milliseconds
0046  *
0047  * \return \p true if the requested signal was received
0048  *         \p false on timeout
0049  * \deprecated since 5.0, use QSignalSpy::wait(timeout)
0050  */
0051 KDELIBS4SUPPORT_DEPRECATED_EXPORT bool kWaitForSignal(QObject *obj, const char *signal, int timeout = 0);
0052 } // namespace QTest
0053 
0054 // By default, unit tests get no gui.
0055 // Pass GUI if you use any GUI classes
0056 enum KDEMainFlag { NoGUI = 0, GUI = 1 }; // bitfield, next item is 2!
0057 Q_DECLARE_FLAGS(KDEMainFlags, KDEMainFlag)
0058 Q_DECLARE_OPERATORS_FOR_FLAGS(KDEMainFlags)
0059 /**
0060  * \short QTEST_KDEMAIN variant with additional argument for the main component name
0061  *
0062  * This variant is useful for testing application classes which rely on the main
0063  * component having a specific name (for instance to find xmlgui .rc files).
0064  *
0065  * This variant should not be needed in kdelibs's own unit tests.
0066  *
0067  * \param TestObject The class you use for testing.
0068  * \param flags one of KDEMainFlag. This is passed to the QApplication constructor.
0069  * \param componentName the name that will be given to the main component data.
0070  *
0071  * \see KDEMainFlag
0072  * \see QTestLib
0073  */
0074 #define QTEST_KDEMAIN_WITH_COMPONENTNAME(TestObject, flags, componentName) \
0075     int main(int argc, char *argv[]) \
0076     { \
0077         qputenv("LC_ALL", "C"); \
0078         assert( !QDir::homePath().isEmpty() ); \
0079         qputenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/local") )); \
0080         qputenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config") )); \
0081         qputenv("XDG_CACHE_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/cache") )); \
0082         qputenv("KDE_SKIP_KDERC", "1"); \
0083         unsetenv("KDE_COLOR_DEBUG"); \
0084         QFile::remove(QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config/qttestrc"));  \
0085         K4AboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") );  \
0086         KDEMainFlags mainFlags = flags;                         \
0087         KComponentData cData(&aboutData); \
0088         QApplication app( argc, argv, (mainFlags & GUI) != 0 ); \
0089         app.setApplicationName( QLatin1String("qttest") ); \
0090         qRegisterMetaType<KUrl>(); /*as done by kapplication*/ \
0091         qRegisterMetaType<QList<KUrl> >(); \
0092         TestObject tc; \
0093         KGlobal::ref(); /* don't quit qeventloop after closing a mainwindow */ \
0094         return QTest::qExec( &tc, argc, argv ); \
0095     }
0096 
0097 /**
0098  * \short KDE Replacement for QTEST_MAIN from QTestLib
0099  *
0100  * This macro should be used for classes that need a KComponentData.
0101  * So instead of writing QTEST_MAIN( TestClass ) you write
0102  * QTEST_KDEMAIN( TestClass, GUI ).
0103  *
0104  * \param TestObject The class you use for testing.
0105  * \param flags one of KDEMainFlag. This is passed to the QApplication constructor.
0106  *
0107  * \see KDEMainFlag
0108  * \see QTestLib
0109  */
0110 #define QTEST_KDEMAIN(TestObject, flags) QTEST_KDEMAIN_WITH_COMPONENTNAME(TestObject, flags, "qttest")
0111 
0112 /**
0113  * \short KDE Replacement for QTEST_MAIN from QTestLib, for non-gui code.
0114  *
0115  * This macro should be used for classes that need a KComponentData.
0116  * So instead of writing QTEST_MAIN( TestClass ) you write
0117  * QTEST_KDEMAIN_CORE( TestClass ).
0118  *
0119  * \param TestObject The class you use for testing.
0120  *
0121  * \see KDEMainFlag
0122  * \see QTestLib
0123  * \since 4.3
0124  */
0125 #define QTEST_KDEMAIN_CORE_WITH_COMPONENTNAME(TestObject, componentName) \
0126     int main(int argc, char *argv[]) \
0127     { \
0128         qputenv("LC_ALL", "C"); \
0129         assert( !QDir::homePath().isEmpty() ); \
0130         qputenv("XDG_DATA_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/local"))); \
0131         qputenv("XDG_CONFIG_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config") )); \
0132         qputenv("XDG_CACHE_HOME", QFile::encodeName( QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/cache") )); \
0133         qputenv("KDE_SKIP_KDERC", "1"); \
0134         unsetenv("KDE_COLOR_DEBUG"); \
0135         QFile::remove(QDir::homePath() + QString::fromLatin1("/.kde-unit-test/xdg/config/qttestrc"));  \
0136         K4AboutData aboutData( QByteArray(componentName), QByteArray(), ki18n("KDE Test Program"), QByteArray("version") );  \
0137         KComponentData cData(&aboutData); \
0138         QCoreApplication app( argc, argv ); \
0139         app.setApplicationName( QLatin1String("qttest") ); \
0140         qRegisterMetaType<KUrl>(); /*as done by kapplication*/ \
0141         qRegisterMetaType<QList<KUrl> >(); \
0142         TestObject tc; \
0143         KGlobal::ref(); /* don't quit qeventloop after closing a mainwindow */ \
0144         return QTest::qExec( &tc, argc, argv ); \
0145     }
0146 
0147 /**
0148  * \short KDE Replacement for QTEST_MAIN from QTestLib, for non-gui code.
0149  *
0150  * This macro should be used for classes that need a KComponentData.
0151  * So instead of writing QTEST_MAIN( TestClass ) you write
0152  * QTEST_KDEMAIN_CORE( TestClass ).
0153  *
0154  * \param TestObject The class you use for testing.
0155  *
0156  * \see KDEMainFlag
0157  * \see QTestLib
0158  */
0159 #define QTEST_KDEMAIN_CORE(TestObject) QTEST_KDEMAIN_CORE_WITH_COMPONENTNAME(TestObject, "qttest")
0160 
0161 #endif /* QTEST_KDE_H */