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_node_shapes_graph_test.h"
0008 
0009 #include <simpletest.h>
0010 
0011 #include "kis_node_shapes_graph.h"
0012 #include "node_shapes_utils.h"
0013 
0014 
0015 /**
0016  * node0
0017  * +--node1
0018  *    +--node7
0019  *    +--node8
0020  * +--node2
0021  * +--node3
0022  * +--node4
0023  *    +--node9
0024  *    +--node10
0025  *    +--node11
0026  * +--node5
0027  * +--node6
0028  */
0029 
0030 void KisNodeShapesGraphTest::init()
0031 {
0032     m_shapesGraph = new KisNodeShapesGraph();
0033     KisNodeSP rootNode = nodeFromId(0);
0034     m_shapesGraph->addNode(rootNode, 0, 0);
0035     m_rootDummy = m_shapesGraph->nodeToDummy(rootNode);
0036 
0037 
0038     KisNodeSP parent;
0039 
0040     parent = rootNode;
0041     for(int i = 6; i >= 1; i--) {
0042         m_shapesGraph->addNode(nodeFromId(i), parent, 0);
0043     }
0044 
0045     parent = findNodeById(m_rootDummy, 1);
0046     Q_ASSERT(parent);
0047     for(int i = 8; i >= 7; i--) {
0048         m_shapesGraph->addNode(nodeFromId(i), parent, 0);
0049     }
0050 
0051     parent = findNodeById(m_rootDummy, 4);
0052     Q_ASSERT(parent);
0053     for(int i = 11; i >= 9; i--) {
0054         m_shapesGraph->addNode(nodeFromId(i), parent, 0);
0055     }
0056 
0057     QString realGraph = collectGraphPattern(m_rootDummy);
0058     QString expectedGraph = "0 1 7 8 2 3 4 9 10 11 5 6";
0059 
0060     QCOMPARE(realGraph, expectedGraph);
0061 }
0062 
0063 void KisNodeShapesGraphTest::cleanup()
0064 {
0065     if (m_rootDummy) {
0066         KisNodeShape *tempShape = m_rootDummy->nodeShape();
0067         delete m_rootDummy;
0068         delete tempShape;
0069     }
0070 
0071     delete m_shapesGraph;
0072 }
0073 
0074 void KisNodeShapesGraphTest::testShapeChildren()
0075 {
0076     KisNodeShape *parent = m_shapesGraph->nodeToShape(findNodeById(m_rootDummy, 4));
0077     QList<KisNodeShape*> expectedChildren;
0078 
0079     for(int i = 9; i <= 11; i++) {
0080         expectedChildren.append(m_shapesGraph->nodeToShape(findNodeById(m_rootDummy, i)));
0081     }
0082 
0083     QList<KoShape*> realChildren = parent->shapes();
0084 
0085     Q_FOREACH (KoShape *shape, realChildren) {
0086         KisNodeShape *nodeShape = dynamic_cast<KisNodeShape*>(shape);
0087 
0088         QVERIFY(expectedChildren.contains(nodeShape));
0089         expectedChildren.removeOne(nodeShape);
0090     }
0091 
0092     QVERIFY(expectedChildren.isEmpty());
0093 }
0094 
0095 void KisNodeShapesGraphTest::testInsert()
0096 {
0097     KisNodeSP parent = findNodeById(m_rootDummy, 4);
0098     KisNodeSP aboveThis = findNodeById(m_rootDummy, 10);
0099     KisNodeSP node = nodeFromId(13);
0100 
0101     KisNodeShape *addedShape =
0102         m_shapesGraph->addNode(node, parent, aboveThis);
0103 
0104     QString realGraph = collectGraphPattern(m_rootDummy);
0105     QString expectedGraph = "0 1 7 8 2 3 4 9 10 13 11 5 6";
0106     QCOMPARE(realGraph, expectedGraph);
0107 
0108     KisNodeShape *nodeShape = m_shapesGraph->nodeToShape(node);
0109     KisNodeShape *parentShape = m_shapesGraph->nodeToShape(parent);
0110     QCOMPARE(addedShape, nodeShape);
0111     QCOMPARE(((KoShape*)nodeShape)->parent(), parentShape);
0112 }
0113 
0114 void KisNodeShapesGraphTest::testRemove()
0115 {
0116     KisNodeSP parent = findNodeById(m_rootDummy, 4);
0117     KisNodeSP node = findNodeById(m_rootDummy, 10);
0118 
0119     KisNodeShape *parentShape = m_shapesGraph->nodeToShape(parent);
0120 
0121     QCOMPARE(parentShape->shapeCount(), 3);
0122     m_shapesGraph->removeNode(node);
0123     QCOMPARE(parentShape->shapeCount(), 2);
0124 
0125     QString realGraph = collectGraphPattern(m_rootDummy);
0126     QString expectedGraph = "0 1 7 8 2 3 4 9 11 5 6";
0127     QCOMPARE(realGraph, expectedGraph);
0128 }
0129 
0130 void KisNodeShapesGraphTest::testRemoveRootNode()
0131 {
0132     KisNodeSP root = findNodeById(m_rootDummy, 0);
0133     m_rootDummy = 0;
0134 
0135     m_shapesGraph->removeNode(root);
0136     QCOMPARE(m_shapesGraph->shapesCount(), 0);
0137 }
0138 
0139 SIMPLE_TEST_MAIN(KisNodeShapesGraphTest)