1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include "../qxyseries/tst_qxyseries.h" |
31 | #include <QtCharts/QScatterSeries> |
32 | |
33 | Q_DECLARE_METATYPE(QList<QPointF>) |
34 | Q_DECLARE_METATYPE(QVector<QPointF>) |
35 | |
36 | class tst_QScatterSeries : public tst_QXYSeries |
37 | { |
38 | Q_OBJECT |
39 | |
40 | public slots: |
41 | void initTestCase(); |
42 | void cleanupTestCase(); |
43 | void init(); |
44 | void cleanup(); |
45 | private slots: |
46 | void qscatterseries_data(); |
47 | void qscatterseries(); |
48 | void scatterChangedSignals(); |
49 | void pressedSignal(); |
50 | void releasedSignal(); |
51 | void doubleClickedSignal(); |
52 | |
53 | protected: |
54 | void pointsVisible_data(); |
55 | }; |
56 | |
57 | void tst_QScatterSeries::initTestCase() |
58 | { |
59 | } |
60 | |
61 | void tst_QScatterSeries::cleanupTestCase() |
62 | { |
63 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
64 | } |
65 | |
66 | void tst_QScatterSeries::init() |
67 | { |
68 | tst_QXYSeries::init(); |
69 | m_series = new QScatterSeries(); |
70 | } |
71 | |
72 | void tst_QScatterSeries::cleanup() |
73 | { |
74 | delete m_series; |
75 | tst_QXYSeries::cleanup(); |
76 | } |
77 | |
78 | void tst_QScatterSeries::qscatterseries_data() |
79 | { |
80 | |
81 | } |
82 | |
83 | void tst_QScatterSeries::qscatterseries() |
84 | { |
85 | QScatterSeries series; |
86 | |
87 | QCOMPARE(series.count(),0); |
88 | QCOMPARE(series.brush(), QBrush()); |
89 | QCOMPARE(series.points(), QList<QPointF>()); |
90 | QCOMPARE(series.pointsVector(), QVector<QPointF>()); |
91 | QCOMPARE(series.pen(), QPen()); |
92 | QCOMPARE(series.pointsVisible(), false); |
93 | |
94 | series.append(points: QList<QPointF>()); |
95 | series.append(x: 0.0,y: 0.0); |
96 | series.append(point: QPointF()); |
97 | |
98 | series.remove(x: 0.0,y: 0.0); |
99 | series.remove(point: QPointF()); |
100 | series.clear(); |
101 | |
102 | series.replace(oldPoint: QPointF(),newPoint: QPointF()); |
103 | series.replace(oldX: 0,oldY: 0,newX: 0,newY: 0); |
104 | series.setBrush(QBrush()); |
105 | |
106 | series.setPen(QPen()); |
107 | series.setPointsVisible(false); |
108 | |
109 | m_chart->addSeries(series: &series); |
110 | m_view->show(); |
111 | QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
112 | } |
113 | |
114 | void tst_QScatterSeries::scatterChangedSignals() |
115 | { |
116 | QScatterSeries *series = qobject_cast<QScatterSeries *>(object: m_series); |
117 | QVERIFY(series); |
118 | |
119 | QSignalSpy colorSpy(series, SIGNAL(colorChanged(QColor))); |
120 | QSignalSpy borderColorSpy(series, SIGNAL(borderColorChanged(QColor))); |
121 | |
122 | // Color |
123 | series->setColor(QColor("blueviolet" )); |
124 | TRY_COMPARE(colorSpy.count(), 1); |
125 | |
126 | // Border color |
127 | series->setBorderColor(QColor("burlywood" )); |
128 | TRY_COMPARE(borderColorSpy.count(), 1); |
129 | |
130 | // Pen |
131 | QPen p = series->pen(); |
132 | p.setColor("lightpink" ); |
133 | series->setPen(p); |
134 | TRY_COMPARE(borderColorSpy.count(), 2); |
135 | |
136 | // Brush |
137 | QBrush b = series->brush(); |
138 | b.setColor("lime" ); |
139 | series->setBrush(b); |
140 | TRY_COMPARE(colorSpy.count(), 2); |
141 | } |
142 | |
143 | void tst_QScatterSeries::pressedSignal() |
144 | { |
145 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
146 | |
147 | QPointF scatterPoint(4, 12); |
148 | QScatterSeries *scatterSeries = new QScatterSeries(); |
149 | scatterSeries->append(point: QPointF(2, 1)); |
150 | scatterSeries->append(point: scatterPoint); |
151 | scatterSeries->append(point: QPointF(6, 12)); |
152 | |
153 | QChartView view; |
154 | view.resize(w: 200, h: 200); |
155 | view.chart()->legend()->setVisible(false); |
156 | view.chart()->addSeries(series: scatterSeries); |
157 | view.show(); |
158 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
159 | |
160 | QSignalSpy seriesSpy(scatterSeries, SIGNAL(pressed(QPointF))); |
161 | |
162 | QPointF checkPoint = view.chart()->mapToPosition(value: scatterPoint); |
163 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
164 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
165 | |
166 | QCOMPARE(seriesSpy.count(), 1); |
167 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
168 | // checkPoint is QPointF and for the mouseClick it it's changed to QPoint |
169 | // this causes small distinction in decimals so we round it before comparing |
170 | QPointF signalPoint = qvariant_cast<QPointF>(v: seriesSpyArg.at(i: 0)); |
171 | QCOMPARE(qRound(signalPoint.x()), qRound(scatterPoint.x())); |
172 | QCOMPARE(qRound(signalPoint.y()), qRound(scatterPoint.y())); |
173 | } |
174 | |
175 | void tst_QScatterSeries::releasedSignal() |
176 | { |
177 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
178 | |
179 | QPointF scatterPoint(4, 12); |
180 | QScatterSeries *scatterSeries = new QScatterSeries(); |
181 | scatterSeries->append(point: QPointF(2, 1)); |
182 | scatterSeries->append(point: scatterPoint); |
183 | scatterSeries->append(point: QPointF(6, 12)); |
184 | |
185 | QChartView view; |
186 | view.resize(w: 200, h: 200); |
187 | view.chart()->legend()->setVisible(false); |
188 | view.chart()->addSeries(series: scatterSeries); |
189 | view.show(); |
190 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
191 | |
192 | QSignalSpy seriesSpy(scatterSeries, SIGNAL(released(QPointF))); |
193 | |
194 | QPointF checkPoint = view.chart()->mapToPosition(value: scatterPoint); |
195 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
196 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
197 | |
198 | QCOMPARE(seriesSpy.count(), 1); |
199 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
200 | // checkPoint is QPointF and for the mouseClick it it's changed to QPoint |
201 | // this causes small distinction in decimals so we round it before comparing |
202 | QPointF signalPoint = qvariant_cast<QPointF>(v: seriesSpyArg.at(i: 0)); |
203 | QCOMPARE(qRound(signalPoint.x()), qRound(scatterPoint.x())); |
204 | QCOMPARE(qRound(signalPoint.y()), qRound(scatterPoint.y())); |
205 | } |
206 | |
207 | void tst_QScatterSeries::doubleClickedSignal() |
208 | { |
209 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
210 | |
211 | QPointF scatterPoint(4, 12); |
212 | QScatterSeries *scatterSeries = new QScatterSeries(); |
213 | scatterSeries->append(point: QPointF(2, 1)); |
214 | scatterSeries->append(point: scatterPoint); |
215 | scatterSeries->append(point: QPointF(6, 12)); |
216 | |
217 | QChartView view; |
218 | view.resize(w: 200, h: 200); |
219 | view.chart()->legend()->setVisible(false); |
220 | view.chart()->addSeries(series: scatterSeries); |
221 | view.show(); |
222 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
223 | |
224 | QSignalSpy seriesSpy(scatterSeries, SIGNAL(doubleClicked(QPointF))); |
225 | |
226 | QPointF checkPoint = view.chart()->mapToPosition(value: scatterPoint); |
227 | QTest::mouseDClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
228 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
229 | |
230 | QCOMPARE(seriesSpy.count(), 1); |
231 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
232 | // checkPoint is QPointF and for the mouseClick it it's changed to QPoint |
233 | // this causes small distinction in decimals so we round it before comparing |
234 | QPointF signalPoint = qvariant_cast<QPointF>(v: seriesSpyArg.at(i: 0)); |
235 | QCOMPARE(qRound(signalPoint.x()), qRound(scatterPoint.x())); |
236 | QCOMPARE(qRound(signalPoint.y()), qRound(scatterPoint.y())); |
237 | } |
238 | |
239 | QTEST_MAIN(tst_QScatterSeries) |
240 | |
241 | #include "tst_qscatterseries.moc" |
242 | |
243 | |