From b7fa0a29ff70dd3316941cf9ec9936d7c1a8434c Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 15 Jul 2021 17:13:33 +0200 Subject: QGeoShape: fix serialization of QGeoPolygon and QGeoPath There were two problems: 1. The deserialization method was still assuming that QList uses int to index the elements, so it tried to extract the polygon and path size as int, while it was serialized as qsizetype. 2. The QGeoPath didn't serialize and deserialize its width - it was simply lost. This patch fixes both problems and adds some unit-tests to cover these cases. Pick-to: 6.2 Change-Id: If0ac87731b4481fde6b91e71fb121b3e916b0bfe Reviewed-by: Alex Blasche --- src/positioning/qgeoshape.cpp | 13 ++++--- tests/auto/qgeoshape/tst_qgeoshape.cpp | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/positioning/qgeoshape.cpp b/src/positioning/qgeoshape.cpp index 859df96e..d4daf813 100644 --- a/src/positioning/qgeoshape.cpp +++ b/src/positioning/qgeoshape.cpp @@ -357,6 +357,7 @@ QDataStream &operator<<(QDataStream &stream, const QGeoShape &shape) } case QGeoShape::PathType: { QGeoPath p = shape; + stream << p.width(); stream << p.path().size(); for (const auto &c: p.path()) stream << c; @@ -400,21 +401,23 @@ QDataStream &operator>>(QDataStream &stream, QGeoShape &shape) case QGeoShape::PathType: { QList l; QGeoCoordinate c; - int sz; + qreal width; + stream >> width; + qsizetype sz; stream >> sz; - for (int i = 0; i < sz; i++) { + for (qsizetype i = 0; i < sz; i++) { stream >> c; l.append(c); } - shape = QGeoPath(l); + shape = QGeoPath(l, width); break; } case QGeoShape::PolygonType: { QList l; QGeoCoordinate c; - int sz; + qsizetype sz; stream >> sz; - for (int i = 0; i < sz; i++) { + for (qsizetype i = 0; i < sz; i++) { stream >> c; l.append(c); } diff --git a/tests/auto/qgeoshape/tst_qgeoshape.cpp b/tests/auto/qgeoshape/tst_qgeoshape.cpp index 72611ff6..77f36a28 100644 --- a/tests/auto/qgeoshape/tst_qgeoshape.cpp +++ b/tests/auto/qgeoshape/tst_qgeoshape.cpp @@ -31,6 +31,8 @@ #include #include #include +#include +#include QString tst_qgeoshape_debug; @@ -55,6 +57,7 @@ private slots: void debug_data(); void debug(); void conversions(); + void serialization(); }; void tst_qgeoshape::testArea() @@ -126,5 +129,73 @@ void tst_qgeoshape::conversions() QVERIFY(varCircle.canConvert()); } +void tst_qgeoshape::serialization() +{ + QByteArray data; + // empty shape + { + QGeoShape shape; + QDataStream writeStream(&data, QDataStream::WriteOnly); + writeStream << shape; + + QGeoShape otherShape; + QDataStream readStream(&data, QDataStream::ReadOnly); + readStream >> otherShape; + + QCOMPARE(otherShape, shape); + } + // circle + { + QGeoCircle circle(QGeoCoordinate(1.1, 2.2), 10.5); + QDataStream writeStream(&data, QDataStream::WriteOnly); + writeStream << circle; + + QGeoShape otherCircle; + QDataStream readStream(&data, QDataStream::ReadOnly); + readStream >> otherCircle; + + QCOMPARE(otherCircle, circle); + } + // rectangle + { + QGeoRectangle rectangle(QGeoCoordinate(30, 160), QGeoCoordinate(-30, 170)); + QDataStream writeStream(&data, QDataStream::WriteOnly); + writeStream << rectangle; + + QGeoShape otherRectangle; + QDataStream readStream(&data, QDataStream::ReadOnly); + readStream >> otherRectangle; + + QCOMPARE(otherRectangle, rectangle); + } + // polygon + { + QGeoPolygon polygon({ QGeoCoordinate(30, 160), + QGeoCoordinate(0, 170), + QGeoCoordinate(-30, 160) }); + QDataStream writeStream(&data, QDataStream::WriteOnly); + writeStream << polygon; + + QGeoShape otherPolygon; + QDataStream readStream(&data, QDataStream::ReadOnly); + readStream >> otherPolygon; + + QCOMPARE(otherPolygon, polygon); + } + // path + { + QGeoPath path({ QGeoCoordinate(30, 160), QGeoCoordinate(0, 170), + QGeoCoordinate(-30, 180) }, 0.5); + QDataStream writeStream(&data, QDataStream::WriteOnly); + writeStream << path; + + QGeoShape otherPath; + QDataStream readStream(&data, QDataStream::ReadOnly); + readStream >> otherPath; + + QCOMPARE(otherPath, path); + } +} + QTEST_MAIN(tst_qgeoshape) #include "tst_qgeoshape.moc" -- cgit v1.2.1