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