@@ -1,571 +1,587 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtTest/QtTest> |
|
22 | 22 | #include <qbarseries.h> |
|
23 | 23 | #include <qbarset.h> |
|
24 | 24 | #include <qchartview.h> |
|
25 | 25 | #include <qchart.h> |
|
26 | 26 | #include "tst_definitions.h" |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | Q_DECLARE_METATYPE(QBarSet*) |
|
31 | 31 | |
|
32 | 32 | class tst_QBarSeries : public QObject |
|
33 | 33 | { |
|
34 | 34 | Q_OBJECT |
|
35 | 35 | |
|
36 | 36 | public slots: |
|
37 | 37 | void initTestCase(); |
|
38 | 38 | void cleanupTestCase(); |
|
39 | 39 | void init(); |
|
40 | 40 | void cleanup(); |
|
41 | 41 | |
|
42 | 42 | private slots: |
|
43 | 43 | void qbarseries_data(); |
|
44 | 44 | void qbarseries(); |
|
45 | 45 | void type_data(); |
|
46 | 46 | void type(); |
|
47 | 47 | void append_data(); |
|
48 | 48 | void append(); |
|
49 | 49 | void remove_data(); |
|
50 | 50 | void remove(); |
|
51 | 51 | void appendList_data(); |
|
52 | 52 | void appendList(); |
|
53 | 53 | void count_data(); |
|
54 | 54 | void count(); |
|
55 | 55 | void barSets_data(); |
|
56 | 56 | void barSets(); |
|
57 | 57 | void setLabelsVisible_data(); |
|
58 | 58 | void setLabelsVisible(); |
|
59 | 59 | void mouseclicked_data(); |
|
60 | 60 | void mouseclicked(); |
|
61 | 61 | void mousehovered_data(); |
|
62 | 62 | void mousehovered(); |
|
63 | 63 | void clearWithAnimations(); |
|
64 | void destruction(); | |
|
64 | 65 | |
|
65 | 66 | private: |
|
66 | 67 | QBarSeries* m_barseries; |
|
67 | 68 | QBarSeries* m_barseries_with_sets; |
|
68 | 69 | |
|
69 | 70 | QList<QBarSet*> m_testSets; |
|
70 | 71 | |
|
71 | 72 | }; |
|
72 | 73 | |
|
73 | 74 | void tst_QBarSeries::initTestCase() |
|
74 | 75 | { |
|
75 | 76 | qRegisterMetaType<QBarSet*>("QBarSet*"); |
|
76 | 77 | } |
|
77 | 78 | |
|
78 | 79 | void tst_QBarSeries::cleanupTestCase() |
|
79 | 80 | { |
|
80 | 81 | } |
|
81 | 82 | |
|
82 | 83 | void tst_QBarSeries::init() |
|
83 | 84 | { |
|
84 | 85 | m_barseries = new QBarSeries(); |
|
85 | 86 | m_barseries_with_sets = new QBarSeries(); |
|
86 | 87 | |
|
87 | 88 | for (int i=0; i<5; i++) { |
|
88 | 89 | m_testSets.append(new QBarSet("testset")); |
|
89 | 90 | m_barseries_with_sets->append(m_testSets.at(i)); |
|
90 | 91 | } |
|
91 | 92 | } |
|
92 | 93 | |
|
93 | 94 | void tst_QBarSeries::cleanup() |
|
94 | 95 | { |
|
95 | 96 | foreach(QBarSet* s, m_testSets) { |
|
96 | 97 | m_barseries_with_sets->remove(s); |
|
97 | 98 | delete s; |
|
98 | 99 | } |
|
99 | 100 | m_testSets.clear(); |
|
100 | 101 | |
|
101 | 102 | delete m_barseries; |
|
102 | 103 | m_barseries = 0; |
|
103 | 104 | delete m_barseries_with_sets; |
|
104 | 105 | m_barseries_with_sets = 0; |
|
105 | 106 | } |
|
106 | 107 | |
|
107 | 108 | void tst_QBarSeries::qbarseries_data() |
|
108 | 109 | { |
|
109 | 110 | } |
|
110 | 111 | |
|
111 | 112 | void tst_QBarSeries::qbarseries() |
|
112 | 113 | { |
|
113 | 114 | QBarSeries *barseries = new QBarSeries(); |
|
114 | 115 | QVERIFY(barseries != 0); |
|
115 | 116 | } |
|
116 | 117 | |
|
117 | 118 | void tst_QBarSeries::type_data() |
|
118 | 119 | { |
|
119 | 120 | |
|
120 | 121 | } |
|
121 | 122 | |
|
122 | 123 | void tst_QBarSeries::type() |
|
123 | 124 | { |
|
124 | 125 | QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar); |
|
125 | 126 | } |
|
126 | 127 | |
|
127 | 128 | void tst_QBarSeries::append_data() |
|
128 | 129 | { |
|
129 | 130 | } |
|
130 | 131 | |
|
131 | 132 | void tst_QBarSeries::append() |
|
132 | 133 | { |
|
133 | 134 | QVERIFY(m_barseries->count() == 0); |
|
134 | 135 | |
|
135 | 136 | bool ret = false; |
|
136 | 137 | |
|
137 | 138 | // Try adding barset |
|
138 | 139 | QBarSet *barset = new QBarSet("testset"); |
|
139 | 140 | ret = m_barseries->append(barset); |
|
140 | 141 | |
|
141 | 142 | QVERIFY(ret == true); |
|
142 | 143 | QVERIFY(m_barseries->count() == 1); |
|
143 | 144 | |
|
144 | 145 | // Try adding another set |
|
145 | 146 | QBarSet *barset2 = new QBarSet("testset2"); |
|
146 | 147 | ret = m_barseries->append(barset2); |
|
147 | 148 | |
|
148 | 149 | QVERIFY(ret == true); |
|
149 | 150 | QVERIFY(m_barseries->count() == 2); |
|
150 | 151 | |
|
151 | 152 | // Try adding same set again |
|
152 | 153 | ret = m_barseries->append(barset2); |
|
153 | 154 | QVERIFY(ret == false); |
|
154 | 155 | QVERIFY(m_barseries->count() == 2); |
|
155 | 156 | |
|
156 | 157 | // Try adding null set |
|
157 | 158 | ret = m_barseries->append(0); |
|
158 | 159 | QVERIFY(ret == false); |
|
159 | 160 | QVERIFY(m_barseries->count() == 2); |
|
160 | 161 | |
|
161 | 162 | } |
|
162 | 163 | |
|
163 | 164 | void tst_QBarSeries::remove_data() |
|
164 | 165 | { |
|
165 | 166 | } |
|
166 | 167 | |
|
167 | 168 | void tst_QBarSeries::remove() |
|
168 | 169 | { |
|
169 | 170 | int count = m_testSets.count(); |
|
170 | 171 | QVERIFY(m_barseries_with_sets->count() == count); |
|
171 | 172 | |
|
172 | 173 | // Try to remove null pointer (should not remove, should not crash) |
|
173 | 174 | bool ret = false; |
|
174 | 175 | ret = m_barseries_with_sets->remove(0); |
|
175 | 176 | QVERIFY(ret == false); |
|
176 | 177 | QVERIFY(m_barseries_with_sets->count() == count); |
|
177 | 178 | |
|
178 | 179 | // Try to remove invalid pointer (should not remove, should not crash) |
|
179 | 180 | ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) ); |
|
180 | 181 | QVERIFY(ret == false); |
|
181 | 182 | QVERIFY(m_barseries_with_sets->count() == count); |
|
182 | 183 | |
|
183 | 184 | // remove some sets |
|
184 | 185 | ret = m_barseries_with_sets->remove(m_testSets.at(2)); |
|
185 | 186 | QVERIFY(ret == true); |
|
186 | 187 | ret = m_barseries_with_sets->remove(m_testSets.at(3)); |
|
187 | 188 | QVERIFY(ret == true); |
|
188 | 189 | ret = m_barseries_with_sets->remove(m_testSets.at(4)); |
|
189 | 190 | QVERIFY(ret == true); |
|
190 | 191 | |
|
191 | 192 | QVERIFY(m_barseries_with_sets->count() == 2); |
|
192 | 193 | |
|
193 | 194 | QList<QBarSet*> verifysets = m_barseries_with_sets->barSets(); |
|
194 | 195 | |
|
195 | 196 | QVERIFY(verifysets.at(0) == m_testSets.at(0)); |
|
196 | 197 | QVERIFY(verifysets.at(1) == m_testSets.at(1)); |
|
197 | 198 | |
|
198 | 199 | // Try removing all sets again (should be ok, even if some sets have already been removed) |
|
199 | 200 | ret = false; |
|
200 | 201 | for (int i=0; i<count; i++) { |
|
201 | 202 | ret |= m_barseries_with_sets->remove(m_testSets.at(i)); |
|
202 | 203 | } |
|
203 | 204 | |
|
204 | 205 | QVERIFY(ret == true); |
|
205 | 206 | QVERIFY(m_barseries_with_sets->count() == 0); |
|
206 | 207 | } |
|
207 | 208 | |
|
208 | 209 | void tst_QBarSeries::appendList_data() |
|
209 | 210 | { |
|
210 | 211 | |
|
211 | 212 | } |
|
212 | 213 | |
|
213 | 214 | void tst_QBarSeries::appendList() |
|
214 | 215 | { |
|
215 | 216 | int count = 5; |
|
216 | 217 | QVERIFY(m_barseries->count() == 0); |
|
217 | 218 | |
|
218 | 219 | QList<QBarSet*> sets; |
|
219 | 220 | for (int i=0; i<count; i++) { |
|
220 | 221 | sets.append(new QBarSet("testset")); |
|
221 | 222 | } |
|
222 | 223 | |
|
223 | 224 | // Append new sets (should succeed, count should match the count of sets) |
|
224 | 225 | bool ret = false; |
|
225 | 226 | ret = m_barseries->append(sets); |
|
226 | 227 | QVERIFY(ret == true); |
|
227 | 228 | QVERIFY(m_barseries->count() == count); |
|
228 | 229 | |
|
229 | 230 | // Append same sets again (should fail, count should remain same) |
|
230 | 231 | ret = m_barseries->append(sets); |
|
231 | 232 | QVERIFY(ret == false); |
|
232 | 233 | QVERIFY(m_barseries->count() == count); |
|
233 | 234 | |
|
234 | 235 | // Try append empty list (should succeed, but count should remain same) |
|
235 | 236 | QList<QBarSet*> invalidList; |
|
236 | 237 | ret = m_barseries->append(invalidList); |
|
237 | 238 | QVERIFY(ret == true); |
|
238 | 239 | QVERIFY(m_barseries->count() == count); |
|
239 | 240 | |
|
240 | 241 | // Try append list with one new and one existing set (should fail, count remains same) |
|
241 | 242 | invalidList.append(new QBarSet("ok set")); |
|
242 | 243 | invalidList.append(sets.at(0)); |
|
243 | 244 | ret = m_barseries->append(invalidList); |
|
244 | 245 | QVERIFY(ret == false); |
|
245 | 246 | QVERIFY(m_barseries->count() == count); |
|
246 | 247 | |
|
247 | 248 | // Try append list with null pointers (should fail, count remains same) |
|
248 | 249 | QList<QBarSet*> invalidList2; |
|
249 | 250 | invalidList2.append(0); |
|
250 | 251 | invalidList2.append(0); |
|
251 | 252 | invalidList2.append(0); |
|
252 | 253 | ret = m_barseries->append(invalidList2); |
|
253 | 254 | QVERIFY(ret == false); |
|
254 | 255 | QVERIFY(m_barseries->count() == count); |
|
255 | 256 | } |
|
256 | 257 | |
|
257 | 258 | void tst_QBarSeries::count_data() |
|
258 | 259 | { |
|
259 | 260 | |
|
260 | 261 | } |
|
261 | 262 | |
|
262 | 263 | void tst_QBarSeries::count() |
|
263 | 264 | { |
|
264 | 265 | QVERIFY(m_barseries->count() == 0); |
|
265 | 266 | QVERIFY(m_barseries_with_sets->count() == m_testSets.count()); |
|
266 | 267 | } |
|
267 | 268 | |
|
268 | 269 | void tst_QBarSeries::barSets_data() |
|
269 | 270 | { |
|
270 | 271 | |
|
271 | 272 | } |
|
272 | 273 | |
|
273 | 274 | void tst_QBarSeries::barSets() |
|
274 | 275 | { |
|
275 | 276 | QVERIFY(m_barseries->barSets().count() == 0); |
|
276 | 277 | |
|
277 | 278 | QList<QBarSet*> sets = m_barseries_with_sets->barSets(); |
|
278 | 279 | QVERIFY(sets.count() == m_testSets.count()); |
|
279 | 280 | |
|
280 | 281 | for (int i=0; i<m_testSets.count(); i++) { |
|
281 | 282 | QVERIFY(sets.at(i) == m_testSets.at(i)); |
|
282 | 283 | } |
|
283 | 284 | } |
|
284 | 285 | |
|
285 | 286 | void tst_QBarSeries::setLabelsVisible_data() |
|
286 | 287 | { |
|
287 | 288 | |
|
288 | 289 | } |
|
289 | 290 | |
|
290 | 291 | void tst_QBarSeries::setLabelsVisible() |
|
291 | 292 | { |
|
292 | 293 | // labels should be invisible by default |
|
293 | 294 | QVERIFY(m_barseries->isLabelsVisible() == false); |
|
294 | 295 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); |
|
295 | 296 | |
|
296 | 297 | // turn labels to visible |
|
297 | 298 | m_barseries_with_sets->setLabelsVisible(true); |
|
298 | 299 | // TODO: test the signal |
|
299 | 300 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); |
|
300 | 301 | |
|
301 | 302 | // turn labels to invisible |
|
302 | 303 | m_barseries_with_sets->setLabelsVisible(false); |
|
303 | 304 | // TODO: test the signal |
|
304 | 305 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); |
|
305 | 306 | |
|
306 | 307 | // without parameter, should turn labels to visible |
|
307 | 308 | m_barseries_with_sets->setLabelsVisible(); |
|
308 | 309 | // TODO: test the signal |
|
309 | 310 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); |
|
310 | 311 | } |
|
311 | 312 | |
|
312 | 313 | void tst_QBarSeries::mouseclicked_data() |
|
313 | 314 | { |
|
314 | 315 | |
|
315 | 316 | } |
|
316 | 317 | |
|
317 | 318 | void tst_QBarSeries::mouseclicked() |
|
318 | 319 | { |
|
319 | 320 | QBarSeries* series = new QBarSeries(); |
|
320 | 321 | |
|
321 | 322 | QBarSet* set1 = new QBarSet(QString("set 1")); |
|
322 | 323 | *set1 << 10 << 10 << 10; |
|
323 | 324 | series->append(set1); |
|
324 | 325 | |
|
325 | 326 | QBarSet* set2 = new QBarSet(QString("set 2")); |
|
326 | 327 | *set2 << 10 << 10 << 10; |
|
327 | 328 | series->append(set2); |
|
328 | 329 | |
|
329 | 330 | QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*))); |
|
330 | 331 | QSignalSpy setSpy1(set1, SIGNAL(clicked(int))); |
|
331 | 332 | QSignalSpy setSpy2(set2, SIGNAL(clicked(int))); |
|
332 | 333 | |
|
333 | 334 | QChartView view(new QChart()); |
|
334 | 335 | view.resize(400,300); |
|
335 | 336 | view.chart()->addSeries(series); |
|
336 | 337 | view.show(); |
|
337 | 338 | QTest::qWaitForWindowShown(&view); |
|
338 | 339 | |
|
339 | 340 | //==================================================================================== |
|
340 | 341 | // barset 1, bar 0 |
|
341 | 342 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(95,143)); |
|
342 | 343 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
343 | 344 | |
|
344 | 345 | QCOMPARE(seriesSpy.count(), 1); |
|
345 | 346 | QCOMPARE(setSpy1.count(), 1); |
|
346 | 347 | QCOMPARE(setSpy2.count(), 0); |
|
347 | 348 | |
|
348 | 349 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
|
349 | 350 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
|
350 | 351 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
|
351 | 352 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
|
352 | 353 | |
|
353 | 354 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
|
354 | 355 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
|
355 | 356 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
|
356 | 357 | |
|
357 | 358 | //==================================================================================== |
|
358 | 359 | // barset 1, bar 1 |
|
359 | 360 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(187,143)); |
|
360 | 361 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
361 | 362 | |
|
362 | 363 | QCOMPARE(seriesSpy.count(), 1); |
|
363 | 364 | QCOMPARE(setSpy1.count(), 1); |
|
364 | 365 | QCOMPARE(setSpy2.count(), 0); |
|
365 | 366 | |
|
366 | 367 | seriesSpyArg = seriesSpy.takeFirst(); |
|
367 | 368 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
|
368 | 369 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
|
369 | 370 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
|
370 | 371 | |
|
371 | 372 | setSpyArg = setSpy1.takeFirst(); |
|
372 | 373 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
|
373 | 374 | QVERIFY(setSpyArg.at(0).toInt() == 1); |
|
374 | 375 | |
|
375 | 376 | //==================================================================================== |
|
376 | 377 | // barset 1, bar 2 |
|
377 | 378 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(280,143)); |
|
378 | 379 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
379 | 380 | |
|
380 | 381 | QCOMPARE(seriesSpy.count(), 1); |
|
381 | 382 | QCOMPARE(setSpy1.count(), 1); |
|
382 | 383 | QCOMPARE(setSpy2.count(), 0); |
|
383 | 384 | |
|
384 | 385 | seriesSpyArg = seriesSpy.takeFirst(); |
|
385 | 386 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
|
386 | 387 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
|
387 | 388 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
|
388 | 389 | |
|
389 | 390 | setSpyArg = setSpy1.takeFirst(); |
|
390 | 391 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
|
391 | 392 | QVERIFY(setSpyArg.at(0).toInt() == 2); |
|
392 | 393 | |
|
393 | 394 | //==================================================================================== |
|
394 | 395 | // barset 2, bar 0 |
|
395 | 396 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(118,143)); |
|
396 | 397 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
397 | 398 | |
|
398 | 399 | QCOMPARE(seriesSpy.count(), 1); |
|
399 | 400 | QCOMPARE(setSpy1.count(), 0); |
|
400 | 401 | QCOMPARE(setSpy2.count(), 1); |
|
401 | 402 | |
|
402 | 403 | seriesSpyArg = seriesSpy.takeFirst(); |
|
403 | 404 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
|
404 | 405 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
|
405 | 406 | QVERIFY(seriesSpyArg.at(0).toInt() == 0); |
|
406 | 407 | |
|
407 | 408 | setSpyArg = setSpy2.takeFirst(); |
|
408 | 409 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
|
409 | 410 | QVERIFY(setSpyArg.at(0).toInt() == 0); |
|
410 | 411 | |
|
411 | 412 | //==================================================================================== |
|
412 | 413 | // barset 2, bar 1 |
|
413 | 414 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(210,143)); |
|
414 | 415 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
415 | 416 | |
|
416 | 417 | QCOMPARE(seriesSpy.count(), 1); |
|
417 | 418 | QCOMPARE(setSpy1.count(), 0); |
|
418 | 419 | QCOMPARE(setSpy2.count(), 1); |
|
419 | 420 | |
|
420 | 421 | seriesSpyArg = seriesSpy.takeFirst(); |
|
421 | 422 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
|
422 | 423 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
|
423 | 424 | QVERIFY(seriesSpyArg.at(0).toInt() == 1); |
|
424 | 425 | |
|
425 | 426 | setSpyArg = setSpy2.takeFirst(); |
|
426 | 427 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
|
427 | 428 | QVERIFY(setSpyArg.at(0).toInt() == 1); |
|
428 | 429 | |
|
429 | 430 | //==================================================================================== |
|
430 | 431 | // barset 2, bar 2 |
|
431 | 432 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(303,143)); |
|
432 | 433 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
433 | 434 | |
|
434 | 435 | QCOMPARE(seriesSpy.count(), 1); |
|
435 | 436 | QCOMPARE(setSpy1.count(), 0); |
|
436 | 437 | QCOMPARE(setSpy2.count(), 1); |
|
437 | 438 | |
|
438 | 439 | seriesSpyArg = seriesSpy.takeFirst(); |
|
439 | 440 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
|
440 | 441 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); |
|
441 | 442 | QVERIFY(seriesSpyArg.at(0).toInt() == 2); |
|
442 | 443 | |
|
443 | 444 | setSpyArg = setSpy2.takeFirst(); |
|
444 | 445 | QVERIFY(setSpyArg.at(0).type() == QVariant::Int); |
|
445 | 446 | QVERIFY(setSpyArg.at(0).toInt() == 2); |
|
446 | 447 | } |
|
447 | 448 | |
|
448 | 449 | void tst_QBarSeries::mousehovered_data() |
|
449 | 450 | { |
|
450 | 451 | |
|
451 | 452 | } |
|
452 | 453 | |
|
453 | 454 | void tst_QBarSeries::mousehovered() |
|
454 | 455 | { |
|
455 | 456 | QBarSeries* series = new QBarSeries(); |
|
456 | 457 | |
|
457 | 458 | QBarSet* set1 = new QBarSet(QString("set 1")); |
|
458 | 459 | *set1 << 10 << 10 << 10; |
|
459 | 460 | series->append(set1); |
|
460 | 461 | |
|
461 | 462 | QBarSet* set2 = new QBarSet(QString("set 2")); |
|
462 | 463 | *set2 << 10 << 10 << 10; |
|
463 | 464 | series->append(set2); |
|
464 | 465 | |
|
465 | 466 | QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*))); |
|
466 | 467 | QSignalSpy setSpy1(set1, SIGNAL(hovered(bool))); |
|
467 | 468 | QSignalSpy setSpy2(set2, SIGNAL(hovered(bool))); |
|
468 | 469 | |
|
469 | 470 | QChartView view(new QChart()); |
|
470 | 471 | view.resize(400,300); |
|
471 | 472 | view.chart()->addSeries(series); |
|
472 | 473 | view.show(); |
|
473 | 474 | QTest::qWaitForWindowShown(&view); |
|
474 | 475 | |
|
475 | 476 | //this is hack since view does not get events otherwise |
|
476 | 477 | view.setMouseTracking(true); |
|
477 | 478 | |
|
478 | 479 | //======================================================================= |
|
479 | 480 | // move mouse to left border |
|
480 | 481 | QTest::mouseMove(view.viewport(), QPoint(0, 143)); |
|
481 | 482 | QCoreApplication::processEvents(QEventLoop::AllEvents, 10000); |
|
482 | 483 | TRY_COMPARE(seriesSpy.count(), 0); |
|
483 | 484 | TRY_COMPARE(setSpy1.count(), 0); |
|
484 | 485 | TRY_COMPARE(setSpy2.count(), 0); |
|
485 | 486 | |
|
486 | 487 | //======================================================================= |
|
487 | 488 | // move mouse on top of set1 |
|
488 | 489 | QTest::mouseMove(view.viewport(), QPoint(95,143)); |
|
489 | 490 | TRY_COMPARE(seriesSpy.count(), 1); |
|
490 | 491 | TRY_COMPARE(setSpy1.count(), 1); |
|
491 | 492 | TRY_COMPARE(setSpy2.count(), 0); |
|
492 | 493 | |
|
493 | 494 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
|
494 | 495 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
|
495 | 496 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); |
|
496 | 497 | QVERIFY(seriesSpyArg.at(0).toBool() == true); |
|
497 | 498 | |
|
498 | 499 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
|
499 | 500 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
500 | 501 | QVERIFY(setSpyArg.at(0).toBool() == true); |
|
501 | 502 | |
|
502 | 503 | //======================================================================= |
|
503 | 504 | // move mouse from top of set1 to top of set2 |
|
504 | 505 | QTest::mouseMove(view.viewport(), QPoint(118,143)); |
|
505 | 506 | TRY_COMPARE(seriesSpy.count(), 2); |
|
506 | 507 | TRY_COMPARE(setSpy1.count(), 1); |
|
507 | 508 | TRY_COMPARE(setSpy2.count(), 1); |
|
508 | 509 | |
|
509 | 510 | // should leave set1 |
|
510 | 511 | seriesSpyArg = seriesSpy.takeFirst(); |
|
511 | 512 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); |
|
512 | 513 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); |
|
513 | 514 | QVERIFY(seriesSpyArg.at(0).toBool() == false); |
|
514 | 515 | |
|
515 | 516 | setSpyArg = setSpy1.takeFirst(); |
|
516 | 517 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
517 | 518 | QVERIFY(setSpyArg.at(0).toBool() == false); |
|
518 | 519 | |
|
519 | 520 | // should enter set2 |
|
520 | 521 | seriesSpyArg = seriesSpy.takeFirst(); |
|
521 | 522 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
|
522 | 523 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); |
|
523 | 524 | QVERIFY(seriesSpyArg.at(0).toBool() == true); |
|
524 | 525 | |
|
525 | 526 | setSpyArg = setSpy2.takeFirst(); |
|
526 | 527 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
527 | 528 | QVERIFY(setSpyArg.at(0).toBool() == true); |
|
528 | 529 | |
|
529 | 530 | //======================================================================= |
|
530 | 531 | // move mouse from top of set2 to background |
|
531 | 532 | QTest::mouseMove(view.viewport(), QPoint(118,0)); |
|
532 | 533 | TRY_COMPARE(seriesSpy.count(), 1); |
|
533 | 534 | TRY_COMPARE(setSpy1.count(), 0); |
|
534 | 535 | TRY_COMPARE(setSpy2.count(), 1); |
|
535 | 536 | |
|
536 | 537 | // should leave set2 |
|
537 | 538 | seriesSpyArg = seriesSpy.takeFirst(); |
|
538 | 539 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); |
|
539 | 540 | QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); |
|
540 | 541 | QVERIFY(seriesSpyArg.at(0).toBool() == false); |
|
541 | 542 | |
|
542 | 543 | setSpyArg = setSpy2.takeFirst(); |
|
543 | 544 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
544 | 545 | QVERIFY(setSpyArg.at(0).toBool() == false); |
|
545 | 546 | } |
|
546 | 547 | |
|
547 | 548 | void tst_QBarSeries::clearWithAnimations() |
|
548 | 549 | { |
|
549 | 550 | QBarSeries* series = new QBarSeries(); |
|
550 | 551 | |
|
551 | 552 | QBarSet* set1 = new QBarSet(QString("set 1")); |
|
552 | 553 | *set1 << 10 << 10 << 10; |
|
553 | 554 | series->append(set1); |
|
554 | 555 | |
|
555 | 556 | QBarSet* set2 = new QBarSet(QString("set 2")); |
|
556 | 557 | *set2 << 10 << 10 << 10; |
|
557 | 558 | series->append(set2); |
|
558 | 559 | |
|
559 | 560 | QChartView view(new QChart()); |
|
560 | 561 | view.resize(400,300); |
|
561 | 562 | view.chart()->setAnimationOptions(QChart::SeriesAnimations); |
|
562 | 563 | view.chart()->addSeries(series); |
|
563 | 564 | view.show(); |
|
564 | 565 | |
|
565 | 566 | series->clear(); |
|
566 | 567 | } |
|
567 | 568 | |
|
569 | void tst_QBarSeries::destruction() | |
|
570 | { | |
|
571 | // add a barset | |
|
572 | QBarSeries *series = new QBarSeries(); | |
|
573 | QBarSet *set = new QBarSet("testset"); | |
|
574 | QSignalSpy spy1(set, SIGNAL(destroyed())); | |
|
575 | series->append(set); | |
|
576 | ||
|
577 | // delete the series | |
|
578 | delete series; | |
|
579 | ||
|
580 | // check that series deletes the set | |
|
581 | QCOMPARE(spy1.count(), 1); | |
|
582 | } | |
|
583 | ||
|
568 | 584 | QTEST_MAIN(tst_QBarSeries) |
|
569 | 585 | |
|
570 | 586 | #include "tst_qbarseries.moc" |
|
571 | 587 |
@@ -1,573 +1,595 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtTest/QtTest> |
|
22 | 22 | #include <qchartview.h> |
|
23 | 23 | #include <qchart.h> |
|
24 | 24 | #include <qpieseries.h> |
|
25 | 25 | #include <qpieslice.h> |
|
26 | 26 | #include <qpiemodelmapper.h> |
|
27 | 27 | #include <QStandardItemModel> |
|
28 | 28 | #include <tst_definitions.h> |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | Q_DECLARE_METATYPE(QPieSlice*) |
|
33 | 33 | Q_DECLARE_METATYPE(QList<QPieSlice*>) |
|
34 | 34 | |
|
35 | 35 | class tst_qpieseries : public QObject |
|
36 | 36 | { |
|
37 | 37 | Q_OBJECT |
|
38 | 38 | |
|
39 | 39 | public slots: |
|
40 | 40 | void initTestCase(); |
|
41 | 41 | void cleanupTestCase(); |
|
42 | 42 | void init(); |
|
43 | 43 | void cleanup(); |
|
44 | 44 | |
|
45 | 45 | private slots: |
|
46 | 46 | void properties(); |
|
47 | 47 | void append(); |
|
48 | 48 | void appendAnimated(); |
|
49 | 49 | void insert(); |
|
50 | 50 | void insertAnimated(); |
|
51 | 51 | void remove(); |
|
52 | 52 | void removeAnimated(); |
|
53 | 53 | void calculatedValues(); |
|
54 | 54 | void clickedSignal(); |
|
55 | 55 | void hoverSignal(); |
|
56 | 56 | void sliceSeries(); |
|
57 | void destruction(); | |
|
57 | 58 | |
|
58 | 59 | private: |
|
59 | 60 | void verifyCalculatedData(const QPieSeries &series, bool *ok); |
|
60 | 61 | |
|
61 | 62 | private: |
|
62 | 63 | QChartView *m_view; |
|
63 | 64 | QPieSeries *m_series; |
|
64 | 65 | }; |
|
65 | 66 | |
|
66 | 67 | void tst_qpieseries::initTestCase() |
|
67 | 68 | { |
|
68 | 69 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); |
|
69 | 70 | qRegisterMetaType<QList<QPieSlice*> >("QList<QPieSlice*>"); |
|
70 | 71 | } |
|
71 | 72 | |
|
72 | 73 | void tst_qpieseries::cleanupTestCase() |
|
73 | 74 | { |
|
74 | 75 | } |
|
75 | 76 | |
|
76 | 77 | void tst_qpieseries::init() |
|
77 | 78 | { |
|
78 | 79 | m_view = new QChartView(); |
|
79 | 80 | m_series = new QPieSeries(m_view); |
|
80 | 81 | } |
|
81 | 82 | |
|
82 | 83 | void tst_qpieseries::cleanup() |
|
83 | 84 | { |
|
84 | 85 | delete m_view; |
|
85 | 86 | m_view = 0; |
|
86 | 87 | m_series = 0; |
|
87 | 88 | } |
|
88 | 89 | |
|
89 | 90 | void tst_qpieseries::properties() |
|
90 | 91 | { |
|
91 | 92 | QSignalSpy countSpy(m_series, SIGNAL(countChanged())); |
|
92 | 93 | QSignalSpy sumSpy(m_series, SIGNAL(sumChanged())); |
|
93 | 94 | |
|
94 | 95 | QVERIFY(m_series->type() == QAbstractSeries::SeriesTypePie); |
|
95 | 96 | QVERIFY(m_series->count() == 0); |
|
96 | 97 | QVERIFY(m_series->isEmpty()); |
|
97 | 98 | QCOMPARE(m_series->sum(), 0.0); |
|
98 | 99 | QCOMPARE(m_series->horizontalPosition(), 0.5); |
|
99 | 100 | QCOMPARE(m_series->verticalPosition(), 0.5); |
|
100 | 101 | QCOMPARE(m_series->pieSize(), 0.7); |
|
101 | 102 | QCOMPARE(m_series->pieStartAngle(), 0.0); |
|
102 | 103 | QCOMPARE(m_series->pieEndAngle(), 360.0); |
|
103 | 104 | |
|
104 | 105 | m_series->append("s1", 1); |
|
105 | 106 | m_series->append("s2", 1); |
|
106 | 107 | m_series->append("s3", 1); |
|
107 | 108 | m_series->insert(1, new QPieSlice("s4", 1)); |
|
108 | 109 | m_series->remove(m_series->slices().first()); |
|
109 | 110 | QCOMPARE(m_series->count(), 3); |
|
110 | 111 | QCOMPARE(m_series->sum(), 3.0); |
|
111 | 112 | m_series->clear(); |
|
112 | 113 | QCOMPARE(m_series->count(), 0); |
|
113 | 114 | QCOMPARE(m_series->sum(), 0.0); |
|
114 | 115 | QCOMPARE(countSpy.count(), 6); |
|
115 | 116 | QCOMPARE(sumSpy.count(), 6); |
|
116 | 117 | |
|
117 | 118 | m_series->setPieSize(-1.0); |
|
118 | 119 | QCOMPARE(m_series->pieSize(), 0.0); |
|
119 | 120 | m_series->setPieSize(0.0); |
|
120 | 121 | m_series->setPieSize(0.9); |
|
121 | 122 | m_series->setPieSize(2.0); |
|
122 | 123 | QCOMPARE(m_series->pieSize(), 1.0); |
|
123 | 124 | |
|
124 | 125 | m_series->setPieSize(0.7); |
|
125 | 126 | QCOMPARE(m_series->pieSize(), 0.7); |
|
126 | 127 | m_series->setDonut(); |
|
127 | 128 | QVERIFY(m_series->donut()); |
|
128 | 129 | |
|
129 | 130 | m_series->setDonutInnerSize(-1.0); |
|
130 | 131 | QCOMPARE(m_series->donutInnerSize(), 0.0); |
|
131 | 132 | m_series->setDonutInnerSize(0.5); |
|
132 | 133 | QCOMPARE(m_series->donutInnerSize(), 0.5); |
|
133 | 134 | |
|
134 | 135 | m_series->setDonutInnerSize(0.8); |
|
135 | 136 | QCOMPARE(m_series->donutInnerSize(), 0.8); |
|
136 | 137 | QCOMPARE(m_series->pieSize(), 0.8); |
|
137 | 138 | |
|
138 | 139 | m_series->setPieSize(0.4); |
|
139 | 140 | QCOMPARE(m_series->pieSize(), 0.4); |
|
140 | 141 | QCOMPARE(m_series->donutInnerSize(), 0.4); |
|
141 | 142 | |
|
142 | 143 | m_series->setPieStartAngle(0); |
|
143 | 144 | m_series->setPieStartAngle(-180); |
|
144 | 145 | m_series->setPieStartAngle(180); |
|
145 | 146 | QCOMPARE(m_series->pieStartAngle(), 180.0); |
|
146 | 147 | |
|
147 | 148 | m_series->setPieEndAngle(360); |
|
148 | 149 | m_series->setPieEndAngle(-180); |
|
149 | 150 | m_series->setPieEndAngle(180); |
|
150 | 151 | QCOMPARE(m_series->pieEndAngle(), 180.0); |
|
151 | 152 | |
|
152 | 153 | m_series->setHorizontalPosition(0.5); |
|
153 | 154 | m_series->setHorizontalPosition(-1.0); |
|
154 | 155 | QCOMPARE(m_series->horizontalPosition(), 0.0); |
|
155 | 156 | m_series->setHorizontalPosition(1.0); |
|
156 | 157 | m_series->setHorizontalPosition(2.0); |
|
157 | 158 | QCOMPARE(m_series->horizontalPosition(), 1.0); |
|
158 | 159 | |
|
159 | 160 | m_series->setVerticalPosition(0.5); |
|
160 | 161 | m_series->setVerticalPosition(-1.0); |
|
161 | 162 | QCOMPARE(m_series->verticalPosition(), 0.0); |
|
162 | 163 | m_series->setVerticalPosition(1.0); |
|
163 | 164 | m_series->setVerticalPosition(2.0); |
|
164 | 165 | QCOMPARE(m_series->verticalPosition(), 1.0); |
|
165 | 166 | } |
|
166 | 167 | |
|
167 | 168 | void tst_qpieseries::append() |
|
168 | 169 | { |
|
169 | 170 | m_view->chart()->addSeries(m_series); |
|
170 | 171 | QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>))); |
|
171 | 172 | |
|
172 | 173 | // append pointer |
|
173 | 174 | QPieSlice *slice1 = 0; |
|
174 | 175 | QVERIFY(!m_series->append(slice1)); |
|
175 | 176 | slice1 = new QPieSlice("slice 1", 1); |
|
176 | 177 | QVERIFY(m_series->append(slice1)); |
|
177 | 178 | QVERIFY(!m_series->append(slice1)); |
|
178 | 179 | QCOMPARE(m_series->count(), 1); |
|
179 | 180 | QCOMPARE(addedSpy.count(), 1); |
|
180 | 181 | QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0)); |
|
181 | 182 | QCOMPARE(added.count(), 1); |
|
182 | 183 | QCOMPARE(added.first(), slice1); |
|
183 | 184 | |
|
184 | 185 | // try to append same slice to another series |
|
185 | 186 | QPieSeries series2; |
|
186 | 187 | QVERIFY(!series2.append(slice1)); |
|
187 | 188 | |
|
188 | 189 | // append pointer list |
|
189 | 190 | QList<QPieSlice *> list; |
|
190 | 191 | QVERIFY(!m_series->append(list)); |
|
191 | 192 | list << (QPieSlice *) 0; |
|
192 | 193 | QVERIFY(!m_series->append(list)); |
|
193 | 194 | list.clear(); |
|
194 | 195 | list << new QPieSlice("slice 2", 2); |
|
195 | 196 | list << new QPieSlice("slice 3", 3); |
|
196 | 197 | QVERIFY(m_series->append(list)); |
|
197 | 198 | QVERIFY(!m_series->append(list)); |
|
198 | 199 | QCOMPARE(m_series->count(), 3); |
|
199 | 200 | QCOMPARE(addedSpy.count(), 2); |
|
200 | 201 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0)); |
|
201 | 202 | QCOMPARE(added.count(), 2); |
|
202 | 203 | QCOMPARE(added, list); |
|
203 | 204 | |
|
204 | 205 | // append operator |
|
205 | 206 | QPieSlice *slice4 = new QPieSlice("slice 4", 4); |
|
206 | 207 | *m_series << slice4; |
|
207 | 208 | *m_series << slice1; // fails because already added |
|
208 | 209 | QCOMPARE(m_series->count(), 4); |
|
209 | 210 | QCOMPARE(addedSpy.count(), 3); |
|
210 | 211 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0)); |
|
211 | 212 | QCOMPARE(added.count(), 1); |
|
212 | 213 | QCOMPARE(added.first(), slice4); |
|
213 | 214 | |
|
214 | 215 | // append with params |
|
215 | 216 | QPieSlice *slice5 = m_series->append("slice 5", 5); |
|
216 | 217 | QVERIFY(slice5 != 0); |
|
217 | 218 | QCOMPARE(slice5->value(), 5.0); |
|
218 | 219 | QCOMPARE(slice5->label(), QString("slice 5")); |
|
219 | 220 | QCOMPARE(m_series->count(), 5); |
|
220 | 221 | QCOMPARE(addedSpy.count(), 4); |
|
221 | 222 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0)); |
|
222 | 223 | QCOMPARE(added.count(), 1); |
|
223 | 224 | QCOMPARE(added.first(), slice5); |
|
224 | 225 | |
|
225 | 226 | // check slices |
|
226 | 227 | QVERIFY(!m_series->isEmpty()); |
|
227 | 228 | for (int i=0; i<m_series->count(); i++) { |
|
228 | 229 | QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1); |
|
229 | 230 | QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
230 | 231 | } |
|
231 | 232 | } |
|
232 | 233 | |
|
233 | 234 | void tst_qpieseries::appendAnimated() |
|
234 | 235 | { |
|
235 | 236 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
236 | 237 | } |
|
237 | 238 | |
|
238 | 239 | void tst_qpieseries::insert() |
|
239 | 240 | { |
|
240 | 241 | m_view->chart()->addSeries(m_series); |
|
241 | 242 | QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>))); |
|
242 | 243 | |
|
243 | 244 | // insert one slice |
|
244 | 245 | QPieSlice *slice1 = 0; |
|
245 | 246 | QVERIFY(!m_series->insert(0, slice1)); |
|
246 | 247 | slice1 = new QPieSlice("slice 1", 1); |
|
247 | 248 | QVERIFY(!m_series->insert(-1, slice1)); |
|
248 | 249 | QVERIFY(!m_series->insert(5, slice1)); |
|
249 | 250 | QVERIFY(m_series->insert(0, slice1)); |
|
250 | 251 | QVERIFY(!m_series->insert(0, slice1)); |
|
251 | 252 | QCOMPARE(m_series->count(), 1); |
|
252 | 253 | QCOMPARE(addedSpy.count(), 1); |
|
253 | 254 | QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0)); |
|
254 | 255 | QCOMPARE(added.count(), 1); |
|
255 | 256 | QCOMPARE(added.first(), slice1); |
|
256 | 257 | |
|
257 | 258 | // try to insert same slice to another series |
|
258 | 259 | QPieSeries series2; |
|
259 | 260 | QVERIFY(!series2.insert(0, slice1)); |
|
260 | 261 | |
|
261 | 262 | // add some more slices |
|
262 | 263 | QPieSlice *slice2 = m_series->append("slice 2", 2); |
|
263 | 264 | QPieSlice *slice4 = m_series->append("slice 4", 4); |
|
264 | 265 | QCOMPARE(m_series->count(), 3); |
|
265 | 266 | QCOMPARE(addedSpy.count(), 3); |
|
266 | 267 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0)); |
|
267 | 268 | QCOMPARE(added.count(), 1); |
|
268 | 269 | QCOMPARE(added.first(), slice2); |
|
269 | 270 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0)); |
|
270 | 271 | QCOMPARE(added.count(), 1); |
|
271 | 272 | QCOMPARE(added.first(), slice4); |
|
272 | 273 | |
|
273 | 274 | // insert between slices |
|
274 | 275 | QPieSlice *slice3 = new QPieSlice("slice 3", 3); |
|
275 | 276 | m_series->insert(2, slice3); |
|
276 | 277 | QCOMPARE(m_series->count(), 4); |
|
277 | 278 | QCOMPARE(addedSpy.count(), 4); |
|
278 | 279 | added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0)); |
|
279 | 280 | QCOMPARE(added.count(), 1); |
|
280 | 281 | QCOMPARE(added.first(), slice3); |
|
281 | 282 | |
|
282 | 283 | // check slices |
|
283 | 284 | for (int i=0; i<m_series->count(); i++) { |
|
284 | 285 | QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1); |
|
285 | 286 | QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
287 | QVERIFY(m_series->slices().at(i)->parent() == m_series); | |
|
286 | 288 | } |
|
287 | 289 | } |
|
288 | 290 | |
|
289 | 291 | void tst_qpieseries::insertAnimated() |
|
290 | 292 | { |
|
291 | 293 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
292 | 294 | } |
|
293 | 295 | |
|
294 | 296 | void tst_qpieseries::remove() |
|
295 | 297 | { |
|
296 | 298 | m_view->chart()->addSeries(m_series); |
|
297 | 299 | QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>))); |
|
298 | 300 | |
|
299 | 301 | // add some slices |
|
300 | 302 | QPieSlice *slice1 = m_series->append("slice 1", 1); |
|
301 | 303 | QPieSlice *slice2 = m_series->append("slice 2", 2); |
|
302 | 304 | QPieSlice *slice3 = m_series->append("slice 3", 3); |
|
303 | 305 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
304 | 306 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); |
|
305 | 307 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); |
|
306 | 308 | QCOMPARE(m_series->count(), 3); |
|
307 | 309 | |
|
308 | 310 | // null pointer remove |
|
309 | 311 | QVERIFY(!m_series->remove(0)); |
|
310 | 312 | |
|
311 | 313 | // remove first |
|
312 | 314 | QVERIFY(m_series->remove(slice1)); |
|
313 | 315 | QVERIFY(!m_series->remove(slice1)); |
|
314 | 316 | QCOMPARE(m_series->count(), 2); |
|
315 | 317 | QCOMPARE(m_series->slices().at(0)->label(), slice2->label()); |
|
316 | 318 | QCOMPARE(removedSpy.count(), 1); |
|
317 | 319 | QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0)); |
|
318 | 320 | QCOMPARE(removed.count(), 1); |
|
319 | 321 | QCOMPARE(removed.first(), slice1); |
|
320 | 322 | |
|
321 | 323 | // remove all |
|
322 | 324 | m_series->clear(); |
|
323 | 325 | QVERIFY(m_series->isEmpty()); |
|
324 | 326 | QVERIFY(m_series->slices().isEmpty()); |
|
325 | 327 | QCOMPARE(m_series->count(), 0); |
|
326 | 328 | QCOMPARE(removedSpy.count(), 2); |
|
327 | 329 | removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(1).at(0)); |
|
328 | 330 | QCOMPARE(removed.count(), 2); |
|
329 | 331 | QCOMPARE(removed.first(), slice2); |
|
330 | 332 | QCOMPARE(removed.last(), slice3); |
|
331 | 333 | |
|
332 | 334 | // check that slices were actually destroyed |
|
333 | 335 | TRY_COMPARE(spy1.count(), 1); |
|
334 | 336 | TRY_COMPARE(spy2.count(), 1); |
|
335 | 337 | TRY_COMPARE(spy3.count(), 1); |
|
336 | 338 | } |
|
337 | 339 | |
|
338 | 340 | void tst_qpieseries::removeAnimated() |
|
339 | 341 | { |
|
340 | 342 | m_view->chart()->setAnimationOptions(QChart::AllAnimations); |
|
341 | 343 | } |
|
342 | 344 | |
|
343 | 345 | void tst_qpieseries::calculatedValues() |
|
344 | 346 | { |
|
345 | 347 | m_view->chart()->addSeries(m_series); |
|
346 | 348 | |
|
347 | 349 | QPieSlice *slice1 = new QPieSlice("slice 1", 1); |
|
348 | 350 | QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged())); |
|
349 | 351 | QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged())); |
|
350 | 352 | QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged())); |
|
351 | 353 | |
|
352 | 354 | // add a slice |
|
353 | 355 | m_series->append(slice1); |
|
354 | 356 | bool ok; |
|
355 | 357 | verifyCalculatedData(*m_series, &ok); |
|
356 | 358 | if (!ok) |
|
357 | 359 | return; |
|
358 | 360 | QCOMPARE(percentageSpy.count(), 1); |
|
359 | 361 | QCOMPARE(startAngleSpy.count(), 0); |
|
360 | 362 | QCOMPARE(angleSpanSpy.count(), 1); |
|
361 | 363 | |
|
362 | 364 | // add some more slices |
|
363 | 365 | QList<QPieSlice *> list; |
|
364 | 366 | list << new QPieSlice("slice 2", 2); |
|
365 | 367 | list << new QPieSlice("slice 3", 3); |
|
366 | 368 | m_series->append(list); |
|
367 | 369 | verifyCalculatedData(*m_series, &ok); |
|
368 | 370 | if (!ok) |
|
369 | 371 | return; |
|
370 | 372 | QCOMPARE(percentageSpy.count(), 2); |
|
371 | 373 | QCOMPARE(startAngleSpy.count(), 0); |
|
372 | 374 | QCOMPARE(angleSpanSpy.count(), 2); |
|
373 | 375 | |
|
374 | 376 | // remove a slice |
|
375 | 377 | m_series->remove(list.first()); // remove slice 2 |
|
376 | 378 | verifyCalculatedData(*m_series, &ok); |
|
377 | 379 | if (!ok) |
|
378 | 380 | return; |
|
379 | 381 | QCOMPARE(percentageSpy.count(), 3); |
|
380 | 382 | QCOMPARE(startAngleSpy.count(), 0); |
|
381 | 383 | QCOMPARE(angleSpanSpy.count(), 3); |
|
382 | 384 | |
|
383 | 385 | // insert a slice |
|
384 | 386 | m_series->insert(0, new QPieSlice("Slice 4", 4)); |
|
385 | 387 | verifyCalculatedData(*m_series, &ok); |
|
386 | 388 | if (!ok) |
|
387 | 389 | return; |
|
388 | 390 | QCOMPARE(percentageSpy.count(), 4); |
|
389 | 391 | QCOMPARE(startAngleSpy.count(), 1); |
|
390 | 392 | QCOMPARE(angleSpanSpy.count(), 4); |
|
391 | 393 | |
|
392 | 394 | // modify pie angles |
|
393 | 395 | m_series->setPieStartAngle(-90); |
|
394 | 396 | m_series->setPieEndAngle(90); |
|
395 | 397 | verifyCalculatedData(*m_series, &ok); |
|
396 | 398 | if (!ok) |
|
397 | 399 | return; |
|
398 | 400 | QCOMPARE(percentageSpy.count(), 4); |
|
399 | 401 | QCOMPARE(startAngleSpy.count(), 3); |
|
400 | 402 | QCOMPARE(angleSpanSpy.count(), 6); |
|
401 | 403 | |
|
402 | 404 | // clear all |
|
403 | 405 | m_series->clear(); |
|
404 | 406 | verifyCalculatedData(*m_series, &ok); |
|
405 | 407 | if (!ok) |
|
406 | 408 | return; |
|
407 | 409 | QCOMPARE(percentageSpy.count(), 4); |
|
408 | 410 | QCOMPARE(startAngleSpy.count(), 3); |
|
409 | 411 | QCOMPARE(angleSpanSpy.count(), 6); |
|
410 | 412 | } |
|
411 | 413 | |
|
412 | 414 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) |
|
413 | 415 | { |
|
414 | 416 | *ok = false; |
|
415 | 417 | |
|
416 | 418 | qreal sum = 0; |
|
417 | 419 | foreach (const QPieSlice *slice, series.slices()) |
|
418 | 420 | sum += slice->value(); |
|
419 | 421 | QCOMPARE(series.sum(), sum); |
|
420 | 422 | |
|
421 | 423 | qreal startAngle = series.pieStartAngle(); |
|
422 | 424 | qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); |
|
423 | 425 | foreach (const QPieSlice *slice, series.slices()) { |
|
424 | 426 | qreal ratio = slice->value() / sum; |
|
425 | 427 | qreal sliceSpan = pieAngleSpan * ratio; |
|
426 | 428 | QCOMPARE(slice->startAngle(), startAngle); |
|
427 | 429 | QCOMPARE(slice->angleSpan(), sliceSpan); |
|
428 | 430 | QCOMPARE(slice->percentage(), ratio); |
|
429 | 431 | startAngle += sliceSpan; |
|
430 | 432 | } |
|
431 | 433 | |
|
432 | 434 | if (!series.isEmpty()) |
|
433 | 435 | QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle()); |
|
434 | 436 | |
|
435 | 437 | *ok = true; |
|
436 | 438 | } |
|
437 | 439 | |
|
438 | 440 | |
|
439 | 441 | void tst_qpieseries::clickedSignal() |
|
440 | 442 | { |
|
441 | 443 | // add some slices |
|
442 | 444 | QPieSlice *s1 = m_series->append("slice 1", 1); |
|
443 | 445 | QPieSlice *s2 = m_series->append("slice 2", 1); |
|
444 | 446 | QPieSlice *s3 = m_series->append("slice 3", 1); |
|
445 | 447 | QPieSlice *s4 = m_series->append("slice 4", 1); |
|
446 | 448 | QSignalSpy clickSpy(m_series, SIGNAL(clicked(QPieSlice*))); |
|
447 | 449 | |
|
448 | 450 | // add series to the chart |
|
449 | 451 | m_view->chart()->legend()->setVisible(false); |
|
450 | 452 | m_view->resize(200, 200); |
|
451 | 453 | m_view->chart()->addSeries(m_series); |
|
452 | 454 | m_view->show(); |
|
453 | 455 | QTest::qWaitForWindowShown(m_view); |
|
454 | 456 | |
|
455 | 457 | // if you devide the chart in four equal tiles these |
|
456 | 458 | // are the center points of those tiles |
|
457 | 459 | QPoint p1(90.25, 90); |
|
458 | 460 | QPoint p2(150, 90); |
|
459 | 461 | QPoint p3(90, 150); |
|
460 | 462 | QPoint p4(150, 150); |
|
461 | 463 | |
|
462 | 464 | QPoint center(120, 120); |
|
463 | 465 | |
|
464 | 466 | m_series->setPieSize(1.0); |
|
465 | 467 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); |
|
466 | 468 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); |
|
467 | 469 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); |
|
468 | 470 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); |
|
469 | 471 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); |
|
470 | 472 | TRY_COMPARE(clickSpy.count(), 5); // all hit |
|
471 | 473 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s4); |
|
472 | 474 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s1); |
|
473 | 475 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3); |
|
474 | 476 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s2); |
|
475 | 477 | clickSpy.clear(); |
|
476 | 478 | |
|
477 | 479 | m_series->setPieSize(0.5); |
|
478 | 480 | m_series->setVerticalPosition(0.25); |
|
479 | 481 | m_series->setHorizontalPosition(0.25); |
|
480 | 482 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); // hits |
|
481 | 483 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); |
|
482 | 484 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); |
|
483 | 485 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); |
|
484 | 486 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); |
|
485 | 487 | TRY_COMPARE(clickSpy.count(), 1); |
|
486 | 488 | clickSpy.clear(); |
|
487 | 489 | |
|
488 | 490 | m_series->setVerticalPosition(0.25); |
|
489 | 491 | m_series->setHorizontalPosition(0.75); |
|
490 | 492 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); |
|
491 | 493 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); // hits |
|
492 | 494 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); |
|
493 | 495 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); |
|
494 | 496 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); |
|
495 | 497 | TRY_COMPARE(clickSpy.count(), 1); |
|
496 | 498 | clickSpy.clear(); |
|
497 | 499 | |
|
498 | 500 | m_series->setVerticalPosition(0.75); |
|
499 | 501 | m_series->setHorizontalPosition(0.25); |
|
500 | 502 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); |
|
501 | 503 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); |
|
502 | 504 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); // hits |
|
503 | 505 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); |
|
504 | 506 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); |
|
505 | 507 | TRY_COMPARE(clickSpy.count(), 1); |
|
506 | 508 | clickSpy.clear(); |
|
507 | 509 | |
|
508 | 510 | m_series->setVerticalPosition(0.75); |
|
509 | 511 | m_series->setHorizontalPosition(0.75); |
|
510 | 512 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); |
|
511 | 513 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); |
|
512 | 514 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); |
|
513 | 515 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); // hits |
|
514 | 516 | QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); |
|
515 | 517 | TRY_COMPARE(clickSpy.count(), 1); |
|
516 | 518 | clickSpy.clear(); |
|
517 | 519 | } |
|
518 | 520 | |
|
519 | 521 | void tst_qpieseries::hoverSignal() |
|
520 | 522 | { |
|
521 | 523 | // add some slices |
|
522 | 524 | m_series->setPieSize(1.0); |
|
523 | 525 | QPieSlice *s1 = m_series->append("slice 1", 1); |
|
524 | 526 | m_series->append("slice 2", 2); |
|
525 | 527 | m_series->append("slice 3", 3); |
|
526 | 528 | |
|
527 | 529 | // add series to the chart |
|
528 | 530 | m_view->chart()->legend()->setVisible(false); |
|
529 | 531 | m_view->resize(200, 200); |
|
530 | 532 | m_view->chart()->addSeries(m_series); |
|
531 | 533 | m_view->show(); |
|
532 | 534 | QTest::qWaitForWindowShown(m_view); |
|
533 | 535 | |
|
534 | 536 | // first move to right top corner |
|
535 | 537 | QTest::mouseMove(m_view->viewport(), QPoint(200, 0)); |
|
536 | 538 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
537 | 539 | |
|
538 | 540 | // move inside the slice |
|
539 | 541 | // pie rectangle: QRectF(60,60 121x121) |
|
540 | 542 | QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool))); |
|
541 | 543 | QTest::mouseMove(m_view->viewport(), QPoint(139, 85)); |
|
542 | 544 | TRY_COMPARE(hoverSpy.count(), 1); |
|
543 | 545 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1); |
|
544 | 546 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true); |
|
545 | 547 | |
|
546 | 548 | // move outside the slice |
|
547 | 549 | QTest::mouseMove(m_view->viewport(), QPoint(200, 0)); |
|
548 | 550 | TRY_COMPARE(hoverSpy.count(), 2); |
|
549 | 551 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1); |
|
550 | 552 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false); |
|
551 | 553 | } |
|
552 | 554 | |
|
553 | 555 | void tst_qpieseries::sliceSeries() |
|
554 | 556 | { |
|
555 | 557 | QPieSlice *slice = new QPieSlice(); |
|
556 | 558 | QVERIFY(!slice->series()); |
|
557 | 559 | delete slice; |
|
558 | 560 | |
|
559 | 561 | slice = new QPieSlice(m_series); |
|
560 | 562 | QVERIFY(!slice->series()); |
|
561 | 563 | |
|
562 | 564 | m_series->append(slice); |
|
563 | 565 | QCOMPARE(slice->series(), m_series); |
|
564 | 566 | |
|
565 | 567 | slice = new QPieSlice(); |
|
566 | 568 | m_series->insert(0, slice); |
|
567 | 569 | QCOMPARE(slice->series(), m_series); |
|
568 | 570 | } |
|
569 | 571 | |
|
572 | void tst_qpieseries::destruction() | |
|
573 | { | |
|
574 | // add some slices | |
|
575 | QPieSlice *slice1 = m_series->append("slice 1", 1); | |
|
576 | QPieSlice *slice2 = m_series->append("slice 2", 2); | |
|
577 | QPieSlice *slice3 = m_series->append("slice 3", 3); | |
|
578 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); | |
|
579 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); | |
|
580 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); | |
|
581 | ||
|
582 | // destroy series | |
|
583 | delete m_series; | |
|
584 | m_series = 0; | |
|
585 | ||
|
586 | // check that series has destroyed its slices | |
|
587 | QCOMPARE(spy1.count(), 1); | |
|
588 | QCOMPARE(spy2.count(), 1); | |
|
589 | QCOMPARE(spy3.count(), 1); | |
|
590 | } | |
|
591 | ||
|
570 | 592 | QTEST_MAIN(tst_qpieseries) |
|
571 | 593 | |
|
572 | 594 | #include "tst_qpieseries.moc" |
|
573 | 595 |
General Comments 0
You need to be logged in to leave comments.
Login now