##// END OF EJS Templates
Added tests for XYModelMapper
Marek Rosa -
r1352:6d51f9f77c12
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_qxymodelmapper.cpp
7
8 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_AUTOTESTS_BIN_DIR"
@@ -0,0 +1,273
1 #include <QtCore/QString>
2 #include <QtTest/QtTest>
3
4 #include <qchart.h>
5 #include <qchartview.h>
6 #include <qxyseries.h>
7 #include <qlineseries.h>
8 #include <qvxymodelmapper.h>
9 #include <qhxymodelmapper.h>
10 #include <QStandardItemModel>
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
14 class tst_qxymodelmapper : public QObject
15 {
16 Q_OBJECT
17
18 public:
19 tst_qxymodelmapper();
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 void seriesUpdated();
35
36 private:
37 QStandardItemModel *m_model;
38 int m_modelRowCount;
39 int m_modelColumnCount;
40
41 QXYSeries *m_series;
42 QChart *m_chart;
43 };
44
45 tst_qxymodelmapper::tst_qxymodelmapper():
46 m_model(0),
47 m_modelRowCount(10),
48 m_modelColumnCount(8)
49 {
50 }
51
52 void tst_qxymodelmapper::init()
53 {
54 m_series = new QLineSeries;
55 m_chart->addSeries(m_series);
56 }
57
58 void tst_qxymodelmapper::cleanup()
59 {
60 m_chart->removeSeries(m_series);
61 delete m_series;
62 m_series = 0;
63 }
64
65 void tst_qxymodelmapper::initTestCase()
66 {
67 m_chart = new QChart;
68 QChartView *chartView = new QChartView(m_chart);
69 chartView->show();
70
71 m_model = new QStandardItemModel(this);
72 for (int row = 0; row < m_modelRowCount; ++row) {
73 for (int column = 0; column < m_modelColumnCount; column++) {
74 QStandardItem *item = new QStandardItem(row * column);
75 m_model->setItem(row, column, item);
76 }
77 }
78 }
79
80 void tst_qxymodelmapper::cleanupTestCase()
81 {
82 m_model->clear();
83 }
84
85 void tst_qxymodelmapper::verticalMapper_data()
86 {
87 QTest::addColumn<int>("xColumn");
88 QTest::addColumn<int>("yColumn");
89 QTest::addColumn<int>("expectedCount");
90 QTest::newRow("different x and y columns") << 0 << 1 << m_modelRowCount;
91 QTest::newRow("same x and y columns") << 1 << 1 << m_modelRowCount;
92 QTest::newRow("invalid x column and correct y column") << -3 << 1 << 0;
93 QTest::newRow("x column beyond the size of model and correct y column") << m_modelColumnCount << 1 << 0;
94 QTest::newRow("x column beyond the size of model and invalid y column") << m_modelColumnCount << -1 << 0;
95 }
96
97 void tst_qxymodelmapper::verticalMapper()
98 {
99 QFETCH(int, xColumn);
100 QFETCH(int, yColumn);
101 QFETCH(int, expectedCount);
102
103 QVXYModelMapper *mapper = new QVXYModelMapper;
104 mapper->setXColumn(xColumn);
105 mapper->setYColumn(yColumn);
106 mapper->setModel(m_model);
107 mapper->setSeries(m_series);
108
109 QCOMPARE(m_series->count(), expectedCount);
110 QCOMPARE(mapper->xColumn(), qMax(-1, xColumn));
111 QCOMPARE(mapper->yColumn(), qMax(-1, yColumn));
112
113 delete mapper;
114 mapper = 0;
115 }
116
117 void tst_qxymodelmapper::verticalMapperCustomMapping_data()
118 {
119 QTest::addColumn<int>("first");
120 QTest::addColumn<int>("countLimit");
121 QTest::addColumn<int>("expectedCount");
122 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelRowCount;
123 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelRowCount - 3;
124 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelRowCount);
125 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelRowCount - 3);
126 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0;
127 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0;
128 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;
129 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelRowCount;
130 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelRowCount;
131 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelRowCount;
132
133 }
134
135 void tst_qxymodelmapper::verticalMapperCustomMapping()
136 {
137 QFETCH(int, first);
138 QFETCH(int, countLimit);
139 QFETCH(int, expectedCount);
140
141 QCOMPARE(m_series->count(), 0);
142
143 QVXYModelMapper *mapper = new QVXYModelMapper;
144 mapper->setXColumn(0);
145 mapper->setYColumn(1);
146 mapper->setModel(m_model);
147 mapper->setSeries(m_series);
148 mapper->setFirst(first);
149 mapper->setCount(countLimit);
150
151 QCOMPARE(m_series->count(), expectedCount);
152
153 // change values column mapping to invalid
154 mapper->setXColumn(-1);
155 mapper->setYColumn(1);
156
157 QCOMPARE(m_series->count(), 0);
158
159 delete mapper;
160 mapper = 0;
161 }
162
163 void tst_qxymodelmapper::horizontalMapper_data()
164 {
165 QTest::addColumn<int>("xRow");
166 QTest::addColumn<int>("yRow");
167 QTest::addColumn<int>("expectedCount");
168 QTest::newRow("different x and y rows") << 0 << 1 << m_modelColumnCount;
169 QTest::newRow("same x and y rows") << 1 << 1 << m_modelColumnCount;
170 QTest::newRow("invalid x row and correct y row") << -3 << 1 << 0;
171 QTest::newRow("x row beyond the size of model and correct y row") << m_modelRowCount << 1 << 0;
172 QTest::newRow("x row beyond the size of model and invalid y row") << m_modelRowCount << -1 << 0;
173 }
174
175 void tst_qxymodelmapper::horizontalMapper()
176 {
177 QFETCH(int, xRow);
178 QFETCH(int, yRow);
179 QFETCH(int, expectedCount);
180
181 QHXYModelMapper *mapper = new QHXYModelMapper;
182 mapper->setXRow(xRow);
183 mapper->setYRow(yRow);
184 mapper->setModel(m_model);
185 mapper->setSeries(m_series);
186
187 QCOMPARE(m_series->count(), expectedCount);
188 QCOMPARE(mapper->xRow(), qMax(-1, xRow));
189 QCOMPARE(mapper->yRow(), qMax(-1, yRow));
190
191 delete mapper;
192 mapper = 0;
193 }
194
195 void tst_qxymodelmapper::horizontalMapperCustomMapping_data()
196 {
197 QTest::addColumn<int>("first");
198 QTest::addColumn<int>("countLimit");
199 QTest::addColumn<int>("expectedCount");
200 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelColumnCount;
201 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelColumnCount - 3;
202 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelColumnCount);
203 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelColumnCount - 3);
204 QTest::newRow("first: +1 greater then the number of columns in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0;
205 QTest::newRow("first: +1 greater then the number of columns in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0;
206 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;
207 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelColumnCount;
208 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelColumnCount;
209 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount;
210 }
211
212 void tst_qxymodelmapper::horizontalMapperCustomMapping()
213 {
214 QFETCH(int, first);
215 QFETCH(int, countLimit);
216 QFETCH(int, expectedCount);
217
218 QCOMPARE(m_series->count(), 0);
219
220 QHXYModelMapper *mapper = new QHXYModelMapper;
221 mapper->setXRow(0);
222 mapper->setYRow(1);
223 mapper->setModel(m_model);
224 mapper->setSeries(m_series);
225 mapper->setFirst(first);
226 mapper->setCount(countLimit);
227
228 QCOMPARE(m_series->count(), expectedCount);
229
230 // change values row mapping to invalid
231 mapper->setXRow(-1);
232 mapper->setYRow(1);
233
234 QCOMPARE(m_series->count(), 0);
235
236 delete mapper;
237 mapper = 0;
238 }
239
240 void tst_qxymodelmapper::seriesUpdated()
241 {
242 QStandardItemModel *otherModel = new QStandardItemModel;
243 for (int row = 0; row < m_modelRowCount; ++row) {
244 for (int column = 0; column < m_modelColumnCount; column++) {
245 QStandardItem *item = new QStandardItem(row * column);
246 otherModel->setItem(row, column, item);
247 }
248 }
249
250 QVXYModelMapper *mapper = new QVXYModelMapper;
251 mapper->setXColumn(0);
252 mapper->setYColumn(1);
253 mapper->setModel(otherModel);
254 mapper->setSeries(m_series);
255 QCOMPARE(m_series->count(), m_modelRowCount);
256 QCOMPARE(mapper->count(), -1);
257
258 m_series->append(QPointF(100, 100));
259 QCOMPARE(m_series->count(), m_modelRowCount + 1);
260 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
261
262 m_series->remove(m_series->points().last());
263 QCOMPARE(m_series->count(), m_modelRowCount);
264 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
265
266 otherModel->clear();
267 delete otherModel;
268 otherModel = 0;
269 }
270
271 QTEST_MAIN(tst_qxymodelmapper)
272
273 #include "tst_qxymodelmapper.moc"
@@ -166,7 +166,7 int QXYModelMapper::xSection() const
166 void QXYModelMapper::setXSection(int xSection)
166 void QXYModelMapper::setXSection(int xSection)
167 {
167 {
168 Q_D(QXYModelMapper);
168 Q_D(QXYModelMapper);
169 d->m_xSection = xSection;
169 d->m_xSection = qMax(-1, xSection);
170 d->initializeXYFromModel();
170 d->initializeXYFromModel();
171 }
171 }
172
172
@@ -186,7 +186,7 int QXYModelMapper::ySection() const
186 void QXYModelMapper::setYSection(int ySection)
186 void QXYModelMapper::setYSection(int ySection)
187 {
187 {
188 Q_D(QXYModelMapper);
188 Q_D(QXYModelMapper);
189 d->m_ySection = ySection;
189 d->m_ySection = qMax(-1, ySection);
190 d->initializeXYFromModel();
190 d->initializeXYFromModel();
191 }
191 }
192
192
@@ -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
6 SUBDIRS += qchartview qchart qlineseries qbarset qbarseries qstackedbarseries qpercentbarseries qgroupedbarseries qpieslice qpieseries qpiemodelmapper qsplineseries qscatterseries qxymodelmapper
7
7
8 test_private:{
8 test_private:{
9 SUBDIRS += chartdataset domain
9 SUBDIRS += chartdataset domain
@@ -10,12 +10,12
10
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
12
13 class tst_piemodelmapper : public QObject
13 class tst_qpiemodelmapper : public QObject
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public:
17 public:
18 tst_piemodelmapper();
18 tst_qpiemodelmapper();
19
19
20 private Q_SLOTS:
20 private Q_SLOTS:
21 void initTestCase();
21 void initTestCase();
@@ -42,27 +42,27 class tst_piemodelmapper : public QObject
42 QChart *m_chart;
42 QChart *m_chart;
43 };
43 };
44
44
45 tst_piemodelmapper::tst_piemodelmapper():
45 tst_qpiemodelmapper::tst_qpiemodelmapper():
46 m_model(0),
46 m_model(0),
47 m_modelRowCount(10),
47 m_modelRowCount(10),
48 m_modelColumnCount(8)
48 m_modelColumnCount(8)
49 {
49 {
50 }
50 }
51
51
52 void tst_piemodelmapper::init()
52 void tst_qpiemodelmapper::init()
53 {
53 {
54 m_series = new QPieSeries;
54 m_series = new QPieSeries;
55 m_chart->addSeries(m_series);
55 m_chart->addSeries(m_series);
56 }
56 }
57
57
58 void tst_piemodelmapper::cleanup()
58 void tst_qpiemodelmapper::cleanup()
59 {
59 {
60 m_chart->removeSeries(m_series);
60 m_chart->removeSeries(m_series);
61 delete m_series;
61 delete m_series;
62 m_series = 0;
62 m_series = 0;
63 }
63 }
64
64
65 void tst_piemodelmapper::initTestCase()
65 void tst_qpiemodelmapper::initTestCase()
66 {
66 {
67 m_chart = new QChart;
67 m_chart = new QChart;
68 QChartView *chartView = new QChartView(m_chart);
68 QChartView *chartView = new QChartView(m_chart);
@@ -77,12 +77,12 void tst_piemodelmapper::initTestCase()
77 }
77 }
78 }
78 }
79
79
80 void tst_piemodelmapper::cleanupTestCase()
80 void tst_qpiemodelmapper::cleanupTestCase()
81 {
81 {
82 m_model->clear();
82 m_model->clear();
83 }
83 }
84
84
85 void tst_piemodelmapper::verticalMapper_data()
85 void tst_qpiemodelmapper::verticalMapper_data()
86 {
86 {
87 QTest::addColumn<int>("valuesColumn");
87 QTest::addColumn<int>("valuesColumn");
88 QTest::addColumn<int>("labelsColumn");
88 QTest::addColumn<int>("labelsColumn");
@@ -91,10 +91,10 void tst_piemodelmapper::verticalMapper_data()
91 QTest::newRow("same values and labels columns") << 1 << 1 << m_modelRowCount;
91 QTest::newRow("same values and labels columns") << 1 << 1 << m_modelRowCount;
92 QTest::newRow("invalid values column and correct labels column") << -3 << 1 << 0;
92 QTest::newRow("invalid values column and correct labels column") << -3 << 1 << 0;
93 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << 1 << 0;
93 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << 1 << 0;
94 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << -1 << 0;
94 QTest::newRow("values column beyond the size of model and invalid labels column") << m_modelColumnCount << -1 << 0;
95 }
95 }
96
96
97 void tst_piemodelmapper::verticalMapper()
97 void tst_qpiemodelmapper::verticalMapper()
98 {
98 {
99 QFETCH(int, valuesColumn);
99 QFETCH(int, valuesColumn);
100 QFETCH(int, labelsColumn);
100 QFETCH(int, labelsColumn);
@@ -114,7 +114,7 void tst_piemodelmapper::verticalMapper()
114 mapper = 0;
114 mapper = 0;
115 }
115 }
116
116
117 void tst_piemodelmapper::verticalMapperCustomMapping_data()
117 void tst_qpiemodelmapper::verticalMapperCustomMapping_data()
118 {
118 {
119 QTest::addColumn<int>("first");
119 QTest::addColumn<int>("first");
120 QTest::addColumn<int>("countLimit");
120 QTest::addColumn<int>("countLimit");
@@ -132,7 +132,7 void tst_piemodelmapper::verticalMapperCustomMapping_data()
132
132
133 }
133 }
134
134
135 void tst_piemodelmapper::verticalMapperCustomMapping()
135 void tst_qpiemodelmapper::verticalMapperCustomMapping()
136 {
136 {
137 QFETCH(int, first);
137 QFETCH(int, first);
138 QFETCH(int, countLimit);
138 QFETCH(int, countLimit);
@@ -160,7 +160,7 void tst_piemodelmapper::verticalMapperCustomMapping()
160 mapper = 0;
160 mapper = 0;
161 }
161 }
162
162
163 void tst_piemodelmapper::horizontalMapper_data()
163 void tst_qpiemodelmapper::horizontalMapper_data()
164 {
164 {
165 QTest::addColumn<int>("valuesRow");
165 QTest::addColumn<int>("valuesRow");
166 QTest::addColumn<int>("labelsRow");
166 QTest::addColumn<int>("labelsRow");
@@ -172,7 +172,7 void tst_piemodelmapper::horizontalMapper_data()
172 QTest::newRow("values row beyond the size of model and invalid labels row") << m_modelRowCount << -1 << 0;
172 QTest::newRow("values row beyond the size of model and invalid labels row") << m_modelRowCount << -1 << 0;
173 }
173 }
174
174
175 void tst_piemodelmapper::horizontalMapper()
175 void tst_qpiemodelmapper::horizontalMapper()
176 {
176 {
177 QFETCH(int, valuesRow);
177 QFETCH(int, valuesRow);
178 QFETCH(int, labelsRow);
178 QFETCH(int, labelsRow);
@@ -192,7 +192,7 void tst_piemodelmapper::horizontalMapper()
192 mapper = 0;
192 mapper = 0;
193 }
193 }
194
194
195 void tst_piemodelmapper::horizontalMapperCustomMapping_data()
195 void tst_qpiemodelmapper::horizontalMapperCustomMapping_data()
196 {
196 {
197 QTest::addColumn<int>("first");
197 QTest::addColumn<int>("first");
198 QTest::addColumn<int>("countLimit");
198 QTest::addColumn<int>("countLimit");
@@ -209,7 +209,7 void tst_piemodelmapper::horizontalMapperCustomMapping_data()
209 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount;
209 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount;
210 }
210 }
211
211
212 void tst_piemodelmapper::horizontalMapperCustomMapping()
212 void tst_qpiemodelmapper::horizontalMapperCustomMapping()
213 {
213 {
214 QFETCH(int, first);
214 QFETCH(int, first);
215 QFETCH(int, countLimit);
215 QFETCH(int, countLimit);
@@ -237,7 +237,7 void tst_piemodelmapper::horizontalMapperCustomMapping()
237 mapper = 0;
237 mapper = 0;
238 }
238 }
239
239
240 void tst_piemodelmapper::seriesUpdated()
240 void tst_qpiemodelmapper::seriesUpdated()
241 {
241 {
242 QStandardItemModel *otherModel = new QStandardItemModel;
242 QStandardItemModel *otherModel = new QStandardItemModel;
243 for (int row = 0; row < m_modelRowCount; ++row) {
243 for (int row = 0; row < m_modelRowCount; ++row) {
@@ -268,6 +268,6 void tst_piemodelmapper::seriesUpdated()
268 otherModel = 0;
268 otherModel = 0;
269 }
269 }
270
270
271 QTEST_MAIN(tst_piemodelmapper)
271 QTEST_MAIN(tst_qpiemodelmapper)
272
272
273 #include "tst_qpiemodelmapper.moc"
273 #include "tst_qpiemodelmapper.moc"
General Comments 0
You need to be logged in to leave comments. Login now