1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
45 #include <QtTest/qtest_global.h>
46 #include <QtTest/qtestcase.h>
47 #include <QtTest/qtestdata.h>
48 #include <QtTest/qbenchmark.h>
50 #include <QtCore/qbytearray.h>
51 #include <QtCore/qstring.h>
52 #include <QtCore/qstringlist.h>
53 #include <QtCore/qdatetime.h>
54 #include <QtCore/qobject.h>
55 #include <QtCore/qvariant.h>
56 #include <QtCore/qurl.h>
58 #include <QtCore/qpoint.h>
59 #include <QtCore/qsize.h>
60 #include <QtCore/qrect.h>
71 template<> inline char *toString(const QLatin1String &str)
73 return qstrdup(str.latin1());
76 template<> inline char *toString(const QString &str)
78 return qstrdup(str.toLatin1().constData());
81 template<> inline char *toString(const QByteArray &ba)
83 return QTest::toHexRepresentation(ba.constData(), ba.length());
86 #ifndef QT_NO_DATESTRING
87 template<> inline char *toString(const QTime &time)
90 ? qstrdup(time.toString(QLatin1String("hh:mm:ss.zzz")).toLatin1().constData())
91 : qstrdup("Invalid QTime");
94 template<> inline char *toString(const QDate &date)
97 ? qstrdup(date.toString(QLatin1String("yyyy/MM/dd")).toLatin1().constData())
98 : qstrdup("Invalid QDate");
101 template<> inline char *toString(const QDateTime &dateTime)
103 return dateTime.isValid()
104 ? qstrdup((dateTime.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz")) +
105 (dateTime.timeSpec() == Qt::LocalTime ? QLatin1String("[local time]") : QLatin1String("[UTC]"))).toLatin1().constData())
106 : qstrdup("Invalid QDateTime");
108 #endif // QT_NO_DATESTRING
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)
148 return qstrdup(QByteArray("Invalid URL: " + uri.errorString().toLatin1()).constData());
149 return qstrdup(uri.toEncoded().constData());
152 template<> inline char *toString(const QVariant &v)
154 QByteArray vstring("QVariant(");
156 QByteArray type(v.typeName());
157 if (type.isEmpty()) {
158 type = QByteArray::number(v.userType());
160 vstring.append(type);
163 if (v.canConvert(QVariant::String)) {
164 vstring.append(qvariant_cast<QString>(v).toLatin1());
167 vstring.append("<value not representable as string>");
173 return qstrdup(vstring.constData());
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(t1, QString(t2), actual, expected, file, line);
183 inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
184 const char *expected, const char *file, int line)
186 return qCompare(QString(t1), t2, actual, expected, file, line);
190 inline bool qCompare(QStringList const &t1, QStringList const &t2,
191 const char *actual, const char *expected, const char *file, int line)
196 const int actualSize = t1.count();
197 const int expectedSize = t2.count();
198 if (actualSize != expectedSize) {
199 qsnprintf(msg, sizeof(msg), "Compared QStringLists have different sizes.\n"
200 " Actual (%s) size: '%d'\n"
201 " Expected (%s) size: '%d'", actual, actualSize, expected, expectedSize);
204 for (int i = 0; isOk && i < actualSize; ++i) {
205 if (t1.at(i) != t2.at(i)) {
206 qsnprintf(msg, sizeof(msg), "Compared QStringLists differ at index %d.\n"
207 " Actual (%s): '%s'\n"
208 " Expected (%s): '%s'", i, actual, t1.at(i).toLatin1().constData(),
209 expected, t2.at(i).toLatin1().constData());
213 return compare_helper(isOk, msg, 0, 0, actual, expected, file, line);
216 template <typename T>
217 inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
218 const char *file, int line)
220 return qCompare(int(t1), int(t2), actual, expected, file, line);
223 template <typename T>
224 inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
225 const char *file, int line)
227 return qCompare(int(t1), t2, actual, expected, file, line);
233 #define QTEST_APPLESS_MAIN(TestObject) \
234 int main(int argc, char *argv[]) \
237 return QTest::qExec(&tc, argc, argv); \
240 #include <QtTest/qtestsystem.h>
242 #if defined(QT_WIDGETS_LIB)
244 #include <QtTest/qtest_gui.h>
246 #ifdef QT_KEYPAD_NAVIGATION
247 # define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
249 # define QTEST_DISABLE_KEYPAD_NAVIGATION
252 #define QTEST_MAIN(TestObject) \
253 int main(int argc, char *argv[]) \
255 QApplication app(argc, argv); \
256 app.setAttribute(Qt::AA_Use96Dpi, true); \
257 QTEST_DISABLE_KEYPAD_NAVIGATION \
259 return QTest::qExec(&tc, argc, argv); \
262 #elif defined(QT_GUI_LIB)
264 #include <QtTest/qtest_gui.h>
266 #define QTEST_MAIN(TestObject) \
267 int main(int argc, char *argv[]) \
269 QGuiApplication app(argc, argv); \
270 app.setAttribute(Qt::AA_Use96Dpi, true); \
272 return QTest::qExec(&tc, argc, argv); \
277 #define QTEST_MAIN(TestObject) \
278 int main(int argc, char *argv[]) \
280 QCoreApplication app(argc, argv); \
281 app.setAttribute(Qt::AA_Use96Dpi, true); \
283 return QTest::qExec(&tc, argc, argv); \
288 #define QTEST_GUILESS_MAIN(TestObject) \
289 int main(int argc, char *argv[]) \
291 QCoreApplication app(argc, argv); \
292 app.setAttribute(Qt::AA_Use96Dpi, true); \
294 return QTest::qExec(&tc, argc, argv); \