From 4e5d8d4a7c440c95b9eeffbec037ffc9a556b068 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 13 Sep 2023 00:59:57 +0800 Subject: [PATCH] Add a test for the various ways of setting a date on datefieldbackend Signed-off-by: Claudio Cambra --- test/testdatefieldbackend.cpp | 49 +++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/test/testdatefieldbackend.cpp b/test/testdatefieldbackend.cpp index 776c61e1032da..c80aa3a2db2e4 100644 --- a/test/testdatefieldbackend.cpp +++ b/test/testdatefieldbackend.cpp @@ -23,11 +23,12 @@ class TestDateFieldBackend : public QObject { Q_OBJECT +private: + static constexpr auto dateStringFormat = "dd/MM/yyyy"; + private slots: void testDefaultBehaviour() { - constexpr auto dateStringFormat = "dd/MM/yyyy"; - Quick::DateFieldBackend backend; backend._dateFormat = dateStringFormat; @@ -101,6 +102,50 @@ private slots: QVERIFY(!backend.validDate()); QCOMPARE(validDateChangedSpy.count(), 8); } + + void testDateSettingMethods() + { + Quick::DateFieldBackend backend; + backend._dateFormat = dateStringFormat; + + QSignalSpy dateChangedSpy(&backend, &Quick::DateFieldBackend::dateChanged); + QSignalSpy dateMsecsChangedSpy(&backend, &Quick::DateFieldBackend::dateMsecsChanged); + QSignalSpy dateStringChangedSpy(&backend, &Quick::DateFieldBackend::dateStringChanged); + + const auto testDate = QDate::currentDate().addDays(800); + const auto testDateMsecs = testDate.startOfDay(Qt::UTC).toMSecsSinceEpoch(); + const auto testDateString = testDate.toString(dateStringFormat); + + backend.setDate(testDate); + QCOMPARE(backend.date(), testDate); + QCOMPARE(dateChangedSpy.count(), 1); + QCOMPARE(dateMsecsChangedSpy.count(), 1); + QCOMPARE(dateStringChangedSpy.count(), 1); + + backend.setDate({}); + QVERIFY(backend.date() != testDate); + QCOMPARE(dateChangedSpy.count(), 2); + QCOMPARE(dateMsecsChangedSpy.count(), 2); + QCOMPARE(dateStringChangedSpy.count(), 2); + + backend.setDateMsecs(testDateMsecs); + QCOMPARE(backend.date(), testDate); + QCOMPARE(dateChangedSpy.count(), 3); + QCOMPARE(dateMsecsChangedSpy.count(), 3); + QCOMPARE(dateStringChangedSpy.count(), 3); + + backend.setDate({}); + QVERIFY(backend.date() != testDate); + QCOMPARE(dateChangedSpy.count(), 4); + QCOMPARE(dateMsecsChangedSpy.count(), 4); + QCOMPARE(dateStringChangedSpy.count(), 4); + + backend.setDateString(testDateString); + QCOMPARE(backend.date(), testDate); + QCOMPARE(dateChangedSpy.count(), 5); + QCOMPARE(dateMsecsChangedSpy.count(), 5); + QCOMPARE(dateStringChangedSpy.count(), 5); + } }; QTEST_MAIN(TestDateFieldBackend)