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

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_dummies_facade_base_test.h"
0008 
0009 #include <simpletest.h>
0010 
0011 #include "kis_node_dummies_graph.h"
0012 #include "kis_dummies_facade.h"
0013 #include "node_shapes_utils.h"
0014 
0015 void KisDummiesFacadeBaseTest::init()
0016 {
0017     initBase();
0018     m_dummiesFacade = dummiesFacadeFactory();
0019 
0020     m_activatedNodes.clear();
0021     m_movedDummies.clear();
0022     connect(m_dummiesFacade, SIGNAL(sigActivateNode(KisNodeSP)),
0023             SLOT(slotNodeActivated(KisNodeSP)));
0024     connect(m_dummiesFacade, SIGNAL(sigEndInsertDummy(KisNodeDummy*)),
0025             SLOT(slotEndInsertDummy(KisNodeDummy*)));
0026     connect(m_dummiesFacade, SIGNAL(sigBeginRemoveDummy(KisNodeDummy*)),
0027             SLOT(slotBeginRemoveDummy(KisNodeDummy*)));
0028 }
0029 
0030 void KisDummiesFacadeBaseTest::cleanup()
0031 {
0032     destroyDummiesFacade(m_dummiesFacade);
0033     cleanupBase();
0034 }
0035 
0036 void KisDummiesFacadeBaseTest::slotNodeActivated(KisNodeSP node)
0037 {
0038     QString prefix = m_activatedNodes.isEmpty() ? "" : " ";
0039     QString name = node ? node->name() : "__null";
0040 
0041     m_activatedNodes += prefix + name;
0042 }
0043 
0044 void KisDummiesFacadeBaseTest::slotEndInsertDummy(KisNodeDummy *dummy)
0045 {
0046     QString prefix = m_movedDummies.isEmpty() ? "" : " ";
0047     QString name = dummy->node()->name();
0048 
0049     m_movedDummies += prefix + "A_" + name;
0050 }
0051 
0052 void KisDummiesFacadeBaseTest::slotBeginRemoveDummy(KisNodeDummy *dummy)
0053 {
0054     QString prefix = m_movedDummies.isEmpty() ? "" : " ";
0055     QString name = dummy->node()->name();
0056 
0057     m_movedDummies += prefix + "R_" + name;
0058 }
0059 
0060 void KisDummiesFacadeBaseTest::verifyActivatedNodes(const QString &nodes)
0061 {
0062     if (nodes != m_activatedNodes)
0063         QEXPECT_FAIL("", "Expected nodes string is not the same as the activated nodes string", Continue);
0064     QCOMPARE(m_activatedNodes, nodes);
0065 }
0066 
0067 void KisDummiesFacadeBaseTest::verifyMovedDummies(const QString &nodes)
0068 {
0069     if (nodes != m_movedDummies)
0070         QEXPECT_FAIL("", "Expected nodes string is not the same as the moved dummies", Continue);
0071     QCOMPARE(m_movedDummies, nodes);
0072 }
0073 
0074 void KisDummiesFacadeBaseTest::testSetImage()
0075 {
0076     constructImage();
0077 
0078     m_dummiesFacade->setImage(m_image);
0079 
0080     QString actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0081     QString expectedGraph = "root layer1 layer2 layer3 mask1 layer4";
0082 
0083     QCOMPARE(actualGraph, expectedGraph);
0084     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0085 
0086     m_dummiesFacade->setImage(0);
0087     QCOMPARE(m_dummiesFacade->dummiesCount(), 0);
0088 
0089     verifyActivatedNodes("layer1 __null");
0090     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_mask1 A_layer4 "
0091                        "R_layer4 R_mask1 R_layer3 R_layer2 R_layer1 R_root");
0092 }
0093 
0094 void KisDummiesFacadeBaseTest::testAddNode()
0095 {
0096     QString actualGraph;
0097     QString expectedGraph;
0098 
0099     m_dummiesFacade->setImage(m_image);
0100 
0101     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0102     expectedGraph = "root";
0103 
0104     QCOMPARE(actualGraph, expectedGraph);
0105     QCOMPARE(m_dummiesFacade->dummiesCount(), 1);
0106 
0107     constructImage();
0108 
0109     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0110     expectedGraph = "root layer1 layer2 layer3 mask1 layer4";
0111 
0112     QCOMPARE(actualGraph, expectedGraph);
0113     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0114 
0115     m_dummiesFacade->setImage(0);
0116     QCOMPARE(m_dummiesFacade->dummiesCount(), 0);
0117 
0118     verifyActivatedNodes("__null layer1 layer2 layer3 layer4 mask1 __null");
0119     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_layer4 A_mask1 "
0120                        "R_layer4 R_mask1 R_layer3 R_layer2 R_layer1 R_root");
0121 }
0122 
0123 void KisDummiesFacadeBaseTest::testRemoveNode()
0124 {
0125     QString actualGraph;
0126     QString expectedGraph;
0127 
0128     constructImage();
0129 
0130     m_dummiesFacade->setImage(m_image);
0131 
0132     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0133     expectedGraph = "root layer1 layer2 layer3 mask1 layer4";
0134 
0135     QCOMPARE(actualGraph, expectedGraph);
0136     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0137 
0138     m_image->removeNode(m_layer2);
0139 
0140     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0141     expectedGraph = "root layer1 layer3 mask1 layer4";
0142 
0143     QCOMPARE(actualGraph, expectedGraph);
0144     QCOMPARE(m_dummiesFacade->dummiesCount(), 5);
0145 
0146     m_image->removeNode(m_layer3);
0147 
0148     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0149     expectedGraph = "root layer1 layer4";
0150 
0151     QCOMPARE(actualGraph, expectedGraph);
0152     QCOMPARE(m_dummiesFacade->dummiesCount(), 3);
0153 
0154     m_dummiesFacade->setImage(0);
0155 
0156     // we are not expected to handle nodes removal, it is done by Qt
0157     verifyActivatedNodes("layer1 __null");
0158 
0159     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_mask1 A_layer4 "
0160                        "R_layer2 R_mask1 R_layer3 R_layer4 R_layer1 R_root");
0161 }
0162 
0163 void KisDummiesFacadeBaseTest::testMoveNodeSameParent()
0164 {
0165     QString actualGraph;
0166     QString expectedGraph;
0167 
0168     constructImage();
0169 
0170     m_dummiesFacade->setImage(m_image);
0171 
0172     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0173     expectedGraph = "root layer1 layer2 layer3 mask1 layer4";
0174 
0175     QCOMPARE(actualGraph, expectedGraph);
0176     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0177 
0178     m_image->moveNode(m_layer2, m_image->root(), m_layer3);
0179 
0180     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0181     expectedGraph = "root layer1 layer3 mask1 layer2 layer4";
0182 
0183     QCOMPARE(actualGraph, expectedGraph);
0184     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0185 
0186     m_dummiesFacade->setImage(0);
0187 
0188     // layer is first removed then added again
0189     verifyActivatedNodes("layer1 layer2 __null");
0190 
0191     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_mask1 A_layer4 "
0192                        "R_layer2 A_layer2 "
0193                        "R_layer4 R_layer2 R_mask1 R_layer3 R_layer1 R_root");
0194 }
0195 
0196 void KisDummiesFacadeBaseTest::testMoveNodeDifferentParent()
0197 {
0198     QString actualGraph;
0199     QString expectedGraph;
0200 
0201     constructImage();
0202 
0203     m_dummiesFacade->setImage(m_image);
0204 
0205     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0206     expectedGraph = "root layer1 layer2 layer3 mask1 layer4";
0207 
0208     QCOMPARE(actualGraph, expectedGraph);
0209     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0210 
0211     m_image->moveNode(m_layer2, m_image->root(), m_layer4);
0212 
0213     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0214     expectedGraph = "root layer1 layer3 mask1 layer4 layer2";
0215 
0216     QCOMPARE(actualGraph, expectedGraph);
0217     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0218 
0219     m_image->moveNode(m_layer3, m_layer4, m_layer4->lastChild());
0220 
0221     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0222     expectedGraph = "root layer1 layer4 layer3 mask1 layer2";
0223 
0224     QCOMPARE(actualGraph, expectedGraph);
0225     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0226 
0227     m_dummiesFacade->setImage(0);
0228 
0229     // layer is first removed then added again
0230     verifyActivatedNodes("layer1 layer2 layer3 __null");
0231 
0232     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_mask1 A_layer4 "
0233                        "R_layer2 A_layer2 R_mask1 R_layer3 A_layer3 A_mask1 "
0234                        "R_layer2 R_mask1 R_layer3 R_layer4 R_layer1 R_root");
0235 }
0236 
0237 void KisDummiesFacadeBaseTest::testSubstituteRootNode()
0238 {
0239     QString actualGraph;
0240     QString expectedGraph;
0241 
0242     constructImage();
0243 
0244     m_dummiesFacade->setImage(m_image);
0245 
0246     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0247     expectedGraph = "root layer1 layer2 layer3 mask1 layer4";
0248 
0249     QCOMPARE(actualGraph, expectedGraph);
0250     QCOMPARE(m_dummiesFacade->dummiesCount(), 6);
0251 
0252     m_image->flatten(0);
0253     m_image->waitForDone();
0254     QTest::qWait(50);
0255 
0256     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0257     expectedGraph = "root root Merged"; // "root Merged" is the name of the layer :)
0258 
0259     QCOMPARE(actualGraph, expectedGraph);
0260     QCOMPARE(m_dummiesFacade->dummiesCount(), 2);
0261 
0262     m_dummiesFacade->setImage(0);
0263 
0264     verifyActivatedNodes("layer1 __null root Merged __null");
0265     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_mask1 A_layer4 "
0266                        "R_layer4 R_mask1 R_layer3 R_layer2 R_layer1 R_root "
0267                        "A_root A_root Merged "
0268                        "R_root Merged R_root");
0269 }
0270 
0271 void KisDummiesFacadeBaseTest::testAddSelectionMasksNoActivation()
0272 {
0273     QString actualGraph;
0274     QString expectedGraph;
0275 
0276     m_dummiesFacade->setImage(m_image);
0277 
0278     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0279     expectedGraph = "root";
0280 
0281     QCOMPARE(actualGraph, expectedGraph);
0282     QCOMPARE(m_dummiesFacade->dummiesCount(), 1);
0283 
0284     constructImage();
0285     addSelectionMasks();
0286 
0287     actualGraph = collectGraphPatternFull(m_dummiesFacade->rootDummy());
0288     expectedGraph = "root sel1 layer1 layer2 sel2 layer3 mask1 sel3 layer4";
0289 
0290     QCOMPARE(actualGraph, expectedGraph);
0291     QCOMPARE(m_dummiesFacade->dummiesCount(), 9);
0292 
0293     m_dummiesFacade->setImage(0);
0294     QCOMPARE(m_dummiesFacade->dummiesCount(), 0);
0295 
0296     verifyActivatedNodes("__null layer1 layer2 layer3 layer4 mask1 __null");
0297     verifyMovedDummies("A_root A_layer1 A_layer2 A_layer3 A_layer4 A_mask1 "
0298                        "A_sel1 A_sel2 A_sel3 "
0299                        "R_layer4 R_sel3 R_mask1 R_layer3 R_sel2 "
0300                        "R_layer2 R_layer1 R_sel1 R_root");
0301 }