File indexing completed on 2024-12-01 03:41:35

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef SELECTIONPROXYMODELTEST_H
0008 #define SELECTIONPROXYMODELTEST_H
0009 
0010 #include "proxymodeltest.h"
0011 
0012 #include "kselectionproxymodel.h"
0013 #include "modelselector.h"
0014 
0015 class SelectionProxyModelTest : public ProxyModelTest
0016 {
0017     Q_OBJECT
0018 public:
0019     SelectionProxyModelTest(QObject *parent = nullptr)
0020         : ProxyModelTest(parent)
0021         , m_selectionModel(nullptr)
0022         , m_modelSelector(nullptr)
0023     {
0024     }
0025 
0026     void setTestData(ModelSelector *modelSelector)
0027     {
0028         disconnectTestSignals(modelSelector);
0029         m_modelSelector = modelSelector;
0030         connectTestSignals(m_modelSelector);
0031     }
0032 
0033 protected:
0034     QAbstractProxyModel *getProxy() override
0035     {
0036         Q_ASSERT(sourceModel());
0037         m_selectionModel = new QItemSelectionModel(sourceModel());
0038 
0039         m_modelSelector->setWatchedModel(sourceModel());
0040         m_modelSelector->setSelectionModel(m_selectionModel);
0041         m_modelSelector->setWatch(true);
0042 
0043         m_proxyModel = new KSelectionProxyModel(m_selectionModel, this);
0044         QVariant filterBehaviour = m_modelSelector->property("filterBehaviour");
0045         if (!filterBehaviour.isNull()) {
0046             m_proxyModel->setFilterBehavior(static_cast<KSelectionProxyModel::FilterBehavior>(filterBehaviour.toInt()));
0047         }
0048         return m_proxyModel;
0049     }
0050 
0051 private Q_SLOTS:
0052     void cleanupTestCase()
0053     {
0054         doCleanupTestCase();
0055         delete m_modelSelector;
0056         m_modelSelector = nullptr;
0057     }
0058 
0059     void cleanup()
0060     {
0061         doCleanup();
0062         if (m_modelSelector->selectionModel()) {
0063             m_modelSelector->selectionModel()->clearSelection();
0064         }
0065         m_modelSelector->setWatch(false);
0066     }
0067 
0068 private:
0069     QItemSelectionModel *m_selectionModel;
0070     KSelectionProxyModel *m_proxyModel;
0071     ModelSelector *m_modelSelector;
0072 };
0073 
0074 template<typename SelectorStrategy, KSelectionProxyModel::FilterBehavior _filterBehaviour>
0075 class TestData : public SelectorStrategy
0076 {
0077 public:
0078     TestData(ProxyModelTest *proxyModelTest)
0079         : SelectorStrategy(proxyModelTest)
0080     {
0081         SelectorStrategy::setProperty("filterBehaviour", QVariant(int(_filterBehaviour)));
0082     }
0083 
0084     void testInsertWhenEmptyData() override
0085     {
0086         SelectorStrategy::testInsertWhenEmptyData();
0087     }
0088     void testInsertInRootData() override
0089     {
0090         SelectorStrategy::testInsertInRootData();
0091     }
0092     void testInsertInTopLevelData() override
0093     {
0094         SelectorStrategy::testInsertInTopLevelData();
0095     }
0096     void testInsertInSecondLevelData() override
0097     {
0098         SelectorStrategy::testInsertInSecondLevelData();
0099     }
0100 
0101     void testRemoveFromRootData() override
0102     {
0103         SelectorStrategy::testRemoveFromRootData();
0104     }
0105     void testRemoveFromTopLevelData() override
0106     {
0107         SelectorStrategy::testRemoveFromTopLevelData();
0108     }
0109     void testRemoveFromSecondLevelData() override
0110     {
0111         SelectorStrategy::testRemoveFromSecondLevelData();
0112     }
0113 
0114     void testMoveFromRootData() override
0115     {
0116         SelectorStrategy::testMoveFromRootData();
0117     }
0118     void testMoveFromTopLevelData() override
0119     {
0120         SelectorStrategy::testMoveFromTopLevelData();
0121     }
0122     void testMoveFromSecondLevelData() override
0123     {
0124         SelectorStrategy::testMoveFromSecondLevelData();
0125     }
0126 
0127     void testModifyInRootData() override
0128     {
0129         SelectorStrategy::testModifyInRootData();
0130     }
0131     void testModifyInTopLevelData() override
0132     {
0133         SelectorStrategy::testModifyInTopLevelData();
0134     }
0135     void testModifyInSecondLevelData() override
0136     {
0137         SelectorStrategy::testModifyInSecondLevelData();
0138     }
0139 };
0140 
0141 /**
0142   @brief Selects only one item in the source model which does not change
0143 */
0144 template<int num1 = 0, int num2 = 0, int num3 = 0, int num4 = 0, int num5 = 0>
0145 class ImmediateSelectionStrategy : public ModelSelector
0146 {
0147 public:
0148     ImmediateSelectionStrategy(ProxyModelTest *parent = nullptr)
0149         : ModelSelector(parent)
0150     {
0151         if (num1 > 0) {
0152             m_selectedRows << num1;
0153         }
0154         if (num2 > 0) {
0155             m_selectedRows << num2;
0156         }
0157         if (num3 > 0) {
0158             m_selectedRows << num3;
0159         }
0160         if (num4 > 0) {
0161             m_selectedRows << num4;
0162         }
0163         if (num5 > 0) {
0164             m_selectedRows << num5;
0165         }
0166     }
0167 };
0168 
0169 /**
0170   For testing the proxy when it has no selection.
0171 */
0172 typedef ImmediateSelectionStrategy<0, 0, 0, 0, 0> NoSelectionStrategy;
0173 
0174 /* clang-format off */
0175 
0176 #define SELECTIONPROXYTESTDATA(SelectionStrategy, num1, num2, num3, num4, num5, Type) \
0177     new TestData<SelectionStrategy<num1, num2, num3, num4, num5>, Type>(proxyModelTestClass)
0178 
0179 #define SELECTIONPROXYTESTCONFIG(SelectionStrategy, num1, num2, num3, num4, num5, Type) \
0180     << "  FilterBehavior:    " << #Type << "\n" \
0181     << "  SelectionStrategy: " << #SelectionStrategy << (num1 > 0 ? #num1 : "") << (num2 > 0 ? #num2 : "") << (num3 > 0 ? #num3 : "") \
0182     << (num4 > 0 ? #num4 : "") << (num5 > 0 ? #num5 : "") << "\n";
0183 
0184 #define SELECTIONPROXYMODELTEST(SelectionStrategy, num1, num2, num3, num4, num5, Type, IntermediateProxy, LazyPersistence) \
0185     PROXYMODELTEST_CUSTOM(SELECTIONPROXYTESTDATA(SelectionStrategy, num1, num2, num3, num4, num5, Type), \
0186                           IntermediateProxy, \
0187                           LazyPersistence, \
0188                           SELECTIONPROXYTESTCONFIG(SelectionStrategy, num1, num2, num3, num4, num5, Type))
0189 
0190 #define SELECTIONCOMPLETETEST(SelectionStrategy, num1, num2, num3, num4, num5, Type) \
0191     SELECTIONPROXYMODELTEST(SelectionStrategy, num1, num2, num3, num4, num5, Type, DynamicTree, ImmediatePersistence) \
0192     SELECTIONPROXYMODELTEST(SelectionStrategy, num1, num2, num3, num4, num5, Type, IntermediateProxy, ImmediatePersistence) \
0193     SELECTIONPROXYMODELTEST(SelectionStrategy, num1, num2, num3, num4, num5, Type, DynamicTree, LazyPersistence) \
0194     SELECTIONPROXYMODELTEST(SelectionStrategy, num1, num2, num3, num4, num5, Type, IntermediateProxy, LazyPersistence)
0195 
0196 #define SIMPLETESTDATA(SelectionStrategy, Type) new TestData<SelectionStrategy, Type>(proxyModelTestClass)
0197 
0198 #define SELECTIONPROXYMODELSIMPLETEST(SelectionStrategy, Type) \
0199     PROXYMODELTEST_CUSTOM(SIMPLETESTDATA(SelectionStrategy, Type), \
0200                           DynamicTree, \
0201                           LazyPersistence, \
0202                           SELECTIONPROXYTESTCONFIG(SelectionStrategy, 0, 0, 0, 0, 0, Type))
0203 
0204 #define SELECTIONCOMPLETETEST1(SelectionStrategy, num1, Type) SELECTIONCOMPLETETEST(SelectionStrategy, num1, 0, 0, 0, 0, Type)
0205 
0206 #define SELECTIONCOMPLETETEST2(SelectionStrategy, num1, num2, Type) SELECTIONCOMPLETETEST(SelectionStrategy, num1, num2, 0, 0, 0, Type)
0207 
0208 #define SELECTIONCOMPLETETEST3(SelectionStrategy, num1, num2, num3, Type) SELECTIONCOMPLETETEST(SelectionStrategy, num1, num2, num3, 0, 0, Type)
0209 
0210 #define SELECTIONCOMPLETETEST4(SelectionStrategy, num1, num2, num3, num4, Type) SELECTIONCOMPLETETEST(SelectionStrategy, num1, num2, num3, num4, 0, Type)
0211 
0212 #endif
0213 /* clang-format on */