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/QSplineSeries> |
32 | |
33 | Q_DECLARE_METATYPE(QList<QPointF>) |
34 | Q_DECLARE_METATYPE(QVector<QPointF>) |
35 | |
36 | class tst_QSplineSeries : 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 qsplineseries_data(); |
47 | void qsplineseries(); |
48 | void pressedSignal(); |
49 | void releasedSignal(); |
50 | void doubleClickedSignal(); |
51 | protected: |
52 | void pointsVisible_data(); |
53 | }; |
54 | |
55 | void tst_QSplineSeries::initTestCase() |
56 | { |
57 | } |
58 | |
59 | void tst_QSplineSeries::cleanupTestCase() |
60 | { |
61 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
62 | } |
63 | |
64 | void tst_QSplineSeries::init() |
65 | { |
66 | tst_QXYSeries::init(); |
67 | m_series = new QSplineSeries(); |
68 | } |
69 | |
70 | void tst_QSplineSeries::cleanup() |
71 | { |
72 | delete m_series; |
73 | tst_QXYSeries::cleanup(); |
74 | } |
75 | |
76 | void tst_QSplineSeries::qsplineseries_data() |
77 | { |
78 | |
79 | } |
80 | |
81 | void tst_QSplineSeries::qsplineseries() |
82 | { |
83 | QSplineSeries series; |
84 | |
85 | QCOMPARE(series.count(),0); |
86 | QCOMPARE(series.brush(), QBrush()); |
87 | QCOMPARE(series.points(), QList<QPointF>()); |
88 | QCOMPARE(series.pointsVector(), QVector<QPointF>()); |
89 | QCOMPARE(series.pen(), QPen()); |
90 | QCOMPARE(series.pointsVisible(), false); |
91 | |
92 | series.append(points: QList<QPointF>()); |
93 | series.append(x: 0.0,y: 0.0); |
94 | series.append(point: QPointF()); |
95 | |
96 | series.remove(x: 0.0,y: 0.0); |
97 | series.remove(point: QPointF()); |
98 | series.clear(); |
99 | |
100 | series.replace(oldPoint: QPointF(),newPoint: QPointF()); |
101 | series.replace(oldX: 0,oldY: 0,newX: 0,newY: 0); |
102 | series.setBrush(QBrush()); |
103 | |
104 | series.setPen(QPen()); |
105 | series.setPointsVisible(false); |
106 | |
107 | m_chart->addSeries(series: &series); |
108 | m_view->show(); |
109 | QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
110 | } |
111 | |
112 | void tst_QSplineSeries::pressedSignal() |
113 | { |
114 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
115 | |
116 | QPointF splinePoint(4, 12); |
117 | QSplineSeries *splineSeries = new QSplineSeries(); |
118 | splineSeries->append(point: QPointF(2, 1)); |
119 | splineSeries->append(point: splinePoint); |
120 | splineSeries->append(point: QPointF(6, 12)); |
121 | |
122 | QChartView view; |
123 | view.resize(w: 200, h: 200); |
124 | view.chart()->legend()->setVisible(false); |
125 | view.chart()->addSeries(series: splineSeries); |
126 | view.show(); |
127 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
128 | |
129 | QSignalSpy seriesSpy(splineSeries, SIGNAL(pressed(QPointF))); |
130 | |
131 | QPointF checkPoint = view.chart()->mapToPosition(value: splinePoint); |
132 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
133 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
134 | |
135 | QCOMPARE(seriesSpy.count(), 1); |
136 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
137 | // checkPoint is QPointF and for the mouseClick it it's changed to QPoint |
138 | // this causes small distinction in decimals so we round it before comparing |
139 | QPointF signalPoint = qvariant_cast<QPointF>(v: seriesSpyArg.at(i: 0)); |
140 | QCOMPARE(qRound(signalPoint.x()), qRound(splinePoint.x())); |
141 | QCOMPARE(qRound(signalPoint.y()), qRound(splinePoint.y())); |
142 | } |
143 | |
144 | void tst_QSplineSeries::releasedSignal() |
145 | { |
146 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
147 | |
148 | QPointF splinePoint(4, 12); |
149 | QSplineSeries *splineSeries = new QSplineSeries(); |
150 | splineSeries->append(point: QPointF(2, 20)); |
151 | splineSeries->append(point: splinePoint); |
152 | splineSeries->append(point: QPointF(6, 12)); |
153 | |
154 | QChartView view; |
155 | view.resize(w: 200, h: 200); |
156 | view.chart()->legend()->setVisible(false); |
157 | view.chart()->addSeries(series: splineSeries); |
158 | view.show(); |
159 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
160 | |
161 | QSignalSpy seriesSpy(splineSeries, SIGNAL(released(QPointF))); |
162 | |
163 | QPointF checkPoint = view.chart()->mapToPosition(value: splinePoint); |
164 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
165 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
166 | |
167 | QCOMPARE(seriesSpy.count(), 1); |
168 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
169 | // checkPoint is QPointF and for the mouseClick it it's changed to QPoint |
170 | // this causes small distinction in decimals so we round it before comparing |
171 | QPointF signalPoint = qvariant_cast<QPointF>(v: seriesSpyArg.at(i: 0)); |
172 | QCOMPARE(qRound(signalPoint.x()), qRound(splinePoint.x())); |
173 | QCOMPARE(qRound(signalPoint.y()), qRound(splinePoint.y())); |
174 | } |
175 | |
176 | void tst_QSplineSeries::doubleClickedSignal() |
177 | { |
178 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
179 | |
180 | QPointF splinePoint(4, 12); |
181 | QSplineSeries *splineSeries = new QSplineSeries(); |
182 | splineSeries->append(point: QPointF(2, 20)); |
183 | splineSeries->append(point: splinePoint); |
184 | splineSeries->append(point: QPointF(6, 12)); |
185 | |
186 | QChartView view; |
187 | view.resize(w: 200, h: 200); |
188 | view.chart()->legend()->setVisible(false); |
189 | view.chart()->addSeries(series: splineSeries); |
190 | view.show(); |
191 | QVERIFY(QTest::qWaitForWindowExposed(&view)); |
192 | |
193 | QSignalSpy seriesSpy(splineSeries, SIGNAL(doubleClicked(QPointF))); |
194 | |
195 | QPointF checkPoint = view.chart()->mapToPosition(value: splinePoint); |
196 | // mouseClick needed first to save the position |
197 | QTest::mouseClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
198 | QTest::mouseDClick(widget: view.viewport(), button: Qt::LeftButton, stateKey: {}, pos: checkPoint.toPoint()); |
199 | QCoreApplication::processEvents(flags: QEventLoop::AllEvents, maxtime: 1000); |
200 | |
201 | QCOMPARE(seriesSpy.count(), 1); |
202 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
203 | // checkPoint is QPointF and for the mouseClick it it's changed to QPoint |
204 | // this causes small distinction in decimals so we round it before comparing |
205 | QPointF signalPoint = qvariant_cast<QPointF>(v: seriesSpyArg.at(i: 0)); |
206 | QCOMPARE(qRound(signalPoint.x()), qRound(splinePoint.x())); |
207 | QCOMPARE(qRound(signalPoint.y()), qRound(splinePoint.y())); |
208 | } |
209 | QTEST_MAIN(tst_QSplineSeries) |
210 | |
211 | #include "tst_qsplineseries.moc" |
212 | |
213 | |