src/testlib/qtest.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtTest module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QTEST_H
       
    43 #define QTEST_H
       
    44 
       
    45 #include <QtTest/qtest_global.h>
       
    46 #include <QtTest/qtestcase.h>
       
    47 #include <QtTest/qtestdata.h>
       
    48 #include <QtTest/qtestdata.h>
       
    49 #include <QtTest/qbenchmark.h>
       
    50 
       
    51 #include <QtCore/qbytearray.h>
       
    52 #include <QtCore/qstring.h>
       
    53 #include <QtCore/qstringlist.h>
       
    54 #include <QtCore/qdatetime.h>
       
    55 #include <QtCore/qobject.h>
       
    56 #include <QtCore/qvariant.h>
       
    57 #include <QtCore/qurl.h>
       
    58 
       
    59 #include <QtCore/qpoint.h>
       
    60 #include <QtCore/qsize.h>
       
    61 #include <QtCore/qrect.h>
       
    62 
       
    63 
       
    64 QT_BEGIN_HEADER
       
    65 
       
    66 QT_BEGIN_NAMESPACE
       
    67 
       
    68 QT_MODULE(Test)
       
    69 
       
    70 namespace QTest
       
    71 {
       
    72 
       
    73 template<> inline char *toString(const QLatin1String &str)
       
    74 {
       
    75     return qstrdup(str.latin1());
       
    76 }
       
    77 
       
    78 template<> inline char *toString(const QString &str)
       
    79 {
       
    80     return qstrdup(str.toLatin1().constData());
       
    81 }
       
    82 
       
    83 template<> inline char *toString(const QByteArray &ba)
       
    84 {
       
    85     return QTest::toHexRepresentation(ba.constData(), ba.length());
       
    86 }
       
    87 
       
    88 template<> inline char *toString(const QTime &time)
       
    89 {
       
    90     return time.isValid()
       
    91         ? qstrdup(time.toString(QLatin1String("hh:mm:ss.zzz")).toLatin1().constData())
       
    92         : qstrdup("Invalid QTime");
       
    93 }
       
    94 
       
    95 template<> inline char *toString(const QDate &date)
       
    96 {
       
    97     return date.isValid()
       
    98         ? qstrdup(date.toString(QLatin1String("yyyy/MM/dd")).toLatin1().constData())
       
    99         : qstrdup("Invalid QDate");
       
   100 }
       
   101 
       
   102 template<> inline char *toString(const QDateTime &dateTime)
       
   103 {
       
   104     return dateTime.isValid()
       
   105         ? qstrdup((dateTime.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz")) +
       
   106                   (dateTime.timeSpec() == Qt::LocalTime ? QLatin1String("[local time]") : QLatin1String("[UTC]"))).toLatin1().constData())
       
   107         : qstrdup("Invalid QDateTime");
       
   108 }
       
   109 
       
   110 template<> inline char *toString(const QChar &c)
       
   111 {
       
   112     return qstrdup(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16)).toLatin1().constData());
       
   113 }
       
   114 
       
   115 template<> inline char *toString(const QPoint &p)
       
   116 {
       
   117     return qstrdup(QString::fromLatin1("QPoint(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData());
       
   118 }
       
   119 
       
   120 template<> inline char *toString(const QSize &s)
       
   121 {
       
   122     return qstrdup(QString::fromLatin1("QSize(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData());
       
   123 }
       
   124 
       
   125 template<> inline char *toString(const QRect &s)
       
   126 {
       
   127     return qstrdup(QString::fromLatin1("QRect(%1,%2 %5x%6) (bottomright %3,%4)").arg(s.left()).arg(s.top()).arg(s.right()).arg(s.bottom()).arg(s.width()).arg(s.height()).toLatin1().constData());
       
   128 }
       
   129 
       
   130 template<> inline char *toString(const QPointF &p)
       
   131 {
       
   132     return qstrdup(QString::fromLatin1("QPointF(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData());
       
   133 }
       
   134 
       
   135 template<> inline char *toString(const QSizeF &s)
       
   136 {
       
   137     return qstrdup(QString::fromLatin1("QSizeF(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData());
       
   138 }
       
   139 
       
   140 template<> inline char *toString(const QRectF &s)
       
   141 {
       
   142     return qstrdup(QString::fromLatin1("QRectF(%1,%2 %5x%6) (bottomright %3,%4)").arg(s.left()).arg(s.top()).arg(s.right()).arg(s.bottom()).arg(s.width()).arg(s.height()).toLatin1().constData());
       
   143 }
       
   144 
       
   145 template<> inline char *toString(const QUrl &uri)
       
   146 {
       
   147     return qstrdup(uri.toEncoded().constData());
       
   148 }
       
   149 
       
   150 template<> inline char *toString(const QVariant &v)
       
   151 {
       
   152     QByteArray vstring("QVariant(");
       
   153     if (v.isValid()) {
       
   154         QByteArray type(v.typeName());
       
   155         if (type.isEmpty()) {
       
   156             type = QByteArray::number(v.userType());
       
   157         }
       
   158         vstring.append(type);
       
   159         if (!v.isNull()) {
       
   160             vstring.append(',');
       
   161             if (v.canConvert(QVariant::String)) {
       
   162                 vstring.append(qVariantValue<QString>(v).toLatin1());
       
   163             }
       
   164             else {
       
   165                 vstring.append("<value not representable as string>");
       
   166             }
       
   167         }
       
   168     }
       
   169     vstring.append(')');
       
   170 
       
   171     return qstrdup(vstring.constData());
       
   172 }
       
   173 
       
   174 #ifndef QTEST_NO_SPECIALIZATIONS
       
   175 template<>
       
   176 #endif
       
   177 inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
       
   178                     const char *expected, const char *file, int line)
       
   179 {
       
   180     return qCompare<QString>(t1, QString(t2), actual, expected, file, line);
       
   181 }
       
   182 #ifndef QTEST_NO_SPECIALIZATIONS
       
   183 template<>
       
   184 #endif
       
   185 inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
       
   186                     const char *expected, const char *file, int line)
       
   187 {
       
   188     return qCompare<QString>(QString(t1), t2, actual, expected, file, line);
       
   189 }
       
   190 
       
   191 template<>
       
   192 inline bool qCompare(QStringList const &t1, QStringList const &t2,
       
   193                     const char *actual, const char *expected, const char *file, int line)
       
   194 {
       
   195     char msg[1024];
       
   196     msg[0] = '\0';
       
   197     bool isOk = true;
       
   198     if (t1.count() != t2.count()) {
       
   199         qt_snprintf(msg, 1024, "Compared QStringLists have different sizes.\n"
       
   200                     "   Actual (%s) size  : '%d'\n"
       
   201                     "   Expected (%s) size: '%d'", actual, t1.count(), expected, t2.count());
       
   202         isOk = false;
       
   203     }
       
   204     const int min = qMin(t1.count(), t2.count());
       
   205     for (int i = 0; isOk && i < min; ++i) {
       
   206         if (t1.at(i) != t2.at(i)) {
       
   207             qt_snprintf(msg, 1024, "Compared QStringLists differ at index %d.\n"
       
   208                         "   Actual (%s) : '%s'\n"
       
   209                         "   Expected (%s) : '%s'", i, actual, t1.at(i).toLatin1().constData(),
       
   210                         expected, t2.at(i).toLatin1().constData());
       
   211             isOk = false;
       
   212         }
       
   213     }
       
   214     return compare_helper(isOk, msg, file, line);
       
   215 }
       
   216 
       
   217 template <typename T>
       
   218 inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
       
   219                     const char *file, int line)
       
   220 {
       
   221     return qCompare(int(t1), int(t2), actual, expected, file, line);
       
   222 }
       
   223 
       
   224 template <typename T>
       
   225 inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
       
   226                     const char *file, int line)
       
   227 {
       
   228     return qCompare(int(t1), t2, actual, expected, file, line);
       
   229 }
       
   230 
       
   231 }
       
   232 QT_END_NAMESPACE
       
   233 
       
   234 #define QTEST_APPLESS_MAIN(TestObject) \
       
   235 int main(int argc, char *argv[]) \
       
   236 { \
       
   237     TestObject tc; \
       
   238     return QTest::qExec(&tc, argc, argv); \
       
   239 }
       
   240 
       
   241 #define QTEST_NOOP_MAIN \
       
   242 int main(int argc, char *argv[]) \
       
   243 { \
       
   244     QObject tc; \
       
   245     return QTest::qExec(&tc, argc, argv); \
       
   246 }
       
   247 
       
   248 #include <QtTest/qtestsystem.h>
       
   249 
       
   250 #ifdef QT_GUI_LIB
       
   251 
       
   252 #include <QtTest/qtest_gui.h>
       
   253 
       
   254 #ifdef QT_KEYPAD_NAVIGATION
       
   255 #  define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
       
   256 #else
       
   257 #  define QTEST_DISABLE_KEYPAD_NAVIGATION
       
   258 #endif
       
   259 
       
   260 #define QTEST_MAIN(TestObject) \
       
   261 int main(int argc, char *argv[]) \
       
   262 { \
       
   263     QApplication app(argc, argv); \
       
   264     QTEST_DISABLE_KEYPAD_NAVIGATION \
       
   265     TestObject tc; \
       
   266     return QTest::qExec(&tc, argc, argv); \
       
   267 }
       
   268 
       
   269 #else
       
   270 
       
   271 #define QTEST_MAIN(TestObject) \
       
   272 int main(int argc, char *argv[]) \
       
   273 { \
       
   274     QCoreApplication app(argc, argv); \
       
   275     TestObject tc; \
       
   276     return QTest::qExec(&tc, argc, argv); \
       
   277 }
       
   278 
       
   279 #endif // QT_GUI_LIB
       
   280 
       
   281 QT_END_HEADER
       
   282 
       
   283 #endif