Debian Codegraph @

autotests/ktimecomboboxtest.cpp

raw symbols SCIP
1 /*
2 SPDX-FileCopyrightText: 2011 John Layt <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "ktimecomboboxtest.h"
8
9 #include <QTime>
10
11 #include <QLineEdit>
12 #include <QTest>
13 #include <ktimecombobox.h>
14
15 QTEST_MAIN(KTimeComboBoxTest)
16
17 KTimeComboBoxTest::KTimeComboBoxTest()
18 {
19 QLocale::setDefault(QLocale(QLocale::C));
20 }
21
22 void KTimeComboBoxTest::testDefaults()
23 {
24 m_combo = new KTimeComboBox();
25 QCOMPARE(m_combo->time(), QTime(0, 0, 0, 0));
26 QCOMPARE(m_combo->minimumTime(), QTime(0, 0, 0, 0));
27 QCOMPARE(m_combo->maximumTime(), QTime(23, 59, 59, 999));
28 QCOMPARE(m_combo->isValid(), true);
29 QCOMPARE(m_combo->options(), KTimeComboBox::EditTime | KTimeComboBox::SelectTime);
30 QCOMPARE(m_combo->timeListInterval(), 15);
31 QCOMPARE(m_combo->displayFormat(), QLocale::ShortFormat);
32 delete m_combo;
33 }
34
35 void KTimeComboBoxTest::testValidNull()
36 {
37 m_combo = new KTimeComboBox(nullptr);
38 QCOMPARE(m_combo->isValid(), true);
39 QCOMPARE(m_combo->isNull(), false);
40 m_combo->setTime(QTime());
41 QCOMPARE(m_combo->isValid(), false);
42 QCOMPARE(m_combo->isNull(), true);
43 m_combo->setTime(QTime(0, 0, 0));
44 m_combo->lineEdit()->setText(QStringLiteral("99:99"));
45 QCOMPARE(m_combo->isValid(), false);
46 QCOMPARE(m_combo->isNull(), false);
47 delete m_combo;
48 }
49
50 void KTimeComboBoxTest::testTimeRange()
51 {
52 m_combo = new KTimeComboBox();
53 m_combo->setTime(QTime(2, 0, 0, 0));
54 QCOMPARE(m_combo->minimumTime(), QTime(0, 0, 0, 0));
55 QCOMPARE(m_combo->maximumTime(), QTime(23, 59, 59, 999));
56 QCOMPARE(m_combo->isValid(), true);
57
58 m_combo->setTimeRange(QTime(3, 0, 0, 0), QTime(22, 0, 0, 0));
59 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0, 0));
60 QCOMPARE(m_combo->maximumTime(), QTime(22, 0, 0, 0));
61 QCOMPARE(m_combo->isValid(), false);
62
63 m_combo->setTime(QTime(23, 0, 0, 0));
64 QCOMPARE(m_combo->isValid(), false);
65 m_combo->setTime(QTime(12, 0, 0, 0));
66 QCOMPARE(m_combo->isValid(), true);
67 m_combo->setTime(QTime(3, 0, 0, 0));
68 QCOMPARE(m_combo->isValid(), true);
69 m_combo->setTime(QTime(22, 0, 0, 0));
70 QCOMPARE(m_combo->isValid(), true);
71 m_combo->setTime(QTime(2, 59, 59, 999));
72 QCOMPARE(m_combo->isValid(), false);
73 m_combo->setTime(QTime(22, 1, 0, 0));
74 QCOMPARE(m_combo->isValid(), false);
75
76 m_combo->setTimeRange(QTime(15, 0, 0, 0), QTime(5, 0, 0, 0));
77 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0, 0));
78 QCOMPARE(m_combo->maximumTime(), QTime(22, 0, 0, 0));
79
80 m_combo->setMinimumTime(QTime(2, 0, 0, 0));
81 QCOMPARE(m_combo->minimumTime(), QTime(2, 0, 0, 0));
82 QCOMPARE(m_combo->maximumTime(), QTime(22, 0, 0, 0));
83
84 m_combo->setMaximumTime(QTime(21, 0, 0, 0));
85 QCOMPARE(m_combo->minimumTime(), QTime(2, 0, 0, 0));
86 QCOMPARE(m_combo->maximumTime(), QTime(21, 0, 0, 0));
87
88 delete m_combo;
89 }
90
91 void KTimeComboBoxTest::testTimeListInterval()
92 {
93 m_combo = new KTimeComboBox();
94 QCOMPARE(m_combo->timeListInterval(), 15);
95 m_combo->setTimeListInterval(60);
96 QCOMPARE(m_combo->timeListInterval(), 60);
97 m_combo->setTimeListInterval(7);
98 QCOMPARE(m_combo->timeListInterval(), 60);
99 m_combo->setTimeListInterval(720);
100 QCOMPARE(m_combo->timeListInterval(), 720);
101 QList<QTime> list;
102 list << QTime(0, 0, 0) << QTime(12, 0, 0) << QTime(23, 59, 59, 999);
103 QCOMPARE(m_combo->timeList(), list);
104 m_combo->setTimeRange(QTime(4, 0, 0, 0), QTime(5, 0, 0, 0));
105 m_combo->setTimeListInterval(30);
106 list.clear();
107 list << QTime(4, 0, 0) << QTime(4, 30, 0) << QTime(5, 0, 0, 0);
108 QCOMPARE(m_combo->timeList(), list);
109 m_combo->setTimeRange(QTime(4, 0, 0, 0), QTime(4, 59, 0, 0));
110 m_combo->setTimeListInterval(30);
111 list.clear();
112 list << QTime(4, 0, 0) << QTime(4, 30, 0) << QTime(4, 59, 0, 0);
113 QCOMPARE(m_combo->timeList(), list);
114 delete m_combo;
115 }
116
117 void KTimeComboBoxTest::testTimeList()
118 {
119 m_combo = new KTimeComboBox();
120 QList<QTime> list;
121
122 // Test default list
123 QTime thisTime = QTime(0, 0, 0);
124 for (int i = 0; i < 1440; i = i + 15) {
125 list << thisTime.addSecs(i * 60);
126 }
127 list << QTime(23, 59, 59, 999);
128 QCOMPARE(m_combo->timeList(), list);
129
130 // Test basic list
131 list.clear();
132 list << QTime(3, 0, 0) << QTime(15, 16, 17);
133 m_combo->setTimeList(list);
134 QCOMPARE(m_combo->timeList(), list);
135 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0));
136 QCOMPARE(m_combo->maximumTime(), QTime(15, 16, 17));
137
138 // Test sort input times
139 list.clear();
140 list << QTime(17, 16, 15) << QTime(4, 0, 0);
141 m_combo->setTimeList(list);
142 std::sort(list.begin(), list.end());
143 QCOMPARE(m_combo->timeList(), list);
144 QCOMPARE(m_combo->minimumTime(), QTime(4, 0, 0));
145 QCOMPARE(m_combo->maximumTime(), QTime(17, 16, 15));
146
147 // Test ignore null QTime
148 list.clear();
149 list << QTime(3, 0, 0) << QTime(15, 16, 17) << QTime();
150 m_combo->setTimeList(list);
151 list.takeLast();
152 QCOMPARE(m_combo->timeList(), list);
153 QCOMPARE(m_combo->minimumTime(), QTime(3, 0, 0));
154 QCOMPARE(m_combo->maximumTime(), QTime(15, 16, 17));
155 delete m_combo;
156 }
157
158 void KTimeComboBoxTest::testOptions()
159 {
160 m_combo = new KTimeComboBox();
161 KTimeComboBox::Options options = KTimeComboBox::EditTime | KTimeComboBox::SelectTime;
162 QCOMPARE(m_combo->options(), options);
163 options = KTimeComboBox::EditTime | KTimeComboBox::WarnOnInvalid;
164 m_combo->setOptions(options);
165 QCOMPARE(m_combo->options(), options);
166 delete m_combo;
167 }
168
169 void KTimeComboBoxTest::testDisplayFormat()
170 {
171 m_combo = new KTimeComboBox();
172 QLocale::FormatType format = QLocale::ShortFormat;
173 QCOMPARE(m_combo->displayFormat(), format);
174 format = QLocale::NarrowFormat;
175 m_combo->setDisplayFormat(format);
176 QCOMPARE(m_combo->displayFormat(), format);
177 delete m_combo;
178 }
179
180 void KTimeComboBoxTest::testMask()
181 {
182 // Store the current locale, and set to one which reproduces the bug.
183 QLocale currentLocale;
184
185 // Test that the line edit input mask AM/PM portion gets correctly
186 // replaced with xx.
187 QLocale::setDefault(QLocale(QLocale::English, QLocale::Australia));
188 m_combo = new KTimeComboBox(nullptr);
189 QString mask = m_combo->lineEdit()->inputMask();
190 QVERIFY(mask.contains(QLatin1String("xx")));
191 delete m_combo;
192
193 // For 24 hour time formats, no am/pm specifier in mask
194 QLocale::setDefault(QLocale(QLocale::NorwegianBokmal, QLocale::Norway));
195 m_combo = new KTimeComboBox(nullptr);
196 mask = m_combo->lineEdit()->inputMask();
197 QVERIFY(!mask.contains(QLatin1String("xx")));
198 delete m_combo;
199
200 // Restore the previous locale
201 QLocale::setDefault(currentLocale);
202 }
203
204 void KTimeComboBoxTest::testEdit_data()
205 {
206 QTest::addColumn<QString>("locale");
207
208 QTest::newRow("C") << QStringLiteral("C");
209 QTest::newRow("el_GR") << QStringLiteral("el_GR"); // bug 361764; non-ASCII AM/PM
210 QTest::newRow("en_AU") << QStringLiteral("en_AU"); // bug 361764
211 QTest::newRow("en_CA") << QStringLiteral("en_CA"); // bug 405857
212 QTest::newRow("en_IE") << QStringLiteral("en_IE"); // bug 361764
213 QTest::newRow("en_US") << QStringLiteral("en_US");
214 QTest::newRow("fr_CA") << QStringLiteral("fr_CA"); // bug 409912
215 QTest::newRow("ko_KR") << QStringLiteral("ko_KR"); // non-ASCII AM/PM
216 }
217
218 static const QTime MIDNIGHT = QTime(00, 00, 00);
219 static const QTime END_AM = QTime(11, 59, 00);
220 static const QTime NOON = QTime(12, 00, 00);
221 static const QTime END_PM = QTime(23, 59, 00);
222
223 // Test that the widget can process times using the current locale and edit mask.
224 // See https://bugs.kde.org/show_bug.cgi?id=409867
225 void KTimeComboBoxTest::testEdit()
226 {
227 QLocale currentLocale;
228
229 QFETCH(QString, locale);
230
231 QLocale::setDefault(QLocale(locale));
232 m_combo = new KTimeComboBox(nullptr);
233
234 m_combo->setTime(MIDNIGHT);
235 QCOMPARE(m_combo->time(), MIDNIGHT);
236 m_combo->setTime(END_AM);
237 QCOMPARE(m_combo->time(), END_AM);
238 m_combo->setTime(NOON);
239 QCOMPARE(m_combo->time(), NOON);
240 m_combo->setTime(END_PM);
241 QCOMPARE(m_combo->time(), END_PM);
242
243 delete m_combo;
244
245 QLocale::setDefault(currentLocale);
246 }
247
248 #include "moc_ktimecomboboxtest.cpp"