Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2016 The Qt Company Ltd.
4 : ** Copyright (C) 2016 Intel Corporation.
5 : ** Contact: https://www.qt.io/licensing/
6 : **
7 : ** This file is part of the QtTest module of the Qt Toolkit.
8 : **
9 : ** $QT_BEGIN_LICENSE:LGPL$
10 : ** Commercial License Usage
11 : ** Licensees holding valid commercial Qt licenses may use this file in
12 : ** accordance with the commercial license agreement provided with the
13 : ** Software or, alternatively, in accordance with the terms contained in
14 : ** a written agreement between you and The Qt Company. For licensing terms
15 : ** and conditions see https://www.qt.io/terms-conditions. For further
16 : ** information use the contact form at https://www.qt.io/contact-us.
17 : **
18 : ** GNU Lesser General Public License Usage
19 : ** Alternatively, this file may be used under the terms of the GNU Lesser
20 : ** General Public License version 3 as published by the Free Software
21 : ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 : ** packaging of this file. Please review the following information to
23 : ** ensure the GNU Lesser General Public License version 3 requirements
24 : ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 : **
26 : ** GNU General Public License Usage
27 : ** Alternatively, this file may be used under the terms of the GNU
28 : ** General Public License version 2.0 or (at your option) the GNU General
29 : ** Public license version 3 or any later version approved by the KDE Free
30 : ** Qt Foundation. The licenses are as published by the Free Software
31 : ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 : ** included in the packaging of this file. Please review the following
33 : ** information to ensure the GNU General Public License requirements will
34 : ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 : ** https://www.gnu.org/licenses/gpl-3.0.html.
36 : **
37 : ** $QT_END_LICENSE$
38 : **
39 : ****************************************************************************/
40 :
41 : #ifndef QTEST_H
42 : #define QTEST_H
43 :
44 : #include <QtTest/qtest_global.h>
45 : #include <QtTest/qtestcase.h>
46 : #include <QtTest/qtestdata.h>
47 : #include <QtTest/qbenchmark.h>
48 :
49 : #include <QtCore/qbytearray.h>
50 : #include <QtCore/qstring.h>
51 : #include <QtCore/qstringlist.h>
52 : #include <QtCore/qdatetime.h>
53 : #include <QtCore/qobject.h>
54 : #include <QtCore/qvariant.h>
55 : #include <QtCore/qurl.h>
56 : #include <QtCore/quuid.h>
57 :
58 : #include <QtCore/qpoint.h>
59 : #include <QtCore/qsize.h>
60 : #include <QtCore/qrect.h>
61 :
62 : QT_BEGIN_NAMESPACE
63 :
64 :
65 : namespace QTest
66 : {
67 :
68 44 : template <> inline char *toString(const QStringView &str)
69 : {
70 44 : return QTest::toPrettyUnicode(str);
71 : }
72 :
73 44 : template<> inline char *toString(const QString &str)
74 : {
75 44 : return toString(QStringView(str));
76 : }
77 :
78 : template<> inline char *toString(const QLatin1String &str)
79 : {
80 : return toString(QString(str));
81 : }
82 :
83 : template<> inline char *toString(const QByteArray &ba)
84 : {
85 : return QTest::toPrettyCString(ba.constData(), ba.length());
86 : }
87 :
88 : #ifndef QT_NO_DATESTRING
89 : template<> inline char *toString(const QTime &time)
90 : {
91 : return time.isValid()
92 : ? qstrdup(qPrintable(time.toString(QStringViewLiteral("hh:mm:ss.zzz"))))
93 : : qstrdup("Invalid QTime");
94 : }
95 :
96 : template<> inline char *toString(const QDate &date)
97 : {
98 : return date.isValid()
99 : ? qstrdup(qPrintable(date.toString(QStringViewLiteral("yyyy/MM/dd"))))
100 : : qstrdup("Invalid QDate");
101 : }
102 :
103 : template<> inline char *toString(const QDateTime &dateTime)
104 : {
105 : return dateTime.isValid()
106 : ? qstrdup(qPrintable(dateTime.toString(QStringViewLiteral("yyyy/MM/dd hh:mm:ss.zzz[t]"))))
107 : : qstrdup("Invalid QDateTime");
108 : }
109 : #endif // QT_NO_DATESTRING
110 :
111 : template<> inline char *toString(const QChar &c)
112 : {
113 : const ushort uc = c.unicode();
114 : if (uc < 128) {
115 : char msg[32] = {'\0'};
116 : qsnprintf(msg, sizeof(msg), "QChar: '%c' (0x%x)", char(uc), unsigned(uc));
117 : return qstrdup(msg);
118 : }
119 : return qstrdup(qPrintable(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16))));
120 : }
121 :
122 : template<> inline char *toString(const QPoint &p)
123 : {
124 : char msg[128] = {'\0'};
125 : qsnprintf(msg, sizeof(msg), "QPoint(%d,%d)", p.x(), p.y());
126 : return qstrdup(msg);
127 : }
128 :
129 : template<> inline char *toString(const QSize &s)
130 : {
131 : char msg[128] = {'\0'};
132 : qsnprintf(msg, sizeof(msg), "QSize(%dx%d)", s.width(), s.height());
133 : return qstrdup(msg);
134 : }
135 :
136 : template<> inline char *toString(const QRect &s)
137 : {
138 : char msg[256] = {'\0'};
139 : qsnprintf(msg, sizeof(msg), "QRect(%d,%d %dx%d) (bottomright %d,%d)",
140 : s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
141 : return qstrdup(msg);
142 : }
143 :
144 : template<> inline char *toString(const QPointF &p)
145 : {
146 : char msg[64] = {'\0'};
147 : qsnprintf(msg, sizeof(msg), "QPointF(%g,%g)", p.x(), p.y());
148 : return qstrdup(msg);
149 : }
150 :
151 : template<> inline char *toString(const QSizeF &s)
152 : {
153 : char msg[64] = {'\0'};
154 : qsnprintf(msg, sizeof(msg), "QSizeF(%gx%g)", s.width(), s.height());
155 : return qstrdup(msg);
156 : }
157 :
158 : template<> inline char *toString(const QRectF &s)
159 : {
160 : char msg[256] = {'\0'};
161 : qsnprintf(msg, sizeof(msg), "QRectF(%g,%g %gx%g) (bottomright %g,%g)",
162 : s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
163 : return qstrdup(msg);
164 : }
165 :
166 : template<> inline char *toString(const QUrl &uri)
167 : {
168 : if (!uri.isValid())
169 : return qstrdup(qPrintable(QLatin1String("Invalid URL: ") + uri.errorString()));
170 : return qstrdup(uri.toEncoded().constData());
171 : }
172 :
173 : template <> inline char *toString(const QUuid &uuid)
174 : {
175 : return qstrdup(uuid.toByteArray().constData());
176 : }
177 :
178 : template<> inline char *toString(const QVariant &v)
179 : {
180 : QByteArray vstring("QVariant(");
181 : if (v.isValid()) {
182 : QByteArray type(v.typeName());
183 : if (type.isEmpty()) {
184 : type = QByteArray::number(v.userType());
185 : }
186 : vstring.append(type);
187 : if (!v.isNull()) {
188 : vstring.append(',');
189 : if (v.canConvert(QVariant::String)) {
190 : vstring.append(v.toString().toLocal8Bit());
191 : }
192 : else {
193 : vstring.append("<value not representable as string>");
194 : }
195 : }
196 : }
197 : vstring.append(')');
198 :
199 : return qstrdup(vstring.constData());
200 : }
201 :
202 : template <typename T1, typename T2>
203 : inline char *toString(const QPair<T1, T2> &pair)
204 : {
205 : const QScopedArrayPointer<char> first(toString(pair.first));
206 : const QScopedArrayPointer<char> second(toString(pair.second));
207 : return toString(QString::asprintf("QPair(%s,%s)", first.data(), second.data()));
208 : }
209 :
210 : template <typename T1, typename T2>
211 : inline char *toString(const std::pair<T1, T2> &pair)
212 : {
213 : const QScopedArrayPointer<char> first(toString(pair.first));
214 : const QScopedArrayPointer<char> second(toString(pair.second));
215 : return toString(QString::asprintf("std::pair(%s,%s)", first.data(), second.data()));
216 : }
217 :
218 : inline char *toString(std::nullptr_t)
219 : {
220 : return toString(QLatin1String("nullptr"));
221 : }
222 :
223 : template<>
224 : inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
225 : const char *expected, const char *file, int line)
226 : {
227 : return qCompare(t1, QString(t2), actual, expected, file, line);
228 : }
229 : template<>
230 : inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
231 : const char *expected, const char *file, int line)
232 : {
233 : return qCompare(QString(t1), t2, actual, expected, file, line);
234 : }
235 :
236 : template <typename T>
237 : inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected,
238 : const char *file, int line)
239 : {
240 : char msg[1024];
241 : msg[0] = '\0';
242 : bool isOk = true;
243 : const int actualSize = t1.count();
244 : const int expectedSize = t2.count();
245 : if (actualSize != expectedSize) {
246 : qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n"
247 : " Actual (%s) size: %d\n"
248 : " Expected (%s) size: %d", actual, actualSize, expected, expectedSize);
249 : isOk = false;
250 : }
251 : for (int i = 0; isOk && i < actualSize; ++i) {
252 : if (!(t1.at(i) == t2.at(i))) {
253 : char *val1 = toString(t1.at(i));
254 : char *val2 = toString(t2.at(i));
255 :
256 : qsnprintf(msg, sizeof(msg), "Compared lists differ at index %d.\n"
257 : " Actual (%s): %s\n"
258 : " Expected (%s): %s", i, actual, val1 ? val1 : "<null>",
259 : expected, val2 ? val2 : "<null>");
260 : isOk = false;
261 :
262 : delete [] val1;
263 : delete [] val2;
264 : }
265 : }
266 : return compare_helper(isOk, msg, Q_NULLPTR, Q_NULLPTR, actual, expected, file, line);
267 : }
268 :
269 : template <>
270 : inline bool qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected,
271 : const char *file, int line)
272 : {
273 : return qCompare<QString>(t1, t2, actual, expected, file, line);
274 : }
275 :
276 : template <typename T>
277 : inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
278 : const char *file, int line)
279 : {
280 : return qCompare(int(t1), int(t2), actual, expected, file, line);
281 : }
282 :
283 : template <typename T>
284 : inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
285 : const char *file, int line)
286 : {
287 : return qCompare(int(t1), t2, actual, expected, file, line);
288 : }
289 :
290 : template<>
291 : inline bool qCompare(qint64 const &t1, qint32 const &t2, const char *actual,
292 : const char *expected, const char *file, int line)
293 : {
294 : return qCompare(t1, static_cast<qint64>(t2), actual, expected, file, line);
295 : }
296 :
297 : template<>
298 : inline bool qCompare(qint64 const &t1, quint32 const &t2, const char *actual,
299 : const char *expected, const char *file, int line)
300 : {
301 : return qCompare(t1, static_cast<qint64>(t2), actual, expected, file, line);
302 : }
303 :
304 : template<>
305 : inline bool qCompare(quint64 const &t1, quint32 const &t2, const char *actual,
306 : const char *expected, const char *file, int line)
307 : {
308 : return qCompare(t1, static_cast<quint64>(t2), actual, expected, file, line);
309 : }
310 :
311 : template<>
312 : inline bool qCompare(qint32 const &t1, qint64 const &t2, const char *actual,
313 : const char *expected, const char *file, int line)
314 : {
315 : return qCompare(static_cast<qint64>(t1), t2, actual, expected, file, line);
316 : }
317 :
318 : template<>
319 : inline bool qCompare(quint32 const &t1, qint64 const &t2, const char *actual,
320 : const char *expected, const char *file, int line)
321 : {
322 : return qCompare(static_cast<qint64>(t1), t2, actual, expected, file, line);
323 : }
324 :
325 : template<>
326 : inline bool qCompare(quint32 const &t1, quint64 const &t2, const char *actual,
327 : const char *expected, const char *file, int line)
328 : {
329 : return qCompare(static_cast<quint64>(t1), t2, actual, expected, file, line);
330 : }
331 :
332 : }
333 : QT_END_NAMESPACE
334 :
335 : #ifdef QT_TESTCASE_BUILDDIR
336 : # define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
337 : #else
338 : # define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
339 : #endif
340 :
341 : #define QTEST_APPLESS_MAIN(TestObject) \
342 : int main(int argc, char *argv[]) \
343 : { \
344 : TestObject tc; \
345 : QTEST_SET_MAIN_SOURCE_PATH \
346 : return QTest::qExec(&tc, argc, argv); \
347 : }
348 :
349 : #include <QtTest/qtestsystem.h>
350 : #include <set>
351 :
352 : #ifndef QT_NO_OPENGL
353 : # define QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
354 : extern Q_TESTLIB_EXPORT std::set<QByteArray> *(*qgpu_features_ptr)(const QString &); \
355 : extern Q_GUI_EXPORT std::set<QByteArray> *qgpu_features(const QString &);
356 : # define QTEST_ADD_GPU_BLACKLIST_SUPPORT \
357 : qgpu_features_ptr = qgpu_features;
358 : #else
359 : # define QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS
360 : # define QTEST_ADD_GPU_BLACKLIST_SUPPORT
361 : #endif
362 :
363 : #if defined(QT_NETWORK_LIB)
364 : # include <QtTest/qtest_network.h>
365 : #endif
366 :
367 : #if defined(QT_WIDGETS_LIB)
368 :
369 : #include <QtTest/qtest_widgets.h>
370 :
371 : #ifdef QT_KEYPAD_NAVIGATION
372 : # define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
373 : #else
374 : # define QTEST_DISABLE_KEYPAD_NAVIGATION
375 : #endif
376 :
377 : #define QTEST_MAIN(TestObject) \
378 : QT_BEGIN_NAMESPACE \
379 : QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
380 : QT_END_NAMESPACE \
381 : int main(int argc, char *argv[]) \
382 : { \
383 : QApplication app(argc, argv); \
384 : app.setAttribute(Qt::AA_Use96Dpi, true); \
385 : QTEST_DISABLE_KEYPAD_NAVIGATION \
386 : QTEST_ADD_GPU_BLACKLIST_SUPPORT \
387 : TestObject tc; \
388 : QTEST_SET_MAIN_SOURCE_PATH \
389 : return QTest::qExec(&tc, argc, argv); \
390 : }
391 :
392 : #elif defined(QT_GUI_LIB)
393 :
394 : #include <QtTest/qtest_gui.h>
395 :
396 : #define QTEST_MAIN(TestObject) \
397 : QT_BEGIN_NAMESPACE \
398 : QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
399 : QT_END_NAMESPACE \
400 : int main(int argc, char *argv[]) \
401 : { \
402 : QGuiApplication app(argc, argv); \
403 : app.setAttribute(Qt::AA_Use96Dpi, true); \
404 : QTEST_ADD_GPU_BLACKLIST_SUPPORT \
405 : TestObject tc; \
406 : QTEST_SET_MAIN_SOURCE_PATH \
407 : return QTest::qExec(&tc, argc, argv); \
408 : }
409 :
410 : #else
411 :
412 : #define QTEST_MAIN(TestObject) \
413 : int main(int argc, char *argv[]) \
414 : { \
415 : QCoreApplication app(argc, argv); \
416 : app.setAttribute(Qt::AA_Use96Dpi, true); \
417 : TestObject tc; \
418 : QTEST_SET_MAIN_SOURCE_PATH \
419 : return QTest::qExec(&tc, argc, argv); \
420 : }
421 :
422 : #endif // QT_GUI_LIB
423 :
424 : #define QTEST_GUILESS_MAIN(TestObject) \
425 : int main(int argc, char *argv[]) \
426 : { \
427 : QCoreApplication app(argc, argv); \
428 : app.setAttribute(Qt::AA_Use96Dpi, true); \
429 : TestObject tc; \
430 : QTEST_SET_MAIN_SOURCE_PATH \
431 : return QTest::qExec(&tc, argc, argv); \
432 : }
433 :
434 : #endif
|