| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2011 John Layt <[email protected]> |
| 3 | SPDX-FileCopyrightText: 2022 g10 Code GmbH |
| 4 | SPDX-FileContributor: Ingo Klöcker <[email protected]> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "kdatecomboboxtest.h" |
| 10 | |
| 11 | #include <QDate> |
| 12 | |
| 13 | #include "kdatecombobox.h" |
| 14 | #include "kdatepickerpopup.h" |
| 15 | |
| 16 | #include <QLineEdit> |
| 17 | #include <QSignalSpy> |
| 18 | #include <QTest> |
| 19 | |
| 20 | QTEST_MAIN(KDateComboBoxTest) |
| 21 | |
| 22 | using namespace Qt::Literals; |
| 23 | |
| 24 | void initLocale() |
| 25 | { |
| 26 | #ifndef Q_OS_WIN |
| 27 | qputenv("LC_ALL", "en_US.utf-8"); |
| 28 | #else |
| 29 | QLocale::setDefault(QLocale(u"en_US"_s)); |
| 30 | #endif |
| 31 | } |
| 32 | |
| 33 | Q_CONSTRUCTOR_FUNCTION(initLocale) |
| 34 | |
| 35 | void KDateComboBoxTest::testDefaults() |
| 36 | { |
| 37 | KDateComboBox combo; |
| 38 | QCOMPARE(combo.date(), QDate::currentDate()); |
| 39 | // Missing support in QLocale; |
| 40 | // QCOMPARE(m_combo.minimumDate(), KLocale::global()->calendar()->earliestValidDate()); |
| 41 | // QCOMPARE(m_combo.maximumDate(), KLocale::global()->calendar()->latestValidDate()); |
| 42 | QCOMPARE(combo.isValid(), true); |
| 43 | QCOMPARE(combo.isNull(), false); |
| 44 | QCOMPARE(combo.options(), KDateComboBox::EditDate | KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::DateKeywords); |
| 45 | QCOMPARE(combo.displayFormat(), QLocale::ShortFormat); |
| 46 | } |
| 47 | |
| 48 | void KDateComboBoxTest::testValidNull() |
| 49 | { |
| 50 | KDateComboBox combo; |
| 51 | QCOMPARE(combo.isValid(), true); |
| 52 | QCOMPARE(combo.isNull(), false); |
| 53 | combo.setDate(QDate()); |
| 54 | QCOMPARE(combo.isValid(), false); |
| 55 | QCOMPARE(combo.isNull(), true); |
| 56 | combo.setDate(QDate(2000, 1, 1)); |
| 57 | combo.lineEdit()->setText(QStringLiteral("invalid")); |
| 58 | QCOMPARE(combo.isValid(), false); |
| 59 | QCOMPARE(combo.isNull(), false); |
| 60 | } |
| 61 | |
| 62 | void KDateComboBoxTest::testDateRange() |
| 63 | { |
| 64 | KDateComboBox combo; |
| 65 | combo.setDate(QDate(2000, 1, 1)); |
| 66 | // Missing support in QLocale; |
| 67 | // QCOMPARE(m_combo.minimumDate(), KLocale::global()->calendar()->earliestValidDate()); |
| 68 | // QCOMPARE(m_combo.maximumDate(), KLocale::global()->calendar()->latestValidDate()); |
| 69 | QCOMPARE(combo.isValid(), true); |
| 70 | |
| 71 | combo.setDateRange(QDate(2001, 1, 1), QDate(2002, 1, 1)); |
| 72 | QCOMPARE(combo.minimumDate(), QDate(2001, 1, 1)); |
| 73 | QCOMPARE(combo.maximumDate(), QDate(2002, 1, 1)); |
| 74 | QCOMPARE(combo.isValid(), false); |
| 75 | |
| 76 | combo.setDate(QDate(2003, 1, 1)); |
| 77 | QCOMPARE(combo.isValid(), false); |
| 78 | combo.setDate(QDate(2001, 6, 1)); |
| 79 | QCOMPARE(combo.isValid(), true); |
| 80 | combo.setDate(QDate(2001, 1, 1)); |
| 81 | QCOMPARE(combo.isValid(), true); |
| 82 | combo.setDate(QDate(2002, 1, 1)); |
| 83 | QCOMPARE(combo.isValid(), true); |
| 84 | combo.setDate(QDate(2000, 12, 31)); |
| 85 | QCOMPARE(combo.isValid(), false); |
| 86 | combo.setDate(QDate(2002, 1, 2)); |
| 87 | QCOMPARE(combo.isValid(), false); |
| 88 | |
| 89 | combo.setDateRange(QDate(1995, 1, 1), QDate(1990, 1, 1)); |
| 90 | QCOMPARE(combo.minimumDate(), QDate(2001, 1, 1)); |
| 91 | QCOMPARE(combo.maximumDate(), QDate(2002, 1, 1)); |
| 92 | |
| 93 | combo.setMinimumDate(QDate(2000, 1, 1)); |
| 94 | QCOMPARE(combo.minimumDate(), QDate(2000, 1, 1)); |
| 95 | QCOMPARE(combo.maximumDate(), QDate(2002, 1, 1)); |
| 96 | |
| 97 | combo.setMaximumDate(QDate(2003, 1, 1)); |
| 98 | QCOMPARE(combo.minimumDate(), QDate(2000, 1, 1)); |
| 99 | QCOMPARE(combo.maximumDate(), QDate(2003, 1, 1)); |
| 100 | |
| 101 | combo.resetDateRange(); |
| 102 | QVERIFY(!combo.minimumDate().isValid()); |
| 103 | QVERIFY(!combo.maximumDate().isValid()); |
| 104 | |
| 105 | // Check functioning when the minimum or maximum date is not already set |
| 106 | |
| 107 | combo.setMinimumDate(QDate(2000, 1, 1)); |
| 108 | QCOMPARE(combo.minimumDate(), QDate(2000, 1, 1)); |
| 109 | QVERIFY(!combo.maximumDate().isValid()); |
| 110 | |
| 111 | combo.resetMinimumDate(); |
| 112 | QVERIFY(!combo.minimumDate().isValid()); |
| 113 | QVERIFY(!combo.maximumDate().isValid()); |
| 114 | |
| 115 | combo.setMaximumDate(QDate(2003, 1, 1)); |
| 116 | QVERIFY(!combo.minimumDate().isValid()); |
| 117 | QCOMPARE(combo.maximumDate(), QDate(2003, 1, 1)); |
| 118 | |
| 119 | combo.resetMaximumDate(); |
| 120 | QVERIFY(!combo.minimumDate().isValid()); |
| 121 | QVERIFY(!combo.maximumDate().isValid()); |
| 122 | } |
| 123 | |
| 124 | void KDateComboBoxTest::testDateList() |
| 125 | { |
| 126 | KDateComboBox combo; |
| 127 | QMap<QDate, QString> map; |
| 128 | |
| 129 | // Test default map |
| 130 | QCOMPARE(combo.dateMap(), map); |
| 131 | |
| 132 | // Test basic map |
| 133 | map.clear(); |
| 134 | map.insert(QDate(2000, 1, 1), QStringLiteral("New Years Day")); |
| 135 | map.insert(QDate(2000, 1, 2), QString()); |
| 136 | map.insert(QDate(2000, 1, 3), QStringLiteral("separator")); |
| 137 | map.insert(QDate(), QStringLiteral("No Date")); |
| 138 | combo.setDateMap(map); |
| 139 | QCOMPARE(combo.dateMap(), map); |
| 140 | } |
| 141 | |
| 142 | void KDateComboBoxTest::testOptions() |
| 143 | { |
| 144 | KDateComboBox combo; |
| 145 | auto datePicker = combo.findChild<KDatePickerPopup *>(); |
| 146 | QVERIFY(datePicker); |
| 147 | KDateComboBox::Options options = KDateComboBox::EditDate | KDateComboBox::SelectDate | KDateComboBox::DatePicker | KDateComboBox::DateKeywords; |
| 148 | QCOMPARE(combo.options(), options); |
| 149 | QCOMPARE(datePicker->modes(), KDatePickerPopup::DatePicker | KDatePickerPopup::Words); |
| 150 | options = KDateComboBox::EditDate | KDateComboBox::WarnOnInvalid; |
| 151 | combo.setOptions(options); |
| 152 | QCOMPARE(combo.options(), options); |
| 153 | QCOMPARE(datePicker->modes(), 0); |
| 154 | } |
| 155 | |
| 156 | void KDateComboBoxTest::testDisplayFormat() |
| 157 | { |
| 158 | KDateComboBox combo; |
| 159 | QLocale::FormatType format = QLocale::ShortFormat; |
| 160 | QCOMPARE(combo.displayFormat(), format); |
| 161 | format = QLocale::NarrowFormat; |
| 162 | combo.setDisplayFormat(format); |
| 163 | QCOMPARE(combo.displayFormat(), format); |
| 164 | } |
| 165 | |
| 166 | void KDateComboBoxTest::testSignals() |
| 167 | { |
| 168 | // GIVEN |
| 169 | KDateComboBox combo; |
| 170 | QSignalSpy spyDateEntered(&combo, &KDateComboBox::dateEntered); |
| 171 | QSignalSpy spyDateEdited(&combo, &KDateComboBox::dateEdited); |
| 172 | QSignalSpy spyDateChanged(&combo, &KDateComboBox::dateChanged); |
| 173 | auto clearSpies = [&]() { |
| 174 | spyDateEntered.clear(); |
| 175 | spyDateEdited.clear(); |
| 176 | spyDateChanged.clear(); |
| 177 | }; |
| 178 | |
| 179 | // WHEN setting a date programmatically |
| 180 | combo.setDate(QDate(2016, 05, 30)); |
| 181 | |
| 182 | // THEN |
| 183 | QCOMPARE(spyDateEntered.count(), 0); |
| 184 | QCOMPARE(spyDateEdited.count(), 0); |
| 185 | QCOMPARE(spyDateChanged.count(), 1); |
| 186 | clearSpies(); |
| 187 | |
| 188 | combo.show(); |
| 189 | combo.activateWindow(); |
| 190 | QVERIFY(QTest::qWaitForWindowActive(&combo)); |
| 191 | QCOMPARE(combo.currentText(), QStringLiteral("5/30/2016")); |
| 192 | QCOMPARE(combo.date(), QDate(2016, 5, 30)); |
| 193 | |
| 194 | // WHEN typing a new day (Home, Del, '4' -> 30 April 2016) |
| 195 | QTest::keyClick(&combo, Qt::Key_Home); |
| 196 | QTest::keyClick(&combo, Qt::Key_Delete); |
| 197 | QTest::keyClick(&combo, Qt::Key_4); |
| 198 | |
| 199 | // THEN |
| 200 | QCOMPARE(combo.currentText(), QStringLiteral("4/30/2016")); |
| 201 | QCOMPARE(combo.date(), QDate(2016, 4, 30)); |
| 202 | |
| 203 | // and losing focus |
| 204 | combo.clearFocus(); |
| 205 | |
| 206 | // THEN |
| 207 | QCOMPARE(spyDateEdited.count(), 2); |
| 208 | QCOMPARE(spyDateEntered.count(), 1); |
| 209 | QCOMPARE(spyDateChanged.count(), 1); |
| 210 | clearSpies(); |
| 211 | |
| 212 | // WHEN typing Key_Up |
| 213 | QTest::keyClick(&combo, Qt::Key_Up); |
| 214 | |
| 215 | // THEN |
| 216 | QCOMPARE(combo.currentText(), QStringLiteral("5/1/2016")); |
| 217 | QCOMPARE(combo.date(), QDate(2016, 5, 1)); |
| 218 | QCOMPARE(spyDateEdited.count(), 0); |
| 219 | QCOMPARE(spyDateEntered.count(), 1); |
| 220 | QCOMPARE(spyDateChanged.count(), 1); |
| 221 | clearSpies(); |
| 222 | |
| 223 | // WHEN typing a digit ('6' -> 1 june 2016) then Return |
| 224 | QTest::keyClick(&combo, Qt::Key_Home); |
| 225 | QTest::keyClick(&combo, Qt::Key_Delete); |
| 226 | QTest::keyClick(&combo, Qt::Key_6); |
| 227 | QTest::keyClick(&combo, Qt::Key_Return); |
| 228 | |
| 229 | // THEN |
| 230 | QCOMPARE(combo.currentText(), QStringLiteral("6/1/2016")); |
| 231 | QCOMPARE(combo.date(), QDate(2016, 6, 1)); |
| 232 | QCOMPARE(spyDateEdited.count(), 2); |
| 233 | QCOMPARE(spyDateEntered.count(), 1); |
| 234 | QCOMPARE(spyDateChanged.count(), 1); |
| 235 | clearSpies(); |
| 236 | } |
| 237 | |
| 238 | void KDateComboBoxTest::testDatePickerIntegration() |
| 239 | { |
| 240 | // GIVEN |
| 241 | KDateComboBox combo; |
| 242 | const QDate minimumDate{2001, 1, 1}; |
| 243 | const QDate maximumDate{2106, 1, 1}; |
| 244 | combo.setDateRange(minimumDate, maximumDate); |
| 245 | const QDate originalDate{2021, 6, 23}; |
| 246 | combo.setDate(originalDate); |
| 247 | |
| 248 | // WHEN the date picker emits dateChanged signal with an invalid date |
| 249 | auto datePicker = combo.findChild<KDatePickerPopup *>(); |
| 250 | QVERIFY(datePicker); |
| 251 | const QDate dateOutsideTheAllowedRange{1921, 6, 23}; |
| 252 | Q_EMIT datePicker->dateChanged(dateOutsideTheAllowedRange); |
| 253 | |
| 254 | // THEN the combo still has the original date |
| 255 | QCOMPARE(combo.date(), originalDate); |
| 256 | } |
| 257 | |
| 258 | #include "moc_kdatecomboboxtest.cpp" |