##// END OF EJS Templates
Fix pie hover tests in ubuntu
Jani Honkonen -
r2074:36a8d99cbda3
parent child
Show More
@@ -1,648 +1,649
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 take();
54 54 void takeAnimated();
55 55 void calculatedValues();
56 56 void clickedSignal();
57 57 void hoverSignal();
58 58 void sliceSeries();
59 59 void destruction();
60 60
61 61 private:
62 62 void verifyCalculatedData(const QPieSeries &series, bool *ok);
63 63 QList<QPoint> slicePoints(QRectF rect);
64 64
65 65 private:
66 66 QChartView *m_view;
67 67 QPieSeries *m_series;
68 68 };
69 69
70 70 void tst_qpieseries::initTestCase()
71 71 {
72 72 qRegisterMetaType<QPieSlice*>("QPieSlice*");
73 73 qRegisterMetaType<QList<QPieSlice*> >("QList<QPieSlice*>");
74 74 }
75 75
76 76 void tst_qpieseries::cleanupTestCase()
77 77 {
78 78 }
79 79
80 80 void tst_qpieseries::init()
81 81 {
82 82 m_view = new QChartView();
83 83 m_series = new QPieSeries(m_view);
84 84 m_view->show();
85 85 QTest::qWaitForWindowShown(m_view);
86 86
87 87 }
88 88
89 89 void tst_qpieseries::cleanup()
90 90 {
91 91 delete m_view;
92 92 m_view = 0;
93 93 m_series = 0;
94 94 }
95 95
96 96 void tst_qpieseries::properties()
97 97 {
98 98 QSignalSpy countSpy(m_series, SIGNAL(countChanged()));
99 99 QSignalSpy sumSpy(m_series, SIGNAL(sumChanged()));
100 100 QSignalSpy opacitySpy(m_series, SIGNAL(opacityChanged()));
101 101
102 102 QVERIFY(m_series->type() == QAbstractSeries::SeriesTypePie);
103 103 QVERIFY(m_series->count() == 0);
104 104 QVERIFY(m_series->isEmpty());
105 105 QCOMPARE(m_series->sum(), 0.0);
106 106 QCOMPARE(m_series->horizontalPosition(), 0.5);
107 107 QCOMPARE(m_series->verticalPosition(), 0.5);
108 108 QCOMPARE(m_series->pieSize(), 0.7);
109 109 QCOMPARE(m_series->pieStartAngle(), 0.0);
110 110 QCOMPARE(m_series->pieEndAngle(), 360.0);
111 111 QCOMPARE(m_series->opacity(), 1.0);
112 112
113 113 m_series->append("s1", 1);
114 114 m_series->append("s2", 1);
115 115 m_series->append("s3", 1);
116 116 m_series->insert(1, new QPieSlice("s4", 1));
117 117 m_series->remove(m_series->slices().first());
118 118 QCOMPARE(m_series->count(), 3);
119 119 QCOMPARE(m_series->sum(), 3.0);
120 120 m_series->clear();
121 121 QCOMPARE(m_series->count(), 0);
122 122 QCOMPARE(m_series->sum(), 0.0);
123 123 QCOMPARE(countSpy.count(), 6);
124 124 QCOMPARE(sumSpy.count(), 6);
125 125
126 126 m_series->setPieSize(-1.0);
127 127 QCOMPARE(m_series->pieSize(), 0.0);
128 128 m_series->setPieSize(0.0);
129 129 m_series->setPieSize(0.9);
130 130 m_series->setPieSize(2.0);
131 131 QCOMPARE(m_series->pieSize(), 1.0);
132 132
133 133 m_series->setPieSize(0.7);
134 134 QCOMPARE(m_series->pieSize(), 0.7);
135 135
136 136 m_series->setHoleSize(-1.0);
137 137 QCOMPARE(m_series->holeSize(), 0.0);
138 138 m_series->setHoleSize(0.5);
139 139 QCOMPARE(m_series->holeSize(), 0.5);
140 140
141 141 m_series->setHoleSize(0.8);
142 142 QCOMPARE(m_series->holeSize(), 0.8);
143 143 QCOMPARE(m_series->pieSize(), 0.8);
144 144
145 145 m_series->setPieSize(0.4);
146 146 QCOMPARE(m_series->pieSize(), 0.4);
147 147 QCOMPARE(m_series->holeSize(), 0.4);
148 148
149 149 m_series->setPieStartAngle(0);
150 150 m_series->setPieStartAngle(-180);
151 151 m_series->setPieStartAngle(180);
152 152 QCOMPARE(m_series->pieStartAngle(), 180.0);
153 153
154 154 m_series->setPieEndAngle(360);
155 155 m_series->setPieEndAngle(-180);
156 156 m_series->setPieEndAngle(180);
157 157 QCOMPARE(m_series->pieEndAngle(), 180.0);
158 158
159 159 m_series->setHorizontalPosition(0.5);
160 160 m_series->setHorizontalPosition(-1.0);
161 161 QCOMPARE(m_series->horizontalPosition(), 0.0);
162 162 m_series->setHorizontalPosition(1.0);
163 163 m_series->setHorizontalPosition(2.0);
164 164 QCOMPARE(m_series->horizontalPosition(), 1.0);
165 165
166 166 m_series->setVerticalPosition(0.5);
167 167 m_series->setVerticalPosition(-1.0);
168 168 QCOMPARE(m_series->verticalPosition(), 0.0);
169 169 m_series->setVerticalPosition(1.0);
170 170 m_series->setVerticalPosition(2.0);
171 171 QCOMPARE(m_series->verticalPosition(), 1.0);
172 172
173 173 m_series->setOpacity(0.5);
174 174 QCOMPARE(m_series->opacity(), 0.5);
175 175 QCOMPARE(opacitySpy.count(), 1);
176 176 m_series->setOpacity(0.0);
177 177 QCOMPARE(m_series->opacity(), 0.0);
178 178 QCOMPARE(opacitySpy.count(), 2);
179 179 m_series->setOpacity(1.0);
180 180 QCOMPARE(m_series->opacity(), 1.0);
181 181 QCOMPARE(opacitySpy.count(), 3);
182 182 }
183 183
184 184 void tst_qpieseries::append()
185 185 {
186 186 m_view->chart()->addSeries(m_series);
187 187 QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>)));
188 188
189 189 // append pointer
190 190 QPieSlice *slice1 = 0;
191 191 QVERIFY(!m_series->append(slice1));
192 192 slice1 = new QPieSlice("slice 1", 1);
193 193 QVERIFY(m_series->append(slice1));
194 194 QVERIFY(!m_series->append(slice1));
195 195 QCOMPARE(m_series->count(), 1);
196 196 QCOMPARE(addedSpy.count(), 1);
197 197 QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0));
198 198 QCOMPARE(added.count(), 1);
199 199 QCOMPARE(added.first(), slice1);
200 200
201 201 // try to append same slice to another series
202 202 QPieSeries series2;
203 203 QVERIFY(!series2.append(slice1));
204 204
205 205 // append pointer list
206 206 QList<QPieSlice *> list;
207 207 QVERIFY(!m_series->append(list));
208 208 list << (QPieSlice *) 0;
209 209 QVERIFY(!m_series->append(list));
210 210 list.clear();
211 211 list << new QPieSlice("slice 2", 2);
212 212 list << new QPieSlice("slice 3", 3);
213 213 QVERIFY(m_series->append(list));
214 214 QVERIFY(!m_series->append(list));
215 215 QCOMPARE(m_series->count(), 3);
216 216 QCOMPARE(addedSpy.count(), 2);
217 217 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0));
218 218 QCOMPARE(added.count(), 2);
219 219 QCOMPARE(added, list);
220 220
221 221 // append operator
222 222 QPieSlice *slice4 = new QPieSlice("slice 4", 4);
223 223 *m_series << slice4;
224 224 *m_series << slice1; // fails because already added
225 225 QCOMPARE(m_series->count(), 4);
226 226 QCOMPARE(addedSpy.count(), 3);
227 227 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0));
228 228 QCOMPARE(added.count(), 1);
229 229 QCOMPARE(added.first(), slice4);
230 230
231 231 // append with params
232 232 QPieSlice *slice5 = m_series->append("slice 5", 5);
233 233 QVERIFY(slice5 != 0);
234 234 QCOMPARE(slice5->value(), 5.0);
235 235 QCOMPARE(slice5->label(), QString("slice 5"));
236 236 QCOMPARE(m_series->count(), 5);
237 237 QCOMPARE(addedSpy.count(), 4);
238 238 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0));
239 239 QCOMPARE(added.count(), 1);
240 240 QCOMPARE(added.first(), slice5);
241 241
242 242 // check slices
243 243 QVERIFY(!m_series->isEmpty());
244 244 for (int i=0; i<m_series->count(); i++) {
245 245 QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1);
246 246 QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1));
247 247 }
248 248 }
249 249
250 250 void tst_qpieseries::appendAnimated()
251 251 {
252 252 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
253 253 append();
254 254 }
255 255
256 256 void tst_qpieseries::insert()
257 257 {
258 258 m_view->chart()->addSeries(m_series);
259 259 QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>)));
260 260
261 261 // insert one slice
262 262 QPieSlice *slice1 = 0;
263 263 QVERIFY(!m_series->insert(0, slice1));
264 264 slice1 = new QPieSlice("slice 1", 1);
265 265 QVERIFY(!m_series->insert(-1, slice1));
266 266 QVERIFY(!m_series->insert(5, slice1));
267 267 QVERIFY(m_series->insert(0, slice1));
268 268 QVERIFY(!m_series->insert(0, slice1));
269 269 QCOMPARE(m_series->count(), 1);
270 270 QCOMPARE(addedSpy.count(), 1);
271 271 QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0));
272 272 QCOMPARE(added.count(), 1);
273 273 QCOMPARE(added.first(), slice1);
274 274
275 275 // try to insert same slice to another series
276 276 QPieSeries series2;
277 277 QVERIFY(!series2.insert(0, slice1));
278 278
279 279 // add some more slices
280 280 QPieSlice *slice2 = m_series->append("slice 2", 2);
281 281 QPieSlice *slice4 = m_series->append("slice 4", 4);
282 282 QCOMPARE(m_series->count(), 3);
283 283 QCOMPARE(addedSpy.count(), 3);
284 284 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0));
285 285 QCOMPARE(added.count(), 1);
286 286 QCOMPARE(added.first(), slice2);
287 287 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0));
288 288 QCOMPARE(added.count(), 1);
289 289 QCOMPARE(added.first(), slice4);
290 290
291 291 // insert between slices
292 292 QPieSlice *slice3 = new QPieSlice("slice 3", 3);
293 293 m_series->insert(2, slice3);
294 294 QCOMPARE(m_series->count(), 4);
295 295 QCOMPARE(addedSpy.count(), 4);
296 296 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0));
297 297 QCOMPARE(added.count(), 1);
298 298 QCOMPARE(added.first(), slice3);
299 299
300 300 // check slices
301 301 for (int i=0; i<m_series->count(); i++) {
302 302 QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1);
303 303 QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1));
304 304 QVERIFY(m_series->slices().at(i)->parent() == m_series);
305 305 }
306 306 }
307 307
308 308 void tst_qpieseries::insertAnimated()
309 309 {
310 310 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
311 311 insert();
312 312 }
313 313
314 314 void tst_qpieseries::remove()
315 315 {
316 316 m_view->chart()->addSeries(m_series);
317 317 QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>)));
318 318
319 319 // add some slices
320 320 QPieSlice *slice1 = m_series->append("slice 1", 1);
321 321 QPieSlice *slice2 = m_series->append("slice 2", 2);
322 322 QPieSlice *slice3 = m_series->append("slice 3", 3);
323 323 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
324 324 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
325 325 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
326 326 QCOMPARE(m_series->count(), 3);
327 327
328 328 // null pointer remove
329 329 QVERIFY(!m_series->remove(0));
330 330
331 331 // remove first
332 332 QVERIFY(m_series->remove(slice1));
333 333 QVERIFY(!m_series->remove(slice1));
334 334 QCOMPARE(m_series->count(), 2);
335 335 QCOMPARE(m_series->slices().at(0)->label(), slice2->label());
336 336 QCOMPARE(removedSpy.count(), 1);
337 337 QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0));
338 338 QCOMPARE(removed.count(), 1);
339 339 QCOMPARE(removed.first(), slice1);
340 340
341 341 // remove all
342 342 m_series->clear();
343 343 QVERIFY(m_series->isEmpty());
344 344 QVERIFY(m_series->slices().isEmpty());
345 345 QCOMPARE(m_series->count(), 0);
346 346 QCOMPARE(removedSpy.count(), 2);
347 347 removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(1).at(0));
348 348 QCOMPARE(removed.count(), 2);
349 349 QCOMPARE(removed.first(), slice2);
350 350 QCOMPARE(removed.last(), slice3);
351 351
352 352 // check that slices were actually destroyed
353 353 TRY_COMPARE(spy1.count(), 1);
354 354 TRY_COMPARE(spy2.count(), 1);
355 355 TRY_COMPARE(spy3.count(), 1);
356 356 }
357 357
358 358 void tst_qpieseries::removeAnimated()
359 359 {
360 360 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
361 361 remove();
362 362 }
363 363
364 364 void tst_qpieseries::take()
365 365 {
366 366 m_view->chart()->addSeries(m_series);
367 367 QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>)));
368 368
369 369 // add some slices
370 370 QPieSlice *slice1 = m_series->append("slice 1", 1);
371 371 QPieSlice *slice2 = m_series->append("slice 2", 2);
372 372 m_series->append("slice 3", 3);
373 373 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
374 374 QCOMPARE(m_series->count(), 3);
375 375
376 376 // null pointer remove
377 377 QVERIFY(!m_series->take(0));
378 378
379 379 // take first
380 380 QVERIFY(m_series->take(slice1));
381 381 TRY_COMPARE(spy1.count(), 0);
382 382 QVERIFY(slice1->parent() == m_series); // series is still the parent object
383 383 QVERIFY(!m_series->take(slice1));
384 384 QCOMPARE(m_series->count(), 2);
385 385 QCOMPARE(m_series->slices().at(0)->label(), slice2->label());
386 386 QCOMPARE(removedSpy.count(), 1);
387 387 QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0));
388 388 QCOMPARE(removed.count(), 1);
389 389 QCOMPARE(removed.first(), slice1);
390 390 }
391 391
392 392 void tst_qpieseries::takeAnimated()
393 393 {
394 394 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
395 395 take();
396 396 }
397 397
398 398 void tst_qpieseries::calculatedValues()
399 399 {
400 400 m_view->chart()->addSeries(m_series);
401 401
402 402 QPieSlice *slice1 = new QPieSlice("slice 1", 1);
403 403 QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged()));
404 404 QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged()));
405 405 QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged()));
406 406
407 407 // add a slice
408 408 m_series->append(slice1);
409 409 bool ok;
410 410 verifyCalculatedData(*m_series, &ok);
411 411 if (!ok)
412 412 return;
413 413 QCOMPARE(percentageSpy.count(), 1);
414 414 QCOMPARE(startAngleSpy.count(), 0);
415 415 QCOMPARE(angleSpanSpy.count(), 1);
416 416
417 417 // add some more slices
418 418 QList<QPieSlice *> list;
419 419 list << new QPieSlice("slice 2", 2);
420 420 list << new QPieSlice("slice 3", 3);
421 421 m_series->append(list);
422 422 verifyCalculatedData(*m_series, &ok);
423 423 if (!ok)
424 424 return;
425 425 QCOMPARE(percentageSpy.count(), 2);
426 426 QCOMPARE(startAngleSpy.count(), 0);
427 427 QCOMPARE(angleSpanSpy.count(), 2);
428 428
429 429 // remove a slice
430 430 m_series->remove(list.first()); // remove slice 2
431 431 verifyCalculatedData(*m_series, &ok);
432 432 if (!ok)
433 433 return;
434 434 QCOMPARE(percentageSpy.count(), 3);
435 435 QCOMPARE(startAngleSpy.count(), 0);
436 436 QCOMPARE(angleSpanSpy.count(), 3);
437 437
438 438 // insert a slice
439 439 m_series->insert(0, new QPieSlice("Slice 4", 4));
440 440 verifyCalculatedData(*m_series, &ok);
441 441 if (!ok)
442 442 return;
443 443 QCOMPARE(percentageSpy.count(), 4);
444 444 QCOMPARE(startAngleSpy.count(), 1);
445 445 QCOMPARE(angleSpanSpy.count(), 4);
446 446
447 447 // modify pie angles
448 448 m_series->setPieStartAngle(-90);
449 449 m_series->setPieEndAngle(90);
450 450 verifyCalculatedData(*m_series, &ok);
451 451 if (!ok)
452 452 return;
453 453 QCOMPARE(percentageSpy.count(), 4);
454 454 QCOMPARE(startAngleSpy.count(), 3);
455 455 QCOMPARE(angleSpanSpy.count(), 6);
456 456
457 457 // clear all
458 458 m_series->clear();
459 459 verifyCalculatedData(*m_series, &ok);
460 460 if (!ok)
461 461 return;
462 462 QCOMPARE(percentageSpy.count(), 4);
463 463 QCOMPARE(startAngleSpy.count(), 3);
464 464 QCOMPARE(angleSpanSpy.count(), 6);
465 465 }
466 466
467 467 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
468 468 {
469 469 *ok = false;
470 470
471 471 qreal sum = 0;
472 472 foreach (const QPieSlice *slice, series.slices())
473 473 sum += slice->value();
474 474 QCOMPARE(series.sum(), sum);
475 475
476 476 qreal startAngle = series.pieStartAngle();
477 477 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
478 478 foreach (const QPieSlice *slice, series.slices()) {
479 479 qreal ratio = slice->value() / sum;
480 480 qreal sliceSpan = pieAngleSpan * ratio;
481 481 QCOMPARE(slice->startAngle(), startAngle);
482 482 QCOMPARE(slice->angleSpan(), sliceSpan);
483 483 QCOMPARE(slice->percentage(), ratio);
484 484 startAngle += sliceSpan;
485 485 }
486 486
487 487 if (!series.isEmpty())
488 488 QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle());
489 489
490 490 *ok = true;
491 491 }
492 492
493 493 void tst_qpieseries::clickedSignal()
494 494 {
495 495 // NOTE:
496 496 // This test is the same as tst_qpieslice::clickedSignal()
497 497 // Just for different signals.
498 498
499 499 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
500 500
501 501 // add some slices
502 502 QPieSlice *s1 = m_series->append("slice 1", 1);
503 503 QPieSlice *s2 = m_series->append("slice 2", 1);
504 504 QPieSlice *s3 = m_series->append("slice 3", 1);
505 505 QPieSlice *s4 = m_series->append("slice 4", 1);
506 506 QSignalSpy clickSpy(m_series, SIGNAL(clicked(QPieSlice*)));
507 507
508 508 // add series to the chart
509 509 m_view->chart()->legend()->setVisible(false);
510 510 m_view->chart()->addSeries(m_series);
511 511 m_view->show();
512 512 QTest::qWaitForWindowShown(m_view);
513 513
514 514 // test maximum size
515 515 m_series->setPieSize(1.0);
516 516 QRectF pieRect = m_view->chart()->plotArea();
517 517 QList<QPoint> points = slicePoints(pieRect);
518 518 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0));
519 519 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1));
520 520 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2));
521 521 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3));
522 522 TRY_COMPARE(clickSpy.count(), 4);
523 523 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s1);
524 524 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2);
525 525 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
526 526 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4);
527 527 clickSpy.clear();
528 528
529 529 // test half size
530 530 m_series->setPieSize(0.5);
531 531 m_series->setVerticalPosition(0.25);
532 532 m_series->setHorizontalPosition(0.25);
533 533 pieRect = QRectF(m_view->chart()->plotArea().topLeft(), m_view->chart()->plotArea().center());
534 534 points = slicePoints(pieRect);
535 535 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0));
536 536 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1));
537 537 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2));
538 538 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3));
539 539 TRY_COMPARE(clickSpy.count(), 4);
540 540 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s1);
541 541 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2);
542 542 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
543 543 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4);
544 544 }
545 545
546 546 void tst_qpieseries::hoverSignal()
547 547 {
548 548 // NOTE:
549 549 // This test is the same as tst_qpieslice::hoverSignal()
550 550 // Just for different signals.
551 551
552 552 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
553 553
554 554 // add some slices
555 555 m_series->append("slice 1", 1);
556 556 m_series->append("slice 2", 1);
557 557 m_series->append("slice 3", 1);
558 558 m_series->append("slice 4", 1);
559 559
560 560 // add series to the chart
561 561 m_view->chart()->legend()->setVisible(false);
562 562 m_view->chart()->addSeries(m_series);
563 563 m_view->show();
564 564 QTest::qWaitForWindowShown(m_view);
565 565
566 566 // move inside the slices
567 567 m_series->setPieSize(1.0);
568 568 QRectF pieRect = m_view->chart()->plotArea();
569 569 QList<QPoint> points = slicePoints(pieRect);
570 570 QTest::mouseMove(m_view->viewport(), pieRect.topRight().toPoint());
571 571 QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool)));
572 572 QTest::mouseMove(m_view->viewport(), points.at(0));
573 573 QTest::mouseMove(m_view->viewport(), points.at(1));
574 574 QTest::mouseMove(m_view->viewport(), points.at(2));
575 575 QTest::mouseMove(m_view->viewport(), points.at(3));
576 576 QTest::mouseMove(m_view->viewport(), pieRect.topLeft().toPoint());
577 QApplication::processEvents();
577 578
578 579 // check
579 580 QCOMPARE(hoverSpy.count(), 8);
580 581 int i = 0;
581 582 foreach (QPieSlice *s, m_series->slices()) {
582 583 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s);
583 584 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), true);
584 585 i++;
585 586 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s);
586 587 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), false);
587 588 i++;
588 589 }
589 590 }
590 591
591 592 void tst_qpieseries::sliceSeries()
592 593 {
593 594 QPieSlice *slice = new QPieSlice();
594 595 QVERIFY(!slice->series());
595 596 delete slice;
596 597
597 598 slice = new QPieSlice(m_series);
598 599 QVERIFY(!slice->series());
599 600
600 601 m_series->append(slice);
601 602 QCOMPARE(slice->series(), m_series);
602 603
603 604 slice = new QPieSlice();
604 605 m_series->insert(0, slice);
605 606 QCOMPARE(slice->series(), m_series);
606 607
607 608 m_series->take(slice);
608 609 QCOMPARE(slice->series(), (QPieSeries*) 0);
609 610 }
610 611
611 612 void tst_qpieseries::destruction()
612 613 {
613 614 // add some slices
614 615 QPieSlice *slice1 = m_series->append("slice 1", 1);
615 616 QPieSlice *slice2 = m_series->append("slice 2", 2);
616 617 QPieSlice *slice3 = m_series->append("slice 3", 3);
617 618 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
618 619 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
619 620 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
620 621
621 622 // destroy series
622 623 delete m_series;
623 624 m_series = 0;
624 625
625 626 // check that series has destroyed its slices
626 627 QCOMPARE(spy1.count(), 1);
627 628 QCOMPARE(spy2.count(), 1);
628 629 QCOMPARE(spy3.count(), 1);
629 630 }
630 631
631 632 QList<QPoint> tst_qpieseries::slicePoints(QRectF rect)
632 633 {
633 634 qreal x1 = rect.topLeft().x() + (rect.width() / 4);
634 635 qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3;
635 636 qreal y1 = rect.topLeft().y() + (rect.height() / 4);
636 637 qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3;
637 638 QList<QPoint> points;
638 639 points << QPoint(x2, y1);
639 640 points << QPoint(x2, y2);
640 641 points << QPoint(x1, y2);
641 642 points << QPoint(x1, y1);
642 643 return points;
643 644 }
644 645
645 646 QTEST_MAIN(tst_qpieseries)
646 647
647 648 #include "tst_qpieseries.moc"
648 649
@@ -1,331 +1,332
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 <tst_definitions.h>
23 23 #include <qchartview.h>
24 24 #include <qchart.h>
25 25 #include <qpieslice.h>
26 26 #include <qpieseries.h>
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 class tst_qpieslice : public QObject
31 31 {
32 32 Q_OBJECT
33 33
34 34 public slots:
35 35 void initTestCase();
36 36 void cleanupTestCase();
37 37 void init();
38 38 void cleanup();
39 39
40 40 private slots:
41 41 void construction();
42 42 void changedSignals();
43 43 void customize();
44 44 void clickedSignal();
45 45 void hoverSignal();
46 46
47 47 private:
48 48 QList<QPoint> slicePoints(QRectF rect);
49 49
50 50 private:
51 51
52 52 };
53 53
54 54 void tst_qpieslice::initTestCase()
55 55 {
56 56 }
57 57
58 58 void tst_qpieslice::cleanupTestCase()
59 59 {
60 60 }
61 61
62 62 void tst_qpieslice::init()
63 63 {
64 64
65 65 }
66 66
67 67 void tst_qpieslice::cleanup()
68 68 {
69 69
70 70 }
71 71
72 72 void tst_qpieslice::construction()
73 73 {
74 74 // no params
75 75 QPieSlice slice1;
76 76 QCOMPARE(slice1.value(), 0.0);
77 77 QVERIFY(slice1.label().isEmpty());
78 78 QVERIFY(!slice1.isLabelVisible());
79 79 QVERIFY(!slice1.isExploded());
80 80 QCOMPARE(slice1.pen(), QPen());
81 81 QCOMPARE(slice1.brush(), QBrush());
82 82 QCOMPARE(slice1.labelBrush(), QBrush());
83 83 QCOMPARE(slice1.labelFont(), QFont());
84 84 QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value
85 85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
86 86 QCOMPARE(slice1.percentage(), 0.0);
87 87 QCOMPARE(slice1.startAngle(), 0.0);
88 88 QCOMPARE(slice1.angleSpan(), 0.0);
89 89
90 90 // value and label params
91 91 QPieSlice slice2("foobar", 1.0);
92 92 QCOMPARE(slice2.value(), 1.0);
93 93 QCOMPARE(slice2.label(), QString("foobar"));
94 94 QVERIFY(!slice2.isLabelVisible());
95 95 QVERIFY(!slice2.isExploded());
96 96 QCOMPARE(slice2.pen(), QPen());
97 97 QCOMPARE(slice2.brush(), QBrush());
98 98 QCOMPARE(slice2.labelBrush(), QBrush());
99 99 QCOMPARE(slice2.labelFont(), QFont());
100 100 QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value
101 101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
102 102 QCOMPARE(slice2.percentage(), 0.0);
103 103 QCOMPARE(slice2.startAngle(), 0.0);
104 104 QCOMPARE(slice2.angleSpan(), 0.0);
105 105 }
106 106
107 107 void tst_qpieslice::changedSignals()
108 108 {
109 109 QPieSlice slice;
110 110
111 111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
112 112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
113 113 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
114 114 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
115 115 QSignalSpy labelBrushSpy(&slice, SIGNAL(labelBrushChanged()));
116 116 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
117 117 QSignalSpy colorSpy(&slice, SIGNAL(colorChanged()));
118 118 QSignalSpy borderColorSpy(&slice, SIGNAL(borderColorChanged()));
119 119 QSignalSpy borderWidthSpy(&slice, SIGNAL(borderWidthChanged()));
120 120 QSignalSpy labelColorSpy(&slice, SIGNAL(labelColorChanged()));
121 121
122 122 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
123 123
124 124 // set everything twice to see we do not get unnecessary signals
125 125 slice.setValue(1.0);
126 126 slice.setValue(-1.0);
127 127 QCOMPARE(slice.value(), 1.0);
128 128 slice.setLabel("foobar");
129 129 slice.setLabel("foobar");
130 130 slice.setLabelVisible();
131 131 slice.setLabelVisible();
132 132 slice.setExploded();
133 133 slice.setExploded();
134 134 slice.setPen(QPen(Qt::red));
135 135 slice.setPen(QPen(QBrush(Qt::red), 3));
136 136 slice.setBrush(QBrush(Qt::red));
137 137 slice.setBrush(QBrush(Qt::red));
138 138 slice.setLabelBrush(QBrush(Qt::green));
139 139 slice.setLabelBrush(QBrush(Qt::green));
140 140 slice.setLabelFont(QFont("Tahoma"));
141 141 slice.setLabelFont(QFont("Tahoma"));
142 142 slice.setLabelPosition(QPieSlice::LabelInsideHorizontal);
143 143 slice.setLabelPosition(QPieSlice::LabelInsideHorizontal);
144 144 slice.setLabelArmLengthFactor(0.1);
145 145 slice.setLabelArmLengthFactor(0.1);
146 146 slice.setExplodeDistanceFactor(0.1);
147 147 slice.setExplodeDistanceFactor(0.1);
148 148
149 149 TRY_COMPARE(valueSpy.count(), 1);
150 150 TRY_COMPARE(labelSpy.count(), 1);
151 151 TRY_COMPARE(penSpy.count(), 2);
152 152 TRY_COMPARE(brushSpy.count(), 1);
153 153 TRY_COMPARE(labelBrushSpy.count(), 1);
154 154 TRY_COMPARE(labelFontSpy.count(), 1);
155 155 TRY_COMPARE(colorSpy.count(), 1);
156 156 TRY_COMPARE(borderColorSpy.count(), 1);
157 157 TRY_COMPARE(borderWidthSpy.count(), 1);
158 158 TRY_COMPARE(labelColorSpy.count(), 1);
159 159 }
160 160
161 161 void tst_qpieslice::customize()
162 162 {
163 163 // create a pie series
164 164 QPieSeries *series = new QPieSeries();
165 165 QPieSlice *s1 = series->append("slice 1", 1);
166 166 QPieSlice *s2 = series->append("slice 2", 2);
167 167 series->append("slice 3", 3);
168 168
169 169 // customize a slice
170 170 QPen p1(Qt::red);
171 171 s1->setPen(p1);
172 172 QBrush b1(Qt::red);
173 173 s1->setBrush(b1);
174 174 s1->setLabelBrush(b1);
175 175 QFont f1("Consolas");
176 176 s1->setLabelFont(f1);
177 177
178 178 // add series to the chart
179 179 QChartView view(new QChart());
180 180 view.resize(200, 200);
181 181 view.chart()->addSeries(series);
182 182 view.show();
183 183 QTest::qWaitForWindowShown(&view);
184 184 //QTest::qWait(1000);
185 185
186 186 // check that customizations persist
187 187 QCOMPARE(s1->pen(), p1);
188 188 QCOMPARE(s1->brush(), b1);
189 189 QCOMPARE(s1->labelBrush(), b1);
190 190 QCOMPARE(s1->labelFont(), f1);
191 191
192 192 // remove a slice
193 193 series->remove(s2);
194 194 QCOMPARE(s1->pen(), p1);
195 195 QCOMPARE(s1->brush(), b1);
196 196 QCOMPARE(s1->labelBrush(), b1);
197 197 QCOMPARE(s1->labelFont(), f1);
198 198
199 199 // add a slice
200 200 series->append("slice 4", 4);
201 201 QCOMPARE(s1->pen(), p1);
202 202 QCOMPARE(s1->brush(), b1);
203 203 QCOMPARE(s1->labelBrush(), b1);
204 204 QCOMPARE(s1->labelFont(), f1);
205 205
206 206 // insert a slice
207 207 series->insert(0, new QPieSlice("slice 5", 5));
208 208 QCOMPARE(s1->pen(), p1);
209 209 QCOMPARE(s1->brush(), b1);
210 210 QCOMPARE(s1->labelBrush(), b1);
211 211 QCOMPARE(s1->labelFont(), f1);
212 212
213 213 // change theme
214 214 // theme will overwrite customizations
215 215 view.chart()->setTheme(QChart::ChartThemeHighContrast);
216 216 QVERIFY(s1->pen() != p1);
217 217 QVERIFY(s1->brush() != b1);
218 218 QVERIFY(s1->labelBrush() != b1);
219 219 QVERIFY(s1->labelFont() != f1);
220 220 }
221 221
222 222 void tst_qpieslice::clickedSignal()
223 223 {
224 224 // NOTE:
225 225 // This test is the same as tst_qpieseries::clickedSignal()
226 226 // Just for different signals.
227 227
228 228 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
229 229
230 230 // create a pie series
231 231 QPieSeries *series = new QPieSeries();
232 232 QPieSlice *s1 = series->append("slice 1", 1);
233 233 QPieSlice *s2 = series->append("slice 2", 1);
234 234 QPieSlice *s3 = series->append("slice 3", 1);
235 235 QPieSlice *s4 = series->append("slice 4", 1);
236 236 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
237 237 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
238 238 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
239 239 QSignalSpy clickSpy4(s4, SIGNAL(clicked()));
240 240
241 241 // add series to the chart
242 242 QChartView view;
243 243 view.chart()->legend()->setVisible(false);
244 244 view.chart()->addSeries(series);
245 245 view.show();
246 246 QTest::qWaitForWindowShown(&view);
247 247
248 248 // simulate clicks
249 249 series->setPieSize(1.0);
250 250 QRectF pieRect = view.chart()->plotArea();
251 251 QList<QPoint> points = slicePoints(pieRect);
252 252 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(0));
253 253 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(1));
254 254 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(2));
255 255 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(3));
256 256 QCOMPARE(clickSpy1.count(), 1);
257 257 QCOMPARE(clickSpy2.count(), 1);
258 258 QCOMPARE(clickSpy3.count(), 1);
259 259 QCOMPARE(clickSpy4.count(), 1);
260 260 }
261 261
262 262 void tst_qpieslice::hoverSignal()
263 263 {
264 264 // NOTE:
265 265 // This test is the same as tst_qpieseries::hoverSignal()
266 266 // Just for different signals.
267 267
268 268 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
269 269
270 270 // add some slices
271 271 QPieSeries *series = new QPieSeries();
272 272 QPieSlice *s1 = series->append("slice 1", 1);
273 273 QPieSlice *s2 = series->append("slice 2", 1);
274 274 QPieSlice *s3 = series->append("slice 3", 1);
275 275 QPieSlice *s4 = series->append("slice 4", 1);
276 276
277 277 // add series to the chart
278 278 QChartView view;
279 279 view.chart()->legend()->setVisible(false);
280 280 view.chart()->addSeries(series);
281 281 view.show();
282 282 QTest::qWaitForWindowShown(&view);
283 283
284 284 // move inside the slices
285 285 series->setPieSize(1.0);
286 286 QRectF pieRect = view.chart()->plotArea();
287 287 QList<QPoint> points = slicePoints(pieRect);
288 288 QTest::mouseMove(view.viewport(), pieRect.topRight().toPoint());
289 289 QSignalSpy hoverSpy1(s1, SIGNAL(hovered(bool)));
290 290 QSignalSpy hoverSpy2(s2, SIGNAL(hovered(bool)));
291 291 QSignalSpy hoverSpy3(s3, SIGNAL(hovered(bool)));
292 292 QSignalSpy hoverSpy4(s4, SIGNAL(hovered(bool)));
293 293 QTest::mouseMove(view.viewport(), points.at(0));
294 294 QTest::mouseMove(view.viewport(), points.at(1));
295 295 QTest::mouseMove(view.viewport(), points.at(2));
296 296 QTest::mouseMove(view.viewport(), points.at(3));
297 297 QTest::mouseMove(view.viewport(), pieRect.topLeft().toPoint());
298 QApplication::processEvents();
298 299
299 300 // check
300 301 QCOMPARE(hoverSpy1.count(), 2);
301 302 QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(0).at(0)), true);
302 303 QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(1).at(0)), false);
303 304 QCOMPARE(hoverSpy2.count(), 2);
304 305 QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(0).at(0)), true);
305 306 QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(1).at(0)), false);
306 307 QCOMPARE(hoverSpy3.count(), 2);
307 308 QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(0).at(0)), true);
308 309 QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(1).at(0)), false);
309 310 QCOMPARE(hoverSpy4.count(), 2);
310 311 QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(0).at(0)), true);
311 312 QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(1).at(0)), false);
312 313 }
313 314
314 315 QList<QPoint> tst_qpieslice::slicePoints(QRectF rect)
315 316 {
316 317 qreal x1 = rect.topLeft().x() + (rect.width() / 4);
317 318 qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3;
318 319 qreal y1 = rect.topLeft().y() + (rect.height() / 4);
319 320 qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3;
320 321 QList<QPoint> points;
321 322 points << QPoint(x2, y1);
322 323 points << QPoint(x2, y2);
323 324 points << QPoint(x1, y2);
324 325 points << QPoint(x1, y1);
325 326 return points;
326 327 }
327 328
328 329 QTEST_MAIN(tst_qpieslice)
329 330
330 331 #include "tst_qpieslice.moc"
331 332
General Comments 0
You need to be logged in to leave comments. Login now