tst_qpieseries.cpp
649 lines
| 20.2 KiB
| text/x-c
|
CppLexer
|
r1105 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include <QtTest/QtTest> | ||||
|
r1119 | #include <qchartview.h> | ||
#include <qchart.h> | ||||
|
r1105 | #include <qpieseries.h> | ||
#include <qpieslice.h> | ||||
|
r1180 | #include <qpiemodelmapper.h> | ||
#include <QStandardItemModel> | ||||
|
r1109 | #include <tst_definitions.h> | ||
|
r1105 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
|
r1119 | Q_DECLARE_METATYPE(QPieSlice*) | ||
|
r1291 | Q_DECLARE_METATYPE(QList<QPieSlice*>) | ||
|
r1119 | |||
|
r1105 | class tst_qpieseries : public QObject | ||
{ | ||||
Q_OBJECT | ||||
public slots: | ||||
void initTestCase(); | ||||
void cleanupTestCase(); | ||||
void init(); | ||||
void cleanup(); | ||||
private slots: | ||||
|
r1255 | void properties(); | ||
|
r1105 | void append(); | ||
|
r1301 | void appendAnimated(); | ||
|
r1105 | void insert(); | ||
|
r1301 | void insertAnimated(); | ||
|
r1105 | void remove(); | ||
|
r1301 | void removeAnimated(); | ||
|
r1798 | void take(); | ||
void takeAnimated(); | ||||
|
r1119 | void calculatedValues(); | ||
void clickedSignal(); | ||||
void hoverSignal(); | ||||
|
r1323 | void sliceSeries(); | ||
|
r1768 | void destruction(); | ||
|
r1105 | |||
private: | ||||
|
r1119 | void verifyCalculatedData(const QPieSeries &series, bool *ok); | ||
|
r2065 | QList<QPoint> slicePoints(QRectF rect); | ||
|
r1105 | |||
private: | ||||
|
r1301 | QChartView *m_view; | ||
QPieSeries *m_series; | ||||
|
r1105 | }; | ||
void tst_qpieseries::initTestCase() | ||||
{ | ||||
|
r1119 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); | ||
|
r1291 | qRegisterMetaType<QList<QPieSlice*> >("QList<QPieSlice*>"); | ||
|
r1105 | } | ||
void tst_qpieseries::cleanupTestCase() | ||||
{ | ||||
} | ||||
void tst_qpieseries::init() | ||||
{ | ||||
|
r1301 | m_view = new QChartView(); | ||
m_series = new QPieSeries(m_view); | ||||
|
r1797 | m_view->show(); | ||
QTest::qWaitForWindowShown(m_view); | ||||
|
r1105 | } | ||
void tst_qpieseries::cleanup() | ||||
{ | ||||
|
r1301 | delete m_view; | ||
m_view = 0; | ||||
m_series = 0; | ||||
|
r1105 | } | ||
|
r1255 | void tst_qpieseries::properties() | ||
|
r1105 | { | ||
|
r1301 | QSignalSpy countSpy(m_series, SIGNAL(countChanged())); | ||
QSignalSpy sumSpy(m_series, SIGNAL(sumChanged())); | ||||
|
r2067 | QSignalSpy opacitySpy(m_series, SIGNAL(opacityChanged())); | ||
|
r1301 | |||
QVERIFY(m_series->type() == QAbstractSeries::SeriesTypePie); | ||||
QVERIFY(m_series->count() == 0); | ||||
QVERIFY(m_series->isEmpty()); | ||||
QCOMPARE(m_series->sum(), 0.0); | ||||
QCOMPARE(m_series->horizontalPosition(), 0.5); | ||||
QCOMPARE(m_series->verticalPosition(), 0.5); | ||||
QCOMPARE(m_series->pieSize(), 0.7); | ||||
QCOMPARE(m_series->pieStartAngle(), 0.0); | ||||
QCOMPARE(m_series->pieEndAngle(), 360.0); | ||||
|
r2067 | QCOMPARE(m_series->opacity(), 1.0); | ||
|
r1301 | |||
m_series->append("s1", 1); | ||||
m_series->append("s2", 1); | ||||
m_series->append("s3", 1); | ||||
m_series->insert(1, new QPieSlice("s4", 1)); | ||||
m_series->remove(m_series->slices().first()); | ||||
QCOMPARE(m_series->count(), 3); | ||||
QCOMPARE(m_series->sum(), 3.0); | ||||
m_series->clear(); | ||||
QCOMPARE(m_series->count(), 0); | ||||
QCOMPARE(m_series->sum(), 0.0); | ||||
|
r1255 | QCOMPARE(countSpy.count(), 6); | ||
QCOMPARE(sumSpy.count(), 6); | ||||
|
r1301 | m_series->setPieSize(-1.0); | ||
QCOMPARE(m_series->pieSize(), 0.0); | ||||
m_series->setPieSize(0.0); | ||||
m_series->setPieSize(0.9); | ||||
m_series->setPieSize(2.0); | ||||
QCOMPARE(m_series->pieSize(), 1.0); | ||||
|
r1255 | |||
|
r1742 | m_series->setPieSize(0.7); | ||
QCOMPARE(m_series->pieSize(), 0.7); | ||||
|
r1838 | m_series->setHoleSize(-1.0); | ||
QCOMPARE(m_series->holeSize(), 0.0); | ||||
m_series->setHoleSize(0.5); | ||||
QCOMPARE(m_series->holeSize(), 0.5); | ||||
|
r1742 | |||
|
r1838 | m_series->setHoleSize(0.8); | ||
QCOMPARE(m_series->holeSize(), 0.8); | ||||
|
r1742 | QCOMPARE(m_series->pieSize(), 0.8); | ||
m_series->setPieSize(0.4); | ||||
QCOMPARE(m_series->pieSize(), 0.4); | ||||
|
r1838 | QCOMPARE(m_series->holeSize(), 0.4); | ||
|
r1742 | |||
|
r1301 | m_series->setPieStartAngle(0); | ||
m_series->setPieStartAngle(-180); | ||||
m_series->setPieStartAngle(180); | ||||
|
r1482 | QCOMPARE(m_series->pieStartAngle(), 180.0); | ||
|
r1255 | |||
|
r1301 | m_series->setPieEndAngle(360); | ||
m_series->setPieEndAngle(-180); | ||||
m_series->setPieEndAngle(180); | ||||
|
r1482 | QCOMPARE(m_series->pieEndAngle(), 180.0); | ||
|
r1255 | |||
|
r1301 | m_series->setHorizontalPosition(0.5); | ||
m_series->setHorizontalPosition(-1.0); | ||||
QCOMPARE(m_series->horizontalPosition(), 0.0); | ||||
m_series->setHorizontalPosition(1.0); | ||||
m_series->setHorizontalPosition(2.0); | ||||
QCOMPARE(m_series->horizontalPosition(), 1.0); | ||||
|
r1255 | |||
|
r1301 | m_series->setVerticalPosition(0.5); | ||
m_series->setVerticalPosition(-1.0); | ||||
QCOMPARE(m_series->verticalPosition(), 0.0); | ||||
m_series->setVerticalPosition(1.0); | ||||
m_series->setVerticalPosition(2.0); | ||||
QCOMPARE(m_series->verticalPosition(), 1.0); | ||||
|
r2067 | |||
m_series->setOpacity(0.5); | ||||
QCOMPARE(m_series->opacity(), 0.5); | ||||
QCOMPARE(opacitySpy.count(), 1); | ||||
m_series->setOpacity(0.0); | ||||
QCOMPARE(m_series->opacity(), 0.0); | ||||
QCOMPARE(opacitySpy.count(), 2); | ||||
m_series->setOpacity(1.0); | ||||
QCOMPARE(m_series->opacity(), 1.0); | ||||
QCOMPARE(opacitySpy.count(), 3); | ||||
|
r1105 | } | ||
void tst_qpieseries::append() | ||||
{ | ||||
|
r1301 | m_view->chart()->addSeries(m_series); | ||
QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>))); | ||||
|
r1105 | |||
// append pointer | ||||
QPieSlice *slice1 = 0; | ||||
|
r1301 | QVERIFY(!m_series->append(slice1)); | ||
|
r1206 | slice1 = new QPieSlice("slice 1", 1); | ||
|
r1301 | QVERIFY(m_series->append(slice1)); | ||
QVERIFY(!m_series->append(slice1)); | ||||
QCOMPARE(m_series->count(), 1); | ||||
|
r1291 | QCOMPARE(addedSpy.count(), 1); | ||
QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice1); | ||||
|
r1105 | |||
|
r1324 | // try to append same slice to another series | ||
QPieSeries series2; | ||||
QVERIFY(!series2.append(slice1)); | ||||
|
r1105 | // append pointer list | ||
QList<QPieSlice *> list; | ||||
|
r1301 | QVERIFY(!m_series->append(list)); | ||
|
r1105 | list << (QPieSlice *) 0; | ||
|
r1301 | QVERIFY(!m_series->append(list)); | ||
|
r1105 | list.clear(); | ||
|
r1206 | list << new QPieSlice("slice 2", 2); | ||
list << new QPieSlice("slice 3", 3); | ||||
|
r1301 | QVERIFY(m_series->append(list)); | ||
QVERIFY(!m_series->append(list)); | ||||
QCOMPARE(m_series->count(), 3); | ||||
|
r1291 | QCOMPARE(addedSpy.count(), 2); | ||
added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0)); | ||||
QCOMPARE(added.count(), 2); | ||||
QCOMPARE(added, list); | ||||
|
r1105 | |||
// append operator | ||||
|
r1291 | QPieSlice *slice4 = new QPieSlice("slice 4", 4); | ||
|
r1301 | *m_series << slice4; | ||
*m_series << slice1; // fails because already added | ||||
QCOMPARE(m_series->count(), 4); | ||||
|
r1291 | QCOMPARE(addedSpy.count(), 3); | ||
added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice4); | ||||
|
r1105 | |||
// append with params | ||||
|
r1301 | QPieSlice *slice5 = m_series->append("slice 5", 5); | ||
|
r1105 | QVERIFY(slice5 != 0); | ||
|
r1125 | QCOMPARE(slice5->value(), 5.0); | ||
|
r1105 | QCOMPARE(slice5->label(), QString("slice 5")); | ||
|
r1301 | QCOMPARE(m_series->count(), 5); | ||
|
r1291 | QCOMPARE(addedSpy.count(), 4); | ||
added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice5); | ||||
|
r1105 | |||
// check slices | ||||
|
r1301 | QVERIFY(!m_series->isEmpty()); | ||
for (int i=0; i<m_series->count(); i++) { | ||||
QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1); | ||||
QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1)); | ||||
|
r1105 | } | ||
} | ||||
|
r1301 | void tst_qpieseries::appendAnimated() | ||
{ | ||||
m_view->chart()->setAnimationOptions(QChart::AllAnimations); | ||||
|
r1797 | append(); | ||
|
r1301 | } | ||
|
r1105 | void tst_qpieseries::insert() | ||
{ | ||||
|
r1301 | m_view->chart()->addSeries(m_series); | ||
QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>))); | ||||
|
r1105 | |||
// insert one slice | ||||
QPieSlice *slice1 = 0; | ||||
|
r1301 | QVERIFY(!m_series->insert(0, slice1)); | ||
|
r1206 | slice1 = new QPieSlice("slice 1", 1); | ||
|
r1301 | QVERIFY(!m_series->insert(-1, slice1)); | ||
QVERIFY(!m_series->insert(5, slice1)); | ||||
QVERIFY(m_series->insert(0, slice1)); | ||||
QVERIFY(!m_series->insert(0, slice1)); | ||||
QCOMPARE(m_series->count(), 1); | ||||
|
r1291 | QCOMPARE(addedSpy.count(), 1); | ||
QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice1); | ||||
|
r1105 | |||
|
r1324 | // try to insert same slice to another series | ||
QPieSeries series2; | ||||
QVERIFY(!series2.insert(0, slice1)); | ||||
|
r1105 | // add some more slices | ||
|
r1301 | QPieSlice *slice2 = m_series->append("slice 2", 2); | ||
QPieSlice *slice4 = m_series->append("slice 4", 4); | ||||
QCOMPARE(m_series->count(), 3); | ||||
|
r1291 | QCOMPARE(addedSpy.count(), 3); | ||
added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice2); | ||||
added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice4); | ||||
|
r1105 | |||
// insert between slices | ||||
|
r1291 | QPieSlice *slice3 = new QPieSlice("slice 3", 3); | ||
|
r1301 | m_series->insert(2, slice3); | ||
QCOMPARE(m_series->count(), 4); | ||||
|
r1291 | QCOMPARE(addedSpy.count(), 4); | ||
added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0)); | ||||
QCOMPARE(added.count(), 1); | ||||
QCOMPARE(added.first(), slice3); | ||||
|
r1105 | |||
// check slices | ||||
|
r1301 | for (int i=0; i<m_series->count(); i++) { | ||
QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1); | ||||
QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1)); | ||||
|
r1768 | QVERIFY(m_series->slices().at(i)->parent() == m_series); | ||
|
r1105 | } | ||
} | ||||
|
r1301 | void tst_qpieseries::insertAnimated() | ||
{ | ||||
m_view->chart()->setAnimationOptions(QChart::AllAnimations); | ||||
|
r1797 | insert(); | ||
|
r1301 | } | ||
|
r1105 | void tst_qpieseries::remove() | ||
{ | ||||
|
r1301 | m_view->chart()->addSeries(m_series); | ||
QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>))); | ||||
|
r1105 | |||
// add some slices | ||||
|
r1301 | QPieSlice *slice1 = m_series->append("slice 1", 1); | ||
QPieSlice *slice2 = m_series->append("slice 2", 2); | ||||
QPieSlice *slice3 = m_series->append("slice 3", 3); | ||||
|
r1105 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); | ||
QSignalSpy spy2(slice2, SIGNAL(destroyed())); | ||||
QSignalSpy spy3(slice3, SIGNAL(destroyed())); | ||||
|
r1301 | QCOMPARE(m_series->count(), 3); | ||
|
r1105 | |||
// null pointer remove | ||||
|
r1301 | QVERIFY(!m_series->remove(0)); | ||
|
r1105 | |||
// remove first | ||||
|
r1301 | QVERIFY(m_series->remove(slice1)); | ||
QVERIFY(!m_series->remove(slice1)); | ||||
QCOMPARE(m_series->count(), 2); | ||||
QCOMPARE(m_series->slices().at(0)->label(), slice2->label()); | ||||
|
r1291 | QCOMPARE(removedSpy.count(), 1); | ||
QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0)); | ||||
QCOMPARE(removed.count(), 1); | ||||
QCOMPARE(removed.first(), slice1); | ||||
|
r1105 | |||
// remove all | ||||
|
r1301 | m_series->clear(); | ||
QVERIFY(m_series->isEmpty()); | ||||
QVERIFY(m_series->slices().isEmpty()); | ||||
QCOMPARE(m_series->count(), 0); | ||||
|
r1291 | QCOMPARE(removedSpy.count(), 2); | ||
removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(1).at(0)); | ||||
QCOMPARE(removed.count(), 2); | ||||
QCOMPARE(removed.first(), slice2); | ||||
QCOMPARE(removed.last(), slice3); | ||||
|
r1105 | |||
// check that slices were actually destroyed | ||||
|
r1109 | TRY_COMPARE(spy1.count(), 1); | ||
TRY_COMPARE(spy2.count(), 1); | ||||
TRY_COMPARE(spy3.count(), 1); | ||||
|
r1105 | } | ||
|
r1301 | void tst_qpieseries::removeAnimated() | ||
{ | ||||
m_view->chart()->setAnimationOptions(QChart::AllAnimations); | ||||
|
r1797 | remove(); | ||
|
r1301 | } | ||
|
r1798 | void tst_qpieseries::take() | ||
{ | ||||
m_view->chart()->addSeries(m_series); | ||||
QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>))); | ||||
// add some slices | ||||
QPieSlice *slice1 = m_series->append("slice 1", 1); | ||||
QPieSlice *slice2 = m_series->append("slice 2", 2); | ||||
m_series->append("slice 3", 3); | ||||
QSignalSpy spy1(slice1, SIGNAL(destroyed())); | ||||
QCOMPARE(m_series->count(), 3); | ||||
// null pointer remove | ||||
QVERIFY(!m_series->take(0)); | ||||
// take first | ||||
QVERIFY(m_series->take(slice1)); | ||||
TRY_COMPARE(spy1.count(), 0); | ||||
QVERIFY(slice1->parent() == m_series); // series is still the parent object | ||||
QVERIFY(!m_series->take(slice1)); | ||||
QCOMPARE(m_series->count(), 2); | ||||
QCOMPARE(m_series->slices().at(0)->label(), slice2->label()); | ||||
QCOMPARE(removedSpy.count(), 1); | ||||
QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0)); | ||||
QCOMPARE(removed.count(), 1); | ||||
QCOMPARE(removed.first(), slice1); | ||||
} | ||||
void tst_qpieseries::takeAnimated() | ||||
{ | ||||
m_view->chart()->setAnimationOptions(QChart::AllAnimations); | ||||
take(); | ||||
} | ||||
|
r1105 | void tst_qpieseries::calculatedValues() | ||
{ | ||||
|
r1301 | m_view->chart()->addSeries(m_series); | ||
|
r1119 | |||
|
r1224 | QPieSlice *slice1 = new QPieSlice("slice 1", 1); | ||
|
r1274 | QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged())); | ||
QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged())); | ||||
QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged())); | ||||
|
r1224 | |||
|
r1119 | // add a slice | ||
|
r1301 | m_series->append(slice1); | ||
bool ok; | ||||
verifyCalculatedData(*m_series, &ok); | ||||
|
r1119 | if (!ok) | ||
return; | ||||
|
r1274 | QCOMPARE(percentageSpy.count(), 1); | ||
QCOMPARE(startAngleSpy.count(), 0); | ||||
QCOMPARE(angleSpanSpy.count(), 1); | ||||
|
r1105 | |||
|
r1119 | // add some more slices | ||
QList<QPieSlice *> list; | ||||
|
r1206 | list << new QPieSlice("slice 2", 2); | ||
list << new QPieSlice("slice 3", 3); | ||||
|
r1301 | m_series->append(list); | ||
verifyCalculatedData(*m_series, &ok); | ||||
|
r1119 | if (!ok) | ||
return; | ||||
|
r1274 | QCOMPARE(percentageSpy.count(), 2); | ||
QCOMPARE(startAngleSpy.count(), 0); | ||||
QCOMPARE(angleSpanSpy.count(), 2); | ||||
|
r1119 | |||
// remove a slice | ||||
|
r1301 | m_series->remove(list.first()); // remove slice 2 | ||
verifyCalculatedData(*m_series, &ok); | ||||
|
r1119 | if (!ok) | ||
return; | ||||
|
r1274 | QCOMPARE(percentageSpy.count(), 3); | ||
QCOMPARE(startAngleSpy.count(), 0); | ||||
QCOMPARE(angleSpanSpy.count(), 3); | ||||
|
r1119 | |||
// insert a slice | ||||
|
r1301 | m_series->insert(0, new QPieSlice("Slice 4", 4)); | ||
verifyCalculatedData(*m_series, &ok); | ||||
|
r1119 | if (!ok) | ||
return; | ||||
|
r1274 | QCOMPARE(percentageSpy.count(), 4); | ||
QCOMPARE(startAngleSpy.count(), 1); | ||||
QCOMPARE(angleSpanSpy.count(), 4); | ||||
|
r1119 | |||
|
r1207 | // modify pie angles | ||
|
r1301 | m_series->setPieStartAngle(-90); | ||
m_series->setPieEndAngle(90); | ||||
verifyCalculatedData(*m_series, &ok); | ||||
|
r1207 | if (!ok) | ||
return; | ||||
|
r1274 | QCOMPARE(percentageSpy.count(), 4); | ||
QCOMPARE(startAngleSpy.count(), 3); | ||||
QCOMPARE(angleSpanSpy.count(), 6); | ||||
|
r1207 | |||
|
r1119 | // clear all | ||
|
r1301 | m_series->clear(); | ||
verifyCalculatedData(*m_series, &ok); | ||||
if (!ok) | ||||
return; | ||||
|
r1274 | QCOMPARE(percentageSpy.count(), 4); | ||
QCOMPARE(startAngleSpy.count(), 3); | ||||
QCOMPARE(angleSpanSpy.count(), 6); | ||||
|
r1105 | } | ||
|
r1119 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) | ||
|
r1105 | { | ||
|
r1119 | *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); | ||||
|
r1274 | QCOMPARE(slice->angleSpan(), sliceSpan); | ||
|
r1119 | QCOMPARE(slice->percentage(), ratio); | ||
startAngle += sliceSpan; | ||||
} | ||||
if (!series.isEmpty()) | ||||
|
r1274 | QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle()); | ||
|
r1105 | |||
|
r1119 | *ok = true; | ||
|
r1105 | } | ||
void tst_qpieseries::clickedSignal() | ||||
{ | ||||
|
r2065 | // NOTE: | ||
// This test is the same as tst_qpieslice::clickedSignal() | ||||
// Just for different signals. | ||||
|
r2062 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||
|
r1301 | // add some slices | ||
QPieSlice *s1 = m_series->append("slice 1", 1); | ||||
QPieSlice *s2 = m_series->append("slice 2", 1); | ||||
QPieSlice *s3 = m_series->append("slice 3", 1); | ||||
QPieSlice *s4 = m_series->append("slice 4", 1); | ||||
QSignalSpy clickSpy(m_series, SIGNAL(clicked(QPieSlice*))); | ||||
|
r1119 | |||
// add series to the chart | ||||
|
r1301 | m_view->chart()->legend()->setVisible(false); | ||
m_view->chart()->addSeries(m_series); | ||||
m_view->show(); | ||||
QTest::qWaitForWindowShown(m_view); | ||||
|
r1119 | |||
|
r2065 | // test maximum size | ||
|
r1301 | m_series->setPieSize(1.0); | ||
|
r2065 | QRectF pieRect = m_view->chart()->plotArea(); | ||
QList<QPoint> 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<QPieSlice*>(clickSpy.at(0).at(0)), s1); | ||||
QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2); | ||||
|
r1245 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3); | ||
|
r2065 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4); | ||
|
r1245 | clickSpy.clear(); | ||
|
r2065 | // test half size | ||
|
r1301 | m_series->setPieSize(0.5); | ||
m_series->setVerticalPosition(0.25); | ||||
m_series->setHorizontalPosition(0.25); | ||||
|
r2065 | 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<QPieSlice*>(clickSpy.at(0).at(0)), s1); | ||||
QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s2); | ||||
QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3); | ||||
QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s4); | ||||
|
r1105 | } | ||
void tst_qpieseries::hoverSignal() | ||||
{ | ||||
|
r2065 | // NOTE: | ||
// This test is the same as tst_qpieslice::hoverSignal() | ||||
// Just for different signals. | ||||
|
r2062 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||
|
r1301 | // add some slices | ||
|
r2072 | m_series->append("slice 1", 1); | ||
m_series->append("slice 2", 1); | ||||
m_series->append("slice 3", 1); | ||||
m_series->append("slice 4", 1); | ||||
|
r1119 | |||
// add series to the chart | ||||
|
r1301 | m_view->chart()->legend()->setVisible(false); | ||
m_view->chart()->addSeries(m_series); | ||||
m_view->show(); | ||||
QTest::qWaitForWindowShown(m_view); | ||||
|
r1119 | |||
|
r2065 | // move inside the slices | ||
m_series->setPieSize(1.0); | ||||
QRectF pieRect = m_view->chart()->plotArea(); | ||||
QList<QPoint> points = slicePoints(pieRect); | ||||
QTest::mouseMove(m_view->viewport(), pieRect.topRight().toPoint()); | ||||
|
r1301 | QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool))); | ||
|
r2065 | 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()); | ||||
|
r2074 | QApplication::processEvents(); | ||
|
r2065 | |||
// check | ||||
QCOMPARE(hoverSpy.count(), 8); | ||||
int i = 0; | ||||
foreach (QPieSlice *s, m_series->slices()) { | ||||
QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), true); | ||||
i++; | ||||
QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(i).at(0)), s); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy.at(i).at(1)), false); | ||||
i++; | ||||
} | ||||
|
r1105 | } | ||
|
r1323 | void tst_qpieseries::sliceSeries() | ||
{ | ||||
QPieSlice *slice = new QPieSlice(); | ||||
QVERIFY(!slice->series()); | ||||
delete slice; | ||||
slice = new QPieSlice(m_series); | ||||
QVERIFY(!slice->series()); | ||||
m_series->append(slice); | ||||
QCOMPARE(slice->series(), m_series); | ||||
slice = new QPieSlice(); | ||||
m_series->insert(0, slice); | ||||
QCOMPARE(slice->series(), m_series); | ||||
|
r1798 | |||
m_series->take(slice); | ||||
QCOMPARE(slice->series(), (QPieSeries*) 0); | ||||
|
r1323 | } | ||
|
r1768 | void tst_qpieseries::destruction() | ||
{ | ||||
// add some slices | ||||
QPieSlice *slice1 = m_series->append("slice 1", 1); | ||||
QPieSlice *slice2 = m_series->append("slice 2", 2); | ||||
QPieSlice *slice3 = m_series->append("slice 3", 3); | ||||
QSignalSpy spy1(slice1, SIGNAL(destroyed())); | ||||
QSignalSpy spy2(slice2, SIGNAL(destroyed())); | ||||
QSignalSpy spy3(slice3, SIGNAL(destroyed())); | ||||
// destroy series | ||||
delete m_series; | ||||
m_series = 0; | ||||
// check that series has destroyed its slices | ||||
QCOMPARE(spy1.count(), 1); | ||||
QCOMPARE(spy2.count(), 1); | ||||
QCOMPARE(spy3.count(), 1); | ||||
} | ||||
|
r2065 | QList<QPoint> 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<QPoint> points; | ||||
points << QPoint(x2, y1); | ||||
points << QPoint(x2, y2); | ||||
points << QPoint(x1, y2); | ||||
points << QPoint(x1, y1); | ||||
return points; | ||||
} | ||||
|
r1105 | QTEST_MAIN(tst_qpieseries) | ||
#include "tst_qpieseries.moc" | ||||