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