summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qdebug/tst_qdebug.cpp
blob: 400625ba7d91837e7a90467668aa5993368c1b26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0


#include <QtCore/QCoreApplication>
#include <QtCore/QtDebug>

#include <QTest>
#include <QtConcurrentRun>
#include <QFutureSynchronizer>
#include <QVariant>
#include <QSemaphore>
#include <QLine>
#include <QMimeType>
#include <QMimeDatabase>

static_assert(QTypeTraits::has_ostream_operator_v<QDebug, int>);
static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QList<int>>);
static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QMap<int, QString>>);
struct NonStreamable {};
static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, NonStreamable>);
static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, QList<NonStreamable>>);
static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, QMap<int, NonStreamable>>);
struct ConvertsToQVariant {
    operator QVariant() {return QVariant::fromValue(*this);};
};
static_assert(!QTypeTraits::has_ostream_operator_v<QDebug, ConvertsToQVariant>);

#if defined(Q_OS_DARWIN)
#include <objc/runtime.h>
#include <Foundation/Foundation.h>
#endif

class tst_QDebug: public QObject
{
    Q_OBJECT
public:
    enum EnumType { EnumValue1 = 1, EnumValue2 = 2 };
    enum FlagType { EnumFlag1 = 1, EnumFlag2 = 2 };
    Q_ENUM(EnumType)
    Q_DECLARE_FLAGS(Flags, FlagType)
    Q_FLAG(Flags)

private slots:
    void assignment() const;
    void warningWithoutDebug() const;
    void criticalWithoutDebug() const;
    void basics() const;
    void debugWithBool() const;
    void debugSpaceHandling() const;
    void debugNoQuotes() const;
    void verbosity() const;
    void stateSaver() const;
    void veryLongWarningMessage() const;
    void qDebugQChar() const;
    void qDebugQString() const;
    void qDebugQStringView() const;
    void qDebugQUtf8StringView() const;
    void qDebugQLatin1String() const;
    void qDebugQByteArray() const;
    void qDebugQByteArrayView() const;
    void qDebugQFlags() const;
    void textStreamModifiers() const;
    void resetFormat() const;
    void defaultMessagehandler() const;
    void threadSafety() const;
    void toString() const;
    void noQVariantEndlessRecursion() const;
#if defined(Q_OS_DARWIN)
    void objcInCppMode_data() const;
    void objcInCppMode() const;
    void objcInObjcMode_data() const;
    void objcInObjcMode() const;
#endif
};

void tst_QDebug::assignment() const
{
    QDebug debug1(QtDebugMsg);
    QDebug debug2(QtWarningMsg);

    QTest::ignoreMessage(QtDebugMsg, "foo");
    QTest::ignoreMessage(QtWarningMsg, "bar 1 2");

    debug1 << "foo";
    debug2 << "bar";
    debug1 = debug2;
    debug1 << "1";
    debug2 << "2";
}

static QtMsgType s_msgType;
static QString s_msg;
static QByteArray s_file;
static int s_line;
static QByteArray s_function;

static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    s_msg = msg;
    s_msgType = type;
    s_file = context.file;
    s_line = context.line;
    s_function = context.function;
}

// Helper class to ensure that the testlib message handler gets
// restored at the end of each test function, even if the test
// fails or throws an exception.
class MessageHandlerSetter
{
public:
    MessageHandlerSetter(QtMessageHandler newMessageHandler)
        : oldMessageHandler(qInstallMessageHandler(newMessageHandler))
    { }

    ~MessageHandlerSetter()
    {
        qInstallMessageHandler(oldMessageHandler);
    }

private:
    QtMessageHandler oldMessageHandler;
};

/*! \internal
  The qWarning() stream should be usable even if QT_NO_DEBUG is defined.
 */
void tst_QDebug::warningWithoutDebug() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    { qWarning() << "A qWarning() message"; }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtWarningMsg);
    QCOMPARE(s_msg, QString::fromLatin1("A qWarning() message"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);
}

/*! \internal
  The qCritical() stream should be usable even if QT_NO_DEBUG is defined.
 */
void tst_QDebug::criticalWithoutDebug() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    { qCritical() << "A qCritical() message"; }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtCriticalMsg);
    QCOMPARE(s_msg, QString::fromLatin1("A qCritical() message"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);
}

void tst_QDebug::basics() const
{
    // test simple types, without quoting or other modifications
    // (bool tested in the next function)
    MessageHandlerSetter mhs(myMessageHandler);

    qDebug() << 'X';
    QCOMPARE(s_msg, "X");

    qDebug() << 123;
    QCOMPARE(s_msg, "123");

    qDebug() << 456U;
    QCOMPARE(s_msg, "456");

    qDebug() << -123L;
    QCOMPARE(s_msg, "-123");

    qDebug() << 456UL;
    QCOMPARE(s_msg, "456");

    qDebug() << Q_INT64_C(-123);
    QCOMPARE(s_msg, "-123");

    qDebug() << Q_UINT64_C(456);
    QCOMPARE(s_msg, "456");

    qDebug() << "Hello";
    QCOMPARE(s_msg, "Hello");

    qDebug() << u"World";
    QCOMPARE(s_msg, "World");

    qDebug() << (void *)0xfff;
    QCOMPARE(s_msg, "0xfff");

    qDebug() << nullptr;
    QCOMPARE(s_msg, "(nullptr)");
}

void tst_QDebug::debugWithBool() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    { qDebug() << false << true; }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("false true"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);
}

class MyPoint
{
public:
    MyPoint(int val1, int val2)
        : v1(val1), v2(val2) {}
    int v1;
    int v2;
};
QDebug operator<< (QDebug s, const MyPoint& point)
{
    const QDebugStateSaver saver(s);
    s.nospace() << "MyPoint(" << point.v1 << ", " << point.v2 << ")";
    return s;
}

class MyLine
{
public:
    MyLine(const MyPoint& point1, const MyPoint& point2)
        : p1(point1), p2(point2) {}
    MyPoint p1;
    MyPoint p2;
};
QDebug operator<< (QDebug s, const MyLine& line)
{
    const QDebugStateSaver saver(s);
    s.nospace();
    s << "MyLine(" << line.p1 << ", "<< line.p2;
    if (s.verbosity() > 2)
        s << ", Manhattan length=" << (qAbs(line.p2.v1 - line.p1.v1) + qAbs(line.p2.v2 - line.p1.v2));
    s << ')';
    return s;
}

void tst_QDebug::debugSpaceHandling() const
{
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        QVERIFY(d.autoInsertSpaces());
        d.setAutoInsertSpaces(false);
        QVERIFY(!d.autoInsertSpaces());
        d << "  ";
        d.setAutoInsertSpaces(true);
        QVERIFY(d.autoInsertSpaces());
        d << "foo";
        d.nospace();
        d << "key=" << "value";
        d.space();
        d << 1 << 2;
        MyLine line(MyPoint(10, 11), MyPoint (12, 13));
        d << line;
        d << "bar";
        // With the old implementation of MyPoint doing dbg.nospace() << ...; dbg.space() we ended up with
        // MyLine(MyPoint(10, 11) ,  MyPoint(12, 13) )
    }
    QCOMPARE(s_msg, QString::fromLatin1("  foo key=value 1 2 MyLine(MyPoint(10, 11), MyPoint(12, 13)) bar"));

    QVERIFY(qDebug().autoInsertSpaces());
    qDebug() << QPoint(21, 22) << QRect(23, 24, 25, 26) << QLine(27, 28, 29, 30);
    QCOMPARE(s_msg, QString::fromLatin1("QPoint(21,22) QRect(23,24 25x26) QLine(QPoint(27,28),QPoint(29,30))"));
    qDebug() << QPointF(21, 22) << QRectF(23, 24, 25, 26) << QLineF(27, 28, 29, 30);
    QCOMPARE(s_msg, QString::fromLatin1("QPointF(21,22) QRectF(23,24 25x26) QLineF(QPointF(27,28),QPointF(29,30))"));
    qDebug() << QMimeType() << QMimeDatabase().mimeTypeForName("application/pdf") << "foo";
    QCOMPARE(s_msg, QString::fromLatin1("QMimeType(invalid) QMimeType(\"application/pdf\") foo"));
}

void tst_QDebug::debugNoQuotes() const
{
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d << QStringLiteral("Hello");
        d.noquote();
        d << QStringLiteral("Hello");
        d.quote();
        d << QStringLiteral("Hello");
    }
    QCOMPARE(s_msg, QString::fromLatin1("\"Hello\" Hello \"Hello\""));

    {
        QDebug d = qDebug();
        d << QChar('H');
        d << QLatin1String("Hello");
        d << QByteArray("Hello");
        d.noquote();
        d << QChar('H');
        d << QLatin1String("Hello");
        d << QByteArray("Hello");
    }
    QCOMPARE(s_msg, QString::fromLatin1("'H' \"Hello\" \"Hello\" H Hello Hello"));
}

void tst_QDebug::verbosity() const
{
    MyLine line(MyPoint(10, 11), MyPoint (12, 13));
    QString output;
    QDebug d(&output);
    d.nospace();
    d << line << '\n';
    const int oldVerbosity = d.verbosity();
    d.setVerbosity(0);
    QCOMPARE(d.verbosity(), 0);
    d.setVerbosity(7);
    QCOMPARE(d.verbosity(), 7);
    const int newVerbosity = oldVerbosity  + 2;
    d.setVerbosity(newVerbosity);
    QCOMPARE(d.verbosity(), newVerbosity);
    d << line << '\n';
    d.setVerbosity(oldVerbosity );
    QCOMPARE(d.verbosity(), oldVerbosity );
    d << line;
    const QStringList lines = output.split(QLatin1Char('\n'));
    QCOMPARE(lines.size(), 3);
    // Verbose should be longer
    QVERIFY2(lines.at(1).size() > lines.at(0).size(), qPrintable(lines.join(QLatin1Char(','))));
    // Switching back to brief produces same output
    QCOMPARE(lines.at(0).size(), lines.at(2).size());
}

void tst_QDebug::stateSaver() const
{
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d << 42;
        {
            QDebugStateSaver saver(d);
            d << 43;
        }
        d << 44;
    }
    QCOMPARE(s_msg, QString::fromLatin1("42 43 44"));

    {
        QDebug d = qDebug();
        {
            QDebugStateSaver saver(d);
            d.nospace() << Qt::hex << Qt::right << qSetFieldWidth(3) << qSetPadChar('0') << 42;
        }
        d << 42;
    }
    QCOMPARE(s_msg, QString::fromLatin1("02a 42"));

    {
        QDebug d = qDebug();
        {
            QDebugStateSaver saver(d);
            d.nospace().noquote() << QStringLiteral("Hello");
        }
        d << QStringLiteral("World");
    }
    QCOMPARE(s_msg, QString::fromLatin1("Hello \"World\""));

    {
        QDebug d = qDebug();
        d.noquote().nospace() << QStringLiteral("Hello") << Qt::hex << 42;
        {
            QDebugStateSaver saver(d);
            d.resetFormat();
            d << QStringLiteral("World") << 42;
        }
        d << QStringLiteral("!") << 42;
    }
    QCOMPARE(s_msg, QString::fromLatin1("Hello2a\"World\" 42!2a"));
}

void tst_QDebug::veryLongWarningMessage() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    QString test;
    {
        QString part("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n");
        for (int i = 0; i < 1000; ++i)
            test.append(part);
        qWarning("Test output:\n%s\nend", qPrintable(test));
    }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 3; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtWarningMsg);
    QCOMPARE(s_msg, QString::fromLatin1("Test output:\n")+test+QString::fromLatin1("\nend"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);
}

void tst_QDebug::qDebugQChar() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d << QChar('f') << QChar(QLatin1Char('\xE4')); // f, ä
        d.nospace().noquote() << QChar('o') << QChar('o')  << QChar(QLatin1Char('\xC4')); // o, o, Ä
    }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("'f' '\\u00e4' oo\\u00c4"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);

}

void tst_QDebug::qDebugQString() const
{
    /* Use a basic string. */
    {
        QString file, function;
        int line = 0;
        const QString in(QLatin1String("input"));
        const QStringView inRef{ in };

        MessageHandlerSetter mhs(myMessageHandler);
        { qDebug() << inRef; }
#ifndef QT_NO_MESSAGELOGCONTEXT
        file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
        QCOMPARE(s_msgType, QtDebugMsg);
        QCOMPARE(s_msg, QString::fromLatin1("\"input\""));
        QCOMPARE(QString::fromLatin1(s_file), file);
        QCOMPARE(s_line, line);
        QCOMPARE(QString::fromLatin1(s_function), function);
    }

    /* simpler tests from now on */
    MessageHandlerSetter mhs(myMessageHandler);

    QString string = "Hello";
    qDebug() << string;
    QCOMPARE(s_msg, QString("\"Hello\""));

    qDebug().noquote().nospace() << string;
    QCOMPARE(s_msg, string);

    qDebug().noquote().nospace() << qSetFieldWidth(8) << string;
    QCOMPARE(s_msg, "   " + string);

    string = "Sm\xc3\xb8rg\xc3\xa5sbord "                               // Latin script
             "\xce\x91\xce\xb8\xce\xae\xce\xbd\xce\xb1 "                // Greek script
             "\xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0";        // Cyrillic script
    qDebug().noquote().nospace() << string;
    QCOMPARE(s_msg, string);

    // This string only contains printable characters
    qDebug() << string;
    QCOMPARE(s_msg, '"' + string + '"');

    string = "\n\t\\\"";
    qDebug().noquote().nospace() << string;
    QCOMPARE(s_msg, string);

    // This string only contains characters that must be escaped
    qDebug() << string;
    QCOMPARE(s_msg, QString("\"\\n\\t\\\\\\\"\""));

    // Unicode escapes, BMP
    string = "\1"                           // U+0001: START OF HEADING (category Cc)
             "\x7f"                         // U+007F: DELETE (category Cc)
             "\xc2\xad"                     // U+00AD: SOFT HYPHEN (category Cf)
             "\xef\xbb\xbf";                // U+FEFF: ZERO WIDTH NO-BREAK SPACE / BOM (category Cf)
    qDebug() << string;
    QCOMPARE(s_msg, QString("\"\\u0001\\u007F\\u00AD\\uFEFF\""));

    // Unicode printable non-BMP
    string = "\xf0\x90\x80\x80";            // U+10000: LINEAR B SYLLABLE B008 A (category Lo)
    qDebug() << string;
    QCOMPARE(s_msg, '"' + string + '"');

    // non-BMP and non-printable
    string = "\xf3\xa0\x80\x81 "            // U+E0001: LANGUAGE TAG (category Cf)
             "\xf4\x80\x80\x80";            // U+100000: Plane 16 Private Use (category Co)
    qDebug() << string;
    QCOMPARE(s_msg, QString("\"\\U000E0001 \\U00100000\""));

    // broken surrogate pairs
    char16_t utf16[] = { 0xDC00, 0xD800, 'x', 0xD800, 0 };
    string = QString::fromUtf16(utf16);
    qDebug() << string;
    QCOMPARE(s_msg, QString("\"\\uDC00\\uD800x\\uD800\""));
}

void tst_QDebug::qDebugQStringView() const
{
    /* Use a basic string. */
    {
        QLatin1String file, function;
        int line = 0;
        const QStringView inView = u"input";

        MessageHandlerSetter mhs(myMessageHandler);
        { qDebug() << inView; }
#ifndef QT_NO_MESSAGELOGCONTEXT
        file = QLatin1String(__FILE__); line = __LINE__ - 2; function = QLatin1String(Q_FUNC_INFO);
#endif
        QCOMPARE(s_msgType, QtDebugMsg);
        QCOMPARE(s_msg, QLatin1String("\"input\""));
        QCOMPARE(QLatin1String(s_file), file);
        QCOMPARE(s_line, line);
        QCOMPARE(QLatin1String(s_function), function);
    }

    /* Use a null QStringView. */
    {
        QString file, function;
        int line = 0;

        const QStringView inView;

        MessageHandlerSetter mhs(myMessageHandler);
        { qDebug() << inView; }
#ifndef QT_NO_MESSAGELOGCONTEXT
        file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
        QCOMPARE(s_msgType, QtDebugMsg);
        QCOMPARE(s_msg, QLatin1String("\"\""));
        QCOMPARE(QLatin1String(s_file), file);
        QCOMPARE(s_line, line);
        QCOMPARE(QLatin1String(s_function), function);
    }
}

void tst_QDebug::qDebugQUtf8StringView() const
{
    /* Use a utf8 string. */
    {
        QLatin1String file, function;
        int line = 0;
        const QUtf8StringView inView = u8"\U0001F609 is ;-)";

        MessageHandlerSetter mhs(myMessageHandler);
        { qDebug() << inView; }
#ifndef QT_NO_MESSAGELOGCONTEXT
        file = QLatin1String(__FILE__); line = __LINE__ - 2; function = QLatin1String(Q_FUNC_INFO);
#endif
        QCOMPARE(s_msgType, QtDebugMsg);
        QCOMPARE(s_msg, QString::fromUtf8("\"\\xF0\\x9F\\x98\\x89 is ;-)\""));
        QCOMPARE(QLatin1String(s_file), file);
        QCOMPARE(s_line, line);
        QCOMPARE(QLatin1String(s_function), function);
    }

    /* Use a null QUtf8StringView. */
    {
        QString file, function;
        int line = 0;

        const QUtf8StringView inView;

        MessageHandlerSetter mhs(myMessageHandler);
        { qDebug() << inView; }
#ifndef QT_NO_MESSAGELOGCONTEXT
        file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
        QCOMPARE(s_msgType, QtDebugMsg);
        QCOMPARE(s_msg, QLatin1String("\"\""));
        QCOMPARE(QLatin1String(s_file), file);
        QCOMPARE(s_line, line);
        QCOMPARE(QLatin1String(s_function), function);
    }
}

void tst_QDebug::qDebugQLatin1String() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d << QLatin1String("foo") << QLatin1String("") << QLatin1String("barbaz", 3);
        d.nospace().noquote() << QLatin1String("baz");
    }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);

    /* simpler tests from now on */
    QLatin1String string("\"Hello\"");
    qDebug() << string;
    QCOMPARE(s_msg, QString("\"\\\"Hello\\\"\""));

    qDebug().noquote().nospace() << string;
    QCOMPARE(s_msg, QString(string));

    qDebug().noquote().nospace() << qSetFieldWidth(8) << string;
    QCOMPARE(s_msg, " " + QString(string));

    string = QLatin1String("\nSm\xF8rg\xE5sbord\\");
    qDebug().noquote().nospace() << string;
    QCOMPARE(s_msg, QString(string));

    qDebug() << string;
    QCOMPARE(s_msg, QString("\"\\nSm\\u00F8rg\\u00E5sbord\\\\\""));
}

void tst_QDebug::qDebugQByteArray() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d << QByteArrayLiteral("foo") << QByteArrayLiteral("") << QByteArray("barbaz", 3);
        d.nospace().noquote() << QByteArrayLiteral("baz");
    }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);

    /* simpler tests from now on */
    QByteArray ba = "\"Hello\"";
    qDebug() << ba;
    QCOMPARE(s_msg, QString("\"\\\"Hello\\\"\""));

    qDebug().noquote().nospace() << ba;
    QCOMPARE(s_msg, QString::fromLatin1(ba));

    qDebug().noquote().nospace() << qSetFieldWidth(8) << ba;
    QCOMPARE(s_msg, " " + QString::fromLatin1(ba));

    ba = "\nSm\xC3\xB8rg\xC3\xA5sbord\\";
    qDebug().noquote().nospace() << ba;
    QCOMPARE(s_msg, QString::fromUtf8(ba));

    qDebug() << ba;
    QCOMPARE(s_msg, QString("\"\\nSm\\xC3\\xB8rg\\xC3\\xA5sbord\\\\\""));

    // ensure that it closes hex escape sequences correctly
    qDebug() << QByteArray("\377FFFF");
    QCOMPARE(s_msg, QString("\"\\xFF\"\"FFFF\""));
}

void tst_QDebug::qDebugQByteArrayView() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d << QByteArrayView("foo") << QByteArrayView("") << QByteArrayView("barbaz", 3);
        d.nospace().noquote() << QByteArrayView("baz");
    }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);

    /* simpler tests from now on */
    QByteArrayView ba = "\"Hello\"";
    qDebug() << ba;
    QCOMPARE(s_msg, QString("\"\\\"Hello\\\"\""));

    qDebug().noquote().nospace() << ba;
    QCOMPARE(s_msg, QString::fromLatin1(ba));

    qDebug().noquote().nospace() << qSetFieldWidth(8) << ba;
    QCOMPARE(s_msg, " " + QString::fromLatin1(ba));

    ba = "\nSm\xC3\xB8rg\xC3\xA5sbord\\";
    qDebug().noquote().nospace() << ba;
    QCOMPARE(s_msg, QString::fromUtf8(ba));

    qDebug() << ba;
    QCOMPARE(s_msg, QString("\"\\nSm\\xC3\\xB8rg\\xC3\\xA5sbord\\\\\""));

    // ensure that it closes hex escape sequences correctly
    qDebug() << QByteArrayView("\377FFFF");
    QCOMPARE(s_msg, QString("\"\\xFF\"\"FFFF\""));
}

enum TestEnum {
    Flag1 = 0x1,
    Flag2 = 0x10
};

Q_DECLARE_FLAGS(TestFlags, TestEnum)

void tst_QDebug::qDebugQFlags() const
{
    QString file, function;
    int line = 0;
    QFlags<TestEnum> flags(Flag1 | Flag2);

    MessageHandlerSetter mhs(myMessageHandler);
    { qDebug() << flags; }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("QFlags(0x1|0x10)"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);

    // Test the output of QFlags with an enum not declared with Q_DECLARE_FLAGS and Q_FLAGS
    QFlags<EnumType> flags2(EnumValue2);
    qDebug() << flags2;
    QCOMPARE(s_msg, QString::fromLatin1("QFlags<tst_QDebug::EnumType>(EnumValue2)"));

    // A now for one that was fully declared
    tst_QDebug::Flags flags3(EnumFlag1);
    qDebug() << flags3;
    QCOMPARE(s_msg, QString::fromLatin1("QFlags<tst_QDebug::FlagType>(EnumFlag1)"));
}

void tst_QDebug::textStreamModifiers() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    { qDebug() << Qt::hex << short(0xf) << int(0xf) << unsigned(0xf) << long(0xf) << qint64(0xf) << quint64(0xf); }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("f f f f f f"));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);
}

void tst_QDebug::resetFormat() const
{
    QString file, function;
    int line = 0;
    MessageHandlerSetter mhs(myMessageHandler);
    {
        QDebug d = qDebug();
        d.nospace().noquote() << Qt::hex <<  int(0xf);
        d.resetFormat() << int(0xf) << QStringLiteral("foo");
    }
#ifndef QT_NO_MESSAGELOGCONTEXT
    file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
    QCOMPARE(s_msgType, QtDebugMsg);
    QCOMPARE(s_msg, QString::fromLatin1("f15 \"foo\""));
    QCOMPARE(QString::fromLatin1(s_file), file);
    QCOMPARE(s_line, line);
    QCOMPARE(QString::fromLatin1(s_function), function);
}

void tst_QDebug::defaultMessagehandler() const
{
    MessageHandlerSetter mhs(0); // set 0, should set default handler
    QtMessageHandler defaultMessageHandler1 = qInstallMessageHandler((QtMessageHandler)0); // set 0, should set and return default handler
    QVERIFY(defaultMessageHandler1);
    QtMessageHandler defaultMessageHandler2 = qInstallMessageHandler(myMessageHandler); // set myMessageHandler and return default handler
    bool same = (*defaultMessageHandler1 == *defaultMessageHandler2);
    QVERIFY(same);
    QtMessageHandler messageHandler = qInstallMessageHandler((QtMessageHandler)0); // set 0, should set default and return myMessageHandler
    same = (*messageHandler == *myMessageHandler);
    QVERIFY(same);
}

QMutex s_mutex;
QStringList s_messages;
QSemaphore s_sema;

static void threadSafeMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QMutexLocker lock(&s_mutex);
    s_messages.append(msg);
    Q_UNUSED(type);
    Q_UNUSED(context);
}

static void doDebug() // called in each thread
{
    s_sema.acquire();
    qDebug() << "doDebug";
}

void tst_QDebug::threadSafety() const
{
    MessageHandlerSetter mhs(threadSafeMessageHandler);
    const int numThreads = 10;
    QThreadPool::globalInstance()->setMaxThreadCount(numThreads);
    QFutureSynchronizer<void> sync;
    for (int i = 0; i < numThreads; ++i) {
        sync.addFuture(QtConcurrent::run(&doDebug));
    }
    s_sema.release(numThreads);
    sync.waitForFinished();
    QMutexLocker lock(&s_mutex);
    QCOMPARE(s_messages.count(), numThreads);
    for (int i = 0; i < numThreads; ++i) {
        QCOMPARE(s_messages.at(i), QStringLiteral("doDebug"));
    }
}

void tst_QDebug::toString() const
{
    // By reference.
    {
        MyPoint point(3, 4);
        QString expectedString;
        QDebug stream(&expectedString);
        stream.nospace() << point;
        QCOMPARE(QDebug::toString(point), expectedString);
    }

    // By pointer.
    {
        QObject qobject;
        qobject.setObjectName("test");
        QString expectedString;
        QDebug stream(&expectedString);
        stream.nospace() << &qobject;
        QCOMPARE(QDebug::toString(&qobject), expectedString);
    }
}

void tst_QDebug::noQVariantEndlessRecursion() const
{
    ConvertsToQVariant conv;
    QVariant var = QVariant::fromValue(conv);
    QTest::ignoreMessage(QtDebugMsg, "QVariant(ConvertsToQVariant, )");
    qDebug() << var;
}

#if defined(Q_OS_DARWIN)

@interface MyObjcClass : NSObject
@end

@implementation MyObjcClass : NSObject
- (NSString *)description
{
    return @"MyObjcClass is the best";
}
@end

void tst_QDebug::objcInCppMode_data() const
{
    QTest::addColumn<objc_object *>("object");
    QTest::addColumn<QString>("message");

    QTest::newRow("nil") << static_cast<objc_object*>(nullptr) << QString::fromLatin1("(null)");

    // Not an NSObject subclass
    auto *nsproxy = reinterpret_cast<objc_object *>(class_createInstance(objc_getClass("NSProxy"), 0));
    QTest::newRow("NSProxy") << nsproxy << QString::fromLatin1("<NSProxy: 0x%1>").arg(uintptr_t(nsproxy), 1, 16);

    // Plain NSObject
    auto *nsobject = reinterpret_cast<objc_object *>(class_createInstance(objc_getClass("NSObject"), 0));
    QTest::newRow("NSObject") << nsobject << QString::fromLatin1("<NSObject: 0x%1>").arg(uintptr_t(nsobject), 1, 16);

    auto str = QString::fromLatin1("foo");
    QTest::newRow("NSString") << reinterpret_cast<objc_object*>(str.toNSString()) << str;

    // Custom debug description
    QTest::newRow("MyObjcClass") << reinterpret_cast<objc_object*>([[MyObjcClass alloc] init])
                                 << QString::fromLatin1("MyObjcClass is the best");
}

void tst_QDebug::objcInCppMode() const
{
    QFETCH(objc_object *, object);
    QFETCH(QString, message);

    MessageHandlerSetter mhs(myMessageHandler);
    { qDebug() << object; }

    QCOMPARE(s_msg, message);
}

void tst_QDebug::objcInObjcMode_data() const
{
    objcInCppMode_data();
}

void tst_QDebug::objcInObjcMode() const
{
    QFETCH(objc_object *, object);
    QFETCH(QString, message);

    MessageHandlerSetter mhs(myMessageHandler);
    { qDebug() << static_cast<id>(object); }

    QCOMPARE(s_msg, message);
}
#endif

// Should compile: instentiation of unrelated operator<< should not cause cause compilation
// error in QDebug operators (QTBUG-47375)
class TestClassA {};
class TestClassB {};

template <typename T>
TestClassA& operator<< (TestClassA& s, T&) { return s; };
template<> TestClassA& operator<< <TestClassB>(TestClassA& s, TestClassB& l);

QTEST_MAIN(tst_QDebug);
#include "tst_qdebug.moc"