1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QABSTRACTITEMMODELTESTER_H
5#define QABSTRACTITEMMODELTESTER_H
6
7#include <QtCore/QObject>
8#include <QtTest/qttestglobal.h>
9#include <QtCore/QAbstractItemModel>
10#include <QtCore/QVariant>
11
12#ifdef QT_GUI_LIB
13#include <QtGui/QFont>
14#include <QtGui/QColor>
15#include <QtGui/QBrush>
16#include <QtGui/QPixmap>
17#include <QtGui/QImage>
18#include <QtGui/QIcon>
19#endif
20
21QT_REQUIRE_CONFIG(itemmodeltester);
22
23QT_BEGIN_NAMESPACE
24
25class QAbstractItemModel;
26class QAbstractItemModelTester;
27class QAbstractItemModelTesterPrivate;
28
29namespace QTestPrivate {
30inline bool testDataGuiRoles(QAbstractItemModelTester *tester);
31}
32
33class Q_TESTLIB_EXPORT QAbstractItemModelTester : public QObject
34{
35 Q_OBJECT
36 Q_DECLARE_PRIVATE(QAbstractItemModelTester)
37
38public:
39 enum class FailureReportingMode {
40 QtTest,
41 Warning,
42 Fatal
43 };
44
45 QAbstractItemModelTester(QAbstractItemModel *model, QObject *parent = nullptr);
46 QAbstractItemModelTester(QAbstractItemModel *model, FailureReportingMode mode, QObject *parent = nullptr);
47
48 QAbstractItemModel *model() const;
49 FailureReportingMode failureReportingMode() const;
50 void setUseFetchMore(bool value);
51
52private:
53 friend inline bool QTestPrivate::testDataGuiRoles(QAbstractItemModelTester *tester);
54 bool verify(bool statement, const char *statementStr, const char *description, const char *file, int line);
55};
56
57namespace QTestPrivate {
58inline bool testDataGuiRoles(QAbstractItemModelTester *tester)
59{
60#ifdef QT_GUI_LIB
61
62#define MODELTESTER_VERIFY(statement) \
63do { \
64 if (!tester->verify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__)) \
65 return false; \
66} while (false)
67
68 const auto model = tester->model();
69 Q_ASSERT(model);
70
71 if (!model->hasChildren())
72 return true;
73
74 QVariant variant;
75
76 variant = model->data(index: model->index(row: 0, column: 0), role: Qt::DecorationRole);
77 if (variant.isValid()) {
78 MODELTESTER_VERIFY(variant.canConvert<QPixmap>()
79 || variant.canConvert<QImage>()
80 || variant.canConvert<QIcon>()
81 || variant.canConvert<QColor>()
82 || variant.canConvert<QBrush>());
83 }
84
85 // General Purpose roles that should return a QFont
86 variant = model->data(index: model->index(row: 0, column: 0), role: Qt::FontRole);
87 if (variant.isValid())
88 MODELTESTER_VERIFY(variant.canConvert<QFont>());
89
90 // General Purpose roles that should return a QColor or a QBrush
91 variant = model->data(index: model->index(row: 0, column: 0), role: Qt::BackgroundRole);
92 if (variant.isValid())
93 MODELTESTER_VERIFY(variant.canConvert<QColor>() || variant.canConvert<QBrush>());
94
95 variant = model->data(index: model->index(row: 0, column: 0), role: Qt::ForegroundRole);
96 if (variant.isValid())
97 MODELTESTER_VERIFY(variant.canConvert<QColor>() || variant.canConvert<QBrush>());
98
99#undef MODELTESTER_VERIFY
100
101#else
102 Q_UNUSED(tester);
103#endif // QT_GUI_LIB
104
105 return true;
106}
107} // namespaceQTestPrivate
108
109QT_END_NAMESPACE
110
111#endif // QABSTRACTITEMMODELTESTER_H
112

source code of qtbase/src/testlib/qabstractitemmodeltester.h