@@ -1,293 +1,333 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtCore/QString> |
|
22 | 22 | #include <QtTest/QtTest> |
|
23 | 23 | |
|
24 | 24 | #include <qchart.h> |
|
25 | 25 | #include <qchartview.h> |
|
26 | 26 | #include <qxyseries.h> |
|
27 | 27 | #include <qlineseries.h> |
|
28 | 28 | #include <qvxymodelmapper.h> |
|
29 | 29 | #include <qhxymodelmapper.h> |
|
30 | 30 | #include <QStandardItemModel> |
|
31 | 31 | |
|
32 | 32 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
33 | 33 | |
|
34 | 34 | class tst_qxymodelmapper : public QObject |
|
35 | 35 | { |
|
36 | 36 | Q_OBJECT |
|
37 | 37 | |
|
38 | 38 | public: |
|
39 | 39 | tst_qxymodelmapper(); |
|
40 | 40 | |
|
41 | 41 | private Q_SLOTS: |
|
42 | 42 | void initTestCase(); |
|
43 | 43 | void cleanupTestCase(); |
|
44 | 44 | void init(); |
|
45 | 45 | void cleanup(); |
|
46 | 46 | void verticalMapper_data(); |
|
47 | 47 | void verticalMapper(); |
|
48 | 48 | void verticalMapperCustomMapping_data(); |
|
49 | 49 | void verticalMapperCustomMapping(); |
|
50 | 50 | void horizontalMapper_data(); |
|
51 | 51 | void horizontalMapper(); |
|
52 | 52 | void horizontalMapperCustomMapping_data(); |
|
53 | 53 | void horizontalMapperCustomMapping(); |
|
54 | 54 | void seriesUpdated(); |
|
55 | void modelUpdated(); | |
|
55 | 56 | |
|
56 | 57 | private: |
|
57 | 58 | QStandardItemModel *m_model; |
|
58 | 59 | int m_modelRowCount; |
|
59 | 60 | int m_modelColumnCount; |
|
60 | 61 | |
|
61 | 62 | QXYSeries *m_series; |
|
62 | 63 | QChart *m_chart; |
|
63 | 64 | }; |
|
64 | 65 | |
|
65 | 66 | tst_qxymodelmapper::tst_qxymodelmapper(): |
|
66 | 67 | m_model(0), |
|
67 | 68 | m_modelRowCount(10), |
|
68 | 69 | m_modelColumnCount(8) |
|
69 | 70 | { |
|
70 | 71 | } |
|
71 | 72 | |
|
72 | 73 | void tst_qxymodelmapper::init() |
|
73 | 74 | { |
|
74 | 75 | m_series = new QLineSeries; |
|
75 | 76 | m_chart->addSeries(m_series); |
|
76 | 77 | } |
|
77 | 78 | |
|
78 | 79 | void tst_qxymodelmapper::cleanup() |
|
79 | 80 | { |
|
80 | 81 | m_chart->removeSeries(m_series); |
|
81 | 82 | delete m_series; |
|
82 | 83 | m_series = 0; |
|
83 | 84 | } |
|
84 | 85 | |
|
85 | 86 | void tst_qxymodelmapper::initTestCase() |
|
86 | 87 | { |
|
87 | 88 | m_chart = new QChart; |
|
88 | 89 | QChartView *chartView = new QChartView(m_chart); |
|
89 | 90 | chartView->show(); |
|
90 | 91 | |
|
91 | 92 | m_model = new QStandardItemModel(this); |
|
92 | 93 | for (int row = 0; row < m_modelRowCount; ++row) { |
|
93 | 94 | for (int column = 0; column < m_modelColumnCount; column++) { |
|
94 | 95 | QStandardItem *item = new QStandardItem(row * column); |
|
95 | 96 | m_model->setItem(row, column, item); |
|
96 | 97 | } |
|
97 | 98 | } |
|
98 | 99 | } |
|
99 | 100 | |
|
100 | 101 | void tst_qxymodelmapper::cleanupTestCase() |
|
101 | 102 | { |
|
102 | 103 | m_model->clear(); |
|
103 | 104 | } |
|
104 | 105 | |
|
105 | 106 | void tst_qxymodelmapper::verticalMapper_data() |
|
106 | 107 | { |
|
107 | 108 | QTest::addColumn<int>("xColumn"); |
|
108 | 109 | QTest::addColumn<int>("yColumn"); |
|
109 | 110 | QTest::addColumn<int>("expectedCount"); |
|
110 | 111 | QTest::newRow("different x and y columns") << 0 << 1 << m_modelRowCount; |
|
111 | 112 | QTest::newRow("same x and y columns") << 1 << 1 << m_modelRowCount; |
|
112 | 113 | QTest::newRow("invalid x column and correct y column") << -3 << 1 << 0; |
|
113 | 114 | QTest::newRow("x column beyond the size of model and correct y column") << m_modelColumnCount << 1 << 0; |
|
114 | 115 | QTest::newRow("x column beyond the size of model and invalid y column") << m_modelColumnCount << -1 << 0; |
|
115 | 116 | } |
|
116 | 117 | |
|
117 | 118 | void tst_qxymodelmapper::verticalMapper() |
|
118 | 119 | { |
|
119 | 120 | QFETCH(int, xColumn); |
|
120 | 121 | QFETCH(int, yColumn); |
|
121 | 122 | QFETCH(int, expectedCount); |
|
122 | 123 | |
|
123 | 124 | QVXYModelMapper *mapper = new QVXYModelMapper; |
|
125 | QVERIFY(mapper->model() == 0); | |
|
126 | ||
|
124 | 127 | mapper->setXColumn(xColumn); |
|
125 | 128 | mapper->setYColumn(yColumn); |
|
126 | 129 | mapper->setModel(m_model); |
|
127 | 130 | mapper->setSeries(m_series); |
|
128 | 131 | |
|
129 | 132 | QCOMPARE(m_series->count(), expectedCount); |
|
130 | 133 | QCOMPARE(mapper->xColumn(), qMax(-1, xColumn)); |
|
131 | 134 | QCOMPARE(mapper->yColumn(), qMax(-1, yColumn)); |
|
132 | 135 | |
|
133 | 136 | delete mapper; |
|
134 | 137 | mapper = 0; |
|
135 | 138 | } |
|
136 | 139 | |
|
137 | 140 | void tst_qxymodelmapper::verticalMapperCustomMapping_data() |
|
138 | 141 | { |
|
139 | 142 | QTest::addColumn<int>("first"); |
|
140 | 143 | QTest::addColumn<int>("countLimit"); |
|
141 | 144 | QTest::addColumn<int>("expectedCount"); |
|
142 | 145 | QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelRowCount; |
|
143 | 146 | QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelRowCount - 3; |
|
144 | 147 | QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelRowCount); |
|
145 | 148 | QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelRowCount - 3); |
|
146 | 149 | QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0; |
|
147 | 150 | QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0; |
|
148 | 151 | 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; |
|
149 | 152 | QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelRowCount; |
|
150 | 153 | QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelRowCount; |
|
151 | 154 | QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelRowCount; |
|
152 | 155 | |
|
153 | 156 | } |
|
154 | 157 | |
|
155 | 158 | void tst_qxymodelmapper::verticalMapperCustomMapping() |
|
156 | 159 | { |
|
157 | 160 | QFETCH(int, first); |
|
158 | 161 | QFETCH(int, countLimit); |
|
159 | 162 | QFETCH(int, expectedCount); |
|
160 | 163 | |
|
161 | 164 | QCOMPARE(m_series->count(), 0); |
|
162 | 165 | |
|
163 | 166 | QVXYModelMapper *mapper = new QVXYModelMapper; |
|
164 | 167 | mapper->setXColumn(0); |
|
165 | 168 | mapper->setYColumn(1); |
|
166 | 169 | mapper->setModel(m_model); |
|
167 | 170 | mapper->setSeries(m_series); |
|
168 | 171 | mapper->setFirst(first); |
|
169 | 172 | mapper->setCount(countLimit); |
|
170 | 173 | |
|
171 | 174 | QCOMPARE(m_series->count(), expectedCount); |
|
172 | 175 | |
|
173 | 176 | // change values column mapping to invalid |
|
174 | 177 | mapper->setXColumn(-1); |
|
175 | 178 | mapper->setYColumn(1); |
|
176 | 179 | |
|
177 | 180 | QCOMPARE(m_series->count(), 0); |
|
178 | 181 | |
|
179 | 182 | delete mapper; |
|
180 | 183 | mapper = 0; |
|
181 | 184 | } |
|
182 | 185 | |
|
183 | 186 | void tst_qxymodelmapper::horizontalMapper_data() |
|
184 | 187 | { |
|
185 | 188 | QTest::addColumn<int>("xRow"); |
|
186 | 189 | QTest::addColumn<int>("yRow"); |
|
187 | 190 | QTest::addColumn<int>("expectedCount"); |
|
188 | 191 | QTest::newRow("different x and y rows") << 0 << 1 << m_modelColumnCount; |
|
189 | 192 | QTest::newRow("same x and y rows") << 1 << 1 << m_modelColumnCount; |
|
190 | 193 | QTest::newRow("invalid x row and correct y row") << -3 << 1 << 0; |
|
191 | 194 | QTest::newRow("x row beyond the size of model and correct y row") << m_modelRowCount << 1 << 0; |
|
192 | 195 | QTest::newRow("x row beyond the size of model and invalid y row") << m_modelRowCount << -1 << 0; |
|
193 | 196 | } |
|
194 | 197 | |
|
195 | 198 | void tst_qxymodelmapper::horizontalMapper() |
|
196 | 199 | { |
|
197 | 200 | QFETCH(int, xRow); |
|
198 | 201 | QFETCH(int, yRow); |
|
199 | 202 | QFETCH(int, expectedCount); |
|
200 | 203 | |
|
201 | 204 | QHXYModelMapper *mapper = new QHXYModelMapper; |
|
202 | 205 | mapper->setXRow(xRow); |
|
203 | 206 | mapper->setYRow(yRow); |
|
204 | 207 | mapper->setModel(m_model); |
|
205 | 208 | mapper->setSeries(m_series); |
|
206 | 209 | |
|
207 | 210 | QCOMPARE(m_series->count(), expectedCount); |
|
208 | 211 | QCOMPARE(mapper->xRow(), qMax(-1, xRow)); |
|
209 | 212 | QCOMPARE(mapper->yRow(), qMax(-1, yRow)); |
|
210 | 213 | |
|
211 | 214 | delete mapper; |
|
212 | 215 | mapper = 0; |
|
213 | 216 | } |
|
214 | 217 | |
|
215 | 218 | void tst_qxymodelmapper::horizontalMapperCustomMapping_data() |
|
216 | 219 | { |
|
217 | 220 | QTest::addColumn<int>("first"); |
|
218 | 221 | QTest::addColumn<int>("countLimit"); |
|
219 | 222 | QTest::addColumn<int>("expectedCount"); |
|
220 | 223 | QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelColumnCount; |
|
221 | 224 | QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelColumnCount - 3; |
|
222 | 225 | QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelColumnCount); |
|
223 | 226 | QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelColumnCount - 3); |
|
224 | 227 | QTest::newRow("first: +1 greater then the number of columns in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0; |
|
225 | 228 | QTest::newRow("first: +1 greater then the number of columns in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0; |
|
226 | 229 | 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; |
|
227 | 230 | QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelColumnCount; |
|
228 | 231 | QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelColumnCount; |
|
229 | 232 | QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount; |
|
230 | 233 | } |
|
231 | 234 | |
|
232 | 235 | void tst_qxymodelmapper::horizontalMapperCustomMapping() |
|
233 | 236 | { |
|
234 | 237 | QFETCH(int, first); |
|
235 | 238 | QFETCH(int, countLimit); |
|
236 | 239 | QFETCH(int, expectedCount); |
|
237 | 240 | |
|
238 | 241 | QCOMPARE(m_series->count(), 0); |
|
239 | 242 | |
|
240 | 243 | QHXYModelMapper *mapper = new QHXYModelMapper; |
|
241 | 244 | mapper->setXRow(0); |
|
242 | 245 | mapper->setYRow(1); |
|
243 | 246 | mapper->setModel(m_model); |
|
244 | 247 | mapper->setSeries(m_series); |
|
245 | 248 | mapper->setFirst(first); |
|
246 | 249 | mapper->setCount(countLimit); |
|
247 | 250 | |
|
248 | 251 | QCOMPARE(m_series->count(), expectedCount); |
|
249 | 252 | |
|
250 | 253 | // change values row mapping to invalid |
|
251 | 254 | mapper->setXRow(-1); |
|
252 | 255 | mapper->setYRow(1); |
|
253 | 256 | |
|
254 | 257 | QCOMPARE(m_series->count(), 0); |
|
255 | 258 | |
|
256 | 259 | delete mapper; |
|
257 | 260 | mapper = 0; |
|
258 | 261 | } |
|
259 | 262 | |
|
260 | 263 | void tst_qxymodelmapper::seriesUpdated() |
|
261 | 264 | { |
|
262 | 265 | QStandardItemModel *otherModel = new QStandardItemModel; |
|
263 | 266 | for (int row = 0; row < m_modelRowCount; ++row) { |
|
264 | 267 | for (int column = 0; column < m_modelColumnCount; column++) { |
|
265 | 268 | QStandardItem *item = new QStandardItem(row * column); |
|
266 | 269 | otherModel->setItem(row, column, item); |
|
267 | 270 | } |
|
268 | 271 | } |
|
269 | 272 | |
|
270 | 273 | QVXYModelMapper *mapper = new QVXYModelMapper; |
|
271 | 274 | mapper->setXColumn(0); |
|
272 | 275 | mapper->setYColumn(1); |
|
273 | 276 | mapper->setModel(otherModel); |
|
274 | 277 | mapper->setSeries(m_series); |
|
275 | 278 | QCOMPARE(m_series->count(), m_modelRowCount); |
|
276 | 279 | QCOMPARE(mapper->count(), -1); |
|
277 | 280 | |
|
278 | 281 | m_series->append(QPointF(100, 100)); |
|
279 | 282 | QCOMPARE(m_series->count(), m_modelRowCount + 1); |
|
280 | 283 | QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model |
|
281 | 284 | |
|
282 | 285 | m_series->remove(m_series->points().last()); |
|
283 | 286 | QCOMPARE(m_series->count(), m_modelRowCount); |
|
284 | 287 | QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model |
|
285 | 288 | |
|
289 | delete mapper; | |
|
290 | mapper = 0; | |
|
291 | ||
|
292 | otherModel->clear(); | |
|
293 | delete otherModel; | |
|
294 | otherModel = 0; | |
|
295 | } | |
|
296 | ||
|
297 | void tst_qxymodelmapper::modelUpdated() | |
|
298 | { | |
|
299 | QStandardItemModel *otherModel = new QStandardItemModel; | |
|
300 | for (int row = 0; row < m_modelRowCount; ++row) { | |
|
301 | for (int column = 0; column < m_modelColumnCount; column++) { | |
|
302 | QStandardItem *item = new QStandardItem(row * column); | |
|
303 | otherModel->setItem(row, column, item); | |
|
304 | } | |
|
305 | } | |
|
306 | ||
|
307 | QVXYModelMapper *mapper = new QVXYModelMapper; | |
|
308 | QVERIFY(mapper->model() == 0); | |
|
309 | mapper->setXColumn(0); | |
|
310 | mapper->setYColumn(1); | |
|
311 | mapper->setModel(otherModel); | |
|
312 | mapper->setSeries(m_series); | |
|
313 | QCOMPARE(m_series->count(), m_modelRowCount); | |
|
314 | ||
|
315 | QVERIFY(mapper->model() != 0); | |
|
316 | ||
|
317 | if (otherModel->insertRows(3, 4)) | |
|
318 | QCOMPARE(m_series->count(), m_modelRowCount + 4); | |
|
319 | ||
|
320 | if (otherModel->removeRows(1, 5)) | |
|
321 | QCOMPARE(m_series->count(), m_modelRowCount - 1); | |
|
322 | ||
|
323 | delete mapper; | |
|
324 | mapper = 0; | |
|
325 | ||
|
286 | 326 | otherModel->clear(); |
|
287 | 327 | delete otherModel; |
|
288 | 328 | otherModel = 0; |
|
289 | 329 | } |
|
290 | 330 | |
|
291 | 331 | QTEST_MAIN(tst_qxymodelmapper) |
|
292 | 332 | |
|
293 | 333 | #include "tst_qxymodelmapper.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now