| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2022 g10 Code GmbH |
| 3 | SPDX-FileContributor: Ingo Klöcker <[email protected]> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <KDatePicker> |
| 9 | |
| 10 | #include <QLineEdit> |
| 11 | #include <QSignalSpy> |
| 12 | #include <QTest> |
| 13 | |
| 14 | using namespace Qt::Literals; |
| 15 | |
| 16 | void initLocale() |
| 17 | { |
| 18 | #ifndef Q_OS_WIN |
| 19 | qputenv("LC_ALL", "en_US.utf-8"); |
| 20 | #else |
| 21 | QLocale::setDefault(QLocale(u"en_US"_s)); |
| 22 | #endif |
| 23 | } |
| 24 | Q_CONSTRUCTOR_FUNCTION(initLocale) |
| 25 | |
| 26 | class KDatePickerTest : public QObject |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | |
| 30 | private Q_SLOTS: |
| 31 | void testSubmittingDateInLineEditEmitsOriginalDate() |
| 32 | { |
| 33 | const QDate originalDate{2022, 5, 15}; |
| 34 | KDatePicker p{originalDate}; |
| 35 | QSignalSpy spyDateEntered(&p, &KDatePicker::dateEntered); |
| 36 | auto lineEdit = p.findChild<QLineEdit *>(); |
| 37 | QVERIFY(lineEdit); |
| 38 | |
| 39 | QTest::keyClick(lineEdit, Qt::Key_Return); |
| 40 | QCOMPARE(spyDateEntered.count(), 1); |
| 41 | QCOMPARE(spyDateEntered.first().at(0).toDate(), originalDate); |
| 42 | } |
| 43 | |
| 44 | void testLineEditShowsDateWith4DigitYear() |
| 45 | { |
| 46 | KDatePicker p; |
| 47 | auto lineEdit = p.findChild<QLineEdit *>(); |
| 48 | QVERIFY(lineEdit); |
| 49 | |
| 50 | p.setDate(QDate{2022, 5, 15}); |
| 51 | QCOMPARE(lineEdit->text(), QStringLiteral("5/15/2022")); |
| 52 | } |
| 53 | |
| 54 | void testPickerAcceptsDatesEnteredInMultipleFormats() |
| 55 | { |
| 56 | KDatePicker p; |
| 57 | QSignalSpy spyDateEntered(&p, &KDatePicker::dateEntered); |
| 58 | auto lineEdit = p.findChild<QLineEdit *>(); |
| 59 | QVERIFY(lineEdit); |
| 60 | |
| 61 | #ifndef Q_OS_WIN |
| 62 | { |
| 63 | // short date format |
| 64 | lineEdit->setText(QStringLiteral("5/15/22")); |
| 65 | QTest::keyClick(lineEdit, Qt::Key_Return); |
| 66 | QCOMPARE(spyDateEntered.count(), 1); |
| 67 | const auto emittedDate = spyDateEntered.first().at(0).toDate(); |
| 68 | const QDate expectedDate{2022, 5, 15}; |
| 69 | QCOMPARE(emittedDate.day(), expectedDate.day()); |
| 70 | QCOMPARE(emittedDate.month(), expectedDate.month()); |
| 71 | // we don't expect Qt to guess the correct century of a two digit year |
| 72 | QCOMPARE(emittedDate.year() % 100, expectedDate.year() % 100); |
| 73 | spyDateEntered.clear(); |
| 74 | } |
| 75 | #endif |
| 76 | { |
| 77 | // long date format |
| 78 | lineEdit->setText(QStringLiteral("Sunday, May 15, 2022")); |
| 79 | QTest::keyClick(lineEdit, Qt::Key_Return); |
| 80 | QCOMPARE(spyDateEntered.count(), 1); |
| 81 | const auto emittedDate = spyDateEntered.first().at(0).toDate(); |
| 82 | const QDate expectedDate{2022, 5, 15}; |
| 83 | QCOMPARE(emittedDate, expectedDate); |
| 84 | spyDateEntered.clear(); |
| 85 | } |
| 86 | { |
| 87 | // short date format with 4-digit year |
| 88 | lineEdit->setText(QStringLiteral("5/15/2022")); |
| 89 | QTest::keyClick(lineEdit, Qt::Key_Return); |
| 90 | QCOMPARE(spyDateEntered.count(), 1); |
| 91 | const auto emittedDate = spyDateEntered.first().at(0).toDate(); |
| 92 | const QDate expectedDate{2022, 5, 15}; |
| 93 | QCOMPARE(emittedDate, expectedDate); |
| 94 | spyDateEntered.clear(); |
| 95 | } |
| 96 | { |
| 97 | // ISO date format |
| 98 | lineEdit->setText(QStringLiteral("2022-05-15")); |
| 99 | QTest::keyClick(lineEdit, Qt::Key_Return); |
| 100 | QCOMPARE(spyDateEntered.count(), 1); |
| 101 | const auto emittedDate = spyDateEntered.first().at(0).toDate(); |
| 102 | const QDate expectedDate{2022, 5, 15}; |
| 103 | QCOMPARE(emittedDate, expectedDate); |
| 104 | spyDateEntered.clear(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void testPickerRejectsDateOutOfRange() |
| 109 | { |
| 110 | KDatePicker p; |
| 111 | p.setDate({2026, 4, 12}); |
| 112 | p.setDateRange({2026, 1, 1}, {2026, 12, 31}); |
| 113 | p.setDate({2025, 6, 1}); |
| 114 | QDate expectedDate{2026, 4, 12}; |
| 115 | QCOMPARE(p.date(), expectedDate); |
| 116 | } |
| 117 | |
| 118 | void testPickerRejectsKeysDateOutOfRange() |
| 119 | { |
| 120 | KDatePicker p; |
| 121 | p.setDate({2026, 4, 12}); |
| 122 | p.setDateRange({2026, 4, 12}, {2026, 4, 12}); |
| 123 | QTest::keyClick(p.findChild<QWidget *>(QLatin1String("table")), Qt::Key_Down); |
| 124 | QDate expectedDate{2026, 4, 12}; |
| 125 | QCOMPARE(p.date(), expectedDate); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | QTEST_MAIN(KDatePickerTest) |
| 130 | |
| 131 | #include "kdatepickerautotest.moc" |