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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Stephen Kelly <stephen@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "proxymodeltestsuite/proxymodeltest.h"
0009 #include <kdescendantsproxymodel.h>
0010 
0011 #include <QApplication>
0012 #include <QDebug>
0013 
0014 class TestData : public ProxyModelTestData
0015 {
0016     Q_OBJECT
0017 public:
0018     TestData(ProxyModelTest *parent = nullptr)
0019         : ProxyModelTestData(parent)
0020     {
0021     }
0022 
0023     void
0024     newInsertWithDataChangedTest(const QString &name, const IndexFinder &indexFinder, int start, int end, int rowCount, const QList<int> &indexWithDataChanged)
0025     {
0026         processTestName(name);
0027 
0028         SignalList signalList;
0029         PersistentChangeList persistentList;
0030 
0031         signalList << m_proxyModelTest->getSignal(RowsAboutToBeInserted, indexFinder, start, end);
0032         signalList << m_proxyModelTest->getSignal(RowsInserted, indexFinder, start, end);
0033 
0034         for (int index : indexWithDataChanged) {
0035             const IndexFinder changedFinder({index});
0036             signalList << m_proxyModelTest->getSignal(DataChanged, changedFinder, changedFinder);
0037         }
0038 
0039         if (rowCount - 1 + (end - start + 1) > end) {
0040             persistentList << m_proxyModelTest->getChange(indexFinder, start, rowCount - 1, end - start + 1);
0041         }
0042 
0043         QTest::newRow(name.toLatin1()) << signalList << persistentList;
0044     }
0045 
0046     void
0047     newRemoveWithDataChangedTest(const QString &name, const IndexFinder &indexFinder, int start, int end, int rowCount, const QList<int> &indexWithDataChanged)
0048     {
0049         processTestName(name);
0050 
0051         SignalList signalList;
0052         PersistentChangeList persistentList;
0053 
0054         signalList << m_proxyModelTest->getSignal(RowsAboutToBeRemoved, indexFinder, start, end);
0055         signalList << m_proxyModelTest->getSignal(RowsRemoved, indexFinder, start, end);
0056 
0057         for (int index : indexWithDataChanged) {
0058             const IndexFinder changedFinder({index});
0059             signalList << m_proxyModelTest->getSignal(DataChanged, changedFinder, changedFinder);
0060         }
0061 
0062         persistentList << m_proxyModelTest->getChange(indexFinder, start, end, -1, true);
0063         if (rowCount - 1 != end) {
0064             persistentList << m_proxyModelTest->getChange(indexFinder, end + 1, rowCount - 1, -1 * (end - start + 1));
0065         }
0066 
0067         QTest::newRow(name.toLatin1()) << signalList << persistentList;
0068     }
0069 
0070 public Q_SLOTS:
0071     void testInsertWhenEmptyData() override
0072     {
0073         QTest::addColumn<SignalList>("signalList");
0074         QTest::addColumn<PersistentChangeList>("changeList");
0075 
0076         static const IndexFinder indexFinder;
0077 
0078         newInsertTest(QStringLiteral("insert01"), indexFinder, 0, 0, 0);
0079         newInsertTest(QStringLiteral("insert02"), indexFinder, 0, 9, 0);
0080 
0081         // The test suite can't handle tree insertion yet, so we skip it for now.
0082         skipTestData(QStringLiteral("insert03"));
0083 
0084         //     processTestName("insert03");
0085 
0086         //     SignalList signalList;
0087         //     PersistentChangeList persistentList;
0088 
0089         //     partialTest(&signalList, &persistentList, indexFinder, 0, 4, 0);
0090         //     partialTest(&signalList, &persistentList, indexFinder, 1, 1, 5);
0091         //     partialTest(&signalList, &persistentList, indexFinder, 4, 4, 6);
0092         //     partialTest(&signalList, &persistentList, indexFinder, 7, 7, 7);
0093         //     partialTest(&signalList, &persistentList, indexFinder, 5, 5, 8);
0094 
0095         //     QTest::newRow("insert03") << signalList << persistentList;
0096     }
0097 
0098     void testInsertInRootData() override
0099     {
0100         QTest::addColumn<SignalList>("signalList");
0101         QTest::addColumn<PersistentChangeList>("changeList");
0102 
0103         static const IndexFinder indexFinder;
0104 
0105         newInsertTest(QStringLiteral("insert01"), indexFinder, 0, 0, 43);
0106         newInsertTest(QStringLiteral("insert02"), indexFinder, 0, 9, 43);
0107         newInsertWithDataChangedTest(QStringLiteral("insert03"), indexFinder, 43, 43, 43, QList<int>({42}));
0108         newInsertWithDataChangedTest(QStringLiteral("insert04"), indexFinder, 43, 52, 43, QList<int>({42}));
0109         newInsertWithDataChangedTest(QStringLiteral("insert05"), indexFinder, 7, 7, 43, QList<int>({6}));
0110         newInsertWithDataChangedTest(QStringLiteral("insert06"), indexFinder, 7, 16, 43, QList<int>({6}));
0111         skipTestData(QStringLiteral("insert07"));
0112         skipTestData(QStringLiteral("insert08"));
0113         skipTestData(QStringLiteral("insert09"));
0114         skipTestData(QStringLiteral("insert10"));
0115         skipTestData(QStringLiteral("insert11"));
0116         skipTestData(QStringLiteral("insert12"));
0117         skipTestData(QStringLiteral("insert13"));
0118         skipTestData(QStringLiteral("insert14"));
0119         skipTestData(QStringLiteral("insert15"));
0120         skipTestData(QStringLiteral("insert16"));
0121         skipTestData(QStringLiteral("insert17"));
0122         skipTestData(QStringLiteral("insert18"));
0123     }
0124 
0125     void testInsertInTopLevelData() override
0126     {
0127         QTest::addColumn<SignalList>("signalList");
0128         QTest::addColumn<PersistentChangeList>("changeList");
0129 
0130         static const IndexFinder indexFinder;
0131 
0132         newInsertWithDataChangedTest(QStringLiteral("insert01"), indexFinder, 9, 9, 43, QList<int>({8}));
0133         newInsertWithDataChangedTest(QStringLiteral("insert02"), indexFinder, 9, 18, 43, QList<int>({8}));
0134         newInsertWithDataChangedTest(QStringLiteral("insert03"), indexFinder, 37, 37, 43, QList<int>({8, 36}));
0135 
0136         newInsertWithDataChangedTest(QStringLiteral("insert04"), indexFinder, 37, 46, 43, QList<int>({8, 36}));
0137         newInsertWithDataChangedTest(QStringLiteral("insert05"), indexFinder, 15, 15, 43, QList<int>({8, 14}));
0138         newInsertWithDataChangedTest(QStringLiteral("insert06"), indexFinder, 15, 24, 43, QList<int>({8, 14}));
0139         skipTestData(QStringLiteral("insert07"));
0140         skipTestData(QStringLiteral("insert08"));
0141         skipTestData(QStringLiteral("insert09"));
0142         skipTestData(QStringLiteral("insert10"));
0143         skipTestData(QStringLiteral("insert11"));
0144         skipTestData(QStringLiteral("insert12"));
0145         skipTestData(QStringLiteral("insert13"));
0146         skipTestData(QStringLiteral("insert14"));
0147         skipTestData(QStringLiteral("insert15"));
0148         skipTestData(QStringLiteral("insert16"));
0149         skipTestData(QStringLiteral("insert17"));
0150         skipTestData(QStringLiteral("insert18"));
0151     }
0152 
0153     void testInsertInSecondLevelData() override
0154     {
0155         QTest::addColumn<SignalList>("signalList");
0156         QTest::addColumn<PersistentChangeList>("changeList");
0157 
0158         static const IndexFinder indexFinder;
0159 
0160         newInsertWithDataChangedTest(QStringLiteral("insert01"), indexFinder, 17, 17, 43, QList<int>({16}));
0161         newInsertWithDataChangedTest(QStringLiteral("insert02"), indexFinder, 17, 26, 43, QList<int>({16}));
0162         newInsertWithDataChangedTest(QStringLiteral("insert03"), indexFinder, 32, 32, 43, QList<int>({16, 31}));
0163         newInsertWithDataChangedTest(QStringLiteral("insert04"), indexFinder, 32, 41, 43, QList<int>({16, 31}));
0164         newInsertWithDataChangedTest(QStringLiteral("insert05"), indexFinder, 23, 23, 43, QList<int>({16, 22}));
0165         newInsertWithDataChangedTest(QStringLiteral("insert06"), indexFinder, 23, 32, 43, QList<int>({16, 22}));
0166         skipTestData(QStringLiteral("insert07"));
0167         skipTestData(QStringLiteral("insert08"));
0168         skipTestData(QStringLiteral("insert09"));
0169         skipTestData(QStringLiteral("insert10"));
0170         skipTestData(QStringLiteral("insert11"));
0171         skipTestData(QStringLiteral("insert12"));
0172         skipTestData(QStringLiteral("insert13"));
0173         skipTestData(QStringLiteral("insert14"));
0174         skipTestData(QStringLiteral("insert15"));
0175         skipTestData(QStringLiteral("insert16"));
0176         skipTestData(QStringLiteral("insert17"));
0177         skipTestData(QStringLiteral("insert18"));
0178     }
0179 
0180     void testRemoveFromRootData() override
0181     {
0182         QTest::addColumn<SignalList>("signalList");
0183         QTest::addColumn<PersistentChangeList>("changeList");
0184 
0185         static const IndexFinder indexFinder;
0186 
0187         newRemoveTest(QStringLiteral("remove01"), indexFinder, 0, 0, 43);
0188         newRemoveTest(QStringLiteral("remove02"), indexFinder, 0, 7, 43);
0189         newRemoveWithDataChangedTest(QStringLiteral("remove03"), indexFinder, 42, 42, 43, QList<int>({41}));
0190     }
0191 
0192     void testRemoveFromTopLevelData() override
0193     {
0194         QTest::addColumn<SignalList>("signalList");
0195         QTest::addColumn<PersistentChangeList>("changeList");
0196 
0197         static const IndexFinder indexFinder;
0198 
0199         newRemoveWithDataChangedTest(QStringLiteral("remove01"), indexFinder, 9, 9, 43, QList<int>({8}));
0200         newRemoveWithDataChangedTest(QStringLiteral("remove02"), indexFinder, 9, 15, 43, QList<int>({8}));
0201         newRemoveWithDataChangedTest(QStringLiteral("remove03"), indexFinder, 36, 36, 43, QList<int>({35, 8, 35}));
0202     }
0203 
0204     void testRemoveFromSecondLevelData() override
0205     {
0206         QTest::addColumn<SignalList>("signalList");
0207         QTest::addColumn<PersistentChangeList>("changeList");
0208 
0209         static const IndexFinder indexFinder;
0210         newRemoveWithDataChangedTest(QStringLiteral("remove01"), indexFinder, 17, 17, 43, QList<int>({16}));
0211         newRemoveWithDataChangedTest(QStringLiteral("remove02"), indexFinder, 17, 23, 43, QList<int>({16}));
0212         newRemoveWithDataChangedTest(QStringLiteral("remove03"), indexFinder, 31, 31, 43, QList<int>({30, 16, 30}));
0213     }
0214 
0215     void testMoveFromRootData() override
0216     {
0217         dummyTestData();
0218     }
0219 
0220     void testMoveFromTopLevelData() override
0221     {
0222         dummyTestData();
0223     }
0224 
0225     void testMoveFromSecondLevelData() override
0226     {
0227         dummyTestData();
0228     }
0229 
0230     void testModifyInRootData() override
0231     {
0232         dummyTestData();
0233     }
0234 
0235     void testModifyInTopLevelData() override
0236     {
0237         dummyTestData();
0238     }
0239 
0240     void testModifyInSecondLevelData() override
0241     {
0242         dummyTestData();
0243     }
0244 };
0245 
0246 class DescendantsProxyModelTest : public ProxyModelTest
0247 {
0248     Q_OBJECT
0249 public:
0250     DescendantsProxyModelTest(QObject *parent = nullptr)
0251         : ProxyModelTest(parent)
0252     {
0253     }
0254 
0255     void setTestData(TestData *data)
0256     {
0257         qDebug() << data;
0258         connectTestSignals(data);
0259     }
0260 
0261 protected:
0262     QAbstractProxyModel *getProxy() override
0263     {
0264         return new KDescendantsProxyModel(this);
0265     }
0266 
0267 private Q_SLOTS:
0268     void cleanupTestCase()
0269     {
0270         doCleanupTestCase();
0271     }
0272 
0273     void cleanup()
0274     {
0275         doCleanup();
0276     }
0277 
0278 private:
0279     KDescendantsProxyModel *m_proxyModel;
0280 };
0281 
0282 PROXYMODELTEST_MAIN(DescendantsProxyModelTest,
0283                     PROXYMODELTEST_CUSTOM(new TestData, DynamicTree, ImmediatePersistence, "")
0284                         PROXYMODELTEST_CUSTOM(new TestData, DynamicTree, LazyPersistence, "")
0285                             PROXYMODELTEST_CUSTOM(new TestData, IntermediateProxy, ImmediatePersistence, "")
0286                                 PROXYMODELTEST_CUSTOM(new TestData, IntermediateProxy, LazyPersistence, ""))
0287 
0288 #include "kdescendantsproxymodel_smoketest.moc"