File indexing completed on 2024-11-10 04:40:15

0001 /*
0002     SPDX-FileCopyrightText: 2010 Till Adam <till@kdab.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include <QSortFilterProxyModel>
0010 #include <QStandardItemModel>
0011 
0012 class KRFPTestModel : public QSortFilterProxyModel
0013 {
0014 public:
0015     explicit KRFPTestModel(QObject *parent)
0016         : QSortFilterProxyModel(parent)
0017     {
0018     }
0019     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
0020     {
0021         const QModelIndex modelIndex = sourceModel()->index(sourceRow, 0, sourceParent);
0022         return !modelIndex.data().toString().contains(QLatin1StringView("three"));
0023     }
0024 };
0025 
0026 class ProxyModelsTest : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 private Q_SLOTS:
0031     void initTestCase();
0032 
0033     void init();
0034 
0035     void testMatch();
0036 
0037 private:
0038     QStandardItemModel m_model;
0039     QSortFilterProxyModel *m_krfp = nullptr;
0040     KRFPTestModel *m_krfptest = nullptr;
0041 };
0042 
0043 void ProxyModelsTest::initTestCase()
0044 {
0045 }
0046 
0047 void ProxyModelsTest::init()
0048 {
0049     m_model.setRowCount(5);
0050     m_model.setColumnCount(1);
0051     m_model.setData(m_model.index(0, 0, QModelIndex()), QStringLiteral("one"));
0052     QModelIndex idx = m_model.index(1, 0, QModelIndex());
0053     m_model.setData(idx, QStringLiteral("two"));
0054     m_model.insertRows(0, 1, idx);
0055     m_model.insertColumns(0, 1, idx);
0056     m_model.setData(m_model.index(0, 0, idx), QStringLiteral("three"));
0057     m_model.setData(m_model.index(2, 0, QModelIndex()), QStringLiteral("three"));
0058     m_model.setData(m_model.index(3, 0, QModelIndex()), QStringLiteral("four"));
0059     m_model.setData(m_model.index(4, 0, QModelIndex()), QStringLiteral("five"));
0060 
0061     m_model.setData(m_model.index(4, 0, QModelIndex()), QStringLiteral("mystuff"), Qt::UserRole + 42);
0062 
0063     m_krfp = new QSortFilterProxyModel(this);
0064     m_krfp->setSourceModel(&m_model);
0065     m_krfp->setRecursiveFilteringEnabled(true);
0066     m_krfptest = new KRFPTestModel(this);
0067     m_krfptest->setSourceModel(m_krfp);
0068 
0069     // some sanity checks that setup worked
0070     QCOMPARE(m_model.rowCount(QModelIndex()), 5);
0071     QCOMPARE(m_model.data(m_model.index(0, 0)).toString(), QStringLiteral("one"));
0072     QCOMPARE(m_krfp->rowCount(QModelIndex()), 5);
0073     QCOMPARE(m_krfp->data(m_krfp->index(0, 0)).toString(), QStringLiteral("one"));
0074     QCOMPARE(m_krfptest->rowCount(QModelIndex()), 4);
0075     QCOMPARE(m_krfptest->data(m_krfptest->index(0, 0)).toString(), QStringLiteral("one"));
0076 
0077     QCOMPARE(m_krfp->rowCount(m_krfp->index(1, 0)), 1);
0078     QCOMPARE(m_krfptest->rowCount(m_krfptest->index(1, 0)), 0);
0079 }
0080 
0081 void ProxyModelsTest::testMatch()
0082 {
0083     QModelIndexList results = m_model.match(m_model.index(0, 0), Qt::DisplayRole, QStringLiteral("three"));
0084     QCOMPARE(results.size(), 1);
0085     results = m_model.match(m_model.index(0, 0), Qt::DisplayRole, QStringLiteral("fourtytwo"));
0086     QCOMPARE(results.size(), 0);
0087     results = m_model.match(m_model.index(0, 0), Qt::UserRole + 42, QStringLiteral("mystuff"));
0088     QCOMPARE(results.size(), 1);
0089 
0090     results = m_krfp->match(m_krfp->index(0, 0), Qt::DisplayRole, QStringLiteral("three"));
0091     QCOMPARE(results.size(), 1);
0092     results = m_krfp->match(m_krfp->index(0, 0), Qt::UserRole + 42, QStringLiteral("mystuff"));
0093     QCOMPARE(results.size(), 1);
0094 
0095     results = m_krfptest->match(m_krfptest->index(0, 0), Qt::DisplayRole, QStringLiteral("three"));
0096     QCOMPARE(results.size(), 0);
0097     results = m_krfptest->match(m_krfptest->index(0, 0), Qt::UserRole + 42, QStringLiteral("mystuff"));
0098     QCOMPARE(results.size(), 1);
0099 
0100     results = m_model.match(QModelIndex(), Qt::DisplayRole, QStringLiteral("three"));
0101     QCOMPARE(results.size(), 0);
0102     results = m_krfp->match(QModelIndex(), Qt::DisplayRole, QStringLiteral("three"));
0103     QCOMPARE(results.size(), 0);
0104     results = m_krfptest->match(QModelIndex(), Qt::DisplayRole, QStringLiteral("three"));
0105     QCOMPARE(results.size(), 0);
0106 
0107     const QModelIndex index = m_model.index(0, 0, QModelIndex());
0108     results = m_model.match(index, Qt::DisplayRole, QStringLiteral("three"), -1, Qt::MatchRecursive | Qt::MatchStartsWith | Qt::MatchWrap);
0109     QCOMPARE(results.size(), 2);
0110 }
0111 
0112 #include "proxymodelstest.moc"
0113 
0114 QTEST_MAIN(ProxyModelsTest)