File indexing completed on 2024-05-19 16:37:10

0001 /*
0002     SPDX-FileCopyrightText: 2012 Aurélien Gâteau <agateau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 #include <sortfiltermodeltest.h>
0007 
0008 #include <declarativeimports/core/datamodel.h>
0009 
0010 // KDE
0011 
0012 // Qt
0013 #include <QSignalSpy>
0014 #include <QStandardItemModel>
0015 #include <QStringListModel>
0016 #include <QTest>
0017 
0018 using namespace Plasma;
0019 
0020 QTEST_MAIN(SortFilterModelTest)
0021 
0022 void SortFilterModelTest::setModel()
0023 {
0024     // TODO: Actually test model change
0025     QStandardItemModel model;
0026 
0027     SortFilterModel filterModel;
0028     QSignalSpy spy(&filterModel, SIGNAL(sourceModelChanged(QObject *)));
0029 
0030     filterModel.setModel(&model);
0031     QCOMPARE(spy.count(), 1);
0032     QList<QVariant> arguments = spy.takeFirst();
0033     QCOMPARE(arguments.at(0).value<QObject *>(), static_cast<QObject *>(&model));
0034 
0035     filterModel.setModel(&model);
0036     QCOMPARE(spy.count(), 0);
0037 }
0038 
0039 void SortFilterModelTest::setEmptyModel()
0040 {
0041     SortFilterModel filterModel;
0042     QStandardItemModel model;
0043     filterModel.setModel(&model);
0044     QCOMPARE(filterModel.sourceModel(), static_cast<QAbstractItemModel *>(&model));
0045     filterModel.setModel(nullptr);
0046     QCOMPARE(filterModel.sourceModel(), static_cast<QAbstractItemModel *>(nullptr));
0047 }
0048 
0049 void SortFilterModelTest::setFilterRegExp()
0050 {
0051     // TODO: Actually test filtering
0052     SortFilterModel filterModel;
0053     QSignalSpy spy(&filterModel, SIGNAL(filterRegExpChanged(QString)));
0054 
0055     filterModel.setFilterRegExp(QStringLiteral("foo"));
0056     QCOMPARE(spy.count(), 1);
0057     QList<QVariant> arguments = spy.takeFirst();
0058     QCOMPARE(arguments.at(0).toString(), QStringLiteral("foo"));
0059 
0060     filterModel.setFilterRegExp(QStringLiteral("foo"));
0061     QCOMPARE(spy.count(), 0);
0062 }
0063 
0064 void SortFilterModelTest::mapRowToSource()
0065 {
0066     QStringList list = QStringList() << QStringLiteral("Foo") << QStringLiteral("Bar") << QStringLiteral("Baz");
0067     QStringListModel model(list);
0068 
0069     SortFilterModel filterModel;
0070     filterModel.setSourceModel(&model);
0071 
0072     QCOMPARE(filterModel.mapRowToSource(0), 0);
0073     QCOMPARE(filterModel.mapRowToSource(2), 2);
0074     QCOMPARE(filterModel.mapRowToSource(3), -1);
0075     QCOMPARE(filterModel.mapRowToSource(-1), -1);
0076 
0077     filterModel.setFilterRegExp(QStringLiteral("Ba"));
0078     // filterModel now contains "Bar" and "Baz"
0079     QCOMPARE(filterModel.mapRowToSource(0), 1);
0080     QCOMPARE(filterModel.mapRowToSource(1), 2);
0081     QCOMPARE(filterModel.mapRowToSource(2), -1);
0082     QCOMPARE(filterModel.mapRowToSource(-1), -1);
0083 }
0084 
0085 void SortFilterModelTest::mapRowFromSource()
0086 {
0087     QStringList list = QStringList() << QStringLiteral("Foo") << QStringLiteral("Bar") << QStringLiteral("Baz");
0088     QStringListModel model(list);
0089 
0090     SortFilterModel filterModel;
0091     filterModel.setSourceModel(&model);
0092 
0093     QCOMPARE(filterModel.mapRowFromSource(0), 0);
0094     QCOMPARE(filterModel.mapRowFromSource(2), 2);
0095     QCOMPARE(filterModel.mapRowFromSource(3), -1);
0096     QCOMPARE(filterModel.mapRowFromSource(-1), -1);
0097 
0098     filterModel.setFilterRegExp(QStringLiteral("Ba"));
0099     // filterModel now contains "Bar" and "Baz"
0100     QCOMPARE(filterModel.mapRowFromSource(0), -1);
0101     QCOMPARE(filterModel.mapRowFromSource(1), 0);
0102     QCOMPARE(filterModel.mapRowFromSource(2), 1);
0103     QCOMPARE(filterModel.mapRowFromSource(-1), -1);
0104 }
0105 
0106 #include "moc_sortfiltermodeltest.cpp"