##// END OF EJS Templates
refactor pie mouse click/hover tests
Jani Honkonen -
r2065:9f99ffd844c2
parent child
Show More
@@ -60,6 +60,7 private slots:
60
60
61 private:
61 private:
62 void verifyCalculatedData(const QPieSeries &series, bool *ok);
62 void verifyCalculatedData(const QPieSeries &series, bool *ok);
63 QList<QPoint> slicePoints(QRectF rect);
63
64
64 private:
65 private:
65 QChartView *m_view;
66 QChartView *m_view;
@@ -477,9 +478,12 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
477 *ok = true;
478 *ok = true;
478 }
479 }
479
480
480
481 void tst_qpieseries::clickedSignal()
481 void tst_qpieseries::clickedSignal()
482 {
482 {
483 // NOTE:
484 // This test is the same as tst_qpieslice::clickedSignal()
485 // Just for different signals.
486
483 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
487 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
484
488
485 // add some slices
489 // add some slices
@@ -491,109 +495,85 void tst_qpieseries::clickedSignal()
491
495
492 // add series to the chart
496 // add series to the chart
493 m_view->chart()->legend()->setVisible(false);
497 m_view->chart()->legend()->setVisible(false);
494 m_view->resize(200, 200);
495 m_view->chart()->addSeries(m_series);
498 m_view->chart()->addSeries(m_series);
496 m_view->show();
499 m_view->show();
497 QTest::qWaitForWindowShown(m_view);
500 QTest::qWaitForWindowShown(m_view);
498
501
499 // if you divide the chart in four equal tiles these
502 // test maximum size
500 // are the center points of those tiles
501 QPoint p1(90.25, 90);
502 QPoint p2(150, 90);
503 QPoint p3(90, 150);
504 QPoint p4(150, 150);
505
506 QPoint center(120, 120);
507
508 m_series->setPieSize(1.0);
503 m_series->setPieSize(1.0);
509 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
504 QRectF pieRect = m_view->chart()->plotArea();
510 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
505 QList<QPoint> points = slicePoints(pieRect);
511 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
506 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0));
512 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
507 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1));
513 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
508 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2));
514 TRY_COMPARE(clickSpy.count(), 5); // all hit
509 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3));
515 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s4);
510 TRY_COMPARE(clickSpy.count(), 4);
516 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s1);
511 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s1);
512 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2);
517 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
513 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
518 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s2);
514 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4);
519 clickSpy.clear();
515 clickSpy.clear();
520
516
517 // test half size
521 m_series->setPieSize(0.5);
518 m_series->setPieSize(0.5);
522 m_series->setVerticalPosition(0.25);
519 m_series->setVerticalPosition(0.25);
523 m_series->setHorizontalPosition(0.25);
520 m_series->setHorizontalPosition(0.25);
524 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); // hits
521 pieRect = QRectF(m_view->chart()->plotArea().topLeft(), m_view->chart()->plotArea().center());
525 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
522 points = slicePoints(pieRect);
526 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
523 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0));
527 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
524 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1));
528 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
525 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2));
529 TRY_COMPARE(clickSpy.count(), 1);
526 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3));
530 clickSpy.clear();
527 TRY_COMPARE(clickSpy.count(), 4);
531
528 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s1);
532 m_series->setVerticalPosition(0.25);
529 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2);
533 m_series->setHorizontalPosition(0.75);
530 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
534 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
531 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4);
535 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); // hits
536 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
537 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
538 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
539 TRY_COMPARE(clickSpy.count(), 1);
540 clickSpy.clear();
541
542 m_series->setVerticalPosition(0.75);
543 m_series->setHorizontalPosition(0.25);
544 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
545 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
546 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); // hits
547 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
548 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
549 TRY_COMPARE(clickSpy.count(), 1);
550 clickSpy.clear();
551
552 m_series->setVerticalPosition(0.75);
553 m_series->setHorizontalPosition(0.75);
554 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
555 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
556 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
557 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); // hits
558 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
559 TRY_COMPARE(clickSpy.count(), 1);
560 clickSpy.clear();
561 }
532 }
562
533
563 void tst_qpieseries::hoverSignal()
534 void tst_qpieseries::hoverSignal()
564 {
535 {
536 // NOTE:
537 // This test is the same as tst_qpieslice::hoverSignal()
538 // Just for different signals.
539
565 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
540 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
566
541
567 // add some slices
542 // add some slices
568 m_series->setPieSize(1.0);
569 QPieSlice *s1 = m_series->append("slice 1", 1);
543 QPieSlice *s1 = m_series->append("slice 1", 1);
570 m_series->append("slice 2", 2);
544 QPieSlice *s2 = m_series->append("slice 2", 1);
571 m_series->append("slice 3", 3);
545 QPieSlice *s3 = m_series->append("slice 3", 1);
546 QPieSlice *s4 = m_series->append("slice 4", 1);
572
547
573 // add series to the chart
548 // add series to the chart
574 m_view->chart()->legend()->setVisible(false);
549 m_view->chart()->legend()->setVisible(false);
575 m_view->resize(200, 200);
576 m_view->chart()->addSeries(m_series);
550 m_view->chart()->addSeries(m_series);
577 m_view->show();
551 m_view->show();
578 QTest::qWaitForWindowShown(m_view);
552 QTest::qWaitForWindowShown(m_view);
579
553
580 // first move to right top corner
554 // move inside the slices
581 QTest::mouseMove(m_view->viewport(), QPoint(200, 0));
555 m_series->setPieSize(1.0);
582 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
556 QRectF pieRect = m_view->chart()->plotArea();
583
557 QList<QPoint> points = slicePoints(pieRect);
584 // move inside the slice
558 QTest::mouseMove(m_view->viewport(), pieRect.topRight().toPoint());
585 // pie rectangle: QRectF(60,60 121x121)
586 QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool)));
559 QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool)));
587 QTest::mouseMove(m_view->viewport(), QPoint(139, 85));
560 QTest::mouseMove(m_view->viewport(), points.at(0));
588 TRY_COMPARE(hoverSpy.count(), 1);
561 QTest::mouseMove(m_view->viewport(), points.at(1));
589 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
562 QTest::mouseMove(m_view->viewport(), points.at(2));
590 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
563 QTest::mouseMove(m_view->viewport(), points.at(3));
591
564 QTest::mouseMove(m_view->viewport(), pieRect.topLeft().toPoint());
592 // move outside the slice
565
593 QTest::mouseMove(m_view->viewport(), QPoint(200, 0));
566 // check
594 TRY_COMPARE(hoverSpy.count(), 2);
567 QCOMPARE(hoverSpy.count(), 8);
595 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
568 int i = 0;
596 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
569 foreach (QPieSlice *s, m_series->slices()) {
570 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s);
571 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), true);
572 i++;
573 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s);
574 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), false);
575 i++;
576 }
597 }
577 }
598
578
599 void tst_qpieseries::sliceSeries()
579 void tst_qpieseries::sliceSeries()
@@ -636,6 +616,20 void tst_qpieseries::destruction()
636 QCOMPARE(spy3.count(), 1);
616 QCOMPARE(spy3.count(), 1);
637 }
617 }
638
618
619 QList<QPoint> tst_qpieseries::slicePoints(QRectF rect)
620 {
621 qreal x1 = rect.topLeft().x() + (rect.width() / 4);
622 qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3;
623 qreal y1 = rect.topLeft().y() + (rect.height() / 4);
624 qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3;
625 QList<QPoint> points;
626 points << QPoint(x2, y1);
627 points << QPoint(x2, y2);
628 points << QPoint(x1, y2);
629 points << QPoint(x1, y1);
630 return points;
631 }
632
639 QTEST_MAIN(tst_qpieseries)
633 QTEST_MAIN(tst_qpieseries)
640
634
641 #include "tst_qpieseries.moc"
635 #include "tst_qpieseries.moc"
@@ -41,11 +41,11 private slots:
41 void construction();
41 void construction();
42 void changedSignals();
42 void changedSignals();
43 void customize();
43 void customize();
44 void mouseClick();
44 void clickedSignal();
45 void mouseHover();
45 void hoverSignal();
46
46
47 private:
47 private:
48
48 QList<QPoint> slicePoints(QRectF rect);
49
49
50 private:
50 private:
51
51
@@ -219,75 +219,110 void tst_qpieslice::customize()
219 QVERIFY(s1->labelFont() != f1);
219 QVERIFY(s1->labelFont() != f1);
220 }
220 }
221
221
222 void tst_qpieslice::mouseClick()
222 void tst_qpieslice::clickedSignal()
223 {
223 {
224 // NOTE:
225 // This test is the same as tst_qpieseries::clickedSignal()
226 // Just for different signals.
227
228 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
229
224 // create a pie series
230 // create a pie series
225 QPieSeries *series = new QPieSeries();
231 QPieSeries *series = new QPieSeries();
226 series->setPieSize(1.0);
227 QPieSlice *s1 = series->append("slice 1", 1);
232 QPieSlice *s1 = series->append("slice 1", 1);
228 QPieSlice *s2 = series->append("slice 2", 2);
233 QPieSlice *s2 = series->append("slice 2", 1);
229 QPieSlice *s3 = series->append("slice 3", 3);
234 QPieSlice *s3 = series->append("slice 3", 1);
235 QPieSlice *s4 = series->append("slice 4", 1);
230 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
236 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
231 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
237 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
232 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
238 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
239 QSignalSpy clickSpy4(s4, SIGNAL(clicked()));
233
240
234 // add series to the chart
241 // add series to the chart
235 QChartView view(new QChart());
242 QChartView view;
236 view.chart()->legend()->setVisible(false);
243 view.chart()->legend()->setVisible(false);
237 view.resize(200, 200);
238 view.chart()->addSeries(series);
244 view.chart()->addSeries(series);
239 view.show();
245 view.show();
240 QTest::qWaitForWindowShown(&view);
246 QTest::qWaitForWindowShown(&view);
241
247
242 // simulate clicks
248 // simulate clicks
243 // pie rectangle: QRectF(60,60 121x121)
249 series->setPieSize(1.0);
244 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
250 QRectF pieRect = view.chart()->plotArea();
245 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
251 QList<QPoint> points = slicePoints(pieRect);
246 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
252 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(0));
247 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
253 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(1));
248 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
254 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(2));
249 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
255 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(3));
250 QCOMPARE(clickSpy1.count(), 1);
256 QCOMPARE(clickSpy1.count(), 1);
251 QCOMPARE(clickSpy2.count(), 1);
257 QCOMPARE(clickSpy2.count(), 1);
252 QCOMPARE(clickSpy3.count(), 1);
258 QCOMPARE(clickSpy3.count(), 1);
259 QCOMPARE(clickSpy4.count(), 1);
253 }
260 }
254
261
255 void tst_qpieslice::mouseHover()
262 void tst_qpieslice::hoverSignal()
256 {
263 {
257 // create a pie series
264 // NOTE:
265 // This test is the same as tst_qpieseries::hoverSignal()
266 // Just for different signals.
267
268 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
269
270 // add some slices
258 QPieSeries *series = new QPieSeries();
271 QPieSeries *series = new QPieSeries();
259 series->setPieSize(1.0);
260 QPieSlice *s1 = series->append("slice 1", 1);
272 QPieSlice *s1 = series->append("slice 1", 1);
261 series->append("slice 2", 2);
273 QPieSlice *s2 = series->append("slice 2", 1);
262 series->append("slice 3", 3);
274 QPieSlice *s3 = series->append("slice 3", 1);
275 QPieSlice *s4 = series->append("slice 4", 1);
263
276
264 // add series to the chart
277 // add series to the chart
265 QChartView view(new QChart());
278 QChartView view;
266 view.chart()->legend()->setVisible(false);
279 view.chart()->legend()->setVisible(false);
267 view.resize(200, 200);
268 view.chart()->addSeries(series);
280 view.chart()->addSeries(series);
269 view.show();
281 view.show();
270 QTest::qWaitForWindowShown(&view);
282 QTest::qWaitForWindowShown(&view);
271
283
272 // first move to right top corner
284 // move inside the slices
273 QTest::mouseMove(view.viewport(), QPoint(200, 0));
285 series->setPieSize(1.0);
274 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
286 QRectF pieRect = view.chart()->plotArea();
275
287 QList<QPoint> points = slicePoints(pieRect);
276 // move inside slice rectangle but NOT the actual slice
288 QTest::mouseMove(view.viewport(), pieRect.topRight().toPoint());
277 // pie rectangle: QRectF(60,60 121x121)
289 QSignalSpy hoverSpy1(s1, SIGNAL(hovered(bool)));
278 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
290 QSignalSpy hoverSpy2(s2, SIGNAL(hovered(bool)));
279 QTest::mouseMove(view.viewport(), QPoint(170, 70));
291 QSignalSpy hoverSpy3(s3, SIGNAL(hovered(bool)));
280 TRY_COMPARE(hoverSpy.count(), 0);
292 QSignalSpy hoverSpy4(s4, SIGNAL(hovered(bool)));
281
293 QTest::mouseMove(view.viewport(), points.at(0));
282 // move inside the slice
294 QTest::mouseMove(view.viewport(), points.at(1));
283 QTest::mouseMove(view.viewport(), QPoint(139, 85));
295 QTest::mouseMove(view.viewport(), points.at(2));
284 TRY_COMPARE(hoverSpy.count(), 1);
296 QTest::mouseMove(view.viewport(), points.at(3));
285 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
297 QTest::mouseMove(view.viewport(), pieRect.topLeft().toPoint());
286
298
287 // move outside the slice
299 // check
288 QTest::mouseMove(view.viewport(), QPoint(200, 0));
300 QCOMPARE(hoverSpy1.count(), 2);
289 TRY_COMPARE(hoverSpy.count(), 2);
301 QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(0).at(0)), true);
290 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
302 QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(1).at(0)), false);
303 QCOMPARE(hoverSpy2.count(), 2);
304 QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(0).at(0)), true);
305 QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(1).at(0)), false);
306 QCOMPARE(hoverSpy3.count(), 2);
307 QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(0).at(0)), true);
308 QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(1).at(0)), false);
309 QCOMPARE(hoverSpy4.count(), 2);
310 QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(0).at(0)), true);
311 QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(1).at(0)), false);
312 }
313
314 QList<QPoint> tst_qpieslice::slicePoints(QRectF rect)
315 {
316 qreal x1 = rect.topLeft().x() + (rect.width() / 4);
317 qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3;
318 qreal y1 = rect.topLeft().y() + (rect.height() / 4);
319 qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3;
320 QList<QPoint> points;
321 points << QPoint(x2, y1);
322 points << QPoint(x2, y2);
323 points << QPoint(x1, y2);
324 points << QPoint(x1, y1);
325 return points;
291 }
326 }
292
327
293 QTEST_MAIN(tst_qpieslice)
328 QTEST_MAIN(tst_qpieslice)
General Comments 0
You need to be logged in to leave comments. Login now