1 /*
2  *  Copyright (c) 2017 Dmitry Kazakov <dimula73@gmail.com>
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 "TestKoMarkerCollection.h"
20 
21 #include <QTest>
22 #include <QFileInfo>
23 #include <QPainter>
24 #include <QPainterPath>
25 #include <KoMarker.h>
26 #include <KoMarkerCollection.h>
27 #include <KoPathShape.h>
28 #include <KoColorBackground.h>
29 
30 #include "kis_debug.h"
31 #include "../../sdk/tests/qimage_test_util.h"
32 #include <sdk/tests/kistest.h>
33 
34 #include <cmath>
35 
36 
initMarkerCollection(KoMarkerCollection * collection)37 void initMarkerCollection(KoMarkerCollection *collection)
38 {
39     QCOMPARE(collection->markers().size(), 1);
40 
41     const QString fileName = TestUtil::fetchDataFileLazy("test_markers.svg");
42     QVERIFY(QFileInfo(fileName).exists());
43 
44     collection->loadMarkersFromFile(fileName);
45     QCOMPARE(collection->markers().size(), 10);
46 }
47 
testLoadMarkersFromFile()48 void TestKoMarkerCollection::testLoadMarkersFromFile()
49 {
50     KoMarkerCollection collection;
51     initMarkerCollection(&collection);
52 }
53 
testDeduplication()54 void TestKoMarkerCollection::testDeduplication()
55 {
56     QPainterPath path1;
57     path1.addRect(QRect(5,5,15,15));
58 
59     KoPathShape *shape1(KoPathShape::createShapeFromPainterPath(path1));
60     shape1->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(Qt::blue)));
61 
62     KoMarker *marker(new KoMarker());
63     marker->setAutoOrientation(true);
64     marker->setShapes({shape1});
65 
66     KoMarkerCollection collection;
67     QCOMPARE(collection.markers().size(), 1);
68 
69     KoMarker *clonedMarker = new KoMarker(*marker);
70 
71     collection.addMarker(marker);
72     QCOMPARE(collection.markers().size(), 2);
73 
74     collection.addMarker(marker);
75     QCOMPARE(collection.markers().size(), 2);
76 
77     collection.addMarker(clonedMarker);
78     QCOMPARE(collection.markers().size(), 2);
79 }
80 
testOneMarkerPosition(KoMarker * marker,KoFlake::MarkerPosition position,const QString & testName)81 void testOneMarkerPosition(KoMarker *marker, KoFlake::MarkerPosition position, const QString &testName)
82 {
83     QImage image(30,30, QImage::Format_ARGB32);
84     image.fill(0);
85     QPainter painter(&image);
86 
87     QPen pen(Qt::black, 2);
88     marker->drawPreview(&painter, image.rect(), pen, position);
89 
90     QVERIFY(TestUtil::checkQImage(image, "marker_collection", "preview", testName));
91 }
92 
testMarkerBounds()93 void TestKoMarkerCollection::testMarkerBounds()
94 {
95     KoMarkerCollection collection;
96     initMarkerCollection(&collection);
97 
98     QList<KoMarker*> allMarkers = collection.markers();
99     KoMarker *marker = allMarkers[3];
100 
101     QCOMPARE(marker->boundingRect(1, 0).toAlignedRect(), QRect(-7,-3,9,6));
102     QCOMPARE(marker->boundingRect(1, M_PI).toAlignedRect(), QRect(-2,-3,9,6));
103 
104     QCOMPARE(marker->outline(1, 0).boundingRect().toAlignedRect(), QRect(-6,-2,7,4));
105     QCOMPARE(marker->outline(1, M_PI).boundingRect().toAlignedRect(), QRect(-1,-2,7,4));
106 
107     testOneMarkerPosition(marker, KoFlake::StartMarker, "start_marker");
108     testOneMarkerPosition(marker, KoFlake::MidMarker, "mid_marker");
109     testOneMarkerPosition(marker, KoFlake::EndMarker, "end_marker");
110 }
111 
112 KISTEST_MAIN(TestKoMarkerCollection)
113