##// END OF EJS Templates
Added some tests for BarModelMapper
Marek Rosa -
r1364:7e4ecbbc6885
parent child
Show More
@@ -0,0 +1,8
1 !include( ../auto.pri ) {
2 error( "Couldn't find the auto.pri file!" )
3 }
4
5 SOURCES += \
6 tst_qbarmodelmapper.cpp
7
8 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_AUTOTESTS_BIN_DIR"
@@ -0,0 +1,266
1 #include <QtCore/QString>
2 #include <QtTest/QtTest>
3
4 #include <qchart.h>
5 #include <qchartview.h>
6 #include <qgroupedbarseries.h>
7 #include <qbarset.h>
8 #include <qvbarmodelmapper.h>
9 #include <qhbarmodelmapper.h>
10 #include <QStandardItemModel>
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
14 class tst_qbarmodelmapper : public QObject
15 {
16 Q_OBJECT
17
18 public:
19 tst_qbarmodelmapper();
20
21 private Q_SLOTS:
22 void initTestCase();
23 void cleanupTestCase();
24 void init();
25 void cleanup();
26 void verticalMapper_data();
27 void verticalMapper();
28 void verticalMapperCustomMapping_data();
29 void verticalMapperCustomMapping();
30 void horizontalMapper_data();
31 void horizontalMapper();
32 void horizontalMapperCustomMapping_data();
33 void horizontalMapperCustomMapping();
34
35 private:
36 QStandardItemModel *m_model;
37 int m_modelRowCount;
38 int m_modelColumnCount;
39
40 QGroupedBarSeries *m_series;
41 QChart *m_chart;
42 };
43
44 tst_qbarmodelmapper::tst_qbarmodelmapper():
45 m_model(0),
46 m_modelRowCount(10),
47 m_modelColumnCount(8)
48 {
49 }
50
51 void tst_qbarmodelmapper::init()
52 {
53 // m_series = new QGroupedBarSeries;
54 // m_chart->addSeries(m_series);
55 }
56
57 void tst_qbarmodelmapper::cleanup()
58 {
59 m_chart->removeSeries(m_series);
60 delete m_series;
61 m_series = 0;
62 }
63
64 void tst_qbarmodelmapper::initTestCase()
65 {
66 m_chart = new QChart;
67 QChartView *chartView = new QChartView(m_chart);
68 chartView->show();
69
70 m_model = new QStandardItemModel(this);
71 for (int row = 0; row < m_modelRowCount; ++row) {
72 for (int column = 0; column < m_modelColumnCount; column++) {
73 QStandardItem *item = new QStandardItem(row * column);
74 m_model->setItem(row, column, item);
75 }
76 }
77 }
78
79 void tst_qbarmodelmapper::cleanupTestCase()
80 {
81 m_model->clear();
82 }
83
84 void tst_qbarmodelmapper::verticalMapper_data()
85 {
86 QTest::addColumn<int>("firstBarSetColumn");
87 QTest::addColumn<int>("lastBarSetColumn");
88 QTest::addColumn<int>("expectedBarSetCount");
89 QTest::newRow("lastBarSetColumn greater than firstBarSetColumn") << 0 << 1 << 2;
90 QTest::newRow("lastBarSetColumn equal to firstBarSetColumn") << 1 << 1 << 1;
91 QTest::newRow("lastBarSetColumn lesser than firstBarSetColumn") << 1 << 0 << 0;
92 QTest::newRow("invalid firstBarSetColumn and correct lastBarSetColumn") << -3 << 1 << 0;
93 QTest::newRow("firstBarSetColumn beyond the size of model and correct lastBarSetColumn") << m_modelColumnCount << 1 << 0;
94 QTest::newRow("firstBarSetColumn beyond the size of model and invalid lastBarSetColumn") << m_modelColumnCount << -1 << 0;
95 }
96
97 void tst_qbarmodelmapper::verticalMapper()
98 {
99 QFETCH(int, firstBarSetColumn);
100 QFETCH(int, lastBarSetColumn);
101 QFETCH(int, expectedBarSetCount);
102
103 m_series = new QGroupedBarSeries;
104
105 QVBarModelMapper *mapper = new QVBarModelMapper;
106 mapper->setFirstBarSetColumn(firstBarSetColumn);
107 mapper->setLastBarSetColumn(lastBarSetColumn);
108 mapper->setModel(m_model);
109 mapper->setSeries(m_series);
110
111 m_chart->addSeries(m_series);
112
113 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
114 QCOMPARE(mapper->firstBarSetColumn(), qMax(-1, firstBarSetColumn));
115 QCOMPARE(mapper->lastBarSetColumn(), qMax(-1, lastBarSetColumn));
116
117 delete mapper;
118 mapper = 0;
119 }
120
121 void tst_qbarmodelmapper::verticalMapperCustomMapping_data()
122 {
123 QTest::addColumn<int>("first");
124 QTest::addColumn<int>("countLimit");
125 QTest::addColumn<int>("expectedBarSetCount");
126 QTest::addColumn<int>("expectedCount");
127 QTest::newRow("first: 0, unlimited count") << 0 << -1 << 2 << m_modelRowCount;
128 QTest::newRow("first: 3, unlimited count") << 3 << -1 << 2 << m_modelRowCount - 3;
129 QTest::newRow("first: 0, count: 5") << 0 << 5 << 2 << qMin(5, m_modelRowCount);
130 QTest::newRow("first: 3, count: 5") << 3 << 5 << 2 << qMin(5, m_modelRowCount - 3);
131 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0 << 0;
132 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0 << 0;
133 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;
134 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << 2 << m_modelRowCount;
135 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << 2 << m_modelRowCount;
136 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << 2 << m_modelRowCount;
137 }
138
139 void tst_qbarmodelmapper::verticalMapperCustomMapping()
140 {
141 QFETCH(int, first);
142 QFETCH(int, countLimit);
143 QFETCH(int, expectedBarSetCount);
144 QFETCH(int, expectedCount);
145
146 m_series = new QGroupedBarSeries;
147
148 QCOMPARE(m_series->barsetCount(), 0);
149
150 QVBarModelMapper *mapper = new QVBarModelMapper;
151 mapper->setFirstBarSetColumn(0);
152 mapper->setLastBarSetColumn(1);
153 mapper->setModel(m_model);
154 mapper->setSeries(m_series);
155 mapper->setFirst(first);
156 mapper->setCount(countLimit);
157 m_chart->addSeries(m_series);
158
159 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
160
161 if (expectedBarSetCount > 0)
162 QCOMPARE(m_series->barSets().first()->count(), expectedCount);
163
164 // change values column mapping to invalid
165 mapper->setFirstBarSetColumn(-1);
166 mapper->setLastBarSetColumn(1);
167
168 QCOMPARE(m_series->barsetCount(), 0);
169
170 delete mapper;
171 mapper = 0;
172 }
173
174 void tst_qbarmodelmapper::horizontalMapper_data()
175 {
176 QTest::addColumn<int>("firstBarSetRow");
177 QTest::addColumn<int>("lastBarSetRow");
178 QTest::addColumn<int>("expectedBarSetCount");
179 QTest::newRow("lastBarSetRow greater than firstBarSetRow") << 0 << 1 << 2;
180 QTest::newRow("lastBarSetRow equal to firstBarSetRow") << 1 << 1 << 1;
181 QTest::newRow("lastBarSetRow lesser than firstBarSetRow") << 1 << 0 << 0;
182 QTest::newRow("invalid firstBarSetRow and correct lastBarSetRow") << -3 << 1 << 0;
183 QTest::newRow("firstBarSetRow beyond the size of model and correct lastBarSetRow") << m_modelRowCount << 1 << 0;
184 QTest::newRow("firstBarSetRow beyond the size of model and invalid lastBarSetRow") << m_modelRowCount << -1 << 0;
185 }
186
187 void tst_qbarmodelmapper::horizontalMapper()
188 {
189 QFETCH(int, firstBarSetRow);
190 QFETCH(int, lastBarSetRow);
191 QFETCH(int, expectedBarSetCount);
192
193 m_series = new QGroupedBarSeries;
194
195 QHBarModelMapper *mapper = new QHBarModelMapper;
196 mapper->setFirstBarSetRow(firstBarSetRow);
197 mapper->setLastBarSetRow(lastBarSetRow);
198 mapper->setModel(m_model);
199 mapper->setSeries(m_series);
200
201 m_chart->addSeries(m_series);
202
203 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
204 QCOMPARE(mapper->firstBarSetRow(), qMax(-1, firstBarSetRow));
205 QCOMPARE(mapper->lastBarSetRow(), qMax(-1, lastBarSetRow));
206
207 delete mapper;
208 mapper = 0;
209 }
210
211 void tst_qbarmodelmapper::horizontalMapperCustomMapping_data()
212 {
213 QTest::addColumn<int>("first");
214 QTest::addColumn<int>("countLimit");
215 QTest::addColumn<int>("expectedBarSetCount");
216 QTest::addColumn<int>("expectedCount");
217 QTest::newRow("first: 0, unlimited count") << 0 << -1 << 2 << m_modelColumnCount;
218 QTest::newRow("first: 3, unlimited count") << 3 << -1 << 2 << m_modelColumnCount - 3;
219 QTest::newRow("first: 0, count: 5") << 0 << 5 << 2 << qMin(5, m_modelColumnCount);
220 QTest::newRow("first: 3, count: 5") << 3 << 5 << 2 << qMin(5, m_modelColumnCount - 3);
221 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0 << 0;
222 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0 << 0;
223 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;
224 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << 2 << m_modelColumnCount;
225 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << 2 << m_modelColumnCount;
226 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << 2 << m_modelColumnCount;
227 }
228
229 void tst_qbarmodelmapper::horizontalMapperCustomMapping()
230 {
231 QFETCH(int, first);
232 QFETCH(int, countLimit);
233 QFETCH(int, expectedBarSetCount);
234 QFETCH(int, expectedCount);
235
236 m_series = new QGroupedBarSeries;
237
238 QCOMPARE(m_series->barsetCount(), 0);
239
240 QHBarModelMapper *mapper = new QHBarModelMapper;
241 mapper->setFirstBarSetRow(0);
242 mapper->setLastBarSetRow(1);
243 mapper->setModel(m_model);
244 mapper->setSeries(m_series);
245 mapper->setFirst(first);
246 mapper->setCount(countLimit);
247 m_chart->addSeries(m_series);
248
249 QCOMPARE(m_series->barsetCount(), expectedBarSetCount);
250
251 if (expectedBarSetCount > 0)
252 QCOMPARE(m_series->barSets().first()->count(), expectedCount);
253
254 // change values column mapping to invalid
255 mapper->setFirstBarSetRow(-1);
256 mapper->setLastBarSetRow(1);
257
258 QCOMPARE(m_series->barsetCount(), 0);
259
260 delete mapper;
261 mapper = 0;
262 }
263
264 QTEST_MAIN(tst_qbarmodelmapper)
265
266 #include "tst_qbarmodelmapper.moc"
@@ -191,7 +191,7 int QBarModelMapper::firstBarSetSection() const
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
192 {
192 {
193 Q_D(QBarModelMapper);
193 Q_D(QBarModelMapper);
194 d->m_firstBarSetSection = firstBarSetSection;
194 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
195 d->initializeBarFromModel();
195 d->initializeBarFromModel();
196 }
196 }
197
197
@@ -211,7 +211,7 int QBarModelMapper::lastBarSetSection() const
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
212 {
212 {
213 Q_D(QBarModelMapper);
213 Q_D(QBarModelMapper);
214 d->m_lastBarSetSection = lastBarSetSection;
214 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
215 d->initializeBarFromModel();
215 d->initializeBarFromModel();
216 }
216 }
217
217
@@ -247,11 +247,11 bool QBarSeries::insert(int index, QBarSet *set)
247 void QBarSeries::clear()
247 void QBarSeries::clear()
248 {
248 {
249 Q_D(QBarSeries);
249 Q_D(QBarSeries);
250 bool success = d->remove(barSets());
250 QList<QBarSet *> sets = barSets();
251 bool success = d->remove(sets);
251 if (success) {
252 if (success) {
252 emit barsetsRemoved(sets);
253 emit barsetsRemoved(sets);
253 }
254 }
254 return success;
255 }
255 }
256
256
257 /*!
257 /*!
@@ -3,7 +3,7
3 }
3 }
4
4
5 TEMPLATE = subdirs
5 TEMPLATE = subdirs
6 SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper
6 SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper qbarmodelmapper
7
7
8 test_private:{
8 test_private:{
9 SUBDIRS += chartdataset domain
9 SUBDIRS += chartdataset domain
General Comments 0
You need to be logged in to leave comments. Login now