Fix binary incompatibility between openssl versions
[profile/ivi/qtbase.git] / src / testlib / qtest.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
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/qbenchmark.h>
49
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>
57
58 #include <QtCore/qpoint.h>
59 #include <QtCore/qsize.h>
60 #include <QtCore/qrect.h>
61
62
63 QT_BEGIN_HEADER
64
65 QT_BEGIN_NAMESPACE
66
67
68 namespace QTest
69 {
70
71 template<> inline char *toString(const QLatin1String &str)
72 {
73     return qstrdup(str.latin1());
74 }
75
76 template<> inline char *toString(const QString &str)
77 {
78     return qstrdup(str.toLatin1().constData());
79 }
80
81 template<> inline char *toString(const QByteArray &ba)
82 {
83     return QTest::toHexRepresentation(ba.constData(), ba.length());
84 }
85
86 #ifndef QT_NO_DATESTRING
87 template<> inline char *toString(const QTime &time)
88 {
89     return time.isValid()
90         ? qstrdup(time.toString(QLatin1String("hh:mm:ss.zzz")).toLatin1().constData())
91         : qstrdup("Invalid QTime");
92 }
93
94 template<> inline char *toString(const QDate &date)
95 {
96     return date.isValid()
97         ? qstrdup(date.toString(QLatin1String("yyyy/MM/dd")).toLatin1().constData())
98         : qstrdup("Invalid QDate");
99 }
100
101 template<> inline char *toString(const QDateTime &dateTime)
102 {
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");
107 }
108 #endif // QT_NO_DATESTRING
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     if (!uri.isValid())
148         return qstrdup(QByteArray("Invalid URL: " + uri.errorString().toLatin1()).constData());
149     return qstrdup(uri.toEncoded().constData());
150 }
151
152 template<> inline char *toString(const QVariant &v)
153 {
154     QByteArray vstring("QVariant(");
155     if (v.isValid()) {
156         QByteArray type(v.typeName());
157         if (type.isEmpty()) {
158             type = QByteArray::number(v.userType());
159         }
160         vstring.append(type);
161         if (!v.isNull()) {
162             vstring.append(',');
163             if (v.canConvert(QVariant::String)) {
164                 vstring.append(qvariant_cast<QString>(v).toLatin1());
165             }
166             else {
167                 vstring.append("<value not representable as string>");
168             }
169         }
170     }
171     vstring.append(')');
172
173     return qstrdup(vstring.constData());
174 }
175
176 template<>
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(t1, QString(t2), actual, expected, file, line);
181 }
182 template<>
183 inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
184                     const char *expected, const char *file, int line)
185 {
186     return qCompare(QString(t1), t2, actual, expected, file, line);
187 }
188
189 template<>
190 inline bool qCompare(QStringList const &t1, QStringList const &t2,
191                     const char *actual, const char *expected, const char *file, int line)
192 {
193     char msg[1024];
194     msg[0] = '\0';
195     bool isOk = true;
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);
202         isOk = false;
203     }
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());
210             isOk = false;
211         }
212     }
213     return compare_helper(isOk, msg, 0, 0, actual, expected, file, line);
214 }
215
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)
219 {
220     return qCompare(int(t1), int(t2), actual, expected, file, line);
221 }
222
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)
226 {
227     return qCompare(int(t1), t2, actual, expected, file, line);
228 }
229
230 }
231 QT_END_NAMESPACE
232
233 #define QTEST_APPLESS_MAIN(TestObject) \
234 int main(int argc, char *argv[]) \
235 { \
236     TestObject tc; \
237     return QTest::qExec(&tc, argc, argv); \
238 }
239
240 #include <QtTest/qtestsystem.h>
241
242 #if defined(QT_WIDGETS_LIB)
243
244 #include <QtTest/qtest_gui.h>
245
246 #ifdef QT_KEYPAD_NAVIGATION
247 #  define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
248 #else
249 #  define QTEST_DISABLE_KEYPAD_NAVIGATION
250 #endif
251
252 #define QTEST_MAIN(TestObject) \
253 int main(int argc, char *argv[]) \
254 { \
255     QApplication app(argc, argv); \
256     app.setAttribute(Qt::AA_Use96Dpi, true); \
257     QTEST_DISABLE_KEYPAD_NAVIGATION \
258     TestObject tc; \
259     return QTest::qExec(&tc, argc, argv); \
260 }
261
262 #elif defined(QT_GUI_LIB)
263
264 #include <QtTest/qtest_gui.h>
265
266 #define QTEST_MAIN(TestObject) \
267 int main(int argc, char *argv[]) \
268 { \
269     QGuiApplication app(argc, argv); \
270     app.setAttribute(Qt::AA_Use96Dpi, true); \
271     TestObject tc; \
272     return QTest::qExec(&tc, argc, argv); \
273 }
274
275 #else
276
277 #define QTEST_MAIN(TestObject) \
278 int main(int argc, char *argv[]) \
279 { \
280     QCoreApplication app(argc, argv); \
281     app.setAttribute(Qt::AA_Use96Dpi, true); \
282     TestObject tc; \
283     return QTest::qExec(&tc, argc, argv); \
284 }
285
286 #endif // QT_GUI_LIB
287
288 #define QTEST_GUILESS_MAIN(TestObject) \
289 int main(int argc, char *argv[]) \
290 { \
291     QCoreApplication app(argc, argv); \
292     app.setAttribute(Qt::AA_Use96Dpi, true); \
293     TestObject tc; \
294     return QTest::qExec(&tc, argc, argv); \
295 }
296
297 QT_END_HEADER
298
299 #endif