tst_dumpers.cpp 88.3 KB
Newer Older
1
2
#include "gdb/gdbmi.h"
#include "gdbmacros.h"
3
#include "gdbmacros_p.h"
4

5
6
7
8
9
10
11
12
#include <QtCore/private/qobject_p.h>

#include <QtGui/QStandardItemModel>
#include <QtGui/QStringListModel>

#include <QtTest/QtTest>
//#include <QtTest/qtest_gui.h>

13
14
15
16
static const char *pointerPrintFormat()
{
    return sizeof(quintptr) == sizeof(long) ? "0x%lx" : "0x%llx";
}
17

18
19
20
21
22
23
24
25
26
#undef NS
#ifdef QT_NAMESPACE
#   define STRINGIFY0(s) #s
#   define STRINGIFY1(s) STRINGIFY0(s)
#   define NS STRINGIFY1(QT_NAMESPACE) "::"
#else
#   define NS ""
#endif

27
28
29
using namespace Debugger;
using namespace Debugger::Internal;

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
static QByteArray operator<<(QByteArray ba, const QByteArray &replacement)
{
    int pos = ba.indexOf('%');
    Q_ASSERT(pos != -1);
    return ba.replace(pos, 1, replacement);
}

static QByteArray &operator<<=(QByteArray &ba, const QByteArray &replacement)
{
    int pos = ba.indexOf('%');
    Q_ASSERT(pos != -1);
    return ba.replace(pos, 1, replacement);
}


template <typename T> 
inline QByteArray N(T t) { return QByteArray::number(t); }

48
static const char gdbmi1[] =
hjk's avatar
hjk committed
49
    "[frame={level=\"0\",addr=\"0x00000000004061ca\","
50
51
52
    "func=\"main\",file=\"test1.cpp\","
    "fullname=\"/home/apoenitz/work/test1/test1.cpp\",line=\"209\"}]";

53
static const char gdbmi2[] =
hjk's avatar
hjk committed
54
    "[frame={level=\"0\",addr=\"0x00002ac058675840\","
55
56
57
    "func=\"QApplication\",file=\"/home/apoenitz/dev/qt/src/gui/kernel/qapplication.cpp\","
    "fullname=\"/home/apoenitz/dev/qt/src/gui/kernel/qapplication.cpp\",line=\"592\"},"
    "frame={level=\"1\",addr=\"0x00000000004061e0\",func=\"main\",file=\"test1.cpp\","
hjk's avatar
hjk committed
58
    "fullname=\"/home/apoenitz/work/test1/test1.cpp\",line=\"209\"}]";
59

60
static const char gdbmi3[] =
hjk's avatar
hjk committed
61
    "[stack={frame={level=\"0\",addr=\"0x00000000004061ca\","
62
    "func=\"main\",file=\"test1.cpp\","
hjk's avatar
hjk committed
63
    "fullname=\"/home/apoenitz/work/test1/test1.cpp\",line=\"209\"}}]";
64

65
static const char gdbmi4[] =
hjk's avatar
hjk committed
66
    "&\"source /home/apoenitz/dev/ide/main/bin/gdb/qt4macros\\n\""
67
68
    "4^done\n";

69
static const char gdbmi5[] =
hjk's avatar
hjk committed
70
    "[reason=\"breakpoint-hit\",bkptno=\"1\",thread-id=\"1\","
71
72
73
    "frame={addr=\"0x0000000000405738\",func=\"main\","
    "args=[{name=\"argc\",value=\"1\"},{name=\"argv\",value=\"0x7fff1ac78f28\"}],"
    "file=\"test1.cpp\",fullname=\"/home/apoenitz/work/test1/test1.cpp\","
hjk's avatar
hjk committed
74
    "line=\"209\"}]";
75

76
static const char gdbmi8[] =
hjk's avatar
hjk committed
77
    "[data={locals={{name=\"a\"},{name=\"w\"}}}]";
78

79
static const char gdbmi9[] =
hjk's avatar
hjk committed
80
    "[data={locals=[name=\"baz\",name=\"urgs\",name=\"purgs\"]}]";
81

82
static const char gdbmi10[] =
hjk's avatar
hjk committed
83
    "[name=\"urgs\",numchild=\"1\",type=\"Urgs\"]";
84

85
static const char gdbmi11[] =
86
87
88
    "[{name=\"size\",value=\"1\",type=\"size_t\",readonly=\"true\"},"
     "{name=\"0\",value=\"one\",type=\"QByteArray\"}]";

89
static const char gdbmi12[] =
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
90
    "[{iname=\"local.hallo\",value=\"\\\"\\\\\\00382\\t\\377\",type=\"QByteArray\","
hjk's avatar
hjk committed
91
     "numchild=\"0\"}]";
92

93

hjk's avatar
hjk committed
94
95
96
97
98
99
100
101
102
103
struct Int3 {
    Int3() { i1 = 42; i2 = 43; i3 = 44; }
    int i1, i2, i3;
};

struct QString3 {
    QString3() { s1 = "a"; s2 = "b"; s3 = "c"; }
    QString s1, s2, s3;
};

104
105
106
107
108
109
110
111
112
113
114
115
116
117
class tst_Debugger : public QObject
{
    Q_OBJECT

public:
    tst_Debugger() {}

    void testMi(const char* input)
    {
        QCOMPARE('\n' + QString::fromLatin1(GdbMi(input).toString(false)),
            '\n' + QString(input));
    }

private slots:
118
119
120
121
122
123
124
125
126
127
128
    void mi1()  { testMi(gdbmi1);  }
    void mi2()  { testMi(gdbmi2);  }
    void mi3()  { testMi(gdbmi3);  }
    //void mi4()  { testMi(gdbmi4);  }
    void mi5()  { testMi(gdbmi5);  }
    void mi8()  { testMi(gdbmi8);  }
    void mi9()  { testMi(gdbmi9);  }
    void mi10() { testMi(gdbmi10); }
    void mi11() { testMi(gdbmi11); }
    //void mi12() { testMi(gdbmi12); }

129
    void infoBreak();
130
131
    void niceType();
    void niceType_data();
132

133
    void dumperCompatibility();
134
    void dumpQAbstractItemAndModelIndex();
ck's avatar
ck committed
135
136
    void dumpQAbstractItemModel();
    void dumpQByteArray();
ck's avatar
ck committed
137
    void dumpQChar();
ck's avatar
ck committed
138
    void dumpQDateTime();
ck's avatar
ck committed
139
    void dumpQDir();
ck's avatar
ck committed
140
    void dumpQFile();
ck's avatar
ck committed
141
    void dumpQFileInfo();
142
    void dumpQHash();
ck's avatar
ck committed
143
    void dumpQHashNode();
ck's avatar
ck committed
144
145
    void dumpQImage();
    void dumpQImageData();
146
    void dumpQLinkedList();
147
148
149
150
151
    void dumpQList_int();
    void dumpQList_char();
    void dumpQList_QString();
    void dumpQList_QString3();
    void dumpQList_Int3();
ck's avatar
ck committed
152
    void dumpQLocale();
ck's avatar
ck committed
153
    void dumpQMap();
154
    void dumpQMapNode();
155
    void dumpQObject();
156
    void dumpQObjectChildList();
ck's avatar
ck committed
157
158
    void dumpQObjectMethodList();
    void dumpQObjectPropertyList();
ck's avatar
ck committed
159
    void dumpQObjectSignal();
160
    void dumpQObjectSignalList();
ck's avatar
ck committed
161
162
    void dumpQObjectSlot();
    void dumpQObjectSlotList();
ck's avatar
ck committed
163
    void dumpQPixmap();
ck's avatar
ck committed
164
    void dumpQSharedPointer();
165
    void dumpQString();
ck's avatar
ck committed
166
    void dumpQTextCodec();
hjk's avatar
hjk committed
167
168
169
    void dumpQVariant_invalid();
    void dumpQVariant_QString();
    void dumpQVariant_QStringList();
170
    void dumpStdVector();
171
    void dumpQWeakPointer();
172
    void initTestCase();
173

hjk's avatar
hjk committed
174
private:
ck's avatar
ck committed
175
    void dumpQAbstractItemHelper(QModelIndex &index);
ck's avatar
ck committed
176
    void dumpQAbstractItemModelHelper(QAbstractItemModel &m);
177
    void dumpQDateTimeHelper(const QDateTime &d);
ck's avatar
ck committed
178
    void dumpQFileHelper(const QString &name, bool exists);
ck's avatar
ck committed
179
    template <typename K, typename V> void dumpQHashNodeHelper(QHash<K, V> &hash);
180
    void dumpQImageHelper(const QImage &img);
ck's avatar
ck committed
181
    void dumpQImageDataHelper(QImage &img);
182
    template <typename T> void dumpQLinkedListHelper(QLinkedList<T> &l);
ck's avatar
ck committed
183
    void dumpQLocaleHelper(QLocale &loc);
ck's avatar
ck committed
184
    template <typename K, typename V> void dumpQMapHelper(QMap<K, V> &m);
185
186
    template <typename K, typename V> void dumpQMapNodeHelper(QMap<K, V> &m);
    void dumpQObjectChildListHelper(QObject &o);
ck's avatar
ck committed
187
    void dumpQObjectSignalHelper(QObject &o, int sigNum);
ck's avatar
ck committed
188
189
#if QT_VERSION >= 0x040500
    template <typename T>
190
    void dumpQSharedPointerHelper(QSharedPointer<T> &ptr);
191
192
    template <typename T>
    void dumpQWeakPointerHelper(QWeakPointer<T> &ptr);
ck's avatar
ck committed
193
#endif
ck's avatar
ck committed
194
    void dumpQTextCodecHelper(QTextCodec *codec);
195
196
};

197
198
void tst_Debugger::infoBreak()
{
ck's avatar
ck committed
199
    // This tests the regular expression used in GdbEngine::extractDataFromInfoBreak
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
    // to discover breakpoints in constructors.

    // Copied from gdbengine.cpp:

    QRegExp re("MULTIPLE.*(0x[0-9a-f]+) in (.*)\\s+at (.*):([\\d]+)([^\\d]|$)");
    re.setMinimal(true);

    QCOMPARE(re.indexIn(
        "2       breakpoint     keep y   <MULTIPLE> 0x0040168e\n"
        "2.1                         y     0x0040168e "
            "in MainWindow::MainWindow(QWidget*) at mainwindow.cpp:7\n"
        "2.2                         y     0x00401792 "
            "in MainWindow::MainWindow(QWidget*) at mainwindow.cpp:7\n"), 33);
    QCOMPARE(re.cap(1), QString("0x0040168e"));
    QCOMPARE(re.cap(2).trimmed(), QString("MainWindow::MainWindow(QWidget*)"));
    QCOMPARE(re.cap(3), QString("mainwindow.cpp"));
    QCOMPARE(re.cap(4), QString("7"));


    QCOMPARE(re.indexIn(
        "Num     Type           Disp Enb Address            What"
        "4       breakpoint     keep y   <MULTIPLE>         0x00000000004066ad"
        "4.1                         y     0x00000000004066ad in CTorTester"
        " at /main/tests/manual/gdbdebugger/simple/app.cpp:124"), 88);

    QCOMPARE(re.cap(1), QString("0x00000000004066ad"));
    QCOMPARE(re.cap(2).trimmed(), QString("CTorTester"));
    QCOMPARE(re.cap(3), QString("/main/tests/manual/gdbdebugger/simple/app.cpp"));
    QCOMPARE(re.cap(4), QString("124"));
}

231
232
233
234
//
// type simplification
//

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
static QString chopConst(QString type)
{
   while (1) {
        if (type.startsWith("const"))
            type = type.mid(5);
        else if (type.startsWith(' '))
            type = type.mid(1);
        else if (type.endsWith("const"))
            type.chop(5);
        else if (type.endsWith(' '))
            type.chop(1);
        else
            break;
    }
    return type;
}

QString niceType(QString type)
{
    type.replace('*', '@');

    for (int i = 0; i < 10; ++i) {
        int start = type.indexOf("std::allocator<");
        if (start == -1)
ck's avatar
ck committed
259
            break;
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
        // search for matching '>'
        int pos;
        int level = 0;
        for (pos = start + 12; pos < type.size(); ++pos) {
            int c = type.at(pos).unicode();
            if (c == '<') {
                ++level;
            } else if (c == '>') {
                --level;
                if (level == 0)
                    break;
            }
        }
        QString alloc = type.mid(start, pos + 1 - start).trimmed();
        QString inner = alloc.mid(15, alloc.size() - 16).trimmed();
        //qDebug() << "MATCH: " << pos << alloc << inner;

        if (inner == QLatin1String("char"))
            // std::string
            type.replace(QLatin1String("basic_string<char, std::char_traits<char>, "
                "std::allocator<char> >"), QLatin1String("string"));
        else if (inner == QLatin1String("wchar_t"))
            // std::wstring
            type.replace(QLatin1String("basic_string<wchar_t, std::char_traits<wchar_t>, "
                "std::allocator<wchar_t> >"), QLatin1String("wstring"));

        // std::vector, std::deque, std::list
        QRegExp re1(QString("(vector|list|deque)<%1, %2\\s*>").arg(inner, alloc));
        if (re1.indexIn(type) != -1)
            type.replace(re1.cap(0), QString("%1<%2>").arg(re1.cap(1), inner));


        // std::stack
        QRegExp re6(QString("stack<%1, std::deque<%2> >").arg(inner, inner));
        re6.setMinimal(true);
        if (re6.indexIn(type) != -1)
            type.replace(re6.cap(0), QString("stack<%1>").arg(inner));

        // std::set
        QRegExp re4(QString("set<%1, std::less<%2>, %3\\s*>").arg(inner, inner, alloc));
        re4.setMinimal(true);
        if (re4.indexIn(type) != -1)
            type.replace(re4.cap(0), QString("set<%1>").arg(inner));


        // std::map
        if (inner.startsWith("std::pair<")) {
            // search for outermost ','
            int pos;
            int level = 0;
            for (pos = 10; pos < inner.size(); ++pos) {
                int c = inner.at(pos).unicode();
                if (c == '<')
                    ++level;
                else if (c == '>')
                    --level;
                else if (c == ',' && level == 0)
                    break;
            }
            QString ckey = inner.mid(10, pos - 10);
            QString key = chopConst(ckey);
            QString value = inner.mid(pos + 2, inner.size() - 3 - pos);

            QRegExp re5(QString("map<%1, %2, std::less<%3>, %4\\s*>")
                .arg(key, value, key, alloc));
            re5.setMinimal(true);
            if (re5.indexIn(type) != -1)
                type.replace(re5.cap(0), QString("map<%1, %2>").arg(key, value));
            else {
                QRegExp re7(QString("map<const %1, %2, std::less<const %3>, %4\\s*>")
                    .arg(key, value, key, alloc));
                re7.setMinimal(true);
                if (re7.indexIn(type) != -1)
                    type.replace(re7.cap(0), QString("map<const %1, %2>").arg(key, value));
            }
        }
    }
    type.replace('@', '*');
    type.replace(QLatin1String(" >"), QString(QLatin1Char('>')));
    return type;
}

void tst_Debugger::niceType()
{
    // cf. watchutils.cpp
    QFETCH(QString, input);
    QFETCH(QString, simplified);
    QCOMPARE(::niceType(input), simplified);
}

void tst_Debugger::niceType_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<QString>("simplified");

    QTest::newRow("list")
        << "std::list<int, std::allocator<int> >"
        << "std::list<int>";

    QTest::newRow("combined")
        << "std::vector<std::list<int, std::allocator<int> >*, "
           "std::allocator<std::list<int, std::allocator<int> >*> >"
        << "std::vector<std::list<int>*>";

    QTest::newRow("stack")
        << "std::stack<int, std::deque<int, std::allocator<int> > >"
        << "std::stack<int>";
ck's avatar
ck committed
367

368
369
370
371
372
373
374
375
376
377
378
    QTest::newRow("map")
        << "std::map<myns::QString, Foo, std::less<myns::QString>, "
           "std::allocator<std::pair<const myns::QString, Foo> > >"
        << "std::map<myns::QString, Foo>";

    QTest::newRow("map2")
        << "std::map<const char*, Foo, std::less<const char*>, "
           "std::allocator<std::pair<const char* const, Foo> > >"
        << "std::map<const char*, Foo>";
}

379
380
381
382
//
// Dumpers
//

383
static void testDumper(QByteArray expected0, const void *data, QByteArray outertype,
384
385
    bool dumpChildren, QByteArray innertype = "", QByteArray exp = "",
    int extraInt0 = 0, int extraInt1 = 0, int extraInt2 = 0, int extraInt3 = 0)
ck's avatar
ck committed
386
387
{
    sprintf(xDumpInBuffer, "%s%c%s%c%s%c%s%c%s%c",
388
389
        outertype.data(), 0, "iname", 0, exp.data(), 0,
        innertype.data(), 0, "iname", 0);
390
    //qDebug() << "FIXME qDumpObjectData440 signature to use const void *";
391
    void *res = qDumpObjectData440(2, 42, data, dumpChildren,
392
393
394
        extraInt0, extraInt1, extraInt2, extraInt3);
    QString expected(expected0);
    char buf[100];
395
    sprintf(buf, pointerPrintFormat(), (quintptr)data);
396
397
    if ((!expected.startsWith('t') && !expected.startsWith('f'))
            || expected.startsWith("type"))
398
399
400
401
402
403
404
405
406
407
408
409
410
        expected = "tiname='$I',addr='$A'," + expected;
    expected.replace("$I", "iname");
    expected.replace("$T", QByteArray(outertype));
    expected.replace("$A", QByteArray(buf));
    expected.replace('\'', '"');
    QString actual____ = QString::fromLatin1(xDumpOutBuffer);
    actual____.replace('\'', '"');
    QCOMPARE(res, xDumpOutBuffer);
    if (actual____ != expected) {
        QStringList l1 = actual____.split(",");
        QStringList l2 = expected.split(",");
        for (int i = 0; i < l1.size() && i < l2.size(); ++i) {
            if (l1.at(i) == l2.at(i))
411
412
413
414
                qWarning() << "== " << l1.at(i);
            else 
                //qWarning() << "!= " << l1.at(i).right(30) << l2.at(i).right(30);
                qWarning() << "!= " << l1.at(i) << l2.at(i);
415
416
        }
        if (l1.size() != l2.size())
417
            qWarning() << "!= size: " << l1.size() << l2.size();
418
419
420
421
422
423
424
    }
    QCOMPARE(actual____, expected);
}

QByteArray str(const void *p)
{
    char buf[100];
425
    sprintf(buf, pointerPrintFormat(), (quintptr)p);
426
427
428
429
430
431
432
433
434
435
    return buf;
}

static const void *deref(const void *p)
{
    return *reinterpret_cast<const char* const*>(p);
}

void tst_Debugger::dumperCompatibility()
{
436
437
438
    // Ensure that no arbitrary padding is introduced by QVectorTypedData.
    const size_t qVectorDataSize = 16;
    QCOMPARE(sizeof(QVectorData), qVectorDataSize);
hjk's avatar
hjk committed
439
440
    QVectorTypedData<int> *v = 0;
    QCOMPARE(size_t(&v->array), qVectorDataSize);
441
442
}

ck's avatar
ck committed
443
444
445
446
447
448
449
450
451
452
static const QByteArray utfToBase64(const QString &string)
{
    return QByteArray(reinterpret_cast<const char *>(string.utf16()), 2 * string.size()).toBase64();
}

static const char *boolToVal(bool b)
{
    return b ? "'true'" : "'false'";
}

ck's avatar
ck committed
453
static const QByteArray ptrToBa(const void *p, bool symbolicNull = true)
ck's avatar
ck committed
454
{
ck's avatar
ck committed
455
    return QByteArray().append(p == 0 && symbolicNull ?
ck's avatar
ck committed
456
        "<null>" :
457
        QByteArray("0x") + QByteArray::number((quintptr) p, 16));
ck's avatar
ck committed
458
459
}

460
static const QByteArray generateQStringSpec(const QString &str)
ck's avatar
ck committed
461
{
462
463
    return QByteArray("value='%',type='"NS"QString',numchild='0',valueencoded='2'")
        << utfToBase64(str);
ck's avatar
ck committed
464
465
}

ck's avatar
ck committed
466
467
static const QByteArray generateQCharSpec(const QChar& ch)
{
468
469
470
    return QByteArray("value='%',valueencoded='2',type='"NS"QChar',numchild='0'")
        << utfToBase64(QString(QLatin1String("'%1' (%2, 0x%3)")).
                   arg(ch).arg(ch.unicode()).arg(ch.unicode(), 0, 16));
ck's avatar
ck committed
471
472
}

ck's avatar
ck committed
473
474
static const QByteArray generateBoolSpec(bool b)
{
475
476
    return QByteArray("value=%,type='bool',numchild='0'")
        << boolToVal(b);
ck's avatar
ck committed
477
478
}

ck's avatar
ck committed
479
480
static const QByteArray generateLongSpec(long n)
{
481
482
    return QByteArray("value='%',type='long',numchild='0'")
        << N(qlonglong(n));
ck's avatar
ck committed
483
484
485
486
}

static const QByteArray generateIntSpec(int n)
{
487
488
    return QByteArray("value='%',type='int',numchild='0'")
        << N(n);
ck's avatar
ck committed
489
490
}

491
492
const QByteArray createExp(const void *ptr,
    const QByteArray &type, const QByteArray &method)
ck's avatar
ck committed
493
{
494
495
    return QByteArray("exp='(("NSX"%"NSY"*)%)->%'")
        << type << ptrToBa(ptr) << method;
ck's avatar
ck committed
496
497
}

ck's avatar
ck committed
498
499
500
501
502
503
504
505
// Helper functions.

#ifdef Q_CC_MSVC
#  define MAP_NODE_TYPE_END ">"
#else
#  define MAP_NODE_TYPE_END " >"
#endif

506
template <typename T> static const char *typeToString()
ck's avatar
ck committed
507
508
509
510
511
512
513
514
515
{
    return "<unknown type>";
}
template <typename T> const QByteArray valToString(const T &)
{
    return "<unknown value>";
}
template <> const QByteArray valToString(const int &n)
{
516
    return QByteArray().append(N(n));
ck's avatar
ck committed
517
518
519
520
521
}
template <> const QByteArray valToString(const QString &s)
{
    return QByteArray(utfToBase64(s)).append("',valueencoded='2");
}
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
template <> const QByteArray valToString(int * const &p)
{
    return ptrToBa(p);
}
template <typename T> const QByteArray derefValToString(const T &v)
{
    return valToString(v);
}
template <> const QByteArray derefValToString(int * const &ptr)
{
    return valToString(*ptr);
}
const QString stripPtrType(const QString &ptrTypeStr)
{
    return ptrTypeStr.mid(0, ptrTypeStr.size() - 2);
}
template <> const char *typeToString<int>()
ck's avatar
ck committed
539
540
541
{
    return "int";
}
542
template <> const char *typeToString<QString>()
ck's avatar
ck committed
543
544
545
{
    return NS"QString";
}
546
547
548
549
550
template <> const char *typeToString<int *>()
{
    return "int *";
}
template <typename T> bool isSimpleType()
ck's avatar
ck committed
551
552
553
{
    return false;
}
554
template <> bool isSimpleType<int>()
ck's avatar
ck committed
555
556
557
{
    return true;
}
558
559
560
561
562
563
564
565
566
567
template <typename T> bool isPointer()
{
    return false;
}
template <> bool isPointer<int *>()
{
    return true;
}

template <typename T> static const char *typeToNumchild()
ck's avatar
ck committed
568
569
570
{
    return "1";
}
571
template <> const char *typeToNumchild<int>()
ck's avatar
ck committed
572
573
574
{
    return "0";
}
575
template <> const char *typeToNumchild<QString>()
ck's avatar
ck committed
576
577
578
{
    return "0";
}
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
template <typename K, typename V>
QByteArray getMapType()
{
    return QByteArray(typeToString<K>()) + "@" + QByteArray(typeToString<V>());
}
template <typename K, typename V>
void getMapNodeParams(size_t &nodeSize, size_t &valOffset)
{
#if QT_VERSION >= 0x040500
    typedef QMapNode<K, V> NodeType;
    NodeType *node = 0;
    nodeSize = sizeof(NodeType);
    valOffset = size_t(&node->value);
#else
    nodeSize = sizeof(K) + sizeof(V) + 2*sizeof(void *);
    valOffset = sizeof(K);
#endif
}
ck's avatar
ck committed
597
598
599
600

void tst_Debugger::dumpQAbstractItemHelper(QModelIndex &index)
{
    const QAbstractItemModel *model = index.model();
601
602
    const QString &rowStr = N(index.row());
    const QString &colStr = N(index.column());
ck's avatar
ck committed
603
604
605
606
607
608
609
610
611
612
    const QByteArray &internalPtrStrSymbolic = ptrToBa(index.internalPointer());
    const QByteArray &internalPtrStrValue = ptrToBa(index.internalPointer(), false);
    const QByteArray &modelPtrStr = ptrToBa(model);
    QByteArray indexSpecSymbolic = QByteArray().append(rowStr + "," + colStr + ",").
        append(internalPtrStrSymbolic + "," + modelPtrStr);
    QByteArray indexSpecValue = QByteArray().append(rowStr + "," + colStr + ",").
        append(internalPtrStrValue + "," + modelPtrStr);
    QByteArray expected = QByteArray("tiname='iname',addr='").append(ptrToBa(&index)).
        append("',type='"NS"QAbstractItem',addr='$").append(indexSpecSymbolic).
        append("',value='").append(valToString(model->data(index).toString())).
613
        append("',numchild='%',children=[");
ck's avatar
ck committed
614
615
    int rowCount = model->rowCount(index);
    int columnCount = model->columnCount(index);
616
    expected <<= N(rowCount * columnCount);
ck's avatar
ck committed
617
618
619
620
    for (int row = 0; row < rowCount; ++row) {
        for (int col = 0; col < columnCount; ++col) {
            const QModelIndex &childIndex = model->index(row, col, index);
            expected.append("{name='[").append(valToString(row)).append(",").
621
                append(N(col)).append("]',numchild='%',addr='$").
622
623
                append(N(childIndex.row())).append(",").
                append(N(childIndex.column())).append(",").
ck's avatar
ck committed
624
625
626
                append(ptrToBa(childIndex.internalPointer())).append(",").
                append(modelPtrStr).append("',type='"NS"QAbstractItem',value='").
                append(valToString(model->data(childIndex).toString())).append("'}");
627
628
            expected <<= N(model->rowCount(childIndex)
                           * model->columnCount(childIndex));
ck's avatar
ck committed
629
630
631
632
633
634
635
636
            if (col < columnCount - 1 || row < rowCount - 1)
                expected.append(",");
        }
    }
    expected.append("]");
    testDumper(expected, &index, NS"QAbstractItem", true, indexSpecValue);
}

637
void tst_Debugger::dumpQAbstractItemAndModelIndex()
ck's avatar
ck committed
638
639
640
641
642
643
644
645
{
    class PseudoTreeItemModel : public QAbstractItemModel
    {
    public:
        PseudoTreeItemModel() : QAbstractItemModel(), parent1(0),
            parent1Child(1), parent2(10), parent2Child1(11), parent2Child2(12)
        {}

hjk's avatar
hjk committed
646
        int columnCount(const QModelIndex &parent = QModelIndex()) const
ck's avatar
ck committed
647
        {
hjk's avatar
hjk committed
648
            Q_UNUSED(parent);
ck's avatar
ck committed
649
650
651
652
653
654
655
656
657
            return 1;
        }

        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
        {
            return !index.isValid() || role != Qt::DisplayRole ?
                    QVariant() : *static_cast<int *>(index.internalPointer());
        }

hjk's avatar
hjk committed
658
        QModelIndex index(int row, int column,
ck's avatar
ck committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
                          const QModelIndex & parent = QModelIndex()) const
        {
            QModelIndex index;
            if (column == 0) {
                if (!parent.isValid()) {
                    if (row == 0)
                        index = createIndex(row, column, &parent1);
                    else if (row == 1)
                        index = createIndex(row, column, &parent2);
                } else if (parent.internalPointer() == &parent1 && row == 0) {
                    index = createIndex(row, column, &parent1Child);
                } else if (parent.internalPointer() == &parent2) {
                    index = createIndex(row, column,
                                row == 0 ? &parent2Child1 : &parent2Child2);
                }
            }
            return index;
        }

hjk's avatar
hjk committed
678
        QModelIndex parent(const QModelIndex & index) const
ck's avatar
ck committed
679
680
681
682
683
684
685
686
687
688
689
690
        {
            QModelIndex parent;
            if (index.isValid()) {
                if (index.internalPointer() == &parent1Child)
                    parent = createIndex(0, 0, &parent1);
                else if (index.internalPointer() == &parent2Child1 ||
                         index.internalPointer() == &parent2Child2)
                    parent = createIndex(1, 0, &parent2);
            }
            return parent;
        }

hjk's avatar
hjk committed
691
        int rowCount(const QModelIndex &parent = QModelIndex()) const
ck's avatar
ck committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
        {
            int rowCount;
            if (!parent.isValid() || parent.internalPointer() == &parent2)
                rowCount = 2;
            else if (parent.internalPointer() == &parent1)
                rowCount = 1;
            else
                rowCount = 0;
            return rowCount;
        }

    private:
        mutable int parent1;
        mutable int parent1Child;
        mutable int parent2;
        mutable int parent2Child1;
        mutable int parent2Child2;
hjk's avatar
hjk committed
709
710
711
    };

    PseudoTreeItemModel m2;
ck's avatar
ck committed
712

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
    // Case 1: ModelIndex with no children.
    QStringListModel m(QStringList() << "item1" << "item2" << "item3");
    QModelIndex index = m.index(2, 0);

    testDumper(QByteArray("type='$T',value='(2, 0)',numchild='5',children=["
        "{name='row',value='2',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='<invalid>',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index.internalId()))
         << ptrToBa(&m),
        &index, NS"QModelIndex", true);

ck's avatar
ck committed
729
730
731
    // Case 2: ModelIndex with one child.
    QModelIndex index2 = m2.index(0, 0);
    dumpQAbstractItemHelper(index2);
732
733
734
735
736
737
738
739
740
741
742
743
744
745

    qDebug() << "FIXME: invalid indices should not have children";
    testDumper(QByteArray("type='$T',value='(0, 0)',numchild='5',children=["
        "{name='row',value='0',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='<invalid>',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index2.internalId()))
         << ptrToBa(&m2),
        &index2, NS"QModelIndex", true);

ck's avatar
ck committed
746
747
748
749

    // Case 3: ModelIndex with two children.
    QModelIndex index3 = m2.index(1, 0);
    dumpQAbstractItemHelper(index3);
750
751
752
753
754
755
756
757
758
759
760
761
762

    testDumper(QByteArray("type='$T',value='(1, 0)',numchild='5',children=["
        "{name='row',value='1',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='<invalid>',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index3.internalId()))
         << ptrToBa(&m2),
        &index3, NS"QModelIndex", true);

763
764
765

    // Case 4: ModelIndex with a parent.
    index = m2.index(0, 0, index3);
766
767
768
769
770
771
772
773
774
775
776
777
    testDumper(QByteArray("type='$T',value='(0, 0)',numchild='5',children=["
        "{name='row',value='0',type='int',numchild='0'},"
        "{name='column',value='0',type='int',numchild='0'},"
        "{name='parent',value='(1, 0)',exp='(('$T'*)$A)->parent()',"
            "type='$T',numchild='1'},"
        "{name='internalId',%},"
        "{name='model',value='%',type='"NS"QAbstractItemModel*',"
            "numchild='1'}]")
         << generateQStringSpec(N(index.internalId()))
         << ptrToBa(&m2),
        &index, NS"QModelIndex", true);

778
779
780

    // Case 5: Empty ModelIndex
    QModelIndex index4;
781
782
    testDumper("type='$T',value='<invalid>',numchild='0'",
        &index4, NS"QModelIndex", true);
ck's avatar
ck committed
783
784
}

ck's avatar
ck committed
785
void tst_Debugger::dumpQAbstractItemModelHelper(QAbstractItemModel &m)
ck's avatar
ck committed
786
{
787
788
789
790
791
792
793
794
795
796
797
798
    QByteArray address = ptrToBa(&m);
    QByteArray expected = QByteArray("tiname='iname',addr='%',"
        "type='"NS"QAbstractItemModel',value='(%,%)',numchild='1',children=["
        "{numchild='1',name='"NS"QObject',addr='%',value='%',"
        "valueencoded='2',type='"NS"QObject',displayedtype='%'}")
            << address
            << N(m.rowCount())
            << N(m.columnCount())
            << address
            << utfToBase64(m.objectName())
            << m.metaObject()->className();

ck's avatar
ck committed
799
800
801
    for (int row = 0; row < m.rowCount(); ++row) {
        for (int column = 0; column < m.columnCount(); ++column) {
            QModelIndex mi = m.index(row, column);
802
            expected.append(QByteArray(",{name='[%,%]',value='%',"
803
                "valueencoded='2',numchild='%',addr='$%,%,%,%',"
804
805
806
807
                "type='"NS"QAbstractItem'}")
                << N(row)
                << N(column)
                << utfToBase64(m.data(mi).toString())
808
                << N(row * column)
809
810
811
812
                << N(mi.row())
                << N(mi.column())
                << ptrToBa(mi.internalPointer())
                << ptrToBa(mi.model()));
ck's avatar
ck committed
813
814
815
816
817
818
        }
    }
    expected.append("]");
    testDumper(expected, &m, NS"QAbstractItemModel", true);
}

ck's avatar
ck committed
819
void tst_Debugger::dumpQAbstractItemModel()
ck's avatar
ck committed
820
821
822
823
{
    // Case 1: No rows, one column.
    QStringList strList;
    QStringListModel model(strList);
ck's avatar
ck committed
824
    dumpQAbstractItemModelHelper(model);
ck's avatar
ck committed
825
826
827
828

    // Case 2: One row, one column.
    strList << "String 1";
    model.setStringList(strList);
ck's avatar
ck committed
829
    dumpQAbstractItemModelHelper(model);
ck's avatar
ck committed
830
831
832
833

    // Case 3: Two rows, one column.
    strList << "String 2";
    model.setStringList(strList);
ck's avatar
ck committed
834
    dumpQAbstractItemModelHelper(model);
ck's avatar
ck committed
835
836
837

    // Case 4: No rows, two columns.
    QStandardItemModel model2(0, 2);
ck's avatar
ck committed
838
    dumpQAbstractItemModelHelper(model2);
ck's avatar
ck committed
839
840
841
842
843

    // Case 5: One row, two columns.
    QStandardItem item1("Item (0,0)");
    QStandardItem item2("(Item (0,1)");
    model2.appendRow(QList<QStandardItem *>() << &item1 << &item2);
ck's avatar
ck committed
844
    dumpQAbstractItemModelHelper(model2);
ck's avatar
ck committed
845
846
847
848
849

    // Case 6: Two rows, two columns
    QStandardItem item3("Item (1,0");
    QStandardItem item4("Item (1,1)");
    model2.appendRow(QList<QStandardItem *>() << &item3 << &item4);
ck's avatar
ck committed
850
851
852
853
854
855
856
    dumpQAbstractItemModelHelper(model);
}

void tst_Debugger::dumpQByteArray()
{
    // Case 1: Empty object.
    QByteArray ba;
857
858
859
    testDumper("value='',valueencoded='1',type='"NS"QByteArray',numchild='0',"
            "childtype='char',childnumchild='0',children=[]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
860
861
862

    // Case 2: One element.
    ba.append('a');
863
864
865
    testDumper("value='YQ==',valueencoded='1',type='"NS"QByteArray',numchild='1',"
            "childtype='char',childnumchild='0',children=[{value='61  (97 'a')'}]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
866
867
868

    // Case 3: Two elements.
    ba.append('b');
869
870
871
872
    testDumper("value='YWI=',valueencoded='1',type='"NS"QByteArray',numchild='2',"
            "childtype='char',childnumchild='0',children=["
            "{value='61  (97 'a')'},{value='62  (98 'b')'}]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
873
874
875

    // Case 4: > 100 elements.
    ba = QByteArray(101, 'a');
876
877
878
879
880
881
882
883
884
885
    QByteArray children;
    for (int i = 0; i < 101; i++)
        children.append("{value='61  (97 'a')'},");
    children.chop(1);
    testDumper(QByteArray("value='YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh"
            "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh"
            "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ== <size: 101, cut...>',"
            "valueencoded='1',type='"NS"QByteArray',numchild='101',"
            "childtype='char',childnumchild='0',children=[%]") << children,
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
886
887

    // Case 5: Regular and special characters and the replacement character.
888
    ba = QByteArray("abc\a\n\r\033\'\"?");
889
890
891
892
893
894
895
896
    testDumper("value='YWJjBwoNGyciPw==',valueencoded='1',type='"NS"QByteArray',"
            "numchild='10',childtype='char',childnumchild='0',children=["
            "{value='61  (97 'a')'},{value='62  (98 'b')'},"
            "{value='63  (99 'c')'},{value='07  (7 '?')'},"
            "{value='0a  (10 '?')'},{value='0d  (13 '?')'},"
            "{value='1b  (27 '?')'},{value='27  (39 '?')'},"
            "{value='22  (34 '?')'},{value='3f  (63 '?')'}]",
        &ba, NS"QByteArray", true);
ck's avatar
ck committed
897
898
899
900
901
902
}

void tst_Debugger::dumpQChar()
{
    // Case 1: Printable ASCII character.
    QChar c('X');
903
904
    testDumper("value=''X', ucs=88',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
905
906
907

    // Case 2: Printable non-ASCII character.
    c = QChar(0x600);
908
909
    testDumper("value=''?', ucs=1536',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
910
911
912

    // Case 3: Non-printable ASCII character.
    c = QChar::fromAscii('\a');
913
914
    testDumper("value=''?', ucs=7',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
915
916
917

    // Case 4: Non-printable non-ASCII character.
    c = QChar(0x9f);
918
919
    testDumper("value=''?', ucs=159',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
920
921
922

    // Case 5: Printable ASCII Character that looks like the replacement character.
    c = QChar::fromAscii('?');
923
924
    testDumper("value=''?', ucs=63',numchild='0'",
        &c, NS"QChar", false);
ck's avatar
ck committed
925
926
}

927
void tst_Debugger::dumpQDateTimeHelper(const QDateTime &d)
ck's avatar
ck committed
928
{
929
    QByteArray value;
ck's avatar
ck committed
930
    if (d.isNull())
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
        value = "value='(null)'";
    else 
        value = QByteArray("value='%',valueencoded='2'")
            << utfToBase64(d.toString());

    QByteArray expected = QByteArray("%,type='$T',numchild='3',children=["
        "{name='isNull',%},"
        "{name='toTime_t',%},"
        "{name='toString',%},"
        "{name='toString_(ISO)',%},"
        "{name='toString_(SystemLocale)',%},"
        "{name='toString_(Locale)',%}]")
            << value
            << generateBoolSpec(d.isNull())
            << generateLongSpec((d.toTime_t()))
            << generateQStringSpec(d.toString())
            << generateQStringSpec(d.toString(Qt::ISODate))
            << generateQStringSpec(d.toString(Qt::SystemLocaleDate))
            << generateQStringSpec(d.toString(Qt::LocaleDate));
ck's avatar
ck committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
    testDumper(expected, &d, NS"QDateTime", true);
}

void tst_Debugger::dumpQDateTime()
{
    // Case 1: Null object.
    QDateTime d;
    dumpQDateTimeHelper(d);

    // Case 2: Non-null object.
    d = QDateTime::currentDateTime();
    dumpQDateTimeHelper(d);
}

ck's avatar
ck committed
964
965
966
967
void tst_Debugger::dumpQDir()
{
    // Case 1: Current working directory.
    QDir dir = QDir::current();
968
969
970
971
972
973
    testDumper(QByteArray("value='%',valueencoded='2',type='"NS"QDir',numchild='3',"
        "children=[{name='absolutePath',%},{name='canonicalPath',%}]")
            << utfToBase64(dir.absolutePath())
            << generateQStringSpec(dir.absolutePath())
            << generateQStringSpec(dir.canonicalPath()),
        &dir, NS"QDir", true);
ck's avatar
ck committed
974
975
976

    // Case 2: Root directory.
    dir = QDir::root();
977
978
979
980
981
982
    testDumper(QByteArray("value='%',valueencoded='2',type='"NS"QDir',numchild='3',"
        "children=[{name='absolutePath',%},{name='canonicalPath',%}]")
            << utfToBase64(dir.absolutePath())
            << generateQStringSpec(dir.absolutePath())
            << generateQStringSpec(dir.canonicalPath()),
        &dir, NS"QDir", true);
ck's avatar
ck committed
983
984
}

ck's avatar
ck committed
985
986
987
void tst_Debugger::dumpQFileHelper(const QString &name, bool exists)
{
    QFile file(name);
ck's avatar
ck committed
988
    QByteArray filenameAsBase64 = utfToBase64(name);
989
990
991
992
993
994
    testDumper(QByteArray("value='%',valueencoded='2',type='$T',numchild='2',"
        "children=[{name='fileName',value='%',type='"NS"QString',"
        "numchild='0',valueencoded='2'},"
        "{name='exists',value=%,type='bool',numchild='0'}]")
            << filenameAsBase64 << filenameAsBase64 << boolToVal(exists),
        &file, NS"QFile", true);
ck's avatar
ck committed
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
}

void tst_Debugger::dumpQFile()
{
    // Case 1: Empty file name => Does not exist.
    dumpQFileHelper("", false);

    // Case 2: File that is known to exist.
    QTemporaryFile file;
    file.open();
    dumpQFileHelper(file.fileName(), true);

    // Case 3: File with a name that most likely does not exist.
    dumpQFileHelper("jfjfdskjdflsdfjfdls", false);
}

ck's avatar
ck committed
1011
1012
1013
void tst_Debugger::dumpQFileInfo()
{
    QFileInfo fi(".");
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
    QByteArray expected("value='%',valueencoded='2',type='$T',numchild='3',"
        "children=["
        "{name='absolutePath',%},"
        "{name='absoluteFilePath',%},"
        "{name='canonicalPath',%},"
        "{name='canonicalFilePath',%},"
        "{name='completeBaseName',%},"
        "{name='completeSuffix',%},"
        "{name='baseName',%},"
#ifdef QX
        "{name='isBundle',%},"
        "{name='bundleName',%},"
#endif
        "{name='fileName',%},"
        "{name='filePath',%},"
        "{name='group',%},"
        "{name='owner',%},"
        "{name='path',%},"
        "{name='groupid',%},"
        "{name='ownerid',%},"
ck's avatar
ck committed
1034
1035
1036
1037
1038
1039
        "{name='permissions',value=' ',type='%',numchild='10',"
        "children=[{name='ReadOwner',%},{name='WriteOwner',%},"
        "{name='ExeOwner',%},{name='ReadUser',%},{name='WriteUser',%},"
        "{name='ExeUser',%},{name='ReadGroup',%},{name='WriteGroup',%},"
        "{name='ExeGroup',%},{name='ReadOther',%},{name='WriteOther',%},"
        "{name='ExeOther',%}]},"
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
        "{name='caching',%},"
        "{name='exists',%},"
        "{name='isAbsolute',%},"
        "{name='isDir',%},"
        "{name='isExecutable',%},"
        "{name='isFile',%},"
        "{name='isHidden',%},"
        "{name='isReadable',%},"
        "{name='isRelative',%},"
        "{name='isRoot',%},"
        "{name='isSymLink',%},"
        "{name='isWritable',%},"
        "{name='created',value='%',valueencoded='2',%,"
            "type='"NS"QDateTime',numchild='1'},"
        "{name='lastModified',value='%',valueencoded='2',%,"
            "type='"NS"QDateTime',numchild='1'},"
        "{name='lastRead',value='%',valueencoded='2',%,"
            "type='"NS"QDateTime',numchild='1'}]");
    expected <<= utfToBase64(fi.filePath());
    expected <<= generateQStringSpec(fi.absolutePath());
    expected <<= generateQStringSpec(fi.absoluteFilePath());
    expected <<= generateQStringSpec(fi.canonicalPath());
    expected <<= generateQStringSpec(fi.canonicalFilePath());
    expected <<= generateQStringSpec(fi.completeBaseName());
    expected <<= generateQStringSpec(fi.completeSuffix());
    expected <<= generateQStringSpec(fi.baseName());
ck's avatar
ck committed
1066
#ifdef Q_OS_MACX
1067
1068
    expected <<= generateBoolSpec(fi.isBundle());
    expected <<= generateQStringSpec(fi.bundleName());
ck's avatar
ck committed
1069
#endif
1070
1071
1072
1073
1074
1075
1076
    expected <<= generateQStringSpec(fi.fileName());
    expected <<= generateQStringSpec(fi.filePath());
    expected <<= generateQStringSpec(fi.group());
    expected <<= generateQStringSpec(fi.owner());
    expected <<= generateQStringSpec(fi.path());
    expected <<= generateLongSpec(fi.groupId());
    expected <<= generateLongSpec(fi.ownerId());
ck's avatar
ck committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    QFile::Permissions perms = fi.permissions();
    expected <<= QByteArray(NS"QFile::Permissions");
    expected <<= generateBoolSpec((perms & QFile::ReadOwner) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteOwner) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeOwner) != 0);
    expected <<= generateBoolSpec((perms & QFile::ReadUser) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteUser) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeUser) != 0);
    expected <<= generateBoolSpec((perms & QFile::ReadGroup) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteGroup) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeGroup) != 0);
    expected <<= generateBoolSpec((perms & QFile::ReadOther) != 0);
    expected <<= generateBoolSpec((perms & QFile::WriteOther) != 0);
    expected <<= generateBoolSpec((perms & QFile::ExeOther) != 0);
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
    expected <<= generateBoolSpec(fi.caching());
    expected <<= generateBoolSpec(fi.exists());
    expected <<= generateBoolSpec(fi.isAbsolute());
    expected <<= generateBoolSpec(fi.isDir());
    expected <<= generateBoolSpec(fi.isExecutable());
    expected <<= generateBoolSpec(fi.isFile());
    expected <<= generateBoolSpec(fi.isHidden());
    expected <<= generateBoolSpec(fi.isReadable());
    expected <<= generateBoolSpec(fi.isRelative());
    expected <<= generateBoolSpec(fi.isRoot());
    expected <<= generateBoolSpec(fi.isSymLink());
    expected <<= generateBoolSpec(fi.isWritable());
    expected <<= utfToBase64(fi.created().toString());
    expected <<= createExp(&fi, "QFileInfo", "created()");
    expected <<= utfToBase64(fi.lastModified().toString());
    expected <<= createExp(&fi, "QFileInfo", "lastModified()");
    expected <<= utfToBase64(fi.lastRead().toString());
    expected <<= createExp(&fi, "QFileInfo", "lastRead()");

ck's avatar
ck committed
1110
    testDumper(expected, &fi, NS"QFileInfo", true);
ck's avatar
ck committed
1111
1112
}

1113
1114
1115
1116
1117
1118
1119
1120
1121
void tst_Debugger::dumpQHash()
{
    QHash<QString, QList<int> > hash;
    hash.insert("Hallo", QList<int>());
    hash.insert("Welt", QList<int>() << 1);
    hash.insert("!", QList<int>() << 1 << 2);
    hash.insert("!", QList<int>() << 1 << 2);
}

ck's avatar
ck committed
1122
template <typename K, typename V>
hjk's avatar
hjk committed
1123
void tst_Debugger::dumpQHashNodeHelper(QHash<K, V> &hash)
ck's avatar
ck committed
1124
1125
1126
{
    typename QHash<K, V>::iterator it = hash.begin();
    typedef QHashNode<K, V> HashNode;
hjk's avatar
hjk committed
1127
    HashNode *dummy = 0;
ck's avatar
ck committed
1128
1129
    HashNode *node =
        reinterpret_cast<HashNode *>(reinterpret_cast<char *>(const_cast<K *>(&it.key())) -
hjk's avatar
hjk committed
1130
            size_t(&dummy->key));
ck's avatar
ck committed
1131
1132
1133
    const K &key = it.key();
    const V &val = it.value();
    QByteArray expected("value='");
1134
    if (isSimpleType<V>())
ck's avatar
ck committed
1135
1136
        expected.append(valToString(val));
    expected.append("',numchild='2',children=[{name='key',type='").
1137
1138
        append(typeToString<K>()).append("',addr='").append(ptrToBa(&key)).
        append("'},{name='value',type='").append(typeToString<V>()).
ck's avatar
ck committed
1139
1140
        append("',addr='").append(ptrToBa(&val)).append("'}]");
    testDumper(expected, node, NS"QHashNode", true,
1141
        getMapType<K, V>(), "", sizeof(it.key()), sizeof(it.value()));
ck's avatar
ck committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
}

void tst_Debugger::dumpQHashNode()
{
    // Case 1: simple type -> simple type.
    QHash<int, int> hash1;
    hash1[2] = 3;
    dumpQHashNodeHelper(hash1);

    // Case 2: simple type -> composite type.
    QHash<int, QString> hash2;
    hash2[5] = "String 7";
    dumpQHashNodeHelper(hash2);

    // Case 3: composite type -> simple type
    QHash<QString, int> hash3;
    hash3["String 11"] = 13;
    dumpQHashNodeHelper(hash3);

    // Case 4: composite type -> composite type
    QHash<QString, QString> hash4;
    hash4["String 17"] = "String 19";
    dumpQHashNodeHelper(hash4);
}

1167
void tst_Debugger::dumpQImageHelper(const QImage &img)
ck's avatar
ck committed
1168
{
1169
1170
1171
1172
1173
    QByteArray expected = "value='(%x%)',type='"NS"QImage',numchild='1',"
        "children=[{name='data',type='"NS"QImageData',addr='%'}]"
            << N(img.width())
            << N(img.height())
            << ptrToBa(&img);
ck's avatar
ck committed
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
    testDumper(expected, &img, NS"QImage", true);
}

void tst_Debugger::dumpQImage()
{
    // Case 1: Null image.
    QImage img;
    dumpQImageHelper(img);

    // Case 2: Normal image.
    img = QImage(3, 700, QImage::Format_RGB555);
    dumpQImageHelper(img);

    // Case 3: Invalid image.
    img = QImage(100, 0, QImage::Format_Invalid);
    dumpQImageHelper(img);
}

void tst_Debugger::dumpQImageDataHelper(QImage &img)
{
    const QByteArray ba(QByteArray::fromRawData((const char*) img.bits(), img.numBytes()));
    QByteArray expected = QByteArray("tiname='$I',addr='$A',type='"NS"QImageData',").
        append("numchild='0',value='<hover here>',valuetooltipencoded='1',").
1197
        append("valuetooltipsize='").append(N(ba.size())).append("',").
ck's avatar
ck committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
        append("valuetooltip='").append(ba.toBase64()).append("'");
    testDumper(expected, &img, NS"QImageData", false);
}

void tst_Debugger::dumpQImageData()
{
    // Case 1: Null image.
    QImage img;
    dumpQImageDataHelper(img);

    // Case 2: Normal image.
    img = QImage(3, 700, QImage::Format_RGB555);
    dumpQImageDataHelper(img);

    // Case 3: Invalid image.
    img = QImage(100, 0, QImage::Format_Invalid);
    dumpQImageDataHelper(img);
}

1217
1218
1219
1220
template <typename T>
        void tst_Debugger::dumpQLinkedListHelper(QLinkedList<T> &l)
{
    const int size = qMin(l.size(), 1000);
1221
    const QString &sizeStr = N(size);
1222
1223
    const QByteArray elemTypeStr = typeToString<T>();
    QByteArray expected = QByteArray("value='<").append(sizeStr).
1224
        append(" items>',valueeditable='false',numchild='").append(sizeStr).
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
        append("',childtype='").append(elemTypeStr).append("',childnumchild='").
        append(typeToNumchild<T>()).append("',children=[");
    typename QLinkedList<T>::const_iterator iter = l.constBegin();
    for (int i = 0; i < size; ++i, ++iter) {
        expected.append("{");
        const T &curElem = *iter;
        if (isPointer<T>()) {
            const QString typeStr = stripPtrType(typeToString<T>());
            const QByteArray addrStr = valToString(curElem);
            if (curElem != 0) {
                expected.append("addr='").append(addrStr).append("',saddr='").
                        append(addrStr).append("',type='").append(typeStr).
                        append("',value='").
                        append(derefValToString(curElem)).append("'");
            } else {
                expected.append("addr='").append(ptrToBa(&curElem)).append("',type='").
                    append(typeStr).append("',value='<null>',numchild='0'");
            }
        } else {
            expected.append("addr='").append(ptrToBa(&curElem)).
                append("',value='").append(valToString(curElem)).append("'");
        }
        expected.append("}");
        if (i < size - 1)
            expected.append(",");
    }
    if (size < l.size())
        expected.append(",...");
    expected.append("]");
    testDumper(expected, &l, NS"QLinkedList", true, elemTypeStr);
}

void tst_Debugger::dumpQLinkedList()
{
    // Case 1: Simple element type.
    QLinkedList<int> l;

    // Case 1.1: Empty list.
    dumpQLinkedListHelper(l);

    // Case 1.2: One element.
    l.append(2);
    dumpQLinkedListHelper(l);

    // Case 1.3: Two elements
    l.append(3);
    dumpQLinkedListHelper(l);

    // Case 2: Composite element type.
    QLinkedList<QString> l2;

    // Case 2.1: Empty list.
    dumpQLinkedListHelper(l2);

    // Case 2.2: One element.
    l2.append("Teststring 1");
    dumpQLinkedListHelper(l2);

    // Case 2.3: Two elements.
    l2.append("Teststring 2");
    dumpQLinkedListHelper(l2);

    // Case 2.4: > 1000 elements.
    for (int i = 3; i <= 1002; ++i)
1289
        l2.append("Test " + N(i));
1290
1291
1292
1293
1294
1295
1296
1297
1298

    // Case 3: Pointer type.
    QLinkedList<int *> l3;
    l3.append(new int(5));
    l3.append(new int(7));
    l3.append(0);
    dumpQLinkedListHelper(l3);
}

1299
1300
1301
1302
1303
1304
1305
    #if 0
    void tst_Debugger::dumpQLinkedList()
    {
        // Case 1: Simple element type.
        QLinkedList<int> l;

        // Case 1.1: Empty list.
1306
        testDumper("value='<0 items>',valueeditable='false',numchild='0',"
1307
1308
1309
1310
1311
            "childtype='int',childnumchild='0',children=[]",
            &l, NS"QLinkedList", true, "int");

        // Case 1.2: One element.
        l.append(2);
1312
        testDumper("value='<1 items>',valueeditable='false',numchild='1',"
1313
1314
1315
1316
1317
1318
1319
1320
            "childtype='int',childnumchild='0',children=[{addr='%',value='2'}]"
                << ptrToBa(l.constBegin().operator->()),
            &l, NS"QLinkedList", true, "int");

        // Case 1.3: Two elements
        l.append(3);
        QByteArray it0 = ptrToBa(l.constBegin().operator->());
        QByteArray it1 = ptrToBa(l.constBegin().operator++().operator->());
1321
        testDumper("value='<2 items>',valueeditable='false',numchild='2',"
1322
1323
1324
1325
1326
1327
1328
1329
1330
            "childtype='int',childnumchild='0',children=[{addr='%',value='2'},"
            "{addr='%',value='3'}]" << it0 << it1,
            &l, NS"QLinkedList", true, "int");

        // Case 2: Composite element type.
        QLinkedList<QString> l2;
        QLinkedList<QString>::const_iterator iter;

        // Case 2.1: Empty list.
1331
        testDumper("value='<0 items>',valueeditable='false',numchild='0',"
1332
1333
1334
1335
1336
1337
1338
            "childtype='"NS"QString',childnumchild='0',children=[]",
            &l2, NS"QLinkedList", true, NS"QString");

        // Case 2.2: One element.
        l2.append("Teststring 1");
        iter = l2.constBegin();
        qDebug() << *iter;
1339
        testDumper("value='<1 items>',valueeditable='false',numchild='1',"
1340
1341
1342
1343
1344
            "childtype='"NS"QString',childnumchild='0',children=[{addr='%',value='%',}]"
                << ptrToBa(iter.operator->()) << utfToBase64(*iter),
            &l2, NS"QLinkedList", true, NS"QString");

        // Case 2.3: Two elements.
1345
        QByteArray expected = "value='<2 items>',valueeditable='false',numchild='2',"
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359