File indexing completed on 2024-04-28 15:31:54
0001 /* 0002 SPDX-FileCopyrightText: 2011 John Layt <john@layt.net> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "ktimecomboboxtest.h" 0008 0009 #include <QTime> 0010 0011 #include <QLineEdit> 0012 #include <QTest> 0013 #include <ktimecombobox.h> 0014 0015 QTEST_MAIN(KTimeComboBoxTest) 0016 0017 KTimeComboBoxTest::KTimeComboBoxTest() 0018 { 0019 QLocale::setDefault(QLocale(QLocale::C)); 0020 } 0021 0022 void KTimeComboBoxTest::testDefaults() 0023 { 0024 m_combo = new KTimeComboBox(); 0025 QCOMPARE(m_combo->time(), QTime(0, 0, 0, 0)); 0026 QCOMPARE(m_combo->minimumTime(), QTime(0, 0, 0, 0)); 0027 QCOMPARE(m_combo->maximumTime(), QTime(23, 59, 59, 999)); 0028 QCOMPARE(m_combo->isValid(), true); 0029 QCOMPARE(m_combo->options(), KTimeComboBox::EditTime | KTimeComboBox::SelectTime); 0030 QCOMPARE(m_combo->timeListInterval(), 15); 0031 QCOMPARE(m_combo->displayFormat(), QLocale::ShortFormat); 0032 delete m_combo; 0033 } 0034 0035 void KTimeComboBoxTest::testValidNull() 0036 { 0037 m_combo = new KTimeComboBox(nullptr); 0038 QCOMPARE(m_combo->isValid(), true); 0039 QCOMPARE(m_combo->isNull(), false); 0040 m_combo->setTime(QTime()); 0041 QCOMPARE(m_combo->isValid(), false); 0042 QCOMPARE(m_combo->isNull(), true); 0043 m_combo->setTime(QTime(0, 0, 0)); 0044 m_combo->lineEdit()->setText(QStringLiteral("99:99")); 0045 QCOMPARE(m_combo->isValid(), false); 0046 QCOMPARE(m_combo->isNull(), false); 0047 delete m_combo; 0048 } 0049 0050 void KTimeComboBoxTest::testTimeRange() 0051 { 0052 m_combo = new KTimeComboBox(); 0053 m_combo->setTime(QTime(2, 0, 0, 0)); 0054 QCOMPARE(m_combo->minimumTime(), QTime(0, 0, 0, 0)); 0055 QCOMPARE(m_combo->maximumTime(), QTime(23, 59, 59, 999)); 0056 QCOMPARE(m_combo->isValid(), true); 0057 0058 m_combo->setTimeRange(QTime(3, 0, 0, 0), QTime(22, 0, 0, 0)); 0059 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0, 0)); 0060 QCOMPARE(m_combo->maximumTime(), QTime(22, 0, 0, 0)); 0061 QCOMPARE(m_combo->isValid(), false); 0062 0063 m_combo->setTime(QTime(23, 0, 0, 0)); 0064 QCOMPARE(m_combo->isValid(), false); 0065 m_combo->setTime(QTime(12, 0, 0, 0)); 0066 QCOMPARE(m_combo->isValid(), true); 0067 m_combo->setTime(QTime(3, 0, 0, 0)); 0068 QCOMPARE(m_combo->isValid(), true); 0069 m_combo->setTime(QTime(22, 0, 0, 0)); 0070 QCOMPARE(m_combo->isValid(), true); 0071 m_combo->setTime(QTime(2, 59, 59, 999)); 0072 QCOMPARE(m_combo->isValid(), false); 0073 m_combo->setTime(QTime(22, 1, 0, 0)); 0074 QCOMPARE(m_combo->isValid(), false); 0075 0076 m_combo->setTimeRange(QTime(15, 0, 0, 0), QTime(5, 0, 0, 0)); 0077 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0, 0)); 0078 QCOMPARE(m_combo->maximumTime(), QTime(22, 0, 0, 0)); 0079 0080 m_combo->setMinimumTime(QTime(2, 0, 0, 0)); 0081 QCOMPARE(m_combo->minimumTime(), QTime(2, 0, 0, 0)); 0082 QCOMPARE(m_combo->maximumTime(), QTime(22, 0, 0, 0)); 0083 0084 m_combo->setMaximumTime(QTime(21, 0, 0, 0)); 0085 QCOMPARE(m_combo->minimumTime(), QTime(2, 0, 0, 0)); 0086 QCOMPARE(m_combo->maximumTime(), QTime(21, 0, 0, 0)); 0087 0088 delete m_combo; 0089 } 0090 0091 void KTimeComboBoxTest::testTimeListInterval() 0092 { 0093 m_combo = new KTimeComboBox(); 0094 QCOMPARE(m_combo->timeListInterval(), 15); 0095 m_combo->setTimeListInterval(60); 0096 QCOMPARE(m_combo->timeListInterval(), 60); 0097 m_combo->setTimeListInterval(7); 0098 QCOMPARE(m_combo->timeListInterval(), 60); 0099 m_combo->setTimeListInterval(720); 0100 QCOMPARE(m_combo->timeListInterval(), 720); 0101 QList<QTime> list; 0102 list << QTime(0, 0, 0) << QTime(12, 0, 0) << QTime(23, 59, 59, 999); 0103 QCOMPARE(m_combo->timeList(), list); 0104 m_combo->setTimeRange(QTime(4, 0, 0, 0), QTime(5, 0, 0, 0)); 0105 m_combo->setTimeListInterval(30); 0106 list.clear(); 0107 list << QTime(4, 0, 0) << QTime(4, 30, 0) << QTime(5, 0, 0, 0); 0108 QCOMPARE(m_combo->timeList(), list); 0109 m_combo->setTimeRange(QTime(4, 0, 0, 0), QTime(4, 59, 0, 0)); 0110 m_combo->setTimeListInterval(30); 0111 list.clear(); 0112 list << QTime(4, 0, 0) << QTime(4, 30, 0) << QTime(4, 59, 0, 0); 0113 QCOMPARE(m_combo->timeList(), list); 0114 delete m_combo; 0115 } 0116 0117 void KTimeComboBoxTest::testTimeList() 0118 { 0119 m_combo = new KTimeComboBox(); 0120 QList<QTime> list; 0121 0122 // Test default list 0123 QTime thisTime = QTime(0, 0, 0); 0124 for (int i = 0; i < 1440; i = i + 15) { 0125 list << thisTime.addSecs(i * 60); 0126 } 0127 list << QTime(23, 59, 59, 999); 0128 QCOMPARE(m_combo->timeList(), list); 0129 0130 // Test basic list 0131 list.clear(); 0132 list << QTime(3, 0, 0) << QTime(15, 16, 17); 0133 m_combo->setTimeList(list); 0134 QCOMPARE(m_combo->timeList(), list); 0135 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0)); 0136 QCOMPARE(m_combo->maximumTime(), QTime(15, 16, 17)); 0137 0138 // Test sort input times 0139 list.clear(); 0140 list << QTime(17, 16, 15) << QTime(4, 0, 0); 0141 m_combo->setTimeList(list); 0142 std::sort(list.begin(), list.end()); 0143 QCOMPARE(m_combo->timeList(), list); 0144 QCOMPARE(m_combo->minimumTime(), QTime(4, 0, 0)); 0145 QCOMPARE(m_combo->maximumTime(), QTime(17, 16, 15)); 0146 0147 // Test ignore null QTime 0148 list.clear(); 0149 list << QTime(3, 0, 0) << QTime(15, 16, 17) << QTime(); 0150 m_combo->setTimeList(list); 0151 list.takeLast(); 0152 QCOMPARE(m_combo->timeList(), list); 0153 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0)); 0154 QCOMPARE(m_combo->maximumTime(), QTime(15, 16, 17)); 0155 delete m_combo; 0156 } 0157 0158 void KTimeComboBoxTest::testOptions() 0159 { 0160 m_combo = new KTimeComboBox(); 0161 KTimeComboBox::Options options = KTimeComboBox::EditTime | KTimeComboBox::SelectTime; 0162 QCOMPARE(m_combo->options(), options); 0163 options = KTimeComboBox::EditTime | KTimeComboBox::WarnOnInvalid; 0164 m_combo->setOptions(options); 0165 QCOMPARE(m_combo->options(), options); 0166 delete m_combo; 0167 } 0168 0169 void KTimeComboBoxTest::testDisplayFormat() 0170 { 0171 m_combo = new KTimeComboBox(); 0172 QLocale::FormatType format = QLocale::ShortFormat; 0173 QCOMPARE(m_combo->displayFormat(), format); 0174 format = QLocale::NarrowFormat; 0175 m_combo->setDisplayFormat(format); 0176 QCOMPARE(m_combo->displayFormat(), format); 0177 delete m_combo; 0178 } 0179 0180 void KTimeComboBoxTest::testMask() 0181 { 0182 // Store the current locale, and set to one which reproduces the bug. 0183 QLocale currentLocale; 0184 0185 // Test that the line edit input mask AM/PM portion gets correctly 0186 // replaced with xx. 0187 QLocale::setDefault(QLocale(QLocale::English, QLocale::Australia)); 0188 m_combo = new KTimeComboBox(nullptr); 0189 QString mask = m_combo->lineEdit()->inputMask(); 0190 QVERIFY(mask.contains(QLatin1String("xx"))); 0191 delete m_combo; 0192 0193 // For 24 hour time formats, no am/pm specifier in mask 0194 QLocale::setDefault(QLocale(QLocale::NorwegianBokmal, QLocale::Norway)); 0195 m_combo = new KTimeComboBox(nullptr); 0196 mask = m_combo->lineEdit()->inputMask(); 0197 QVERIFY(!mask.contains(QLatin1String("xx"))); 0198 delete m_combo; 0199 0200 // Restore the previous locale 0201 QLocale::setDefault(currentLocale); 0202 } 0203 0204 void KTimeComboBoxTest::testEdit_data() 0205 { 0206 QTest::addColumn<QString>("locale"); 0207 0208 QTest::newRow("C") << QStringLiteral("C"); 0209 QTest::newRow("el_GR") << QStringLiteral("el_GR"); // bug 361764; non-ASCII AM/PM 0210 QTest::newRow("en_AU") << QStringLiteral("en_AU"); // bug 361764 0211 QTest::newRow("en_CA") << QStringLiteral("en_CA"); // bug 405857 0212 QTest::newRow("en_IE") << QStringLiteral("en_IE"); // bug 361764 0213 QTest::newRow("en_US") << QStringLiteral("en_US"); 0214 QTest::newRow("fr_CA") << QStringLiteral("fr_CA"); // bug 409912 0215 QTest::newRow("ko_KR") << QStringLiteral("ko_KR"); // non-ASCII AM/PM 0216 } 0217 0218 static const QTime MIDNIGHT = QTime(00, 00, 00); 0219 static const QTime END_AM = QTime(11, 59, 00); 0220 static const QTime NOON = QTime(12, 00, 00); 0221 static const QTime END_PM = QTime(23, 59, 00); 0222 0223 // Test that the widget can process times using the current locale and edit mask. 0224 // See https://bugs.kde.org/show_bug.cgi?id=409867 0225 void KTimeComboBoxTest::testEdit() 0226 { 0227 QLocale currentLocale; 0228 0229 QFETCH(QString, locale); 0230 0231 QLocale::setDefault(QLocale(locale)); 0232 m_combo = new KTimeComboBox(nullptr); 0233 0234 m_combo->setTime(MIDNIGHT); 0235 QCOMPARE(m_combo->time(), MIDNIGHT); 0236 m_combo->setTime(END_AM); 0237 QCOMPARE(m_combo->time(), END_AM); 0238 m_combo->setTime(NOON); 0239 QCOMPARE(m_combo->time(), NOON); 0240 m_combo->setTime(END_PM); 0241 QCOMPARE(m_combo->time(), END_PM); 0242 0243 delete m_combo; 0244 0245 QLocale::setDefault(currentLocale); 0246 } 0247 0248 #include "moc_ktimecomboboxtest.cpp"