Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Add unit tests to CutThroughShapeStrategy

Agata Cackoeba492fd5ab1
diff --git a/plugins/tools/tool_knife/CMakeLists.txt b/plugins/tools/tool_knife/CMakeLists.txt
index 0ba936fa3f..25997bd777 100644
--- a/plugins/tools/tool_knife/CMakeLists.txt
+++ b/plugins/tools/tool_knife/CMakeLists.txt
@@ -1,19 +1,30 @@
-set(kritatoolKnife_SOURCES
- ToolKnife.cpp
+add_subdirectory(tests)
+
+set(kritatoolKnife_static_SRCS
KisToolKnife.cpp
KisToolKnifeOptionsWidget.cpp
- CutThroughShapeStrategy.cpp
+ CutThroughShapeStrategy.cpp
RemoveGutterStrategy.cpp
GutterWidthsConfig.cpp
)
-ki18n_wrap_ui(kritatoolKnife_SOURCES KisToolKnifeOptionsWidget.ui)
+
+set(kritatoolKnife_SOURCES
+ ToolKnife.cpp
+ )
+
+
+ki18n_wrap_ui(kritatoolKnife_static_SOURCES KisToolKnifeOptionsWidget.ui)
+
+kis_add_library(kritatoolKnife_static STATIC ${kritatoolKnife_static_SRCS})
+generate_export_header(kritatoolKnife_static BASE_NAME kritatoolKnife)
+
kis_add_library(kritatoolKnife MODULE ${kritatoolKnife_SOURCES})
-generate_export_header(kritatoolKnife BASE_NAME kritatoolKnife)
-target_link_libraries(kritatoolKnife kritaui)
+target_link_libraries(kritatoolKnife_static PUBLIC kritaui)
+target_link_libraries(kritatoolKnife PRIVATE kritatoolKnife_static)
install(TARGETS kritatoolKnife DESTINATION ${KRITA_PLUGIN_INSTALL_DIR})
diff --git a/plugins/tools/tool_knife/CutThroughShapeStrategy.h b/plugins/tools/tool_knife/CutThroughShapeStrategy.h
index 1fdee58e70..cead069849 100644
--- a/plugins/tools/tool_knife/CutThroughShapeStrategy.h
+++ b/plugins/tools/tool_knife/CutThroughShapeStrategy.h
@@ -48,6 +48,7 @@ private:
private:
+ friend class CutThroughShapeStrategyTest;
QPointF m_startPoint = QPointF();
QPointF m_endPoint = QPointF();
diff --git a/plugins/tools/tool_knife/tests/CMakeLists.txt b/plugins/tools/tool_knife/tests/CMakeLists.txt
new file mode 100644
index 0000000000..8018efcd55
--- /dev/null
+++ b/plugins/tools/tool_knife/tests/CMakeLists.txt
@@ -0,0 +1,7 @@
+########### next target ###############
+
+kis_add_test(CutThroughShapeStrategyTest.cpp ../CutThroughShapeStrategy.cpp
+ TEST_NAME CutThroughShapeStrategyTest
+ LINK_LIBRARIES kritatoolKnife_static kritaflake kritaui kritatestsdk
+ )
+
diff --git a/plugins/tools/tool_knife/tests/CutThroughShapeStrategyTest.cpp b/plugins/tools/tool_knife/tests/CutThroughShapeStrategyTest.cpp
new file mode 100644
index 0000000000..f9caf6c98c
--- /dev/null
+++ b/plugins/tools/tool_knife/tests/CutThroughShapeStrategyTest.cpp
@@ -0,0 +1,223 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Agata Cacko <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "CutThroughShapeStrategyTest.h"
+
+#include "CutThroughShapeStrategy.h"
+#include "KisToolKnife.h"
+#include <KoSelection.h>
+#include <KoCanvasBase.h>
+#include <tests/MockShapes.h>
+#include <KoSvgTextShape.h>
+
+
+#include <testutil.h>
+#include "kistest.h"
+#include <KoPathShape.h>
+#include <KoShape.h>
+
+#include <QPainterPath>
+#include <QRectF>
+#include <QLineF>
+#include <QPolygonF>
+#include <QSharedPointer>
+#include <kis_algebra_2d.h>
+#include <QRandomGenerator>
+
+Q_DECLARE_METATYPE(QPainterPath)
+Q_DECLARE_METATYPE(QSharedPointer<KoShape>)
+
+
+void CutThroughShapeStrategyTest::addRandom() {
+
+ QRandomGenerator gen;
+ gen.seed(1024);
+ int range = 1000;
+
+ gen.bounded(range);
+
+
+ QPainterPath shapeLeft;
+ shapeLeft.addPolygon(QPolygonF({QPointF(gen.bounded(range), gen.bounded(range)), QPointF(gen.bounded(range), gen.bounded(range)), QPointF(gen.bounded(range), gen.bounded(range)), QPointF(gen.bounded(range), gen.bounded(range)), QPointF(gen.bounded(range), gen.bounded(range))}));
+
+ QPainterPath shapeRight;
+ shapeRight.addPolygon(QPolygonF({QPointF(300, 100), QPointF(800, 100), QPointF(800, 500), QPointF(400, 500), QPointF(300, 100)}));
+
+ QRectF outlineRect = shapeLeft.boundingRect() | shapeRight.boundingRect();
+
+ QLineF gapLine1 = QLineF(QPointF(0, 300), QPointF(150, 600));
+ qreal width = 20;
+
+ QList<QLineF> gapLines = KisAlgebra2D::getParallelLines(gapLine1, width/2);
+
+ QPainterPath left, right;
+ QRectF gapLineRect;
+ QPolygonF gapLinePolygon;
+
+ CutThroughShapeStrategy::initializeGapShapes(outlineRect, gapLines[0], gapLines[1], left, right, gapLineRect, gapLinePolygon);
+
+ ENTER_FUNCTION() << ppVar(gapLineRect);
+
+ QTest::addRow("check 2: cannot be outside of rect of the gap") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << QPainterPath() << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines[0] << gapLines[1] << gapLinePolygon << false << false;
+
+
+
+ QTest::addRow("check 3: cannot be outside of rect of the gap") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << QPainterPath() << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines[0] << gapLines[1] << gapLinePolygon << false << false;
+}
+
+void CutThroughShapeStrategyTest::WillShapeBeCutTest_data()
+{
+
+ QTest::addColumn<QSharedPointer<KoShape>>("referenceShape");
+ QTest::addColumn<QPainterPath>("srcOutline");
+ QTest::addColumn<bool>("checkGapLineRect");
+ QTest::addColumn<QRectF>("gapLineRect");
+ QTest::addColumn<QLineF>("gapLine");
+ QTest::addColumn<QLineF>("leftLine");
+ QTest::addColumn<QLineF>("rightLine");
+ QTest::addColumn<QPolygonF>("gapLinePolygon");
+ QTest::addColumn<bool>("expectedGeneral");
+ QTest::addColumn<bool>("expectedPrecise");
+
+ //QTest::addRow() << referenceShape << srcOutline << leftOppositeRect << rightOppositeRect << checkGapLineRect << gapLineRect << gapLine << leftLine << rightLine << gapLinePolygon << expectedGeneral << expectedPrecise;
+
+ QPainterPath path;
+ path.moveTo(20, 40);
+ path.lineTo(200, 400);
+ path.lineTo(40, 50);
+
+
+ QSharedPointer<KoShape> referenceShape(KoPathShape::createShapeFromPainterPath(path));
+
+ //
+ //QTest::addRow("a") << referenceShape << QPainterPath() << QRectF() << QRectF() << false << QRectF() << QLineF() << QLineF() << QLineF() << QPolygonF() << true << true;
+
+
+
+
+ //
+ KoSvgTextShape* text = new KoSvgTextShape();
+ ENTER_FUNCTION() << text->outline() << text->outlineRect();
+ text->insertText(0, "Test case 1");
+ ENTER_FUNCTION() << text->outline() << text->outlineRect();
+ QSharedPointer<KoShape> textP(text);
+ QTest::addRow("check 1: cannot be text") << textP << QPainterPath() << false << QRectF() << QLineF() << QLineF() << QLineF() << QPolygonF() << false << false;
+
+
+ QPainterPath shapeLeft;
+ shapeLeft.addPolygon(QPolygonF({QPointF(100, 100), QPointF(200, 100), QPointF(300, 500), QPointF(100, 500), QPointF(100, 100)}));
+
+ QPainterPath shapeRight;
+ shapeRight.addPolygon(QPolygonF({QPointF(300, 100), QPointF(800, 100), QPointF(800, 500), QPointF(400, 500), QPointF(300, 100)}));
+
+ QRectF outlineRect = shapeLeft.boundingRect() | shapeRight.boundingRect();
+
+ QLineF gapLine1 = QLineF(QPointF(0, 300), QPointF(50, 900));
+ qreal width = 20;
+
+ QList<QLineF> gapLines1 = KisAlgebra2D::getParallelLines(gapLine1, width/2);
+
+ QPainterPath left, right;
+ QRectF gapLineRect;
+ QPolygonF gapLinePolygon;
+
+ CutThroughShapeStrategy::initializeGapShapes(outlineRect, gapLines1[0], gapLines1[1], left, right, gapLineRect, gapLinePolygon);
+
+ ENTER_FUNCTION() << ppVar(gapLineRect);
+
+ QTest::addRow("check 2 case 1: cannot be outside of rect of the gap") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << shapeLeft << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines1[0] << gapLines1[1] << gapLinePolygon << false << false;
+
+
+
+ QTest::addRow("check 2 case 2: cannot be outside of rect of the gap") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeRight)) << shapeRight << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines1[0] << gapLines1[1] << gapLinePolygon << false << false;
+
+
+ gapLine1 = QLineF(QPointF(0, 300), QPointF(150, 300));
+ gapLines1 = KisAlgebra2D::getParallelLines(gapLine1, width/2);
+ CutThroughShapeStrategy::initializeGapShapes(outlineRect, gapLines1[0], gapLines1[1], left, right, gapLineRect, gapLinePolygon);
+
+
+ QTest::addRow("check 3: cannot have exactly one line point within the shape") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << shapeLeft << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines1[0] << gapLines1[1] << gapLinePolygon << true << false;
+
+
+
+
+ gapLine1 = QLineF(QPointF(150, 300), QPointF(200, 300));
+ gapLines1 = KisAlgebra2D::getParallelLines(gapLine1, width/2);
+ CutThroughShapeStrategy::initializeGapShapes(outlineRect, gapLines1[0], gapLines1[1], left, right, gapLineRect, gapLinePolygon);
+
+
+ QTest::addRow("check 4a: can have two points within the shape") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << shapeLeft << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines1[0] << gapLines1[1] << gapLinePolygon << true << true;
+
+
+ gapLine1 = QLineF(QPointF(150, 300), QPointF(200, 300));
+ gapLines1 = KisAlgebra2D::getParallelLines(gapLine1, width/2);
+ CutThroughShapeStrategy::initializeGapShapes(outlineRect, gapLines1[0], gapLines1[1], left, right, gapLineRect, gapLinePolygon);
+
+
+ QTest::addRow("check 4b: can cross either gap line") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << shapeLeft << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines1[0] << gapLines1[1] << gapLinePolygon << true << true;
+
+
+ gapLine1 = QLineF(QPointF(80, 80), QPointF(310, 510));
+ gapLines1 = KisAlgebra2D::getParallelLines(gapLine1, 200);
+ CutThroughShapeStrategy::initializeGapShapes(outlineRect, gapLines1[0], gapLines1[1], left, right, gapLineRect, gapLinePolygon);
+
+ ENTER_FUNCTION() << ppVar(gapLinePolygon);
+
+
+ QTest::addRow("check 4c: can be inside of the gap") << QSharedPointer<KoShape>(KoPathShape::createShapeFromPainterPath(shapeLeft)) << shapeLeft << !gapLineRect.isEmpty()
+ << gapLineRect << gapLine1 << gapLines1[0] << gapLines1[1] << gapLinePolygon << true << true;
+
+
+
+
+
+}
+
+
+
+void CutThroughShapeStrategyTest::WillShapeBeCutTest()
+{
+
+ QFETCH(QSharedPointer<KoShape>, referenceShape);
+ QFETCH(QPainterPath, srcOutline);
+ QFETCH(bool, checkGapLineRect);
+ QFETCH(QRectF, gapLineRect);
+ QFETCH(QLineF, gapLine);
+ QFETCH(QLineF, leftLine);
+ QFETCH(QLineF, rightLine);
+ QFETCH(QPolygonF, gapLinePolygon);
+ QFETCH(bool, expectedGeneral);
+ QFETCH(bool, expectedPrecise);
+
+ ENTER_FUNCTION() << referenceShape->absoluteOutlineRect();
+
+
+ //KoSelection koSelection;
+ //KoCanvasBase koCanvasBase;
+ //MockCanvas mockCanvas;
+ //KisToolKnife toolKnife = KisToolKnife(&mockCanvas);
+ //CutThroughShapeStrategy strategy(&toolKnife, &koSelection, {}, QPointF(), GutterWidthsConfig(KoUnit(), 1.0, 1.0, 1.0));
+ //ENTER_FUNCTION() << strategy.decorationThickness();
+
+ bool resultGeneral = CutThroughShapeStrategy::willShapeBeCutGeneral(referenceShape.data(), srcOutline, checkGapLineRect, gapLineRect);
+ bool resultPrecise = CutThroughShapeStrategy::willShapeBeCutPrecise(srcOutline, gapLine, leftLine, rightLine, gapLinePolygon);
+
+ QCOMPARE(resultGeneral, expectedGeneral);
+ QCOMPARE(resultPrecise, expectedPrecise);
+
+
+
+}
+
+KISTEST_MAIN(CutThroughShapeStrategyTest)
diff --git a/plugins/tools/tool_knife/tests/CutThroughShapeStrategyTest.h b/plugins/tools/tool_knife/tests/CutThroughShapeStrategyTest.h
new file mode 100644
index 0000000000..31f453b950
--- /dev/null
+++ b/plugins/tools/tool_knife/tests/CutThroughShapeStrategyTest.h
@@ -0,0 +1,24 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Agata Cacko <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef CUT_THROUGH_SHAPE_STRATEGY_TEST_H
+#define CUT_THROUGH_SHAPE_STRATEGY_TEST_H
+
+#include <QObject>
+
+class CutThroughShapeStrategyTest : public QObject
+{
+ Q_OBJECT
+private:
+ void addRandom();
+
+private Q_SLOTS:
+
+ void WillShapeBeCutTest_data();
+ void WillShapeBeCutTest();
+};
+
+#endif // CUT_THROUGH_SHAPE_STRATEGY_TEST_H