tst_qbarseries.cpp
707 lines
| 21.1 KiB
| text/x-c
|
CppLexer
|
r1100 | /**************************************************************************** | ||
** | ||||
|
r2433 | ** Copyright (C) 2013 Digia Plc | ||
|
r1100 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r1100 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r1100 | ** 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> | ||||
|
r1594 | #include <qbarseries.h> | ||
|
r1106 | #include <qbarset.h> | ||
|
r1145 | #include <qchartview.h> | ||
#include <qchart.h> | ||||
|
r1466 | #include "tst_definitions.h" | ||
|
r1100 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
|
r1145 | Q_DECLARE_METATYPE(QBarSet*) | ||
|
r1802 | Q_DECLARE_METATYPE(QList<QBarSet*>) | ||
|
r1145 | |||
|
r1100 | class tst_QBarSeries : public QObject | ||
{ | ||||
Q_OBJECT | ||||
public slots: | ||||
void initTestCase(); | ||||
void cleanupTestCase(); | ||||
void init(); | ||||
void cleanup(); | ||||
private slots: | ||||
void qbarseries_data(); | ||||
void qbarseries(); | ||||
void type_data(); | ||||
void type(); | ||||
|
r1194 | void append_data(); | ||
void append(); | ||||
void remove_data(); | ||||
void remove(); | ||||
|
r1802 | void take_data(); | ||
void take(); | ||||
|
r1194 | void appendList_data(); | ||
void appendList(); | ||||
|
r1463 | void count_data(); | ||
void count(); | ||||
|
r1101 | void barSets_data(); | ||
void barSets(); | ||||
void setLabelsVisible_data(); | ||||
void setLabelsVisible(); | ||||
|
r2067 | void opacity(); | ||
|
r1146 | void mouseclicked_data(); | ||
void mouseclicked(); | ||||
void mousehovered_data(); | ||||
void mousehovered(); | ||||
|
r1375 | void clearWithAnimations(); | ||
|
r1768 | void destruction(); | ||
|
r1100 | |||
private: | ||||
|
r1594 | QBarSeries* m_barseries; | ||
QBarSeries* m_barseries_with_sets; | ||||
|
r1101 | |||
QList<QBarSet*> m_testSets; | ||||
|
r1100 | }; | ||
void tst_QBarSeries::initTestCase() | ||||
{ | ||||
|
r1145 | qRegisterMetaType<QBarSet*>("QBarSet*"); | ||
|
r1803 | qRegisterMetaType<QList<QBarSet*> >("QList<QBarSet*>"); | ||
|
r1100 | } | ||
void tst_QBarSeries::cleanupTestCase() | ||||
{ | ||||
} | ||||
void tst_QBarSeries::init() | ||||
{ | ||||
|
r1594 | m_barseries = new QBarSeries(); | ||
m_barseries_with_sets = new QBarSeries(); | ||||
|
r1101 | |||
for (int i=0; i<5; i++) { | ||||
m_testSets.append(new QBarSet("testset")); | ||||
|
r1194 | m_barseries_with_sets->append(m_testSets.at(i)); | ||
|
r1101 | } | ||
|
r1100 | } | ||
void tst_QBarSeries::cleanup() | ||||
{ | ||||
|
r2100 | foreach (QBarSet* s, m_testSets) { | ||
|
r1194 | m_barseries_with_sets->remove(s); | ||
|
r1101 | } | ||
m_testSets.clear(); | ||||
|
r1100 | delete m_barseries; | ||
m_barseries = 0; | ||||
|
r1101 | delete m_barseries_with_sets; | ||
m_barseries_with_sets = 0; | ||||
|
r1100 | } | ||
void tst_QBarSeries::qbarseries_data() | ||||
{ | ||||
} | ||||
void tst_QBarSeries::qbarseries() | ||||
{ | ||||
|
r1594 | QBarSeries *barseries = new QBarSeries(); | ||
|
r1100 | QVERIFY(barseries != 0); | ||
} | ||||
void tst_QBarSeries::type_data() | ||||
{ | ||||
} | ||||
void tst_QBarSeries::type() | ||||
{ | ||||
QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar); | ||||
} | ||||
|
r1194 | void tst_QBarSeries::append_data() | ||
|
r1100 | { | ||
} | ||||
|
r1194 | void tst_QBarSeries::append() | ||
|
r1100 | { | ||
|
r1463 | QVERIFY(m_barseries->count() == 0); | ||
|
r1100 | |||
|
r1122 | bool ret = false; | ||
// Try adding barset | ||||
|
r1100 | QBarSet *barset = new QBarSet("testset"); | ||
|
r1194 | ret = m_barseries->append(barset); | ||
|
r1100 | |||
|
r1122 | QVERIFY(ret == true); | ||
|
r1463 | QVERIFY(m_barseries->count() == 1); | ||
|
r1101 | |||
|
r1122 | // Try adding another set | ||
|
r1101 | QBarSet *barset2 = new QBarSet("testset2"); | ||
|
r1194 | ret = m_barseries->append(barset2); | ||
|
r1122 | |||
QVERIFY(ret == true); | ||||
|
r1463 | QVERIFY(m_barseries->count() == 2); | ||
|
r1101 | |||
|
r1122 | // Try adding same set again | ||
|
r1194 | ret = m_barseries->append(barset2); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries->count() == 2); | ||
|
r1122 | |||
// Try adding null set | ||||
|
r1194 | ret = m_barseries->append(0); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries->count() == 2); | ||
|
r1122 | |||
|
r1101 | } | ||
|
r1194 | void tst_QBarSeries::remove_data() | ||
|
r1101 | { | ||
} | ||||
|
r1194 | void tst_QBarSeries::remove() | ||
|
r1101 | { | ||
int count = m_testSets.count(); | ||||
|
r1463 | QVERIFY(m_barseries_with_sets->count() == count); | ||
|
r1101 | |||
|
r1122 | // Try to remove null pointer (should not remove, should not crash) | ||
bool ret = false; | ||||
|
r1194 | ret = m_barseries_with_sets->remove(0); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries_with_sets->count() == count); | ||
|
r1122 | |||
// Try to remove invalid pointer (should not remove, should not crash) | ||||
|
r1194 | ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) ); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries_with_sets->count() == count); | ||
|
r1122 | |||
|
r1101 | // remove some sets | ||
|
r1194 | ret = m_barseries_with_sets->remove(m_testSets.at(2)); | ||
|
r1122 | QVERIFY(ret == true); | ||
|
r1194 | ret = m_barseries_with_sets->remove(m_testSets.at(3)); | ||
|
r1122 | QVERIFY(ret == true); | ||
|
r1194 | ret = m_barseries_with_sets->remove(m_testSets.at(4)); | ||
|
r1122 | QVERIFY(ret == true); | ||
|
r1101 | |||
|
r1463 | QVERIFY(m_barseries_with_sets->count() == 2); | ||
|
r1101 | |||
QList<QBarSet*> verifysets = m_barseries_with_sets->barSets(); | ||||
QVERIFY(verifysets.at(0) == m_testSets.at(0)); | ||||
QVERIFY(verifysets.at(1) == m_testSets.at(1)); | ||||
|
r1122 | // Try removing all sets again (should be ok, even if some sets have already been removed) | ||
ret = false; | ||||
|
r1101 | for (int i=0; i<count; i++) { | ||
|
r1194 | ret |= m_barseries_with_sets->remove(m_testSets.at(i)); | ||
|
r1101 | } | ||
|
r1122 | QVERIFY(ret == true); | ||
|
r1463 | QVERIFY(m_barseries_with_sets->count() == 0); | ||
|
r1101 | } | ||
|
r1802 | void tst_QBarSeries::take_data() | ||
{ | ||||
} | ||||
void tst_QBarSeries::take() | ||||
{ | ||||
int count = m_testSets.count(); | ||||
QVERIFY(m_barseries_with_sets->count() == count); | ||||
QSignalSpy countSpy(m_barseries_with_sets,SIGNAL(countChanged())); | ||||
|
r2110 | QSignalSpy removedSpy(m_barseries_with_sets,SIGNAL(barsetsRemoved(QList<QBarSet*>))); | ||
|
r1802 | |||
for (int i=0; i<m_testSets.count(); i++) { | ||||
QBarSet* set = m_testSets.at(i); | ||||
bool success = m_barseries_with_sets->take(set); | ||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||||
QVERIFY(success); | ||||
TRY_COMPARE(countSpy.count(),1); | ||||
TRY_COMPARE(removedSpy.count(),1); | ||||
QList<QVariant> removedSpyArg = removedSpy.takeFirst(); | ||||
|
r1803 | QList<QBarSet*> removedSets = qvariant_cast<QList<QBarSet*> > (removedSpyArg.at(0)); | ||
|
r1802 | QCOMPARE(removedSets.at(0), m_testSets.at(i)); | ||
countSpy.takeFirst(); | ||||
} | ||||
} | ||||
|
r1194 | void tst_QBarSeries::appendList_data() | ||
|
r1101 | { | ||
} | ||||
|
r1194 | void tst_QBarSeries::appendList() | ||
|
r1101 | { | ||
int count = 5; | ||||
|
r1463 | QVERIFY(m_barseries->count() == 0); | ||
|
r1101 | |||
QList<QBarSet*> sets; | ||||
for (int i=0; i<count; i++) { | ||||
sets.append(new QBarSet("testset")); | ||||
} | ||||
|
r1122 | // Append new sets (should succeed, count should match the count of sets) | ||
bool ret = false; | ||||
|
r1194 | ret = m_barseries->append(sets); | ||
|
r1122 | QVERIFY(ret == true); | ||
|
r1463 | QVERIFY(m_barseries->count() == count); | ||
|
r1122 | |||
// Append same sets again (should fail, count should remain same) | ||||
|
r1194 | ret = m_barseries->append(sets); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries->count() == count); | ||
|
r1122 | |||
// Try append empty list (should succeed, but count should remain same) | ||||
QList<QBarSet*> invalidList; | ||||
|
r1194 | ret = m_barseries->append(invalidList); | ||
|
r1122 | QVERIFY(ret == true); | ||
|
r1463 | QVERIFY(m_barseries->count() == count); | ||
|
r1122 | |||
// Try append list with one new and one existing set (should fail, count remains same) | ||||
invalidList.append(new QBarSet("ok set")); | ||||
invalidList.append(sets.at(0)); | ||||
|
r1194 | ret = m_barseries->append(invalidList); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries->count() == count); | ||
|
r1122 | |||
// Try append list with null pointers (should fail, count remains same) | ||||
QList<QBarSet*> invalidList2; | ||||
invalidList2.append(0); | ||||
invalidList2.append(0); | ||||
invalidList2.append(0); | ||||
|
r1194 | ret = m_barseries->append(invalidList2); | ||
|
r1122 | QVERIFY(ret == false); | ||
|
r1463 | QVERIFY(m_barseries->count() == count); | ||
|
r1101 | } | ||
|
r1463 | void tst_QBarSeries::count_data() | ||
|
r1101 | { | ||
} | ||||
|
r1463 | void tst_QBarSeries::count() | ||
|
r1101 | { | ||
|
r1463 | QVERIFY(m_barseries->count() == 0); | ||
QVERIFY(m_barseries_with_sets->count() == m_testSets.count()); | ||||
|
r1101 | } | ||
void tst_QBarSeries::barSets_data() | ||||
{ | ||||
} | ||||
void tst_QBarSeries::barSets() | ||||
{ | ||||
QVERIFY(m_barseries->barSets().count() == 0); | ||||
QList<QBarSet*> sets = m_barseries_with_sets->barSets(); | ||||
QVERIFY(sets.count() == m_testSets.count()); | ||||
for (int i=0; i<m_testSets.count(); i++) { | ||||
QVERIFY(sets.at(i) == m_testSets.at(i)); | ||||
} | ||||
} | ||||
void tst_QBarSeries::setLabelsVisible_data() | ||||
{ | ||||
} | ||||
void tst_QBarSeries::setLabelsVisible() | ||||
{ | ||||
|
r1123 | // labels should be invisible by default | ||
|
r1246 | QVERIFY(m_barseries->isLabelsVisible() == false); | ||
QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); | ||||
|
r1101 | |||
|
r1123 | // turn labels to visible | ||
|
r1101 | m_barseries_with_sets->setLabelsVisible(true); | ||
|
r1246 | // TODO: test the signal | ||
QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); | ||||
|
r1101 | |||
|
r1123 | // turn labels to invisible | ||
|
r1101 | m_barseries_with_sets->setLabelsVisible(false); | ||
|
r1246 | // TODO: test the signal | ||
QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); | ||||
|
r1123 | |||
// without parameter, should turn labels to visible | ||||
m_barseries_with_sets->setLabelsVisible(); | ||||
|
r1246 | // TODO: test the signal | ||
QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); | ||||
|
r1100 | } | ||
|
r2067 | void tst_QBarSeries::opacity() | ||
{ | ||||
QSignalSpy opacitySpy(m_barseries, SIGNAL(opacityChanged())); | ||||
QCOMPARE(m_barseries->opacity(), 1.0); | ||||
m_barseries->setOpacity(0.5); | ||||
QCOMPARE(m_barseries->opacity(), 0.5); | ||||
QCOMPARE(opacitySpy.count(), 1); | ||||
m_barseries->setOpacity(0.0); | ||||
QCOMPARE(m_barseries->opacity(), 0.0); | ||||
QCOMPARE(opacitySpy.count(), 2); | ||||
m_barseries->setOpacity(1.0); | ||||
QCOMPARE(m_barseries->opacity(), 1.0); | ||||
QCOMPARE(opacitySpy.count(), 3); | ||||
} | ||||
|
r1146 | void tst_QBarSeries::mouseclicked_data() | ||
|
r1145 | { | ||
} | ||||
|
r1146 | void tst_QBarSeries::mouseclicked() | ||
|
r1145 | { | ||
|
r2066 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||
|
r1594 | QBarSeries* series = new QBarSeries(); | ||
|
r1145 | |||
QBarSet* set1 = new QBarSet(QString("set 1")); | ||||
|
r1580 | *set1 << 10 << 10 << 10; | ||
|
r1194 | series->append(set1); | ||
|
r1145 | |||
QBarSet* set2 = new QBarSet(QString("set 2")); | ||||
|
r1580 | *set2 << 10 << 10 << 10; | ||
|
r1194 | series->append(set2); | ||
|
r2073 | QList<QBarSet*> barSets = series->barSets(); | ||
|
r1145 | |||
|
r1563 | QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*))); | ||
|
r1490 | QSignalSpy setSpy1(set1, SIGNAL(clicked(int))); | ||
QSignalSpy setSpy2(set2, SIGNAL(clicked(int))); | ||||
|
r1145 | |||
QChartView view(new QChart()); | ||||
view.resize(400,300); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
|
r2073 | // Calculate expected layout for bars | ||
QRectF plotArea = view.chart()->plotArea(); | ||||
qreal width = plotArea.width(); | ||||
qreal height = plotArea.height(); | ||||
qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10 | ||||
qreal rangeX = 3; // 3 values per set | ||||
qreal scaleY = (height / rangeY); | ||||
qreal scaleX = (width / rangeX); | ||||
qreal setCount = series->count(); | ||||
qreal domainMinY = 0; // These come from internal domain used by barseries. | ||||
qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. | ||||
qreal rectWidth = (scaleX / setCount) * series->barWidth(); | ||||
QVector<QRectF> layout; | ||||
// 3 = count of values in set | ||||
// Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) | ||||
for (int i = 0; i < 3; i++) { | ||||
qreal yPos = height + scaleY * domainMinY + plotArea.top(); | ||||
for (int set = 0; set < setCount; set++) { | ||||
qreal xPos = (i - domainMinX) * scaleX + plotArea.left(); | ||||
xPos -= series->count()*rectWidth/2; | ||||
xPos += set*rectWidth; | ||||
qreal rectHeight = barSets.at(set)->at(i) * scaleY; | ||||
QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); | ||||
layout.append(rect); | ||||
} | ||||
} | ||||
|
r1145 | //==================================================================================== | ||
|
r1386 | // barset 1, bar 0 | ||
|
r2073 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint()); | ||
|
r1145 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||
QCOMPARE(seriesSpy.count(), 1); | ||||
|
r1490 | QCOMPARE(setSpy1.count(), 1); | ||
QCOMPARE(setSpy2.count(), 0); | ||||
|
r1145 | |||
QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(seriesSpyArg.at(0).toInt() == 0); | ||||
|
r1145 | |||
|
r1490 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(setSpyArg.at(0).toInt() == 0); | ||||
|
r1145 | //==================================================================================== | ||
|
r1386 | // barset 1, bar 1 | ||
|
r2073 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint()); | ||
|
r1145 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||
QCOMPARE(seriesSpy.count(), 1); | ||||
|
r1490 | QCOMPARE(setSpy1.count(), 1); | ||
QCOMPARE(setSpy2.count(), 0); | ||||
|
r1145 | |||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(seriesSpyArg.at(0).toInt() == 1); | ||||
|
r1145 | |||
|
r1490 | setSpyArg = setSpy1.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(setSpyArg.at(0).toInt() == 1); | ||||
|
r1145 | //==================================================================================== | ||
|
r1386 | // barset 1, bar 2 | ||
|
r2073 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint()); | ||
|
r1145 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||
QCOMPARE(seriesSpy.count(), 1); | ||||
|
r1490 | QCOMPARE(setSpy1.count(), 1); | ||
QCOMPARE(setSpy2.count(), 0); | ||||
|
r1145 | |||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(seriesSpyArg.at(0).toInt() == 2); | ||||
|
r1145 | |||
|
r1490 | setSpyArg = setSpy1.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(setSpyArg.at(0).toInt() == 2); | ||||
|
r1145 | //==================================================================================== | ||
|
r1386 | // barset 2, bar 0 | ||
|
r2073 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint()); | ||
|
r1145 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||
QCOMPARE(seriesSpy.count(), 1); | ||||
|
r1490 | QCOMPARE(setSpy1.count(), 0); | ||
QCOMPARE(setSpy2.count(), 1); | ||||
|
r1145 | |||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(seriesSpyArg.at(0).toInt() == 0); | ||||
|
r1145 | |||
|
r1490 | setSpyArg = setSpy2.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(setSpyArg.at(0).toInt() == 0); | ||||
|
r1145 | //==================================================================================== | ||
|
r1386 | // barset 2, bar 1 | ||
|
r2073 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint()); | ||
|
r1145 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||
QCOMPARE(seriesSpy.count(), 1); | ||||
|
r1490 | QCOMPARE(setSpy1.count(), 0); | ||
QCOMPARE(setSpy2.count(), 1); | ||||
|
r1145 | |||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(seriesSpyArg.at(0).toInt() == 1); | ||||
|
r1145 | |||
|
r1490 | setSpyArg = setSpy2.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(setSpyArg.at(0).toInt() == 1); | ||||
|
r1145 | //==================================================================================== | ||
|
r1386 | // barset 2, bar 2 | ||
|
r2073 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint()); | ||
|
r1145 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | ||
QCOMPARE(seriesSpy.count(), 1); | ||||
|
r1490 | QCOMPARE(setSpy1.count(), 0); | ||
QCOMPARE(setSpy2.count(), 1); | ||||
|
r1145 | |||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(seriesSpyArg.at(0).toInt() == 2); | ||||
|
r1490 | |||
setSpyArg = setSpy2.takeFirst(); | ||||
QVERIFY(setSpyArg.at(0).type() == QVariant::Int); | ||||
QVERIFY(setSpyArg.at(0).toInt() == 2); | ||||
|
r1145 | } | ||
|
r1146 | void tst_QBarSeries::mousehovered_data() | ||
{ | ||||
} | ||||
void tst_QBarSeries::mousehovered() | ||||
{ | ||||
|
r2066 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); | ||
|
r1594 | QBarSeries* series = new QBarSeries(); | ||
|
r1146 | |||
QBarSet* set1 = new QBarSet(QString("set 1")); | ||||
|
r1580 | *set1 << 10 << 10 << 10; | ||
|
r1194 | series->append(set1); | ||
|
r1146 | |||
QBarSet* set2 = new QBarSet(QString("set 2")); | ||||
|
r1580 | *set2 << 10 << 10 << 10; | ||
|
r1194 | series->append(set2); | ||
|
r2073 | QList<QBarSet*> barSets = series->barSets(); | ||
|
r1146 | |||
|
r1563 | QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*))); | ||
|
r1490 | QSignalSpy setSpy1(set1, SIGNAL(hovered(bool))); | ||
QSignalSpy setSpy2(set2, SIGNAL(hovered(bool))); | ||||
|
r1146 | |||
QChartView view(new QChart()); | ||||
view.resize(400,300); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
QTest::qWaitForWindowShown(&view); | ||||
|
r1287 | //this is hack since view does not get events otherwise | ||
view.setMouseTracking(true); | ||||
|
r2073 | // Calculate expected layout for bars | ||
QRectF plotArea = view.chart()->plotArea(); | ||||
qreal width = plotArea.width(); | ||||
qreal height = plotArea.height(); | ||||
qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10 | ||||
qreal rangeX = 3; // 3 values per set | ||||
qreal scaleY = (height / rangeY); | ||||
qreal scaleX = (width / rangeX); | ||||
qreal setCount = series->count(); | ||||
qreal domainMinY = 0; // These come from internal domain used by barseries. | ||||
qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values. | ||||
qreal rectWidth = (scaleX / setCount) * series->barWidth(); | ||||
QVector<QRectF> layout; | ||||
// 3 = count of values in set | ||||
// Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.) | ||||
for (int i = 0; i < 3; i++) { | ||||
qreal yPos = height + scaleY * domainMinY + plotArea.top(); | ||||
for (int set = 0; set < setCount; set++) { | ||||
qreal xPos = (i - domainMinX) * scaleX + plotArea.left(); | ||||
xPos -= series->count()*rectWidth/2; | ||||
xPos += set*rectWidth; | ||||
qreal rectHeight = barSets.at(set)->at(i) * scaleY; | ||||
QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); | ||||
layout.append(rect); | ||||
} | ||||
} | ||||
|
r1146 | //======================================================================= | ||
// move mouse to left border | ||||
|
r2073 | QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y())); | ||
|
r1466 | QCoreApplication::processEvents(QEventLoop::AllEvents, 10000); | ||
TRY_COMPARE(seriesSpy.count(), 0); | ||||
|
r1490 | TRY_COMPARE(setSpy1.count(), 0); | ||
TRY_COMPARE(setSpy2.count(), 0); | ||||
|
r1146 | |||
//======================================================================= | ||||
// move mouse on top of set1 | ||||
|
r2073 | QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint()); | ||
|
r1466 | TRY_COMPARE(seriesSpy.count(), 1); | ||
|
r1490 | TRY_COMPARE(setSpy1.count(), 1); | ||
TRY_COMPARE(setSpy2.count(), 0); | ||||
|
r1146 | |||
QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(seriesSpyArg.at(0).toBool() == true); | ||||
|
r1146 | |||
|
r1490 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(setSpyArg.at(0).toBool() == true); | ||||
|
r1146 | //======================================================================= | ||
// move mouse from top of set1 to top of set2 | ||||
|
r2073 | QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint()); | ||
|
r1466 | TRY_COMPARE(seriesSpy.count(), 2); | ||
|
r1490 | TRY_COMPARE(setSpy1.count(), 1); | ||
TRY_COMPARE(setSpy2.count(), 1); | ||||
|
r1146 | |||
// should leave set1 | ||||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(seriesSpyArg.at(0).toBool() == false); | ||||
|
r1146 | |||
|
r1490 | setSpyArg = setSpy1.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(setSpyArg.at(0).toBool() == false); | ||||
|
r1146 | // should enter set2 | ||
seriesSpyArg = seriesSpy.takeFirst(); | ||||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(seriesSpyArg.at(0).toBool() == true); | ||||
|
r1146 | |||
|
r1490 | setSpyArg = setSpy2.takeFirst(); | ||
QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(setSpyArg.at(0).toBool() == true); | ||||
|
r1146 | //======================================================================= | ||
// move mouse from top of set2 to background | ||||
|
r2073 | QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0)); | ||
|
r1466 | TRY_COMPARE(seriesSpy.count(), 1); | ||
|
r1490 | TRY_COMPARE(setSpy1.count(), 0); | ||
TRY_COMPARE(setSpy2.count(), 1); | ||||
|
r1146 | |||
|
r1287 | // should leave set2 | ||
|
r1146 | seriesSpyArg = seriesSpy.takeFirst(); | ||
|
r1563 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2); | ||
QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(seriesSpyArg.at(0).toBool() == false); | ||||
|
r1490 | |||
setSpyArg = setSpy2.takeFirst(); | ||||
QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); | ||||
QVERIFY(setSpyArg.at(0).toBool() == false); | ||||
|
r1146 | } | ||
|
r1375 | void tst_QBarSeries::clearWithAnimations() | ||
{ | ||||
|
r1594 | QBarSeries* series = new QBarSeries(); | ||
|
r1375 | |||
QBarSet* set1 = new QBarSet(QString("set 1")); | ||||
|
r1580 | *set1 << 10 << 10 << 10; | ||
|
r1375 | series->append(set1); | ||
QBarSet* set2 = new QBarSet(QString("set 2")); | ||||
|
r1580 | *set2 << 10 << 10 << 10; | ||
|
r1375 | series->append(set2); | ||
QChartView view(new QChart()); | ||||
view.resize(400,300); | ||||
view.chart()->setAnimationOptions(QChart::SeriesAnimations); | ||||
view.chart()->addSeries(series); | ||||
view.show(); | ||||
series->clear(); | ||||
} | ||||
|
r1768 | void tst_QBarSeries::destruction() | ||
{ | ||||
// add a barset | ||||
QBarSeries *series = new QBarSeries(); | ||||
QBarSet *set = new QBarSet("testset"); | ||||
QSignalSpy spy1(set, SIGNAL(destroyed())); | ||||
series->append(set); | ||||
// delete the series | ||||
delete series; | ||||
// check that series deletes the set | ||||
QCOMPARE(spy1.count(), 1); | ||||
} | ||||
|
r1100 | QTEST_MAIN(tst_QBarSeries) | ||
#include "tst_qbarseries.moc" | ||||