From 7e4ecbbc688552bf249c1b8e94560428248b79d4 2012-06-02 12:24:20 From: Marek Rosa Date: 2012-06-02 12:24:20 Subject: [PATCH] Added some tests for BarModelMapper --- diff --git a/src/barchart/qbarmodelmapper.cpp b/src/barchart/qbarmodelmapper.cpp index 7ac77e8..52766eb 100644 --- a/src/barchart/qbarmodelmapper.cpp +++ b/src/barchart/qbarmodelmapper.cpp @@ -191,7 +191,7 @@ int QBarModelMapper::firstBarSetSection() const void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection) { Q_D(QBarModelMapper); - d->m_firstBarSetSection = firstBarSetSection; + d->m_firstBarSetSection = qMax(-1, firstBarSetSection); d->initializeBarFromModel(); } @@ -211,7 +211,7 @@ int QBarModelMapper::lastBarSetSection() const void QBarModelMapper::setLastBarSetSection(int lastBarSetSection) { Q_D(QBarModelMapper); - d->m_lastBarSetSection = lastBarSetSection; + d->m_lastBarSetSection = qMax(-1, lastBarSetSection); d->initializeBarFromModel(); } diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index be1b07a..26c14b2 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -247,11 +247,11 @@ bool QBarSeries::insert(int index, QBarSet *set) void QBarSeries::clear() { Q_D(QBarSeries); - bool success = d->remove(barSets()); + QList sets = barSets(); + bool success = d->remove(sets); if (success) { emit barsetsRemoved(sets); } - return success; } /*! diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 571d88c..54222d9 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -3,7 +3,7 @@ } TEMPLATE = subdirs -SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper +SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper qbarmodelmapper test_private:{ SUBDIRS += chartdataset domain diff --git a/tests/auto/qbarmodelmapper/qbarmodelmapper.pro b/tests/auto/qbarmodelmapper/qbarmodelmapper.pro new file mode 100644 index 0000000..6041c8f --- /dev/null +++ b/tests/auto/qbarmodelmapper/qbarmodelmapper.pro @@ -0,0 +1,8 @@ +!include( ../auto.pri ) { + error( "Couldn't find the auto.pri file!" ) +} + +SOURCES += \ + tst_qbarmodelmapper.cpp + +!system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_AUTOTESTS_BIN_DIR" diff --git a/tests/auto/qbarmodelmapper/tst_qbarmodelmapper.cpp b/tests/auto/qbarmodelmapper/tst_qbarmodelmapper.cpp new file mode 100644 index 0000000..b66e889 --- /dev/null +++ b/tests/auto/qbarmodelmapper/tst_qbarmodelmapper.cpp @@ -0,0 +1,266 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class tst_qbarmodelmapper : public QObject +{ + Q_OBJECT + + public: + tst_qbarmodelmapper(); + + private Q_SLOTS: + void initTestCase(); + void cleanupTestCase(); + void init(); + void cleanup(); + void verticalMapper_data(); + void verticalMapper(); + void verticalMapperCustomMapping_data(); + void verticalMapperCustomMapping(); + void horizontalMapper_data(); + void horizontalMapper(); + void horizontalMapperCustomMapping_data(); + void horizontalMapperCustomMapping(); + + private: + QStandardItemModel *m_model; + int m_modelRowCount; + int m_modelColumnCount; + + QGroupedBarSeries *m_series; + QChart *m_chart; +}; + +tst_qbarmodelmapper::tst_qbarmodelmapper(): + m_model(0), + m_modelRowCount(10), + m_modelColumnCount(8) +{ +} + +void tst_qbarmodelmapper::init() +{ + // m_series = new QGroupedBarSeries; + // m_chart->addSeries(m_series); +} + +void tst_qbarmodelmapper::cleanup() +{ + m_chart->removeSeries(m_series); + delete m_series; + m_series = 0; +} + +void tst_qbarmodelmapper::initTestCase() +{ + m_chart = new QChart; + QChartView *chartView = new QChartView(m_chart); + chartView->show(); + + m_model = new QStandardItemModel(this); + for (int row = 0; row < m_modelRowCount; ++row) { + for (int column = 0; column < m_modelColumnCount; column++) { + QStandardItem *item = new QStandardItem(row * column); + m_model->setItem(row, column, item); + } + } +} + +void tst_qbarmodelmapper::cleanupTestCase() +{ + m_model->clear(); +} + +void tst_qbarmodelmapper::verticalMapper_data() +{ + QTest::addColumn("firstBarSetColumn"); + QTest::addColumn("lastBarSetColumn"); + QTest::addColumn("expectedBarSetCount"); + QTest::newRow("lastBarSetColumn greater than firstBarSetColumn") << 0 << 1 << 2; + QTest::newRow("lastBarSetColumn equal to firstBarSetColumn") << 1 << 1 << 1; + QTest::newRow("lastBarSetColumn lesser than firstBarSetColumn") << 1 << 0 << 0; + QTest::newRow("invalid firstBarSetColumn and correct lastBarSetColumn") << -3 << 1 << 0; + QTest::newRow("firstBarSetColumn beyond the size of model and correct lastBarSetColumn") << m_modelColumnCount << 1 << 0; + QTest::newRow("firstBarSetColumn beyond the size of model and invalid lastBarSetColumn") << m_modelColumnCount << -1 << 0; +} + +void tst_qbarmodelmapper::verticalMapper() +{ + QFETCH(int, firstBarSetColumn); + QFETCH(int, lastBarSetColumn); + QFETCH(int, expectedBarSetCount); + + m_series = new QGroupedBarSeries; + + QVBarModelMapper *mapper = new QVBarModelMapper; + mapper->setFirstBarSetColumn(firstBarSetColumn); + mapper->setLastBarSetColumn(lastBarSetColumn); + mapper->setModel(m_model); + mapper->setSeries(m_series); + + m_chart->addSeries(m_series); + + QCOMPARE(m_series->barsetCount(), expectedBarSetCount); + QCOMPARE(mapper->firstBarSetColumn(), qMax(-1, firstBarSetColumn)); + QCOMPARE(mapper->lastBarSetColumn(), qMax(-1, lastBarSetColumn)); + + delete mapper; + mapper = 0; +} + +void tst_qbarmodelmapper::verticalMapperCustomMapping_data() +{ + QTest::addColumn("first"); + QTest::addColumn("countLimit"); + QTest::addColumn("expectedBarSetCount"); + QTest::addColumn("expectedCount"); + QTest::newRow("first: 0, unlimited count") << 0 << -1 << 2 << m_modelRowCount; + QTest::newRow("first: 3, unlimited count") << 3 << -1 << 2 << m_modelRowCount - 3; + QTest::newRow("first: 0, count: 5") << 0 << 5 << 2 << qMin(5, m_modelRowCount); + QTest::newRow("first: 3, count: 5") << 3 << 5 << 2 << qMin(5, m_modelRowCount - 3); + QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0 << 0; + QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0 << 0; + QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelRowCount + 3 << 2 << m_modelRowCount; + QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << 2 << m_modelRowCount; + QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << 2 << m_modelRowCount; + QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << 2 << m_modelRowCount; +} + +void tst_qbarmodelmapper::verticalMapperCustomMapping() +{ + QFETCH(int, first); + QFETCH(int, countLimit); + QFETCH(int, expectedBarSetCount); + QFETCH(int, expectedCount); + + m_series = new QGroupedBarSeries; + + QCOMPARE(m_series->barsetCount(), 0); + + QVBarModelMapper *mapper = new QVBarModelMapper; + mapper->setFirstBarSetColumn(0); + mapper->setLastBarSetColumn(1); + mapper->setModel(m_model); + mapper->setSeries(m_series); + mapper->setFirst(first); + mapper->setCount(countLimit); + m_chart->addSeries(m_series); + + QCOMPARE(m_series->barsetCount(), expectedBarSetCount); + + if (expectedBarSetCount > 0) + QCOMPARE(m_series->barSets().first()->count(), expectedCount); + + // change values column mapping to invalid + mapper->setFirstBarSetColumn(-1); + mapper->setLastBarSetColumn(1); + + QCOMPARE(m_series->barsetCount(), 0); + + delete mapper; + mapper = 0; +} + +void tst_qbarmodelmapper::horizontalMapper_data() +{ + QTest::addColumn("firstBarSetRow"); + QTest::addColumn("lastBarSetRow"); + QTest::addColumn("expectedBarSetCount"); + QTest::newRow("lastBarSetRow greater than firstBarSetRow") << 0 << 1 << 2; + QTest::newRow("lastBarSetRow equal to firstBarSetRow") << 1 << 1 << 1; + QTest::newRow("lastBarSetRow lesser than firstBarSetRow") << 1 << 0 << 0; + QTest::newRow("invalid firstBarSetRow and correct lastBarSetRow") << -3 << 1 << 0; + QTest::newRow("firstBarSetRow beyond the size of model and correct lastBarSetRow") << m_modelRowCount << 1 << 0; + QTest::newRow("firstBarSetRow beyond the size of model and invalid lastBarSetRow") << m_modelRowCount << -1 << 0; +} + +void tst_qbarmodelmapper::horizontalMapper() +{ + QFETCH(int, firstBarSetRow); + QFETCH(int, lastBarSetRow); + QFETCH(int, expectedBarSetCount); + + m_series = new QGroupedBarSeries; + + QHBarModelMapper *mapper = new QHBarModelMapper; + mapper->setFirstBarSetRow(firstBarSetRow); + mapper->setLastBarSetRow(lastBarSetRow); + mapper->setModel(m_model); + mapper->setSeries(m_series); + + m_chart->addSeries(m_series); + + QCOMPARE(m_series->barsetCount(), expectedBarSetCount); + QCOMPARE(mapper->firstBarSetRow(), qMax(-1, firstBarSetRow)); + QCOMPARE(mapper->lastBarSetRow(), qMax(-1, lastBarSetRow)); + + delete mapper; + mapper = 0; +} + +void tst_qbarmodelmapper::horizontalMapperCustomMapping_data() +{ + QTest::addColumn("first"); + QTest::addColumn("countLimit"); + QTest::addColumn("expectedBarSetCount"); + QTest::addColumn("expectedCount"); + QTest::newRow("first: 0, unlimited count") << 0 << -1 << 2 << m_modelColumnCount; + QTest::newRow("first: 3, unlimited count") << 3 << -1 << 2 << m_modelColumnCount - 3; + QTest::newRow("first: 0, count: 5") << 0 << 5 << 2 << qMin(5, m_modelColumnCount); + QTest::newRow("first: 3, count: 5") << 3 << 5 << 2 << qMin(5, m_modelColumnCount - 3); + QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0 << 0; + QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0 << 0; + QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelColumnCount + 3 << 2 << m_modelColumnCount; + QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << 2 << m_modelColumnCount; + QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << 2 << m_modelColumnCount; + QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << 2 << m_modelColumnCount; +} + +void tst_qbarmodelmapper::horizontalMapperCustomMapping() +{ + QFETCH(int, first); + QFETCH(int, countLimit); + QFETCH(int, expectedBarSetCount); + QFETCH(int, expectedCount); + + m_series = new QGroupedBarSeries; + + QCOMPARE(m_series->barsetCount(), 0); + + QHBarModelMapper *mapper = new QHBarModelMapper; + mapper->setFirstBarSetRow(0); + mapper->setLastBarSetRow(1); + mapper->setModel(m_model); + mapper->setSeries(m_series); + mapper->setFirst(first); + mapper->setCount(countLimit); + m_chart->addSeries(m_series); + + QCOMPARE(m_series->barsetCount(), expectedBarSetCount); + + if (expectedBarSetCount > 0) + QCOMPARE(m_series->barSets().first()->count(), expectedCount); + + // change values column mapping to invalid + mapper->setFirstBarSetRow(-1); + mapper->setLastBarSetRow(1); + + QCOMPARE(m_series->barsetCount(), 0); + + delete mapper; + mapper = 0; +} + +QTEST_MAIN(tst_qbarmodelmapper) + +#include "tst_qbarmodelmapper.moc"