From 13f94c38695b94bff86a8ae2aace8a6140faf2d4 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Mon, 1 Feb 2016 17:23:42 -0800 Subject: Factor out testing code for QTBUG_48870_fastModelUpdates This is going to be reused for QQuickGridview's counterpart. Change-Id: I3eaf272229b0e45dfc8c0b78ba94d57c72f9cc5d Reviewed-by: Shawn Rutledge --- tests/auto/quick/shared/viewtestutil.cpp | 80 ++++++++++++++++++++++++++++++++ tests/auto/quick/shared/viewtestutil.h | 22 +++++++++ 2 files changed, 102 insertions(+) (limited to 'tests/auto/quick/shared') diff --git a/tests/auto/quick/shared/viewtestutil.cpp b/tests/auto/quick/shared/viewtestutil.cpp index 1330cbccc9..5a1006c8e0 100644 --- a/tests/auto/quick/shared/viewtestutil.cpp +++ b/tests/auto/quick/shared/viewtestutil.cpp @@ -40,6 +40,7 @@ #include #include +#include QQuickView *QQuickViewTestUtil::createView() @@ -348,6 +349,85 @@ QList > QQuickViewTestUtil::ListRange::getModelDataValues return data; } +QQuickViewTestUtil::StressTestModel::StressTestModel() + : QAbstractListModel() + , m_rowCount(20) +{ + QTimer *t = new QTimer(this); + t->setInterval(500); + t->start(); + + qsrand(qHash(QDateTime::currentDateTime())); + connect(t, &QTimer::timeout, this, &StressTestModel::updateModel); +} + +int QQuickViewTestUtil::StressTestModel::rowCount(const QModelIndex &) const +{ + return m_rowCount; +} + +QVariant QQuickViewTestUtil::StressTestModel::data(const QModelIndex &, int) const +{ + return QVariant(); +} + +void QQuickViewTestUtil::StressTestModel::updateModel() +{ + if (m_rowCount > 10) { + for (int i = 0; i < 10; ++i) { + int rnum = qrand() % m_rowCount; + beginRemoveRows(QModelIndex(), rnum, rnum); + m_rowCount--; + endRemoveRows(); + } + } + if (m_rowCount < 20) { + for (int i = 0; i < 10; ++i) { + int rnum = qrand() % m_rowCount; + beginInsertRows(QModelIndex(), rnum, rnum); + m_rowCount++; + endInsertRows(); + } + } +} + +bool QQuickViewTestUtil::testVisibleItems(const QQuickItemViewPrivate *priv, bool *nonUnique, FxViewItem **failItem, int *expectedIdx) +{ + QHash uniqueItems; + + int skip = 0; + for (int i = 0; i < priv->visibleItems.count(); ++i) { + FxViewItem *item = priv->visibleItems.at(i); + if (!item) { + *failItem = Q_NULLPTR; + return false; + } +#if 0 + qDebug() << "\t" << item->index + << item->item + << item->position() + << (!item->item || QQuickItemPrivate::get(item->item)->culled ? "hidden" : "visible"); +#endif + if (item->index == -1) { + ++skip; + } else if (item->index != priv->visibleIndex + i - skip) { + *nonUnique = false; + *failItem = item; + *expectedIdx = priv->visibleIndex + i - skip; + return false; + } else if (uniqueItems.contains(item->item)) { + *nonUnique = true; + *failItem = item; + *expectedIdx = uniqueItems.find(item->item).value(); + return false; + } + + uniqueItems.insert(item->item, item->index); + } + + return true; +} + namespace QQuickTouchUtils { /* QQuickWindow does event compression and only delivers events just diff --git a/tests/auto/quick/shared/viewtestutil.h b/tests/auto/quick/shared/viewtestutil.h index 1643eca979..155d7967ba 100644 --- a/tests/auto/quick/shared/viewtestutil.h +++ b/tests/auto/quick/shared/viewtestutil.h @@ -39,6 +39,8 @@ #include QT_FORWARD_DECLARE_CLASS(QQuickView) +QT_FORWARD_DECLARE_CLASS(QQuickItemViewPrivate) +QT_FORWARD_DECLARE_CLASS(FxViewItem) namespace QQuickViewTestUtil { @@ -158,6 +160,26 @@ namespace QQuickViewTestUtil for (; f != replaced.end(); ++f, ++t) *t = *f; } + + class StressTestModel : public QAbstractListModel + { + Q_OBJECT + + public: + + StressTestModel(); + + int rowCount(const QModelIndex &) const; + QVariant data(const QModelIndex &, int) const; + + public Q_SLOTS: + void updateModel(); + + private: + int m_rowCount; + }; + + bool testVisibleItems(const QQuickItemViewPrivate *priv, bool *nonUnique, FxViewItem **failItem, int *expectedIdx); } namespace QQuickTouchUtils { -- cgit v1.2.3