@@ -1,221 +1,328 | |||||
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> | |||
|
23 | #include <qchart.h> | |||
22 | #include <qpieseries.h> |
|
24 | #include <qpieseries.h> | |
23 | #include <qpieslice.h> |
|
25 | #include <qpieslice.h> | |
24 | #include <tst_definitions.h> |
|
26 | #include <tst_definitions.h> | |
25 |
|
27 | |||
26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |
27 |
|
29 | |||
|
30 | Q_DECLARE_METATYPE(QPieSlice*) | |||
|
31 | ||||
28 | class tst_qpieseries : public QObject |
|
32 | class tst_qpieseries : public QObject | |
29 | { |
|
33 | { | |
30 | Q_OBJECT |
|
34 | Q_OBJECT | |
31 |
|
35 | |||
32 | public slots: |
|
36 | public slots: | |
33 | void initTestCase(); |
|
37 | void initTestCase(); | |
34 | void cleanupTestCase(); |
|
38 | void cleanupTestCase(); | |
35 | void init(); |
|
39 | void init(); | |
36 | void cleanup(); |
|
40 | void cleanup(); | |
37 |
|
41 | |||
38 | private slots: |
|
42 | private slots: | |
39 | void construction(); |
|
43 | void construction(); | |
40 | void append(); |
|
44 | void append(); | |
41 | void insert(); |
|
45 | void insert(); | |
42 | void remove(); |
|
46 | void remove(); | |
43 |
|
|
47 | void calculatedValues(); | |
44 | //void themes(); |
|
48 | void clickedSignal(); | |
45 |
|
|
49 | void hoverSignal(); | |
46 | //void hoverSignal(); |
|
|||
47 |
|
50 | |||
48 | private: |
|
51 | private: | |
49 |
|
52 | void verifyCalculatedData(const QPieSeries &series, bool *ok); | ||
50 |
|
53 | |||
51 | private: |
|
54 | private: | |
52 |
|
55 | |||
53 | }; |
|
56 | }; | |
54 |
|
57 | |||
55 | void tst_qpieseries::initTestCase() |
|
58 | void tst_qpieseries::initTestCase() | |
56 | { |
|
59 | { | |
|
60 | qRegisterMetaType<QPieSlice*>("QPieSlice*"); | |||
57 | } |
|
61 | } | |
58 |
|
62 | |||
59 | void tst_qpieseries::cleanupTestCase() |
|
63 | void tst_qpieseries::cleanupTestCase() | |
60 | { |
|
64 | { | |
61 | } |
|
65 | } | |
62 |
|
66 | |||
63 | void tst_qpieseries::init() |
|
67 | void tst_qpieseries::init() | |
64 | { |
|
68 | { | |
65 |
|
69 | |||
66 | } |
|
70 | } | |
67 |
|
71 | |||
68 | void tst_qpieseries::cleanup() |
|
72 | void tst_qpieseries::cleanup() | |
69 | { |
|
73 | { | |
70 |
|
74 | |||
71 | } |
|
75 | } | |
72 |
|
76 | |||
73 | void tst_qpieseries::construction() |
|
77 | void tst_qpieseries::construction() | |
74 | { |
|
78 | { | |
75 | // verify default values |
|
79 | // verify default values | |
76 | QPieSeries s; |
|
80 | QPieSeries s; | |
77 | QVERIFY(s.type() == QAbstractSeries::SeriesTypePie); |
|
81 | QVERIFY(s.type() == QAbstractSeries::SeriesTypePie); | |
78 | QVERIFY(s.count() == 0); |
|
82 | QVERIFY(s.count() == 0); | |
79 | QVERIFY(s.isEmpty()); |
|
83 | QVERIFY(s.isEmpty()); | |
80 | QVERIFY(qFuzzyIsNull(s.sum())); |
|
84 | QVERIFY(qFuzzyIsNull(s.sum())); | |
81 | QVERIFY(qFuzzyCompare(s.horizontalPosition(), 0.5)); |
|
85 | QVERIFY(qFuzzyCompare(s.horizontalPosition(), 0.5)); | |
82 | QVERIFY(qFuzzyCompare(s.verticalPosition(), 0.5)); |
|
86 | QVERIFY(qFuzzyCompare(s.verticalPosition(), 0.5)); | |
83 | QVERIFY(qFuzzyCompare(s.pieSize(), 0.7)); |
|
87 | QVERIFY(qFuzzyCompare(s.pieSize(), 0.7)); | |
84 | QVERIFY(qFuzzyIsNull(s.pieStartAngle())); |
|
88 | QVERIFY(qFuzzyIsNull(s.pieStartAngle())); | |
85 | QVERIFY(qFuzzyCompare(s.pieEndAngle(), 360)); |
|
89 | QVERIFY(qFuzzyCompare(s.pieEndAngle(), 360)); | |
86 | } |
|
90 | } | |
87 |
|
91 | |||
88 | void tst_qpieseries::append() |
|
92 | void tst_qpieseries::append() | |
89 | { |
|
93 | { | |
90 | QPieSeries s; |
|
94 | QPieSeries s; | |
91 |
|
95 | |||
92 | // append pointer |
|
96 | // append pointer | |
93 | QPieSlice *slice1 = 0; |
|
97 | QPieSlice *slice1 = 0; | |
94 | QVERIFY(!s.append(slice1)); |
|
98 | QVERIFY(!s.append(slice1)); | |
95 | slice1 = new QPieSlice(1, "slice 1"); |
|
99 | slice1 = new QPieSlice(1, "slice 1"); | |
96 | QVERIFY(s.append(slice1)); |
|
100 | QVERIFY(s.append(slice1)); | |
97 | QVERIFY(!s.append(slice1)); |
|
101 | QVERIFY(!s.append(slice1)); | |
98 | QCOMPARE(s.count(), 1); |
|
102 | QCOMPARE(s.count(), 1); | |
99 |
|
103 | |||
100 | // append pointer list |
|
104 | // append pointer list | |
101 | QList<QPieSlice *> list; |
|
105 | QList<QPieSlice *> list; | |
102 | QVERIFY(!s.append(list)); |
|
106 | QVERIFY(!s.append(list)); | |
103 | list << (QPieSlice *) 0; |
|
107 | list << (QPieSlice *) 0; | |
104 | QVERIFY(!s.append(list)); |
|
108 | QVERIFY(!s.append(list)); | |
105 | list.clear(); |
|
109 | list.clear(); | |
106 | list << new QPieSlice(2, "slice 2"); |
|
110 | list << new QPieSlice(2, "slice 2"); | |
107 | list << new QPieSlice(3, "slice 3"); |
|
111 | list << new QPieSlice(3, "slice 3"); | |
108 | QVERIFY(s.append(list)); |
|
112 | QVERIFY(s.append(list)); | |
109 | QVERIFY(!s.append(list)); |
|
113 | QVERIFY(!s.append(list)); | |
110 | QCOMPARE(s.count(), 3); |
|
114 | QCOMPARE(s.count(), 3); | |
111 |
|
115 | |||
112 | // append operator |
|
116 | // append operator | |
113 | s << new QPieSlice(4, "slice 4"); |
|
117 | s << new QPieSlice(4, "slice 4"); | |
114 | s << slice1; // fails because already added |
|
118 | s << slice1; // fails because already added | |
115 | QCOMPARE(s.count(), 4); |
|
119 | QCOMPARE(s.count(), 4); | |
116 |
|
120 | |||
117 | // append with params |
|
121 | // append with params | |
118 | QPieSlice *slice5 = s.append(5, "slice 5"); |
|
122 | QPieSlice *slice5 = s.append(5, "slice 5"); | |
119 | QVERIFY(slice5 != 0); |
|
123 | QVERIFY(slice5 != 0); | |
120 | QVERIFY(qFuzzyCompare(slice5->value(), 5.0)); |
|
124 | QVERIFY(qFuzzyCompare(slice5->value(), 5.0)); | |
121 | QCOMPARE(slice5->label(), QString("slice 5")); |
|
125 | QCOMPARE(slice5->label(), QString("slice 5")); | |
122 | QCOMPARE(s.count(), 5); |
|
126 | QCOMPARE(s.count(), 5); | |
123 |
|
127 | |||
124 | // check slices |
|
128 | // check slices | |
125 | QVERIFY(!s.isEmpty()); |
|
129 | QVERIFY(!s.isEmpty()); | |
126 | for (int i=0; i<s.count(); i++) { |
|
130 | for (int i=0; i<s.count(); i++) { | |
127 | QVERIFY(qFuzzyCompare(s.slices().at(i)->value(), (qreal) i+1)); |
|
131 | QVERIFY(qFuzzyCompare(s.slices().at(i)->value(), (qreal) i+1)); | |
128 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
132 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); | |
129 | } |
|
133 | } | |
130 | } |
|
134 | } | |
131 |
|
135 | |||
132 | void tst_qpieseries::insert() |
|
136 | void tst_qpieseries::insert() | |
133 | { |
|
137 | { | |
134 | QPieSeries s; |
|
138 | QPieSeries s; | |
135 |
|
139 | |||
136 | // insert one slice |
|
140 | // insert one slice | |
137 | QPieSlice *slice1 = 0; |
|
141 | QPieSlice *slice1 = 0; | |
138 | QVERIFY(!s.insert(0, slice1)); |
|
142 | QVERIFY(!s.insert(0, slice1)); | |
139 | slice1 = new QPieSlice(1, "slice 1"); |
|
143 | slice1 = new QPieSlice(1, "slice 1"); | |
140 | QVERIFY(!s.insert(-1, slice1)); |
|
144 | QVERIFY(!s.insert(-1, slice1)); | |
141 | QVERIFY(!s.insert(5, slice1)); |
|
145 | QVERIFY(!s.insert(5, slice1)); | |
142 | QVERIFY(s.insert(0, slice1)); |
|
146 | QVERIFY(s.insert(0, slice1)); | |
143 | QVERIFY(!s.insert(0, slice1)); |
|
147 | QVERIFY(!s.insert(0, slice1)); | |
144 | QCOMPARE(s.count(), 1); |
|
148 | QCOMPARE(s.count(), 1); | |
145 |
|
149 | |||
146 | // add some more slices |
|
150 | // add some more slices | |
147 | s.append(2, "slice 2"); |
|
151 | s.append(2, "slice 2"); | |
148 | s.append(4, "slice 4"); |
|
152 | s.append(4, "slice 4"); | |
149 | QCOMPARE(s.count(), 3); |
|
153 | QCOMPARE(s.count(), 3); | |
150 |
|
154 | |||
151 | // insert between slices |
|
155 | // insert between slices | |
152 | s.insert(2, new QPieSlice(3, "slice 3")); |
|
156 | s.insert(2, new QPieSlice(3, "slice 3")); | |
153 | QCOMPARE(s.count(), 4); |
|
157 | QCOMPARE(s.count(), 4); | |
154 |
|
158 | |||
155 | // check slices |
|
159 | // check slices | |
156 | for (int i=0; i<s.count(); i++) { |
|
160 | for (int i=0; i<s.count(); i++) { | |
157 | QVERIFY(qFuzzyCompare(s.slices().at(i)->value(), (qreal) i+1)); |
|
161 | QVERIFY(qFuzzyCompare(s.slices().at(i)->value(), (qreal) i+1)); | |
158 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); |
|
162 | QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1)); | |
159 | } |
|
163 | } | |
160 | } |
|
164 | } | |
161 |
|
165 | |||
162 | void tst_qpieseries::remove() |
|
166 | void tst_qpieseries::remove() | |
163 | { |
|
167 | { | |
164 | QPieSeries s; |
|
168 | QPieSeries s; | |
165 |
|
169 | |||
166 | // add some slices |
|
170 | // add some slices | |
167 | QPieSlice *slice1 = s.append(1, "slice 1"); |
|
171 | QPieSlice *slice1 = s.append(1, "slice 1"); | |
168 | QPieSlice *slice2 = s.append(2, "slice 2"); |
|
172 | QPieSlice *slice2 = s.append(2, "slice 2"); | |
169 | QPieSlice *slice3 = s.append(3, "slice 3"); |
|
173 | QPieSlice *slice3 = s.append(3, "slice 3"); | |
170 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); |
|
174 | QSignalSpy spy1(slice1, SIGNAL(destroyed())); | |
171 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); |
|
175 | QSignalSpy spy2(slice2, SIGNAL(destroyed())); | |
172 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); |
|
176 | QSignalSpy spy3(slice3, SIGNAL(destroyed())); | |
173 | QCOMPARE(s.count(), 3); |
|
177 | QCOMPARE(s.count(), 3); | |
174 |
|
178 | |||
175 | // null pointer remove |
|
179 | // null pointer remove | |
176 | QVERIFY(!s.remove(0)); |
|
180 | QVERIFY(!s.remove(0)); | |
177 |
|
181 | |||
178 | // remove first |
|
182 | // remove first | |
179 | QVERIFY(s.remove(slice1)); |
|
183 | QVERIFY(s.remove(slice1)); | |
180 | QVERIFY(!s.remove(slice1)); |
|
184 | QVERIFY(!s.remove(slice1)); | |
181 | QCOMPARE(s.count(), 2); |
|
185 | QCOMPARE(s.count(), 2); | |
182 | QCOMPARE(s.slices().at(0)->label(), slice2->label()); |
|
186 | QCOMPARE(s.slices().at(0)->label(), slice2->label()); | |
183 |
|
187 | |||
184 | // remove all |
|
188 | // remove all | |
185 | s.clear(); |
|
189 | s.clear(); | |
186 | QVERIFY(s.isEmpty()); |
|
190 | QVERIFY(s.isEmpty()); | |
187 | QVERIFY(s.slices().isEmpty()); |
|
191 | QVERIFY(s.slices().isEmpty()); | |
188 | QCOMPARE(s.count(), 0); |
|
192 | QCOMPARE(s.count(), 0); | |
189 |
|
193 | |||
190 | // check that slices were actually destroyed |
|
194 | // check that slices were actually destroyed | |
191 | TRY_COMPARE(spy1.count(), 1); |
|
195 | TRY_COMPARE(spy1.count(), 1); | |
192 | TRY_COMPARE(spy2.count(), 1); |
|
196 | TRY_COMPARE(spy2.count(), 1); | |
193 | TRY_COMPARE(spy3.count(), 1); |
|
197 | TRY_COMPARE(spy3.count(), 1); | |
194 | } |
|
198 | } | |
195 |
|
199 | |||
196 | /* |
|
|||
197 | void tst_qpieseries::calculatedValues() |
|
200 | void tst_qpieseries::calculatedValues() | |
198 | { |
|
201 | { | |
|
202 | bool ok; | |||
|
203 | QPieSeries s; | |||
|
204 | ||||
|
205 | // add a slice | |||
|
206 | QPieSlice *slice1 = s.append(1, "slice 1"); | |||
|
207 | verifyCalculatedData(s, &ok); | |||
|
208 | if (!ok) | |||
|
209 | return; | |||
199 |
|
210 | |||
|
211 | // add some more slices | |||
|
212 | QList<QPieSlice *> list; | |||
|
213 | list << new QPieSlice(2, "slice 2"); | |||
|
214 | list << new QPieSlice(3, "slice 3"); | |||
|
215 | s.append(list); | |||
|
216 | verifyCalculatedData(s, &ok); | |||
|
217 | if (!ok) | |||
|
218 | return; | |||
|
219 | ||||
|
220 | // remove a slice | |||
|
221 | s.remove(slice1); | |||
|
222 | verifyCalculatedData(s, &ok); | |||
|
223 | if (!ok) | |||
|
224 | return; | |||
|
225 | ||||
|
226 | // insert a slice | |||
|
227 | s.insert(0, new QPieSlice(1, "Slice 4")); | |||
|
228 | verifyCalculatedData(s, &ok); | |||
|
229 | if (!ok) | |||
|
230 | return; | |||
|
231 | ||||
|
232 | // clear all | |||
|
233 | s.clear(); | |||
|
234 | verifyCalculatedData(s, &ok); | |||
200 | } |
|
235 | } | |
201 |
|
236 | |||
202 | void tst_qpieseries::themes() |
|
237 | void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) | |
203 | { |
|
238 | { | |
|
239 | *ok = false; | |||
|
240 | ||||
|
241 | qreal sum = 0; | |||
|
242 | foreach (const QPieSlice *slice, series.slices()) | |||
|
243 | sum += slice->value(); | |||
|
244 | QCOMPARE(series.sum(), sum); | |||
|
245 | ||||
|
246 | qreal startAngle = series.pieStartAngle(); | |||
|
247 | qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle(); | |||
|
248 | foreach (const QPieSlice *slice, series.slices()) { | |||
|
249 | qreal ratio = slice->value() / sum; | |||
|
250 | qreal sliceSpan = pieAngleSpan * ratio; | |||
|
251 | QCOMPARE(slice->startAngle(), startAngle); | |||
|
252 | QCOMPARE(slice->endAngle(), startAngle + sliceSpan); | |||
|
253 | QCOMPARE(slice->percentage(), ratio); | |||
|
254 | startAngle += sliceSpan; | |||
|
255 | } | |||
|
256 | ||||
|
257 | if (!series.isEmpty()) | |||
|
258 | QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle()); | |||
204 |
|
259 | |||
|
260 | *ok = true; | |||
205 | } |
|
261 | } | |
206 |
|
262 | |||
|
263 | ||||
207 | void tst_qpieseries::clickedSignal() |
|
264 | void tst_qpieseries::clickedSignal() | |
208 | { |
|
265 | { | |
209 |
|
266 | // create a pie series | ||
|
267 | QPieSeries *series = new QPieSeries(); | |||
|
268 | series->setPieSize(1.0); | |||
|
269 | QPieSlice *s1 = series->append(1, "slice 1"); | |||
|
270 | series->append(2, "slice 2"); | |||
|
271 | series->append(3, "slice 3"); | |||
|
272 | QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*))); | |||
|
273 | ||||
|
274 | // add series to the chart | |||
|
275 | QChartView view(new QChart()); | |||
|
276 | view.resize(200, 200); | |||
|
277 | view.chart()->addSeries(series); | |||
|
278 | view.show(); | |||
|
279 | QTest::qWaitForWindowShown(&view); | |||
|
280 | ||||
|
281 | // simulate clicks | |||
|
282 | // pie rectangle: QRectF(60,60 121x121) | |||
|
283 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 | |||
|
284 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
285 | QCOMPARE(clickSpy1.count(), 1); | |||
|
286 | QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1); | |||
210 | } |
|
287 | } | |
211 |
|
288 | |||
212 | void tst_qpieseries::hoverSignal() |
|
289 | void tst_qpieseries::hoverSignal() | |
213 | { |
|
290 | { | |
214 |
|
291 | // create a pie series | ||
|
292 | QPieSeries *series = new QPieSeries(); | |||
|
293 | series->setPieSize(1.0); | |||
|
294 | QPieSlice *s1 = series->append(1, "slice 1"); | |||
|
295 | series->append(2, "slice 2"); | |||
|
296 | series->append(3, "slice 3"); | |||
|
297 | ||||
|
298 | // add series to the chart | |||
|
299 | QChartView view(new QChart()); | |||
|
300 | view.resize(200, 200); | |||
|
301 | view.chart()->addSeries(series); | |||
|
302 | view.show(); | |||
|
303 | QTest::qWaitForWindowShown(&view); | |||
|
304 | ||||
|
305 | // first move to right top corner | |||
|
306 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |||
|
307 | ||||
|
308 | // move inside the slice | |||
|
309 | // pie rectangle: QRectF(60,60 121x121) | |||
|
310 | QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool))); | |||
|
311 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); | |||
|
312 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
313 | QCOMPARE(hoverSpy.count(), 1); | |||
|
314 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1); | |||
|
315 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true); | |||
|
316 | ||||
|
317 | // move outside the slice | |||
|
318 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |||
|
319 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
320 | QCOMPARE(hoverSpy.count(), 2); | |||
|
321 | QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1); | |||
|
322 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false); | |||
215 | } |
|
323 | } | |
216 | */ |
|
|||
217 |
|
324 | |||
218 | QTEST_MAIN(tst_qpieseries) |
|
325 | QTEST_MAIN(tst_qpieseries) | |
219 |
|
326 | |||
220 | #include "tst_qpieseries.moc" |
|
327 | #include "tst_qpieseries.moc" | |
221 |
|
328 |
@@ -1,132 +1,270 | |||||
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 <qpieslice.h> |
|
|||
23 | #include <tst_definitions.h> |
|
22 | #include <tst_definitions.h> | |
|
23 | #include <qchartview.h> | |||
|
24 | #include <qchart.h> | |||
|
25 | #include <qpieslice.h> | |||
|
26 | #include <qpieseries.h> | |||
24 |
|
27 | |||
25 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |
26 |
|
29 | |||
27 | class tst_qpieslice : public QObject |
|
30 | class tst_qpieslice : public QObject | |
28 | { |
|
31 | { | |
29 | Q_OBJECT |
|
32 | Q_OBJECT | |
30 |
|
33 | |||
31 | public slots: |
|
34 | public slots: | |
32 | void initTestCase(); |
|
35 | void initTestCase(); | |
33 | void cleanupTestCase(); |
|
36 | void cleanupTestCase(); | |
34 | void init(); |
|
37 | void init(); | |
35 | void cleanup(); |
|
38 | void cleanup(); | |
36 |
|
39 | |||
37 | private slots: |
|
40 | private slots: | |
38 | void construction(); |
|
41 | void construction(); | |
39 | void changedSignals(); |
|
42 | void changedSignals(); | |
|
43 | void customize(); | |||
|
44 | void mouseClick(); | |||
|
45 | void mouseHover(); | |||
40 |
|
46 | |||
41 | private: |
|
47 | private: | |
42 |
|
48 | |||
43 |
|
49 | |||
44 | private: |
|
50 | private: | |
45 |
|
51 | |||
46 | }; |
|
52 | }; | |
47 |
|
53 | |||
48 | void tst_qpieslice::initTestCase() |
|
54 | void tst_qpieslice::initTestCase() | |
49 | { |
|
55 | { | |
50 | } |
|
56 | } | |
51 |
|
57 | |||
52 | void tst_qpieslice::cleanupTestCase() |
|
58 | void tst_qpieslice::cleanupTestCase() | |
53 | { |
|
59 | { | |
54 | } |
|
60 | } | |
55 |
|
61 | |||
56 | void tst_qpieslice::init() |
|
62 | void tst_qpieslice::init() | |
57 | { |
|
63 | { | |
58 |
|
64 | |||
59 | } |
|
65 | } | |
60 |
|
66 | |||
61 | void tst_qpieslice::cleanup() |
|
67 | void tst_qpieslice::cleanup() | |
62 | { |
|
68 | { | |
63 |
|
69 | |||
64 | } |
|
70 | } | |
65 |
|
71 | |||
66 | void tst_qpieslice::construction() |
|
72 | void tst_qpieslice::construction() | |
67 | { |
|
73 | { | |
68 | // no params |
|
74 | // no params | |
69 | QPieSlice slice1; |
|
75 | QPieSlice slice1; | |
70 | QVERIFY(qFuzzyIsNull(slice1.value())); |
|
76 | QVERIFY(qFuzzyIsNull(slice1.value())); | |
71 | QVERIFY(slice1.label().isEmpty()); |
|
77 | QVERIFY(slice1.label().isEmpty()); | |
72 | QVERIFY(!slice1.isLabelVisible()); |
|
78 | QVERIFY(!slice1.isLabelVisible()); | |
73 | QVERIFY(!slice1.isExploded()); |
|
79 | QVERIFY(!slice1.isExploded()); | |
74 | QCOMPARE(slice1.pen(), QPen()); |
|
80 | QCOMPARE(slice1.pen(), QPen()); | |
75 | QCOMPARE(slice1.brush(), QBrush()); |
|
81 | QCOMPARE(slice1.brush(), QBrush()); | |
76 | QCOMPARE(slice1.labelPen(), QPen()); |
|
82 | QCOMPARE(slice1.labelPen(), QPen()); | |
77 | QCOMPARE(slice1.labelFont(), QFont()); |
|
83 | QCOMPARE(slice1.labelFont(), QFont()); | |
78 | QVERIFY(qFuzzyCompare(slice1.labelArmLengthFactor(), 0.15)); // default value |
|
84 | QVERIFY(qFuzzyCompare(slice1.labelArmLengthFactor(), 0.15)); // default value | |
79 | QVERIFY(qFuzzyCompare(slice1.explodeDistanceFactor(), 0.15)); // default value |
|
85 | QVERIFY(qFuzzyCompare(slice1.explodeDistanceFactor(), 0.15)); // default value | |
80 | QVERIFY(qFuzzyIsNull(slice1.percentage())); |
|
86 | QVERIFY(qFuzzyIsNull(slice1.percentage())); | |
81 | QVERIFY(qFuzzyIsNull(slice1.startAngle())); |
|
87 | QVERIFY(qFuzzyIsNull(slice1.startAngle())); | |
82 | QVERIFY(qFuzzyIsNull(slice1.endAngle())); |
|
88 | QVERIFY(qFuzzyIsNull(slice1.endAngle())); | |
83 |
|
89 | |||
84 | // value and label params |
|
90 | // value and label params | |
85 | QPieSlice slice2(1.0, "foobar"); |
|
91 | QPieSlice slice2(1.0, "foobar"); | |
86 | QVERIFY(qFuzzyCompare(slice2.value(), 1.0)); |
|
92 | QVERIFY(qFuzzyCompare(slice2.value(), 1.0)); | |
87 | QCOMPARE(slice2.label(), QString("foobar")); |
|
93 | QCOMPARE(slice2.label(), QString("foobar")); | |
88 | QVERIFY(!slice2.isLabelVisible()); |
|
94 | QVERIFY(!slice2.isLabelVisible()); | |
89 | QVERIFY(!slice2.isExploded()); |
|
95 | QVERIFY(!slice2.isExploded()); | |
90 | QCOMPARE(slice2.pen(), QPen()); |
|
96 | QCOMPARE(slice2.pen(), QPen()); | |
91 | QCOMPARE(slice2.brush(), QBrush()); |
|
97 | QCOMPARE(slice2.brush(), QBrush()); | |
92 | QCOMPARE(slice2.labelPen(), QPen()); |
|
98 | QCOMPARE(slice2.labelPen(), QPen()); | |
93 | QCOMPARE(slice2.labelFont(), QFont()); |
|
99 | QCOMPARE(slice2.labelFont(), QFont()); | |
94 | QVERIFY(qFuzzyCompare(slice2.labelArmLengthFactor(), 0.15)); // default value |
|
100 | QVERIFY(qFuzzyCompare(slice2.labelArmLengthFactor(), 0.15)); // default value | |
95 | QVERIFY(qFuzzyCompare(slice2.explodeDistanceFactor(), 0.15)); // default value |
|
101 | QVERIFY(qFuzzyCompare(slice2.explodeDistanceFactor(), 0.15)); // default value | |
96 | QVERIFY(qFuzzyIsNull(slice2.percentage())); |
|
102 | QVERIFY(qFuzzyIsNull(slice2.percentage())); | |
97 | QVERIFY(qFuzzyIsNull(slice2.startAngle())); |
|
103 | QVERIFY(qFuzzyIsNull(slice2.startAngle())); | |
98 | QVERIFY(qFuzzyIsNull(slice2.endAngle())); |
|
104 | QVERIFY(qFuzzyIsNull(slice2.endAngle())); | |
99 | } |
|
105 | } | |
100 |
|
106 | |||
101 | void tst_qpieslice::changedSignals() |
|
107 | void tst_qpieslice::changedSignals() | |
102 | { |
|
108 | { | |
103 | // set everything twice to see we do not get unnecessary signals |
|
109 | // set everything twice to see we do not get unnecessary signals | |
104 | QPieSlice slice; |
|
110 | QPieSlice slice; | |
105 | QSignalSpy spy(&slice, SIGNAL(changed())); // TODO: this will be changed to something more refined |
|
111 | QSignalSpy spy(&slice, SIGNAL(changed())); // TODO: this will be changed to something more refined | |
106 | slice.setValue(1); |
|
112 | slice.setValue(1); | |
107 | slice.setValue(1); |
|
113 | slice.setValue(1); | |
108 | slice.setLabel("foobar"); |
|
114 | slice.setLabel("foobar"); | |
109 | slice.setLabel("foobar"); |
|
115 | slice.setLabel("foobar"); | |
110 | slice.setLabelVisible(); |
|
116 | slice.setLabelVisible(); | |
111 | slice.setLabelVisible(); |
|
117 | slice.setLabelVisible(); | |
112 | slice.setExploded(); |
|
118 | slice.setExploded(); | |
113 | slice.setExploded(); |
|
119 | slice.setExploded(); | |
114 | slice.setPen(QPen(Qt::red)); |
|
120 | slice.setPen(QPen(Qt::red)); | |
115 | slice.setPen(QPen(Qt::red)); |
|
121 | slice.setPen(QPen(Qt::red)); | |
116 | slice.setBrush(QBrush(Qt::red)); |
|
122 | slice.setBrush(QBrush(Qt::red)); | |
117 | slice.setBrush(QBrush(Qt::red)); |
|
123 | slice.setBrush(QBrush(Qt::red)); | |
118 | slice.setLabelPen(QPen(Qt::green)); |
|
124 | slice.setLabelPen(QPen(Qt::green)); | |
119 | slice.setLabelPen(QPen(Qt::green)); |
|
125 | slice.setLabelPen(QPen(Qt::green)); | |
120 | slice.setLabelFont(QFont("Tahoma")); |
|
126 | slice.setLabelFont(QFont("Tahoma")); | |
121 | slice.setLabelFont(QFont("Tahoma")); |
|
127 | slice.setLabelFont(QFont("Tahoma")); | |
122 | slice.setLabelArmLengthFactor(0.1); |
|
128 | slice.setLabelArmLengthFactor(0.1); | |
123 | slice.setLabelArmLengthFactor(0.1); |
|
129 | slice.setLabelArmLengthFactor(0.1); | |
124 | slice.setExplodeDistanceFactor(0.1); |
|
130 | slice.setExplodeDistanceFactor(0.1); | |
125 | slice.setExplodeDistanceFactor(0.1); |
|
131 | slice.setExplodeDistanceFactor(0.1); | |
126 | TRY_COMPARE(spy.count(), 10); |
|
132 | TRY_COMPARE(spy.count(), 10); | |
127 | } |
|
133 | } | |
128 |
|
134 | |||
|
135 | void tst_qpieslice::customize() | |||
|
136 | { | |||
|
137 | // create a pie series | |||
|
138 | QPieSeries *series = new QPieSeries(); | |||
|
139 | QPieSlice *s1 = series->append(1, "slice 1"); | |||
|
140 | QPieSlice *s2 = series->append(2, "slice 2"); | |||
|
141 | series->append(3, "slice 3"); | |||
|
142 | ||||
|
143 | // customize a slice | |||
|
144 | QPen p1(Qt::red); | |||
|
145 | s1->setPen(p1); | |||
|
146 | QBrush b1(Qt::red); | |||
|
147 | s1->setBrush(b1); | |||
|
148 | s1->setLabelPen(p1); | |||
|
149 | QFont f1("Consolas"); | |||
|
150 | s1->setLabelFont(f1); | |||
|
151 | ||||
|
152 | // add series to the chart | |||
|
153 | QChartView view(new QChart()); | |||
|
154 | view.resize(200, 200); | |||
|
155 | view.chart()->addSeries(series); | |||
|
156 | view.show(); | |||
|
157 | QTest::qWaitForWindowShown(&view); | |||
|
158 | //QTest::qWait(1000); | |||
|
159 | ||||
|
160 | // check that customizations persist | |||
|
161 | QCOMPARE(s1->pen(), p1); | |||
|
162 | QCOMPARE(s1->brush(), b1); | |||
|
163 | QCOMPARE(s1->labelPen(), p1); | |||
|
164 | QCOMPARE(s1->labelFont(), f1); | |||
|
165 | ||||
|
166 | // remove a slice | |||
|
167 | series->remove(s2); | |||
|
168 | QCOMPARE(s1->pen(), p1); | |||
|
169 | QCOMPARE(s1->brush(), b1); | |||
|
170 | QCOMPARE(s1->labelPen(), p1); | |||
|
171 | QCOMPARE(s1->labelFont(), f1); | |||
|
172 | ||||
|
173 | // add a slice | |||
|
174 | series->append(4, "slice 4"); | |||
|
175 | QCOMPARE(s1->pen(), p1); | |||
|
176 | QCOMPARE(s1->brush(), b1); | |||
|
177 | QCOMPARE(s1->labelPen(), p1); | |||
|
178 | QCOMPARE(s1->labelFont(), f1); | |||
|
179 | ||||
|
180 | // insert a slice | |||
|
181 | series->insert(0, new QPieSlice(5, "slice 5")); | |||
|
182 | QCOMPARE(s1->pen(), p1); | |||
|
183 | QCOMPARE(s1->brush(), b1); | |||
|
184 | QCOMPARE(s1->labelPen(), p1); | |||
|
185 | QCOMPARE(s1->labelFont(), f1); | |||
|
186 | ||||
|
187 | // change theme | |||
|
188 | // theme will overwrite customizations | |||
|
189 | view.chart()->setTheme(QChart::ChartThemeHighContrast); | |||
|
190 | QVERIFY(s1->pen() != p1); | |||
|
191 | QVERIFY(s1->brush() != b1); | |||
|
192 | QVERIFY(s1->labelPen() != p1); | |||
|
193 | QVERIFY(s1->labelFont() != f1); | |||
|
194 | } | |||
|
195 | ||||
|
196 | void tst_qpieslice::mouseClick() | |||
|
197 | { | |||
|
198 | // create a pie series | |||
|
199 | QPieSeries *series = new QPieSeries(); | |||
|
200 | series->setPieSize(1.0); | |||
|
201 | QPieSlice *s1 = series->append(1, "slice 1"); | |||
|
202 | QPieSlice *s2 = series->append(2, "slice 2"); | |||
|
203 | QPieSlice *s3 = series->append(3, "slice 3"); | |||
|
204 | QSignalSpy clickSpy1(s1, SIGNAL(clicked())); | |||
|
205 | QSignalSpy clickSpy2(s2, SIGNAL(clicked())); | |||
|
206 | QSignalSpy clickSpy3(s3, SIGNAL(clicked())); | |||
|
207 | ||||
|
208 | // add series to the chart | |||
|
209 | QChartView view(new QChart()); | |||
|
210 | view.resize(200, 200); | |||
|
211 | view.chart()->addSeries(series); | |||
|
212 | view.show(); | |||
|
213 | QTest::qWaitForWindowShown(&view); | |||
|
214 | ||||
|
215 | // simulate clicks | |||
|
216 | // pie rectangle: QRectF(60,60 121x121) | |||
|
217 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1 | |||
|
218 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2 | |||
|
219 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3 | |||
|
220 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice | |||
|
221 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice | |||
|
222 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
223 | QCOMPARE(clickSpy1.count(), 1); | |||
|
224 | QCOMPARE(clickSpy2.count(), 1); | |||
|
225 | QCOMPARE(clickSpy3.count(), 1); | |||
|
226 | } | |||
|
227 | ||||
|
228 | void tst_qpieslice::mouseHover() | |||
|
229 | { | |||
|
230 | // create a pie series | |||
|
231 | QPieSeries *series = new QPieSeries(); | |||
|
232 | series->setPieSize(1.0); | |||
|
233 | QPieSlice *s1 = series->append(1, "slice 1"); | |||
|
234 | series->append(2, "slice 2"); | |||
|
235 | series->append(3, "slice 3"); | |||
|
236 | ||||
|
237 | // add series to the chart | |||
|
238 | QChartView view(new QChart()); | |||
|
239 | view.resize(200, 200); | |||
|
240 | view.chart()->addSeries(series); | |||
|
241 | view.show(); | |||
|
242 | QTest::qWaitForWindowShown(&view); | |||
|
243 | ||||
|
244 | // first move to right top corner | |||
|
245 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |||
|
246 | ||||
|
247 | // move inside slice rectangle but NOT the actual slice | |||
|
248 | // pie rectangle: QRectF(60,60 121x121) | |||
|
249 | QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool))); | |||
|
250 | QTest::mouseMove(view.viewport(), QPoint(170, 70)); | |||
|
251 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
252 | QCOMPARE(hoverSpy.count(), 0); | |||
|
253 | ||||
|
254 | // move inside the slice | |||
|
255 | QTest::mouseMove(view.viewport(), QPoint(139, 85)); | |||
|
256 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
257 | QCOMPARE(hoverSpy.count(), 1); | |||
|
258 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true); | |||
|
259 | ||||
|
260 | // move outside the slice | |||
|
261 | QTest::mouseMove(view.viewport(), QPoint(200, 0)); | |||
|
262 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); | |||
|
263 | QCOMPARE(hoverSpy.count(), 2); | |||
|
264 | QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false); | |||
|
265 | } | |||
|
266 | ||||
129 | QTEST_MAIN(tst_qpieslice) |
|
267 | QTEST_MAIN(tst_qpieslice) | |
130 |
|
268 | |||
131 | #include "tst_qpieslice.moc" |
|
269 | #include "tst_qpieslice.moc" | |
132 |
|
270 |
General Comments 0
You need to be logged in to leave comments.
Login now