Add a test for the various ways of setting a date on datefieldbackend

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-09-13 00:59:57 +08:00 committed by Matthieu Gallien
parent e254dfc67b
commit b642c2e1f6

View file

@ -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)