Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti committed May 17, 2021
1 parent 818396a commit 1db3e54
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions tests/src/app/testqgsvertextool.cpp
Expand Up @@ -70,6 +70,7 @@ class TestQgsVertexTool : public QObject
void testAddVertexAtEndpoint();
void testAddVertexDoubleClick();
void testAddVertexDoubleClickWithShift();
void testAvoidIntersections();
void testDeleteVertex();
void testMoveMultipleVertices();
void testMoveMultipleVertices2();
Expand Down Expand Up @@ -160,6 +161,7 @@ class TestQgsVertexTool : public QObject
QgsFeatureId mFidMultiLineF1 = 0;
QgsFeatureId mFidLineF13857 = 0;
QgsFeatureId mFidPolygonF1 = 0;
QgsFeatureId mFidPolygonF2 = 0;
QgsFeatureId mFidMultiPolygonF1 = 0;
QgsFeatureId mFidPointF1 = 0;
QgsFeatureId mFidCompoundCurveF1 = 0;
Expand Down Expand Up @@ -229,6 +231,13 @@ void TestQgsVertexTool::initTestCase()
QgsFeature polygonF1;
polygonF1.setGeometry( QgsGeometry::fromPolygonXY( polygon1 ) );

QgsPolygonXY polygon2;
QgsPolylineXY polygon2exterior;
polygon2exterior << QgsPointXY( 8, 1 ) << QgsPointXY( 9, 1 ) << QgsPointXY( 9, 4 ) << QgsPointXY( 8, 4 ) << QgsPointXY( 8, 1 );
polygon2 << polygon2exterior;
QgsFeature polygonF2;
polygonF2.setGeometry( QgsGeometry::fromPolygonXY( polygon2 ) );

QgsFeature multiPolygonF1;
multiPolygonF1.setGeometry( QgsGeometry::fromWkt( "MultiPolygon (((1 5, 2 5, 2 6.5, 2 8, 1 8, 1 6.5, 1 5),(1.25 5.5, 1.25 6, 1.75 6, 1.75 5.5, 1.25 5.5),(1.25 7, 1.75 7, 1.75 7.5, 1.25 7.5, 1.25 7)),((3 5, 3 6.5, 3 8, 4 8, 4 6.5, 4 5, 3 5),(3.25 5.5, 3.75 5.5, 3.75 6, 3.25 6, 3.25 5.5),(3.25 7, 3.75 7, 3.75 7.5, 3.25 7.5, 3.25 7)))" ) );

Expand Down Expand Up @@ -277,6 +286,10 @@ void TestQgsVertexTool::initTestCase()
mFidPolygonF1 = polygonF1.id();
QCOMPARE( mLayerPolygon->featureCount(), ( long )1 );

mLayerPolygon->addFeature( polygonF2 );
mFidPolygonF2 = polygonF2.id();
QCOMPARE( mLayerPolygon->featureCount(), ( long )2 );

mLayerMultiPolygon->startEditing();
mLayerMultiPolygon->addFeature( multiPolygonF1 );
mFidMultiPolygonF1 = multiPolygonF1.id();
Expand Down Expand Up @@ -304,7 +317,7 @@ void TestQgsVertexTool::initTestCase()
// just one added feature in each undo stack
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerMultiLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerMultiPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );
// except for layerLineZ
Expand Down Expand Up @@ -439,7 +452,7 @@ void TestQgsVertexTool::testMoveVertex()
mouseClick( 4, 1, Qt::LeftButton );
mouseClick( 5, 2, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((5 2, 7 1, 7 4, 4 4, 5 2))" ) );

mLayerPolygon->undoStack()->undo();
Expand All @@ -449,7 +462,7 @@ void TestQgsVertexTool::testMoveVertex()
mouseClick( 4, 4, Qt::LeftButton );
mouseClick( 5, 5, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 5 5, 4 1))" ) );

mLayerPolygon->undoStack()->undo();
Expand All @@ -469,7 +482,7 @@ void TestQgsVertexTool::testMoveVertex()

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );
}

Expand All @@ -493,7 +506,7 @@ void TestQgsVertexTool::testMoveEdge()
mouseClick( 5, 1, Qt::LeftButton );
mouseClick( 6, 1, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((5 1, 8 1, 7 4, 4 4, 5 1))" ) );

mLayerPolygon->undoStack()->undo();
Expand All @@ -502,7 +515,7 @@ void TestQgsVertexTool::testMoveEdge()

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );
}

Expand All @@ -526,7 +539,7 @@ void TestQgsVertexTool::testAddVertex()
mouseClick( 4, 2.5, Qt::LeftButton );
mouseClick( 3, 2.5, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 3 2.5, 4 1))" ) );

mLayerPolygon->undoStack()->undo();
Expand All @@ -535,7 +548,7 @@ void TestQgsVertexTool::testAddVertex()

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );
}

Expand Down Expand Up @@ -614,7 +627,7 @@ void TestQgsVertexTool::testAddVertexDoubleClick()
mouseDoubleClick( 4, 2, Qt::LeftButton );
mouseClick( 3, 2.5, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 3 2.5, 4 1))" ) );

mLayerPolygon->undoStack()->undo();
Expand All @@ -623,7 +636,7 @@ void TestQgsVertexTool::testAddVertexDoubleClick()

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );

}
Expand All @@ -645,7 +658,7 @@ void TestQgsVertexTool::testAddVertexDoubleClickWithShift()
// add vertex in polygon
mouseDoubleClick( 4, 2, Qt::LeftButton, Qt::ShiftModifier );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 4 2, 4 1))" ) );

mLayerPolygon->undoStack()->undo();
Expand All @@ -654,7 +667,7 @@ void TestQgsVertexTool::testAddVertexDoubleClickWithShift()

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );

}
Expand All @@ -679,7 +692,7 @@ void TestQgsVertexTool::testDeleteVertex()
mouseClick( 7, 4, Qt::LeftButton );
keyClick( Qt::Key_Delete );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 4 4, 4 1))" ) );

mLayerPolygon->undoStack()->undo();
Expand Down Expand Up @@ -793,7 +806,7 @@ void TestQgsVertexTool::testDeleteVertex()

// no other unexpected changes happened
QCOMPARE( mLayerLine->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 1 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPoint->undoStack()->index(), 1 );
}

Expand Down Expand Up @@ -861,7 +874,7 @@ void TestQgsVertexTool::testMoveVertexTopo()
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((3 3, 7 1, 7 4, 4 4, 3 3))" ) );

QCOMPARE( mLayerLine->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 ); // one more move of vertex from earlier
QCOMPARE( mLayerPolygon->undoStack()->index(), 4 ); // one more move of vertex from earlier
mLayerLine->undoStack()->undo();
mLayerPolygon->undoStack()->undo();
mLayerPolygon->undoStack()->undo();
Expand Down Expand Up @@ -890,7 +903,7 @@ void TestQgsVertexTool::testDeleteVertexTopo()
QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((7 1, 7 4, 4 4, 7 1))" ) );

QCOMPARE( mLayerLine->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 ); // one more move of vertex from earlier
QCOMPARE( mLayerPolygon->undoStack()->index(), 4 ); // one more move of vertex from earlier
mLayerLine->undoStack()->undo();
mLayerPolygon->undoStack()->undo();
mLayerPolygon->undoStack()->undo();
Expand All @@ -912,14 +925,14 @@ void TestQgsVertexTool::testAddVertexTopo()
QVERIFY( resAdd );
QgsFeatureId fTmpId = fTmp.id();

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );

QgsProject::instance()->setTopologicalEditing( true );

mouseClick( 5.5, 4, Qt::LeftButton );
mouseClick( 5, 5, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 4 );

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 5 5, 4 4, 4 1))" ) );
QCOMPARE( mLayerPolygon->getFeature( fTmpId ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 4, 5 5, 7 4, 7 6, 4 6, 4 4))" ) );
Expand All @@ -943,15 +956,15 @@ void TestQgsVertexTool::testMoveEdgeTopo()
QVERIFY( resAdd );
QgsFeatureId fTmpId = fTmp.id();

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );

QgsProject::instance()->setTopologicalEditing( true );

// move shared segment
mouseClick( 6, 4, Qt::LeftButton );
mouseClick( 6, 5, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 4 );

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 5, 4 5, 4 1))" ) );
QCOMPARE( mLayerPolygon->getFeature( fTmpId ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 5, 7 5, 7 6, 4 6, 4 5))" ) );
Expand All @@ -973,7 +986,7 @@ void TestQgsVertexTool::testMoveEdgeTopo()
mouseClick( 4, 4, Qt::LeftButton );
mouseClick( 4, 3, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 4 );

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 3, 4 3, 4 1))" ) );
QCOMPARE( mLayerPolygon->getFeature( fTmpId ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 3, 7 3, 7 6, 4 6, 4 3))" ) );
Expand All @@ -999,7 +1012,7 @@ void TestQgsVertexTool::testAddVertexTopoFirstSegment()
mouseClick( 5.5, 1, Qt::LeftButton );
mouseClick( 5, 2, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 2 );
QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 5 2, 7 1, 7 4, 4 4, 4 1))" ) );

Expand All @@ -1010,6 +1023,29 @@ void TestQgsVertexTool::testAddVertexTopoFirstSegment()
QgsProject::instance()->setTopologicalEditing( false );
}

void TestQgsVertexTool::testAvoidIntersections()
{
// check that when adding a vertex to the first segment of a polygon's ring with topo editing
// enabled, the geometry does not get corrupted (#20774)

QgsProject::instance()->setTopologicalEditing( true );
QgsProject::AvoidIntersectionsMode mode( QgsProject::instance()->avoidIntersectionsMode() );
QgsProject::instance()->setAvoidIntersectionsMode( QgsProject::AvoidIntersectionsMode::AvoidIntersectionsCurrentLayer );

mouseClick( 6.5, 1, Qt::LeftButton );
mouseClick( 9, 2, Qt::LeftButton );

QCOMPARE( mLayerPolygon->undoStack()->index(), 3 );

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry().asWkt( 1 ), "MultiPolygon (((4 4, 7 4, 8 3.2, 8 2, 6.5 2, 4 4)),((9 2.4, 9.5 2, 9 2, 9 2.4)))" );

mLayerPolygon->undoStack()->undo();

QCOMPARE( mLayerPolygon->getFeature( mFidPolygonF1 ).geometry(), QgsGeometry::fromWkt( "POLYGON((4 1, 7 1, 7 4, 4 4, 4 1))" ) );

QgsProject::instance()->setTopologicalEditing( false );
QgsProject::instance()->setAvoidIntersectionsMode( mode );
}
void TestQgsVertexTool::testActiveLayerPriority()
{
// check that features from current layer get priority when picking points
Expand Down

0 comments on commit 1db3e54

Please sign in to comment.