tst_QAbstractItemModelTester: fix unittest

rowsAboutToBeMoved() and rowsMoved() signals aren't emitted for
QSortFilterProxyModel, which meant the two connections in the
ObservingObject's constructor didn't trigger the slots, which let the
test pass (the store/checkPersistentFailureCount stayed at 0).

- Instead connect to layoutAboutToBeChanged() and layoutChanged()
  respectively, these two are emitted for QSFPM
- Use PMF syntax
- Verify m_persistent{Proxy,Source}Indexes aren't empty

Change-Id: I8b83989de02c2bfb22bde9b230cb5b68814f74b6
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2023-08-14 02:33:13 +03:00 committed by Ahmad Samir
parent 9c819c9073
commit 9016def4dc

View File

@ -139,14 +139,8 @@ class AccessibleProxyModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
public: public:
AccessibleProxyModel(QObject *parent = nullptr) : QSortFilterProxyModel(parent) using QSortFilterProxyModel::QSortFilterProxyModel;
{ using QSortFilterProxyModel::persistentIndexList;
}
QModelIndexList persistent()
{
return persistentIndexList();
}
}; };
class ObservingObject : public QObject class ObservingObject : public QObject
@ -159,15 +153,18 @@ public:
storePersistentFailureCount(0), storePersistentFailureCount(0),
checkPersistentFailureCount(0) checkPersistentFailureCount(0)
{ {
connect(m_proxy, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), // moveRows signals can not come through because the proxy might sort/filter
SLOT(storePersistent())); // out some of the moved rows. therefore layoutChanged signals are sent from
connect(m_proxy, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), // the QSFPM and we have to listen to them here to get properly notified
SLOT(checkPersistent())); connect(m_proxy, &QAbstractProxyModel::layoutAboutToBeChanged, this,
&ObservingObject::storePersistent);
connect(m_proxy, &QAbstractProxyModel::layoutChanged, this,
&ObservingObject::checkPersistent);
} }
public slots: public slots:
void storePersistent(const QModelIndex &parent) void storePersistentRecursive(const QModelIndex &parent)
{ {
for (int row = 0; row < m_proxy->rowCount(parent); ++row) { for (int row = 0; row < m_proxy->rowCount(parent); ++row) {
QModelIndex proxyIndex = m_proxy->index(row, 0, parent); QModelIndex proxyIndex = m_proxy->index(row, 0, parent);
@ -183,26 +180,27 @@ public slots:
m_persistentSourceIndexes.append(sourceIndex); m_persistentSourceIndexes.append(sourceIndex);
m_persistentProxyIndexes.append(proxyIndex); m_persistentProxyIndexes.append(proxyIndex);
if (m_proxy->hasChildren(proxyIndex)) if (m_proxy->hasChildren(proxyIndex))
storePersistent(proxyIndex); storePersistentRecursive(proxyIndex);
} }
} }
void storePersistent() void storePersistent(const QList<QPersistentModelIndex> &parents = {})
{ {
// This method is called from rowsAboutToBeMoved. Persistent indexes should be valid // This method is called from source model rowsAboutToBeMoved. Persistent indexes should be valid
foreach (const QModelIndex &idx, m_persistentProxyIndexes) foreach (const QModelIndex &idx, m_persistentProxyIndexes)
if (!idx.isValid()) { if (!idx.isValid()) {
qWarning("%s: persistentProxyIndexes contains invalid index", Q_FUNC_INFO); qWarning("%s: persistentProxyIndexes contains invalid index", Q_FUNC_INFO);
++storePersistentFailureCount; ++storePersistentFailureCount;
} }
const auto validCount = std::count_if(parents.begin(), parents.end(),
if (!m_proxy->persistent().isEmpty()) { [](const auto &idx) { return idx.isValid(); });
qWarning("%s: proxy should have no persistent indexes when storePersistent called", if (m_proxy->persistentIndexList().size() != validCount) {
qWarning("%s: proxy should have no additional persistent indexes when storePersistent called",
Q_FUNC_INFO); Q_FUNC_INFO);
++storePersistentFailureCount; ++storePersistentFailureCount;
} }
storePersistent(QModelIndex()); storePersistentRecursive(QModelIndex());
if (m_proxy->persistent().isEmpty()) { if (m_proxy->persistentIndexList().isEmpty()) {
qWarning("%s: proxy should have persistent index after storePersistent called", qWarning("%s: proxy should have persistent index after storePersistent called",
Q_FUNC_INFO); Q_FUNC_INFO);
++storePersistentFailureCount; ++storePersistentFailureCount;
@ -211,6 +209,9 @@ public slots:
void checkPersistent() void checkPersistent()
{ {
QVERIFY(!m_persistentProxyIndexes.isEmpty());
QVERIFY(!m_persistentSourceIndexes.isEmpty());
for (int row = 0; row < m_persistentProxyIndexes.size(); ++row) { for (int row = 0; row < m_persistentProxyIndexes.size(); ++row) {
m_persistentProxyIndexes.at(row); m_persistentProxyIndexes.at(row);
m_persistentSourceIndexes.at(row); m_persistentSourceIndexes.at(row);