tst_qpieslice.cpp
450 lines
| 14.4 KiB
| text/x-c
|
CppLexer
|
r1105 | /**************************************************************************** | ||
** | ||||
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
|
r1105 | ** All rights reserved. | ||
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
|
r1105 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r1105 | ** | ||
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
|
r2776 | ** agreement between you and The Qt Company. | ||
|
r1105 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r1105 | ** | ||
****************************************************************************/ | ||||
#include <QtTest/QtTest> | ||||
|
r1109 | #include <tst_definitions.h> | ||
|
r2714 | #include <QtCharts/QChartView> | ||
#include <QtCharts/QChart> | ||||
#include <QtCharts/QPieSlice> | ||||
#include <QtCharts/QPieSeries> | ||||
|
r1105 | |||
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
|
r1105 | |||
class tst_qpieslice : public QObject | ||||
{ | ||||
Q_OBJECT | ||||
public slots: | ||||
void initTestCase(); | ||||
void cleanupTestCase(); | ||||
void init(); | ||||
void cleanup(); | ||||
private slots: | ||||
void construction(); | ||||
void changedSignals(); | ||||
|
r1119 | void customize(); | ||
|
r2065 | void clickedSignal(); | ||
void hoverSignal(); | ||||
|
r2739 | void pressedSignal(); | ||
void releasedSignal(); | ||||
void doubleClickedSignal(); | ||||
|
r1105 | |||
private: | ||||
|
r2065 | QList<QPoint> slicePoints(QRectF rect); | ||
|
r1105 | |||
private: | ||||
}; | ||||
void tst_qpieslice::initTestCase() | ||||
{ | ||||
} | ||||
void tst_qpieslice::cleanupTestCase() | ||||
{ | ||||
|
r2733 | QTest::qWait(1); // Allow final deleteLaters to run | ||
|
r1105 | } | ||
void tst_qpieslice::init() | ||||
{ | ||||
} | ||||
void tst_qpieslice::cleanup() | ||||
{ | ||||
} | ||||
void tst_qpieslice::construction() | ||||
{ | ||||
// no params | ||||
QPieSlice slice1; | ||||
|
r1125 | QCOMPARE(slice1.value(), 0.0); | ||
|
r1105 | QVERIFY(slice1.label().isEmpty()); | ||
QVERIFY(!slice1.isLabelVisible()); | ||||
QVERIFY(!slice1.isExploded()); | ||||
QCOMPARE(slice1.pen(), QPen()); | ||||
QCOMPARE(slice1.brush(), QBrush()); | ||||
|
r1307 | QCOMPARE(slice1.labelBrush(), QBrush()); | ||
|
r1105 | QCOMPARE(slice1.labelFont(), QFont()); | ||
|
r1125 | QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value | ||
QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value | ||||
QCOMPARE(slice1.percentage(), 0.0); | ||||
QCOMPARE(slice1.startAngle(), 0.0); | ||||
|
r1274 | QCOMPARE(slice1.angleSpan(), 0.0); | ||
|
r1105 | |||
// value and label params | ||||
|
r1206 | QPieSlice slice2("foobar", 1.0); | ||
|
r1125 | QCOMPARE(slice2.value(), 1.0); | ||
|
r1105 | QCOMPARE(slice2.label(), QString("foobar")); | ||
QVERIFY(!slice2.isLabelVisible()); | ||||
QVERIFY(!slice2.isExploded()); | ||||
QCOMPARE(slice2.pen(), QPen()); | ||||
QCOMPARE(slice2.brush(), QBrush()); | ||||
|
r1307 | QCOMPARE(slice2.labelBrush(), QBrush()); | ||
|
r1105 | QCOMPARE(slice2.labelFont(), QFont()); | ||
|
r1125 | QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value | ||
QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value | ||||
QCOMPARE(slice2.percentage(), 0.0); | ||||
QCOMPARE(slice2.startAngle(), 0.0); | ||||
|
r1274 | QCOMPARE(slice2.angleSpan(), 0.0); | ||
|
r1105 | } | ||
void tst_qpieslice::changedSignals() | ||||
{ | ||||
QPieSlice slice; | ||||
|
r1224 | |||
QSignalSpy valueSpy(&slice, SIGNAL(valueChanged())); | ||||
QSignalSpy labelSpy(&slice, SIGNAL(labelChanged())); | ||||
|
r1274 | QSignalSpy penSpy(&slice, SIGNAL(penChanged())); | ||
QSignalSpy brushSpy(&slice, SIGNAL(brushChanged())); | ||||
|
r1307 | QSignalSpy labelBrushSpy(&slice, SIGNAL(labelBrushChanged())); | ||
|
r1274 | QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged())); | ||
|
r1334 | QSignalSpy colorSpy(&slice, SIGNAL(colorChanged())); | ||
QSignalSpy borderColorSpy(&slice, SIGNAL(borderColorChanged())); | ||||
QSignalSpy borderWidthSpy(&slice, SIGNAL(borderWidthChanged())); | ||||
QSignalSpy labelColorSpy(&slice, SIGNAL(labelColorChanged())); | ||||
|
r1274 | |||
// percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues() | ||||
|
r1224 | |||
// set everything twice to see we do not get unnecessary signals | ||||
|
r1280 | slice.setValue(1.0); | ||
slice.setValue(-1.0); | ||||
QCOMPARE(slice.value(), 1.0); | ||||
|
r1105 | slice.setLabel("foobar"); | ||
slice.setLabel("foobar"); | ||||
slice.setLabelVisible(); | ||||
slice.setLabelVisible(); | ||||
slice.setExploded(); | ||||
slice.setExploded(); | ||||
slice.setPen(QPen(Qt::red)); | ||||
|
r1334 | slice.setPen(QPen(QBrush(Qt::red), 3)); | ||
|
r1105 | slice.setBrush(QBrush(Qt::red)); | ||
slice.setBrush(QBrush(Qt::red)); | ||||
|
r1307 | slice.setLabelBrush(QBrush(Qt::green)); | ||
slice.setLabelBrush(QBrush(Qt::green)); | ||||
|
r1105 | slice.setLabelFont(QFont("Tahoma")); | ||
slice.setLabelFont(QFont("Tahoma")); | ||||
|
r1759 | slice.setLabelPosition(QPieSlice::LabelInsideHorizontal); | ||
slice.setLabelPosition(QPieSlice::LabelInsideHorizontal); | ||||
|
r1105 | slice.setLabelArmLengthFactor(0.1); | ||
slice.setLabelArmLengthFactor(0.1); | ||||
slice.setExplodeDistanceFactor(0.1); | ||||
slice.setExplodeDistanceFactor(0.1); | ||||
|
r1224 | |||
TRY_COMPARE(valueSpy.count(), 1); | ||||
TRY_COMPARE(labelSpy.count(), 1); | ||||
|
r1334 | TRY_COMPARE(penSpy.count(), 2); | ||
|
r1274 | TRY_COMPARE(brushSpy.count(), 1); | ||
|
r1307 | TRY_COMPARE(labelBrushSpy.count(), 1); | ||
|
r1274 | TRY_COMPARE(labelFontSpy.count(), 1); | ||
|
r1334 | TRY_COMPARE(colorSpy.count(), 1); | ||
TRY_COMPARE(borderColorSpy.count(), 1); | ||||
TRY_COMPARE(borderWidthSpy.count(), 1); | ||||
TRY_COMPARE(labelColorSpy.count(), 1); | ||||
|
r1105 | } | ||
|
r1119 | void tst_qpieslice::customize() | ||
{ | ||||
// create a pie series | ||||
QPieSeries *series = new QPieSeries(); | ||||
|
r1206 | QPieSlice *s1 = series->append("slice 1", 1); | ||
QPieSlice *s2 = series->append("slice 2", 2); | ||||
series->append("slice 3", 3); | ||||
|
r1119 | |||
// customize a slice | ||||
QPen p1(Qt::red); | ||||
s1->setPen(p1); | ||||
QBrush b1(Qt::red); | ||||
s1->setBrush(b1); | ||||
|
r1307 | s1->setLabelBrush(b1); | ||
|
r1119 | 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); | ||||
|
r1307 | QCOMPARE(s1->labelBrush(), b1); | ||
|
r1119 | QCOMPARE(s1->labelFont(), f1); | ||
// remove a slice | ||||
series->remove(s2); | ||||
QCOMPARE(s1->pen(), p1); | ||||
QCOMPARE(s1->brush(), b1); | ||||
|
r1307 | QCOMPARE(s1->labelBrush(), b1); | ||
|
r1119 | QCOMPARE(s1->labelFont(), f1); | ||
// add a slice | ||||
|
r1206 | series->append("slice 4", 4); | ||
|
r1119 | QCOMPARE(s1->pen(), p1); | ||
QCOMPARE(s1->brush(), b1); | ||||
|
r1307 | QCOMPARE(s1->labelBrush(), b1); | ||
|
r1119 | QCOMPARE(s1->labelFont(), f1); | ||
// insert a slice | ||||
|
r1206 | series->insert(0, new QPieSlice("slice 5", 5)); | ||
|
r1119 | QCOMPARE(s1->pen(), p1); | ||
QCOMPARE(s1->brush(), b1); | ||||
|
r1307 | QCOMPARE(s1->labelBrush(), b1); | ||
|
r1119 | QCOMPARE(s1->labelFont(), f1); | ||
// change theme | ||||
// theme will overwrite customizations | ||||
view.chart()->setTheme(QChart::ChartThemeHighContrast); | ||||
QVERIFY(s1->pen() != p1); | ||||
QVERIFY(s1->brush() != b1); | ||||
|
r1307 | QVERIFY(s1->labelBrush() != b1); | ||
|
r1119 | QVERIFY(s1->labelFont() != f1); | ||
} | ||||
|
r2065 | void tst_qpieslice::clickedSignal() | ||
|
r1119 | { | ||
|
r2065 | // NOTE: | ||
// This test is the same as tst_qpieseries::clickedSignal() | ||||
// Just for different signals. | ||||
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||||
|
r1119 | // create a pie series | ||
QPieSeries *series = new QPieSeries(); | ||||
|
r1206 | QPieSlice *s1 = series->append("slice 1", 1); | ||
|
r2065 | QPieSlice *s2 = series->append("slice 2", 1); | ||
QPieSlice *s3 = series->append("slice 3", 1); | ||||
QPieSlice *s4 = series->append("slice 4", 1); | ||||
|
r1119 | QSignalSpy clickSpy1(s1, SIGNAL(clicked())); | ||
QSignalSpy clickSpy2(s2, SIGNAL(clicked())); | ||||
QSignalSpy clickSpy3(s3, SIGNAL(clicked())); | ||||
|
r2065 | QSignalSpy clickSpy4(s4, SIGNAL(clicked())); | ||
|
r1119 | |||
// add series to the chart | ||||
|
r2065 | QChartView view; | ||
|
r1192 | view.chart()->legend()->setVisible(false); | ||
|
r1119 | view.chart()->addSeries(series); | ||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
// simulate clicks | ||||
|
r2065 | series->setPieSize(1.0); | ||
QRectF pieRect = view.chart()->plotArea(); | ||||
QList<QPoint> 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)); | ||||
|
r1119 | QCOMPARE(clickSpy1.count(), 1); | ||
QCOMPARE(clickSpy2.count(), 1); | ||||
QCOMPARE(clickSpy3.count(), 1); | ||||
|
r2065 | QCOMPARE(clickSpy4.count(), 1); | ||
|
r1119 | } | ||
|
r2065 | void tst_qpieslice::hoverSignal() | ||
|
r1119 | { | ||
|
r2065 | // NOTE: | ||
// This test is the same as tst_qpieseries::hoverSignal() | ||||
// Just for different signals. | ||||
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||||
// add some slices | ||||
|
r1119 | QPieSeries *series = new QPieSeries(); | ||
|
r1206 | QPieSlice *s1 = series->append("slice 1", 1); | ||
|
r2065 | QPieSlice *s2 = series->append("slice 2", 1); | ||
QPieSlice *s3 = series->append("slice 3", 1); | ||||
QPieSlice *s4 = series->append("slice 4", 1); | ||||
|
r1119 | |||
// add series to the chart | ||||
|
r2065 | QChartView view; | ||
|
r1192 | view.chart()->legend()->setVisible(false); | ||
|
r1119 | view.chart()->addSeries(series); | ||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
|
r2078 | // try to ensure focus | ||
QApplication::setActiveWindow(&view); | ||||
view.setFocus(); | ||||
QApplication::processEvents(); | ||||
QVERIFY(view.isActiveWindow()); | ||||
QVERIFY(view.hasFocus()); | ||||
|
r2065 | // move inside the slices | ||
series->setPieSize(1.0); | ||||
QRectF pieRect = view.chart()->plotArea(); | ||||
QList<QPoint> points = slicePoints(pieRect); | ||||
|
r2076 | QTest::mouseMove(view.viewport(), pieRect.topRight().toPoint(), 100); | ||
|
r2065 | QSignalSpy hoverSpy1(s1, SIGNAL(hovered(bool))); | ||
QSignalSpy hoverSpy2(s2, SIGNAL(hovered(bool))); | ||||
QSignalSpy hoverSpy3(s3, SIGNAL(hovered(bool))); | ||||
QSignalSpy hoverSpy4(s4, SIGNAL(hovered(bool))); | ||||
|
r2075 | QTest::mouseMove(view.viewport(), points.at(0), 100); | ||
QTest::mouseMove(view.viewport(), points.at(1), 100); | ||||
QTest::mouseMove(view.viewport(), points.at(2), 100); | ||||
QTest::mouseMove(view.viewport(), points.at(3), 100); | ||||
QTest::mouseMove(view.viewport(), pieRect.topLeft().toPoint(), 100); | ||||
|
r2422 | // Final hoverevent can take few milliseconds to appear in some environments, so wait a bit | ||
QTest::qWait(100); | ||||
|
r2065 | |||
// check | ||||
QCOMPARE(hoverSpy1.count(), 2); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(0).at(0)), true); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy1.at(1).at(0)), false); | ||||
QCOMPARE(hoverSpy2.count(), 2); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(0).at(0)), true); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy2.at(1).at(0)), false); | ||||
QCOMPARE(hoverSpy3.count(), 2); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(0).at(0)), true); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy3.at(1).at(0)), false); | ||||
QCOMPARE(hoverSpy4.count(), 2); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(0).at(0)), true); | ||||
QCOMPARE(qvariant_cast<bool>(hoverSpy4.at(1).at(0)), false); | ||||
} | ||||
QList<QPoint> 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<QPoint> points; | ||||
points << QPoint(x2, y1); | ||||
points << QPoint(x2, y2); | ||||
points << QPoint(x1, y2); | ||||
points << QPoint(x1, y1); | ||||
return points; | ||||
|
r1119 | } | ||
|
r2739 | void tst_qpieslice::pressedSignal() | ||
{ | ||||
// NOTE: | ||||
// This test is the same as tst_qpieseries::pressedSignal() | ||||
// Just for different signals. | ||||
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||||
// create a pie series | ||||
QPieSeries *series = new QPieSeries(); | ||||
QPieSlice *s1 = series->append("slice 1", 1); | ||||
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(pressed())); | ||||
QSignalSpy clickSpy2(s2, SIGNAL(pressed())); | ||||
QSignalSpy clickSpy3(s3, SIGNAL(pressed())); | ||||
QSignalSpy clickSpy4(s4, SIGNAL(pressed())); | ||||
// add series to the chart | ||||
QChartView view; | ||||
view.chart()->legend()->setVisible(false); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
// simulate clicks | ||||
series->setPieSize(1.0); | ||||
QRectF pieRect = view.chart()->plotArea(); | ||||
QList<QPoint> 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::releasedSignal() | ||||
{ | ||||
// NOTE: | ||||
// This test is the same as tst_qpieseries::releasedSignal() | ||||
// Just for different signals. | ||||
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||||
// create a pie series | ||||
QPieSeries *series = new QPieSeries(); | ||||
QPieSlice *s1 = series->append("slice 1", 1); | ||||
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(released())); | ||||
QSignalSpy clickSpy2(s2, SIGNAL(released())); | ||||
QSignalSpy clickSpy3(s3, SIGNAL(released())); | ||||
QSignalSpy clickSpy4(s4, SIGNAL(released())); | ||||
// add series to the chart | ||||
QChartView view; | ||||
view.chart()->legend()->setVisible(false); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
// simulate clicks | ||||
series->setPieSize(1.0); | ||||
QRectF pieRect = view.chart()->plotArea(); | ||||
QList<QPoint> 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::doubleClickedSignal() | ||||
{ | ||||
// NOTE: | ||||
// This test is the same as tst_qpieseries::doubleClickedSignal() | ||||
// Just for different signals. | ||||
SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||||
// create a pie series | ||||
QPieSeries *series = new QPieSeries(); | ||||
QPieSlice *s1 = series->append("slice 1", 1); | ||||
QSignalSpy clickSpy1(s1, SIGNAL(doubleClicked())); | ||||
// add series to the chart | ||||
QChartView view; | ||||
view.chart()->legend()->setVisible(false); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
// simulate clicks | ||||
series->setPieSize(1.0); | ||||
QRectF pieRect = view.chart()->plotArea(); | ||||
QList<QPoint> points = slicePoints(pieRect); | ||||
QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, points.at(0)); | ||||
QCOMPARE(clickSpy1.count(), 1); | ||||
} | ||||
|
r1105 | QTEST_MAIN(tst_qpieslice) | ||
#include "tst_qpieslice.moc" | ||||