summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp')
-rw-r--r--tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp223
1 files changed, 178 insertions, 45 deletions
diff --git a/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp b/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
index 4406d40986..62512889fd 100644
--- a/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
+++ b/tests/auto/corelib/itemmodels/qabstractproxymodel/tst_qabstractproxymodel.cpp
@@ -1,32 +1,8 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include <QtTest/QtTest>
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QTest>
+#include <QtTest/private/qpropertytesthelper_p.h>
#include <qabstractproxymodel.h>
#include <QItemSelection>
#include <qstandarditemmodel.h>
@@ -43,6 +19,7 @@ private slots:
void flags();
void headerData_data();
void headerData();
+ void headerDataInBounds();
void itemData_data();
void itemData();
void mapFromSource_data();
@@ -60,6 +37,7 @@ private slots:
void testRoleNames();
void testSwappingRowsProxy();
void testDragAndDrop();
+ void sourceModelBinding();
};
// Subclass that exposes the protected functions.
@@ -67,29 +45,29 @@ class SubQAbstractProxyModel : public QAbstractProxyModel
{
public:
// QAbstractProxyModel::mapFromSource is a pure virtual function.
- QModelIndex mapFromSource(QModelIndex const& sourceIndex) const
+ QModelIndex mapFromSource(QModelIndex const& sourceIndex) const override
{ Q_UNUSED(sourceIndex); return QModelIndex(); }
// QAbstractProxyModel::mapToSource is a pure virtual function.
- QModelIndex mapToSource(QModelIndex const& proxyIndex) const
+ QModelIndex mapToSource(QModelIndex const& proxyIndex) const override
{ Q_UNUSED(proxyIndex); return QModelIndex(); }
- QModelIndex index(int, int, const QModelIndex&) const
+ QModelIndex index(int, int, const QModelIndex&) const override
{
return QModelIndex();
}
- QModelIndex parent(const QModelIndex&) const
+ QModelIndex parent(const QModelIndex&) const override
{
return QModelIndex();
}
- int rowCount(const QModelIndex&) const
+ int rowCount(const QModelIndex&) const override
{
return 0;
}
- int columnCount(const QModelIndex&) const
+ int columnCount(const QModelIndex&) const override
{
return 0;
}
@@ -172,6 +150,133 @@ void tst_QAbstractProxyModel::headerData()
QCOMPARE(model.headerData(section, orientation, role), headerData);
}
+class SimpleTableReverseColumnsProxy : public QAbstractProxyModel
+{
+public:
+ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
+ {
+ if (parent.isValid())
+ return {};
+
+ if (row < 0 || row >= rowCount() || column < 0 || column >= columnCount())
+ qFatal("error"); // cannot QFAIL here
+
+ return createIndex(row, column);
+ }
+
+ int rowCount(const QModelIndex &parent = QModelIndex()) const override
+ {
+ if (parent.isValid())
+ return 0;
+ return sourceModel()->rowCount();
+ }
+
+ int columnCount(const QModelIndex &parent = QModelIndex()) const override
+ {
+ if (parent.isValid())
+ return 0;
+ return sourceModel()->columnCount();
+ }
+
+ QModelIndex parent(const QModelIndex &) const override
+ {
+ return QModelIndex();
+ }
+
+ QModelIndex mapToSource(const QModelIndex &idx) const override
+ {
+ if (!idx.isValid())
+ return QModelIndex();
+ return sourceModel()->index(idx.row(), columnCount() - 1 - idx.column());
+ }
+
+ QModelIndex mapFromSource(const QModelIndex &idx) const override
+ {
+ if (idx.parent().isValid())
+ return QModelIndex();
+ return createIndex(idx.row(), columnCount() - 1 - idx.column());
+ }
+};
+
+void tst_QAbstractProxyModel::headerDataInBounds()
+{
+ QStandardItemModel qsim(0, 5);
+ qsim.setHorizontalHeaderLabels({"Col1", "Col2", "Col3", "Col4", "Col5"});
+
+ SimpleTableReverseColumnsProxy proxy;
+ QSignalSpy headerDataChangedSpy(&proxy, &QAbstractItemModel::headerDataChanged);
+ QVERIFY(headerDataChangedSpy.isValid());
+ proxy.setSourceModel(&qsim);
+ QCOMPARE(proxy.rowCount(), 0);
+ QCOMPARE(proxy.columnCount(), 5);
+
+ for (int i = 0; i < proxy.columnCount(); ++i) {
+ QString expected = QString("Col%1").arg(i + 1);
+ QCOMPARE(proxy.headerData(i, Qt::Horizontal).toString(), expected);
+ }
+
+ qsim.appendRow({
+ new QStandardItem("A"),
+ new QStandardItem("B"),
+ new QStandardItem("C"),
+ new QStandardItem("D"),
+ new QStandardItem("E")
+ });
+
+ QCOMPARE(proxy.rowCount(), 1);
+ QCOMPARE(proxy.columnCount(), 5);
+ QTRY_COMPARE(headerDataChangedSpy.size(), 1);
+ QCOMPARE(headerDataChangedSpy[0][0].value<Qt::Orientation>(), Qt::Horizontal);
+ QCOMPARE(headerDataChangedSpy[0][1].value<int>(), 0);
+ QCOMPARE(headerDataChangedSpy[0][2].value<int>(), 4);
+
+ for (int i = 0; i < proxy.columnCount(); ++i) {
+ QString expected = QString("Col%1").arg(proxy.columnCount() - i);
+ QCOMPARE(proxy.headerData(i, Qt::Horizontal).toString(), expected);
+ }
+
+ qsim.appendRow({
+ new QStandardItem("A"),
+ new QStandardItem("B"),
+ new QStandardItem("C"),
+ new QStandardItem("D"),
+ new QStandardItem("E")
+ });
+ QCOMPARE(proxy.rowCount(), 2);
+ QCOMPARE(proxy.columnCount(), 5);
+ QCOMPARE(headerDataChangedSpy.size(), 1);
+
+ for (int i = 0; i < proxy.columnCount(); ++i) {
+ QString expected = QString("Col%1").arg(proxy.columnCount() - i);
+ QCOMPARE(proxy.headerData(i, Qt::Horizontal).toString(), expected);
+ }
+
+ QVERIFY(qsim.removeRows(0, 1));
+
+ QCOMPARE(proxy.rowCount(), 1);
+ QCOMPARE(proxy.columnCount(), 5);
+ QCOMPARE(headerDataChangedSpy.size(), 1);
+
+ for (int i = 0; i < proxy.columnCount(); ++i) {
+ QString expected = QString("Col%1").arg(proxy.columnCount() - i);
+ QCOMPARE(proxy.headerData(i, Qt::Horizontal).toString(), expected);
+ }
+
+ QVERIFY(qsim.removeRows(0, 1));
+
+ QCOMPARE(proxy.rowCount(), 0);
+ QCOMPARE(proxy.columnCount(), 5);
+ QTRY_COMPARE(headerDataChangedSpy.size(), 2);
+ QCOMPARE(headerDataChangedSpy[1][0].value<Qt::Orientation>(), Qt::Horizontal);
+ QCOMPARE(headerDataChangedSpy[1][1].value<int>(), 0);
+ QCOMPARE(headerDataChangedSpy[1][2].value<int>(), 4);
+
+ for (int i = 0; i < proxy.columnCount(); ++i) {
+ QString expected = QString("Col%1").arg(i + 1);
+ QCOMPARE(proxy.headerData(i, Qt::Horizontal).toString(), expected);
+ }
+}
+
void tst_QAbstractProxyModel::itemData_data()
{
QTest::addColumn<QModelIndex>("index");
@@ -186,7 +291,7 @@ void tst_QAbstractProxyModel::itemData()
QFETCH(QModelIndex, index);
QFETCH(int, count);
SubQAbstractProxyModel model;
- QCOMPARE(model.itemData(index).count(), count);
+ QCOMPARE(model.itemData(index).size(), count);
}
void tst_QAbstractProxyModel::mapFromSource_data()
@@ -399,7 +504,7 @@ class SwappingProxy : public QAbstractProxyModel
}
}
public:
- virtual QModelIndex index(int row, int column, const QModelIndex &parentIdx) const
+ virtual QModelIndex index(int row, int column, const QModelIndex &parentIdx) const override
{
if (!sourceModel())
return QModelIndex();
@@ -412,28 +517,28 @@ public:
return createIndex(row, column, parentIdx.internalPointer());
}
- virtual QModelIndex parent(const QModelIndex &parentIdx) const
+ virtual QModelIndex parent(const QModelIndex &parentIdx) const override
{
// well, we're a 2D model
Q_UNUSED(parentIdx);
return QModelIndex();
}
- virtual int rowCount(const QModelIndex &parentIdx) const
+ virtual int rowCount(const QModelIndex &parentIdx) const override
{
if (parentIdx.isValid() || !sourceModel())
return 0;
return sourceModel()->rowCount();
}
- virtual int columnCount(const QModelIndex &parentIdx) const
+ virtual int columnCount(const QModelIndex &parentIdx) const override
{
if (parentIdx.isValid() || !sourceModel())
return 0;
return sourceModel()->rowCount();
}
- virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
+ virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
{
if (!proxyIndex.isValid())
return QModelIndex();
@@ -443,7 +548,7 @@ public:
return sourceModel()->index(swapRow(proxyIndex.row()), proxyIndex.column(), QModelIndex());
}
- virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
+ virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
{
if (!sourceIndex.isValid())
return QModelIndex();
@@ -485,9 +590,9 @@ void tst_QAbstractProxyModel::testSwappingRowsProxy()
class StandardItemModelWithCustomDragAndDrop : public QStandardItemModel
{
public:
- QStringList mimeTypes() const { return QStringList() << QStringLiteral("foo/mimetype"); }
- Qt::DropActions supportedDragActions() const { return Qt::CopyAction | Qt::LinkAction; }
- Qt::DropActions supportedDropActions() const { return Qt::MoveAction; }
+ QStringList mimeTypes() const override { return QStringList() << QStringLiteral("foo/mimetype"); }
+ Qt::DropActions supportedDragActions() const override { return Qt::CopyAction | Qt::LinkAction; }
+ Qt::DropActions supportedDropActions() const override { return Qt::MoveAction; }
};
void tst_QAbstractProxyModel::testDragAndDrop()
@@ -500,6 +605,34 @@ void tst_QAbstractProxyModel::testDragAndDrop()
QCOMPARE(proxy.supportedDropActions(), sourceModel.supportedDropActions());
}
+void tst_QAbstractProxyModel::sourceModelBinding()
+{
+ SubQAbstractProxyModel proxy;
+ QStandardItemModel model1;
+ QStandardItemModel model2;
+ QTestPrivate::testReadWritePropertyBasics<SubQAbstractProxyModel, QAbstractItemModel *>(
+ proxy, &model1, &model2, "sourceModel");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed model - model test");
+ return;
+ }
+
+ proxy.setSourceModel(&model2);
+ QTestPrivate::testReadWritePropertyBasics<SubQAbstractProxyModel, QAbstractItemModel *>(
+ proxy, &model1, nullptr, "sourceModel");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed model - nullptr test");
+ return;
+ }
+
+ proxy.setSourceModel(&model1);
+ QTestPrivate::testReadWritePropertyBasics<SubQAbstractProxyModel, QAbstractItemModel *>(
+ proxy, nullptr, &model2, "sourceModel");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed nullptr - model test");
+ return;
+ }
+}
QTEST_MAIN(tst_QAbstractProxyModel)
#include "tst_qabstractproxymodel.moc"