File indexing completed on 2024-03-03 04:04:21

0001 /*
0002     SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QAbstractItemModelTester>
0008 #include <QSignalSpy>
0009 #include <QStandardItemModel>
0010 #include <QTest>
0011 
0012 #include <KColumnHeadersModel>
0013 
0014 class KColumnHeadersModelTest : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 private Q_SLOTS:
0019     void testStatic()
0020     {
0021         auto model = new KColumnHeadersModel{this};
0022 
0023         auto sourceModel = new QStandardItemModel{this};
0024         sourceModel->setHorizontalHeaderLabels(
0025             {QStringLiteral("Test 1"), QStringLiteral("Test 2"), QStringLiteral("Test 3"), QStringLiteral("Test 4"), QStringLiteral("Test 5")});
0026 
0027         model->setSourceModel(sourceModel);
0028 
0029         new QAbstractItemModelTester(model, this);
0030 
0031         QCOMPARE(model->rowCount(), 5);
0032         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0033         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0034         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0035         QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4"));
0036         QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5"));
0037 
0038         QSignalSpy spy{model, &QAbstractItemModel::dataChanged};
0039         QVERIFY(spy.isValid());
0040 
0041         sourceModel->setHorizontalHeaderLabels(
0042             {QStringLiteral("Test 5"), QStringLiteral("Test 4"), QStringLiteral("Test 3"), QStringLiteral("Test 2"), QStringLiteral("Test 1")});
0043 
0044         QCOMPARE(spy.count(), 4);
0045     }
0046 
0047     void testAddColumns()
0048     {
0049         auto model = new KColumnHeadersModel{this};
0050         auto sourceModel = new QStandardItemModel{this};
0051         sourceModel->setHorizontalHeaderLabels({QStringLiteral("Test 1"), QStringLiteral("Test 2")});
0052         model->setSourceModel(sourceModel);
0053 
0054         new QAbstractItemModelTester(model, this);
0055 
0056         QSignalSpy spy{model, &QAbstractItemModel::rowsInserted};
0057         QVERIFY(spy.isValid());
0058 
0059         QCOMPARE(model->rowCount(), 2);
0060         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0061         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0062 
0063         sourceModel->setHorizontalHeaderLabels({QStringLiteral("Test 1"), QStringLiteral("Test 2"), QStringLiteral("Test 3")});
0064 
0065         QCOMPARE(spy.count(), 1);
0066         QCOMPARE(model->rowCount(), 3);
0067         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0068         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0069         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0070 
0071         sourceModel->setHorizontalHeaderLabels(
0072             {QStringLiteral("Test 1"), QStringLiteral("Test 2"), QStringLiteral("Test 3"), QStringLiteral("Test 4"), QStringLiteral("Test 5")});
0073 
0074         QCOMPARE(spy.count(), 2);
0075         QCOMPARE(model->rowCount(), 5);
0076         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0077         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0078         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0079         QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4"));
0080         QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5"));
0081 
0082         sourceModel->setHorizontalHeaderLabels({QStringLiteral("Test 1"),
0083                                                 QStringLiteral("Test 2"),
0084                                                 QStringLiteral("Test 6"),
0085                                                 QStringLiteral("Test 3"),
0086                                                 QStringLiteral("Test 4"),
0087                                                 QStringLiteral("Test 5")});
0088 
0089         QCOMPARE(spy.count(), 3);
0090         QCOMPARE(model->rowCount(), 6);
0091         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0092         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0093         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 6"));
0094         QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0095         QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4"));
0096         QCOMPARE(model->data(model->index(5, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5"));
0097     }
0098 
0099     void testRemoveColumns()
0100     {
0101         auto model = new KColumnHeadersModel{this};
0102 
0103         auto sourceModel = new QStandardItemModel{this};
0104         sourceModel->setHorizontalHeaderLabels(
0105             {QStringLiteral("Test 1"), QStringLiteral("Test 2"), QStringLiteral("Test 3"), QStringLiteral("Test 4"), QStringLiteral("Test 5")});
0106 
0107         model->setSourceModel(sourceModel);
0108 
0109         new QAbstractItemModelTester(model, this);
0110 
0111         QSignalSpy spy{model, &QAbstractItemModel::rowsRemoved};
0112         QVERIFY(spy.isValid());
0113 
0114         QCOMPARE(model->rowCount(), 5);
0115         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0116         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0117         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0118         QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4"));
0119         QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5"));
0120 
0121         sourceModel->takeColumn(4);
0122 
0123         QCOMPARE(spy.count(), 1);
0124 
0125         QCOMPARE(model->rowCount(), 4);
0126         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0127         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2"));
0128         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0129         QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4"));
0130 
0131         sourceModel->takeColumn(1);
0132 
0133         QCOMPARE(spy.count(), 2);
0134 
0135         QCOMPARE(model->rowCount(), 3);
0136         QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1"));
0137         QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3"));
0138         QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4"));
0139     }
0140 };
0141 
0142 QTEST_MAIN(KColumnHeadersModelTest)
0143 
0144 #include "kcolumnheadersmodeltest.moc"