tst_chartdataset.cpp
407 lines
| 11.8 KiB
| text/x-c
|
CppLexer
|
r2854 | /**************************************************************************** | ||
|
r771 | ** | ||
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
|
r771 | ** | ||
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
|
r771 | ** | ||
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
|
r771 | ** | ||
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
|
r2854 | ****************************************************************************/ | ||
|
r1977 | #ifndef BUILD_PRIVATE_UNIT_TESTS | ||
#include <QtTest/QtTest> | ||||
class tst_ChartDataSet: public QObject { | ||||
Q_OBJECT | ||||
private Q_SLOTS: | ||||
void skip(); | ||||
}; | ||||
void tst_ChartDataSet::skip() | ||||
{ | ||||
|
r2367 | QSKIP("This test requires the debug version of library"); | ||
|
r1977 | } | ||
QTEST_MAIN(tst_ChartDataSet) | ||||
#include "tst_chartdataset.moc" | ||||
#else | ||||
|
r771 | |||
|
r224 | #include <QtTest/QtTest> | ||
|
r2714 | #include <QtCharts/QAbstractAxis> | ||
#include <QtCharts/QValueAxis> | ||||
#include <QtCharts/QBarCategoryAxis> | ||||
#include <QtCharts/QCategoryAxis> | ||||
|
r2391 | #ifndef QT_ON_ARM | ||
|
r2714 | #include <QtCharts/QDateTimeAxis> | ||
|
r2391 | #endif | ||
|
r2714 | #include <QtCharts/QLineSeries> | ||
#include <QtCharts/QAreaSeries> | ||||
#include <QtCharts/QScatterSeries> | ||||
#include <QtCharts/QSplineSeries> | ||||
#include <QtCharts/QPieSeries> | ||||
#include <QtCharts/QBarSeries> | ||||
#include <QtCharts/QPercentBarSeries> | ||||
#include <QtCharts/QStackedBarSeries> | ||||
|
r224 | #include <private/chartdataset_p.h> | ||
|
r2275 | #include <private/abstractdomain_p.h> | ||
|
r1109 | #include <tst_definitions.h> | ||
|
r224 | |||
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
|
r224 | |||
|
r2275 | Q_DECLARE_METATYPE(AbstractDomain *) | ||
|
r1541 | Q_DECLARE_METATYPE(QAbstractAxis *) | ||
|
r988 | Q_DECLARE_METATYPE(QAbstractSeries *) | ||
|
r1564 | Q_DECLARE_METATYPE(QList<QAbstractSeries *>) | ||
Q_DECLARE_METATYPE(QList<QAbstractAxis *>) | ||||
|
r2273 | Q_DECLARE_METATYPE(Qt::Alignment) | ||
Q_DECLARE_METATYPE(QList<Qt::Alignment>) | ||||
|
r988 | Q_DECLARE_METATYPE(QLineSeries *) | ||
|
r224 | |||
class tst_ChartDataSet: public QObject { | ||||
|
r771 | Q_OBJECT | ||
|
r2284 | public: | ||
tst_ChartDataSet():m_dataset(0){}; | ||||
|
r771 | |||
public Q_SLOTS: | ||||
|
r2247 | void initTestCase(); | ||
void cleanupTestCase(); | ||||
void init(); | ||||
void cleanup(); | ||||
|
r224 | |||
|
r771 | private Q_SLOTS: | ||
|
r2247 | void chartdataset_data(); | ||
void chartdataset(); | ||||
void addSeries_data(); | ||||
void addSeries(); | ||||
void removeSeries_data(); | ||||
void removeSeries(); | ||||
|
r2273 | void addAxis_data(); | ||
void addAxis(); | ||||
void removeAxis_data(); | ||||
void removeAxis(); | ||||
void attachAxis_data(); | ||||
void attachAxis(); | ||||
void detachAxis_data(); | ||||
void detachAxis(); | ||||
|
r1593 | |||
|
r1078 | private: | ||
ChartDataSet* m_dataset; | ||||
|
r224 | }; | ||
void tst_ChartDataSet::initTestCase() | ||||
{ | ||||
|
r2275 | qRegisterMetaType<AbstractDomain*>(); | ||
|
r2247 | qRegisterMetaType<QAbstractAxis*>(); | ||
|
r989 | qRegisterMetaType<QAbstractSeries*>(); | ||
|
r224 | } | ||
void tst_ChartDataSet::cleanupTestCase() | ||||
{ | ||||
|
r2733 | QTest::qWait(1); // Allow final deleteLaters to run | ||
|
r224 | } | ||
void tst_ChartDataSet::init() | ||||
{ | ||||
|
r2733 | Q_ASSERT(!m_dataset); | ||
|
r2273 | m_dataset = new ChartDataSet(0); | ||
|
r224 | } | ||
|
r1078 | |||
|
r224 | void tst_ChartDataSet::cleanup() | ||
{ | ||||
|
r2733 | delete m_dataset; | ||
m_dataset = 0; | ||||
|
r224 | } | ||
void tst_ChartDataSet::chartdataset_data() | ||||
{ | ||||
} | ||||
void tst_ChartDataSet::chartdataset() | ||||
{ | ||||
|
r2273 | QVERIFY(m_dataset->axes().isEmpty()); | ||
QVERIFY(m_dataset->series().isEmpty()); | ||||
m_dataset->createDefaultAxes(); | ||||
|
r224 | } | ||
|
r1564 | |||
|
r224 | void tst_ChartDataSet::addSeries_data() | ||
{ | ||||
|
r2247 | QTest::addColumn<QAbstractSeries*>("series"); | ||
|
r1564 | |||
QAbstractSeries* line = new QLineSeries(this); | ||||
|
r2247 | QTest::newRow("line") << line; | ||
|
r2284 | |||
QAbstractSeries* area = new QAreaSeries(static_cast<QLineSeries*>(new QLineSeries(this))); | ||||
|
r2247 | QTest::newRow("area") << area; | ||
|
r2284 | |||
QAbstractSeries* scatter = new QScatterSeries(this); | ||||
|
r2247 | QTest::newRow("scatter") << scatter; | ||
|
r2284 | |||
QAbstractSeries* spline = new QSplineSeries(this); | ||||
|
r2247 | QTest::newRow("spline") << spline; | ||
|
r2284 | |||
QAbstractSeries* pie = new QPieSeries(this); | ||||
|
r2247 | QTest::newRow("pie") << pie; | ||
|
r2284 | |||
QAbstractSeries* bar = new QBarSeries(this); | ||||
|
r1564 | QTest::newRow("bar") << bar; | ||
|
r2284 | |||
QAbstractSeries* percent = new QPercentBarSeries(this); | ||||
|
r2247 | QTest::newRow("percent") << percent; | ||
|
r2284 | |||
QAbstractSeries* stacked = new QStackedBarSeries(this); | ||||
|
r2247 | QTest::newRow("stacked") << stacked; | ||
|
r1564 | } | ||
void tst_ChartDataSet::addSeries() | ||||
{ | ||||
QFETCH(QAbstractSeries*, series); | ||||
|
r2273 | QVERIFY(m_dataset->series().isEmpty()); | ||
|
r439 | |||
|
r2273 | QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); | ||
|
r1564 | QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); | ||
|
r2273 | QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); | ||
|
r2110 | QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); | ||
|
r439 | |||
|
r1564 | m_dataset->addSeries(series); | ||
|
r411 | |||
|
r2273 | QCOMPARE(m_dataset->series().count(),1); | ||
|
r1577 | TRY_COMPARE(spy0.count(), 0); | ||
|
r1109 | TRY_COMPARE(spy1.count(), 0); | ||
|
r2273 | TRY_COMPARE(spy2.count(), 1); | ||
|
r1564 | TRY_COMPARE(spy3.count(), 0); | ||
|
r224 | } | ||
|
r1564 | void tst_ChartDataSet::removeSeries_data() | ||
|
r224 | { | ||
|
r1564 | addSeries_data(); | ||
} | ||||
|
r974 | |||
|
r1564 | void tst_ChartDataSet::removeSeries() | ||
{ | ||||
QFETCH(QAbstractSeries*, series); | ||||
|
r2273 | QVERIFY(m_dataset->series().isEmpty()); | ||
|
r1564 | m_dataset->addSeries(series); | ||
|
r974 | |||
|
r2273 | QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); | ||
|
r1564 | QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); | ||
|
r2273 | QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); | ||
|
r2110 | QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); | ||
|
r974 | |||
|
r1564 | m_dataset->removeSeries(series); | ||
|
r974 | |||
|
r2273 | QCOMPARE(m_dataset->series().count(),0); | ||
|
r1109 | TRY_COMPARE(spy0.count(), 0); | ||
|
r2273 | TRY_COMPARE(spy1.count(), 0); | ||
|
r1109 | TRY_COMPARE(spy2.count(), 0); | ||
|
r1564 | TRY_COMPARE(spy3.count(), 1); | ||
|
r2733 | |||
delete series; | ||||
|
r224 | } | ||
|
r2273 | void tst_ChartDataSet::addAxis_data() | ||
|
r910 | { | ||
|
r2273 | QTest::addColumn<QAbstractAxis*>("axis"); | ||
QAbstractAxis* value = new QValueAxis(this); | ||||
QAbstractAxis* category = new QCategoryAxis(this); | ||||
QAbstractAxis* barcategory = new QBarCategoryAxis(this); | ||||
|
r2391 | #ifndef Q_WS_QWS | ||
|
r2273 | QAbstractAxis* datetime = new QDateTimeAxis(this); | ||
|
r2391 | #endif | ||
|
r2273 | |||
QTest::newRow("value") << value; | ||||
QTest::newRow("category") << category; | ||||
QTest::newRow("barcategory") << barcategory; | ||||
|
r2391 | #ifndef Q_WS_QWS | ||
|
r2273 | QTest::newRow("datetime") << datetime; | ||
|
r2391 | #endif | ||
|
r910 | } | ||
|
r2273 | void tst_ChartDataSet::addAxis() | ||
|
r910 | { | ||
|
r2273 | QFETCH(QAbstractAxis*, axis); | ||
QVERIFY(m_dataset->axes().isEmpty()); | ||||
|
r910 | |||
|
r2273 | QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); | ||
|
r2110 | QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); | ||
|
r2273 | QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); | ||
|
r2110 | QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); | ||
|
r910 | |||
|
r2273 | m_dataset->addAxis(axis,Qt::AlignBottom); | ||
QCOMPARE(m_dataset->axes().count(),1); | ||||
TRY_COMPARE(spy0.count(), 1); | ||||
|
r1109 | TRY_COMPARE(spy1.count(), 0); | ||
TRY_COMPARE(spy2.count(), 0); | ||||
TRY_COMPARE(spy3.count(), 0); | ||||
|
r910 | } | ||
|
r2273 | void tst_ChartDataSet::removeAxis_data() | ||
|
r771 | { | ||
|
r2273 | addAxis_data(); | ||
|
r771 | } | ||
|
r2273 | void tst_ChartDataSet::removeAxis() | ||
|
r771 | { | ||
|
r2273 | QFETCH(QAbstractAxis*, axis); | ||
QVERIFY(m_dataset->series().isEmpty()); | ||||
m_dataset->addAxis(axis,Qt::AlignBottom); | ||||
|
r910 | |||
|
r2273 | QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); | ||
|
r2110 | QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); | ||
|
r2273 | QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); | ||
|
r1078 | QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); | ||
|
r771 | |||
|
r2273 | m_dataset->removeAxis(axis); | ||
|
r771 | |||
|
r2384 | QCOMPARE(m_dataset->axes().count(),0); | ||
|
r2273 | QCOMPARE(m_dataset->series().count(),0); | ||
|
r1109 | TRY_COMPARE(spy0.count(), 0); | ||
|
r2273 | TRY_COMPARE(spy1.count(), 1); | ||
|
r1109 | TRY_COMPARE(spy2.count(), 0); | ||
TRY_COMPARE(spy3.count(), 0); | ||||
|
r2733 | |||
delete axis; | ||||
|
r2273 | } | ||
|
r910 | |||
|
r2273 | void tst_ChartDataSet::attachAxis_data() | ||
|
r771 | { | ||
|
r2284 | |||
|
r2273 | QTest::addColumn<QList<QAbstractSeries*> >("series"); | ||
QTest::addColumn<QList<QAbstractAxis*> >("axis"); | ||||
QTest::addColumn<QList<Qt::Alignment> >("alignment"); | ||||
QTest::addColumn<QAbstractSeries*>("attachSeries"); | ||||
QTest::addColumn<QAbstractAxis*>("attachAxis"); | ||||
QTest::addColumn<bool>("success"); | ||||
|
r771 | |||
|
r2284 | { | ||
QList<QAbstractSeries*> series; | ||||
QList<QAbstractAxis*> axes; | ||||
QList<Qt::Alignment> alignment; | ||||
QAbstractSeries* line = new QLineSeries(this); | ||||
QAbstractAxis* value1 = new QValueAxis(this); | ||||
QAbstractAxis* value2 = new QValueAxis(this); | ||||
series << line << 0; | ||||
axes << value1 << value2; | ||||
alignment << Qt::AlignBottom << Qt::AlignLeft; | ||||
QTest::newRow("first") << series << axes << alignment << line << value2 << true ; | ||||
} | ||||
|
r1593 | |||
|
r2284 | { | ||
QList<QAbstractSeries*> series; | ||||
QList<QAbstractAxis*> axes; | ||||
QList<Qt::Alignment> alignment; | ||||
QAbstractSeries* line = new QLineSeries(this); | ||||
QAbstractAxis* value1 = new QValueAxis(this); | ||||
QAbstractAxis* value2 = new QValueAxis(this); | ||||
series << 0 << line; | ||||
axes << value1 << value2; | ||||
alignment << Qt::AlignBottom << Qt::AlignLeft; | ||||
QTest::newRow("second") << series << axes << alignment << line << value1 << true; | ||||
} | ||||
|
r1593 | |||
|
r771 | } | ||
|
r2273 | void tst_ChartDataSet::attachAxis() | ||
|
r771 | { | ||
|
r2273 | QFETCH(QList<QAbstractSeries*>, series); | ||
QFETCH(QList<QAbstractAxis*>, axis); | ||||
QFETCH(QList<Qt::Alignment>, alignment); | ||||
QFETCH(QAbstractSeries*, attachSeries); | ||||
QFETCH(QAbstractAxis*, attachAxis); | ||||
QFETCH(bool, success); | ||||
|
r771 | |||
|
r2273 | Q_ASSERT(series.count() == axis.count()); | ||
Q_ASSERT(series.count() == alignment.count()); | ||||
|
r771 | |||
|
r2273 | QVERIFY(m_dataset->series().isEmpty()); | ||
QVERIFY(m_dataset->axes().isEmpty()); | ||||
|
r771 | |||
|
r2273 | for(int i = 0 ; i < series.count() ; i++){ | ||
if(series[i]) m_dataset->addSeries(series[i]); | ||||
if(axis[i]) m_dataset->addAxis(axis[i],alignment[i]); | ||||
if(series[i] && axis[i]) m_dataset->attachAxis(series[i],axis[i]); | ||||
|
r1593 | } | ||
|
r771 | |||
|
r2273 | QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); | ||
QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); | ||||
QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); | ||||
QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); | ||||
|
r771 | |||
|
r2273 | QCOMPARE(m_dataset->attachAxis(attachSeries,attachAxis),success); | ||
|
r1593 | |||
|
r771 | } | ||
|
r2273 | void tst_ChartDataSet::detachAxis_data() | ||
|
r771 | { | ||
|
r2273 | QTest::addColumn<QList<QAbstractSeries*> >("series"); | ||
QTest::addColumn<QList<QAbstractAxis*> >("axis"); | ||||
QTest::addColumn<QAbstractSeries*>("detachSeries"); | ||||
QTest::addColumn<QAbstractAxis*>("detachAxis"); | ||||
QTest::addColumn<bool>("success"); | ||||
|
r771 | |||
|
r2284 | { | ||
|
r2273 | QList<QAbstractSeries*> series; | ||
QList<QAbstractAxis*> axes; | ||||
QAbstractSeries* line = new QLineSeries(this); | ||||
QAbstractAxis* value = new QValueAxis(this); | ||||
series << line; | ||||
axes << value; | ||||
|
r2284 | QTest::newRow("first") << series << axes << line << value << true; | ||
} | ||||
|
r2273 | } | ||
|
r771 | |||
|
r2273 | void tst_ChartDataSet::detachAxis() | ||
{ | ||||
QFETCH(QList<QAbstractSeries*>, series); | ||||
QFETCH(QList<QAbstractAxis*>, axis); | ||||
QFETCH(QAbstractSeries*, detachSeries); | ||||
QFETCH(QAbstractAxis*, detachAxis); | ||||
QFETCH(bool, success); | ||||
|
r771 | |||
|
r2273 | Q_ASSERT(series.count() == axis.count()); | ||
|
r771 | |||
|
r2273 | QVERIFY(m_dataset->series().isEmpty()); | ||
QVERIFY(m_dataset->axes().isEmpty()); | ||||
|
r771 | |||
|
r2273 | for(int i = 0; i < series.count(); i++) { | ||
if(series[i]) m_dataset->addSeries(series[i]); | ||||
if(axis[i]) m_dataset->addAxis(axis[i],Qt::AlignBottom); | ||||
if(series[i] && axis[i]) m_dataset->attachAxis(series[i],axis[i]); | ||||
|
r1593 | } | ||
|
r771 | |||
|
r2273 | QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); | ||
QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); | ||||
QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); | ||||
QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); | ||||
QCOMPARE(m_dataset->detachAxis(detachSeries,detachAxis),success); | ||||
|
r771 | } | ||
|
r1593 | |||
|
r224 | QTEST_MAIN(tst_ChartDataSet) | ||
#include "tst_chartdataset.moc" | ||||
|
r1977 | #endif | ||