##// END OF EJS Templates
Adding series destruction tests for bar & pie...
Jani Honkonen -
r1768:c46eb44b5ea1
parent child
Show More
@@ -1,571 +1,587
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qbarseries.h>
22 #include <qbarseries.h>
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26 #include "tst_definitions.h"
26 #include "tst_definitions.h"
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 Q_DECLARE_METATYPE(QBarSet*)
30 Q_DECLARE_METATYPE(QBarSet*)
31
31
32 class tst_QBarSeries : public QObject
32 class tst_QBarSeries : public QObject
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35
35
36 public slots:
36 public slots:
37 void initTestCase();
37 void initTestCase();
38 void cleanupTestCase();
38 void cleanupTestCase();
39 void init();
39 void init();
40 void cleanup();
40 void cleanup();
41
41
42 private slots:
42 private slots:
43 void qbarseries_data();
43 void qbarseries_data();
44 void qbarseries();
44 void qbarseries();
45 void type_data();
45 void type_data();
46 void type();
46 void type();
47 void append_data();
47 void append_data();
48 void append();
48 void append();
49 void remove_data();
49 void remove_data();
50 void remove();
50 void remove();
51 void appendList_data();
51 void appendList_data();
52 void appendList();
52 void appendList();
53 void count_data();
53 void count_data();
54 void count();
54 void count();
55 void barSets_data();
55 void barSets_data();
56 void barSets();
56 void barSets();
57 void setLabelsVisible_data();
57 void setLabelsVisible_data();
58 void setLabelsVisible();
58 void setLabelsVisible();
59 void mouseclicked_data();
59 void mouseclicked_data();
60 void mouseclicked();
60 void mouseclicked();
61 void mousehovered_data();
61 void mousehovered_data();
62 void mousehovered();
62 void mousehovered();
63 void clearWithAnimations();
63 void clearWithAnimations();
64 void destruction();
64
65
65 private:
66 private:
66 QBarSeries* m_barseries;
67 QBarSeries* m_barseries;
67 QBarSeries* m_barseries_with_sets;
68 QBarSeries* m_barseries_with_sets;
68
69
69 QList<QBarSet*> m_testSets;
70 QList<QBarSet*> m_testSets;
70
71
71 };
72 };
72
73
73 void tst_QBarSeries::initTestCase()
74 void tst_QBarSeries::initTestCase()
74 {
75 {
75 qRegisterMetaType<QBarSet*>("QBarSet*");
76 qRegisterMetaType<QBarSet*>("QBarSet*");
76 }
77 }
77
78
78 void tst_QBarSeries::cleanupTestCase()
79 void tst_QBarSeries::cleanupTestCase()
79 {
80 {
80 }
81 }
81
82
82 void tst_QBarSeries::init()
83 void tst_QBarSeries::init()
83 {
84 {
84 m_barseries = new QBarSeries();
85 m_barseries = new QBarSeries();
85 m_barseries_with_sets = new QBarSeries();
86 m_barseries_with_sets = new QBarSeries();
86
87
87 for (int i=0; i<5; i++) {
88 for (int i=0; i<5; i++) {
88 m_testSets.append(new QBarSet("testset"));
89 m_testSets.append(new QBarSet("testset"));
89 m_barseries_with_sets->append(m_testSets.at(i));
90 m_barseries_with_sets->append(m_testSets.at(i));
90 }
91 }
91 }
92 }
92
93
93 void tst_QBarSeries::cleanup()
94 void tst_QBarSeries::cleanup()
94 {
95 {
95 foreach(QBarSet* s, m_testSets) {
96 foreach(QBarSet* s, m_testSets) {
96 m_barseries_with_sets->remove(s);
97 m_barseries_with_sets->remove(s);
97 delete s;
98 delete s;
98 }
99 }
99 m_testSets.clear();
100 m_testSets.clear();
100
101
101 delete m_barseries;
102 delete m_barseries;
102 m_barseries = 0;
103 m_barseries = 0;
103 delete m_barseries_with_sets;
104 delete m_barseries_with_sets;
104 m_barseries_with_sets = 0;
105 m_barseries_with_sets = 0;
105 }
106 }
106
107
107 void tst_QBarSeries::qbarseries_data()
108 void tst_QBarSeries::qbarseries_data()
108 {
109 {
109 }
110 }
110
111
111 void tst_QBarSeries::qbarseries()
112 void tst_QBarSeries::qbarseries()
112 {
113 {
113 QBarSeries *barseries = new QBarSeries();
114 QBarSeries *barseries = new QBarSeries();
114 QVERIFY(barseries != 0);
115 QVERIFY(barseries != 0);
115 }
116 }
116
117
117 void tst_QBarSeries::type_data()
118 void tst_QBarSeries::type_data()
118 {
119 {
119
120
120 }
121 }
121
122
122 void tst_QBarSeries::type()
123 void tst_QBarSeries::type()
123 {
124 {
124 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
125 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
125 }
126 }
126
127
127 void tst_QBarSeries::append_data()
128 void tst_QBarSeries::append_data()
128 {
129 {
129 }
130 }
130
131
131 void tst_QBarSeries::append()
132 void tst_QBarSeries::append()
132 {
133 {
133 QVERIFY(m_barseries->count() == 0);
134 QVERIFY(m_barseries->count() == 0);
134
135
135 bool ret = false;
136 bool ret = false;
136
137
137 // Try adding barset
138 // Try adding barset
138 QBarSet *barset = new QBarSet("testset");
139 QBarSet *barset = new QBarSet("testset");
139 ret = m_barseries->append(barset);
140 ret = m_barseries->append(barset);
140
141
141 QVERIFY(ret == true);
142 QVERIFY(ret == true);
142 QVERIFY(m_barseries->count() == 1);
143 QVERIFY(m_barseries->count() == 1);
143
144
144 // Try adding another set
145 // Try adding another set
145 QBarSet *barset2 = new QBarSet("testset2");
146 QBarSet *barset2 = new QBarSet("testset2");
146 ret = m_barseries->append(barset2);
147 ret = m_barseries->append(barset2);
147
148
148 QVERIFY(ret == true);
149 QVERIFY(ret == true);
149 QVERIFY(m_barseries->count() == 2);
150 QVERIFY(m_barseries->count() == 2);
150
151
151 // Try adding same set again
152 // Try adding same set again
152 ret = m_barseries->append(barset2);
153 ret = m_barseries->append(barset2);
153 QVERIFY(ret == false);
154 QVERIFY(ret == false);
154 QVERIFY(m_barseries->count() == 2);
155 QVERIFY(m_barseries->count() == 2);
155
156
156 // Try adding null set
157 // Try adding null set
157 ret = m_barseries->append(0);
158 ret = m_barseries->append(0);
158 QVERIFY(ret == false);
159 QVERIFY(ret == false);
159 QVERIFY(m_barseries->count() == 2);
160 QVERIFY(m_barseries->count() == 2);
160
161
161 }
162 }
162
163
163 void tst_QBarSeries::remove_data()
164 void tst_QBarSeries::remove_data()
164 {
165 {
165 }
166 }
166
167
167 void tst_QBarSeries::remove()
168 void tst_QBarSeries::remove()
168 {
169 {
169 int count = m_testSets.count();
170 int count = m_testSets.count();
170 QVERIFY(m_barseries_with_sets->count() == count);
171 QVERIFY(m_barseries_with_sets->count() == count);
171
172
172 // Try to remove null pointer (should not remove, should not crash)
173 // Try to remove null pointer (should not remove, should not crash)
173 bool ret = false;
174 bool ret = false;
174 ret = m_barseries_with_sets->remove(0);
175 ret = m_barseries_with_sets->remove(0);
175 QVERIFY(ret == false);
176 QVERIFY(ret == false);
176 QVERIFY(m_barseries_with_sets->count() == count);
177 QVERIFY(m_barseries_with_sets->count() == count);
177
178
178 // Try to remove invalid pointer (should not remove, should not crash)
179 // Try to remove invalid pointer (should not remove, should not crash)
179 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
180 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
180 QVERIFY(ret == false);
181 QVERIFY(ret == false);
181 QVERIFY(m_barseries_with_sets->count() == count);
182 QVERIFY(m_barseries_with_sets->count() == count);
182
183
183 // remove some sets
184 // remove some sets
184 ret = m_barseries_with_sets->remove(m_testSets.at(2));
185 ret = m_barseries_with_sets->remove(m_testSets.at(2));
185 QVERIFY(ret == true);
186 QVERIFY(ret == true);
186 ret = m_barseries_with_sets->remove(m_testSets.at(3));
187 ret = m_barseries_with_sets->remove(m_testSets.at(3));
187 QVERIFY(ret == true);
188 QVERIFY(ret == true);
188 ret = m_barseries_with_sets->remove(m_testSets.at(4));
189 ret = m_barseries_with_sets->remove(m_testSets.at(4));
189 QVERIFY(ret == true);
190 QVERIFY(ret == true);
190
191
191 QVERIFY(m_barseries_with_sets->count() == 2);
192 QVERIFY(m_barseries_with_sets->count() == 2);
192
193
193 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
194 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
194
195
195 QVERIFY(verifysets.at(0) == m_testSets.at(0));
196 QVERIFY(verifysets.at(0) == m_testSets.at(0));
196 QVERIFY(verifysets.at(1) == m_testSets.at(1));
197 QVERIFY(verifysets.at(1) == m_testSets.at(1));
197
198
198 // Try removing all sets again (should be ok, even if some sets have already been removed)
199 // Try removing all sets again (should be ok, even if some sets have already been removed)
199 ret = false;
200 ret = false;
200 for (int i=0; i<count; i++) {
201 for (int i=0; i<count; i++) {
201 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
202 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
202 }
203 }
203
204
204 QVERIFY(ret == true);
205 QVERIFY(ret == true);
205 QVERIFY(m_barseries_with_sets->count() == 0);
206 QVERIFY(m_barseries_with_sets->count() == 0);
206 }
207 }
207
208
208 void tst_QBarSeries::appendList_data()
209 void tst_QBarSeries::appendList_data()
209 {
210 {
210
211
211 }
212 }
212
213
213 void tst_QBarSeries::appendList()
214 void tst_QBarSeries::appendList()
214 {
215 {
215 int count = 5;
216 int count = 5;
216 QVERIFY(m_barseries->count() == 0);
217 QVERIFY(m_barseries->count() == 0);
217
218
218 QList<QBarSet*> sets;
219 QList<QBarSet*> sets;
219 for (int i=0; i<count; i++) {
220 for (int i=0; i<count; i++) {
220 sets.append(new QBarSet("testset"));
221 sets.append(new QBarSet("testset"));
221 }
222 }
222
223
223 // Append new sets (should succeed, count should match the count of sets)
224 // Append new sets (should succeed, count should match the count of sets)
224 bool ret = false;
225 bool ret = false;
225 ret = m_barseries->append(sets);
226 ret = m_barseries->append(sets);
226 QVERIFY(ret == true);
227 QVERIFY(ret == true);
227 QVERIFY(m_barseries->count() == count);
228 QVERIFY(m_barseries->count() == count);
228
229
229 // Append same sets again (should fail, count should remain same)
230 // Append same sets again (should fail, count should remain same)
230 ret = m_barseries->append(sets);
231 ret = m_barseries->append(sets);
231 QVERIFY(ret == false);
232 QVERIFY(ret == false);
232 QVERIFY(m_barseries->count() == count);
233 QVERIFY(m_barseries->count() == count);
233
234
234 // Try append empty list (should succeed, but count should remain same)
235 // Try append empty list (should succeed, but count should remain same)
235 QList<QBarSet*> invalidList;
236 QList<QBarSet*> invalidList;
236 ret = m_barseries->append(invalidList);
237 ret = m_barseries->append(invalidList);
237 QVERIFY(ret == true);
238 QVERIFY(ret == true);
238 QVERIFY(m_barseries->count() == count);
239 QVERIFY(m_barseries->count() == count);
239
240
240 // Try append list with one new and one existing set (should fail, count remains same)
241 // Try append list with one new and one existing set (should fail, count remains same)
241 invalidList.append(new QBarSet("ok set"));
242 invalidList.append(new QBarSet("ok set"));
242 invalidList.append(sets.at(0));
243 invalidList.append(sets.at(0));
243 ret = m_barseries->append(invalidList);
244 ret = m_barseries->append(invalidList);
244 QVERIFY(ret == false);
245 QVERIFY(ret == false);
245 QVERIFY(m_barseries->count() == count);
246 QVERIFY(m_barseries->count() == count);
246
247
247 // Try append list with null pointers (should fail, count remains same)
248 // Try append list with null pointers (should fail, count remains same)
248 QList<QBarSet*> invalidList2;
249 QList<QBarSet*> invalidList2;
249 invalidList2.append(0);
250 invalidList2.append(0);
250 invalidList2.append(0);
251 invalidList2.append(0);
251 invalidList2.append(0);
252 invalidList2.append(0);
252 ret = m_barseries->append(invalidList2);
253 ret = m_barseries->append(invalidList2);
253 QVERIFY(ret == false);
254 QVERIFY(ret == false);
254 QVERIFY(m_barseries->count() == count);
255 QVERIFY(m_barseries->count() == count);
255 }
256 }
256
257
257 void tst_QBarSeries::count_data()
258 void tst_QBarSeries::count_data()
258 {
259 {
259
260
260 }
261 }
261
262
262 void tst_QBarSeries::count()
263 void tst_QBarSeries::count()
263 {
264 {
264 QVERIFY(m_barseries->count() == 0);
265 QVERIFY(m_barseries->count() == 0);
265 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
266 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
266 }
267 }
267
268
268 void tst_QBarSeries::barSets_data()
269 void tst_QBarSeries::barSets_data()
269 {
270 {
270
271
271 }
272 }
272
273
273 void tst_QBarSeries::barSets()
274 void tst_QBarSeries::barSets()
274 {
275 {
275 QVERIFY(m_barseries->barSets().count() == 0);
276 QVERIFY(m_barseries->barSets().count() == 0);
276
277
277 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
278 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
278 QVERIFY(sets.count() == m_testSets.count());
279 QVERIFY(sets.count() == m_testSets.count());
279
280
280 for (int i=0; i<m_testSets.count(); i++) {
281 for (int i=0; i<m_testSets.count(); i++) {
281 QVERIFY(sets.at(i) == m_testSets.at(i));
282 QVERIFY(sets.at(i) == m_testSets.at(i));
282 }
283 }
283 }
284 }
284
285
285 void tst_QBarSeries::setLabelsVisible_data()
286 void tst_QBarSeries::setLabelsVisible_data()
286 {
287 {
287
288
288 }
289 }
289
290
290 void tst_QBarSeries::setLabelsVisible()
291 void tst_QBarSeries::setLabelsVisible()
291 {
292 {
292 // labels should be invisible by default
293 // labels should be invisible by default
293 QVERIFY(m_barseries->isLabelsVisible() == false);
294 QVERIFY(m_barseries->isLabelsVisible() == false);
294 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
295 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
295
296
296 // turn labels to visible
297 // turn labels to visible
297 m_barseries_with_sets->setLabelsVisible(true);
298 m_barseries_with_sets->setLabelsVisible(true);
298 // TODO: test the signal
299 // TODO: test the signal
299 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
300 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
300
301
301 // turn labels to invisible
302 // turn labels to invisible
302 m_barseries_with_sets->setLabelsVisible(false);
303 m_barseries_with_sets->setLabelsVisible(false);
303 // TODO: test the signal
304 // TODO: test the signal
304 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
305 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
305
306
306 // without parameter, should turn labels to visible
307 // without parameter, should turn labels to visible
307 m_barseries_with_sets->setLabelsVisible();
308 m_barseries_with_sets->setLabelsVisible();
308 // TODO: test the signal
309 // TODO: test the signal
309 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
310 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
310 }
311 }
311
312
312 void tst_QBarSeries::mouseclicked_data()
313 void tst_QBarSeries::mouseclicked_data()
313 {
314 {
314
315
315 }
316 }
316
317
317 void tst_QBarSeries::mouseclicked()
318 void tst_QBarSeries::mouseclicked()
318 {
319 {
319 QBarSeries* series = new QBarSeries();
320 QBarSeries* series = new QBarSeries();
320
321
321 QBarSet* set1 = new QBarSet(QString("set 1"));
322 QBarSet* set1 = new QBarSet(QString("set 1"));
322 *set1 << 10 << 10 << 10;
323 *set1 << 10 << 10 << 10;
323 series->append(set1);
324 series->append(set1);
324
325
325 QBarSet* set2 = new QBarSet(QString("set 2"));
326 QBarSet* set2 = new QBarSet(QString("set 2"));
326 *set2 << 10 << 10 << 10;
327 *set2 << 10 << 10 << 10;
327 series->append(set2);
328 series->append(set2);
328
329
329 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
330 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
330 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
331 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
331 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
332 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
332
333
333 QChartView view(new QChart());
334 QChartView view(new QChart());
334 view.resize(400,300);
335 view.resize(400,300);
335 view.chart()->addSeries(series);
336 view.chart()->addSeries(series);
336 view.show();
337 view.show();
337 QTest::qWaitForWindowShown(&view);
338 QTest::qWaitForWindowShown(&view);
338
339
339 //====================================================================================
340 //====================================================================================
340 // barset 1, bar 0
341 // barset 1, bar 0
341 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(95,143));
342 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(95,143));
342 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
343 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
343
344
344 QCOMPARE(seriesSpy.count(), 1);
345 QCOMPARE(seriesSpy.count(), 1);
345 QCOMPARE(setSpy1.count(), 1);
346 QCOMPARE(setSpy1.count(), 1);
346 QCOMPARE(setSpy2.count(), 0);
347 QCOMPARE(setSpy2.count(), 0);
347
348
348 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
349 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
349 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
350 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
350 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
351 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
351 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
352 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
352
353
353 QList<QVariant> setSpyArg = setSpy1.takeFirst();
354 QList<QVariant> setSpyArg = setSpy1.takeFirst();
354 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
355 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
355 QVERIFY(setSpyArg.at(0).toInt() == 0);
356 QVERIFY(setSpyArg.at(0).toInt() == 0);
356
357
357 //====================================================================================
358 //====================================================================================
358 // barset 1, bar 1
359 // barset 1, bar 1
359 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(187,143));
360 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(187,143));
360 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
361 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
361
362
362 QCOMPARE(seriesSpy.count(), 1);
363 QCOMPARE(seriesSpy.count(), 1);
363 QCOMPARE(setSpy1.count(), 1);
364 QCOMPARE(setSpy1.count(), 1);
364 QCOMPARE(setSpy2.count(), 0);
365 QCOMPARE(setSpy2.count(), 0);
365
366
366 seriesSpyArg = seriesSpy.takeFirst();
367 seriesSpyArg = seriesSpy.takeFirst();
367 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
368 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
368 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
369 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
369 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
370 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
370
371
371 setSpyArg = setSpy1.takeFirst();
372 setSpyArg = setSpy1.takeFirst();
372 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
373 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
373 QVERIFY(setSpyArg.at(0).toInt() == 1);
374 QVERIFY(setSpyArg.at(0).toInt() == 1);
374
375
375 //====================================================================================
376 //====================================================================================
376 // barset 1, bar 2
377 // barset 1, bar 2
377 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(280,143));
378 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(280,143));
378 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
379 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
379
380
380 QCOMPARE(seriesSpy.count(), 1);
381 QCOMPARE(seriesSpy.count(), 1);
381 QCOMPARE(setSpy1.count(), 1);
382 QCOMPARE(setSpy1.count(), 1);
382 QCOMPARE(setSpy2.count(), 0);
383 QCOMPARE(setSpy2.count(), 0);
383
384
384 seriesSpyArg = seriesSpy.takeFirst();
385 seriesSpyArg = seriesSpy.takeFirst();
385 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
386 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
386 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
387 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
387 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
388 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
388
389
389 setSpyArg = setSpy1.takeFirst();
390 setSpyArg = setSpy1.takeFirst();
390 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
391 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
391 QVERIFY(setSpyArg.at(0).toInt() == 2);
392 QVERIFY(setSpyArg.at(0).toInt() == 2);
392
393
393 //====================================================================================
394 //====================================================================================
394 // barset 2, bar 0
395 // barset 2, bar 0
395 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(118,143));
396 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(118,143));
396 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
397 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
397
398
398 QCOMPARE(seriesSpy.count(), 1);
399 QCOMPARE(seriesSpy.count(), 1);
399 QCOMPARE(setSpy1.count(), 0);
400 QCOMPARE(setSpy1.count(), 0);
400 QCOMPARE(setSpy2.count(), 1);
401 QCOMPARE(setSpy2.count(), 1);
401
402
402 seriesSpyArg = seriesSpy.takeFirst();
403 seriesSpyArg = seriesSpy.takeFirst();
403 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
404 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
404 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
405 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
405 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
406 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
406
407
407 setSpyArg = setSpy2.takeFirst();
408 setSpyArg = setSpy2.takeFirst();
408 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
409 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
409 QVERIFY(setSpyArg.at(0).toInt() == 0);
410 QVERIFY(setSpyArg.at(0).toInt() == 0);
410
411
411 //====================================================================================
412 //====================================================================================
412 // barset 2, bar 1
413 // barset 2, bar 1
413 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(210,143));
414 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(210,143));
414 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
415 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
415
416
416 QCOMPARE(seriesSpy.count(), 1);
417 QCOMPARE(seriesSpy.count(), 1);
417 QCOMPARE(setSpy1.count(), 0);
418 QCOMPARE(setSpy1.count(), 0);
418 QCOMPARE(setSpy2.count(), 1);
419 QCOMPARE(setSpy2.count(), 1);
419
420
420 seriesSpyArg = seriesSpy.takeFirst();
421 seriesSpyArg = seriesSpy.takeFirst();
421 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
422 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
422 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
423 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
423 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
424 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
424
425
425 setSpyArg = setSpy2.takeFirst();
426 setSpyArg = setSpy2.takeFirst();
426 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
427 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
427 QVERIFY(setSpyArg.at(0).toInt() == 1);
428 QVERIFY(setSpyArg.at(0).toInt() == 1);
428
429
429 //====================================================================================
430 //====================================================================================
430 // barset 2, bar 2
431 // barset 2, bar 2
431 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(303,143));
432 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(303,143));
432 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
433 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
433
434
434 QCOMPARE(seriesSpy.count(), 1);
435 QCOMPARE(seriesSpy.count(), 1);
435 QCOMPARE(setSpy1.count(), 0);
436 QCOMPARE(setSpy1.count(), 0);
436 QCOMPARE(setSpy2.count(), 1);
437 QCOMPARE(setSpy2.count(), 1);
437
438
438 seriesSpyArg = seriesSpy.takeFirst();
439 seriesSpyArg = seriesSpy.takeFirst();
439 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
440 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
440 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
441 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
441 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
442 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
442
443
443 setSpyArg = setSpy2.takeFirst();
444 setSpyArg = setSpy2.takeFirst();
444 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
445 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
445 QVERIFY(setSpyArg.at(0).toInt() == 2);
446 QVERIFY(setSpyArg.at(0).toInt() == 2);
446 }
447 }
447
448
448 void tst_QBarSeries::mousehovered_data()
449 void tst_QBarSeries::mousehovered_data()
449 {
450 {
450
451
451 }
452 }
452
453
453 void tst_QBarSeries::mousehovered()
454 void tst_QBarSeries::mousehovered()
454 {
455 {
455 QBarSeries* series = new QBarSeries();
456 QBarSeries* series = new QBarSeries();
456
457
457 QBarSet* set1 = new QBarSet(QString("set 1"));
458 QBarSet* set1 = new QBarSet(QString("set 1"));
458 *set1 << 10 << 10 << 10;
459 *set1 << 10 << 10 << 10;
459 series->append(set1);
460 series->append(set1);
460
461
461 QBarSet* set2 = new QBarSet(QString("set 2"));
462 QBarSet* set2 = new QBarSet(QString("set 2"));
462 *set2 << 10 << 10 << 10;
463 *set2 << 10 << 10 << 10;
463 series->append(set2);
464 series->append(set2);
464
465
465 QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*)));
466 QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*)));
466 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
467 QSignalSpy setSpy1(set1, SIGNAL(hovered(bool)));
467 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
468 QSignalSpy setSpy2(set2, SIGNAL(hovered(bool)));
468
469
469 QChartView view(new QChart());
470 QChartView view(new QChart());
470 view.resize(400,300);
471 view.resize(400,300);
471 view.chart()->addSeries(series);
472 view.chart()->addSeries(series);
472 view.show();
473 view.show();
473 QTest::qWaitForWindowShown(&view);
474 QTest::qWaitForWindowShown(&view);
474
475
475 //this is hack since view does not get events otherwise
476 //this is hack since view does not get events otherwise
476 view.setMouseTracking(true);
477 view.setMouseTracking(true);
477
478
478 //=======================================================================
479 //=======================================================================
479 // move mouse to left border
480 // move mouse to left border
480 QTest::mouseMove(view.viewport(), QPoint(0, 143));
481 QTest::mouseMove(view.viewport(), QPoint(0, 143));
481 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
482 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
482 TRY_COMPARE(seriesSpy.count(), 0);
483 TRY_COMPARE(seriesSpy.count(), 0);
483 TRY_COMPARE(setSpy1.count(), 0);
484 TRY_COMPARE(setSpy1.count(), 0);
484 TRY_COMPARE(setSpy2.count(), 0);
485 TRY_COMPARE(setSpy2.count(), 0);
485
486
486 //=======================================================================
487 //=======================================================================
487 // move mouse on top of set1
488 // move mouse on top of set1
488 QTest::mouseMove(view.viewport(), QPoint(95,143));
489 QTest::mouseMove(view.viewport(), QPoint(95,143));
489 TRY_COMPARE(seriesSpy.count(), 1);
490 TRY_COMPARE(seriesSpy.count(), 1);
490 TRY_COMPARE(setSpy1.count(), 1);
491 TRY_COMPARE(setSpy1.count(), 1);
491 TRY_COMPARE(setSpy2.count(), 0);
492 TRY_COMPARE(setSpy2.count(), 0);
492
493
493 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
494 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
494 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
495 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
495 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
496 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
496 QVERIFY(seriesSpyArg.at(0).toBool() == true);
497 QVERIFY(seriesSpyArg.at(0).toBool() == true);
497
498
498 QList<QVariant> setSpyArg = setSpy1.takeFirst();
499 QList<QVariant> setSpyArg = setSpy1.takeFirst();
499 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
500 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
500 QVERIFY(setSpyArg.at(0).toBool() == true);
501 QVERIFY(setSpyArg.at(0).toBool() == true);
501
502
502 //=======================================================================
503 //=======================================================================
503 // move mouse from top of set1 to top of set2
504 // move mouse from top of set1 to top of set2
504 QTest::mouseMove(view.viewport(), QPoint(118,143));
505 QTest::mouseMove(view.viewport(), QPoint(118,143));
505 TRY_COMPARE(seriesSpy.count(), 2);
506 TRY_COMPARE(seriesSpy.count(), 2);
506 TRY_COMPARE(setSpy1.count(), 1);
507 TRY_COMPARE(setSpy1.count(), 1);
507 TRY_COMPARE(setSpy2.count(), 1);
508 TRY_COMPARE(setSpy2.count(), 1);
508
509
509 // should leave set1
510 // should leave set1
510 seriesSpyArg = seriesSpy.takeFirst();
511 seriesSpyArg = seriesSpy.takeFirst();
511 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
512 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
512 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
513 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
513 QVERIFY(seriesSpyArg.at(0).toBool() == false);
514 QVERIFY(seriesSpyArg.at(0).toBool() == false);
514
515
515 setSpyArg = setSpy1.takeFirst();
516 setSpyArg = setSpy1.takeFirst();
516 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
517 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
517 QVERIFY(setSpyArg.at(0).toBool() == false);
518 QVERIFY(setSpyArg.at(0).toBool() == false);
518
519
519 // should enter set2
520 // should enter set2
520 seriesSpyArg = seriesSpy.takeFirst();
521 seriesSpyArg = seriesSpy.takeFirst();
521 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
522 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
522 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
523 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
523 QVERIFY(seriesSpyArg.at(0).toBool() == true);
524 QVERIFY(seriesSpyArg.at(0).toBool() == true);
524
525
525 setSpyArg = setSpy2.takeFirst();
526 setSpyArg = setSpy2.takeFirst();
526 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
527 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
527 QVERIFY(setSpyArg.at(0).toBool() == true);
528 QVERIFY(setSpyArg.at(0).toBool() == true);
528
529
529 //=======================================================================
530 //=======================================================================
530 // move mouse from top of set2 to background
531 // move mouse from top of set2 to background
531 QTest::mouseMove(view.viewport(), QPoint(118,0));
532 QTest::mouseMove(view.viewport(), QPoint(118,0));
532 TRY_COMPARE(seriesSpy.count(), 1);
533 TRY_COMPARE(seriesSpy.count(), 1);
533 TRY_COMPARE(setSpy1.count(), 0);
534 TRY_COMPARE(setSpy1.count(), 0);
534 TRY_COMPARE(setSpy2.count(), 1);
535 TRY_COMPARE(setSpy2.count(), 1);
535
536
536 // should leave set2
537 // should leave set2
537 seriesSpyArg = seriesSpy.takeFirst();
538 seriesSpyArg = seriesSpy.takeFirst();
538 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
539 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
539 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
540 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
540 QVERIFY(seriesSpyArg.at(0).toBool() == false);
541 QVERIFY(seriesSpyArg.at(0).toBool() == false);
541
542
542 setSpyArg = setSpy2.takeFirst();
543 setSpyArg = setSpy2.takeFirst();
543 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
544 QVERIFY(setSpyArg.at(0).type() == QVariant::Bool);
544 QVERIFY(setSpyArg.at(0).toBool() == false);
545 QVERIFY(setSpyArg.at(0).toBool() == false);
545 }
546 }
546
547
547 void tst_QBarSeries::clearWithAnimations()
548 void tst_QBarSeries::clearWithAnimations()
548 {
549 {
549 QBarSeries* series = new QBarSeries();
550 QBarSeries* series = new QBarSeries();
550
551
551 QBarSet* set1 = new QBarSet(QString("set 1"));
552 QBarSet* set1 = new QBarSet(QString("set 1"));
552 *set1 << 10 << 10 << 10;
553 *set1 << 10 << 10 << 10;
553 series->append(set1);
554 series->append(set1);
554
555
555 QBarSet* set2 = new QBarSet(QString("set 2"));
556 QBarSet* set2 = new QBarSet(QString("set 2"));
556 *set2 << 10 << 10 << 10;
557 *set2 << 10 << 10 << 10;
557 series->append(set2);
558 series->append(set2);
558
559
559 QChartView view(new QChart());
560 QChartView view(new QChart());
560 view.resize(400,300);
561 view.resize(400,300);
561 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
562 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
562 view.chart()->addSeries(series);
563 view.chart()->addSeries(series);
563 view.show();
564 view.show();
564
565
565 series->clear();
566 series->clear();
566 }
567 }
567
568
569 void tst_QBarSeries::destruction()
570 {
571 // add a barset
572 QBarSeries *series = new QBarSeries();
573 QBarSet *set = new QBarSet("testset");
574 QSignalSpy spy1(set, SIGNAL(destroyed()));
575 series->append(set);
576
577 // delete the series
578 delete series;
579
580 // check that series deletes the set
581 QCOMPARE(spy1.count(), 1);
582 }
583
568 QTEST_MAIN(tst_QBarSeries)
584 QTEST_MAIN(tst_QBarSeries)
569
585
570 #include "tst_qbarseries.moc"
586 #include "tst_qbarseries.moc"
571
587
@@ -1,573 +1,595
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qchartview.h>
22 #include <qchartview.h>
23 #include <qchart.h>
23 #include <qchart.h>
24 #include <qpieseries.h>
24 #include <qpieseries.h>
25 #include <qpieslice.h>
25 #include <qpieslice.h>
26 #include <qpiemodelmapper.h>
26 #include <qpiemodelmapper.h>
27 #include <QStandardItemModel>
27 #include <QStandardItemModel>
28 #include <tst_definitions.h>
28 #include <tst_definitions.h>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 Q_DECLARE_METATYPE(QPieSlice*)
32 Q_DECLARE_METATYPE(QPieSlice*)
33 Q_DECLARE_METATYPE(QList<QPieSlice*>)
33 Q_DECLARE_METATYPE(QList<QPieSlice*>)
34
34
35 class tst_qpieseries : public QObject
35 class tst_qpieseries : public QObject
36 {
36 {
37 Q_OBJECT
37 Q_OBJECT
38
38
39 public slots:
39 public slots:
40 void initTestCase();
40 void initTestCase();
41 void cleanupTestCase();
41 void cleanupTestCase();
42 void init();
42 void init();
43 void cleanup();
43 void cleanup();
44
44
45 private slots:
45 private slots:
46 void properties();
46 void properties();
47 void append();
47 void append();
48 void appendAnimated();
48 void appendAnimated();
49 void insert();
49 void insert();
50 void insertAnimated();
50 void insertAnimated();
51 void remove();
51 void remove();
52 void removeAnimated();
52 void removeAnimated();
53 void calculatedValues();
53 void calculatedValues();
54 void clickedSignal();
54 void clickedSignal();
55 void hoverSignal();
55 void hoverSignal();
56 void sliceSeries();
56 void sliceSeries();
57 void destruction();
57
58
58 private:
59 private:
59 void verifyCalculatedData(const QPieSeries &series, bool *ok);
60 void verifyCalculatedData(const QPieSeries &series, bool *ok);
60
61
61 private:
62 private:
62 QChartView *m_view;
63 QChartView *m_view;
63 QPieSeries *m_series;
64 QPieSeries *m_series;
64 };
65 };
65
66
66 void tst_qpieseries::initTestCase()
67 void tst_qpieseries::initTestCase()
67 {
68 {
68 qRegisterMetaType<QPieSlice*>("QPieSlice*");
69 qRegisterMetaType<QPieSlice*>("QPieSlice*");
69 qRegisterMetaType<QList<QPieSlice*> >("QList<QPieSlice*>");
70 qRegisterMetaType<QList<QPieSlice*> >("QList<QPieSlice*>");
70 }
71 }
71
72
72 void tst_qpieseries::cleanupTestCase()
73 void tst_qpieseries::cleanupTestCase()
73 {
74 {
74 }
75 }
75
76
76 void tst_qpieseries::init()
77 void tst_qpieseries::init()
77 {
78 {
78 m_view = new QChartView();
79 m_view = new QChartView();
79 m_series = new QPieSeries(m_view);
80 m_series = new QPieSeries(m_view);
80 }
81 }
81
82
82 void tst_qpieseries::cleanup()
83 void tst_qpieseries::cleanup()
83 {
84 {
84 delete m_view;
85 delete m_view;
85 m_view = 0;
86 m_view = 0;
86 m_series = 0;
87 m_series = 0;
87 }
88 }
88
89
89 void tst_qpieseries::properties()
90 void tst_qpieseries::properties()
90 {
91 {
91 QSignalSpy countSpy(m_series, SIGNAL(countChanged()));
92 QSignalSpy countSpy(m_series, SIGNAL(countChanged()));
92 QSignalSpy sumSpy(m_series, SIGNAL(sumChanged()));
93 QSignalSpy sumSpy(m_series, SIGNAL(sumChanged()));
93
94
94 QVERIFY(m_series->type() == QAbstractSeries::SeriesTypePie);
95 QVERIFY(m_series->type() == QAbstractSeries::SeriesTypePie);
95 QVERIFY(m_series->count() == 0);
96 QVERIFY(m_series->count() == 0);
96 QVERIFY(m_series->isEmpty());
97 QVERIFY(m_series->isEmpty());
97 QCOMPARE(m_series->sum(), 0.0);
98 QCOMPARE(m_series->sum(), 0.0);
98 QCOMPARE(m_series->horizontalPosition(), 0.5);
99 QCOMPARE(m_series->horizontalPosition(), 0.5);
99 QCOMPARE(m_series->verticalPosition(), 0.5);
100 QCOMPARE(m_series->verticalPosition(), 0.5);
100 QCOMPARE(m_series->pieSize(), 0.7);
101 QCOMPARE(m_series->pieSize(), 0.7);
101 QCOMPARE(m_series->pieStartAngle(), 0.0);
102 QCOMPARE(m_series->pieStartAngle(), 0.0);
102 QCOMPARE(m_series->pieEndAngle(), 360.0);
103 QCOMPARE(m_series->pieEndAngle(), 360.0);
103
104
104 m_series->append("s1", 1);
105 m_series->append("s1", 1);
105 m_series->append("s2", 1);
106 m_series->append("s2", 1);
106 m_series->append("s3", 1);
107 m_series->append("s3", 1);
107 m_series->insert(1, new QPieSlice("s4", 1));
108 m_series->insert(1, new QPieSlice("s4", 1));
108 m_series->remove(m_series->slices().first());
109 m_series->remove(m_series->slices().first());
109 QCOMPARE(m_series->count(), 3);
110 QCOMPARE(m_series->count(), 3);
110 QCOMPARE(m_series->sum(), 3.0);
111 QCOMPARE(m_series->sum(), 3.0);
111 m_series->clear();
112 m_series->clear();
112 QCOMPARE(m_series->count(), 0);
113 QCOMPARE(m_series->count(), 0);
113 QCOMPARE(m_series->sum(), 0.0);
114 QCOMPARE(m_series->sum(), 0.0);
114 QCOMPARE(countSpy.count(), 6);
115 QCOMPARE(countSpy.count(), 6);
115 QCOMPARE(sumSpy.count(), 6);
116 QCOMPARE(sumSpy.count(), 6);
116
117
117 m_series->setPieSize(-1.0);
118 m_series->setPieSize(-1.0);
118 QCOMPARE(m_series->pieSize(), 0.0);
119 QCOMPARE(m_series->pieSize(), 0.0);
119 m_series->setPieSize(0.0);
120 m_series->setPieSize(0.0);
120 m_series->setPieSize(0.9);
121 m_series->setPieSize(0.9);
121 m_series->setPieSize(2.0);
122 m_series->setPieSize(2.0);
122 QCOMPARE(m_series->pieSize(), 1.0);
123 QCOMPARE(m_series->pieSize(), 1.0);
123
124
124 m_series->setPieSize(0.7);
125 m_series->setPieSize(0.7);
125 QCOMPARE(m_series->pieSize(), 0.7);
126 QCOMPARE(m_series->pieSize(), 0.7);
126 m_series->setDonut();
127 m_series->setDonut();
127 QVERIFY(m_series->donut());
128 QVERIFY(m_series->donut());
128
129
129 m_series->setDonutInnerSize(-1.0);
130 m_series->setDonutInnerSize(-1.0);
130 QCOMPARE(m_series->donutInnerSize(), 0.0);
131 QCOMPARE(m_series->donutInnerSize(), 0.0);
131 m_series->setDonutInnerSize(0.5);
132 m_series->setDonutInnerSize(0.5);
132 QCOMPARE(m_series->donutInnerSize(), 0.5);
133 QCOMPARE(m_series->donutInnerSize(), 0.5);
133
134
134 m_series->setDonutInnerSize(0.8);
135 m_series->setDonutInnerSize(0.8);
135 QCOMPARE(m_series->donutInnerSize(), 0.8);
136 QCOMPARE(m_series->donutInnerSize(), 0.8);
136 QCOMPARE(m_series->pieSize(), 0.8);
137 QCOMPARE(m_series->pieSize(), 0.8);
137
138
138 m_series->setPieSize(0.4);
139 m_series->setPieSize(0.4);
139 QCOMPARE(m_series->pieSize(), 0.4);
140 QCOMPARE(m_series->pieSize(), 0.4);
140 QCOMPARE(m_series->donutInnerSize(), 0.4);
141 QCOMPARE(m_series->donutInnerSize(), 0.4);
141
142
142 m_series->setPieStartAngle(0);
143 m_series->setPieStartAngle(0);
143 m_series->setPieStartAngle(-180);
144 m_series->setPieStartAngle(-180);
144 m_series->setPieStartAngle(180);
145 m_series->setPieStartAngle(180);
145 QCOMPARE(m_series->pieStartAngle(), 180.0);
146 QCOMPARE(m_series->pieStartAngle(), 180.0);
146
147
147 m_series->setPieEndAngle(360);
148 m_series->setPieEndAngle(360);
148 m_series->setPieEndAngle(-180);
149 m_series->setPieEndAngle(-180);
149 m_series->setPieEndAngle(180);
150 m_series->setPieEndAngle(180);
150 QCOMPARE(m_series->pieEndAngle(), 180.0);
151 QCOMPARE(m_series->pieEndAngle(), 180.0);
151
152
152 m_series->setHorizontalPosition(0.5);
153 m_series->setHorizontalPosition(0.5);
153 m_series->setHorizontalPosition(-1.0);
154 m_series->setHorizontalPosition(-1.0);
154 QCOMPARE(m_series->horizontalPosition(), 0.0);
155 QCOMPARE(m_series->horizontalPosition(), 0.0);
155 m_series->setHorizontalPosition(1.0);
156 m_series->setHorizontalPosition(1.0);
156 m_series->setHorizontalPosition(2.0);
157 m_series->setHorizontalPosition(2.0);
157 QCOMPARE(m_series->horizontalPosition(), 1.0);
158 QCOMPARE(m_series->horizontalPosition(), 1.0);
158
159
159 m_series->setVerticalPosition(0.5);
160 m_series->setVerticalPosition(0.5);
160 m_series->setVerticalPosition(-1.0);
161 m_series->setVerticalPosition(-1.0);
161 QCOMPARE(m_series->verticalPosition(), 0.0);
162 QCOMPARE(m_series->verticalPosition(), 0.0);
162 m_series->setVerticalPosition(1.0);
163 m_series->setVerticalPosition(1.0);
163 m_series->setVerticalPosition(2.0);
164 m_series->setVerticalPosition(2.0);
164 QCOMPARE(m_series->verticalPosition(), 1.0);
165 QCOMPARE(m_series->verticalPosition(), 1.0);
165 }
166 }
166
167
167 void tst_qpieseries::append()
168 void tst_qpieseries::append()
168 {
169 {
169 m_view->chart()->addSeries(m_series);
170 m_view->chart()->addSeries(m_series);
170 QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>)));
171 QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>)));
171
172
172 // append pointer
173 // append pointer
173 QPieSlice *slice1 = 0;
174 QPieSlice *slice1 = 0;
174 QVERIFY(!m_series->append(slice1));
175 QVERIFY(!m_series->append(slice1));
175 slice1 = new QPieSlice("slice 1", 1);
176 slice1 = new QPieSlice("slice 1", 1);
176 QVERIFY(m_series->append(slice1));
177 QVERIFY(m_series->append(slice1));
177 QVERIFY(!m_series->append(slice1));
178 QVERIFY(!m_series->append(slice1));
178 QCOMPARE(m_series->count(), 1);
179 QCOMPARE(m_series->count(), 1);
179 QCOMPARE(addedSpy.count(), 1);
180 QCOMPARE(addedSpy.count(), 1);
180 QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0));
181 QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0));
181 QCOMPARE(added.count(), 1);
182 QCOMPARE(added.count(), 1);
182 QCOMPARE(added.first(), slice1);
183 QCOMPARE(added.first(), slice1);
183
184
184 // try to append same slice to another series
185 // try to append same slice to another series
185 QPieSeries series2;
186 QPieSeries series2;
186 QVERIFY(!series2.append(slice1));
187 QVERIFY(!series2.append(slice1));
187
188
188 // append pointer list
189 // append pointer list
189 QList<QPieSlice *> list;
190 QList<QPieSlice *> list;
190 QVERIFY(!m_series->append(list));
191 QVERIFY(!m_series->append(list));
191 list << (QPieSlice *) 0;
192 list << (QPieSlice *) 0;
192 QVERIFY(!m_series->append(list));
193 QVERIFY(!m_series->append(list));
193 list.clear();
194 list.clear();
194 list << new QPieSlice("slice 2", 2);
195 list << new QPieSlice("slice 2", 2);
195 list << new QPieSlice("slice 3", 3);
196 list << new QPieSlice("slice 3", 3);
196 QVERIFY(m_series->append(list));
197 QVERIFY(m_series->append(list));
197 QVERIFY(!m_series->append(list));
198 QVERIFY(!m_series->append(list));
198 QCOMPARE(m_series->count(), 3);
199 QCOMPARE(m_series->count(), 3);
199 QCOMPARE(addedSpy.count(), 2);
200 QCOMPARE(addedSpy.count(), 2);
200 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0));
201 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0));
201 QCOMPARE(added.count(), 2);
202 QCOMPARE(added.count(), 2);
202 QCOMPARE(added, list);
203 QCOMPARE(added, list);
203
204
204 // append operator
205 // append operator
205 QPieSlice *slice4 = new QPieSlice("slice 4", 4);
206 QPieSlice *slice4 = new QPieSlice("slice 4", 4);
206 *m_series << slice4;
207 *m_series << slice4;
207 *m_series << slice1; // fails because already added
208 *m_series << slice1; // fails because already added
208 QCOMPARE(m_series->count(), 4);
209 QCOMPARE(m_series->count(), 4);
209 QCOMPARE(addedSpy.count(), 3);
210 QCOMPARE(addedSpy.count(), 3);
210 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0));
211 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0));
211 QCOMPARE(added.count(), 1);
212 QCOMPARE(added.count(), 1);
212 QCOMPARE(added.first(), slice4);
213 QCOMPARE(added.first(), slice4);
213
214
214 // append with params
215 // append with params
215 QPieSlice *slice5 = m_series->append("slice 5", 5);
216 QPieSlice *slice5 = m_series->append("slice 5", 5);
216 QVERIFY(slice5 != 0);
217 QVERIFY(slice5 != 0);
217 QCOMPARE(slice5->value(), 5.0);
218 QCOMPARE(slice5->value(), 5.0);
218 QCOMPARE(slice5->label(), QString("slice 5"));
219 QCOMPARE(slice5->label(), QString("slice 5"));
219 QCOMPARE(m_series->count(), 5);
220 QCOMPARE(m_series->count(), 5);
220 QCOMPARE(addedSpy.count(), 4);
221 QCOMPARE(addedSpy.count(), 4);
221 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0));
222 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0));
222 QCOMPARE(added.count(), 1);
223 QCOMPARE(added.count(), 1);
223 QCOMPARE(added.first(), slice5);
224 QCOMPARE(added.first(), slice5);
224
225
225 // check slices
226 // check slices
226 QVERIFY(!m_series->isEmpty());
227 QVERIFY(!m_series->isEmpty());
227 for (int i=0; i<m_series->count(); i++) {
228 for (int i=0; i<m_series->count(); i++) {
228 QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1);
229 QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1);
229 QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1));
230 QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1));
230 }
231 }
231 }
232 }
232
233
233 void tst_qpieseries::appendAnimated()
234 void tst_qpieseries::appendAnimated()
234 {
235 {
235 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
236 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
236 }
237 }
237
238
238 void tst_qpieseries::insert()
239 void tst_qpieseries::insert()
239 {
240 {
240 m_view->chart()->addSeries(m_series);
241 m_view->chart()->addSeries(m_series);
241 QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>)));
242 QSignalSpy addedSpy(m_series, SIGNAL(added(QList<QPieSlice*>)));
242
243
243 // insert one slice
244 // insert one slice
244 QPieSlice *slice1 = 0;
245 QPieSlice *slice1 = 0;
245 QVERIFY(!m_series->insert(0, slice1));
246 QVERIFY(!m_series->insert(0, slice1));
246 slice1 = new QPieSlice("slice 1", 1);
247 slice1 = new QPieSlice("slice 1", 1);
247 QVERIFY(!m_series->insert(-1, slice1));
248 QVERIFY(!m_series->insert(-1, slice1));
248 QVERIFY(!m_series->insert(5, slice1));
249 QVERIFY(!m_series->insert(5, slice1));
249 QVERIFY(m_series->insert(0, slice1));
250 QVERIFY(m_series->insert(0, slice1));
250 QVERIFY(!m_series->insert(0, slice1));
251 QVERIFY(!m_series->insert(0, slice1));
251 QCOMPARE(m_series->count(), 1);
252 QCOMPARE(m_series->count(), 1);
252 QCOMPARE(addedSpy.count(), 1);
253 QCOMPARE(addedSpy.count(), 1);
253 QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0));
254 QList<QPieSlice*> added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(0).at(0));
254 QCOMPARE(added.count(), 1);
255 QCOMPARE(added.count(), 1);
255 QCOMPARE(added.first(), slice1);
256 QCOMPARE(added.first(), slice1);
256
257
257 // try to insert same slice to another series
258 // try to insert same slice to another series
258 QPieSeries series2;
259 QPieSeries series2;
259 QVERIFY(!series2.insert(0, slice1));
260 QVERIFY(!series2.insert(0, slice1));
260
261
261 // add some more slices
262 // add some more slices
262 QPieSlice *slice2 = m_series->append("slice 2", 2);
263 QPieSlice *slice2 = m_series->append("slice 2", 2);
263 QPieSlice *slice4 = m_series->append("slice 4", 4);
264 QPieSlice *slice4 = m_series->append("slice 4", 4);
264 QCOMPARE(m_series->count(), 3);
265 QCOMPARE(m_series->count(), 3);
265 QCOMPARE(addedSpy.count(), 3);
266 QCOMPARE(addedSpy.count(), 3);
266 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0));
267 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(1).at(0));
267 QCOMPARE(added.count(), 1);
268 QCOMPARE(added.count(), 1);
268 QCOMPARE(added.first(), slice2);
269 QCOMPARE(added.first(), slice2);
269 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0));
270 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(2).at(0));
270 QCOMPARE(added.count(), 1);
271 QCOMPARE(added.count(), 1);
271 QCOMPARE(added.first(), slice4);
272 QCOMPARE(added.first(), slice4);
272
273
273 // insert between slices
274 // insert between slices
274 QPieSlice *slice3 = new QPieSlice("slice 3", 3);
275 QPieSlice *slice3 = new QPieSlice("slice 3", 3);
275 m_series->insert(2, slice3);
276 m_series->insert(2, slice3);
276 QCOMPARE(m_series->count(), 4);
277 QCOMPARE(m_series->count(), 4);
277 QCOMPARE(addedSpy.count(), 4);
278 QCOMPARE(addedSpy.count(), 4);
278 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0));
279 added = qvariant_cast<QList<QPieSlice*> >(addedSpy.at(3).at(0));
279 QCOMPARE(added.count(), 1);
280 QCOMPARE(added.count(), 1);
280 QCOMPARE(added.first(), slice3);
281 QCOMPARE(added.first(), slice3);
281
282
282 // check slices
283 // check slices
283 for (int i=0; i<m_series->count(); i++) {
284 for (int i=0; i<m_series->count(); i++) {
284 QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1);
285 QCOMPARE(m_series->slices().at(i)->value(), (qreal) i+1);
285 QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1));
286 QCOMPARE(m_series->slices().at(i)->label(), QString("slice ") + QString::number(i+1));
287 QVERIFY(m_series->slices().at(i)->parent() == m_series);
286 }
288 }
287 }
289 }
288
290
289 void tst_qpieseries::insertAnimated()
291 void tst_qpieseries::insertAnimated()
290 {
292 {
291 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
293 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
292 }
294 }
293
295
294 void tst_qpieseries::remove()
296 void tst_qpieseries::remove()
295 {
297 {
296 m_view->chart()->addSeries(m_series);
298 m_view->chart()->addSeries(m_series);
297 QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>)));
299 QSignalSpy removedSpy(m_series, SIGNAL(removed(QList<QPieSlice*>)));
298
300
299 // add some slices
301 // add some slices
300 QPieSlice *slice1 = m_series->append("slice 1", 1);
302 QPieSlice *slice1 = m_series->append("slice 1", 1);
301 QPieSlice *slice2 = m_series->append("slice 2", 2);
303 QPieSlice *slice2 = m_series->append("slice 2", 2);
302 QPieSlice *slice3 = m_series->append("slice 3", 3);
304 QPieSlice *slice3 = m_series->append("slice 3", 3);
303 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
305 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
304 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
306 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
305 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
307 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
306 QCOMPARE(m_series->count(), 3);
308 QCOMPARE(m_series->count(), 3);
307
309
308 // null pointer remove
310 // null pointer remove
309 QVERIFY(!m_series->remove(0));
311 QVERIFY(!m_series->remove(0));
310
312
311 // remove first
313 // remove first
312 QVERIFY(m_series->remove(slice1));
314 QVERIFY(m_series->remove(slice1));
313 QVERIFY(!m_series->remove(slice1));
315 QVERIFY(!m_series->remove(slice1));
314 QCOMPARE(m_series->count(), 2);
316 QCOMPARE(m_series->count(), 2);
315 QCOMPARE(m_series->slices().at(0)->label(), slice2->label());
317 QCOMPARE(m_series->slices().at(0)->label(), slice2->label());
316 QCOMPARE(removedSpy.count(), 1);
318 QCOMPARE(removedSpy.count(), 1);
317 QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0));
319 QList<QPieSlice*> removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(0).at(0));
318 QCOMPARE(removed.count(), 1);
320 QCOMPARE(removed.count(), 1);
319 QCOMPARE(removed.first(), slice1);
321 QCOMPARE(removed.first(), slice1);
320
322
321 // remove all
323 // remove all
322 m_series->clear();
324 m_series->clear();
323 QVERIFY(m_series->isEmpty());
325 QVERIFY(m_series->isEmpty());
324 QVERIFY(m_series->slices().isEmpty());
326 QVERIFY(m_series->slices().isEmpty());
325 QCOMPARE(m_series->count(), 0);
327 QCOMPARE(m_series->count(), 0);
326 QCOMPARE(removedSpy.count(), 2);
328 QCOMPARE(removedSpy.count(), 2);
327 removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(1).at(0));
329 removed = qvariant_cast<QList<QPieSlice*> >(removedSpy.at(1).at(0));
328 QCOMPARE(removed.count(), 2);
330 QCOMPARE(removed.count(), 2);
329 QCOMPARE(removed.first(), slice2);
331 QCOMPARE(removed.first(), slice2);
330 QCOMPARE(removed.last(), slice3);
332 QCOMPARE(removed.last(), slice3);
331
333
332 // check that slices were actually destroyed
334 // check that slices were actually destroyed
333 TRY_COMPARE(spy1.count(), 1);
335 TRY_COMPARE(spy1.count(), 1);
334 TRY_COMPARE(spy2.count(), 1);
336 TRY_COMPARE(spy2.count(), 1);
335 TRY_COMPARE(spy3.count(), 1);
337 TRY_COMPARE(spy3.count(), 1);
336 }
338 }
337
339
338 void tst_qpieseries::removeAnimated()
340 void tst_qpieseries::removeAnimated()
339 {
341 {
340 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
342 m_view->chart()->setAnimationOptions(QChart::AllAnimations);
341 }
343 }
342
344
343 void tst_qpieseries::calculatedValues()
345 void tst_qpieseries::calculatedValues()
344 {
346 {
345 m_view->chart()->addSeries(m_series);
347 m_view->chart()->addSeries(m_series);
346
348
347 QPieSlice *slice1 = new QPieSlice("slice 1", 1);
349 QPieSlice *slice1 = new QPieSlice("slice 1", 1);
348 QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged()));
350 QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged()));
349 QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged()));
351 QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged()));
350 QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged()));
352 QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged()));
351
353
352 // add a slice
354 // add a slice
353 m_series->append(slice1);
355 m_series->append(slice1);
354 bool ok;
356 bool ok;
355 verifyCalculatedData(*m_series, &ok);
357 verifyCalculatedData(*m_series, &ok);
356 if (!ok)
358 if (!ok)
357 return;
359 return;
358 QCOMPARE(percentageSpy.count(), 1);
360 QCOMPARE(percentageSpy.count(), 1);
359 QCOMPARE(startAngleSpy.count(), 0);
361 QCOMPARE(startAngleSpy.count(), 0);
360 QCOMPARE(angleSpanSpy.count(), 1);
362 QCOMPARE(angleSpanSpy.count(), 1);
361
363
362 // add some more slices
364 // add some more slices
363 QList<QPieSlice *> list;
365 QList<QPieSlice *> list;
364 list << new QPieSlice("slice 2", 2);
366 list << new QPieSlice("slice 2", 2);
365 list << new QPieSlice("slice 3", 3);
367 list << new QPieSlice("slice 3", 3);
366 m_series->append(list);
368 m_series->append(list);
367 verifyCalculatedData(*m_series, &ok);
369 verifyCalculatedData(*m_series, &ok);
368 if (!ok)
370 if (!ok)
369 return;
371 return;
370 QCOMPARE(percentageSpy.count(), 2);
372 QCOMPARE(percentageSpy.count(), 2);
371 QCOMPARE(startAngleSpy.count(), 0);
373 QCOMPARE(startAngleSpy.count(), 0);
372 QCOMPARE(angleSpanSpy.count(), 2);
374 QCOMPARE(angleSpanSpy.count(), 2);
373
375
374 // remove a slice
376 // remove a slice
375 m_series->remove(list.first()); // remove slice 2
377 m_series->remove(list.first()); // remove slice 2
376 verifyCalculatedData(*m_series, &ok);
378 verifyCalculatedData(*m_series, &ok);
377 if (!ok)
379 if (!ok)
378 return;
380 return;
379 QCOMPARE(percentageSpy.count(), 3);
381 QCOMPARE(percentageSpy.count(), 3);
380 QCOMPARE(startAngleSpy.count(), 0);
382 QCOMPARE(startAngleSpy.count(), 0);
381 QCOMPARE(angleSpanSpy.count(), 3);
383 QCOMPARE(angleSpanSpy.count(), 3);
382
384
383 // insert a slice
385 // insert a slice
384 m_series->insert(0, new QPieSlice("Slice 4", 4));
386 m_series->insert(0, new QPieSlice("Slice 4", 4));
385 verifyCalculatedData(*m_series, &ok);
387 verifyCalculatedData(*m_series, &ok);
386 if (!ok)
388 if (!ok)
387 return;
389 return;
388 QCOMPARE(percentageSpy.count(), 4);
390 QCOMPARE(percentageSpy.count(), 4);
389 QCOMPARE(startAngleSpy.count(), 1);
391 QCOMPARE(startAngleSpy.count(), 1);
390 QCOMPARE(angleSpanSpy.count(), 4);
392 QCOMPARE(angleSpanSpy.count(), 4);
391
393
392 // modify pie angles
394 // modify pie angles
393 m_series->setPieStartAngle(-90);
395 m_series->setPieStartAngle(-90);
394 m_series->setPieEndAngle(90);
396 m_series->setPieEndAngle(90);
395 verifyCalculatedData(*m_series, &ok);
397 verifyCalculatedData(*m_series, &ok);
396 if (!ok)
398 if (!ok)
397 return;
399 return;
398 QCOMPARE(percentageSpy.count(), 4);
400 QCOMPARE(percentageSpy.count(), 4);
399 QCOMPARE(startAngleSpy.count(), 3);
401 QCOMPARE(startAngleSpy.count(), 3);
400 QCOMPARE(angleSpanSpy.count(), 6);
402 QCOMPARE(angleSpanSpy.count(), 6);
401
403
402 // clear all
404 // clear all
403 m_series->clear();
405 m_series->clear();
404 verifyCalculatedData(*m_series, &ok);
406 verifyCalculatedData(*m_series, &ok);
405 if (!ok)
407 if (!ok)
406 return;
408 return;
407 QCOMPARE(percentageSpy.count(), 4);
409 QCOMPARE(percentageSpy.count(), 4);
408 QCOMPARE(startAngleSpy.count(), 3);
410 QCOMPARE(startAngleSpy.count(), 3);
409 QCOMPARE(angleSpanSpy.count(), 6);
411 QCOMPARE(angleSpanSpy.count(), 6);
410 }
412 }
411
413
412 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
414 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
413 {
415 {
414 *ok = false;
416 *ok = false;
415
417
416 qreal sum = 0;
418 qreal sum = 0;
417 foreach (const QPieSlice *slice, series.slices())
419 foreach (const QPieSlice *slice, series.slices())
418 sum += slice->value();
420 sum += slice->value();
419 QCOMPARE(series.sum(), sum);
421 QCOMPARE(series.sum(), sum);
420
422
421 qreal startAngle = series.pieStartAngle();
423 qreal startAngle = series.pieStartAngle();
422 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
424 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
423 foreach (const QPieSlice *slice, series.slices()) {
425 foreach (const QPieSlice *slice, series.slices()) {
424 qreal ratio = slice->value() / sum;
426 qreal ratio = slice->value() / sum;
425 qreal sliceSpan = pieAngleSpan * ratio;
427 qreal sliceSpan = pieAngleSpan * ratio;
426 QCOMPARE(slice->startAngle(), startAngle);
428 QCOMPARE(slice->startAngle(), startAngle);
427 QCOMPARE(slice->angleSpan(), sliceSpan);
429 QCOMPARE(slice->angleSpan(), sliceSpan);
428 QCOMPARE(slice->percentage(), ratio);
430 QCOMPARE(slice->percentage(), ratio);
429 startAngle += sliceSpan;
431 startAngle += sliceSpan;
430 }
432 }
431
433
432 if (!series.isEmpty())
434 if (!series.isEmpty())
433 QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle());
435 QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle());
434
436
435 *ok = true;
437 *ok = true;
436 }
438 }
437
439
438
440
439 void tst_qpieseries::clickedSignal()
441 void tst_qpieseries::clickedSignal()
440 {
442 {
441 // add some slices
443 // add some slices
442 QPieSlice *s1 = m_series->append("slice 1", 1);
444 QPieSlice *s1 = m_series->append("slice 1", 1);
443 QPieSlice *s2 = m_series->append("slice 2", 1);
445 QPieSlice *s2 = m_series->append("slice 2", 1);
444 QPieSlice *s3 = m_series->append("slice 3", 1);
446 QPieSlice *s3 = m_series->append("slice 3", 1);
445 QPieSlice *s4 = m_series->append("slice 4", 1);
447 QPieSlice *s4 = m_series->append("slice 4", 1);
446 QSignalSpy clickSpy(m_series, SIGNAL(clicked(QPieSlice*)));
448 QSignalSpy clickSpy(m_series, SIGNAL(clicked(QPieSlice*)));
447
449
448 // add series to the chart
450 // add series to the chart
449 m_view->chart()->legend()->setVisible(false);
451 m_view->chart()->legend()->setVisible(false);
450 m_view->resize(200, 200);
452 m_view->resize(200, 200);
451 m_view->chart()->addSeries(m_series);
453 m_view->chart()->addSeries(m_series);
452 m_view->show();
454 m_view->show();
453 QTest::qWaitForWindowShown(m_view);
455 QTest::qWaitForWindowShown(m_view);
454
456
455 // if you devide the chart in four equal tiles these
457 // if you devide the chart in four equal tiles these
456 // are the center points of those tiles
458 // are the center points of those tiles
457 QPoint p1(90.25, 90);
459 QPoint p1(90.25, 90);
458 QPoint p2(150, 90);
460 QPoint p2(150, 90);
459 QPoint p3(90, 150);
461 QPoint p3(90, 150);
460 QPoint p4(150, 150);
462 QPoint p4(150, 150);
461
463
462 QPoint center(120, 120);
464 QPoint center(120, 120);
463
465
464 m_series->setPieSize(1.0);
466 m_series->setPieSize(1.0);
465 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
467 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
466 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
468 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
467 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
469 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
468 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
470 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
469 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
471 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
470 TRY_COMPARE(clickSpy.count(), 5); // all hit
472 TRY_COMPARE(clickSpy.count(), 5); // all hit
471 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s4);
473 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(0).at(0)), s4);
472 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s1);
474 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(1).at(0)), s1);
473 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
475 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(2).at(0)), s3);
474 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s2);
476 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy.at(3).at(0)), s2);
475 clickSpy.clear();
477 clickSpy.clear();
476
478
477 m_series->setPieSize(0.5);
479 m_series->setPieSize(0.5);
478 m_series->setVerticalPosition(0.25);
480 m_series->setVerticalPosition(0.25);
479 m_series->setHorizontalPosition(0.25);
481 m_series->setHorizontalPosition(0.25);
480 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); // hits
482 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1); // hits
481 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
483 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
482 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
484 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
483 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
485 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
484 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
486 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
485 TRY_COMPARE(clickSpy.count(), 1);
487 TRY_COMPARE(clickSpy.count(), 1);
486 clickSpy.clear();
488 clickSpy.clear();
487
489
488 m_series->setVerticalPosition(0.25);
490 m_series->setVerticalPosition(0.25);
489 m_series->setHorizontalPosition(0.75);
491 m_series->setHorizontalPosition(0.75);
490 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
492 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
491 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); // hits
493 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2); // hits
492 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
494 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
493 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
495 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
494 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
496 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
495 TRY_COMPARE(clickSpy.count(), 1);
497 TRY_COMPARE(clickSpy.count(), 1);
496 clickSpy.clear();
498 clickSpy.clear();
497
499
498 m_series->setVerticalPosition(0.75);
500 m_series->setVerticalPosition(0.75);
499 m_series->setHorizontalPosition(0.25);
501 m_series->setHorizontalPosition(0.25);
500 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
502 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
501 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
503 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
502 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); // hits
504 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3); // hits
503 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
505 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4);
504 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
506 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
505 TRY_COMPARE(clickSpy.count(), 1);
507 TRY_COMPARE(clickSpy.count(), 1);
506 clickSpy.clear();
508 clickSpy.clear();
507
509
508 m_series->setVerticalPosition(0.75);
510 m_series->setVerticalPosition(0.75);
509 m_series->setHorizontalPosition(0.75);
511 m_series->setHorizontalPosition(0.75);
510 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
512 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p1);
511 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
513 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p2);
512 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
514 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p3);
513 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); // hits
515 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, p4); // hits
514 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
516 QTest::mouseClick(m_view->viewport(), Qt::LeftButton, 0, center);
515 TRY_COMPARE(clickSpy.count(), 1);
517 TRY_COMPARE(clickSpy.count(), 1);
516 clickSpy.clear();
518 clickSpy.clear();
517 }
519 }
518
520
519 void tst_qpieseries::hoverSignal()
521 void tst_qpieseries::hoverSignal()
520 {
522 {
521 // add some slices
523 // add some slices
522 m_series->setPieSize(1.0);
524 m_series->setPieSize(1.0);
523 QPieSlice *s1 = m_series->append("slice 1", 1);
525 QPieSlice *s1 = m_series->append("slice 1", 1);
524 m_series->append("slice 2", 2);
526 m_series->append("slice 2", 2);
525 m_series->append("slice 3", 3);
527 m_series->append("slice 3", 3);
526
528
527 // add series to the chart
529 // add series to the chart
528 m_view->chart()->legend()->setVisible(false);
530 m_view->chart()->legend()->setVisible(false);
529 m_view->resize(200, 200);
531 m_view->resize(200, 200);
530 m_view->chart()->addSeries(m_series);
532 m_view->chart()->addSeries(m_series);
531 m_view->show();
533 m_view->show();
532 QTest::qWaitForWindowShown(m_view);
534 QTest::qWaitForWindowShown(m_view);
533
535
534 // first move to right top corner
536 // first move to right top corner
535 QTest::mouseMove(m_view->viewport(), QPoint(200, 0));
537 QTest::mouseMove(m_view->viewport(), QPoint(200, 0));
536 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
538 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
537
539
538 // move inside the slice
540 // move inside the slice
539 // pie rectangle: QRectF(60,60 121x121)
541 // pie rectangle: QRectF(60,60 121x121)
540 QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool)));
542 QSignalSpy hoverSpy(m_series, SIGNAL(hovered(QPieSlice*,bool)));
541 QTest::mouseMove(m_view->viewport(), QPoint(139, 85));
543 QTest::mouseMove(m_view->viewport(), QPoint(139, 85));
542 TRY_COMPARE(hoverSpy.count(), 1);
544 TRY_COMPARE(hoverSpy.count(), 1);
543 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
545 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
544 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
546 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
545
547
546 // move outside the slice
548 // move outside the slice
547 QTest::mouseMove(m_view->viewport(), QPoint(200, 0));
549 QTest::mouseMove(m_view->viewport(), QPoint(200, 0));
548 TRY_COMPARE(hoverSpy.count(), 2);
550 TRY_COMPARE(hoverSpy.count(), 2);
549 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
551 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
550 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
552 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
551 }
553 }
552
554
553 void tst_qpieseries::sliceSeries()
555 void tst_qpieseries::sliceSeries()
554 {
556 {
555 QPieSlice *slice = new QPieSlice();
557 QPieSlice *slice = new QPieSlice();
556 QVERIFY(!slice->series());
558 QVERIFY(!slice->series());
557 delete slice;
559 delete slice;
558
560
559 slice = new QPieSlice(m_series);
561 slice = new QPieSlice(m_series);
560 QVERIFY(!slice->series());
562 QVERIFY(!slice->series());
561
563
562 m_series->append(slice);
564 m_series->append(slice);
563 QCOMPARE(slice->series(), m_series);
565 QCOMPARE(slice->series(), m_series);
564
566
565 slice = new QPieSlice();
567 slice = new QPieSlice();
566 m_series->insert(0, slice);
568 m_series->insert(0, slice);
567 QCOMPARE(slice->series(), m_series);
569 QCOMPARE(slice->series(), m_series);
568 }
570 }
569
571
572 void tst_qpieseries::destruction()
573 {
574 // add some slices
575 QPieSlice *slice1 = m_series->append("slice 1", 1);
576 QPieSlice *slice2 = m_series->append("slice 2", 2);
577 QPieSlice *slice3 = m_series->append("slice 3", 3);
578 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
579 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
580 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
581
582 // destroy series
583 delete m_series;
584 m_series = 0;
585
586 // check that series has destroyed its slices
587 QCOMPARE(spy1.count(), 1);
588 QCOMPARE(spy2.count(), 1);
589 QCOMPARE(spy3.count(), 1);
590 }
591
570 QTEST_MAIN(tst_qpieseries)
592 QTEST_MAIN(tst_qpieseries)
571
593
572 #include "tst_qpieseries.moc"
594 #include "tst_qpieseries.moc"
573
595
General Comments 0
You need to be logged in to leave comments. Login now