From 6d51f9f77c12bfa0be8fa52283c6afd32c3a049f 2012-06-01 14:11:00 From: Marek Rosa Date: 2012-06-01 14:11:00 Subject: [PATCH] Added tests for XYModelMapper --- diff --git a/src/xychart/qxymodelmapper.cpp b/src/xychart/qxymodelmapper.cpp index 3913981..d995706 100644 --- a/src/xychart/qxymodelmapper.cpp +++ b/src/xychart/qxymodelmapper.cpp @@ -166,7 +166,7 @@ int QXYModelMapper::xSection() const void QXYModelMapper::setXSection(int xSection) { Q_D(QXYModelMapper); - d->m_xSection = xSection; + d->m_xSection = qMax(-1, xSection); d->initializeXYFromModel(); } @@ -186,7 +186,7 @@ int QXYModelMapper::ySection() const void QXYModelMapper::setYSection(int ySection) { Q_D(QXYModelMapper); - d->m_ySection = ySection; + d->m_ySection = qMax(-1, ySection); d->initializeXYFromModel(); } diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 5c21980..571d88c 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 +SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper test_private:{ SUBDIRS += chartdataset domain diff --git a/tests/auto/qpiemodelmapper/tst_qpiemodelmapper.cpp b/tests/auto/qpiemodelmapper/tst_qpiemodelmapper.cpp index 57ed461..78fcef1 100644 --- a/tests/auto/qpiemodelmapper/tst_qpiemodelmapper.cpp +++ b/tests/auto/qpiemodelmapper/tst_qpiemodelmapper.cpp @@ -10,12 +10,12 @@ QTCOMMERCIALCHART_USE_NAMESPACE -class tst_piemodelmapper : public QObject +class tst_qpiemodelmapper : public QObject { Q_OBJECT public: - tst_piemodelmapper(); + tst_qpiemodelmapper(); private Q_SLOTS: void initTestCase(); @@ -42,27 +42,27 @@ class tst_piemodelmapper : public QObject QChart *m_chart; }; -tst_piemodelmapper::tst_piemodelmapper(): +tst_qpiemodelmapper::tst_qpiemodelmapper(): m_model(0), m_modelRowCount(10), m_modelColumnCount(8) { } -void tst_piemodelmapper::init() +void tst_qpiemodelmapper::init() { m_series = new QPieSeries; m_chart->addSeries(m_series); } -void tst_piemodelmapper::cleanup() +void tst_qpiemodelmapper::cleanup() { m_chart->removeSeries(m_series); delete m_series; m_series = 0; } -void tst_piemodelmapper::initTestCase() +void tst_qpiemodelmapper::initTestCase() { m_chart = new QChart; QChartView *chartView = new QChartView(m_chart); @@ -77,12 +77,12 @@ void tst_piemodelmapper::initTestCase() } } -void tst_piemodelmapper::cleanupTestCase() +void tst_qpiemodelmapper::cleanupTestCase() { m_model->clear(); } -void tst_piemodelmapper::verticalMapper_data() +void tst_qpiemodelmapper::verticalMapper_data() { QTest::addColumn("valuesColumn"); QTest::addColumn("labelsColumn"); @@ -91,10 +91,10 @@ void tst_piemodelmapper::verticalMapper_data() QTest::newRow("same values and labels columns") << 1 << 1 << m_modelRowCount; QTest::newRow("invalid values column and correct labels column") << -3 << 1 << 0; QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << 1 << 0; - QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << -1 << 0; + QTest::newRow("values column beyond the size of model and invalid labels column") << m_modelColumnCount << -1 << 0; } -void tst_piemodelmapper::verticalMapper() +void tst_qpiemodelmapper::verticalMapper() { QFETCH(int, valuesColumn); QFETCH(int, labelsColumn); @@ -114,7 +114,7 @@ void tst_piemodelmapper::verticalMapper() mapper = 0; } -void tst_piemodelmapper::verticalMapperCustomMapping_data() +void tst_qpiemodelmapper::verticalMapperCustomMapping_data() { QTest::addColumn("first"); QTest::addColumn("countLimit"); @@ -132,7 +132,7 @@ void tst_piemodelmapper::verticalMapperCustomMapping_data() } -void tst_piemodelmapper::verticalMapperCustomMapping() +void tst_qpiemodelmapper::verticalMapperCustomMapping() { QFETCH(int, first); QFETCH(int, countLimit); @@ -160,7 +160,7 @@ void tst_piemodelmapper::verticalMapperCustomMapping() mapper = 0; } -void tst_piemodelmapper::horizontalMapper_data() +void tst_qpiemodelmapper::horizontalMapper_data() { QTest::addColumn("valuesRow"); QTest::addColumn("labelsRow"); @@ -172,7 +172,7 @@ void tst_piemodelmapper::horizontalMapper_data() QTest::newRow("values row beyond the size of model and invalid labels row") << m_modelRowCount << -1 << 0; } -void tst_piemodelmapper::horizontalMapper() +void tst_qpiemodelmapper::horizontalMapper() { QFETCH(int, valuesRow); QFETCH(int, labelsRow); @@ -192,7 +192,7 @@ void tst_piemodelmapper::horizontalMapper() mapper = 0; } -void tst_piemodelmapper::horizontalMapperCustomMapping_data() +void tst_qpiemodelmapper::horizontalMapperCustomMapping_data() { QTest::addColumn("first"); QTest::addColumn("countLimit"); @@ -209,7 +209,7 @@ void tst_piemodelmapper::horizontalMapperCustomMapping_data() QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount; } -void tst_piemodelmapper::horizontalMapperCustomMapping() +void tst_qpiemodelmapper::horizontalMapperCustomMapping() { QFETCH(int, first); QFETCH(int, countLimit); @@ -237,7 +237,7 @@ void tst_piemodelmapper::horizontalMapperCustomMapping() mapper = 0; } -void tst_piemodelmapper::seriesUpdated() +void tst_qpiemodelmapper::seriesUpdated() { QStandardItemModel *otherModel = new QStandardItemModel; for (int row = 0; row < m_modelRowCount; ++row) { @@ -268,6 +268,6 @@ void tst_piemodelmapper::seriesUpdated() otherModel = 0; } -QTEST_MAIN(tst_piemodelmapper) +QTEST_MAIN(tst_qpiemodelmapper) #include "tst_qpiemodelmapper.moc" diff --git a/tests/auto/qxymodelmapper/qxymodelmapper.pro b/tests/auto/qxymodelmapper/qxymodelmapper.pro new file mode 100644 index 0000000..24c7c25 --- /dev/null +++ b/tests/auto/qxymodelmapper/qxymodelmapper.pro @@ -0,0 +1,8 @@ +!include( ../auto.pri ) { + error( "Couldn't find the auto.pri file!" ) +} + +SOURCES += \ + tst_qxymodelmapper.cpp + +!system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_AUTOTESTS_BIN_DIR" diff --git a/tests/auto/qxymodelmapper/tst_qxymodelmapper.cpp b/tests/auto/qxymodelmapper/tst_qxymodelmapper.cpp new file mode 100644 index 0000000..cc65048 --- /dev/null +++ b/tests/auto/qxymodelmapper/tst_qxymodelmapper.cpp @@ -0,0 +1,273 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class tst_qxymodelmapper : public QObject +{ + Q_OBJECT + +public: + tst_qxymodelmapper(); + +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(); + void seriesUpdated(); + +private: + QStandardItemModel *m_model; + int m_modelRowCount; + int m_modelColumnCount; + + QXYSeries *m_series; + QChart *m_chart; +}; + +tst_qxymodelmapper::tst_qxymodelmapper(): + m_model(0), + m_modelRowCount(10), + m_modelColumnCount(8) +{ +} + +void tst_qxymodelmapper::init() +{ + m_series = new QLineSeries; + m_chart->addSeries(m_series); +} + +void tst_qxymodelmapper::cleanup() +{ + m_chart->removeSeries(m_series); + delete m_series; + m_series = 0; +} + +void tst_qxymodelmapper::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_qxymodelmapper::cleanupTestCase() +{ + m_model->clear(); +} + +void tst_qxymodelmapper::verticalMapper_data() +{ + QTest::addColumn("xColumn"); + QTest::addColumn("yColumn"); + QTest::addColumn("expectedCount"); + QTest::newRow("different x and y columns") << 0 << 1 << m_modelRowCount; + QTest::newRow("same x and y columns") << 1 << 1 << m_modelRowCount; + QTest::newRow("invalid x column and correct y column") << -3 << 1 << 0; + QTest::newRow("x column beyond the size of model and correct y column") << m_modelColumnCount << 1 << 0; + QTest::newRow("x column beyond the size of model and invalid y column") << m_modelColumnCount << -1 << 0; +} + +void tst_qxymodelmapper::verticalMapper() +{ + QFETCH(int, xColumn); + QFETCH(int, yColumn); + QFETCH(int, expectedCount); + + QVXYModelMapper *mapper = new QVXYModelMapper; + mapper->setXColumn(xColumn); + mapper->setYColumn(yColumn); + mapper->setModel(m_model); + mapper->setSeries(m_series); + + QCOMPARE(m_series->count(), expectedCount); + QCOMPARE(mapper->xColumn(), qMax(-1, xColumn)); + QCOMPARE(mapper->yColumn(), qMax(-1, yColumn)); + + delete mapper; + mapper = 0; +} + +void tst_qxymodelmapper::verticalMapperCustomMapping_data() +{ + QTest::addColumn("first"); + QTest::addColumn("countLimit"); + QTest::addColumn("expectedCount"); + QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelRowCount; + QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelRowCount - 3; + QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelRowCount); + QTest::newRow("first: 3, count: 5") << 3 << 5 << 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; + QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 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 << m_modelRowCount; + QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelRowCount; + QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelRowCount; + QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelRowCount; + +} + +void tst_qxymodelmapper::verticalMapperCustomMapping() +{ + QFETCH(int, first); + QFETCH(int, countLimit); + QFETCH(int, expectedCount); + + QCOMPARE(m_series->count(), 0); + + QVXYModelMapper *mapper = new QVXYModelMapper; + mapper->setXColumn(0); + mapper->setYColumn(1); + mapper->setModel(m_model); + mapper->setSeries(m_series); + mapper->setFirst(first); + mapper->setCount(countLimit); + + QCOMPARE(m_series->count(), expectedCount); + + // change values column mapping to invalid + mapper->setXColumn(-1); + mapper->setYColumn(1); + + QCOMPARE(m_series->count(), 0); + + delete mapper; + mapper = 0; +} + +void tst_qxymodelmapper::horizontalMapper_data() +{ + QTest::addColumn("xRow"); + QTest::addColumn("yRow"); + QTest::addColumn("expectedCount"); + QTest::newRow("different x and y rows") << 0 << 1 << m_modelColumnCount; + QTest::newRow("same x and y rows") << 1 << 1 << m_modelColumnCount; + QTest::newRow("invalid x row and correct y row") << -3 << 1 << 0; + QTest::newRow("x row beyond the size of model and correct y row") << m_modelRowCount << 1 << 0; + QTest::newRow("x row beyond the size of model and invalid y row") << m_modelRowCount << -1 << 0; +} + +void tst_qxymodelmapper::horizontalMapper() +{ + QFETCH(int, xRow); + QFETCH(int, yRow); + QFETCH(int, expectedCount); + + QHXYModelMapper *mapper = new QHXYModelMapper; + mapper->setXRow(xRow); + mapper->setYRow(yRow); + mapper->setModel(m_model); + mapper->setSeries(m_series); + + QCOMPARE(m_series->count(), expectedCount); + QCOMPARE(mapper->xRow(), qMax(-1, xRow)); + QCOMPARE(mapper->yRow(), qMax(-1, yRow)); + + delete mapper; + mapper = 0; +} + +void tst_qxymodelmapper::horizontalMapperCustomMapping_data() +{ + QTest::addColumn("first"); + QTest::addColumn("countLimit"); + QTest::addColumn("expectedCount"); + QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelColumnCount; + QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelColumnCount - 3; + QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelColumnCount); + QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelColumnCount - 3); + QTest::newRow("first: +1 greater then the number of columns in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0; + QTest::newRow("first: +1 greater then the number of columns in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0; + QTest::newRow("first: 0, count: +3 greater than the number of columns in the model (should limit to the size of model)") << 0 << m_modelColumnCount + 3 << m_modelColumnCount; + QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelColumnCount; + QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelColumnCount; + QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount; +} + +void tst_qxymodelmapper::horizontalMapperCustomMapping() +{ + QFETCH(int, first); + QFETCH(int, countLimit); + QFETCH(int, expectedCount); + + QCOMPARE(m_series->count(), 0); + + QHXYModelMapper *mapper = new QHXYModelMapper; + mapper->setXRow(0); + mapper->setYRow(1); + mapper->setModel(m_model); + mapper->setSeries(m_series); + mapper->setFirst(first); + mapper->setCount(countLimit); + + QCOMPARE(m_series->count(), expectedCount); + + // change values row mapping to invalid + mapper->setXRow(-1); + mapper->setYRow(1); + + QCOMPARE(m_series->count(), 0); + + delete mapper; + mapper = 0; +} + +void tst_qxymodelmapper::seriesUpdated() +{ + QStandardItemModel *otherModel = new QStandardItemModel; + for (int row = 0; row < m_modelRowCount; ++row) { + for (int column = 0; column < m_modelColumnCount; column++) { + QStandardItem *item = new QStandardItem(row * column); + otherModel->setItem(row, column, item); + } + } + + QVXYModelMapper *mapper = new QVXYModelMapper; + mapper->setXColumn(0); + mapper->setYColumn(1); + mapper->setModel(otherModel); + mapper->setSeries(m_series); + QCOMPARE(m_series->count(), m_modelRowCount); + QCOMPARE(mapper->count(), -1); + + m_series->append(QPointF(100, 100)); + QCOMPARE(m_series->count(), m_modelRowCount + 1); + QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model + + m_series->remove(m_series->points().last()); + QCOMPARE(m_series->count(), m_modelRowCount); + QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model + + otherModel->clear(); + delete otherModel; + otherModel = 0; +} + +QTEST_MAIN(tst_qxymodelmapper) + +#include "tst_qxymodelmapper.moc"