| 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/QCategoryAxis> | 
| 32 | #include <QtCharts/QLineSeries> | 
| 33 |  | 
| 34 | class tst_QCategoryAxis: 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 qcategoryaxis_data(); | 
| 46 |     void qcategoryaxis(); | 
| 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 labels_position(); | 
| 67 |  | 
| 68 |     void interval_data(); | 
| 69 |     void interval(); | 
| 70 |     void reverse(); | 
| 71 |  | 
| 72 | private: | 
| 73 |     QCategoryAxis* m_categoryaxis; | 
| 74 |     QLineSeries* m_series; | 
| 75 | }; | 
| 76 |  | 
| 77 | void tst_QCategoryAxis::initTestCase() | 
| 78 | { | 
| 79 |     qRegisterMetaType<QCategoryAxis::AxisLabelsPosition>(typeName: "QCategoryAxis::AxisLabelsPosition" ); | 
| 80 | } | 
| 81 |  | 
| 82 | void tst_QCategoryAxis::cleanupTestCase() | 
| 83 | { | 
| 84 |     QTest::qWait(ms: 1); // Allow final deleteLaters to run | 
| 85 | } | 
| 86 |  | 
| 87 | void tst_QCategoryAxis::init() | 
| 88 | { | 
| 89 |     m_categoryaxis = new QCategoryAxis(); | 
| 90 |     m_series = new QLineSeries(); | 
| 91 |     *m_series << QPointF(-100, -100) << QPointF(0, 0) << QPointF(100, 100); | 
| 92 |     tst_QAbstractAxis::initAxes(axis: m_categoryaxis, series: m_series); | 
| 93 |     m_chart->addSeries(series: m_series); | 
| 94 |     m_chart->createDefaultAxes(); | 
| 95 | } | 
| 96 |  | 
| 97 | void tst_QCategoryAxis::cleanup() | 
| 98 | { | 
| 99 |     delete m_series; | 
| 100 |     delete m_categoryaxis; | 
| 101 |     m_series = 0; | 
| 102 |     m_categoryaxis = 0; | 
| 103 |     tst_QAbstractAxis::cleanup(); | 
| 104 | } | 
| 105 |  | 
| 106 | void tst_QCategoryAxis::qcategoryaxis_data() | 
| 107 | { | 
| 108 | } | 
| 109 |  | 
| 110 | void tst_QCategoryAxis::qcategoryaxis() | 
| 111 | { | 
| 112 |     qabstractaxis(); | 
| 113 |  | 
| 114 |     QVERIFY(qFuzzyCompare(m_categoryaxis->max(), 0)); | 
| 115 |     QVERIFY(qFuzzyCompare(m_categoryaxis->min(), 0)); | 
| 116 |     QCOMPARE(m_categoryaxis->type(), QAbstractAxis::AxisTypeCategory); | 
| 117 |     QCOMPARE(m_categoryaxis->labelsPosition(), QCategoryAxis::AxisLabelsPositionCenter); | 
| 118 |  | 
| 119 |     m_chart->addAxis(axis: m_categoryaxis, alignment: Qt::AlignBottom); | 
| 120 |     m_series->attachAxis(axis: m_categoryaxis); | 
| 121 |     m_view->show(); | 
| 122 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 123 |  | 
| 124 |     QVERIFY(!qFuzzyCompare(m_categoryaxis->max(), 0)); | 
| 125 |     QVERIFY(!qFuzzyCompare(m_categoryaxis->min(), 0)); | 
| 126 |  | 
| 127 |     QCOMPARE(m_categoryaxis->isReverse(), false); | 
| 128 | } | 
| 129 |  | 
| 130 | void tst_QCategoryAxis::max_raw_data() | 
| 131 | { | 
| 132 |     QTest::addColumn<qreal>(name: "max" ); | 
| 133 |     QTest::newRow(dataTag: "1.0" ) << (qreal)1.0; | 
| 134 |     QTest::newRow(dataTag: "50.0" ) << (qreal)50.0; | 
| 135 |     QTest::newRow(dataTag: "101.0" ) << (qreal)101.0; | 
| 136 | } | 
| 137 |  | 
| 138 | void tst_QCategoryAxis::max_raw() | 
| 139 | { | 
| 140 |     QFETCH(qreal, max); | 
| 141 |  | 
| 142 |     QSignalSpy spy0(m_categoryaxis, SIGNAL(maxChanged(qreal))); | 
| 143 |     QSignalSpy spy1(m_categoryaxis, SIGNAL(minChanged(qreal))); | 
| 144 |     QSignalSpy spy2(m_categoryaxis, SIGNAL(rangeChanged(qreal,qreal))); | 
| 145 |  | 
| 146 |     m_categoryaxis->setMax(max); | 
| 147 |     QVERIFY2(qFuzzyCompare(m_categoryaxis->max(), max), "Not equal" ); | 
| 148 |  | 
| 149 |     QCOMPARE(spy0.count(), 1); | 
| 150 |     QCOMPARE(spy1.count(), 0); | 
| 151 |     QCOMPARE(spy2.count(), 1); | 
| 152 | } | 
| 153 |  | 
| 154 | void tst_QCategoryAxis::max_data() | 
| 155 | { | 
| 156 |     max_raw_data(); | 
| 157 | } | 
| 158 |  | 
| 159 | void tst_QCategoryAxis::max() | 
| 160 | { | 
| 161 |     m_chart->addAxis(axis: m_categoryaxis, alignment: Qt::AlignBottom); | 
| 162 |     m_series->attachAxis(axis: m_categoryaxis); | 
| 163 |     m_view->show(); | 
| 164 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 165 |     max_raw(); | 
| 166 | } | 
| 167 |  | 
| 168 | void tst_QCategoryAxis::max_animation_data() | 
| 169 | { | 
| 170 |     max_data(); | 
| 171 | } | 
| 172 |  | 
| 173 | void tst_QCategoryAxis::max_animation() | 
| 174 | { | 
| 175 |     m_chart->setAnimationOptions(QChart::GridAxisAnimations); | 
| 176 |     max(); | 
| 177 | } | 
| 178 |  | 
| 179 | void tst_QCategoryAxis::min_raw_data() | 
| 180 | { | 
| 181 |     QTest::addColumn<qreal>(name: "min" ); | 
| 182 |     QTest::newRow(dataTag: "-1.0" ) << (qreal)-1.0; | 
| 183 |     QTest::newRow(dataTag: "-50.0" ) << (qreal)-50.0; | 
| 184 |     QTest::newRow(dataTag: "-101.0" ) << (qreal)-101.0; | 
| 185 | } | 
| 186 |  | 
| 187 | void tst_QCategoryAxis::min_raw() | 
| 188 | { | 
| 189 |     QFETCH(qreal, min); | 
| 190 |  | 
| 191 |     QSignalSpy spy0(m_categoryaxis, SIGNAL(maxChanged(qreal))); | 
| 192 |     QSignalSpy spy1(m_categoryaxis, SIGNAL(minChanged(qreal))); | 
| 193 |     QSignalSpy spy2(m_categoryaxis, SIGNAL(rangeChanged(qreal,qreal))); | 
| 194 |  | 
| 195 |     m_categoryaxis->setMin(min); | 
| 196 |     QVERIFY2(qFuzzyCompare(m_categoryaxis->min(), min), "Not equal" ); | 
| 197 |  | 
| 198 |     QCOMPARE(spy0.count(), 0); | 
| 199 |     QCOMPARE(spy1.count(), 1); | 
| 200 |     QCOMPARE(spy2.count(), 1); | 
| 201 | } | 
| 202 |  | 
| 203 | void tst_QCategoryAxis::min_data() | 
| 204 | { | 
| 205 |     min_raw_data(); | 
| 206 | } | 
| 207 |  | 
| 208 | void tst_QCategoryAxis::min() | 
| 209 | { | 
| 210 |     m_chart->addAxis(axis: m_categoryaxis, alignment: Qt::AlignBottom); | 
| 211 |     m_series->attachAxis(axis: m_categoryaxis); | 
| 212 |     m_view->show(); | 
| 213 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 214 |     min_raw(); | 
| 215 | } | 
| 216 |  | 
| 217 | void tst_QCategoryAxis::min_animation_data() | 
| 218 | { | 
| 219 |     min_data(); | 
| 220 | } | 
| 221 |  | 
| 222 | void tst_QCategoryAxis::min_animation() | 
| 223 | { | 
| 224 |     m_chart->setAnimationOptions(QChart::GridAxisAnimations); | 
| 225 |     min(); | 
| 226 | } | 
| 227 |  | 
| 228 | void tst_QCategoryAxis::range_raw_data() | 
| 229 | { | 
| 230 |     QTest::addColumn<qreal>(name: "min" ); | 
| 231 |     QTest::addColumn<qreal>(name: "max" ); | 
| 232 |     QTest::newRow(dataTag: "1.0 - 101.0" ) << (qreal)-1.0 << (qreal)101.0; | 
| 233 |     QTest::newRow(dataTag: "25.0 - 75.0" ) << (qreal)25.0 << (qreal)75.0; | 
| 234 |     QTest::newRow(dataTag: "101.0" ) << (qreal)40.0 << (qreal)60.0; | 
| 235 |     QTest::newRow(dataTag: "-35.0 - 0.0" ) << (qreal)-35.0 << (qreal)10.0; | 
| 236 |     QTest::newRow(dataTag: "-35.0 - 0.0" ) << (qreal)-35.0 << (qreal)-15.0; | 
| 237 |     QTest::newRow(dataTag: "0.0 - 0.0" ) << (qreal)-0.1 << (qreal)0.1; | 
| 238 | } | 
| 239 |  | 
| 240 | void tst_QCategoryAxis::range_raw() | 
| 241 | { | 
| 242 |     QFETCH(qreal, min); | 
| 243 |     QFETCH(qreal, max); | 
| 244 |  | 
| 245 |     QSignalSpy spy0(m_categoryaxis, SIGNAL(maxChanged(qreal))); | 
| 246 |     QSignalSpy spy1(m_categoryaxis, SIGNAL(minChanged(qreal))); | 
| 247 |     QSignalSpy spy2(m_categoryaxis, SIGNAL(rangeChanged(qreal,qreal))); | 
| 248 |  | 
| 249 |     m_categoryaxis->setRange(min, max); | 
| 250 |     QVERIFY2(qFuzzyCompare(m_categoryaxis->min(), min), "Min not equal" ); | 
| 251 |     QVERIFY2(qFuzzyCompare(m_categoryaxis->max(), max), "Max not equal" ); | 
| 252 |  | 
| 253 |     QCOMPARE(spy0.count(), 1); | 
| 254 |     QCOMPARE(spy1.count(), 1); | 
| 255 |     QCOMPARE(spy2.count(), 1); | 
| 256 | } | 
| 257 |  | 
| 258 | void tst_QCategoryAxis::range_data() | 
| 259 | { | 
| 260 |     range_raw_data(); | 
| 261 | } | 
| 262 |  | 
| 263 | void tst_QCategoryAxis::range() | 
| 264 | { | 
| 265 |     m_chart->addAxis(axis: m_categoryaxis, alignment: Qt::AlignBottom); | 
| 266 |     m_series->attachAxis(axis: m_categoryaxis); | 
| 267 |     m_view->show(); | 
| 268 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 269 |     range_raw(); | 
| 270 | } | 
| 271 |  | 
| 272 | void tst_QCategoryAxis::range_animation_data() | 
| 273 | { | 
| 274 |     range_data(); | 
| 275 | } | 
| 276 |  | 
| 277 | void tst_QCategoryAxis::range_animation() | 
| 278 | { | 
| 279 |     m_chart->setAnimationOptions(QChart::GridAxisAnimations); | 
| 280 |     range(); | 
| 281 | } | 
| 282 |  | 
| 283 | void tst_QCategoryAxis::interval_data() | 
| 284 | { | 
| 285 |     // | 
| 286 | } | 
| 287 |  | 
| 288 | void tst_QCategoryAxis::interval() | 
| 289 | { | 
| 290 |     // append one correct interval | 
| 291 |     m_categoryaxis->append(label: "first" , categoryEndValue: (qreal)45); | 
| 292 |     QCOMPARE(m_categoryaxis->startValue("first" ), (qreal)0); | 
| 293 |     QCOMPARE(m_categoryaxis->endValue("first" ), (qreal)45); | 
| 294 |  | 
| 295 |     // append one more correct interval | 
| 296 |     m_categoryaxis->append(label: "second" , categoryEndValue: (qreal)75); | 
| 297 |     QCOMPARE(m_categoryaxis->startValue("second" ), (qreal)45); | 
| 298 |     QCOMPARE(m_categoryaxis->endValue("second" ), (qreal)75); | 
| 299 |  | 
| 300 |     // append one incorrect interval | 
| 301 |     m_categoryaxis->append(label: "third" , categoryEndValue: (qreal)15); | 
| 302 |     QCOMPARE(m_categoryaxis->count(), 2); | 
| 303 |     QCOMPARE(m_categoryaxis->endValue(m_categoryaxis->categoriesLabels().last()), (qreal)75); | 
| 304 | //    QCOMPARE(intervalMax("first"), (qreal)75); | 
| 305 |  | 
| 306 |     // append one more correct interval | 
| 307 |     m_categoryaxis->append(label: "third" , categoryEndValue: (qreal)100); | 
| 308 |     QCOMPARE(m_categoryaxis->count(), 3); | 
| 309 |     QCOMPARE(m_categoryaxis->startValue("third" ), (qreal)75); | 
| 310 |     QCOMPARE(m_categoryaxis->endValue("third" ), (qreal)100); | 
| 311 |  | 
| 312 |     // remove one interval | 
| 313 |     m_categoryaxis->remove(label: "first" ); | 
| 314 |     QCOMPARE(m_categoryaxis->count(), 2); | 
| 315 |     QCOMPARE(m_categoryaxis->startValue("second" ), (qreal)0); // second interval should extend to firstInterval minimum | 
| 316 |     QCOMPARE(m_categoryaxis->endValue("second" ), (qreal)75); | 
| 317 |  | 
| 318 |     // remove one interval | 
| 319 |     m_categoryaxis->replaceLabel(oldLabel: "second" , newLabel: "replaced" ); | 
| 320 |     QCOMPARE(m_categoryaxis->count(), 2); | 
| 321 |     QCOMPARE(m_categoryaxis->startValue("replaced" ), (qreal)0); // second interval should extend to firstInterval minimum | 
| 322 |     QCOMPARE(m_categoryaxis->endValue("replaced" ), (qreal)75); | 
| 323 | } | 
| 324 |  | 
| 325 | void tst_QCategoryAxis::labels_position() | 
| 326 | { | 
| 327 |     QSignalSpy spy(m_categoryaxis, | 
| 328 |                    SIGNAL(labelsPositionChanged(QCategoryAxis::AxisLabelsPosition))); | 
| 329 |     m_categoryaxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); | 
| 330 |     QCOMPARE(m_categoryaxis->labelsPosition(), QCategoryAxis::AxisLabelsPositionOnValue); | 
| 331 |     QCOMPARE(spy.count(), 1); | 
| 332 | } | 
| 333 |  | 
| 334 | void tst_QCategoryAxis::reverse() | 
| 335 | { | 
| 336 |     QSignalSpy spy(m_categoryaxis, SIGNAL(reverseChanged(bool))); | 
| 337 |     QCOMPARE(m_categoryaxis->isReverse(), false); | 
| 338 |  | 
| 339 |     m_categoryaxis->setReverse(); | 
| 340 |     QCOMPARE(m_categoryaxis->isReverse(), true); | 
| 341 |  | 
| 342 |     m_chart->addAxis(axis: m_categoryaxis, alignment: Qt::AlignBottom); | 
| 343 |     m_series->attachAxis(axis: m_categoryaxis); | 
| 344 |     QCOMPARE(spy.count(), 1); | 
| 345 |  | 
| 346 |     m_view->show(); | 
| 347 |     QVERIFY(QTest::qWaitForWindowExposed(m_view)); | 
| 348 |     QCOMPARE(m_categoryaxis->isReverse(), true); | 
| 349 | } | 
| 350 |  | 
| 351 | QTEST_MAIN(tst_QCategoryAxis) | 
| 352 | #include "tst_qcategoryaxis.moc" | 
| 353 |  | 
| 354 |  |