QAbstractItemModelTester: Enable testing the QtGui roles without linking to QtGui
This enables doing QtGui testing when using QAbstractItemModelTester in Qt for Python, where the QTestlib binding only links against QtCore. Port the code to check against QMetaType constructed from type names. Task-number: PYSIDE-2772 Change-Id: Ifcd8f1ea4758459d8a178226e3f215e5c2b273b8 Reviewed-by: hjk <hjk@qt.io> (cherry picked from commit bb542a55b28dd591709ed2c68e4505ec08fc62b5) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
b34fa2f61f
commit
0723c6fd05
@ -6,6 +6,7 @@
|
||||
|
||||
#include <private/qobject_p.h>
|
||||
#include <private/qabstractitemmodel_p.h>
|
||||
#include <QtCore/qmetatype.h>
|
||||
#include <QtCore/QPointer>
|
||||
#include <QtCore/QAbstractItemModel>
|
||||
#include <QtCore/QStack>
|
||||
@ -73,6 +74,7 @@ private:
|
||||
void checkChildren(const QModelIndex &parent, int currentDepth = 0);
|
||||
|
||||
bool verify(bool statement, const char *statementStr, const char *description, const char *file, int line);
|
||||
void testDataGuiRoles(QAbstractItemModelTester *tester);
|
||||
|
||||
template<typename T1, typename T2>
|
||||
bool compare(const T1 &t1, const T2 &t2,
|
||||
@ -623,6 +625,48 @@ void QAbstractItemModelTesterPrivate::checkChildren(const QModelIndex &parent, i
|
||||
}
|
||||
}
|
||||
|
||||
void QAbstractItemModelTesterPrivate::testDataGuiRoles(QAbstractItemModelTester *tester)
|
||||
{
|
||||
const auto model = tester->model();
|
||||
Q_ASSERT(model);
|
||||
|
||||
if (!model->hasChildren())
|
||||
return;
|
||||
|
||||
static const QMetaType pixmapType = QMetaType(QMetaType::QPixmap);
|
||||
if (!pixmapType.isValid())
|
||||
return;
|
||||
|
||||
static const QMetaType imageType = QMetaType(QMetaType::QImage);
|
||||
static const QMetaType iconType = QMetaType(QMetaType::QIcon);
|
||||
static const QMetaType colorType = QMetaType(QMetaType::QColor);
|
||||
static const QMetaType brushType = QMetaType(QMetaType::QBrush);
|
||||
static const QMetaType fontType = QMetaType(QMetaType::QFont);
|
||||
|
||||
QVariant variant = model->data(model->index(0, 0), Qt::DecorationRole);
|
||||
if (variant.isValid()) {
|
||||
MODELTESTER_VERIFY(variant.canConvert(pixmapType)
|
||||
|| variant.canConvert(imageType)
|
||||
|| variant.canConvert(iconType)
|
||||
|| variant.canConvert(colorType)
|
||||
|| variant.canConvert(brushType));
|
||||
}
|
||||
|
||||
// General Purpose roles that should return a QFont
|
||||
variant = model->data(model->index(0, 0), Qt::FontRole);
|
||||
if (variant.isValid())
|
||||
MODELTESTER_VERIFY(variant.canConvert(fontType));
|
||||
|
||||
// General Purpose roles that should return a QColor or a QBrush
|
||||
variant = model->data(model->index(0, 0), Qt::BackgroundRole);
|
||||
if (variant.isValid())
|
||||
MODELTESTER_VERIFY(variant.canConvert(colorType) || variant.canConvert(brushType));
|
||||
|
||||
variant = model->data(model->index(0, 0), Qt::ForegroundRole);
|
||||
if (variant.isValid())
|
||||
MODELTESTER_VERIFY(variant.canConvert(colorType) || variant.canConvert(brushType));
|
||||
}
|
||||
|
||||
/*
|
||||
Tests model's implementation of QAbstractItemModel::data()
|
||||
*/
|
||||
@ -674,9 +718,7 @@ void QAbstractItemModelTesterPrivate::data()
|
||||
MODELTESTER_VERIFY(sizeHintVariant.canConvert<QSize>());
|
||||
|
||||
Q_Q(QAbstractItemModelTester);
|
||||
|
||||
if (!QTestPrivate::testDataGuiRoles(q))
|
||||
return;
|
||||
testDataGuiRoles(q);
|
||||
}
|
||||
|
||||
void QAbstractItemModelTesterPrivate::columnsAboutToBeInserted(const QModelIndex &parent, int start,
|
||||
|
@ -50,62 +50,9 @@ public:
|
||||
void setUseFetchMore(bool value);
|
||||
|
||||
private:
|
||||
friend inline bool QTestPrivate::testDataGuiRoles(QAbstractItemModelTester *tester);
|
||||
bool verify(bool statement, const char *statementStr, const char *description, const char *file, int line);
|
||||
};
|
||||
|
||||
namespace QTestPrivate {
|
||||
inline bool testDataGuiRoles(QAbstractItemModelTester *tester)
|
||||
{
|
||||
#ifdef QT_GUI_LIB
|
||||
|
||||
#define MODELTESTER_VERIFY(statement) \
|
||||
do { \
|
||||
if (!tester->verify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__)) \
|
||||
return false; \
|
||||
} while (false)
|
||||
|
||||
const auto model = tester->model();
|
||||
Q_ASSERT(model);
|
||||
|
||||
if (!model->hasChildren())
|
||||
return true;
|
||||
|
||||
QVariant variant;
|
||||
|
||||
variant = model->data(model->index(0, 0), Qt::DecorationRole);
|
||||
if (variant.isValid()) {
|
||||
MODELTESTER_VERIFY(variant.canConvert<QPixmap>()
|
||||
|| variant.canConvert<QImage>()
|
||||
|| variant.canConvert<QIcon>()
|
||||
|| variant.canConvert<QColor>()
|
||||
|| variant.canConvert<QBrush>());
|
||||
}
|
||||
|
||||
// General Purpose roles that should return a QFont
|
||||
variant = model->data(model->index(0, 0), Qt::FontRole);
|
||||
if (variant.isValid())
|
||||
MODELTESTER_VERIFY(variant.canConvert<QFont>());
|
||||
|
||||
// General Purpose roles that should return a QColor or a QBrush
|
||||
variant = model->data(model->index(0, 0), Qt::BackgroundRole);
|
||||
if (variant.isValid())
|
||||
MODELTESTER_VERIFY(variant.canConvert<QColor>() || variant.canConvert<QBrush>());
|
||||
|
||||
variant = model->data(model->index(0, 0), Qt::ForegroundRole);
|
||||
if (variant.isValid())
|
||||
MODELTESTER_VERIFY(variant.canConvert<QColor>() || variant.canConvert<QBrush>());
|
||||
|
||||
#undef MODELTESTER_VERIFY
|
||||
|
||||
#else
|
||||
Q_UNUSED(tester);
|
||||
#endif // QT_GUI_LIB
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespaceQTestPrivate
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QABSTRACTITEMMODELTESTER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user