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