diff --git a/test/auto/qpieseries/tst_qpieseries.cpp b/test/auto/qpieseries/tst_qpieseries.cpp index 78eded3..0866911 100644 --- a/test/auto/qpieseries/tst_qpieseries.cpp +++ b/test/auto/qpieseries/tst_qpieseries.cpp @@ -19,12 +19,16 @@ ****************************************************************************/ #include +#include +#include #include #include #include QTCOMMERCIALCHART_USE_NAMESPACE +Q_DECLARE_METATYPE(QPieSlice*) + class tst_qpieseries : public QObject { Q_OBJECT @@ -40,13 +44,12 @@ private slots: void append(); void insert(); void remove(); - //void calculatedValues(); - //void themes(); - //void clickedSignal(); - //void hoverSignal(); + void calculatedValues(); + void clickedSignal(); + void hoverSignal(); private: - + void verifyCalculatedData(const QPieSeries &series, bool *ok); private: @@ -54,6 +57,7 @@ private: void tst_qpieseries::initTestCase() { + qRegisterMetaType("QPieSlice*"); } void tst_qpieseries::cleanupTestCase() @@ -193,27 +197,130 @@ void tst_qpieseries::remove() TRY_COMPARE(spy3.count(), 1); } -/* void tst_qpieseries::calculatedValues() { + bool ok; + QPieSeries s; + + // add a slice + QPieSlice *slice1 = s.append(1, "slice 1"); + verifyCalculatedData(s, &ok); + if (!ok) + return; + // add some more slices + QList list; + list << new QPieSlice(2, "slice 2"); + list << new QPieSlice(3, "slice 3"); + s.append(list); + verifyCalculatedData(s, &ok); + if (!ok) + return; + + // remove a slice + s.remove(slice1); + verifyCalculatedData(s, &ok); + if (!ok) + return; + + // insert a slice + s.insert(0, new QPieSlice(1, "Slice 4")); + verifyCalculatedData(s, &ok); + if (!ok) + return; + + // clear all + s.clear(); + verifyCalculatedData(s, &ok); } -void tst_qpieseries::themes() +void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) { + *ok = false; + + qreal sum = 0; + foreach (const QPieSlice *slice, series.slices()) + sum += slice->value(); + QCOMPARE(series.sum(), sum); + + qreal startAngle = series.pieStartAngle(); + qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); + foreach (const QPieSlice *slice, series.slices()) { + qreal ratio = slice->value() / sum; + qreal sliceSpan = pieAngleSpan * ratio; + QCOMPARE(slice->startAngle(), startAngle); + QCOMPARE(slice->endAngle(), startAngle + sliceSpan); + QCOMPARE(slice->percentage(), ratio); + startAngle += sliceSpan; + } + + if (!series.isEmpty()) + QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle()); + *ok = true; } + void tst_qpieseries::clickedSignal() { - + // create a pie series + QPieSeries *series = new QPieSeries(); + series->setPieSize(1.0); + QPieSlice *s1 = series->append(1, "slice 1"); + series->append(2, "slice 2"); + series->append(3, "slice 3"); + QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*))); + + // add series to the chart + QChartView view(new QChart()); + 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 + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QCOMPARE(clickSpy1.count(), 1); + QCOMPARE(qvariant_cast(clickSpy1.at(0).at(0)), s1); } void tst_qpieseries::hoverSignal() { - + // create a pie series + QPieSeries *series = new QPieSeries(); + series->setPieSize(1.0); + QPieSlice *s1 = series->append(1, "slice 1"); + series->append(2, "slice 2"); + series->append(3, "slice 3"); + + // add series to the chart + QChartView view(new QChart()); + 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)); + + // move inside the slice + // pie rectangle: QRectF(60,60 121x121) + QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool))); + QTest::mouseMove(view.viewport(), QPoint(139, 85)); + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QCOMPARE(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(view.viewport(), QPoint(200, 0)); + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QCOMPARE(hoverSpy.count(), 2); + QCOMPARE(qvariant_cast(hoverSpy.at(1).at(0)), s1); + QCOMPARE(qvariant_cast(hoverSpy.at(1).at(1)), false); } -*/ QTEST_MAIN(tst_qpieseries) diff --git a/test/auto/qpieslice/tst_qpieslice.cpp b/test/auto/qpieslice/tst_qpieslice.cpp index 42738e0..eae950d 100644 --- a/test/auto/qpieslice/tst_qpieslice.cpp +++ b/test/auto/qpieslice/tst_qpieslice.cpp @@ -19,8 +19,11 @@ ****************************************************************************/ #include -#include #include +#include +#include +#include +#include QTCOMMERCIALCHART_USE_NAMESPACE @@ -37,6 +40,9 @@ public slots: private slots: void construction(); void changedSignals(); + void customize(); + void mouseClick(); + void mouseHover(); private: @@ -126,6 +132,138 @@ void tst_qpieslice::changedSignals() TRY_COMPARE(spy.count(), 10); } +void tst_qpieslice::customize() +{ + // create a pie series + QPieSeries *series = new QPieSeries(); + QPieSlice *s1 = series->append(1, "slice 1"); + QPieSlice *s2 = series->append(2, "slice 2"); + series->append(3, "slice 3"); + + // customize a slice + QPen p1(Qt::red); + s1->setPen(p1); + QBrush b1(Qt::red); + s1->setBrush(b1); + s1->setLabelPen(p1); + QFont f1("Consolas"); + s1->setLabelFont(f1); + + // add series to the chart + QChartView view(new QChart()); + view.resize(200, 200); + view.chart()->addSeries(series); + view.show(); + QTest::qWaitForWindowShown(&view); + //QTest::qWait(1000); + + // check that customizations persist + QCOMPARE(s1->pen(), p1); + QCOMPARE(s1->brush(), b1); + QCOMPARE(s1->labelPen(), p1); + QCOMPARE(s1->labelFont(), f1); + + // remove a slice + series->remove(s2); + QCOMPARE(s1->pen(), p1); + QCOMPARE(s1->brush(), b1); + QCOMPARE(s1->labelPen(), p1); + QCOMPARE(s1->labelFont(), f1); + + // add a slice + series->append(4, "slice 4"); + QCOMPARE(s1->pen(), p1); + QCOMPARE(s1->brush(), b1); + QCOMPARE(s1->labelPen(), p1); + QCOMPARE(s1->labelFont(), f1); + + // insert a slice + series->insert(0, new QPieSlice(5, "slice 5")); + QCOMPARE(s1->pen(), p1); + QCOMPARE(s1->brush(), b1); + QCOMPARE(s1->labelPen(), p1); + QCOMPARE(s1->labelFont(), f1); + + // change theme + // theme will overwrite customizations + view.chart()->setTheme(QChart::ChartThemeHighContrast); + QVERIFY(s1->pen() != p1); + QVERIFY(s1->brush() != b1); + QVERIFY(s1->labelPen() != p1); + QVERIFY(s1->labelFont() != f1); +} + +void tst_qpieslice::mouseClick() +{ + // create a pie series + QPieSeries *series = new QPieSeries(); + series->setPieSize(1.0); + QPieSlice *s1 = series->append(1, "slice 1"); + QPieSlice *s2 = series->append(2, "slice 2"); + QPieSlice *s3 = series->append(3, "slice 3"); + QSignalSpy clickSpy1(s1, SIGNAL(clicked())); + QSignalSpy clickSpy2(s2, SIGNAL(clicked())); + QSignalSpy clickSpy3(s3, SIGNAL(clicked())); + + // add series to the chart + QChartView view(new QChart()); + 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); + QCOMPARE(clickSpy1.count(), 1); + QCOMPARE(clickSpy2.count(), 1); + QCOMPARE(clickSpy3.count(), 1); +} + +void tst_qpieslice::mouseHover() +{ + // create a pie series + QPieSeries *series = new QPieSeries(); + series->setPieSize(1.0); + QPieSlice *s1 = series->append(1, "slice 1"); + series->append(2, "slice 2"); + series->append(3, "slice 3"); + + // add series to the chart + QChartView view(new QChart()); + 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)); + + // 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)); + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QCOMPARE(hoverSpy.count(), 0); + + // move inside the slice + QTest::mouseMove(view.viewport(), QPoint(139, 85)); + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QCOMPARE(hoverSpy.count(), 1); + QCOMPARE(qvariant_cast(hoverSpy.at(0).at(0)), true); + + // move outside the slice + QTest::mouseMove(view.viewport(), QPoint(200, 0)); + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + QCOMPARE(hoverSpy.count(), 2); + QCOMPARE(qvariant_cast(hoverSpy.at(1).at(0)), false); +} + QTEST_MAIN(tst_qpieslice) #include "tst_qpieslice.moc"