1 /*
2  *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_node_query_path_test.h"
20 
21 #include <QTest>
22 
23 #include "kis_node_query_path.h"
24 #include <kis_node.h>
25 #include <kis_image.h>
26 #include <kis_group_layer.h>
27 #include <kis_paint_layer.h>
28 #include <KoColorSpaceRegistry.h>
29 
KisNodeQueryPathTest()30 KisNodeQueryPathTest::KisNodeQueryPathTest()
31 {
32     image = new KisImage(0, 100, 100, KoColorSpaceRegistry::instance()->rgb8(), "");
33     current = new KisGroupLayer(image, "", OPACITY_OPAQUE_U8);
34     child1 = new KisPaintLayer(image, "", OPACITY_OPAQUE_U8);
35     child2 = new KisPaintLayer(image, "", OPACITY_OPAQUE_U8);
36     image->addNode(child1, current);
37     image->addNode(child2, current);
38     parent = new KisGroupLayer(image, "", OPACITY_OPAQUE_U8);
39     brother1 = new KisPaintLayer(image, "", OPACITY_OPAQUE_U8);
40     brother2 = new KisPaintLayer(image, "", OPACITY_OPAQUE_U8);
41     image->addNode(brother1, parent);
42     image->addNode(current, parent);
43     image->addNode(brother2, parent);
44     image->addNode(parent, image->rootLayer());
45 }
46 
47 #define TESTS(name, relativeStr, absoluteStr1, absoluteStr2, node)              \
48     void KisNodeQueryPathTest::test##name##LayerFromRelativeString()            \
49     {                                                                           \
50         KisNodeQueryPath path = KisNodeQueryPath::fromString(relativeStr);      \
51         QCOMPARE(path.toString(), QString(relativeStr));                        \
52         QList<KisNodeSP> nodes = path.queryNodes(image, current);               \
53         QCOMPARE(nodes.size(), 1);                                              \
54         QCOMPARE(nodes[0], node);                                               \
55     }                                                                           \
56                                                                                 \
57     void KisNodeQueryPathTest::test##name##LayerFromAbsoluteString()            \
58     {                                                                           \
59         {                                                                       \
60             KisNodeQueryPath path = KisNodeQueryPath::fromString(absoluteStr1); \
61             QCOMPARE(path.toString(), QString(absoluteStr1));                   \
62             QList<KisNodeSP> nodes = path.queryNodes(image, current);           \
63             QCOMPARE(nodes.size(), 1);                                          \
64             QCOMPARE(nodes[0], node);                                           \
65         }                                                                       \
66         {                                                                       \
67             KisNodeQueryPath path = KisNodeQueryPath::fromString(absoluteStr2); \
68             QCOMPARE(path.toString(), QString(absoluteStr2));                   \
69             QList<KisNodeSP> nodes = path.queryNodes(image, current);           \
70             QCOMPARE(nodes.size(), 1);                                          \
71             QCOMPARE(nodes[0], node);                                           \
72         }                                                                       \
73     }                                                                           \
74                                                                                 \
75     void KisNodeQueryPathTest::test##name##LayerFromAbsolutePath()              \
76     {                                                                           \
77         KisNodeQueryPath path = KisNodeQueryPath::absolutePath(node);           \
78         QCOMPARE(path.toString(), QString(absoluteStr1));                       \
79         QList<KisNodeSP> nodes = path.queryNodes(image, current);               \
80         QCOMPARE(nodes.size(), 1);                                              \
81         QCOMPARE(nodes[0], node);                                               \
82     }
83 
84 TESTS(Current, ".", "/0/1", "/*/1", current)
85 TESTS(Child1, "0", "/0/1/0", "/*/1/0", child1)
86 TESTS(Child2, "1", "/0/1/1", "/*/1/1", child2)
87 TESTS(Brother1, "../0", "/0/0", "/*/0", brother1)
88 TESTS(Brother2, "../2", "/0/2", "/*/2", brother2)
89 TESTS(Parent, "..", "/0", "/*", parent)
90 TESTS(Root, "../..", "/", "/", KisNodeSP(image->rootLayer()))
91 
testPathCompression()92 void KisNodeQueryPathTest::testPathCompression()
93 {
94     KisNodeQueryPath path = KisNodeQueryPath::fromString("1/../3/../5");
95     QCOMPARE(path.toString(), QString("5"));
96     KisNodeQueryPath path2 = KisNodeQueryPath::fromString("/*/..");
97     QCOMPARE(path2.toString(), QString("/"));
98 }
99 
100 QTEST_MAIN(KisNodeQueryPathTest)
101