1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <QtCharts/QBarSet> |
32 | #include <QtCharts/QBarSeries> |
33 | #include <QtCharts/QChartView> |
34 | #include "tst_definitions.h" |
35 | |
36 | QT_CHARTS_USE_NAMESPACE |
37 | |
38 | class tst_QBarSet : public QObject |
39 | { |
40 | Q_OBJECT |
41 | |
42 | public slots: |
43 | void initTestCase(); |
44 | void cleanupTestCase(); |
45 | void init(); |
46 | void cleanup(); |
47 | |
48 | private slots: |
49 | void qbarset_data(); |
50 | void qbarset(); |
51 | void label_data(); |
52 | void label(); |
53 | void append_data(); |
54 | void append(); |
55 | void appendOperator_data(); |
56 | void appendOperator(); |
57 | void insert_data(); |
58 | void insert(); |
59 | void remove_data(); |
60 | void remove(); |
61 | void replace_data(); |
62 | void replace(); |
63 | void at_data(); |
64 | void at(); |
65 | void atOperator_data(); |
66 | void atOperator(); |
67 | void count_data(); |
68 | void count(); |
69 | void sum_data(); |
70 | void sum(); |
71 | void customize(); |
72 | |
73 | private: |
74 | QBarSet* m_barset; |
75 | }; |
76 | |
77 | void tst_QBarSet::initTestCase() |
78 | { |
79 | } |
80 | |
81 | void tst_QBarSet::cleanupTestCase() |
82 | { |
83 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
84 | } |
85 | |
86 | void tst_QBarSet::init() |
87 | { |
88 | m_barset = new QBarSet(QString("label" )); |
89 | } |
90 | |
91 | void tst_QBarSet::cleanup() |
92 | { |
93 | delete m_barset; |
94 | m_barset = 0; |
95 | } |
96 | |
97 | void tst_QBarSet::qbarset_data() |
98 | { |
99 | } |
100 | |
101 | void tst_QBarSet::qbarset() |
102 | { |
103 | QBarSet barset(QString("label" )); |
104 | QCOMPARE(barset.label(), QString("label" )); |
105 | QCOMPARE(barset.count(), 0); |
106 | QVERIFY(qFuzzyCompare(barset.sum(), 0)); |
107 | } |
108 | |
109 | void tst_QBarSet::label_data() |
110 | { |
111 | QTest::addColumn<QString> (name: "label" ); |
112 | QTest::addColumn<QString> (name: "result" ); |
113 | QTest::newRow(dataTag: "label0" ) << QString("label0" ) << QString("label0" ); |
114 | QTest::newRow(dataTag: "label1" ) << QString("label1" ) << QString("label1" ); |
115 | } |
116 | |
117 | void tst_QBarSet::label() |
118 | { |
119 | QFETCH(QString, label); |
120 | QFETCH(QString, result); |
121 | |
122 | QSignalSpy labelSpy(m_barset,SIGNAL(labelChanged())); |
123 | m_barset->setLabel(label); |
124 | QCOMPARE(m_barset->label(), result); |
125 | QVERIFY(labelSpy.count() == 1); |
126 | } |
127 | |
128 | void tst_QBarSet::append_data() |
129 | { |
130 | QTest::addColumn<int> (name: "count" ); |
131 | QTest::newRow(dataTag: "0" ) << 0; |
132 | QTest::newRow(dataTag: "5" ) << 5; |
133 | QTest::newRow(dataTag: "100" ) << 100; |
134 | QTest::newRow(dataTag: "1000" ) << 1000; |
135 | } |
136 | |
137 | void tst_QBarSet::append() |
138 | { |
139 | QFETCH(int, count); |
140 | |
141 | QCOMPARE(m_barset->count(), 0); |
142 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
143 | |
144 | QSignalSpy valueSpy(m_barset, SIGNAL(valuesAdded(int,int))); |
145 | |
146 | qreal sum(0.0); |
147 | qreal value(0.0); |
148 | |
149 | for (int i=0; i<count; i++) { |
150 | m_barset->append(value); |
151 | QCOMPARE(m_barset->at(i), value); |
152 | sum += value; |
153 | value += 1.0; |
154 | } |
155 | |
156 | QCOMPARE(m_barset->count(), count); |
157 | QVERIFY(qFuzzyCompare(m_barset->sum(), sum)); |
158 | |
159 | QCOMPARE(valueSpy.count(), count); |
160 | } |
161 | |
162 | void tst_QBarSet::appendOperator_data() |
163 | { |
164 | append_data(); |
165 | } |
166 | |
167 | void tst_QBarSet::appendOperator() |
168 | { |
169 | QFETCH(int, count); |
170 | |
171 | QCOMPARE(m_barset->count(), 0); |
172 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
173 | |
174 | QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); |
175 | |
176 | qreal sum(0.0); |
177 | qreal value(0.0); |
178 | |
179 | for (int i=0; i<count; i++) { |
180 | *m_barset << value; |
181 | QCOMPARE(m_barset->at(i), value); |
182 | sum += value; |
183 | value += 1.0; |
184 | } |
185 | |
186 | QCOMPARE(m_barset->count(), count); |
187 | QVERIFY(qFuzzyCompare(m_barset->sum(), sum)); |
188 | QCOMPARE(valueSpy.count(), count); |
189 | } |
190 | |
191 | void tst_QBarSet::insert_data() |
192 | { |
193 | } |
194 | |
195 | void tst_QBarSet::insert() |
196 | { |
197 | QCOMPARE(m_barset->count(), 0); |
198 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
199 | QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); |
200 | |
201 | m_barset->insert(index: 0, value: 1.0); // 1.0 |
202 | QCOMPARE(m_barset->at(0), 1.0); |
203 | QCOMPARE(m_barset->count(), 1); |
204 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)1.0)); |
205 | |
206 | m_barset->insert(index: 0, value: 2.0); // 2.0 1.0 |
207 | QCOMPARE(m_barset->at(0), 2.0); |
208 | QCOMPARE(m_barset->at(1), 1.0); |
209 | QCOMPARE(m_barset->count(), 2); |
210 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)3.0)); |
211 | |
212 | m_barset->insert(index: 1, value: 3.0); // 2.0 3.0 1.0 |
213 | QCOMPARE(m_barset->at(1), 3.0); |
214 | QCOMPARE(m_barset->at(0), 2.0); |
215 | QCOMPARE(m_barset->at(2), 1.0); |
216 | QCOMPARE(m_barset->count(), 3); |
217 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)6.0)); |
218 | QCOMPARE(valueSpy.count(), 3); |
219 | } |
220 | |
221 | void tst_QBarSet::remove_data() |
222 | { |
223 | } |
224 | |
225 | void tst_QBarSet::remove() |
226 | { |
227 | QCOMPARE(m_barset->count(), 0); |
228 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
229 | |
230 | QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int))); |
231 | |
232 | m_barset->append(value: 1.0); |
233 | m_barset->append(value: 2.0); |
234 | m_barset->append(value: 3.0); |
235 | m_barset->append(value: 4.0); |
236 | |
237 | QCOMPARE(m_barset->count(), 4); |
238 | QCOMPARE(m_barset->sum(), 10.0); |
239 | |
240 | // Remove middle |
241 | m_barset->remove(index: 2); // 1.0 2.0 4.0 |
242 | QCOMPARE(m_barset->at(0), 1.0); |
243 | QCOMPARE(m_barset->at(1), 2.0); |
244 | QCOMPARE(m_barset->at(2), 4.0); |
245 | QCOMPARE(m_barset->count(), 3); |
246 | QCOMPARE(m_barset->sum(), 7.0); |
247 | QCOMPARE(valueSpy.count(), 1); |
248 | |
249 | QList<QVariant> valueSpyArg = valueSpy.takeFirst(); |
250 | // Verify index of removed signal |
251 | QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); |
252 | QVERIFY(valueSpyArg.at(0).toInt() == 2); |
253 | // Verify count of removed signal |
254 | QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); |
255 | QVERIFY(valueSpyArg.at(1).toInt() == 1); |
256 | |
257 | // Remove first |
258 | m_barset->remove(index: 0); // 2.0 4.0 |
259 | QCOMPARE(m_barset->at(0), 2.0); |
260 | QCOMPARE(m_barset->at(1), 4.0); |
261 | QCOMPARE(m_barset->count(), 2); |
262 | QCOMPARE(m_barset->sum(), 6.0); |
263 | |
264 | QCOMPARE(valueSpy.count(), 1); |
265 | valueSpyArg = valueSpy.takeFirst(); |
266 | // Verify index of removed signal |
267 | QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); |
268 | QVERIFY(valueSpyArg.at(0).toInt() == 0); |
269 | // Verify count of removed signal |
270 | QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); |
271 | QVERIFY(valueSpyArg.at(1).toInt() == 1); |
272 | |
273 | |
274 | // Illegal indexes |
275 | m_barset->remove(index: 4); |
276 | QCOMPARE(m_barset->count(), 2); |
277 | QCOMPARE(m_barset->sum(), 6.0); |
278 | m_barset->remove(index: -1); |
279 | QCOMPARE(m_barset->count(), 2); |
280 | QCOMPARE(m_barset->sum(), 6.0); |
281 | |
282 | // nothing removed, no signals should be emitted |
283 | QCOMPARE(valueSpy.count(), 0); |
284 | |
285 | // Remove more items than list has |
286 | m_barset->remove(index: 0,count: 312); |
287 | QCOMPARE(m_barset->count(), 0); |
288 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
289 | |
290 | QCOMPARE(valueSpy.count(), 1); |
291 | valueSpyArg = valueSpy.takeFirst(); |
292 | |
293 | // Verify index of removed signal |
294 | QVERIFY(valueSpyArg.at(0).type() == QVariant::Int); |
295 | QVERIFY(valueSpyArg.at(0).toInt() == 0); |
296 | // Verify count of removed signal (expect 2 values removed, because list had only 2 items) |
297 | QVERIFY(valueSpyArg.at(1).type() == QVariant::Int); |
298 | QVERIFY(valueSpyArg.at(1).toInt() == 2); |
299 | } |
300 | |
301 | void tst_QBarSet::replace_data() |
302 | { |
303 | |
304 | } |
305 | |
306 | void tst_QBarSet::replace() |
307 | { |
308 | QCOMPARE(m_barset->count(), 0); |
309 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
310 | QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int))); |
311 | |
312 | m_barset->append(value: 1.0); |
313 | m_barset->append(value: 2.0); |
314 | m_barset->append(value: 3.0); |
315 | m_barset->append(value: 4.0); |
316 | |
317 | QCOMPARE(m_barset->count(), 4); |
318 | QCOMPARE(m_barset->sum(), 10.0); |
319 | |
320 | // Replace first |
321 | m_barset->replace(index: 0, value: 5.0); // 5.0 2.0 3.0 4.0 |
322 | QCOMPARE(m_barset->count(), 4); |
323 | QCOMPARE(m_barset->sum(), 14.0); |
324 | QCOMPARE(m_barset->at(0), 5.0); |
325 | |
326 | // Replace last |
327 | m_barset->replace(index: 3, value: 6.0); |
328 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 |
329 | QCOMPARE(m_barset->sum(), 16.0); |
330 | QCOMPARE(m_barset->at(0), 5.0); |
331 | QCOMPARE(m_barset->at(1), 2.0); |
332 | QCOMPARE(m_barset->at(2), 3.0); |
333 | QCOMPARE(m_barset->at(3), 6.0); |
334 | |
335 | // Illegal indexes |
336 | m_barset->replace(index: 4, value: 6.0); |
337 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 |
338 | QCOMPARE(m_barset->sum(), 16.0); |
339 | m_barset->replace(index: -1, value: 6.0); |
340 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 |
341 | QCOMPARE(m_barset->sum(), 16.0); |
342 | m_barset->replace(index: 4, value: 1.0); |
343 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 |
344 | QCOMPARE(m_barset->sum(), 16.0); |
345 | m_barset->replace(index: -1, value: 1.0); |
346 | QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0 |
347 | QCOMPARE(m_barset->sum(), 16.0); |
348 | |
349 | QVERIFY(valueSpy.count() == 2); |
350 | } |
351 | |
352 | void tst_QBarSet::at_data() |
353 | { |
354 | |
355 | } |
356 | |
357 | void tst_QBarSet::at() |
358 | { |
359 | QCOMPARE(m_barset->count(), 0); |
360 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
361 | |
362 | m_barset->append(value: 1.0); |
363 | m_barset->append(value: 2.0); |
364 | m_barset->append(value: 3.0); |
365 | m_barset->append(value: 4.0); |
366 | |
367 | QCOMPARE(m_barset->at(0), 1.0); |
368 | QCOMPARE(m_barset->at(1), 2.0); |
369 | QCOMPARE(m_barset->at(2), 3.0); |
370 | QCOMPARE(m_barset->at(3), 4.0); |
371 | } |
372 | |
373 | void tst_QBarSet::atOperator_data() |
374 | { |
375 | |
376 | } |
377 | |
378 | void tst_QBarSet::atOperator() |
379 | { |
380 | QCOMPARE(m_barset->count(), 0); |
381 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
382 | |
383 | m_barset->append(value: 1.0); |
384 | m_barset->append(value: 2.0); |
385 | m_barset->append(value: 3.0); |
386 | m_barset->append(value: 4.0); |
387 | |
388 | QCOMPARE(m_barset->operator [](0), 1.0); |
389 | QCOMPARE(m_barset->operator [](1), 2.0); |
390 | QCOMPARE(m_barset->operator [](2), 3.0); |
391 | QCOMPARE(m_barset->operator [](3), 4.0); |
392 | } |
393 | |
394 | void tst_QBarSet::count_data() |
395 | { |
396 | |
397 | } |
398 | |
399 | void tst_QBarSet::count() |
400 | { |
401 | QCOMPARE(m_barset->count(), 0); |
402 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
403 | |
404 | m_barset->append(value: 1.0); |
405 | QCOMPARE(m_barset->count(),1); |
406 | m_barset->append(value: 2.0); |
407 | QCOMPARE(m_barset->count(),2); |
408 | m_barset->append(value: 3.0); |
409 | QCOMPARE(m_barset->count(),3); |
410 | m_barset->append(value: 4.0); |
411 | QCOMPARE(m_barset->count(),4); |
412 | } |
413 | |
414 | void tst_QBarSet::sum_data() |
415 | { |
416 | |
417 | } |
418 | |
419 | void tst_QBarSet::sum() |
420 | { |
421 | QCOMPARE(m_barset->count(), 0); |
422 | QVERIFY(qFuzzyCompare(m_barset->sum(), 0)); |
423 | |
424 | m_barset->append(value: 1.0); |
425 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)1.0)); |
426 | m_barset->append(value: 2.0); |
427 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)3.0)); |
428 | m_barset->append(value: 3.0); |
429 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)6.0)); |
430 | m_barset->append(value: 4.0); |
431 | QVERIFY(qFuzzyCompare(m_barset->sum(), (qreal)10.0)); |
432 | } |
433 | |
434 | void tst_QBarSet::customize() |
435 | { |
436 | // Create sets |
437 | QBarSet *set1 = new QBarSet("set1" ); |
438 | QBarSet *set2 = new QBarSet("set2" ); |
439 | |
440 | // Append set1 to series |
441 | QBarSeries *series = new QBarSeries(); |
442 | bool success = series->append(set: set1); |
443 | QVERIFY(success); |
444 | |
445 | // Add series to the chart |
446 | QChartView view(new QChart()); |
447 | view.resize(w: 200, h: 200); |
448 | view.chart()->addSeries(series); |
449 | view.show(); |
450 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
451 | |
452 | // Test adding data to the sets |
453 | *set1 << 1 << 2 << 1 << 3; |
454 | *set2 << 2 << 1 << 3 << 1; |
455 | |
456 | // Remove sets from series |
457 | series->take(set: set1); |
458 | series->take(set: set2); |
459 | |
460 | // Test pen |
461 | QVERIFY(set1->pen() != QPen()); |
462 | QVERIFY(set2->pen() == QPen()); |
463 | QPen pen(QColor(128,128,128,128)); |
464 | set1->setPen(pen); |
465 | |
466 | // Add sets back to series |
467 | series->append(set: set1); |
468 | series->append(set: set2); |
469 | |
470 | QVERIFY(set1->pen() == pen); // Should be customized |
471 | QVERIFY(set2->pen() != QPen()); // Should be decorated by theme |
472 | |
473 | // Remove sets from series |
474 | series->take(set: set1); |
475 | series->take(set: set2); |
476 | |
477 | // Test brush |
478 | set2->setBrush(QBrush()); |
479 | QVERIFY(set1->brush() != QBrush()); |
480 | QVERIFY(set2->brush() == QBrush()); |
481 | QBrush brush(QColor(128,128,128,128)); |
482 | set1->setBrush(brush); |
483 | |
484 | // Add sets back to series |
485 | series->append(set: set1); |
486 | series->append(set: set2); |
487 | |
488 | QVERIFY(set1->brush() == brush); // Should be customized |
489 | QVERIFY(set2->brush() == QBrush()); // Setting empty brush doesn't reset to theme brush |
490 | |
491 | // Remove sets from series |
492 | series->take(set: set1); |
493 | series->take(set: set2); |
494 | |
495 | // Test label brush |
496 | set2->setLabelBrush(QBrush()); |
497 | QVERIFY(set1->labelBrush() != QBrush()); |
498 | QVERIFY(set2->labelBrush() == QBrush()); |
499 | set1->setLabelBrush(brush); |
500 | |
501 | series->append(set: set1); |
502 | series->append(set: set2); |
503 | QVERIFY(set1->labelBrush() == brush); // Should be customized |
504 | QVERIFY(set2->labelBrush() == QBrush()); // Setting empty brush doesn't reset to theme brush |
505 | |
506 | // Test label font |
507 | // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the |
508 | // same for the set added to the series (depending on the QChart's theme configuration) |
509 | QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont()); |
510 | QVERIFY(set2->labelFont() == QFont()); |
511 | QFont font; |
512 | font.setBold(true); |
513 | font.setItalic(true); |
514 | set1->setLabelFont(font); |
515 | QVERIFY(set1->labelFont() == font); |
516 | QVERIFY(set2->labelFont() == QFont()); |
517 | |
518 | // Test adding data to the sets |
519 | *set1 << 1 << 2 << 1 << 3; |
520 | *set2 << 2 << 1 << 3 << 1; |
521 | |
522 | } |
523 | |
524 | |
525 | |
526 | |
527 | QTEST_MAIN(tst_QBarSet) |
528 | |
529 | #include "tst_qbarset.moc" |
530 | |
531 | |