QtProtobufQtTypesQtCoreTest::qDateTime(LocalTime) fails

LocalTime test-case is refactored.
Idea of particular test is serialize/de-serialize object and verify
with expected constant result.
But in case LocalTime is used for QDateTime creation, the resulting
QDateTime object will always be different depending on system time zone
of concrete user.
So it makes sense to preset timezone before LocalTime object creation,
and returned it back after.

Fixes: QTBUG-113342
Change-Id: Ic4dfd033ff00d525225669fe771de6fb386d1b0b
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
Tatiana Borisova 2023-05-03 17:23:13 +03:00
parent 618d6c8aa1
commit 6e64776d47
2 changed files with 37 additions and 5 deletions

View File

@ -11,6 +11,7 @@ qt_internal_add_test(tst_protobuf_qtcoretypes
Qt::Protobuf Qt::Protobuf
Qt::ProtobufQtCoreTypes Qt::ProtobufQtCoreTypes
Qt::Core Qt::Core
Qt::CorePrivate
DEFINES DEFINES
PROTOC_EXECUTABLE="$<TARGET_FILE:WrapProtoc::WrapProtoc>" PROTOC_EXECUTABLE="$<TARGET_FILE:WrapProtoc::WrapProtoc>"
) )

View File

@ -9,6 +9,7 @@
#include <QObject> #include <QObject>
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <private/qtenvironmentvariables_p.h>
constexpr char conversionErrorMessage[] = "Qt Proto Type conversion error."; constexpr char conversionErrorMessage[] = "Qt Proto Type conversion error.";
@ -40,6 +41,29 @@ private slots:
private: private:
QProtobufSerializer serializer; QProtobufSerializer serializer;
#if QT_CONFIG(timezone)
class TimeZoneRollback
{
const QByteArray prior;
public:
explicit TimeZoneRollback(const QByteArray &zone) : prior(qgetenv("TZ"))
{ reset(zone); }
void reset(const QByteArray &zone)
{
qputenv("TZ", zone);
qTzSet();
}
~TimeZoneRollback()
{
if (prior.isNull())
qunsetenv("TZ");
else
qputenv("TZ", prior);
qTzSet();
}
};
#endif // timezone
}; };
void QtProtobufQtTypesQtCoreTest::initTestCase() void QtProtobufQtTypesQtCoreTest::initTestCase()
@ -204,17 +228,17 @@ void QtProtobufQtTypesQtCoreTest::qDateTime_data()
<< QByteArray("0a1b0891ddef89eb25121212104175737472616c69612f44617277696e") << QByteArray("0a1b0891ddef89eb25121212104175737472616c69612f44617277696e")
<< true; << true;
} }
QTest::addRow("LocalTime")
<< QTimeZone(QTimeZone(QTimeZone::LocalTime))
<< QDate(2011, 3, 14)
<< QByteArray("0a0b08d190979aeb2512021800")
<< true;
#endif //QT_CONFIG(timezone) #endif //QT_CONFIG(timezone)
QTest::addRow("UTC") QTest::addRow("UTC")
<< QTimeZone(QTimeZone::UTC) << QTimeZone(QTimeZone::UTC)
<< QDate(2011, 3, 14) << QDate(2011, 3, 14)
<< QByteArray("0a0b08d190979aeb2512021801") << QByteArray("0a0b08d190979aeb2512021801")
<< true; << true;
QTest::addRow("LocalTime")
<< QTimeZone(QTimeZone::LocalTime)
<< QDate(2011, 3, 14)
<< QByteArray("0a0b08d190979aeb2512021800")
<< true;
QTest::addRow("OffsetFromUTC") QTest::addRow("OffsetFromUTC")
<< QTimeZone::fromSecondsAheadOfUtc(3141) << QTimeZone::fromSecondsAheadOfUtc(3141)
<< QDate(2011, 3, 14) << QDate(2011, 3, 14)
@ -225,6 +249,13 @@ void QtProtobufQtTypesQtCoreTest::qDateTime_data()
void QtProtobufQtTypesQtCoreTest::qDateTime() void QtProtobufQtTypesQtCoreTest::qDateTime()
{ {
#if QT_CONFIG(timezone)
#ifdef Q_OS_WIN
TimeZoneRollback useZone("GMT Standard Time");
#else
TimeZoneRollback useZone("GMT");
#endif //Q_OS_WIN
#endif //QT_CONFIG(timezone)
QFETCH(const QTimeZone, zone); QFETCH(const QTimeZone, zone);
QFETCH(const QDate, date); QFETCH(const QDate, date);
QFETCH(const QByteArray, zoneHex); QFETCH(const QByteArray, zoneHex);