File: problemreportertest.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (408 lines) | stat: -rw-r--r-- 17,420 bytes parent folder | download
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
/*
  problemreportertest.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2018 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Anton Kreuzkamp <anton.kreuzkamp@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "baseprobetest.h"

#include <core/problemcollector.h>
#include <common/problem.h>
#include <common/sourcelocation.h>
#include <common/objectbroker.h>

#include <common/tools/problemreporter/problemmodelroles.h>

#include <QAbstractItemModelTester>
#include <QDebug>
#include <QTest>
#include <QObject>
#include <QThread>
#include <QThreadPool>
#include <QRunnable>
#include <QUuid>

#ifdef QT_QML_LIB
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlContext>
#endif

#ifdef HAVE_QT_WIDGETS
#include <QAction>
#include <QKeySequence>
#endif

using namespace GammaRay;

class CrossThreadConnectionTask : public QRunnable
{
public:
    void run() override
    {
        newThreadObj.reset(new QObject());
        newThreadObj->setObjectName("newThreadObj");
        QObject::connect(newThreadObj.get(), &QObject::destroyed, mainThreadObj.get(), &QObject::deleteLater, Qt::DirectConnection);
    }
    std::unique_ptr<QObject> newThreadObj;
    std::unique_ptr<QObject> mainThreadObj;
};

struct UnregisteredType
{
};

class FaultyMetaObjectBaseClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(UnregisteredType someProp READ someProp CONSTANT)
    UnregisteredType someProp() const
    {
        return {};
    }
};

class FaultyMetaObjectClass : public FaultyMetaObjectBaseClass
{
    Q_OBJECT

    Q_PROPERTY(UnregisteredType someProp READ someProp CONSTANT)

public:
    Q_INVOKABLE static void noop(UnregisteredType param)
    {
        Q_UNUSED(param)
    }

    UnregisteredType someProp() const
    {
        return {};
    }
};

namespace GammaRay {
class ProblemReporterTest : public BaseProbeTest
{
    Q_OBJECT

    static void dummyScan()
    {
        Problem p1;
        p1.problemId = QUuid::createUuid().toString();
        p1.findingCategory = Problem::Scan;
        ProblemCollector::addProblem(p1);

        Problem p2;
        p2.problemId = QUuid::createUuid().toString();
        p2.findingCategory = Problem::Scan;
        ProblemCollector::addProblem(p2);
    }

    std::unique_ptr<QAbstractItemModelTester> problemModelTest;
    std::unique_ptr<QAbstractItemModelTester> availableCheckersModelTest;

private slots:
    void initTestCase()
    {
        createProbe();
        problemModelTest.reset(new QAbstractItemModelTester(ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.ProblemModel"))));
        availableCheckersModelTest.reset(new QAbstractItemModelTester(ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.AvailableProblemCheckersModel"))));
    }

    static void cleanup()
    {
        ProblemCollector::instance()->clearScans();
        QCOMPARE(ProblemCollector::instance()->problems().size(), 0);
    }

    static void testDuplicates()
    {
        QCOMPARE(ProblemCollector::instance()->problems().size(), 0);

        Problem p1;
        p1.problemId = QStringLiteral("9skjlksdjb");
        ProblemCollector::addProblem(p1);

        Problem p2;
        p2.problemId = QStringLiteral("9skjlksdjb");
        ProblemCollector::addProblem(p2);

        QCOMPARE(ProblemCollector::instance()->problems().size(), 1);

        ProblemCollector::removeProblem(QStringLiteral("9skjlksdjb"));
    }

    static void testMultipleSourceLocations()
    {
        QCOMPARE(ProblemCollector::instance()->problems().size(), 0);

        SourceLocation l1 = SourceLocation::fromOneBased(QUrl("../main.qml"), 101, 42);
        SourceLocation l2 = SourceLocation::fromOneBased(QUrl("A.qml"), 43, 21);
        SourceLocation l3 = SourceLocation::fromOneBased(QUrl("../../src/Singleton.cpp"), 555, 1);
        SourceLocation l4 = SourceLocation::fromOneBased(QUrl("../main.qml"), 101, 42);
        SourceLocation l5 = SourceLocation::fromOneBased(QUrl("A.qml"), 44, 21);

        Problem p1;
        p1.problemId = QStringLiteral("abcdefg");
        ProblemCollector::addProblem(p1);

        Problem p2;
        p2.problemId = QStringLiteral("abcdefg");
        p2.locations << l1;
        ProblemCollector::addProblem(p2);

        Problem p3;
        p3.problemId = QStringLiteral("abcdefg");
        p3.locations << l2 << l3 << l4;
        ProblemCollector::addProblem(p3);

        Problem p4;
        p4.problemId = QStringLiteral("abcdefg");
        p4.locations << l5;
        ProblemCollector::addProblem(p4);
        QCOMPARE(ProblemCollector::instance()->problems().size(), 1);

        QCOMPARE(ProblemCollector::instance()->problems().front().locations.size(), 4);
        QCOMPARE(ProblemCollector::instance()->problems().front().locations.at(0), SourceLocation::fromOneBased(QUrl("../main.qml"), 101, 42));
        QCOMPARE(ProblemCollector::instance()->problems().front().locations.at(1), SourceLocation::fromOneBased(QUrl("A.qml"), 43, 21));
        QCOMPARE(ProblemCollector::instance()->problems().front().locations.at(2), SourceLocation::fromOneBased(QUrl("../../src/Singleton.cpp"), 555, 1));
        QCOMPARE(ProblemCollector::instance()->problems().front().locations.at(3), SourceLocation::fromOneBased(QUrl("A.qml"), 44, 21));

        ProblemCollector::removeProblem(QStringLiteral("abcdefg"));
    }

    static void testScans()
    {
        auto standardCheckersCount = ProblemCollector::instance()->availableCheckers().size();
        ProblemCollector::registerProblemChecker(QStringLiteral("Dummy"),
                                                 QStringLiteral("Dummy"),
                                                 QStringLiteral("Always reports two dummy problems"),
                                                 &ProblemReporterTest::dummyScan);

        QCOMPARE(ProblemCollector::instance()->availableCheckers().size(), standardCheckersCount + 1);

        QCOMPARE(ProblemCollector::instance()->problems().size(), 0);
        ProblemCollector::instance()->requestScan();
        auto problemsFromScansCount = ProblemCollector::instance()->problems().size();
        ProblemCollector::instance()->requestScan(); // scans should always be reproducible if the program didn't change.
        QCOMPARE(ProblemCollector::instance()->problems().size(), problemsFromScansCount);

        auto dummyChecker = std::find_if(ProblemCollector::instance()->availableCheckers().begin(),
                                         ProblemCollector::instance()->availableCheckers().end(),
                                         [](const ProblemCollector::Checker &c) { return c.id == QStringLiteral("Dummy"); });
        dummyChecker->enabled = false;

        ProblemCollector::instance()->requestScan(); // scans should always be reproducible if the program didn't change.
        QCOMPARE(ProblemCollector::instance()->problems().size(), problemsFromScansCount - 2);
        dummyChecker->enabled = true;

        Problem p1;
        p1.problemId = QStringLiteral("9skjlksdjb");
        p1.findingCategory = Problem::Live;
        ProblemCollector::addProblem(p1);
        Problem p2;
        p2.problemId = QStringLiteral("abcdefg");
        p2.findingCategory = Problem::Permanent;
        ProblemCollector::addProblem(p2);

        // all problems originating from a scan should be deleted before doing a new scan, but not live- and permanent problems
        ProblemCollector::instance()->requestScan();
        QCOMPARE(ProblemCollector::instance()->problems().size(), problemsFromScansCount + 2);


        ProblemCollector::instance()->clearScans();
        QCOMPARE(ProblemCollector::instance()->problems().size(), 2);
        ProblemCollector::removeProblem(QStringLiteral("9skjlksdjb"));
        ProblemCollector::removeProblem(QStringLiteral("abcdefg"));

        ProblemCollector::instance()->availableCheckers().erase(dummyChecker);
    }

    static void testAvailableScansModel()
    {
        auto model = ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.AvailableProblemCheckersModel"));
        auto rowCount = model->rowCount();
        ProblemCollector::registerProblemChecker(QStringLiteral("Dummy"),
                                                 QStringLiteral("Dummy"),
                                                 QStringLiteral("Always reports two dummy problems"),
                                                 &ProblemReporterTest::dummyScan);
        QCOMPARE(model->rowCount(), rowCount + 1);

        auto &checkers = ProblemCollector::instance()->availableCheckers();
        checkers.erase(std::remove_if(checkers.begin(),
                                      checkers.end(),
                                      [](ProblemCollector::Checker &c) { return c.id == "Dummy"; }));
        QCOMPARE(model->rowCount(), rowCount);
    }

    void testProblemModel()
    {
        auto model = ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.ProblemModel"));
        auto rowCount = model->rowCount();

        Problem p1;
        p1.problemId = QStringLiteral("9skjlksdjb");
        p1.description = QStringLiteral("Lorem ipsum dolor sit amet.");
        p1.severity = Problem::Warning;
        SourceLocation l1 = SourceLocation::fromOneBased(QUrl("../main.qml"), 101, 42);
        SourceLocation l2 = SourceLocation::fromOneBased(QUrl("A.qml"), 43, 21);
        p1.object = ObjectId(this);
        p1.locations << l1 << l2;
        ProblemCollector::addProblem(p1);

        Problem p2;
        p2.problemId = QStringLiteral("asdf");
        ProblemCollector::addProblem(p2);

        QCOMPARE(model->rowCount(), rowCount + 2);

        auto index = model->index(rowCount, 0, QModelIndex());
        QCOMPARE(index.data(Qt::DisplayRole).toString(), QStringLiteral("Lorem ipsum dolor sit amet."));
        QCOMPARE(index.sibling(rowCount, 1).data(Qt::DisplayRole).toString(), l1.displayString());
        QCOMPARE(index.data(ObjectModel::ObjectIdRole).value<ObjectId>(), ObjectId(this));
        QCOMPARE(index.data(ProblemModelRoles::SourceLocationRole).value<QVector<SourceLocation>>(), p1.locations);
        QCOMPARE(index.data(ProblemModelRoles::SeverityRole).value<int>(), static_cast<int>(Problem::Warning));
        QCOMPARE(index.data(ProblemModelRoles::ProblemIdRole).toString(), QStringLiteral("9skjlksdjb"));


        ProblemCollector::removeProblem(QStringLiteral("9skjlksdjb"));
        QCOMPARE(model->rowCount(), rowCount + 1);
        ProblemCollector::removeProblem(QStringLiteral("asdf"));
        QCOMPARE(model->rowCount(), rowCount);
    }

#ifdef QT_QML_LIB
    static void testBindingLoopChecker()
    {
        QSKIP("Binding Loop detection doesn't work on Qt6 yet");
    }
#endif

    static void testConnectionIssues()
    {
        QVERIFY(ProblemCollector::instance()->isCheckerRegistered("com.kdab.GammaRay.ObjectInspector.ConnectionsCheck"));

        auto task = std::unique_ptr<CrossThreadConnectionTask>(new CrossThreadConnectionTask());
        task->mainThreadObj.reset(new QObject());
        task->mainThreadObj->setObjectName("mainThreadObj");
        task->setAutoDelete(false);
        // QThreadPool takes ownership and deletes 'hello' automatically
        QThreadPool::globalInstance()->start(task.get());

        auto o1 = std::unique_ptr<QObject>(new QObject());
        o1->setObjectName("o1");
        auto o2 = std::unique_ptr<QObject>(new QObject());
        o2->setObjectName("o2");
        // clang-format off
        connect(o1.get(), SIGNAL(destroyed(QObject*)), o2.get(), SLOT(deleteLater()));
        connect(o1.get(), SIGNAL(destroyed(QObject*)), o2.get(), SLOT(deleteLater()));
        // clang-format on

        QTest::qWait(10);
        ProblemCollector::instance()->requestScan();

        o1->disconnect();
        task->newThreadObj->disconnect();

        const auto &problems = ProblemCollector::instance()->problems();
        auto crossThreadProblem = std::find_if(problems.begin(), problems.end(),
                                               [](const Problem &p) { return p.problemId.startsWith("com.kdab.GammaRay.ObjectInspector.ConnectionsCheck.CrossTread"); });

        QVERIFY(crossThreadProblem != problems.end());
        QCOMPARE(crossThreadProblem->object, ObjectId(task->mainThreadObj.get()));
        QVERIFY2(crossThreadProblem->description.contains("direct cross-thread connection"), qPrintable(crossThreadProblem->description));
        QVERIFY2(crossThreadProblem->description.contains("signal QObject (newThreadObj)"), qPrintable(crossThreadProblem->description));
        QVERIFY2(crossThreadProblem->description.contains("slot QObject (mainThreadObj)"), qPrintable(crossThreadProblem->description));

        auto duplicateProblem = std::find_if(problems.begin(), problems.end(),
                                             [](const Problem &p) { return p.problemId.startsWith("com.kdab.GammaRay.ObjectInspector.ConnectionsCheck.Duplicate"); });

        QVERIFY(duplicateProblem != problems.end());
        QCOMPARE(duplicateProblem->object, ObjectId(o2.get()));
        QVERIFY(duplicateProblem->description.contains("multiple times"));
        QVERIFY2(duplicateProblem->description.contains("signal QObject (o1)"), qPrintable(duplicateProblem->description));
        QVERIFY2(duplicateProblem->description.contains("slot QObject (o2)"), qPrintable(duplicateProblem->description));

        disconnect(o1.get(), nullptr, o2.get(), nullptr);
        connect(o1.get(), &QObject::destroyed, o2.get(), &QObject::deleteLater);
        connect(o1.get(), &QObject::destroyed, o2.get(), &QObject::deleteLater);
        QTest::qWait(10);
        ProblemCollector::instance()->requestScan();

        const auto &problems2 = ProblemCollector::instance()->problems();
        auto duplicateProblem2 = std::find_if(problems2.begin(), problems2.end(),
                                              [&o2](const Problem &p) {
                                                  return p.problemId.startsWith("com.kdab.GammaRay.ObjectInspector.ConnectionsCheck.Duplicate")
                                                      && p.object == ObjectId(o2.get());
                                              });

        QEXPECT_FAIL("", "We can't find duplicates with PMF connects, yet.", Abort);
        QVERIFY(duplicateProblem2 != problems2.end());
        QCOMPARE(duplicateProblem2->object, ObjectId(o2.get()));
        QVERIFY(duplicateProblem2->description.contains("multiple times"));
        QVERIFY(duplicateProblem2->description.contains("signal o1"));
        QVERIFY(duplicateProblem2->description.contains("slot o2"));
    }

    static void testMetaTypeChecks()
    {
        std::unique_ptr<QObject> obj(new FaultyMetaObjectClass);
        QTest::qWait(1);

        auto &checkers = ProblemCollector::instance()->availableCheckers();
        auto checker = std::find_if(checkers.begin(), checkers.end(),
                                    [](ProblemCollector::Checker &c) { return c.id == "com.kdab.GammaRay.MetaObjectBrowser.QMetaObjectValidator"; });
        QVERIFY(checker != checkers.end());
        checker->enabled = true;

        ProblemCollector::instance()->requestScan();

        const auto &problems = ProblemCollector::instance()->problems();
        QVERIFY(std::any_of(problems.begin(), problems.end(),
                            [&obj](const Problem &p) {
                                return p.problemId.startsWith("com.kdab.GammaRay.MetaObjectBrowser.QMetaObjectValidator")
                                    && p.object == ObjectId(const_cast<QMetaObject *>(obj->metaObject()), "const QMetaObject*")
                                    && p.description.contains(QLatin1String("overrides base class property"));
                            }));
    }

#ifdef HAVE_QT_WIDGETS
    static void testActionValidator()
    {
        QAction *a1 = new QAction(QStringLiteral("Action 1"), qApp);
        a1->setShortcut(QKeySequence(QStringLiteral("Ctrl+K")));
        a1->setShortcutContext(Qt::ApplicationShortcut);
        QAction *a2 = new QAction(QStringLiteral("Action 2"), qApp);
        a2->setShortcut(QKeySequence(QStringLiteral("Ctrl+K")));
        a2->setShortcutContext(Qt::WidgetShortcut);
        QTest::qWait(1); // event loop re-entry

        QVERIFY(ProblemCollector::instance()->isCheckerRegistered("gammaray_actioninspector.ShortcutDuplicates"));

        ProblemCollector::instance()->requestScan();

        const auto &problems = ProblemCollector::instance()->problems();
        QVERIFY(std::any_of(problems.begin(), problems.end(),
                            [=](const Problem &p) {
                                return p.problemId.startsWith("gammaray_actioninspector.ShortcutDuplicates")
                                    && (p.object == ObjectId(a1) || p.object == ObjectId(a2))
                                    && p.description.contains("ambiguous")
                                    && p.description.contains(QKeySequence(QStringLiteral("Ctrl+K")).toString(QKeySequence::NativeText))
                                    && p.problemId.endsWith(QKeySequence(QStringLiteral("Ctrl+K")).toString(QKeySequence::PortableText));
                            }));
    }
#endif
};

}

QTEST_MAIN(ProblemReporterTest)

#include "problemreportertest.moc"