1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtTest module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
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>
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>
59 #include <QtCore/qpoint.h>
60 #include <QtCore/qsize.h>
61 #include <QtCore/qrect.h>
73 template<> inline char *toString(const QLatin1String &str)
75 return qstrdup(str.latin1());
78 template<> inline char *toString(const QString &str)
80 return qstrdup(str.toLatin1().constData());
83 template<> inline char *toString(const QByteArray &ba)
85 return QTest::toHexRepresentation(ba.constData(), ba.length());
88 template<> inline char *toString(const QTime &time)
91 ? qstrdup(time.toString(QLatin1String("hh:mm:ss.zzz")).toLatin1().constData())
92 : qstrdup("Invalid QTime");
95 template<> inline char *toString(const QDate &date)
98 ? qstrdup(date.toString(QLatin1String("yyyy/MM/dd")).toLatin1().constData())
99 : qstrdup("Invalid QDate");
102 template<> inline char *toString(const QDateTime &dateTime)
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");
110 template<> inline char *toString(const QChar &c)
112 return qstrdup(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16)).toLatin1().constData());
115 template<> inline char *toString(const QPoint &p)
117 return qstrdup(QString::fromLatin1("QPoint(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData());
120 template<> inline char *toString(const QSize &s)
122 return qstrdup(QString::fromLatin1("QSize(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData());
125 template<> inline char *toString(const QRect &s)
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());
130 template<> inline char *toString(const QPointF &p)
132 return qstrdup(QString::fromLatin1("QPointF(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData());
135 template<> inline char *toString(const QSizeF &s)
137 return qstrdup(QString::fromLatin1("QSizeF(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData());
140 template<> inline char *toString(const QRectF &s)
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());
145 template<> inline char *toString(const QUrl &uri)
147 return qstrdup(uri.toEncoded().constData());
150 template<> inline char *toString(const QVariant &v)
152 QByteArray vstring("QVariant(");
154 QByteArray type(v.typeName());
155 if (type.isEmpty()) {
156 type = QByteArray::number(v.userType());
158 vstring.append(type);
161 if (v.canConvert(QVariant::String)) {
162 vstring.append(qvariant_cast<QString>(v).toLatin1());
165 vstring.append("<value not representable as string>");
171 return qstrdup(vstring.constData());
174 #ifndef QTEST_NO_SPECIALIZATIONS
177 inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
178 const char *expected, const char *file, int line)
180 return qCompare<QString>(t1, QString(t2), actual, expected, file, line);
182 #ifndef QTEST_NO_SPECIALIZATIONS
185 inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
186 const char *expected, const char *file, int line)
188 return qCompare<QString>(QString(t1), t2, actual, expected, file, line);
192 inline bool qCompare(QStringList const &t1, QStringList const &t2,
193 const char *actual, const char *expected, const char *file, int line)
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());
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());
214 return compare_helper(isOk, msg, file, line);
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)
221 return qCompare(int(t1), int(t2), actual, expected, file, line);
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)
228 return qCompare(int(t1), t2, actual, expected, file, line);
234 #define QTEST_APPLESS_MAIN(TestObject) \
235 int main(int argc, char *argv[]) \
238 return QTest::qExec(&tc, argc, argv); \
241 #define QTEST_NOOP_MAIN \
242 int main(int argc, char *argv[]) \
245 return QTest::qExec(&tc, argc, argv); \
248 #include <QtTest/qtestsystem.h>
252 #include <QtTest/qtest_gui.h>
254 #ifdef QT_KEYPAD_NAVIGATION
255 # define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
257 # define QTEST_DISABLE_KEYPAD_NAVIGATION
260 #define QTEST_MAIN(TestObject) \
261 int main(int argc, char *argv[]) \
263 QApplication app(argc, argv); \
264 QTEST_DISABLE_KEYPAD_NAVIGATION \
266 return QTest::qExec(&tc, argc, argv); \
271 #define QTEST_MAIN(TestObject) \
272 int main(int argc, char *argv[]) \
274 QCoreApplication app(argc, argv); \
276 return QTest::qExec(&tc, argc, argv); \