From d491268ca8b354d028d71767df5857d8a986b7ae Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 13 Sep 2023 00:03:43 +0800 Subject: [PATCH] Add test for date field backend boundaries Signed-off-by: Claudio Cambra --- test/testdatefieldbackend.cpp | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/testdatefieldbackend.cpp b/test/testdatefieldbackend.cpp index eb1480207516b..918d633dec6a7 100644 --- a/test/testdatefieldbackend.cpp +++ b/test/testdatefieldbackend.cpp @@ -39,6 +39,47 @@ private slots: QCOMPARE(backend.dateMsecs(), currentDateMSecs); QCOMPARE(backend.dateString(), currentDateString); } + + void testDateBoundaries() + { + Quick::DateFieldBackend backend; + const auto minDate = QDate::currentDate().addDays(-5); + const auto maxDate = QDate::currentDate().addDays(5); + const auto minDateMs = minDate.startOfDay(Qt::UTC).toMSecsSinceEpoch(); + const auto maxDateMs = maxDate.startOfDay(Qt::UTC).toMSecsSinceEpoch(); + const auto invalidMinDate = minDate.addDays(-1); + const auto invalidMaxDate = maxDate.addDays(1); + + // Set by QDate + backend.setMinimumDate(minDate); + backend.setMaximumDate(maxDate); + + QCOMPARE(backend.minimumDate(), minDate); + QCOMPARE(backend.maximumDate(), maxDate); + QCOMPARE(backend.minimumDateMsecs(), minDateMs); + QCOMPARE(backend.maximumDateMsecs(), maxDateMs); + + // Reset and try when setting by MSecs + backend.setMinimumDate({}); + backend.setMaximumDate({}); + backend.setMinimumDateMsecs(minDateMs); + backend.setMaximumDateMsecs(maxDateMs); + + QCOMPARE(backend.minimumDate(), minDate); + QCOMPARE(backend.maximumDate(), maxDate); + QCOMPARE(backend.minimumDateMsecs(), minDateMs); + QCOMPARE(backend.maximumDateMsecs(), maxDateMs); + + // Since we default to the current date, the date should be valid + QVERIFY(backend.validDate()); + + // Now try with invalid dates + backend.setDate(invalidMinDate); + QVERIFY(!backend.validDate()); + + backend.setDate(invalidMaxDate); + QVERIFY(!backend.validDate()); + } }; QTEST_MAIN(TestDateFieldBackend)