File indexing completed on 2024-12-22 04:12:52

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_model_index_converter_test.h"
0008 
0009 #include <simpletest.h>
0010 
0011 #include "kis_node_model.h"
0012 #include "kis_dummies_facade.h"
0013 #include "kis_node_dummies_graph.h"
0014 #include "kis_model_index_converter.h"
0015 #include "kis_model_index_converter_show_all.h"
0016 
0017 
0018 void KisModelIndexConverterTest::init()
0019 {
0020     m_dummiesFacade = new KisDummiesFacade(0);
0021     m_nodeModel = new KisNodeModel(0);
0022     initBase();
0023     constructImage();
0024     addSelectionMasks();
0025 
0026     m_dummiesFacade->setImage(m_image);
0027     m_nodeModel->setDummiesFacade(m_dummiesFacade, m_image, 0, 0, 0);
0028 }
0029 
0030 void KisModelIndexConverterTest::cleanup()
0031 {
0032     m_nodeModel->setDummiesFacade(0, 0, 0, 0, 0);
0033     m_dummiesFacade->setImage(0);
0034 
0035     cleanupBase();
0036     delete m_indexConverter;
0037     delete m_nodeModel;
0038     delete m_dummiesFacade;
0039 }
0040 
0041 inline void KisModelIndexConverterTest::checkIndexFromDummy(KisNodeSP node, int row) {
0042     QModelIndex index;
0043     KisNodeDummy *dummy;
0044 
0045     dummy = m_dummiesFacade->dummyForNode(node);
0046     index = m_indexConverter->indexFromDummy(dummy);
0047 
0048     QVERIFY(index.isValid());
0049     QCOMPARE(index.column(), 0);
0050     QCOMPARE(index.row(), row);
0051     QCOMPARE(m_indexConverter->dummyFromIndex(index), dummy);
0052 }
0053 
0054 inline void KisModelIndexConverterTest::checkInvalidIndexFromDummy(KisNodeSP node) {
0055     QModelIndex index;
0056     KisNodeDummy *dummy;
0057 
0058     dummy = m_dummiesFacade->dummyForNode(node);
0059     index = m_indexConverter->indexFromDummy(dummy);
0060 
0061     QVERIFY(!index.isValid());
0062 }
0063 
0064 inline void KisModelIndexConverterTest::checkIndexFromAddedAllowedDummy(KisNodeSP parent, int index, int parentRow, int childRow, bool parentValid)
0065 {
0066     QString type = KisLayer::staticMetaObject.className();
0067     checkIndexFromAddedDummy(parent, index, type, parentRow, childRow, parentValid);
0068 }
0069 
0070 inline void KisModelIndexConverterTest::checkIndexFromAddedDeniedDummy(KisNodeSP parent, int index, int parentRow, int childRow, bool parentValid)
0071 {
0072     QString type = KisSelectionMask::staticMetaObject.className();
0073     checkIndexFromAddedDummy(parent, index, type, parentRow, childRow, parentValid);
0074 }
0075 
0076 inline void KisModelIndexConverterTest::checkIndexFromAddedDummy(KisNodeSP parent, int index, const QString &type, int parentRow, int childRow, bool parentValid)
0077 {
0078    QModelIndex modelIndex;
0079    KisNodeDummy *dummy;
0080 
0081    int row = 0;
0082    bool result;
0083 
0084    dummy = parent ? m_dummiesFacade->dummyForNode(parent) : 0;
0085    result = m_indexConverter->indexFromAddedDummy(dummy, index, type, modelIndex, row);
0086    if(!result) dbgKrita << "Failing parent:" << (parent ? parent->name() : "none") << "index:" << index;
0087    QVERIFY(result);
0088 
0089    QCOMPARE(modelIndex.isValid(), parentValid);
0090 
0091    if(modelIndex.isValid()) {
0092        QCOMPARE(modelIndex.row(), parentRow);
0093        QCOMPARE(modelIndex.column(), 0);
0094    }
0095 
0096    if(row != childRow) dbgKrita << "Failing parent:" << (parent ? parent->name() : "none") << "index:" << index;
0097    QCOMPARE(row, childRow);
0098 }
0099 
0100 inline void KisModelIndexConverterTest::checkInvalidIndexFromAddedAllowedDummy(KisNodeSP parent, int index)
0101 {
0102     QString type = KisLayer::staticMetaObject.className();
0103     checkInvalidIndexFromAddedDummy(parent, index, type);
0104 }
0105 
0106 inline void KisModelIndexConverterTest::checkInvalidIndexFromAddedDeniedDummy(KisNodeSP parent, int index)
0107 {
0108     QString type = KisSelectionMask::staticMetaObject.className();
0109     checkInvalidIndexFromAddedDummy(parent, index, type);
0110 }
0111 
0112 inline void KisModelIndexConverterTest::checkInvalidIndexFromAddedDummy(KisNodeSP parent, int index, const QString &type)
0113 {
0114     QModelIndex modelIndex;
0115     KisNodeDummy *dummy;
0116 
0117     int row = 0;
0118     bool result;
0119 
0120     dummy = parent ? m_dummiesFacade->dummyForNode(parent) : 0;
0121     result = m_indexConverter->indexFromAddedDummy(dummy, index, type, modelIndex, row);
0122     QVERIFY(!result);
0123 }
0124 
0125 inline void KisModelIndexConverterTest::checkDummyFromRow(KisNodeSP parent, int row, KisNodeSP expectedNode)
0126 {
0127     QModelIndex parentIndex;
0128     KisNodeDummy *parentDummy;
0129 
0130     if(parent) {
0131         parentDummy = m_dummiesFacade->dummyForNode(parent);
0132         parentIndex = m_indexConverter->indexFromDummy(parentDummy);
0133     }
0134 
0135     KisNodeDummy *resultDummy = m_indexConverter->dummyFromRow(row, parentIndex);
0136     KisNodeSP resultNode = resultDummy ? resultDummy->node() : 0;
0137 
0138     if(resultNode != expectedNode) {
0139         dbgKrita << "Actual node:  " << (resultNode ? resultNode->name() : "none");
0140         dbgKrita << "Expected node:" << (expectedNode ? expectedNode->name() : "none");
0141         QFAIL("Wrong node");
0142     }
0143 }
0144 
0145 inline void KisModelIndexConverterTest::checkRowCount(KisNodeSP parent, int rowCount)
0146 {
0147     QModelIndex parentIndex;
0148     KisNodeDummy *parentDummy;
0149 
0150     if(parent) {
0151         parentDummy = m_dummiesFacade->dummyForNode(parent);
0152         parentIndex = m_indexConverter->indexFromDummy(parentDummy);
0153     }
0154 
0155     int resultRowCount = m_indexConverter->rowCount(parentIndex);
0156 
0157     if(resultRowCount != rowCount) {
0158         dbgKrita << "Wrong row count for:" << (parent ? parent->name() : "none");
0159         dbgKrita << "Actual:  " << resultRowCount;
0160         dbgKrita << "Expected:" << rowCount;
0161         QFAIL("Wrong row count");
0162     }
0163 }
0164 
0165 void KisModelIndexConverterTest::testIndexFromDummy()
0166 {
0167     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, false);
0168 
0169     checkIndexFromDummy(m_layer1, 3);
0170     checkIndexFromDummy(m_layer2, 2);
0171     checkIndexFromDummy(m_layer3, 1);
0172     checkIndexFromDummy(m_layer4, 0);
0173 
0174     checkIndexFromDummy(m_mask1, 1);
0175     checkIndexFromDummy(m_sel3, 0);
0176 
0177     checkInvalidIndexFromDummy(m_image->root());
0178     checkInvalidIndexFromDummy(m_sel1);
0179     checkInvalidIndexFromDummy(m_sel2);
0180 }
0181 
0182 void KisModelIndexConverterTest::testIndexFromAddedAllowedDummy()
0183 {
0184     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, false);
0185 
0186     checkIndexFromAddedAllowedDummy(m_image->root(), 0, 0, 4, false);
0187     checkIndexFromAddedAllowedDummy(m_image->root(), 1, 0, 4, false);
0188     checkIndexFromAddedAllowedDummy(m_image->root(), 2, 0, 3, false);
0189     checkIndexFromAddedAllowedDummy(m_image->root(), 3, 0, 2, false);
0190     checkIndexFromAddedAllowedDummy(m_image->root(), 4, 0, 2, false);
0191     checkIndexFromAddedAllowedDummy(m_image->root(), 5, 0, 1, false);
0192     checkIndexFromAddedAllowedDummy(m_image->root(), 6, 0, 0, false);
0193 
0194     checkIndexFromAddedAllowedDummy(m_layer1, 0, 3, 0, true);
0195 
0196     checkIndexFromAddedAllowedDummy(m_layer3, 0, 1, 2, true);
0197     checkIndexFromAddedAllowedDummy(m_layer3, 1, 1, 1, true);
0198     checkIndexFromAddedAllowedDummy(m_layer3, 2, 1, 0, true);
0199 
0200     checkInvalidIndexFromAddedAllowedDummy(0, 0);
0201 }
0202 
0203 void KisModelIndexConverterTest::testIndexFromAddedDeniedDummy()
0204 {
0205     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, false);
0206 
0207     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 0);
0208     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 1);
0209     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 2);
0210     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 3);
0211     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 4);
0212     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 5);
0213     checkInvalidIndexFromAddedDeniedDummy(m_image->root(), 6);
0214 
0215     checkIndexFromAddedDeniedDummy(m_layer1, 0, 3, 0, true);
0216 
0217     checkIndexFromAddedDeniedDummy(m_layer3, 0, 1, 2, true);
0218     checkIndexFromAddedDeniedDummy(m_layer3, 1, 1, 1, true);
0219     checkIndexFromAddedDeniedDummy(m_layer3, 2, 1, 0, true);
0220 
0221     checkInvalidIndexFromAddedDeniedDummy(0, 0);
0222 }
0223 
0224 void KisModelIndexConverterTest::testDummyFromRow()
0225 {
0226     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, false);
0227 
0228     checkDummyFromRow(m_image->root(), 0, m_layer4);
0229     checkDummyFromRow(m_image->root(), 1, m_layer3);
0230     checkDummyFromRow(m_image->root(), 2, m_layer2);
0231     checkDummyFromRow(m_image->root(), 3, m_layer1);
0232 
0233     checkDummyFromRow(0, 0, m_layer4);
0234     checkDummyFromRow(0, 1, m_layer3);
0235     checkDummyFromRow(0, 2, m_layer2);
0236     checkDummyFromRow(0, 3, m_layer1);
0237 
0238     checkDummyFromRow(m_layer3, 0, m_sel3);
0239     checkDummyFromRow(m_layer3, 1, m_mask1);
0240 }
0241 
0242 void KisModelIndexConverterTest::testRowCount()
0243 {
0244     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, false);
0245 
0246     checkRowCount(m_image->root(), 4);
0247     checkRowCount(0, 4);
0248 
0249     checkRowCount(m_layer1, 0);
0250     checkRowCount(m_layer2, 0);
0251     checkRowCount(m_layer3, 2);
0252     checkRowCount(m_layer4, 0);
0253 }
0254 
0255 void KisModelIndexConverterTest::testIndexFromDummyShowGlobalSelection()
0256 {
0257     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, true);
0258 
0259     checkIndexFromDummy(m_sel1,   5);
0260     checkIndexFromDummy(m_layer1, 4);
0261     checkIndexFromDummy(m_layer2, 3);
0262     checkIndexFromDummy(m_sel2,   2);
0263     checkIndexFromDummy(m_layer3, 1);
0264     checkIndexFromDummy(m_layer4, 0);
0265 
0266     checkIndexFromDummy(m_mask1, 1);
0267     checkIndexFromDummy(m_sel3, 0);
0268 
0269     checkInvalidIndexFromDummy(m_image->root());
0270 }
0271 
0272 void KisModelIndexConverterTest::testIndexFromAddedAllowedDummyShowGlobalSelection()
0273 {
0274     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, true);
0275 
0276     checkIndexFromAddedAllowedDummy(m_image->root(), 0, 0, 6, false);
0277     checkIndexFromAddedAllowedDummy(m_image->root(), 1, 0, 5, false);
0278     checkIndexFromAddedAllowedDummy(m_image->root(), 2, 0, 4, false);
0279     checkIndexFromAddedAllowedDummy(m_image->root(), 3, 0, 3, false);
0280     checkIndexFromAddedAllowedDummy(m_image->root(), 4, 0, 2, false);
0281     checkIndexFromAddedAllowedDummy(m_image->root(), 5, 0, 1, false);
0282     checkIndexFromAddedAllowedDummy(m_image->root(), 6, 0, 0, false);
0283 
0284     checkIndexFromAddedAllowedDummy(m_layer1, 0, 4, 0, true);
0285 
0286     checkIndexFromAddedAllowedDummy(m_layer3, 0, 1, 2, true);
0287     checkIndexFromAddedAllowedDummy(m_layer3, 1, 1, 1, true);
0288     checkIndexFromAddedAllowedDummy(m_layer3, 2, 1, 0, true);
0289 
0290     checkInvalidIndexFromAddedAllowedDummy(0, 0);
0291 }
0292 
0293 void KisModelIndexConverterTest::testIndexFromAddedDeniedDummyShowGlobalSelection()
0294 {
0295     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, true);
0296 
0297     checkIndexFromAddedDeniedDummy(m_image->root(), 0, 0, 6, false);
0298     checkIndexFromAddedDeniedDummy(m_image->root(), 1, 0, 5, false);
0299     checkIndexFromAddedDeniedDummy(m_image->root(), 2, 0, 4, false);
0300     checkIndexFromAddedDeniedDummy(m_image->root(), 3, 0, 3, false);
0301     checkIndexFromAddedDeniedDummy(m_image->root(), 4, 0, 2, false);
0302     checkIndexFromAddedDeniedDummy(m_image->root(), 5, 0, 1, false);
0303     checkIndexFromAddedDeniedDummy(m_image->root(), 6, 0, 0, false);
0304 
0305     checkIndexFromAddedDeniedDummy(m_layer1, 0, 4, 0, true);
0306 
0307     checkIndexFromAddedDeniedDummy(m_layer3, 0, 1, 2, true);
0308     checkIndexFromAddedDeniedDummy(m_layer3, 1, 1, 1, true);
0309     checkIndexFromAddedDeniedDummy(m_layer3, 2, 1, 0, true);
0310 
0311     checkInvalidIndexFromAddedDeniedDummy(0, 0);
0312 }
0313 
0314 void KisModelIndexConverterTest::testDummyFromRowShowGlobalSelection()
0315 {
0316     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, true);
0317 
0318     checkDummyFromRow(m_image->root(), 0, m_layer4);
0319     checkDummyFromRow(m_image->root(), 1, m_layer3);
0320     checkDummyFromRow(m_image->root(), 2, m_sel2);
0321     checkDummyFromRow(m_image->root(), 3, m_layer2);
0322     checkDummyFromRow(m_image->root(), 4, m_layer1);
0323     checkDummyFromRow(m_image->root(), 5, m_sel1);
0324 
0325     checkDummyFromRow(0, 0, m_layer4);
0326     checkDummyFromRow(0, 1, m_layer3);
0327     checkDummyFromRow(0, 2, m_sel2);
0328     checkDummyFromRow(0, 3, m_layer2);
0329     checkDummyFromRow(0, 4, m_layer1);
0330     checkDummyFromRow(0, 5, m_sel1);
0331 
0332     checkDummyFromRow(m_layer3, 0, m_sel3);
0333     checkDummyFromRow(m_layer3, 1, m_mask1);
0334 }
0335 
0336 void KisModelIndexConverterTest::testRowCountShowGlobalSelection()
0337 {
0338     m_indexConverter = new KisModelIndexConverter(m_dummiesFacade, m_nodeModel, true);
0339 
0340     checkRowCount(m_image->root(), 6);
0341     checkRowCount(0, 6);
0342 
0343     checkRowCount(m_layer1, 0);
0344     checkRowCount(m_layer2, 0);
0345     checkRowCount(m_layer3, 2);
0346     checkRowCount(m_layer4, 0);
0347 }
0348 
0349 void KisModelIndexConverterTest::testIndexFromDummyShowAll()
0350 {
0351     m_indexConverter = new KisModelIndexConverterShowAll(m_dummiesFacade, m_nodeModel);
0352 
0353     checkIndexFromDummy(m_sel1,   5);
0354     checkIndexFromDummy(m_layer1, 4);
0355     checkIndexFromDummy(m_layer2, 3);
0356     checkIndexFromDummy(m_sel2,   2);
0357     checkIndexFromDummy(m_layer3, 1);
0358     checkIndexFromDummy(m_layer4, 0);
0359 
0360     checkIndexFromDummy(m_mask1, 1);
0361     checkIndexFromDummy(m_sel3, 0);
0362 
0363     checkIndexFromDummy(m_image->root(), 0);
0364 }
0365 
0366 void KisModelIndexConverterTest::testIndexFromAddedAllowedDummyShowAll()
0367 {
0368     m_indexConverter = new KisModelIndexConverterShowAll(m_dummiesFacade, m_nodeModel);
0369 
0370     checkIndexFromAddedAllowedDummy(m_image->root(), 0, 0, 6, true);
0371     checkIndexFromAddedAllowedDummy(m_image->root(), 1, 0, 5, true);
0372     checkIndexFromAddedAllowedDummy(m_image->root(), 2, 0, 4, true);
0373     checkIndexFromAddedAllowedDummy(m_image->root(), 3, 0, 3, true);
0374     checkIndexFromAddedAllowedDummy(m_image->root(), 4, 0, 2, true);
0375     checkIndexFromAddedAllowedDummy(m_image->root(), 5, 0, 1, true);
0376     checkIndexFromAddedAllowedDummy(m_image->root(), 6, 0, 0, true);
0377 
0378     checkIndexFromAddedAllowedDummy(m_layer1, 0, 4, 0, true);
0379 
0380     checkIndexFromAddedAllowedDummy(m_layer3, 0, 1, 2, true);
0381     checkIndexFromAddedAllowedDummy(m_layer3, 1, 1, 1, true);
0382     checkIndexFromAddedAllowedDummy(m_layer3, 2, 1, 0, true);
0383 
0384     checkIndexFromAddedAllowedDummy(0, 0, 0, 0, false);
0385 }
0386 
0387 void KisModelIndexConverterTest::testIndexFromAddedDeniedDummyShowAll()
0388 {
0389     m_indexConverter = new KisModelIndexConverterShowAll(m_dummiesFacade, m_nodeModel);
0390 
0391     checkIndexFromAddedDeniedDummy(m_image->root(), 0, 0, 6, true);
0392     checkIndexFromAddedDeniedDummy(m_image->root(), 1, 0, 5, true);
0393     checkIndexFromAddedDeniedDummy(m_image->root(), 2, 0, 4, true);
0394     checkIndexFromAddedDeniedDummy(m_image->root(), 3, 0, 3, true);
0395     checkIndexFromAddedDeniedDummy(m_image->root(), 4, 0, 2, true);
0396     checkIndexFromAddedDeniedDummy(m_image->root(), 5, 0, 1, true);
0397     checkIndexFromAddedDeniedDummy(m_image->root(), 6, 0, 0, true);
0398 
0399     checkIndexFromAddedDeniedDummy(m_layer1, 0, 4, 0, true);
0400 
0401     checkIndexFromAddedDeniedDummy(m_layer3, 0, 1, 2, true);
0402     checkIndexFromAddedDeniedDummy(m_layer3, 1, 1, 1, true);
0403     checkIndexFromAddedDeniedDummy(m_layer3, 2, 1, 0, true);
0404 
0405     checkIndexFromAddedDeniedDummy(0, 0, 0, 0, false);
0406 }
0407 
0408 void KisModelIndexConverterTest::testDummyFromRowShowAll()
0409 {
0410     m_indexConverter = new KisModelIndexConverterShowAll(m_dummiesFacade, m_nodeModel);
0411 
0412     checkDummyFromRow(m_image->root(), 0, m_layer4);
0413     checkDummyFromRow(m_image->root(), 1, m_layer3);
0414     checkDummyFromRow(m_image->root(), 2, m_sel2);
0415     checkDummyFromRow(m_image->root(), 3, m_layer2);
0416     checkDummyFromRow(m_image->root(), 4, m_layer1);
0417     checkDummyFromRow(m_image->root(), 5, m_sel1);
0418 
0419     checkDummyFromRow(0, 0, m_image->root());
0420 
0421     checkDummyFromRow(m_layer3, 0, m_sel3);
0422     checkDummyFromRow(m_layer3, 1, m_mask1);
0423 }
0424 
0425 
0426 void KisModelIndexConverterTest::testRowCountShowAll()
0427 {
0428     m_indexConverter = new KisModelIndexConverterShowAll(m_dummiesFacade, m_nodeModel);
0429 
0430     checkRowCount(m_image->root(), 6);
0431     checkRowCount(0, 1);
0432 
0433     checkRowCount(m_layer1, 0);
0434     checkRowCount(m_layer2, 0);
0435     checkRowCount(m_layer3, 2);
0436     checkRowCount(m_layer4, 0);
0437 }
0438 
0439 SIMPLE_TEST_MAIN(KisModelIndexConverterTest)