| 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 "../qabstractaxis/tst_qabstractaxis.h" | 
| 31 | #include <QtCharts/QDateTimeAxis> | 
| 32 | #include <QtCharts/QLineSeries> | 
| 33 |  | 
| 34 | class tst_QDateTimeAxis : public QObject//: public tst_QAbstractAxis | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 |  | 
| 38 | public slots: | 
| 39 |     void initTestCase(); | 
| 40 |     void cleanupTestCase(); | 
| 41 |     void init(); | 
| 42 |     void cleanup(); | 
| 43 |  | 
| 44 | private slots: | 
| 45 |     void qdatetimeaxis_data(); | 
| 46 |     void qdatetimeaxis(); | 
| 47 |  | 
| 48 |     void max_raw_data(); | 
| 49 |     void max_raw(); | 
| 50 |     void max_data(); | 
| 51 |     void max(); | 
| 52 |     void max_animation_data(); | 
| 53 |     void max_animation(); | 
| 54 |     void min_raw_data(); | 
| 55 |     void min_raw(); | 
| 56 |     void min_data(); | 
| 57 |     void min(); | 
| 58 |     void min_animation_data(); | 
| 59 |     void min_animation(); | 
| 60 |     void range_raw_data(); | 
| 61 |     void range_raw(); | 
| 62 |     void range_data(); | 
| 63 |     void range(); | 
| 64 |     void range_animation_data(); | 
| 65 |     void range_animation(); | 
| 66 |     void reverse(); | 
| 67 |  | 
| 68 | private: | 
| 69 |     QDateTimeAxis *m_dateTimeAxisX; | 
| 70 |     QDateTimeAxis *m_dateTimeAxisY; | 
| 71 |     QLineSeries* m_series; | 
| 72 |     QChartView* m_view; | 
| 73 |     QChart* m_chart; | 
| 74 | }; | 
| 75 |  | 
| 76 | void tst_QDateTimeAxis::initTestCase() | 
| 77 | { | 
| 78 | } | 
| 79 |  | 
| 80 | void tst_QDateTimeAxis::cleanupTestCase() | 
| 81 | { | 
| 82 |     QTest::qWait(ms: 1); // Allow final deleteLaters to run | 
| 83 | } | 
| 84 |  | 
| 85 | void tst_QDateTimeAxis::init() | 
| 86 | { | 
| 87 |     m_dateTimeAxisX = new QDateTimeAxis(); | 
| 88 |     m_dateTimeAxisY = new QDateTimeAxis(); | 
| 89 |     m_series = new QLineSeries(); | 
| 90 |     *m_series << QPointF(-100, -100) << QPointF(0, 0) << QPointF(100, 100); | 
| 91 |     //    tst_QAbstractAxis::initAxes(m_datetimeaxis, m_series); | 
| 92 |  | 
| 93 |     m_view = new QChartView; | 
| 94 |     m_view->resize(w: 200, h: 200); | 
| 95 |     m_chart = m_view->chart(); | 
| 96 |     m_chart->addSeries(series: m_series); | 
| 97 |     m_chart->addAxis(axis: m_dateTimeAxisY, alignment: Qt::AlignLeft); | 
| 98 |     m_series->attachAxis(axis: m_dateTimeAxisY); | 
| 99 |     m_chart->addAxis(axis: m_dateTimeAxisX, alignment: Qt::AlignBottom); | 
| 100 |     m_series->attachAxis(axis: m_dateTimeAxisX); | 
| 101 | } | 
| 102 |  | 
| 103 | void tst_QDateTimeAxis::cleanup() | 
| 104 | { | 
| 105 |     delete m_series; | 
| 106 |     delete m_dateTimeAxisX; | 
| 107 |     delete m_dateTimeAxisY; | 
| 108 |     m_series = 0; | 
| 109 |     m_dateTimeAxisX = 0; | 
| 110 |     m_dateTimeAxisY = 0; | 
| 111 |     delete m_view; | 
| 112 |     m_view = 0; | 
| 113 |     m_chart = 0; | 
| 114 |     //    tst_QAbstractAxis::cleanup(); | 
| 115 | } | 
| 116 |  | 
| 117 | void tst_QDateTimeAxis::qdatetimeaxis_data() | 
| 118 | { | 
| 119 | } | 
| 120 |  | 
| 121 | void tst_QDateTimeAxis::qdatetimeaxis() | 
| 122 | { | 
| 123 |     //    qabstractaxis(); | 
| 124 |     QCOMPARE(m_dateTimeAxisX->type(), QAbstractAxis::AxisTypeDateTime); | 
| 125 |  | 
| 126 |     m_view->show(); | 
| 127 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 128 |  | 
| 129 |     QVERIFY(m_dateTimeAxisX->max().toMSecsSinceEpoch() != 0); | 
| 130 |     QVERIFY(m_dateTimeAxisX->min().toMSecsSinceEpoch() != 0); | 
| 131 |  | 
| 132 |     QCOMPARE(m_dateTimeAxisX->isReverse(), false); | 
| 133 |     QCOMPARE(m_dateTimeAxisY->isReverse(), false); | 
| 134 | } | 
| 135 |  | 
| 136 | void tst_QDateTimeAxis::max_raw_data() | 
| 137 | { | 
| 138 |     QTest::addColumn<QDateTime>(name: "max" ); | 
| 139 |     QTest::addColumn<bool>(name: "valid" ); | 
| 140 |     QDateTime dateTime; | 
| 141 |     dateTime.setDate(QDate(2012, 7, 19)); | 
| 142 |     QTest::newRow(dataTag: "19.7.2012 - Valid" ) << dateTime << true; | 
| 143 |     dateTime.setDate(QDate(2012, 17, 32)); | 
| 144 |     QTest::newRow(dataTag: "32.17.2012 - Invalid" ) << dateTime << false; | 
| 145 | } | 
| 146 |  | 
| 147 | void tst_QDateTimeAxis::max_raw() | 
| 148 | { | 
| 149 |     QFETCH(QDateTime, max); | 
| 150 |     QFETCH(bool, valid); | 
| 151 |  | 
| 152 |     QSignalSpy spy0(m_dateTimeAxisX, SIGNAL(maxChanged(QDateTime))); | 
| 153 |     QSignalSpy spy1(m_dateTimeAxisX, SIGNAL(minChanged(QDateTime))); | 
| 154 |     QSignalSpy spy2(m_dateTimeAxisX, SIGNAL(rangeChanged(QDateTime,QDateTime))); | 
| 155 |  | 
| 156 |     m_dateTimeAxisX->setMax(max); | 
| 157 |  | 
| 158 |     if (valid) { | 
| 159 |         QVERIFY2(m_dateTimeAxisX->max() == max, "Not equal" ); | 
| 160 |         QCOMPARE(spy0.count(), 1); | 
| 161 |         QCOMPARE(spy1.count(), 0); | 
| 162 |         QCOMPARE(spy2.count(), 1); | 
| 163 |     } else { | 
| 164 |         QVERIFY2(m_dateTimeAxisX->max() != max, "Date is invalid and should not be set" ); | 
| 165 |         QCOMPARE(spy0.count(), 0); | 
| 166 |         QCOMPARE(spy1.count(), 0); | 
| 167 |         QCOMPARE(spy2.count(), 0); | 
| 168 |     } | 
| 169 | } | 
| 170 |  | 
| 171 | void tst_QDateTimeAxis::max_data() | 
| 172 | { | 
| 173 |     max_raw_data(); | 
| 174 | } | 
| 175 |  | 
| 176 | void tst_QDateTimeAxis::max() | 
| 177 | { | 
| 178 | //    m_chart->setAxisX(m_dateTimeAxisX, m_series); | 
| 179 |     m_view->show(); | 
| 180 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 181 |     max_raw(); | 
| 182 | } | 
| 183 |  | 
| 184 | void tst_QDateTimeAxis::max_animation_data() | 
| 185 | { | 
| 186 |     max_data(); | 
| 187 | } | 
| 188 |  | 
| 189 | void tst_QDateTimeAxis::max_animation() | 
| 190 | { | 
| 191 |     m_chart->setAnimationOptions(QChart::GridAxisAnimations); | 
| 192 |     max(); | 
| 193 | } | 
| 194 |  | 
| 195 | void tst_QDateTimeAxis::min_raw_data() | 
| 196 | { | 
| 197 |     QTest::addColumn<QDateTime>(name: "min" ); | 
| 198 |     QTest::addColumn<bool>(name: "valid" ); | 
| 199 |     QDateTime dateTime; | 
| 200 |     dateTime.setDate(QDate(1908, 1, 11)); | 
| 201 |     QTest::newRow(dataTag: "11.1.1908 - Valid" ) << dateTime << true; // negative MSecs from Epoch | 
| 202 |     dateTime.setDate(QDate(2012, 17, 32)); | 
| 203 |     QTest::newRow(dataTag: "32.17.2012 - Invalid" ) << dateTime << false; | 
| 204 | } | 
| 205 |  | 
| 206 | void tst_QDateTimeAxis::min_raw() | 
| 207 | { | 
| 208 |     QFETCH(QDateTime, min); | 
| 209 |     QFETCH(bool, valid); | 
| 210 |  | 
| 211 |     QSignalSpy spy0(m_dateTimeAxisX, SIGNAL(maxChanged(QDateTime))); | 
| 212 |     QSignalSpy spy1(m_dateTimeAxisX, SIGNAL(minChanged(QDateTime))); | 
| 213 |     QSignalSpy spy2(m_dateTimeAxisX, SIGNAL(rangeChanged(QDateTime,QDateTime))); | 
| 214 |  | 
| 215 |     m_dateTimeAxisX->setMin(min); | 
| 216 |  | 
| 217 |     if (valid) { | 
| 218 |         QVERIFY2(m_dateTimeAxisX->min() == min, "Not equal" ); | 
| 219 |         QCOMPARE(spy0.count(), 0); | 
| 220 |         QCOMPARE(spy1.count(), 1); | 
| 221 |         QCOMPARE(spy2.count(), 1); | 
| 222 |     } else { | 
| 223 |         QVERIFY2(m_dateTimeAxisX->min() != min, "Date is invalid and should not be set" ); | 
| 224 |         QCOMPARE(spy0.count(), 0); | 
| 225 |         QCOMPARE(spy1.count(), 0); | 
| 226 |         QCOMPARE(spy2.count(), 0); | 
| 227 |     } | 
| 228 | } | 
| 229 |  | 
| 230 | void tst_QDateTimeAxis::min_data() | 
| 231 | { | 
| 232 |     min_raw_data(); | 
| 233 | } | 
| 234 |  | 
| 235 | void tst_QDateTimeAxis::min() | 
| 236 | { | 
| 237 | //    m_chart->setAxisX(m_dateTimeAxisX, m_series); | 
| 238 |     m_view->show(); | 
| 239 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 240 |     min_raw(); | 
| 241 | } | 
| 242 |  | 
| 243 | void tst_QDateTimeAxis::min_animation_data() | 
| 244 | { | 
| 245 |     min_data(); | 
| 246 | } | 
| 247 |  | 
| 248 | void tst_QDateTimeAxis::min_animation() | 
| 249 | { | 
| 250 |     m_chart->setAnimationOptions(QChart::GridAxisAnimations); | 
| 251 |     min(); | 
| 252 | } | 
| 253 |  | 
| 254 | void tst_QDateTimeAxis::range_raw_data() | 
| 255 | { | 
| 256 |     QTest::addColumn<QDateTime>(name: "min" ); | 
| 257 |     QTest::addColumn<bool>(name: "minValid" ); | 
| 258 |     QTest::addColumn<QDateTime>(name: "max" ); | 
| 259 |     QTest::addColumn<bool>(name: "maxValid" ); | 
| 260 |  | 
| 261 |     QDateTime minDateTime; | 
| 262 |     QDateTime maxDateTime; | 
| 263 |     minDateTime.setDate(QDate(1908, 1, 11)); | 
| 264 |     maxDateTime.setDate(QDate(1958, 11, 21)); | 
| 265 |     QTest::newRow(dataTag: "11.1.1908 - min valid, 21.12.1958 - max valid" ) << minDateTime << true << maxDateTime << true; // negative MSecs from Epoch, min < max | 
| 266 |  | 
| 267 |     minDateTime.setDate(QDate(2012, 17, 32)); | 
| 268 |     QTest::newRow(dataTag: "32.17.2012 - min invalid, 21.12.1958 - max valid" ) << minDateTime << false << maxDateTime << true; | 
| 269 |  | 
| 270 |     maxDateTime.setDate(QDate(2017, 0, 1)); | 
| 271 |     QTest::newRow(dataTag: "32.17.2012 - min invalid, 1.0.2017 - max invalid" ) << minDateTime << false << maxDateTime << false; | 
| 272 |  | 
| 273 |     minDateTime.setDate(QDate(2012, 1, 1)); | 
| 274 |     QTest::newRow(dataTag: "1.1.2012 - min valid, 1.0.2017 - max invalid" ) << minDateTime << true << maxDateTime << false; | 
| 275 |  | 
| 276 |     maxDateTime.setDate(QDate(2005, 2, 5)); | 
| 277 |     QTest::newRow(dataTag: "1.1.2012 - min valid, 5.2.2005 - max valid" ) << minDateTime << true << maxDateTime << true; // min > max | 
| 278 | } | 
| 279 |  | 
| 280 | void tst_QDateTimeAxis::range_raw() | 
| 281 | { | 
| 282 |     QFETCH(QDateTime, min); | 
| 283 |     QFETCH(bool, minValid); | 
| 284 |     QFETCH(QDateTime, max); | 
| 285 |     QFETCH(bool, maxValid); | 
| 286 |  | 
| 287 |     QSignalSpy spy0(m_dateTimeAxisX, SIGNAL(maxChanged(QDateTime))); | 
| 288 |     QSignalSpy spy1(m_dateTimeAxisX, SIGNAL(minChanged(QDateTime))); | 
| 289 |     QSignalSpy spy2(m_dateTimeAxisX, SIGNAL(rangeChanged(QDateTime,QDateTime))); | 
| 290 |  | 
| 291 |     m_dateTimeAxisX->setRange(min, max); | 
| 292 |  | 
| 293 |     if (minValid && maxValid && min < max) { | 
| 294 |         QCOMPARE(spy0.count(), 1); | 
| 295 |         QCOMPARE(spy1.count(), 1); | 
| 296 |         QCOMPARE(spy2.count(), 1); | 
| 297 |     } else { | 
| 298 |         QCOMPARE(spy0.count(), 0); | 
| 299 |         QCOMPARE(spy1.count(), 0); | 
| 300 |         QCOMPARE(spy2.count(), 0); | 
| 301 |     } | 
| 302 | } | 
| 303 |  | 
| 304 | void tst_QDateTimeAxis::range_data() | 
| 305 | { | 
| 306 |     range_raw_data(); | 
| 307 | } | 
| 308 |  | 
| 309 | void tst_QDateTimeAxis::range() | 
| 310 | { | 
| 311 | //    m_chart->setAxisX(m_dateTimeAxisX, m_series); | 
| 312 |     m_view->show(); | 
| 313 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 314 |     range_raw(); | 
| 315 | } | 
| 316 |  | 
| 317 | void tst_QDateTimeAxis::range_animation_data() | 
| 318 | { | 
| 319 |     range_data(); | 
| 320 | } | 
| 321 |  | 
| 322 | void tst_QDateTimeAxis::range_animation() | 
| 323 | { | 
| 324 |     m_chart->setAnimationOptions(QChart::GridAxisAnimations); | 
| 325 |     range(); | 
| 326 | } | 
| 327 |  | 
| 328 | void tst_QDateTimeAxis::reverse() | 
| 329 | { | 
| 330 |     QSignalSpy spy(m_dateTimeAxisX, SIGNAL(reverseChanged(bool))); | 
| 331 |     QCOMPARE(m_dateTimeAxisX->isReverse(), false); | 
| 332 |  | 
| 333 |     m_dateTimeAxisX->setReverse(); | 
| 334 |     QCOMPARE(m_dateTimeAxisX->isReverse(), true); | 
| 335 |     QCOMPARE(spy.count(), 1); | 
| 336 |  | 
| 337 |     m_view->show(); | 
| 338 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 339 |     QCOMPARE(m_dateTimeAxisX->isReverse(), true); | 
| 340 | } | 
| 341 |  | 
| 342 | QTEST_MAIN(tst_QDateTimeAxis) | 
| 343 | #include "tst_qdatetimeaxis.moc" | 
| 344 |  | 
| 345 |  |