From 9f99ffd844c25ff98397e21f4b273b3c7a9e8fb7 2012-09-17 09:59:57 From: Jani Honkonen Date: 2012-09-17 09:59:57 Subject: [PATCH] refactor pie mouse click/hover tests --- diff --git a/tests/auto/qpieseries/tst_qpieseries.cpp b/tests/auto/qpieseries/tst_qpieseries.cpp index 910a3a4..5a446c8 100644 --- a/tests/auto/qpieseries/tst_qpieseries.cpp +++ b/tests/auto/qpieseries/tst_qpieseries.cpp @@ -60,6 +60,7 @@ private slots: private: void verifyCalculatedData(const QPieSeries &series, bool *ok); + QList slicePoints(QRectF rect); private: QChartView *m_view; @@ -477,9 +478,12 @@ void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) *ok = true; } - void tst_qpieseries::clickedSignal() { + // NOTE: + // This test is the same as tst_qpieslice::clickedSignal() + // Just for different signals. + SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); // add some slices @@ -491,109 +495,85 @@ void tst_qpieseries::clickedSignal() // add series to the chart m_view->chart()->legend()->setVisible(false); - m_view->resize(200, 200); m_view->chart()->addSeries(m_series); m_view->show(); QTest::qWaitForWindowShown(m_view); - // if you divide the chart in four equal tiles these - // are the center points of those tiles - QPoint p1(90.25, 90); - QPoint p2(150, 90); - QPoint p3(90, 150); - QPoint p4(150, 150); - - QPoint center(120, 120); - + // test maximum size m_series->setPieSize(1.0); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); - TRY_COMPARE(clickSpy.count(), 5); // all hit - QCOMPARE(qvariant_cast(clickSpy.at(0).at(0)), s4); - QCOMPARE(qvariant_cast(clickSpy.at(1).at(0)), s1); + QRectF pieRect = m_view->chart()->plotArea(); + QList points = slicePoints(pieRect); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0)); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1)); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2)); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3)); + TRY_COMPARE(clickSpy.count(), 4); + QCOMPARE(qvariant_cast(clickSpy.at(0).at(0)), s1); + QCOMPARE(qvariant_cast(clickSpy.at(1).at(0)), s2); QCOMPARE(qvariant_cast(clickSpy.at(2).at(0)), s3); - QCOMPARE(qvariant_cast(clickSpy.at(3).at(0)), s2); + QCOMPARE(qvariant_cast(clickSpy.at(3).at(0)), s4); clickSpy.clear(); + // test half size m_series->setPieSize(0.5); m_series->setVerticalPosition(0.25); m_series->setHorizontalPosition(0.25); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); // hits - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); - TRY_COMPARE(clickSpy.count(), 1); - clickSpy.clear(); - - m_series->setVerticalPosition(0.25); - m_series->setHorizontalPosition(0.75); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); // hits - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); - TRY_COMPARE(clickSpy.count(), 1); - clickSpy.clear(); - - m_series->setVerticalPosition(0.75); - m_series->setHorizontalPosition(0.25); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); // hits - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); - TRY_COMPARE(clickSpy.count(), 1); - clickSpy.clear(); - - m_series->setVerticalPosition(0.75); - m_series->setHorizontalPosition(0.75); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); // hits - QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center); - TRY_COMPARE(clickSpy.count(), 1); - clickSpy.clear(); + pieRect = QRectF(m_view->chart()->plotArea().topLeft(), m_view->chart()->plotArea().center()); + points = slicePoints(pieRect); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(0)); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(1)); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(2)); + QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, points.at(3)); + TRY_COMPARE(clickSpy.count(), 4); + QCOMPARE(qvariant_cast(clickSpy.at(0).at(0)), s1); + QCOMPARE(qvariant_cast(clickSpy.at(1).at(0)), s2); + QCOMPARE(qvariant_cast(clickSpy.at(2).at(0)), s3); + QCOMPARE(qvariant_cast(clickSpy.at(3).at(0)), s4); } void tst_qpieseries::hoverSignal() { + // NOTE: + // This test is the same as tst_qpieslice::hoverSignal() + // Just for different signals. + SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); // add some slices - m_series->setPieSize(1.0); QPieSlice *s1 = m_series->append("slice 1", 1); - m_series->append("slice 2", 2); - m_series->append("slice 3", 3); + QPieSlice *s2 = m_series->append("slice 2", 1); + QPieSlice *s3 = m_series->append("slice 3", 1); + QPieSlice *s4 = m_series->append("slice 4", 1); // add series to the chart m_view->chart()->legend()->setVisible(false); - m_view->resize(200, 200); m_view->chart()->addSeries(m_series); m_view->show(); QTest::qWaitForWindowShown(m_view); - // first move to right top corner - QTest::mouseMove(m_view->viewport(), QPoint(200, 0)); - QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); - - // move inside the slice - // pie rectangle: QRectF(60,60 121x121) + // move inside the slices + m_series->setPieSize(1.0); + QRectF pieRect = m_view->chart()->plotArea(); + QList points = slicePoints(pieRect); + QTest::mouseMove(m_view->viewport(), pieRect.topRight().toPoint()); QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool))); - QTest::mouseMove(m_view->viewport(), QPoint(139, 85)); - TRY_COMPARE(hoverSpy.count(), 1); - QCOMPARE(qvariant_cast(hoverSpy.at(0).at(0)), s1); - QCOMPARE(qvariant_cast(hoverSpy.at(0).at(1)), true); - - // move outside the slice - QTest::mouseMove(m_view->viewport(), QPoint(200, 0)); - TRY_COMPARE(hoverSpy.count(), 2); - QCOMPARE(qvariant_cast(hoverSpy.at(1).at(0)), s1); - QCOMPARE(qvariant_cast(hoverSpy.at(1).at(1)), false); + QTest::mouseMove(m_view->viewport(), points.at(0)); + QTest::mouseMove(m_view->viewport(), points.at(1)); + QTest::mouseMove(m_view->viewport(), points.at(2)); + QTest::mouseMove(m_view->viewport(), points.at(3)); + QTest::mouseMove(m_view->viewport(), pieRect.topLeft().toPoint()); + + // check + QCOMPARE(hoverSpy.count(), 8); + int i = 0; + foreach (QPieSlice *s, m_series->slices()) { + QCOMPARE(qvariant_cast(hoverSpy.at(i).at(0)), s); + QCOMPARE(qvariant_cast(hoverSpy.at(i).at(1)), true); + i++; + QCOMPARE(qvariant_cast(hoverSpy.at(i).at(0)), s); + QCOMPARE(qvariant_cast(hoverSpy.at(i).at(1)), false); + i++; + } } void tst_qpieseries::sliceSeries() @@ -636,6 +616,20 @@ void tst_qpieseries::destruction() QCOMPARE(spy3.count(), 1); } +QList tst_qpieseries::slicePoints(QRectF rect) +{ + qreal x1 = rect.topLeft().x() + (rect.width() / 4); + qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3; + qreal y1 = rect.topLeft().y() + (rect.height() / 4); + qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3; + QList points; + points << QPoint(x2, y1); + points << QPoint(x2, y2); + points << QPoint(x1, y2); + points << QPoint(x1, y1); + return points; +} + QTEST_MAIN(tst_qpieseries) #include "tst_qpieseries.moc" diff --git a/tests/auto/qpieslice/tst_qpieslice.cpp b/tests/auto/qpieslice/tst_qpieslice.cpp index a0f67f2..bd05096 100644 --- a/tests/auto/qpieslice/tst_qpieslice.cpp +++ b/tests/auto/qpieslice/tst_qpieslice.cpp @@ -41,11 +41,11 @@ private slots: void construction(); void changedSignals(); void customize(); - void mouseClick(); - void mouseHover(); + void clickedSignal(); + void hoverSignal(); private: - + QList slicePoints(QRectF rect); private: @@ -219,75 +219,110 @@ void tst_qpieslice::customize() QVERIFY(s1->labelFont() != f1); } -void tst_qpieslice::mouseClick() +void tst_qpieslice::clickedSignal() { + // NOTE: + // This test is the same as tst_qpieseries::clickedSignal() + // Just for different signals. + + SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); + // create a pie series QPieSeries *series = new QPieSeries(); - series->setPieSize(1.0); QPieSlice *s1 = series->append("slice 1", 1); - QPieSlice *s2 = series->append("slice 2", 2); - QPieSlice *s3 = series->append("slice 3", 3); + QPieSlice *s2 = series->append("slice 2", 1); + QPieSlice *s3 = series->append("slice 3", 1); + QPieSlice *s4 = series->append("slice 4", 1); QSignalSpy clickSpy1(s1, SIGNAL(clicked())); QSignalSpy clickSpy2(s2, SIGNAL(clicked())); QSignalSpy clickSpy3(s3, SIGNAL(clicked())); + QSignalSpy clickSpy4(s4, SIGNAL(clicked())); // add series to the chart - QChartView view(new QChart()); + QChartView view; view.chart()->legend()->setVisible(false); - view.resize(200, 200); view.chart()->addSeries(series); view.show(); QTest::qWaitForWindowShown(&view); // simulate clicks - // pie rectangle: QRectF(60,60 121x121) - QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 - QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2 - QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3 - QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice - QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice - QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + series->setPieSize(1.0); + QRectF pieRect = view.chart()->plotArea(); + QList points = slicePoints(pieRect); + QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(0)); + QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(1)); + QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(2)); + QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, points.at(3)); QCOMPARE(clickSpy1.count(), 1); QCOMPARE(clickSpy2.count(), 1); QCOMPARE(clickSpy3.count(), 1); + QCOMPARE(clickSpy4.count(), 1); } -void tst_qpieslice::mouseHover() +void tst_qpieslice::hoverSignal() { - // create a pie series + // NOTE: + // This test is the same as tst_qpieseries::hoverSignal() + // Just for different signals. + + SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); + + // add some slices QPieSeries *series = new QPieSeries(); - series->setPieSize(1.0); QPieSlice *s1 = series->append("slice 1", 1); - series->append("slice 2", 2); - series->append("slice 3", 3); + QPieSlice *s2 = series->append("slice 2", 1); + QPieSlice *s3 = series->append("slice 3", 1); + QPieSlice *s4 = series->append("slice 4", 1); // add series to the chart - QChartView view(new QChart()); + QChartView view; view.chart()->legend()->setVisible(false); - view.resize(200, 200); view.chart()->addSeries(series); view.show(); QTest::qWaitForWindowShown(&view); - // first move to right top corner - QTest::mouseMove(view.viewport(), QPoint(200, 0)); - QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); - - // move inside slice rectangle but NOT the actual slice - // pie rectangle: QRectF(60,60 121x121) - QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool))); - QTest::mouseMove(view.viewport(), QPoint(170, 70)); - TRY_COMPARE(hoverSpy.count(), 0); - - // move inside the slice - QTest::mouseMove(view.viewport(), QPoint(139, 85)); - TRY_COMPARE(hoverSpy.count(), 1); - QCOMPARE(qvariant_cast(hoverSpy.at(0).at(0)), true); - - // move outside the slice - QTest::mouseMove(view.viewport(), QPoint(200, 0)); - TRY_COMPARE(hoverSpy.count(), 2); - QCOMPARE(qvariant_cast(hoverSpy.at(1).at(0)), false); + // move inside the slices + series->setPieSize(1.0); + QRectF pieRect = view.chart()->plotArea(); + QList points = slicePoints(pieRect); + QTest::mouseMove(view.viewport(), pieRect.topRight().toPoint()); + QSignalSpy hoverSpy1(s1, SIGNAL(hovered(bool))); + QSignalSpy hoverSpy2(s2, SIGNAL(hovered(bool))); + QSignalSpy hoverSpy3(s3, SIGNAL(hovered(bool))); + QSignalSpy hoverSpy4(s4, SIGNAL(hovered(bool))); + QTest::mouseMove(view.viewport(), points.at(0)); + QTest::mouseMove(view.viewport(), points.at(1)); + QTest::mouseMove(view.viewport(), points.at(2)); + QTest::mouseMove(view.viewport(), points.at(3)); + QTest::mouseMove(view.viewport(), pieRect.topLeft().toPoint()); + + // check + QCOMPARE(hoverSpy1.count(), 2); + QCOMPARE(qvariant_cast(hoverSpy1.at(0).at(0)), true); + QCOMPARE(qvariant_cast(hoverSpy1.at(1).at(0)), false); + QCOMPARE(hoverSpy2.count(), 2); + QCOMPARE(qvariant_cast(hoverSpy2.at(0).at(0)), true); + QCOMPARE(qvariant_cast(hoverSpy2.at(1).at(0)), false); + QCOMPARE(hoverSpy3.count(), 2); + QCOMPARE(qvariant_cast(hoverSpy3.at(0).at(0)), true); + QCOMPARE(qvariant_cast(hoverSpy3.at(1).at(0)), false); + QCOMPARE(hoverSpy4.count(), 2); + QCOMPARE(qvariant_cast(hoverSpy4.at(0).at(0)), true); + QCOMPARE(qvariant_cast(hoverSpy4.at(1).at(0)), false); +} + +QList tst_qpieslice::slicePoints(QRectF rect) +{ + qreal x1 = rect.topLeft().x() + (rect.width() / 4); + qreal x2 = rect.topLeft().x() + (rect.width() / 4) * 3; + qreal y1 = rect.topLeft().y() + (rect.height() / 4); + qreal y2 = rect.topLeft().y() + (rect.height() / 4) * 3; + QList points; + points << QPoint(x2, y1); + points << QPoint(x2, y2); + points << QPoint(x1, y2); + points << QPoint(x1, y1); + return points; } QTEST_MAIN(tst_qpieslice)