1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Copyright (C) 2020 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/qbitarray.h>
50#include <QtCore/qbytearray.h>
51#include <QtCore/qcborarray.h>
52#include <QtCore/qcborcommon.h>
53#include <QtCore/qcbormap.h>
54#include <QtCore/qcborvalue.h>
55#include <QtCore/qstring.h>
56#include <QtCore/qstringlist.h>
57#include <QtCore/qcborcommon.h>
58#include <QtCore/qdatetime.h>
59#if QT_CONFIG(itemmodel)
60#include <QtCore/qabstractitemmodel.h>
61#endif
62#include <QtCore/qobject.h>
63#include <QtCore/qvariant.h>
64#include <QtCore/qurl.h>
65#include <QtCore/quuid.h>
66
67#if defined(TESTCASE_LOWDPI)
68#include <QtCore/qcoreapplication.h>
69#endif
70
71#include <QtCore/qpoint.h>
72#include <QtCore/qsize.h>
73#include <QtCore/qrect.h>
74
75#include <initializer_list>
76#include <memory>
77
78QT_BEGIN_NAMESPACE
79
80namespace QTest
81{
82
83template <> inline char *toString(const QStringView &str)
84{
85 return QTest::toPrettyUnicode(str);
86}
87
88template<> inline char *toString(const QString &str)
89{
90 return toString(QStringView(str));
91}
92
93template<> inline char *toString(const QLatin1String &str)
94{
95 return toString(QString(str));
96}
97
98template<> inline char *toString(const QByteArray &ba)
99{
100 return QTest::toPrettyCString(ba.constData(), ba.length());
101}
102
103template<> inline char *toString(const QBitArray &ba)
104{
105 qsizetype size = ba.size();
106 char *str = new char[size + 1];
107 for (qsizetype i = 0; i < size; ++i)
108 str[i] = "01"[ba.testBit(i)];
109 str[size] = '\0';
110 return str;
111}
112
113#if QT_CONFIG(datestring)
114template<> inline char *toString(const QTime &time)
115{
116 return time.isValid()
117 ? qstrdup(qPrintable(time.toString(u"hh:mm:ss.zzz")))
118 : qstrdup("Invalid QTime");
119}
120
121template<> inline char *toString(const QDate &date)
122{
123 return date.isValid()
124 ? qstrdup(qPrintable(date.toString(u"yyyy/MM/dd")))
125 : qstrdup("Invalid QDate");
126}
127
128template<> inline char *toString(const QDateTime &dateTime)
129{
130 return dateTime.isValid()
131 ? qstrdup(qPrintable(dateTime.toString(u"yyyy/MM/dd hh:mm:ss.zzz[t]")))
132 : qstrdup("Invalid QDateTime");
133}
134#endif // datestring
135
136template<> inline char *toString(const QCborError &c)
137{
138 // use the Q_ENUM formatting
139 return toString(c.c);
140}
141
142template<> inline char *toString(const QChar &c)
143{
144 const ushort uc = c.unicode();
145 if (uc < 128) {
146 char msg[32] = {'\0'};
147 qsnprintf(msg, sizeof(msg), "QChar: '%c' (0x%x)", char(uc), unsigned(uc));
148 return qstrdup(msg);
149 }
150 return qstrdup(qPrintable(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16))));
151}
152
153#if QT_CONFIG(itemmodel)
154template<> inline char *toString(const QModelIndex &idx)
155{
156 char msg[128];
157 qsnprintf(msg, sizeof(msg), "QModelIndex(%d,%d,%p,%p)", idx.row(), idx.column(), idx.internalPointer(), idx.model());
158 return qstrdup(msg);
159}
160#endif
161
162template<> inline char *toString(const QPoint &p)
163{
164 char msg[128] = {'\0'};
165 qsnprintf(msg, sizeof(msg), "QPoint(%d,%d)", p.x(), p.y());
166 return qstrdup(msg);
167}
168
169template<> inline char *toString(const QSize &s)
170{
171 char msg[128] = {'\0'};
172 qsnprintf(msg, sizeof(msg), "QSize(%dx%d)", s.width(), s.height());
173 return qstrdup(msg);
174}
175
176template<> inline char *toString(const QRect &s)
177{
178 char msg[256] = {'\0'};
179 qsnprintf(msg, sizeof(msg), "QRect(%d,%d %dx%d) (bottomright %d,%d)",
180 s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
181 return qstrdup(msg);
182}
183
184template<> inline char *toString(const QPointF &p)
185{
186 char msg[64] = {'\0'};
187 qsnprintf(msg, sizeof(msg), "QPointF(%g,%g)", p.x(), p.y());
188 return qstrdup(msg);
189}
190
191template<> inline char *toString(const QSizeF &s)
192{
193 char msg[64] = {'\0'};
194 qsnprintf(msg, sizeof(msg), "QSizeF(%gx%g)", s.width(), s.height());
195 return qstrdup(msg);
196}
197
198template<> inline char *toString(const QRectF &s)
199{
200 char msg[256] = {'\0'};
201 qsnprintf(msg, sizeof(msg), "QRectF(%g,%g %gx%g) (bottomright %g,%g)",
202 s.left(), s.top(), s.width(), s.height(), s.right(), s.bottom());
203 return qstrdup(msg);
204}
205
206template<> inline char *toString(const QUrl &uri)
207{
208 if (!uri.isValid())
209 return qstrdup(qPrintable(QLatin1String("Invalid URL: ") + uri.errorString()));
210 return qstrdup(uri.toEncoded().constData());
211}
212
213template <> inline char *toString(const QUuid &uuid)
214{
215 return qstrdup(uuid.toByteArray().constData());
216}
217
218template<> inline char *toString(const QVariant &v)
219{
220 QByteArray vstring("QVariant(");
221 if (v.isValid()) {
222 QByteArray type(v.typeName());
223 if (type.isEmpty()) {
224 type = QByteArray::number(v.userType());
225 }
226 vstring.append(type);
227 if (!v.isNull()) {
228 vstring.append(',');
229 if (v.canConvert<QString>()) {
230 vstring.append(v.toString().toLocal8Bit());
231 }
232 else {
233 vstring.append("<value not representable as string>");
234 }
235 }
236 }
237 vstring.append(')');
238
239 return qstrdup(vstring.constData());
240}
241
242namespace Internal {
243struct QCborValueFormatter
244{
245 enum { BufferLen = 256 };
246 static char *formatSimpleType(QCborSimpleType st)
247 {
248 char *buf = new char[BufferLen];
249 qsnprintf(buf, BufferLen, "QCborValue(QCborSimpleType(%d))", int(st));
250 return buf;
251 }
252
253 static char *formatTag(QCborTag tag, const QCborValue &taggedValue)
254 {
255 QScopedArrayPointer<char> hold(format(taggedValue));
256 char *buf = new char[BufferLen];
257 qsnprintf(buf, BufferLen, "QCborValue(QCborTag(%llu), %s)", tag, hold.get());
258 return buf;
259 }
260
261 static char *innerFormat(QCborValue::Type t, const char *str)
262 {
263 static const QMetaEnum typeEnum = []() {
264 int idx = QCborValue::staticMetaObject.indexOfEnumerator("Type");
265 return QCborValue::staticMetaObject.enumerator(idx);
266 }();
267
268 char *buf = new char[BufferLen];
269 const char *typeName = typeEnum.valueToKey(t);
270 if (typeName)
271 qsnprintf(buf, BufferLen, "QCborValue(%s, %s)", typeName, str);
272 else
273 qsnprintf(buf, BufferLen, "QCborValue(<unknown type 0x%02x>)", t);
274 return buf;
275 }
276
277 template<typename T> static char *format(QCborValue::Type type, const T &t)
278 {
279 QScopedArrayPointer<char> hold(QTest::toString(t));
280 return innerFormat(type, hold.get());
281 }
282
283 static char *format(const QCborValue &v)
284 {
285 switch (v.type()) {
286 case QCborValue::Integer:
287 return format(v.type(), v.toInteger());
288 case QCborValue::ByteArray:
289 return format(v.type(), v.toByteArray());
290 case QCborValue::String:
291 return format(v.type(), v.toString());
292 case QCborValue::Array:
293 return innerFormat(v.type(), QScopedArrayPointer<char>(format(v.toArray())).get());
294 case QCborValue::Map:
295 return innerFormat(v.type(), QScopedArrayPointer<char>(format(v.toMap())).get());
296 case QCborValue::Tag:
297 return formatTag(v.tag(), v.taggedValue());
298 case QCborValue::SimpleType:
299 break;
300 case QCborValue::True:
301 return qstrdup("QCborValue(true)");
302 case QCborValue::False:
303 return qstrdup("QCborValue(false)");
304 case QCborValue::Null:
305 return qstrdup("QCborValue(nullptr)");
306 case QCborValue::Undefined:
307 return qstrdup("QCborValue()");
308 case QCborValue::Double:
309 return format(v.type(), v.toDouble());
310 case QCborValue::DateTime:
311 case QCborValue::Url:
312 case QCborValue::RegularExpression:
313 return format(v.type(), v.taggedValue().toString());
314 case QCborValue::Uuid:
315 return format(v.type(), v.toUuid());
316 case QCborValue::Invalid:
317 return qstrdup("QCborValue(<invalid>)");
318 }
319
320 if (v.isSimpleType())
321 return formatSimpleType(v.toSimpleType());
322 return innerFormat(v.type(), "");
323 }
324
325 static char *format(const QCborArray &a)
326 {
327 QByteArray out(1, '[');
328 const char *comma = "";
329 for (const QCborValueRef v : a) {
330 QScopedArrayPointer<char> s(format(v));
331 out += comma;
332 out += s.get();
333 comma = ", ";
334 }
335 out += ']';
336 return qstrdup(out.constData());
337 }
338
339 static char *format(const QCborMap &m)
340 {
341 QByteArray out(1, '{');
342 const char *comma = "";
343 for (auto pair : m) {
344 QScopedArrayPointer<char> key(format(pair.first));
345 QScopedArrayPointer<char> value(format(pair.second));
346 out += comma;
347 out += key.get();
348 out += ": ";
349 out += value.get();
350 comma = ", ";
351 }
352 out += '}';
353 return qstrdup(out.constData());
354 }
355};
356}
357
358template<> inline char *toString(const QCborValue &v)
359{
360 return Internal::QCborValueFormatter::format(v);
361}
362
363template<> inline char *toString(const QCborValueRef &v)
364{
365 return toString(QCborValue(v));
366}
367
368template<> inline char *toString(const QCborArray &a)
369{
370 return Internal::QCborValueFormatter::format(a);
371}
372
373template<> inline char *toString(const QCborMap &m)
374{
375 return Internal::QCborValueFormatter::format(m);
376}
377
378template <typename T1, typename T2>
379inline char *toString(const std::pair<T1, T2> &pair)
380{
381 const QScopedArrayPointer<char> first(toString(pair.first));
382 const QScopedArrayPointer<char> second(toString(pair.second));
383 return toString(QString::asprintf("std::pair(%s,%s)", first.data(), second.data()));
384}
385
386template <typename Tuple, int... I>
387inline char *toString(const Tuple & tuple, QtPrivate::IndexesList<I...>) {
388 using UP = std::unique_ptr<char[]>;
389 // Generate a table of N + 1 elements where N is the number of
390 // elements in the tuple.
391 // The last element is needed to support the empty tuple use case.
392 const UP data[] = {
393 UP(toString(std::get<I>(tuple)))..., UP{}
394 };
395 return formatString("std::tuple(", ")", sizeof...(I), data[I].get()...);
396}
397
398template <class... Types>
399inline char *toString(const std::tuple<Types...> &tuple)
400{
401 static const std::size_t params_count = sizeof...(Types);
402 return toString(tuple, typename QtPrivate::Indexes<params_count>::Value());
403}
404
405inline char *toString(std::nullptr_t)
406{
407 return toString(QLatin1String("nullptr"));
408}
409
410template<>
411inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
412 const char *expected, const char *file, int line)
413{
414 return qCompare(t1, QString(t2), actual, expected, file, line);
415}
416template<>
417inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
418 const char *expected, const char *file, int line)
419{
420 return qCompare(QString(t1), t2, actual, expected, file, line);
421}
422
423// Compare sequences of equal size
424template <typename ActualIterator, typename ExpectedIterator>
425bool _q_compareSequence(ActualIterator actualIt, ActualIterator actualEnd,
426 ExpectedIterator expectedBegin, ExpectedIterator expectedEnd,
427 const char *actual, const char *expected,
428 const char *file, int line)
429{
430 char msg[1024];
431 msg[0] = '\0';
432
433 const qsizetype actualSize = actualEnd - actualIt;
434 const qsizetype expectedSize = expectedEnd - expectedBegin;
435 bool isOk = actualSize == expectedSize;
436
437 if (!isOk) {
438 qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n"
439 " Actual (%s) size: %zd\n"
440 " Expected (%s) size: %zd", actual, actualSize,
441 expected, expectedSize);
442 }
443
444 for (auto expectedIt = expectedBegin; isOk && expectedIt < expectedEnd; ++actualIt, ++expectedIt) {
445 if (!(*actualIt == *expectedIt)) {
446 const qsizetype i = qsizetype(expectedIt - expectedBegin);
447 char *val1 = toString(*actualIt);
448 char *val2 = toString(*expectedIt);
449
450 qsnprintf(msg, sizeof(msg), "Compared lists differ at index %zd.\n"
451 " Actual (%s): %s\n"
452 " Expected (%s): %s", i, actual, val1 ? val1 : "<null>",
453 expected, val2 ? val2 : "<null>");
454 isOk = false;
455
456 delete [] val1;
457 delete [] val2;
458 }
459 }
460 return compare_helper(isOk, msg, nullptr, nullptr, actual, expected, file, line);
461}
462
463namespace Internal {
464
465#if defined(TESTCASE_LOWDPI)
466void disableHighDpi()
467{
468 qputenv("QT_ENABLE_HIGHDPI_SCALING", "0");
469}
470Q_CONSTRUCTOR_FUNCTION(disableHighDpi);
471#endif
472
473} // namespace Internal
474
475template <typename T>
476inline bool qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected,
477 const char *file, int line)
478{
479 return _q_compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(),
480 actual, expected, file, line);
481}
482
483template <typename T, int N>
484bool qCompare(QList<T> const &t1, std::initializer_list<T> t2,
485 const char *actual, const char *expected,
486 const char *file, int line)
487{
488 return _q_compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(),
489 actual, expected, file, line);
490}
491
492// Compare QList against array
493template <typename T, int N>
494bool qCompare(QList<T> const &t1, const T (& t2)[N],
495 const char *actual, const char *expected,
496 const char *file, int line)
497{
498 return _q_compareSequence(t1.cbegin(), t1.cend(), t2, t2 + N,
499 actual, expected, file, line);
500}
501
502template <typename T>
503inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
504 const char *file, int line)
505{
506 return qCompare(int(t1), int(t2), actual, expected, file, line);
507}
508
509template <typename T>
510inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
511 const char *file, int line)
512{
513 return qCompare(int(t1), t2, actual, expected, file, line);
514}
515
516template<>
517inline bool qCompare(qint64 const &t1, qint32 const &t2, const char *actual,
518 const char *expected, const char *file, int line)
519{
520 return qCompare(t1, static_cast<qint64>(t2), actual, expected, file, line);
521}
522
523template<>
524inline bool qCompare(qint64 const &t1, quint32 const &t2, const char *actual,
525 const char *expected, const char *file, int line)
526{
527 return qCompare(t1, static_cast<qint64>(t2), actual, expected, file, line);
528}
529
530template<>
531inline bool qCompare(quint64 const &t1, quint32 const &t2, const char *actual,
532 const char *expected, const char *file, int line)
533{
534 return qCompare(t1, static_cast<quint64>(t2), actual, expected, file, line);
535}
536
537template<>
538inline bool qCompare(qint32 const &t1, qint64 const &t2, const char *actual,
539 const char *expected, const char *file, int line)
540{
541 return qCompare(static_cast<qint64>(t1), t2, actual, expected, file, line);
542}
543
544template<>
545inline bool qCompare(quint32 const &t1, qint64 const &t2, const char *actual,
546 const char *expected, const char *file, int line)
547{
548 return qCompare(static_cast<qint64>(t1), t2, actual, expected, file, line);
549}
550
551template<>
552inline bool qCompare(quint32 const &t1, quint64 const &t2, const char *actual,
553 const char *expected, const char *file, int line)
554{
555 return qCompare(static_cast<quint64>(t1), t2, actual, expected, file, line);
556}
557namespace Internal {
558
559template <typename T>
560class HasInitMain // SFINAE test for the presence of initMain()
561{
562private:
563 using YesType = char[1];
564 using NoType = char[2];
565
566 template <typename C> static YesType& test( decltype(&C::initMain) ) ;
567 template <typename C> static NoType& test(...);
568
569public:
570 enum { value = sizeof(test<T>(nullptr)) == sizeof(YesType) };
571};
572
573template<typename T>
574typename std::enable_if<HasInitMain<T>::value, void>::type callInitMain()
575{
576 T::initMain();
577}
578
579template<typename T>
580typename std::enable_if<!HasInitMain<T>::value, void>::type callInitMain()
581{
582}
583
584} // namespace Internal
585
586} // namespace QTest
587QT_END_NAMESPACE
588
589#ifdef QT_TESTCASE_BUILDDIR
590# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
591#else
592# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
593#endif
594
595// Hooks for coverage-testing of QTestLib itself:
596#if QT_CONFIG(testlib_selfcover) && defined(__COVERAGESCANNER__)
597struct QtCoverageScanner
598{
599 QtCoverageScanner(const char *name)
600 {
601 __coveragescanner_clear();
602 __coveragescanner_testname(name);
603 }
604 ~QtCoverageScanner()
605 {
606 __coveragescanner_save();
607 __coveragescanner_testname("");
608 }
609};
610#define TESTLIB_SELFCOVERAGE_START(name) QtCoverageScanner _qtCoverageScanner(name);
611#else
612#define TESTLIB_SELFCOVERAGE_START(name)
613#endif
614
615#define QTEST_APPLESS_MAIN(TestObject) \
616int main(int argc, char *argv[]) \
617{ \
618 TESTLIB_SELFCOVERAGE_START(TestObject) \
619 TestObject tc; \
620 QTEST_SET_MAIN_SOURCE_PATH \
621 return QTest::qExec(&tc, argc, argv); \
622}
623
624#include <QtTest/qtestsystem.h>
625
626#if defined(QT_NETWORK_LIB)
627# include <QtTest/qtest_network.h>
628#endif
629
630#if defined(QT_WIDGETS_LIB)
631
632#include <QtTest/qtest_widgets.h>
633
634#ifdef QT_KEYPAD_NAVIGATION
635# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
636#else
637# define QTEST_DISABLE_KEYPAD_NAVIGATION
638#endif
639
640#define QTEST_MAIN_IMPL(TestObject) \
641 TESTLIB_SELFCOVERAGE_START(#TestObject) \
642 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
643 QApplication app(argc, argv); \
644 app.setAttribute(Qt::AA_Use96Dpi, true); \
645 QTEST_DISABLE_KEYPAD_NAVIGATION \
646 TestObject tc; \
647 QTEST_SET_MAIN_SOURCE_PATH \
648 return QTest::qExec(&tc, argc, argv);
649
650#elif defined(QT_GUI_LIB)
651
652#include <QtTest/qtest_gui.h>
653
654#define QTEST_MAIN_IMPL(TestObject) \
655 TESTLIB_SELFCOVERAGE_START(#TestObject) \
656 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
657 QGuiApplication app(argc, argv); \
658 app.setAttribute(Qt::AA_Use96Dpi, true); \
659 TestObject tc; \
660 QTEST_SET_MAIN_SOURCE_PATH \
661 return QTest::qExec(&tc, argc, argv);
662
663#else
664
665#define QTEST_MAIN_IMPL(TestObject) \
666 TESTLIB_SELFCOVERAGE_START(#TestObject) \
667 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
668 QCoreApplication app(argc, argv); \
669 app.setAttribute(Qt::AA_Use96Dpi, true); \
670 TestObject tc; \
671 QTEST_SET_MAIN_SOURCE_PATH \
672 return QTest::qExec(&tc, argc, argv);
673
674#endif // QT_GUI_LIB
675
676#define QTEST_MAIN(TestObject) \
677int main(int argc, char *argv[]) \
678{ \
679 QTEST_MAIN_IMPL(TestObject) \
680}
681
682#define QTEST_GUILESS_MAIN(TestObject) \
683int main(int argc, char *argv[]) \
684{ \
685 TESTLIB_SELFCOVERAGE_START(#TestObject) \
686 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>(); \
687 QCoreApplication app(argc, argv); \
688 app.setAttribute(Qt::AA_Use96Dpi, true); \
689 TestObject tc; \
690 QTEST_SET_MAIN_SOURCE_PATH \
691 return QTest::qExec(&tc, argc, argv); \
692}
693
694#endif
695