##// END OF EJS Templates
Added more tests to QPieModelMapper
Marek Rosa -
r1427:7897f923faab
parent child
Show More
@@ -24,6 +24,7
24 #include <qchart.h>
24 #include <qchart.h>
25 #include <qchartview.h>
25 #include <qchartview.h>
26 #include <qpieseries.h>
26 #include <qpieseries.h>
27 #include <qpieslice.h>
27 #include <qvpiemodelmapper.h>
28 #include <qvpiemodelmapper.h>
28 #include <qhpiemodelmapper.h>
29 #include <qhpiemodelmapper.h>
29 #include <QStandardItemModel>
30 #include <QStandardItemModel>
@@ -36,6 +37,8 class tst_qpiemodelmapper : public QObject
36
37
37 public:
38 public:
38 tst_qpiemodelmapper();
39 tst_qpiemodelmapper();
40 void createVerticalMapper();
41 void createHorizontalMapper();
39
42
40 private Q_SLOTS:
43 private Q_SLOTS:
41 void initTestCase();
44 void initTestCase();
@@ -51,12 +54,22 class tst_qpiemodelmapper : public QObject
51 void horizontalMapperCustomMapping_data();
54 void horizontalMapperCustomMapping_data();
52 void horizontalMapperCustomMapping();
55 void horizontalMapperCustomMapping();
53 void seriesUpdated();
56 void seriesUpdated();
54
57 void verticalModelInsertRows();
58 void verticalModelRemoveRows();
59 void verticalModelInsertColumns();
60 void verticalModelRemoveColumns();
61 void horizontalModelInsertRows();
62 void horizontalModelRemoveRows();
63 void horizontalModelInsertColumns();
64 void horizontalModelRemoveColumns();
65 void modelUpdateCell();
55
66
56 private:
67 private:
57 QStandardItemModel *m_model;
68 QStandardItemModel *m_model;
58 int m_modelRowCount;
69 int m_modelRowCount;
59 int m_modelColumnCount;
70 int m_modelColumnCount;
71 QVPieModelMapper *m_vMapper;
72 QHPieModelMapper *m_hMapper;
60
73
61 QPieSeries *m_series;
74 QPieSeries *m_series;
62 QChart *m_chart;
75 QChart *m_chart;
@@ -65,21 +78,66 class tst_qpiemodelmapper : public QObject
65 tst_qpiemodelmapper::tst_qpiemodelmapper():
78 tst_qpiemodelmapper::tst_qpiemodelmapper():
66 m_model(0),
79 m_model(0),
67 m_modelRowCount(10),
80 m_modelRowCount(10),
68 m_modelColumnCount(8)
81 m_modelColumnCount(8),
82 m_vMapper(0),
83 m_hMapper(0),
84 m_series(0),
85 m_chart(0)
86 {
87 }
88
89 void tst_qpiemodelmapper::createVerticalMapper()
90 {
91 m_vMapper = new QVPieModelMapper;
92 QVERIFY(m_vMapper->model() == 0);
93 m_vMapper->setValuesColumn(0);
94 m_vMapper->setLabelsColumn(1);
95 m_vMapper->setModel(m_model);
96 m_vMapper->setSeries(m_series);
97 }
98
99 void tst_qpiemodelmapper::createHorizontalMapper()
69 {
100 {
101 m_hMapper = new QHPieModelMapper;
102 QVERIFY(m_hMapper->model() == 0);
103 m_hMapper->setValuesRow(0);
104 m_hMapper->setLabelsRow(1);
105 m_hMapper->setModel(m_model);
106 m_hMapper->setSeries(m_series);
70 }
107 }
71
108
72 void tst_qpiemodelmapper::init()
109 void tst_qpiemodelmapper::init()
73 {
110 {
74 m_series = new QPieSeries;
111 m_series = new QPieSeries;
75 m_chart->addSeries(m_series);
112 m_chart->addSeries(m_series);
113
114 m_model = new QStandardItemModel(m_modelRowCount, m_modelColumnCount, this);
115 for (int row = 0; row < m_modelRowCount; ++row) {
116 for (int column = 0; column < m_modelColumnCount; column++) {
117 m_model->setData(m_model->index(row, column), row * column);
118 }
119 }
76 }
120 }
77
121
78 void tst_qpiemodelmapper::cleanup()
122 void tst_qpiemodelmapper::cleanup()
79 {
123 {
80 m_chart->removeSeries(m_series);
124 m_chart->removeSeries(m_series);
81 delete m_series;
125 m_series->deleteLater();
82 m_series = 0;
126 m_series = 0;
127
128 m_model->clear();
129 m_model->deleteLater();
130 m_model = 0;
131
132 if (m_vMapper) {
133 m_vMapper->deleteLater();
134 m_vMapper = 0;
135 }
136
137 if (m_hMapper) {
138 m_hMapper->deleteLater();
139 m_hMapper = 0;
140 }
83 }
141 }
84
142
85 void tst_qpiemodelmapper::initTestCase()
143 void tst_qpiemodelmapper::initTestCase()
@@ -87,19 +145,11 void tst_qpiemodelmapper::initTestCase()
87 m_chart = new QChart;
145 m_chart = new QChart;
88 QChartView *chartView = new QChartView(m_chart);
146 QChartView *chartView = new QChartView(m_chart);
89 chartView->show();
147 chartView->show();
90
91 m_model = new QStandardItemModel(this);
92 for (int row = 0; row < m_modelRowCount; ++row) {
93 for (int column = 0; column < m_modelColumnCount; column++) {
94 QStandardItem *item = new QStandardItem(row * column);
95 m_model->setItem(row, column, item);
96 }
97 }
98 }
148 }
99
149
100 void tst_qpiemodelmapper::cleanupTestCase()
150 void tst_qpiemodelmapper::cleanupTestCase()
101 {
151 {
102 m_model->clear();
152 //
103 }
153 }
104
154
105 void tst_qpiemodelmapper::verticalMapper_data()
155 void tst_qpiemodelmapper::verticalMapper_data()
@@ -259,33 +309,218 void tst_qpiemodelmapper::horizontalMapperCustomMapping()
259
309
260 void tst_qpiemodelmapper::seriesUpdated()
310 void tst_qpiemodelmapper::seriesUpdated()
261 {
311 {
262 QStandardItemModel *otherModel = new QStandardItemModel;
312 // setup the mapper
263 for (int row = 0; row < m_modelRowCount; ++row) {
313 createVerticalMapper();
264 for (int column = 0; column < m_modelColumnCount; column++) {
265 QStandardItem *item = new QStandardItem(row * column);
266 otherModel->setItem(row, column, item);
267 }
268 }
269
270 QVPieModelMapper *mapper = new QVPieModelMapper;
271 mapper->setValuesColumn(0);
272 mapper->setLabelsColumn(1);
273 mapper->setModel(otherModel);
274 mapper->setSeries(m_series);
275 QCOMPARE(m_series->count(), m_modelRowCount);
314 QCOMPARE(m_series->count(), m_modelRowCount);
276 QCOMPARE(mapper->count(), -1);
315 QCOMPARE(m_vMapper->count(), -1);
277
316
278 m_series->append("1000", 1000);
317 m_series->append("1000", 1000);
279 QCOMPARE(m_series->count(), m_modelRowCount + 1);
318 QCOMPARE(m_series->count(), m_modelRowCount + 1);
280 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
319 QCOMPARE(m_vMapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
281
320
282 m_series->remove(m_series->slices().last());
321 m_series->remove(m_series->slices().last());
283 QCOMPARE(m_series->count(), m_modelRowCount);
322 QCOMPARE(m_series->count(), m_modelRowCount);
284 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
323 QCOMPARE(m_vMapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
324
325 QPieSlice *slice = m_series->slices().first();
326 slice->setValue(25.0);
327 slice->setLabel(QString("25.0"));
328 QCOMPARE(m_model->data(m_model->index(0, 0)).toReal(), 25.0);
329 QCOMPARE(m_model->data(m_model->index(0, 1)).toString(), QString("25.0"));
330 }
331
332 void tst_qpiemodelmapper::verticalModelInsertRows()
333 {
334 // setup the mapper
335 createVerticalMapper();
336 QCOMPARE(m_series->count(), m_modelRowCount);
337 QVERIFY(m_vMapper->model() != 0);
338
339 int insertCount = 4;
340 m_model->insertRows(3, insertCount);
341 QCOMPARE(m_series->count(), m_modelRowCount + insertCount);
342
343 int first = 3;
344 m_vMapper->setFirst(3);
345 QCOMPARE(m_series->count(), m_modelRowCount + insertCount - first);
346
347 m_model->insertRows(3, insertCount);
348 QCOMPARE(m_series->count(), m_modelRowCount + 2 * insertCount - first);
349
350 int countLimit = 6;
351 m_vMapper->setCount(countLimit);
352 QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount + 2 * insertCount - first));
353
354 m_model->insertRows(3, insertCount);
355 QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount + 3 * insertCount - first));
356
357 m_vMapper->setFirst(0);
358 QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount + 3 * insertCount));
359
360 m_vMapper->setCount(-1);
361 QCOMPARE(m_series->count(), m_modelRowCount + 3 * insertCount);
362 }
363
364 void tst_qpiemodelmapper::verticalModelRemoveRows()
365 {
366 // setup the mapper
367 createVerticalMapper();
368 QCOMPARE(m_series->count(), m_modelRowCount);
369 QVERIFY(m_vMapper->model() != 0);
370
371 int removeCount = 2;
372 m_model->removeRows(1, removeCount);
373 QCOMPARE(m_series->count(), m_modelRowCount - removeCount);
374
375 int first = 1;
376 m_vMapper->setFirst(first);
377 QCOMPARE(m_series->count(), m_modelRowCount - removeCount - first);
378
379 m_model->removeRows(1, removeCount);
380 QCOMPARE(m_series->count(), m_modelRowCount - 2 * removeCount - first);
381
382 int countLimit = 3;
383 m_vMapper->setCount(countLimit);
384 QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount - 2 * removeCount - first));
385
386 m_model->removeRows(1, removeCount);
387 QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount - 3 * removeCount - first));
388
389 m_vMapper->setFirst(0);
390 QCOMPARE(m_series->count(), qMin(countLimit, m_modelRowCount - 3 * removeCount));
391
392 m_vMapper->setCount(-1);
393 QCOMPARE(m_series->count(), m_modelRowCount - 3 * removeCount);
394 }
395
396 void tst_qpiemodelmapper::verticalModelInsertColumns()
397 {
398 // setup the mapper
399 createVerticalMapper();
400 QCOMPARE(m_series->count(), m_modelRowCount);
401 QVERIFY(m_vMapper->model() != 0);
402
403 int insertCount = 4;
404 m_model->insertColumns(3, insertCount);
405 QCOMPARE(m_series->count(), m_modelRowCount);
406 }
407
408 void tst_qpiemodelmapper::verticalModelRemoveColumns()
409 {
410 // setup the mapper
411 createVerticalMapper();
412 QCOMPARE(m_series->count(), m_modelRowCount);
413 QVERIFY(m_vMapper->model() != 0);
414
415 int removeCount = m_modelColumnCount - 2;
416 m_model->removeColumns(0, removeCount);
417 QCOMPARE(m_series->count(), m_modelRowCount);
418
419 // leave only one column
420 m_model->removeColumns(0, m_modelColumnCount - removeCount - 1);
421 QCOMPARE(m_series->count(), 0);
422 }
423
424 void tst_qpiemodelmapper::horizontalModelInsertRows()
425 {
426 // setup the mapper
427 createHorizontalMapper();
428 QCOMPARE(m_series->count(), m_modelColumnCount);
429 QVERIFY(m_hMapper->model() != 0);
430
431 int insertCount = 4;
432 m_model->insertRows(3, insertCount);
433 QCOMPARE(m_series->count(), m_modelColumnCount);
434 }
435
436 void tst_qpiemodelmapper::horizontalModelRemoveRows()
437 {
438 // setup the mapper
439 createHorizontalMapper();
440 QCOMPARE(m_series->count(), m_modelColumnCount);
441 QVERIFY(m_hMapper->model() != 0);
442
443 int removeCount = m_modelRowCount - 2;
444 m_model->removeRows(0, removeCount);
445 QCOMPARE(m_series->count(), m_modelColumnCount);
446
447 // leave only one column
448 m_model->removeRows(0, m_modelRowCount - removeCount - 1);
449 QCOMPARE(m_series->count(), 0);
450 }
451
452 void tst_qpiemodelmapper::horizontalModelInsertColumns()
453 {
454 // setup the mapper
455 createHorizontalMapper();
456 QCOMPARE(m_series->count(), m_modelColumnCount);
457 QVERIFY(m_hMapper->model() != 0);
458
459 int insertCount = 4;
460 m_model->insertColumns(3, insertCount);
461 QCOMPARE(m_series->count(), m_modelColumnCount + insertCount);
462
463 int first = 3;
464 m_hMapper->setFirst(3);
465 QCOMPARE(m_series->count(), m_modelColumnCount + insertCount - first);
466
467 m_model->insertColumns(3, insertCount);
468 QCOMPARE(m_series->count(), m_modelColumnCount + 2 * insertCount - first);
469
470 int countLimit = 6;
471 m_hMapper->setCount(countLimit);
472 QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount + 2 * insertCount - first));
473
474 m_model->insertColumns(3, insertCount);
475 QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount + 3 * insertCount - first));
476
477 m_hMapper->setFirst(0);
478 QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount + 3 * insertCount));
479
480 m_hMapper->setCount(-1);
481 QCOMPARE(m_series->count(), m_modelColumnCount + 3 * insertCount);
482 }
483
484 void tst_qpiemodelmapper::horizontalModelRemoveColumns()
485 {
486 // setup the mapper
487 createHorizontalMapper();
488 QCOMPARE(m_series->count(), m_modelColumnCount);
489 QVERIFY(m_hMapper->model() != 0);
490
491 int removeCount = 2;
492 m_model->removeColumns(1, removeCount);
493 QCOMPARE(m_series->count(), m_modelColumnCount - removeCount);
494
495 int first = 1;
496 m_hMapper->setFirst(first);
497 QCOMPARE(m_series->count(), m_modelColumnCount - removeCount - first);
498
499 m_model->removeColumns(1, removeCount);
500 QCOMPARE(m_series->count(), m_modelColumnCount - 2 * removeCount - first);
501
502 int countLimit = 3;
503 m_hMapper->setCount(countLimit);
504 QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount - 2 * removeCount - first));
505
506 m_model->removeColumns(1, removeCount);
507 QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount - 3 * removeCount - first));
508
509 m_hMapper->setFirst(0);
510 QCOMPARE(m_series->count(), qMin(countLimit, m_modelColumnCount - 3 * removeCount));
511
512 m_hMapper->setCount(-1);
513 QCOMPARE(m_series->count(), m_modelColumnCount - 3 * removeCount);
514 }
515
516 void tst_qpiemodelmapper::modelUpdateCell()
517 {
518 // setup the mapper
519 createVerticalMapper();
285
520
286 otherModel->clear();
521 QVERIFY(m_model->setData(m_model->index(1, 0), 44));
287 delete otherModel;
522 QCOMPARE(m_series->slices().at(1)->value(), 44.0);
288 otherModel = 0;
523 QCOMPARE(m_model->data(m_model->index(1, 0)).toReal(), 44.0);
289 }
524 }
290
525
291 QTEST_MAIN(tst_qpiemodelmapper)
526 QTEST_MAIN(tst_qpiemodelmapper)
@@ -312,24 +312,22 void tst_qxymodelmapper::horizontalMapperCustomMapping()
312
312
313 void tst_qxymodelmapper::seriesUpdated()
313 void tst_qxymodelmapper::seriesUpdated()
314 {
314 {
315 QVXYModelMapper *mapper = new QVXYModelMapper;
315 // setup the mapper
316 mapper->setXColumn(0);
316 createVerticalMapper();
317 mapper->setYColumn(1);
318 mapper->setModel(m_model);
319 mapper->setSeries(m_series);
320 QCOMPARE(m_series->count(), m_modelRowCount);
317 QCOMPARE(m_series->count(), m_modelRowCount);
321 QCOMPARE(mapper->count(), -1);
318 QCOMPARE(m_vMapper->count(), -1);
322
319
323 m_series->append(QPointF(100, 100));
320 m_series->append(QPointF(100, 100));
324 QCOMPARE(m_series->count(), m_modelRowCount + 1);
321 QCOMPARE(m_series->count(), m_modelRowCount + 1);
325 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
322 QCOMPARE(m_vMapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
326
323
327 m_series->remove(m_series->points().last());
324 m_series->remove(m_series->points().last());
328 QCOMPARE(m_series->count(), m_modelRowCount);
325 QCOMPARE(m_series->count(), m_modelRowCount);
329 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
326 QCOMPARE(m_vMapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
330
327
331 delete mapper;
328 m_series->replace(m_series->points().first(), QPointF(25.0, 75.0));
332 mapper = 0;
329 QCOMPARE(m_model->data(m_model->index(0, 0)).toReal(), 25.0);
330 QCOMPARE(m_model->data(m_model->index(0, 1)).toReal(), 75.0);
333 }
331 }
334
332
335 void tst_qxymodelmapper::verticalModelInsertRows()
333 void tst_qxymodelmapper::verticalModelInsertRows()
General Comments 0
You need to be logged in to leave comments. Login now