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/qttestglobal.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/qcborcommon.h>
53#include <QtCore/qdatetime.h>
54#if QT_CONFIG(itemmodel)
55#include <QtCore/qabstractitemmodel.h>
56#endif
57#include <QtCore/qobject.h>
58#include <QtCore/qvariant.h>
59#include <QtCore/qurl.h>
60#include <QtCore/quuid.h>
61
62#include <QtCore/qpoint.h>
63#include <QtCore/qsize.h>
64#include <QtCore/qrect.h>
65
66#include <memory>
67
68QT_BEGIN_NAMESPACE
69
70
71namespace QTest
72{
73
74template <> inline char *toString(const QStringView &str)
75{
76 return QTest::toPrettyUnicode(str);
77}
78
79template<> inline char *toString(const QString &str)
80{
81 return toString(QStringView(str));
82}
83
84template<> inline char *toString(const QLatin1String &str)
85{
86 return toString(QString(str));
87}
88
89template<> inline char *toString(const QByteArray &ba)
90{
91 return QTest::toPrettyCString(ba.constData(), ba.length());
92}
93
94#if QT_CONFIG(datestring)
95template<> inline char *toString(const QTime &time)
96{
97 return time.isValid()
98 ? qstrdup(qPrintable(time.toString(u"hh:mm:ss.zzz")))
99 : qstrdup("Invalid QTime");
100}
101
102template<> inline char *toString(const QDate &date)
103{
104 return date.isValid()
105 ? qstrdup(qPrintable(date.toString(u"yyyy/MM/dd")))
106 : qstrdup("Invalid QDate");
107}
108
109template<> inline char *toString(const QDateTime &dateTime)
110{
111 return dateTime.isValid()
112 ? qstrdup(qPrintable(dateTime.toString(u"yyyy/MM/dd hh:mm:ss.zzz[t]")))
113 : qstrdup("Invalid QDateTime");
114}
115#endif // datestring
116
117template<> inline char *toString(const QCborError &c)
118{
119 // use the Q_ENUM formatting
120 return toString(c.c);
121}
122
123template<> inline char *toString(const QChar &c)
124{
125 const ushort uc = c.unicode();
126 if (uc < 128) {
127 char msg[32] = {'\0'};
128 qsnprintf(msg, sizeof(msg), "QChar: '%c' (0x%x)", char(uc), unsigned(uc));
129 return qstrdup(msg);
130 }
131 return qstrdup(qPrintable(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16))));
132}
133
134#if QT_CONFIG(itemmodel)
135template<> inline char *toString(const QModelIndex &idx)
136{
137 char msg[128];
138 qsnprintf(msg, sizeof(msg), "QModelIndex(%d,%d,%p,%p)", idx.row(), idx.column(), idx.internalPointer(), idx.model());
139 return qstrdup(msg);
140}
141#endif
142
143template<> inline char *toString(const QPoint &p)
144{
145 char msg[128] = {'\0'};
146 qsnprintf(msg, sizeof(msg), "QPoint(%d,%d)", p.x(), p.y());
147 return qstrdup(msg);
148}
149
150template<> inline char *toString(const QSize &s)
151{
152 char msg[128] = {'\0'};
153 qsnprintf(msg, sizeof(msg), "QSize(%dx%d)", s.width(), s.height());
154 return qstrdup(msg);
155}
156
157template<> inline char *toString(const QRect &s)
158{
159 char msg[256] = {'\0'};
160 qsnprintf(msg, sizeof(msg), "QRect(%d,%d %dx%d) (bottomright %d,%d)",
161 s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
162 return qstrdup(msg);
163}
164
165template<> inline char *toString(const QPointF &p)
166{
167 char msg[64] = {'\0'};
168 qsnprintf(msg, sizeof(msg), "QPointF(%g,%g)", p.x(), p.y());
169 return qstrdup(msg);
170}
171
172template<> inline char *toString(const QSizeF &s)
173{
174 char msg[64] = {'\0'};
175 qsnprintf(msg, sizeof(msg), "QSizeF(%gx%g)", s.width(), s.height());
176 return qstrdup(msg);
177}
178
179template<> inline char *toString(const QRectF &s)
180{
181 char msg[256] = {'\0'};
182 qsnprintf(msg, sizeof(msg), "QRectF(%g,%g %gx%g) (bottomright %g,%g)",
183 s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
184 return qstrdup(msg);
185}
186
187template<> inline char *toString(const QUrl &uri)
188{
189 if (!uri.isValid())
190 return qstrdup(qPrintable(QLatin1String("Invalid URL: ") + uri.errorString()));
191 return qstrdup(uri.toEncoded().constData());
192}
193
194template <> inline char *toString(const QUuid &uuid)
195{
196 return qstrdup(uuid.toByteArray().constData());
197}
198
199template<> inline char *toString(const QVariant &v)
200{
201 QByteArray vstring("QVariant(");
202 if (v.isValid()) {
203 QByteArray type(v.typeName());
204 if (type.isEmpty()) {
205 type = QByteArray::number(v.userType());
206 }
207 vstring.append(type);
208 if (!v.isNull()) {
209 vstring.append(',');
210 if (v.canConvert(QVariant::String)) {
211 vstring.append(v.toString().toLocal8Bit());
212 }
213 else {
214 vstring.append("<value not representable as string>");
215 }
216 }
217 }
218 vstring.append(')');
219
220 return qstrdup(vstring.constData());
221}
222
223template <typename T1, typename T2>
224inline char *toString(const QPair<T1, T2> &pair)
225{
226 const QScopedArrayPointer<char> first(toString(pair.first));
227 const QScopedArrayPointer<char> second(toString(pair.second));
228 return toString(QString::asprintf("QPair(%s,%s)", first.data(), second.data()));
229}
230
231template <typename T1, typename T2>
232inline char *toString(const std::pair<T1, T2> &pair)
233{
234 const QScopedArrayPointer<char> first(toString(pair.first));
235 const QScopedArrayPointer<char> second(toString(pair.second));
236 return toString(QString::asprintf("std::pair(%s,%s)", first.data(), second.data()));
237}
238
239template <typename Tuple, int... I>
240inline char *toString(const Tuple & tuple, QtPrivate::IndexesList<I...>) {
241 using UP = std::unique_ptr<char[]>;
242 // Generate a table of N + 1 elements where N is the number of
243 // elements in the tuple.
244 // The last element is needed to support the empty tuple use case.
245 const UP data[] = {
246 UP(toString(std::get<I>(tuple)))..., UP{}
247 };
248 return formatString("std::tuple(", ")", sizeof...(I), data[I].get()...);
249}
250
251template <class... Types>
252inline char *toString(const std::tuple<Types...> &tuple)
253{
254 static const std::size_t params_count = sizeof...(Types);
255 return toString(tuple, typename QtPrivate::Indexes<params_count>::Value());
256}
257
258inline char *toString(std::nullptr_t)
259{
260 return toString(QLatin1String("nullptr"));
261}
262
263template<>
264inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
265 const char *expected, const char *file, int line)
266{
267 return qCompare(t1, QString(t2), actual, expected, file, line);
268}
269template<>
270inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
271 const char *expected, const char *file, int line)
272{
273 return qCompare(QString(t1), t2, actual, expected, file, line);
274}
275
276template <typename T>
277inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected,
278 const char *file, int line)
279{
280 char msg[1024];
281 msg[0] = '\0';
282 bool isOk = true;
283 const int actualSize = t1.count();
284 const int expectedSize = t2.count();
285 if (actualSize != expectedSize) {
286 qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n"
287 " Actual (%s) size: %d\n"
288 " Expected (%s) size: %d", actual, actualSize, expected, expectedSize);
289 isOk = false;
290 }
291 for (int i = 0; isOk && i < actualSize; ++i) {
292 if (!(t1.at(i) == t2.at(i))) {
293 char *val1 = toString(t1.at(i));
294 char *val2 = toString(t2.at(i));
295
296 qsnprintf(msg, sizeof(msg), "Compared lists differ at index %d.\n"
297 " Actual (%s): %s\n"
298 " Expected (%s): %s", i, actual, val1 ? val1 : "<null>",
299 expected, val2 ? val2 : "<null>");
300 isOk = false;
301
302 delete [] val1;
303 delete [] val2;
304 }
305 }
306 return compare_helper(isOk, msg, nullptr, nullptr, actual, expected, file, line);
307}
308
309template <>
310inline bool qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected,
311 const char *file, int line)
312{
313 return qCompare<QString>(t1, t2, actual, expected, file, line);
314}
315
316template <typename T>
317inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
318 const char *file, int line)
319{
320 return qCompare(int(t1), int(t2), actual, expected, file, line);
321}
322
323template <typename T>
324inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
325 const char *file, int line)
326{
327 return qCompare(int(t1), t2, actual, expected, file, line);
328}
329
330template<>
331inline bool qCompare(qint64 const &t1, qint32 const &t2, const char *actual,
332 const char *expected, const char *file, int line)
333{
334 return qCompare(t1, static_cast<qint64>(t2), actual, expected, file, line);
335}
336
337template<>
338inline bool qCompare(qint64 const &t1, quint32 const &t2, const char *actual,
339 const char *expected, const char *file, int line)
340{
341 return qCompare(t1, static_cast<qint64>(t2), actual, expected, file, line);
342}
343
344template<>
345inline bool qCompare(quint64 const &t1, quint32 const &t2, const char *actual,
346 const char *expected, const char *file, int line)
347{
348 return qCompare(t1, static_cast<quint64>(t2), actual, expected, file, line);
349}
350
351template<>
352inline bool qCompare(qint32 const &t1, qint64 const &t2, const char *actual,
353 const char *expected, const char *file, int line)
354{
355 return qCompare(static_cast<qint64>(t1), t2, actual, expected, file, line);
356}
357
358template<>
359inline bool qCompare(quint32 const &t1, qint64 const &t2, const char *actual,
360 const char *expected, const char *file, int line)
361{
362 return qCompare(static_cast<qint64>(t1), t2, actual, expected, file, line);
363}
364
365template<>
366inline bool qCompare(quint32 const &t1, quint64 const &t2, const char *actual,
367 const char *expected, const char *file, int line)
368{
369 return qCompare(static_cast<quint64>(t1), t2, actual, expected, file, line);
370}
371
372}
373QT_END_NAMESPACE
374
375#ifdef QT_TESTCASE_BUILDDIR
376# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
377#else
378# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
379#endif
380
381// Hooks for coverage-testing of QTestLib itself:
382#if QT_CONFIG(testlib_selfcover) && defined(__COVERAGESCANNER__)
383struct QtCoverageScanner
384{
385 QtCoverageScanner(const char *name)
386 {
387 __coveragescanner_clear();
388 __coveragescanner_testname(name);
389 }
390 ~QtCoverageScanner()
391 {
392 __coveragescanner_save();
393 __coveragescanner_testname("");
394 }
395};
396#define TESTLIB_SELFCOVERAGE_START(name) QtCoverageScanner _qtCoverageScanner(name);
397#else
398#define TESTLIB_SELFCOVERAGE_START(name)
399#endif
400
401#define QTEST_APPLESS_MAIN(TestObject) \
402int main(int argc, char *argv[]) \
403{ \
404 TESTLIB_SELFCOVERAGE_START(TestObject) \
405 TestObject tc; \
406 QTEST_SET_MAIN_SOURCE_PATH \
407 return QTest::qExec(&tc, argc, argv); \
408}
409
410#include <QtTest/qtestsystem.h>
411
412// Two backwards-compatibility defines for an obsolete feature:
413#define QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS
414#define QTEST_ADD_GPU_BLACKLIST_SUPPORT
415// ### Qt 6: fully remove these.
416
417#if defined(QT_NETWORK_LIB)
418# include <QtTest/qtest_network.h>
419#endif
420
421#if defined(QT_WIDGETS_LIB)
422
423#include <QtTest/qtest_widgets.h>
424
425#ifdef QT_KEYPAD_NAVIGATION
426# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
427#else
428# define QTEST_DISABLE_KEYPAD_NAVIGATION
429#endif
430
431#define QTEST_MAIN_IMPL(TestObject) \
432 TESTLIB_SELFCOVERAGE_START(#TestObject) \
433 QApplication app(argc, argv); \
434 app.setAttribute(Qt::AA_Use96Dpi, true); \
435 QTEST_DISABLE_KEYPAD_NAVIGATION \
436 TestObject tc; \
437 QTEST_SET_MAIN_SOURCE_PATH \
438 return QTest::qExec(&tc, argc, argv);
439
440#elif defined(QT_GUI_LIB)
441
442#include <QtTest/qtest_gui.h>
443
444#define QTEST_MAIN_IMPL(TestObject) \
445 TESTLIB_SELFCOVERAGE_START(#TestObject) \
446 QGuiApplication app(argc, argv); \
447 app.setAttribute(Qt::AA_Use96Dpi, true); \
448 TestObject tc; \
449 QTEST_SET_MAIN_SOURCE_PATH \
450 return QTest::qExec(&tc, argc, argv);
451
452#else
453
454#define QTEST_MAIN_IMPL(TestObject) \
455 TESTLIB_SELFCOVERAGE_START(#TestObject) \
456 QCoreApplication app(argc, argv); \
457 app.setAttribute(Qt::AA_Use96Dpi, true); \
458 TestObject tc; \
459 QTEST_SET_MAIN_SOURCE_PATH \
460 return QTest::qExec(&tc, argc, argv);
461
462#endif // QT_GUI_LIB
463
464#define QTEST_MAIN(TestObject) \
465int main(int argc, char *argv[]) \
466{ \
467 QTEST_MAIN_IMPL(TestObject) \
468}
469
470#define QTEST_GUILESS_MAIN(TestObject) \
471int main(int argc, char *argv[]) \
472{ \
473 TESTLIB_SELFCOVERAGE_START(#TestObject) \
474 QCoreApplication app(argc, argv); \
475 app.setAttribute(Qt::AA_Use96Dpi, true); \
476 TestObject tc; \
477 QTEST_SET_MAIN_SOURCE_PATH \
478 return QTest::qExec(&tc, argc, argv); \
479}
480
481#endif
482