Warning, file /frameworks/kitemmodels/autotests/klinkitemselectionmodeltest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2013 Aurélien Gateau <agateau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "klinkitemselectionmodeltest.h"
0008 
0009 #include <klinkitemselectionmodel.h>
0010 
0011 #include <QIdentityProxyModel>
0012 #include <QItemSelectionModel>
0013 #include <QSortFilterProxyModel>
0014 #include <QStandardItem>
0015 #include <QStandardItemModel>
0016 
0017 #include <QTest>
0018 
0019 void KLinkItemSelectionModelTest::init()
0020 {
0021     // Init m_mainModel
0022     m_mainModel = new QStandardItemModel;
0023     for (int x = 0; x < 10; ++x) {
0024         m_mainModel->appendRow(new QStandardItem(QString::number(x)));
0025     }
0026     m_mainSelectionModel = new QItemSelectionModel(m_mainModel);
0027 
0028     // Init subModel
0029     m_subModel = new QSortFilterProxyModel;
0030     m_subModel->setFilterRegularExpression(QRegularExpression(QStringLiteral("^[5-9]")));
0031     m_subModel->setSourceModel(m_mainModel);
0032     m_subSelectionModel = new KLinkItemSelectionModel(m_subModel, m_mainSelectionModel);
0033 }
0034 
0035 void KLinkItemSelectionModelTest::cleanup()
0036 {
0037     delete m_mainSelectionModel;
0038     m_mainSelectionModel = nullptr;
0039     delete m_mainModel;
0040     m_mainModel = nullptr;
0041     delete m_subSelectionModel;
0042     m_subSelectionModel = nullptr;
0043     delete m_subModel;
0044     m_subModel = nullptr;
0045 }
0046 
0047 void KLinkItemSelectionModelTest::testToggle()
0048 {
0049     // Select last index in subModel
0050     QModelIndex subIndex = m_subModel->index(m_subModel->rowCount() - 1, 0);
0051     m_subSelectionModel->select(subIndex, QItemSelectionModel::Toggle);
0052 
0053     // Check selections
0054     QModelIndexList subList = m_subSelectionModel->selectedIndexes();
0055     QCOMPARE(subList.count(), 1);
0056     QCOMPARE(subList.first(), subIndex);
0057 
0058     QModelIndexList mainList = m_mainSelectionModel->selectedIndexes();
0059     QModelIndex mainIndex = m_mainModel->index(m_mainModel->rowCount() - 1, 0);
0060     QCOMPARE(mainList.count(), 1);
0061     QCOMPARE(mainList.first(), mainIndex);
0062 }
0063 
0064 void KLinkItemSelectionModelTest::testMainSetCurrent()
0065 {
0066     // Set last index of mainModel as current
0067     QModelIndex mainIndex = m_mainModel->index(m_mainModel->rowCount() - 1, 0);
0068     m_mainSelectionModel->setCurrentIndex(mainIndex, QItemSelectionModel::Current);
0069 
0070     // Last index of subModel should be current as well
0071     QModelIndex subIndex = m_subSelectionModel->currentIndex();
0072     QVERIFY(subIndex.isValid());
0073     QCOMPARE(subIndex, m_subModel->index(m_subModel->rowCount() - 1, 0));
0074 
0075     // Set first index of mainModel as current. First index of mainModel does
0076     // not exist in subModel, so its current index should remain the same.
0077     subIndex = m_subSelectionModel->currentIndex();
0078     QVERIFY(subIndex.isValid());
0079     mainIndex = m_mainModel->index(0, 0);
0080     m_mainSelectionModel->setCurrentIndex(mainIndex, QItemSelectionModel::Current);
0081     QCOMPARE(subIndex, m_subSelectionModel->currentIndex());
0082 }
0083 
0084 void KLinkItemSelectionModelTest::testSubSetCurrent()
0085 {
0086     // Set last index of subModel as current
0087     QModelIndex subIndex = m_subModel->index(m_subModel->rowCount() - 1, 0);
0088     m_subSelectionModel->setCurrentIndex(subIndex, QItemSelectionModel::Current);
0089 
0090     // Last index of mainModel should be current as well
0091     QModelIndex mainIndex = m_mainSelectionModel->currentIndex();
0092     QVERIFY(mainIndex.isValid());
0093     QCOMPARE(mainIndex, m_mainModel->index(m_mainModel->rowCount() - 1, 0));
0094 }
0095 
0096 void KLinkItemSelectionModelTest::testChangeModel()
0097 {
0098     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0099 
0100     {
0101         auto idx = m_mainModel->index(6, 0);
0102         m_mainSelectionModel->select(idx, QItemSelectionModel::Select);
0103     }
0104 
0105     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0106     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0107     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 6);
0108     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 1);
0109 
0110     QIdentityProxyModel newIntermediateProxy;
0111 
0112     newIntermediateProxy.setSourceModel(m_mainModel);
0113 
0114     // Change the model of the KLinkItemSelectionModel
0115     m_subSelectionModel->setModel(&newIntermediateProxy);
0116 
0117     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0118     QVERIFY(m_subSelectionModel->selection().isEmpty());
0119 }
0120 
0121 void KLinkItemSelectionModelTest::testChangeModelOfExternal()
0122 {
0123     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0124 
0125     {
0126         auto idx = m_mainModel->index(6, 0);
0127         m_mainSelectionModel->select(idx, QItemSelectionModel::Select);
0128     }
0129 
0130     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0131     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0132     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 6);
0133     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 1);
0134 
0135     QIdentityProxyModel newIntermediateProxy;
0136 
0137     newIntermediateProxy.setSourceModel(m_mainModel);
0138 
0139     // Change the model of the external QISM
0140     m_mainSelectionModel->setModel(&newIntermediateProxy);
0141 
0142     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0143     QVERIFY(m_subSelectionModel->selection().isEmpty());
0144 }
0145 
0146 void KLinkItemSelectionModelTest::testChangeLinkedSelectionModel()
0147 {
0148     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0149 
0150     {
0151         auto idx = m_mainModel->index(6, 0);
0152         m_mainSelectionModel->select(idx, QItemSelectionModel::Select);
0153     }
0154 
0155     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0156     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0157     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 6);
0158     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 1);
0159 
0160     QItemSelectionModel replacementSelectionModel(m_mainModel, nullptr);
0161     {
0162         auto idx = m_mainModel->index(7, 0);
0163         replacementSelectionModel.select(idx, QItemSelectionModel::Select);
0164     }
0165 
0166     m_subSelectionModel->setLinkedItemSelectionModel(&replacementSelectionModel);
0167 
0168     QVERIFY(!replacementSelectionModel.selection().isEmpty());
0169     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0170     QCOMPARE(replacementSelectionModel.selection().indexes().first().row(), 7);
0171     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 2);
0172 }
0173 
0174 void KLinkItemSelectionModelTest::testAdditionalLink()
0175 {
0176     {
0177         auto idx = m_mainModel->index(6, 0);
0178         m_mainSelectionModel->select(idx, QItemSelectionModel::Select);
0179     }
0180 
0181     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0182     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0183     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 6);
0184     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 1);
0185 
0186     {
0187         QSortFilterProxyModel additionalProxy;
0188         additionalProxy.setFilterRegularExpression(QRegularExpression(QStringLiteral("^[3-9]")));
0189         additionalProxy.setSourceModel(m_mainModel);
0190 
0191         KLinkItemSelectionModel additionalLink;
0192         additionalLink.setModel(&additionalProxy);
0193         additionalLink.setLinkedItemSelectionModel(m_mainSelectionModel);
0194         QVERIFY(!additionalLink.selection().isEmpty());
0195         QCOMPARE(additionalLink.selection().indexes().first().row(), 3);
0196 
0197         auto idx = additionalProxy.index(4, 0);
0198         QVERIFY(idx.isValid());
0199         additionalLink.select(idx, QItemSelectionModel::ClearAndSelect);
0200 
0201         QVERIFY(!additionalLink.selection().isEmpty());
0202         QCOMPARE(additionalLink.selection().indexes().first().row(), 4);
0203 
0204         QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0205         QVERIFY(!m_subSelectionModel->selection().isEmpty());
0206         QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 7);
0207         QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 2);
0208     }
0209 
0210     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0211     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0212     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 7);
0213     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 2);
0214 }
0215 
0216 void KLinkItemSelectionModelTest::testClearSelection()
0217 {
0218     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0219 
0220     {
0221         auto idx = m_mainModel->index(6, 0);
0222         m_mainSelectionModel->select(idx, QItemSelectionModel::Select);
0223     }
0224 
0225     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0226     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0227     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 6);
0228     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 1);
0229 
0230     m_subSelectionModel->clear();
0231 
0232     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0233     QVERIFY(m_subSelectionModel->selection().isEmpty());
0234 
0235     {
0236         auto idx = m_mainModel->index(6, 0);
0237         m_mainSelectionModel->select(idx, QItemSelectionModel::Select);
0238     }
0239 
0240     QVERIFY(!m_mainSelectionModel->selection().isEmpty());
0241     QVERIFY(!m_subSelectionModel->selection().isEmpty());
0242     QCOMPARE(m_mainSelectionModel->selection().indexes().first().row(), 6);
0243     QCOMPARE(m_subSelectionModel->selection().indexes().first().row(), 1);
0244 
0245     m_mainSelectionModel->clear();
0246 
0247     QVERIFY(m_mainSelectionModel->selection().isEmpty());
0248     QVERIFY(m_subSelectionModel->selection().isEmpty());
0249 }
0250 
0251 QTEST_MAIN(KLinkItemSelectionModelTest)