QAbstractItemModelTester: fix out-of-bounds index() calls

When removing rows, the tester is looking at the data of the row
"just before" and the row "just after" the removed rows, to see if
they are still the same at the end of the removal operation.
Guard this with bounds check, in case there is no row just before
or just after.

This is the opportunity to use modeltester in tst_qidentityproxymodel,
which was already a testcase for removing the only row in a given parent.

Change-Id: Iec8228c16b9c670b794e2665356d153679178494
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
David Faure 2018-06-25 15:50:35 +02:00
parent 74be42ca59
commit 27ea5a65dd
2 changed files with 24 additions and 7 deletions

View File

@ -712,8 +712,17 @@ void QAbstractItemModelTesterPrivate::rowsAboutToBeRemoved(const QModelIndex &pa
Changing c;
c.parent = parent;
c.oldSize = model->rowCount(parent);
c.last = model->data(model->index(start - 1, 0, parent));
c.next = model->data(model->index(end + 1, 0, parent));
if (start > 0) {
const QModelIndex startIndex = model->index(start - 1, 0, parent);
MODELTESTER_VERIFY(startIndex.isValid());
c.last = model->data(startIndex);
}
if (end < c.oldSize - 1) {
const QModelIndex endIndex = model->index(end + 1, 0, parent);
MODELTESTER_VERIFY(endIndex.isValid());
c.next = model->data(endIndex);
}
remove.push(c);
}
@ -732,8 +741,10 @@ void QAbstractItemModelTesterPrivate::rowsRemoved(const QModelIndex &parent, int
Changing c = remove.pop();
MODELTESTER_COMPARE(parent, c.parent);
MODELTESTER_COMPARE(model->rowCount(parent), c.oldSize - (end - start + 1));
MODELTESTER_COMPARE(model->data(model->index(start - 1, 0, c.parent)), c.last);
MODELTESTER_COMPARE(model->data(model->index(start, 0, c.parent)), c.next);
if (start > 0)
MODELTESTER_COMPARE(model->data(model->index(start - 1, 0, c.parent)), c.last);
if (end < c.oldSize - 1)
MODELTESTER_COMPARE(model->data(model->index(start, 0, c.parent)), c.next);
}
void QAbstractItemModelTesterPrivate::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)

View File

@ -26,9 +26,12 @@
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtCore/QCoreApplication>
#include <QtGui/QStandardItemModel>
#include <QAbstractItemModelTester>
#include <QCoreApplication>
#include <QSignalSpy>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QTest>
#include "dynamictreemodel.h"
#include "qidentityproxymodel.h"
@ -76,6 +79,7 @@ protected:
private:
QStandardItemModel *m_model;
QIdentityProxyModel *m_proxy;
QAbstractItemModelTester *m_modelTest;
};
tst_QIdentityProxyModel::tst_QIdentityProxyModel()
@ -88,12 +92,14 @@ void tst_QIdentityProxyModel::initTestCase()
qRegisterMetaType<QVector<int> >();
m_model = new QStandardItemModel(0, 1);
m_proxy = new QIdentityProxyModel();
m_modelTest = new QAbstractItemModelTester(m_proxy, this);
}
void tst_QIdentityProxyModel::cleanupTestCase()
{
delete m_proxy;
delete m_model;
delete m_modelTest;
}
void tst_QIdentityProxyModel::cleanup()