diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2021-03-25 10:50:58 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2021-04-16 19:10:45 +0200 |
commit | a149265bc65cd10921ebb7ca16f616d48620c662 (patch) | |
tree | b3a68253dc77b73f84738e890b959a820c8b5ffa | |
parent | 48f1032fb1722132cd44e5abda213469e7896138 (diff) |
Simplify the WaitForDataChanged test-helper class
Made use of the new WaitHelper class to simplify the logic for waiting
for the data insertion in the model.
Task-number: QTBUG-90688
Pick-to: 5.15
Change-Id: I2dfaa87c8efb7775bf12f5623abf283c1e678acd
Reviewed-by: Brett Stottlemyer <bstottle@ford.com>
-rw-r--r-- | tests/auto/modelview/tst_modelview.cpp | 22 | ||||
-rw-r--r-- | tests/auto/proxy/proxy.pro | 1 | ||||
-rw-r--r-- | tests/auto/proxy/tst_proxy.cpp | 9 | ||||
-rw-r--r-- | tests/auto/shared/model_utilities.h | 60 |
4 files changed, 26 insertions, 66 deletions
diff --git a/tests/auto/modelview/tst_modelview.cpp b/tests/auto/modelview/tst_modelview.cpp index e2fd129..2fd58ae 100644 --- a/tests/auto/modelview/tst_modelview.cpp +++ b/tests/auto/modelview/tst_modelview.cpp @@ -774,7 +774,6 @@ void TestModelView::testDataInsertion() RowsWatcher watcher(model.data(), insertedRowsCount); QVector<QModelIndex> pending; - QSignalSpy dataChangedSpy(model.data(), &QAbstractItemModelReplica::dataChanged); m_sourceModel.insertRows(2, insertedRowsCount); watcher.scheduleRowsToWatch(QModelIndex(), 2, 2 + insertedRowsCount - 1); @@ -787,8 +786,7 @@ void TestModelView::testDataInsertion() m_sourceModel.setData(m_sourceModel.index(0, 1), QColor(Qt::green), Qt::BackgroundRole); m_sourceModel.setData(m_sourceModel.index(0, 1), QLatin1String("foo"), Qt::DisplayRole); pending.append(model->index(0, 1)); - WaitForDataChanged w(pending, &dataChangedSpy); - + WaitForDataChanged w(model.data(), pending); QVERIFY(w.wait()); compareData(&m_sourceModel, model.data()); @@ -807,7 +805,6 @@ void TestModelView::testDataInsertionTree() const int insertedChildRowsCount = 4; RowsWatcher watcher(model.data(), insertedRowsCount + insertedChildRowsCount); - QSignalSpy dataChangedSpy(model.data(), &QAbstractItemModelReplica::dataChanged); QVector<QModelIndex> pending; for (int i = 0; i < insertedRowsCount; ++i) { @@ -839,7 +836,7 @@ void TestModelView::testDataInsertionTree() // change one row to check for inconsistencies pending << m_sourceModel.index(0, 0, parent); - WaitForDataChanged w(pending, &dataChangedSpy); + WaitForDataChanged w(model.data(), pending); m_sourceModel.setData(m_sourceModel.index(0, 0, parent), QColor(Qt::green), Qt::BackgroundRole); m_sourceModel.setData(m_sourceModel.index(0, 0, parent), QLatin1String("foo"), Qt::DisplayRole); @@ -879,9 +876,8 @@ void TestModelView::testDataRemoval() // change one row to check for inconsistencies QVector<QModelIndex> pending; - QSignalSpy dataChangedSpy(model.data(), &QAbstractItemModelReplica::dataChanged); pending << m_sourceModel.index(0, 0, parent); - WaitForDataChanged w(pending, &dataChangedSpy); + WaitForDataChanged w(model.data(), pending); m_sourceModel.setData(m_sourceModel.index(0, 0, parent), QColor(Qt::green), Qt::BackgroundRole); m_sourceModel.setData(m_sourceModel.index(0, 0, parent), QLatin1String("foo"), Qt::DisplayRole); @@ -987,8 +983,6 @@ void TestModelView::testSetData() compareTreeData(&m_sourceModel, model.data(), model->availableRoles()); //fetched and verified initial state, now setData on the client - QSignalSpy dataChangedSpy(&m_sourceModel, &QStandardItemModel::dataChanged); - QSignalSpy dataChangedReplicaSpy(model.data(), &QAbstractItemModelReplica::dataChanged); QVector<QModelIndex> pending; QVector<QModelIndex> pendingReplica; for (int row = 0, numRows = model->rowCount(); row < numRows; ++row) { @@ -1000,9 +994,9 @@ void TestModelView::testSetData() pendingReplica.append(model->index(row, column)); } } - WaitForDataChanged waiter(pending, &dataChangedSpy); + WaitForDataChanged waiter(&m_sourceModel, pending); QVERIFY(waiter.wait()); - WaitForDataChanged waiterReplica(pendingReplica, &dataChangedReplicaSpy); + WaitForDataChanged waiterReplica(model.data(), pendingReplica); QVERIFY(waiterReplica.wait()); compareData(&m_sourceModel, model.data()); } @@ -1018,8 +1012,6 @@ void TestModelView::testSetDataTree() compareTreeData(&m_sourceModel, model.data(), model->availableRoles()); //fetched and verified initial state, now setData on the client - QSignalSpy dataChangedSpy(&m_sourceModel, &QStandardItemModel::dataChanged); - QSignalSpy dataChangedReplicaSpy(model.data(), &QAbstractItemModelReplica::dataChanged); QVector<QModelIndex> pending; QVector<QModelIndex> pendingReplica; @@ -1047,9 +1039,9 @@ void TestModelView::testSetDataTree() } } } - WaitForDataChanged waiter(pending, &dataChangedSpy); + WaitForDataChanged waiter(&m_sourceModel, pending); QVERIFY(waiter.wait()); - WaitForDataChanged waiterReplica(pendingReplica, &dataChangedReplicaSpy); + WaitForDataChanged waiterReplica(model.data(), pendingReplica); QVERIFY(waiterReplica.wait()); compareData(&m_sourceModel, model.data()); } diff --git a/tests/auto/proxy/proxy.pro b/tests/auto/proxy/proxy.pro index c4e0a07..9f3d018 100644 --- a/tests/auto/proxy/proxy.pro +++ b/tests/auto/proxy/proxy.pro @@ -4,6 +4,7 @@ QT += testlib remoteobjects QT -= gui SOURCES += tst_proxy.cpp +HEADERS += $$PWD/../shared/model_utilities.h REPC_MERGED += engine.rep REPC_MERGED += subclass.rep diff --git a/tests/auto/proxy/tst_proxy.cpp b/tests/auto/proxy/tst_proxy.cpp index 29ec65f..2c2c5a0 100644 --- a/tests/auto/proxy/tst_proxy.cpp +++ b/tests/auto/proxy/tst_proxy.cpp @@ -224,7 +224,6 @@ void ProxyTest::testProxy() QCOMPARE(QSet<int>::fromList(rep->tracks()->availableRoles().toList()), QSet<int>::fromList(model.roleNames().keys())); } - QSignalSpy dataSpy(rep->tracks(), &QAbstractItemModelReplica::dataChanged); QVector<QModelIndex> pending; QTRY_COMPARE(rep->tracks()->rowCount(), model.rowCount()); for (int i = 0; i < rep->tracks()->rowCount(); i++) @@ -235,10 +234,10 @@ void ProxyTest::testProxy() pending.append(index); } if (useProxy) { // A first batch of updates will be the empty proxy values - WaitForDataChanged w(pending, &dataSpy); + WaitForDataChanged w(rep->tracks(), pending); QVERIFY(w.wait()); } - WaitForDataChanged w(pending, &dataSpy); + WaitForDataChanged w(rep->tracks(), pending); QVERIFY(w.wait()); for (int i = 0; i < rep->tracks()->rowCount(); i++) { @@ -304,10 +303,10 @@ void ProxyTest::testProxy() pending.append(index); } if (useProxy) { // A first batch of updates will be the empty proxy values - WaitForDataChanged w(pending, &dataSpy); + WaitForDataChanged w(tracksReplica, pending); QVERIFY(w.wait()); } - WaitForDataChanged w(pending, &dataSpy); + WaitForDataChanged w(tracksReplica, pending); QVERIFY(w.wait()); for (int i = 0; i < tracksReplica->rowCount(); i++) { diff --git a/tests/auto/shared/model_utilities.h b/tests/auto/shared/model_utilities.h index b1044a5..14aa733 100644 --- a/tests/auto/shared/model_utilities.h +++ b/tests/auto/shared/model_utilities.h @@ -86,53 +86,20 @@ inline bool compareIndices(const QModelIndex &lhs, const QModelIndex &rhs) return true; } -struct WaitForDataChanged +struct WaitForDataChanged : public WaitHelper { - struct IndexPair + WaitForDataChanged(const QAbstractItemModel *model, const QVector<QModelIndex> &pending) + : WaitHelper(), m_model(model), m_pending(pending) { - QModelIndex topLeft; - QModelIndex bottomRight; - }; - - WaitForDataChanged(const QVector<QModelIndex> &pending, QSignalSpy *spy) : m_pending(pending), m_spy(spy){} - bool wait() - { - Q_ASSERT(m_spy); - const int maxRuns = std::min(m_pending.size(), 100); - int runs = 0; - bool cancel = false; - while (!cancel) { - const int numSignals = m_spy->size(); - for (int i = 0; i < numSignals; ++i) { - const QList<QVariant> &signal = m_spy->takeFirst(); - IndexPair pair = extractPair(signal); - checkAndRemoveRange(pair.topLeft, pair.bottomRight); - cancel = m_pending.isEmpty(); - } - if (!cancel) - m_spy->wait(); - ++runs; - if (runs >= maxRuns) - cancel = true; - } - return runs < maxRuns; - } - - static IndexPair extractPair(const QList<QVariant> &signal) - { - IndexPair pair; - if (signal.size() != 3) - return pair; - const bool matchingTypes = signal[0].type() == QVariant::nameToType("QModelIndex") - && signal[1].type() == QVariant::nameToType("QModelIndex") - && signal[2].type() == QVariant::nameToType("QVector<int>"); - if (!matchingTypes) - return pair; - const QModelIndex topLeft = signal[0].value<QModelIndex>(); - const QModelIndex bottomRight = signal[1].value<QModelIndex>(); - pair.topLeft = topLeft; - pair.bottomRight = bottomRight; - return pair; + connect(m_model, &QAbstractItemModel::dataChanged, this, + [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, + const QVector<int> &roles) { + Q_UNUSED(roles) + + checkAndRemoveRange(topLeft, bottomRight); + if (m_pending.isEmpty()) + finish(); + }); } void checkAndRemoveRange(const QModelIndex &topLeft, const QModelIndex &bottomRight) @@ -156,8 +123,9 @@ struct WaitForDataChanged } } +private: + const QAbstractItemModel *m_model = nullptr; QVector<QModelIndex> m_pending; - QSignalSpy *m_spy; }; } // namespace |