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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Ableton AG <info@ableton.com>
0003     SPDX-FileContributor: Stephen Kelly <stephen.kelly@ableton.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QIdentityProxyModel>
0009 #include <QItemSelection>
0010 #include <QSignalSpy>
0011 #include <QStringListModel>
0012 #include <QTest>
0013 
0014 #include "kmodelindexproxymapper.h"
0015 
0016 class ModelIndexProxyMapperTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020 
0021     void init();
0022 
0023     void testIndexMapping();
0024     void testSelectionMapping();
0025     void selfConnection();
0026     void connectedChangedSimple();
0027     void connectedChangedComplex();
0028     void crossWires();
0029     void isConnected();
0030 
0031 private:
0032     QStringListModel baseModel;
0033 
0034     QIdentityProxyModel proxy_common1;
0035 
0036     QIdentityProxyModel proxy_left1;
0037     QIdentityProxyModel proxy_left2;
0038     QIdentityProxyModel proxy_left3;
0039 
0040     QIdentityProxyModel proxy_right1;
0041     QIdentityProxyModel proxy_right2;
0042     QIdentityProxyModel proxy_right3;
0043     QIdentityProxyModel proxy_right4;
0044 };
0045 
0046 void ModelIndexProxyMapperTest::init()
0047 {
0048     baseModel.setStringList({"Monday", "Tuesday", "Wednesday"});
0049 
0050     proxy_common1.setSourceModel(&baseModel);
0051 
0052     proxy_left1.setSourceModel(&proxy_common1);
0053     proxy_left2.setSourceModel(&proxy_left1);
0054     proxy_left3.setSourceModel(&proxy_left2);
0055 
0056     proxy_right1.setSourceModel(&proxy_common1);
0057     proxy_right2.setSourceModel(&proxy_right1);
0058     proxy_right3.setSourceModel(&proxy_right2);
0059     proxy_right4.setSourceModel(&proxy_right3);
0060 }
0061 
0062 void ModelIndexProxyMapperTest::testIndexMapping()
0063 {
0064     KModelIndexProxyMapper mapper(&proxy_left3, &proxy_right4);
0065 
0066     auto leftIdx = proxy_left3.index(0, 0);
0067     auto rightIdx = proxy_right4.index(0, 0);
0068 
0069     QVERIFY(leftIdx.isValid());
0070     QVERIFY(rightIdx.isValid());
0071 
0072     QCOMPARE(mapper.mapLeftToRight(leftIdx), rightIdx);
0073     QCOMPARE(mapper.mapRightToLeft(rightIdx), leftIdx);
0074 }
0075 
0076 void ModelIndexProxyMapperTest::testSelectionMapping()
0077 {
0078     KModelIndexProxyMapper mapper(&proxy_left3, &proxy_right4);
0079 
0080     auto leftIdx = proxy_left3.index(0, 0);
0081     auto rightIdx = proxy_right4.index(0, 0);
0082 
0083     auto leftSel = QItemSelection(leftIdx, leftIdx);
0084     auto rightSel = QItemSelection(rightIdx, rightIdx);
0085 
0086     QCOMPARE(leftSel.indexes().size(), 1);
0087     QCOMPARE(rightSel.indexes().size(), 1);
0088 
0089     QVERIFY(leftSel.indexes().first().isValid());
0090     QVERIFY(rightSel.indexes().first().isValid());
0091 
0092     QCOMPARE(mapper.mapSelectionLeftToRight(leftSel), rightSel);
0093     QCOMPARE(mapper.mapSelectionRightToLeft(rightSel), leftSel);
0094 }
0095 
0096 void ModelIndexProxyMapperTest::selfConnection()
0097 {
0098     KModelIndexProxyMapper mapper(&baseModel, &baseModel);
0099     QVERIFY(mapper.isConnected());
0100 
0101     auto idx = baseModel.index(0, 0);
0102     QVERIFY(idx.isValid());
0103 
0104     QCOMPARE(mapper.mapLeftToRight(idx), idx);
0105 }
0106 
0107 void ModelIndexProxyMapperTest::connectedChangedSimple()
0108 {
0109     QIdentityProxyModel proxy1;
0110     Q_SET_OBJECT_NAME(proxy1);
0111 
0112     KModelIndexProxyMapper mapper(&proxy1, &baseModel);
0113 
0114     QSignalSpy spy(&mapper, SIGNAL(isConnectedChanged()));
0115 
0116     QVERIFY(!mapper.isConnected());
0117     proxy1.setSourceModel(&baseModel);
0118 
0119     QVERIFY(mapper.isConnected());
0120     QCOMPARE(spy.count(), 1);
0121 }
0122 
0123 void ModelIndexProxyMapperTest::connectedChangedComplex()
0124 {
0125     KModelIndexProxyMapper mapper(&proxy_left3, &proxy_right4);
0126 
0127     QSignalSpy spy(&mapper, SIGNAL(isConnectedChanged()));
0128 
0129     QVERIFY(mapper.isConnected());
0130 
0131     proxy_right2.setSourceModel(nullptr);
0132 
0133     QVERIFY(!mapper.isConnected());
0134     QCOMPARE(spy.count(), 1);
0135 
0136     proxy_right2.setSourceModel(&proxy_right1);
0137 
0138     QVERIFY(mapper.isConnected());
0139     QCOMPARE(spy.count(), 2);
0140 
0141     auto leftIdx = proxy_left3.index(0, 0);
0142     QVERIFY(leftIdx.isValid());
0143     auto rightIdx = mapper.mapLeftToRight(leftIdx);
0144     QVERIFY(rightIdx.isValid());
0145     QCOMPARE(mapper.mapRightToLeft(rightIdx), leftIdx);
0146 
0147     QIdentityProxyModel replacement_right1;
0148     replacement_right1.setSourceModel(&proxy_right1);
0149     proxy_right2.setSourceModel(&replacement_right1);
0150 
0151     QVERIFY(mapper.isConnected());
0152     QCOMPARE(spy.count(), 2);
0153 }
0154 
0155 void ModelIndexProxyMapperTest::crossWires()
0156 {
0157     KModelIndexProxyMapper mapper(&proxy_left3, &proxy_right4);
0158 
0159     QSignalSpy spy(&mapper, SIGNAL(isConnectedChanged()));
0160 
0161     QVERIFY(mapper.isConnected());
0162 
0163     proxy_left3.setSourceModel(&proxy_right3);
0164 
0165     QVERIFY(mapper.isConnected());
0166     QCOMPARE(spy.count(), 0);
0167 
0168     {
0169         auto leftIdx = proxy_left3.index(0, 0);
0170         auto rightIdx = proxy_right4.index(0, 0);
0171         QCOMPARE(mapper.mapLeftToRight(leftIdx), rightIdx);
0172     }
0173 
0174     proxy_right4.setSourceModel(&proxy_left2);
0175 
0176     QVERIFY(mapper.isConnected());
0177     QCOMPARE(spy.count(), 0);
0178 
0179     {
0180         auto leftIdx = proxy_left3.index(0, 0);
0181         auto rightIdx = proxy_right4.index(0, 0);
0182         QCOMPARE(mapper.mapLeftToRight(leftIdx), rightIdx);
0183     }
0184 }
0185 
0186 void ModelIndexProxyMapperTest::isConnected()
0187 {
0188     KModelIndexProxyMapper mapper1(&proxy_left1, &baseModel);
0189     QVERIFY(mapper1.isConnected());
0190     KModelIndexProxyMapper mapper2(&baseModel, &proxy_left1);
0191     QVERIFY(mapper2.isConnected());
0192 }
0193 
0194 QTEST_MAIN(ModelIndexProxyMapperTest)
0195 #include "kmodelindexproxymappertest.moc"