| 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 <QtTest/QtTest> |
| 31 | #include <QtCharts/qchartview.h> |
| 32 | #include <QtCharts/qlineseries.h> |
| 33 | #include <cmath> |
| 34 | #include <tst_definitions.h> |
| 35 | #include <QtCharts/QValueAxis> |
| 36 | |
| 37 | QT_CHARTS_USE_NAMESPACE |
| 38 | |
| 39 | |
| 40 | Q_DECLARE_METATYPE(QChart*) |
| 41 | Q_DECLARE_METATYPE(QChartView::RubberBands) |
| 42 | Q_DECLARE_METATYPE(Qt::Key) |
| 43 | |
| 44 | class tst_QChartView : public QObject |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | |
| 48 | public Q_SLOTS: |
| 49 | void initTestCase(); |
| 50 | void cleanupTestCase(); |
| 51 | void init(); |
| 52 | void cleanup(); |
| 53 | |
| 54 | private Q_SLOTS: |
| 55 | void qchartview_data(); |
| 56 | void qchartview(); |
| 57 | void chart_data(); |
| 58 | void chart(); |
| 59 | void rubberBand_data(); |
| 60 | void rubberBand(); |
| 61 | void setChart(); |
| 62 | |
| 63 | private: |
| 64 | QChartView* m_view; |
| 65 | }; |
| 66 | |
| 67 | void tst_QChartView::initTestCase() |
| 68 | { |
| 69 | //test tracks mouse, give a while to user to relese it |
| 70 | QTest::qWait(ms: 1000); |
| 71 | } |
| 72 | |
| 73 | void tst_QChartView::cleanupTestCase() |
| 74 | { |
| 75 | QTest::qWait(ms: 1); // Allow final deleteLaters to run |
| 76 | } |
| 77 | |
| 78 | void tst_QChartView::init() |
| 79 | { |
| 80 | m_view = new QChartView(newQChartOrQPolarChart()); |
| 81 | m_view->resize(w: 200, h: 200); |
| 82 | m_view->chart()->legend()->setVisible(false); |
| 83 | } |
| 84 | |
| 85 | void tst_QChartView::cleanup() |
| 86 | { |
| 87 | delete m_view; |
| 88 | m_view =0; |
| 89 | } |
| 90 | |
| 91 | void tst_QChartView::qchartview_data() |
| 92 | { |
| 93 | |
| 94 | } |
| 95 | |
| 96 | void tst_QChartView::qchartview() |
| 97 | { |
| 98 | QVERIFY(m_view->chart()); |
| 99 | QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand); |
| 100 | m_view->show(); |
| 101 | QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
| 102 | |
| 103 | delete(new QChartView()); |
| 104 | |
| 105 | QChartView view; |
| 106 | QVERIFY(view.chart()); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | void tst_QChartView::chart_data() |
| 111 | { |
| 112 | |
| 113 | QTest::addColumn<QChart*>(name: "chart" ); |
| 114 | QTest::newRow(dataTag: "qchart" ) << new QChart(); |
| 115 | } |
| 116 | |
| 117 | void tst_QChartView::chart() |
| 118 | { |
| 119 | QFETCH(QChart*, chart); |
| 120 | QChartView* view = new QChartView(chart); |
| 121 | QCOMPARE(view->chart(), chart); |
| 122 | delete view; |
| 123 | } |
| 124 | |
| 125 | void tst_QChartView::rubberBand_data() |
| 126 | { |
| 127 | QTest::addColumn<QChartView::RubberBands>(name: "rubberBand" ); |
| 128 | QTest::addColumn<int>(name: "Xcount" ); |
| 129 | QTest::addColumn<int>(name: "Ycount" ); |
| 130 | |
| 131 | QTest::addColumn<QPoint>(name: "min" ); |
| 132 | QTest::addColumn<QPoint>(name: "max" ); |
| 133 | |
| 134 | if (isPolarTest()) { |
| 135 | QTest::newRow(dataTag: "HorizontalRubberBand" ) << QChartView::RubberBands(QChartView::HorizontalRubberBand) << 0 << 0 << QPoint(5,5) << QPoint(5,5); |
| 136 | QTest::newRow(dataTag: "VerticalRubberBand" ) << QChartView::RubberBands(QChartView::VerticalRubberBand) << 0 << 0 << QPoint(5,5) << QPoint(5,5); |
| 137 | QTest::newRow(dataTag: "RectangleRubberBand" ) << QChartView::RubberBands(QChartView::RectangleRubberBand) << 0 << 0 << QPoint(5,5) << QPoint(5,5); |
| 138 | } else { |
| 139 | QTest::newRow(dataTag: "HorizontalRubberBand" ) << QChartView::RubberBands(QChartView::HorizontalRubberBand) << 0 << 1 << QPoint(5,5) << QPoint(5,5); |
| 140 | QTest::newRow(dataTag: "VerticalRubberBand" ) << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << QPoint(5,5) << QPoint(5,5); |
| 141 | QTest::newRow(dataTag: "RectangleRubberBand" ) << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 << QPoint(5,5) << QPoint(5,5); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void tst_QChartView::rubberBand() |
| 146 | { |
| 147 | SKIP_IF_CANNOT_TEST_MOUSE_EVENTS(); |
| 148 | SKIP_IF_FLAKY_MOUSE_MOVE(); |
| 149 | |
| 150 | QFETCH(QChartView::RubberBands, rubberBand); |
| 151 | QFETCH(int, Xcount); |
| 152 | QFETCH(int, Ycount); |
| 153 | QFETCH(QPoint, min); |
| 154 | QFETCH(QPoint, max); |
| 155 | |
| 156 | m_view->setRubberBand(rubberBand); |
| 157 | |
| 158 | QCOMPARE(m_view->rubberBand(), rubberBand); |
| 159 | |
| 160 | QLineSeries* line = new QLineSeries(); |
| 161 | *line << QPointF(0, 0) << QPointF(200, 200); |
| 162 | |
| 163 | m_view->chart()->addSeries(series: line); |
| 164 | m_view->chart()->createDefaultAxes(); |
| 165 | m_view->show(); |
| 166 | QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
| 167 | |
| 168 | QRectF plotArea = m_view->chart()->plotArea(); |
| 169 | //this is hack since view does not get events otherwise |
| 170 | m_view->setMouseTracking(true); |
| 171 | |
| 172 | QAbstractAxis *axisY = m_view->chart()->axes(orientation: Qt::Vertical).value(i: 0); |
| 173 | QVERIFY(axisY); |
| 174 | QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal))); |
| 175 | QAbstractAxis *axisX = m_view->chart()->axes(orientation: Qt::Horizontal).value(i: 0); |
| 176 | QVERIFY(axisX); |
| 177 | QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal))); |
| 178 | QValueAxis* vaxisX = qobject_cast<QValueAxis*>(object: axisX); |
| 179 | QValueAxis* vaxisY = qobject_cast<QValueAxis*>(object: axisY); |
| 180 | int minX = vaxisX->min(); |
| 181 | int minY = vaxisY->min(); |
| 182 | int maxX = vaxisX->max(); |
| 183 | int maxY = vaxisY->max(); |
| 184 | |
| 185 | // try to ensure focus |
| 186 | QApplication::setActiveWindow(m_view); |
| 187 | m_view->setFocus(); |
| 188 | QApplication::processEvents(); |
| 189 | QVERIFY(m_view->isActiveWindow()); |
| 190 | QVERIFY(m_view->hasFocus()); |
| 191 | |
| 192 | QTest::mouseMove(widget: m_view->viewport(), pos: min + plotArea.topLeft().toPoint(), delay: 100); |
| 193 | QTest::mousePress(widget: m_view->viewport(), button: Qt::LeftButton, stateKey: {}, pos: min + plotArea.topLeft().toPoint(), delay: 100); |
| 194 | QTest::mouseMove(widget: m_view->viewport(), pos: plotArea.bottomRight().toPoint() - max, delay: 100); |
| 195 | QTest::mouseRelease(widget: m_view->viewport(), button: Qt::LeftButton, stateKey: {}, pos: plotArea.bottomRight().toPoint() - max, delay: 100); |
| 196 | QApplication::processEvents(); |
| 197 | |
| 198 | TRY_COMPARE(spy0.count(), Xcount); |
| 199 | TRY_COMPARE(spy1.count(), Ycount); |
| 200 | |
| 201 | //this is hack since view does not get events otherwise |
| 202 | m_view->setMouseTracking(false); |
| 203 | |
| 204 | QVERIFY(vaxisX->min() >= minX ); |
| 205 | QVERIFY(vaxisX->max() <= maxX ); |
| 206 | QVERIFY(vaxisY->min() >= minY ); |
| 207 | QVERIFY(vaxisY->max() <= maxY ); |
| 208 | |
| 209 | } |
| 210 | |
| 211 | void tst_QChartView::setChart() |
| 212 | { |
| 213 | QPointer<QChart> oldChart = m_view->chart(); |
| 214 | QLineSeries *series1 = new QLineSeries(); |
| 215 | series1->append(x: 0,y: 0); |
| 216 | series1->append(x: 1,y: 1); |
| 217 | oldChart->addSeries(series: series1); |
| 218 | |
| 219 | QPointer<QChart> newChart = newQChartOrQPolarChart(); |
| 220 | |
| 221 | QLineSeries *series2 = new QLineSeries(); |
| 222 | series2->append(x: 0,y: 1); |
| 223 | series2->append(x: 1,y: 0); |
| 224 | newChart->addSeries(series: series2); |
| 225 | |
| 226 | // show current chart |
| 227 | m_view->show(); |
| 228 | QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
| 229 | QTest::qWait(ms: 1000); |
| 230 | |
| 231 | // set new chart |
| 232 | m_view->setChart(newChart); |
| 233 | QVERIFY(m_view->chart() == newChart); |
| 234 | QVERIFY(!oldChart.isNull()); // ownership of the oldChart is released and not deleted |
| 235 | QTest::qWait(ms: 1000); |
| 236 | |
| 237 | // delete the view |
| 238 | delete m_view; |
| 239 | m_view = 0; |
| 240 | QVERIFY(newChart.isNull()); // view owns and deletes it |
| 241 | QVERIFY(!oldChart.isNull()); // not owned by view |
| 242 | |
| 243 | delete oldChart; |
| 244 | } |
| 245 | |
| 246 | QTEST_MAIN(tst_QChartView) |
| 247 | #include "tst_qchartview.moc" |
| 248 | |
| 249 | |