| 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 Data Visualization 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 | |
| 32 | #include <QtDataVisualization/Q3DBars> |
| 33 | #include <QtDataVisualization/QCustom3DItem> |
| 34 | #include <QtDataVisualization/Q3DInputHandler> |
| 35 | #include <QtDataVisualization/QTouch3DInputHandler> |
| 36 | |
| 37 | #include "cpptestutil.h" |
| 38 | |
| 39 | using namespace QtDataVisualization; |
| 40 | |
| 41 | class tst_bars: public QObject |
| 42 | { |
| 43 | Q_OBJECT |
| 44 | |
| 45 | private slots: |
| 46 | void initTestCase(); |
| 47 | void cleanupTestCase(); |
| 48 | void init(); |
| 49 | void cleanup(); |
| 50 | |
| 51 | void construct(); |
| 52 | |
| 53 | void initialProperties(); |
| 54 | void initializeProperties(); |
| 55 | void invalidProperties(); |
| 56 | |
| 57 | void addSeries(); |
| 58 | void addMultipleSeries(); |
| 59 | void selectSeries(); |
| 60 | void removeSeries(); |
| 61 | void removeMultipleSeries(); |
| 62 | |
| 63 | // The following tests are not required for scatter or surface, as they are handled identically |
| 64 | void addInputHandler(); |
| 65 | void removeInputHandler(); |
| 66 | |
| 67 | void addTheme(); |
| 68 | void removeTheme(); |
| 69 | |
| 70 | void addCustomItem(); |
| 71 | void removeCustomItem(); |
| 72 | |
| 73 | void renderToImage(); |
| 74 | |
| 75 | private: |
| 76 | Q3DBars *m_graph; |
| 77 | }; |
| 78 | |
| 79 | QBar3DSeries *newSeries() |
| 80 | { |
| 81 | QBar3DSeries *series = new QBar3DSeries; |
| 82 | QBarDataRow *data = new QBarDataRow; |
| 83 | *data << -1.0f << 3.0f << 7.5f << 5.0f << 2.2f; |
| 84 | series->dataProxy()->addRow(row: data); |
| 85 | return series; |
| 86 | } |
| 87 | |
| 88 | void tst_bars::initTestCase() |
| 89 | { |
| 90 | if (!CpptestUtil::isOpenGLSupported()) |
| 91 | QSKIP("OpenGL not supported on this platform" ); |
| 92 | } |
| 93 | |
| 94 | void tst_bars::cleanupTestCase() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void tst_bars::init() |
| 99 | { |
| 100 | m_graph = new Q3DBars(); |
| 101 | } |
| 102 | |
| 103 | void tst_bars::cleanup() |
| 104 | { |
| 105 | delete m_graph; |
| 106 | } |
| 107 | |
| 108 | void tst_bars::construct() |
| 109 | { |
| 110 | Q3DBars *graph = new Q3DBars(); |
| 111 | QVERIFY(graph); |
| 112 | delete graph; |
| 113 | |
| 114 | QSurfaceFormat format; |
| 115 | graph = new Q3DBars(&format); |
| 116 | QVERIFY(graph); |
| 117 | delete graph; |
| 118 | } |
| 119 | |
| 120 | void tst_bars::initialProperties() |
| 121 | { |
| 122 | QVERIFY(m_graph); |
| 123 | QCOMPARE(m_graph->isMultiSeriesUniform(), false); |
| 124 | QCOMPARE(m_graph->barThickness(), 1.0); |
| 125 | QCOMPARE(m_graph->barSpacing(), QSizeF(1.0f, 1.0f)); |
| 126 | QCOMPARE(m_graph->isBarSpacingRelative(), true); |
| 127 | QCOMPARE(m_graph->seriesList().length(), 0); |
| 128 | QVERIFY(!m_graph->selectedSeries()); |
| 129 | QVERIFY(!m_graph->primarySeries()); |
| 130 | QCOMPARE(m_graph->floorLevel(), 0.0); |
| 131 | QCOMPARE(m_graph->columnAxis()->orientation(), QAbstract3DAxis::AxisOrientationX); |
| 132 | QCOMPARE(m_graph->valueAxis()->orientation(), QAbstract3DAxis::AxisOrientationY); |
| 133 | QCOMPARE(m_graph->rowAxis()->orientation(), QAbstract3DAxis::AxisOrientationZ); |
| 134 | |
| 135 | // Common properties |
| 136 | QCOMPARE(m_graph->activeTheme()->type(), Q3DTheme::ThemeQt); |
| 137 | QCOMPARE(m_graph->selectionMode(), QAbstract3DGraph::SelectionItem); |
| 138 | QCOMPARE(m_graph->shadowQuality(), QAbstract3DGraph::ShadowQualityMedium); |
| 139 | QVERIFY(m_graph->scene()); |
| 140 | QCOMPARE(m_graph->measureFps(), false); |
| 141 | QCOMPARE(m_graph->isOrthoProjection(), false); |
| 142 | QCOMPARE(m_graph->selectedElement(), QAbstract3DGraph::ElementNone); |
| 143 | QCOMPARE(m_graph->aspectRatio(), 2.0); |
| 144 | QCOMPARE(m_graph->optimizationHints(), QAbstract3DGraph::OptimizationDefault); |
| 145 | QCOMPARE(m_graph->isPolar(), false); |
| 146 | QCOMPARE(m_graph->radialLabelOffset(), 1.0); |
| 147 | QCOMPARE(m_graph->horizontalAspectRatio(), 0.0); |
| 148 | QCOMPARE(m_graph->isReflection(), false); |
| 149 | QCOMPARE(m_graph->reflectivity(), 0.5); |
| 150 | QCOMPARE(m_graph->locale(), QLocale("C" )); |
| 151 | QCOMPARE(m_graph->queriedGraphPosition(), QVector3D(0, 0, 0)); |
| 152 | QCOMPARE(m_graph->margin(), -1.0); |
| 153 | } |
| 154 | |
| 155 | void tst_bars::initializeProperties() |
| 156 | { |
| 157 | QVERIFY(m_graph); |
| 158 | |
| 159 | m_graph->setMultiSeriesUniform(true); |
| 160 | m_graph->setBarThickness(0.2f); |
| 161 | m_graph->setBarSpacing(QSizeF(0.1f, 0.1f)); |
| 162 | m_graph->setBarSpacingRelative(false); |
| 163 | m_graph->setFloorLevel(1.0f); |
| 164 | |
| 165 | QCOMPARE(m_graph->isMultiSeriesUniform(), true); |
| 166 | QCOMPARE(m_graph->barThickness(), 0.2f); |
| 167 | QCOMPARE(m_graph->barSpacing(), QSizeF(0.1f, 0.1f)); |
| 168 | QCOMPARE(m_graph->isBarSpacingRelative(), false); |
| 169 | QCOMPARE(m_graph->floorLevel(), 1.0f); |
| 170 | |
| 171 | Q3DTheme *theme = new Q3DTheme(Q3DTheme::ThemeDigia); |
| 172 | m_graph->setActiveTheme(theme); |
| 173 | m_graph->setSelectionMode(QAbstract3DGraph::SelectionItem | QAbstract3DGraph::SelectionRow | QAbstract3DGraph::SelectionSlice); |
| 174 | m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualitySoftHigh); |
| 175 | QCOMPARE(m_graph->shadowQuality(), QAbstract3DGraph::ShadowQualitySoftHigh); |
| 176 | m_graph->setMeasureFps(true); |
| 177 | m_graph->setOrthoProjection(true); |
| 178 | m_graph->setAspectRatio(1.0); |
| 179 | m_graph->setOptimizationHints(QAbstract3DGraph::OptimizationStatic); |
| 180 | m_graph->setPolar(true); |
| 181 | m_graph->setRadialLabelOffset(0.1f); |
| 182 | m_graph->setHorizontalAspectRatio(1.0); |
| 183 | m_graph->setReflection(true); |
| 184 | m_graph->setReflectivity(0.1); |
| 185 | m_graph->setLocale(QLocale("FI" )); |
| 186 | m_graph->setMargin(1.0); |
| 187 | |
| 188 | QCOMPARE(m_graph->activeTheme()->type(), Q3DTheme::ThemeDigia); |
| 189 | QCOMPARE(m_graph->selectionMode(), QAbstract3DGraph::SelectionItem | QAbstract3DGraph::SelectionRow | QAbstract3DGraph::SelectionSlice); |
| 190 | QCOMPARE(m_graph->shadowQuality(), QAbstract3DGraph::ShadowQualityNone); // Ortho disables shadows |
| 191 | QCOMPARE(m_graph->measureFps(), true); |
| 192 | QCOMPARE(m_graph->isOrthoProjection(), true); |
| 193 | QCOMPARE(m_graph->aspectRatio(), 1.0); |
| 194 | QCOMPARE(m_graph->optimizationHints(), QAbstract3DGraph::OptimizationStatic); |
| 195 | QCOMPARE(m_graph->isPolar(), true); |
| 196 | QCOMPARE(m_graph->radialLabelOffset(), 0.1f); |
| 197 | QCOMPARE(m_graph->horizontalAspectRatio(), 1.0); |
| 198 | QCOMPARE(m_graph->isReflection(), true); |
| 199 | QCOMPARE(m_graph->reflectivity(), 0.1); |
| 200 | QCOMPARE(m_graph->locale(), QLocale("FI" )); |
| 201 | QCOMPARE(m_graph->margin(), 1.0); |
| 202 | } |
| 203 | |
| 204 | void tst_bars::invalidProperties() |
| 205 | { |
| 206 | m_graph->setSelectionMode(QAbstract3DGraph::SelectionColumn | QAbstract3DGraph::SelectionRow | QAbstract3DGraph::SelectionSlice); |
| 207 | m_graph->setAspectRatio(-1.0); |
| 208 | m_graph->setHorizontalAspectRatio(-1.0); |
| 209 | m_graph->setReflectivity(-1.0); |
| 210 | m_graph->setLocale(QLocale("XX" )); |
| 211 | |
| 212 | QCOMPARE(m_graph->selectionMode(), QAbstract3DGraph::SelectionItem); |
| 213 | QCOMPARE(m_graph->aspectRatio(), -1.0/*2.0*/); // TODO: Fix once QTRD-3367 is done |
| 214 | QCOMPARE(m_graph->horizontalAspectRatio(), -1.0/*0.0*/); // TODO: Fix once QTRD-3367 is done |
| 215 | QCOMPARE(m_graph->reflectivity(), -1.0/*0.5*/); // TODO: Fix once QTRD-3367 is done |
| 216 | QCOMPARE(m_graph->locale(), QLocale("C" )); |
| 217 | } |
| 218 | |
| 219 | void tst_bars::addSeries() |
| 220 | { |
| 221 | QBar3DSeries *series = newSeries(); |
| 222 | |
| 223 | m_graph->addSeries(series); |
| 224 | |
| 225 | QCOMPARE(m_graph->seriesList().length(), 1); |
| 226 | QVERIFY(!m_graph->selectedSeries()); |
| 227 | QCOMPARE(m_graph->primarySeries(), series); |
| 228 | } |
| 229 | |
| 230 | void tst_bars::addMultipleSeries() |
| 231 | { |
| 232 | QBar3DSeries *series = newSeries(); |
| 233 | QBar3DSeries *series2 = newSeries(); |
| 234 | QBar3DSeries *series3 = newSeries(); |
| 235 | |
| 236 | m_graph->addSeries(series); |
| 237 | m_graph->addSeries(series: series2); |
| 238 | m_graph->addSeries(series: series3); |
| 239 | |
| 240 | QCOMPARE(m_graph->seriesList().length(), 3); |
| 241 | QCOMPARE(m_graph->primarySeries(), series); |
| 242 | |
| 243 | m_graph->setPrimarySeries(series2); |
| 244 | |
| 245 | QCOMPARE(m_graph->primarySeries(), series2); |
| 246 | } |
| 247 | |
| 248 | void tst_bars::selectSeries() |
| 249 | { |
| 250 | QBar3DSeries *series = newSeries(); |
| 251 | |
| 252 | m_graph->addSeries(series); |
| 253 | m_graph->primarySeries()->setSelectedBar(QPoint(0, 0)); |
| 254 | |
| 255 | QCOMPARE(m_graph->seriesList().length(), 1); |
| 256 | QCOMPARE(m_graph->selectedSeries(), series); |
| 257 | |
| 258 | m_graph->clearSelection(); |
| 259 | QVERIFY(!m_graph->selectedSeries()); |
| 260 | } |
| 261 | |
| 262 | void tst_bars::removeSeries() |
| 263 | { |
| 264 | QBar3DSeries *series = newSeries(); |
| 265 | |
| 266 | m_graph->addSeries(series); |
| 267 | m_graph->removeSeries(series); |
| 268 | QCOMPARE(m_graph->seriesList().length(), 0); |
| 269 | delete series; |
| 270 | } |
| 271 | |
| 272 | void tst_bars::removeMultipleSeries() |
| 273 | { |
| 274 | QBar3DSeries *series = newSeries(); |
| 275 | QBar3DSeries *series2 = newSeries(); |
| 276 | QBar3DSeries *series3 = newSeries(); |
| 277 | |
| 278 | m_graph->addSeries(series); |
| 279 | m_graph->addSeries(series: series2); |
| 280 | m_graph->addSeries(series: series3); |
| 281 | |
| 282 | m_graph->primarySeries()->setSelectedBar(QPoint(0, 0)); |
| 283 | QCOMPARE(m_graph->selectedSeries(), series); |
| 284 | |
| 285 | m_graph->removeSeries(series); |
| 286 | QCOMPARE(m_graph->seriesList().length(), 2); |
| 287 | QCOMPARE(m_graph->primarySeries(), series2); |
| 288 | QVERIFY(!m_graph->selectedSeries()); |
| 289 | |
| 290 | m_graph->removeSeries(series: series2); |
| 291 | QCOMPARE(m_graph->seriesList().length(), 1); |
| 292 | QCOMPARE(m_graph->primarySeries(), series3); |
| 293 | |
| 294 | m_graph->removeSeries(series: series3); |
| 295 | QCOMPARE(m_graph->seriesList().length(), 0); |
| 296 | |
| 297 | delete series; |
| 298 | delete series2; |
| 299 | delete series3; |
| 300 | } |
| 301 | |
| 302 | // The following tests are not required for scatter or surface, as they are handled identically |
| 303 | void tst_bars::addInputHandler() |
| 304 | { |
| 305 | Q3DInputHandler *handler = new Q3DInputHandler(); |
| 306 | QTouch3DInputHandler *handler2 = new QTouch3DInputHandler(); |
| 307 | QAbstract3DInputHandler *initialHandler = m_graph->activeInputHandler(); |
| 308 | |
| 309 | m_graph->addInputHandler(inputHandler: handler); |
| 310 | m_graph->addInputHandler(inputHandler: handler2); |
| 311 | |
| 312 | QCOMPARE(m_graph->inputHandlers().length(), 3); // Default, as it is still active, plus added ones |
| 313 | QCOMPARE(m_graph->activeInputHandler(), initialHandler); |
| 314 | m_graph->setActiveInputHandler(handler2); |
| 315 | QCOMPARE(m_graph->activeInputHandler(), handler2); |
| 316 | |
| 317 | m_graph->setActiveInputHandler(NULL); |
| 318 | QVERIFY(!m_graph->activeInputHandler()); |
| 319 | QCOMPARE(m_graph->inputHandlers().length(), 2); |
| 320 | } |
| 321 | |
| 322 | void tst_bars::removeInputHandler() |
| 323 | { |
| 324 | Q3DInputHandler *handler = new Q3DInputHandler(); |
| 325 | QTouch3DInputHandler *handler2 = new QTouch3DInputHandler(); |
| 326 | |
| 327 | m_graph->addInputHandler(inputHandler: handler); |
| 328 | m_graph->addInputHandler(inputHandler: handler2); |
| 329 | |
| 330 | m_graph->setActiveInputHandler(handler2); |
| 331 | QCOMPARE(m_graph->inputHandlers().length(), 2); // Default handler removed by previous call |
| 332 | QCOMPARE(m_graph->activeInputHandler(), handler2); |
| 333 | m_graph->releaseInputHandler(inputHandler: handler2); |
| 334 | QCOMPARE(m_graph->inputHandlers().length(), 1); |
| 335 | m_graph->releaseInputHandler(inputHandler: handler); |
| 336 | QCOMPARE(m_graph->inputHandlers().length(), 0); |
| 337 | |
| 338 | delete handler2; |
| 339 | delete handler; |
| 340 | } |
| 341 | |
| 342 | void tst_bars::addTheme() |
| 343 | { |
| 344 | Q3DTheme *theme = new Q3DTheme(Q3DTheme::ThemeDigia); |
| 345 | Q3DTheme *theme2 = new Q3DTheme(); |
| 346 | Q3DTheme *initialTheme = m_graph->activeTheme(); |
| 347 | m_graph->addTheme(theme); |
| 348 | m_graph->addTheme(theme: theme2); |
| 349 | |
| 350 | QCOMPARE(m_graph->themes().length(), 3); // Default, plus added ones |
| 351 | QCOMPARE(m_graph->activeTheme(), initialTheme); |
| 352 | m_graph->setActiveTheme(theme2); |
| 353 | QCOMPARE(m_graph->activeTheme(), theme2); |
| 354 | } |
| 355 | |
| 356 | void tst_bars::removeTheme() |
| 357 | { |
| 358 | Q3DTheme *theme = new Q3DTheme(Q3DTheme::ThemeDigia); |
| 359 | Q3DTheme *theme2 = new Q3DTheme(); |
| 360 | m_graph->addTheme(theme); |
| 361 | m_graph->addTheme(theme: theme2); |
| 362 | |
| 363 | m_graph->setActiveTheme(theme2); |
| 364 | QCOMPARE(m_graph->activeTheme(), theme2); |
| 365 | m_graph->releaseTheme(theme: theme2); |
| 366 | QCOMPARE(m_graph->themes().length(), 2); |
| 367 | m_graph->releaseTheme(theme); |
| 368 | QCOMPARE(m_graph->themes().length(), 1); // Default theme remains |
| 369 | |
| 370 | delete theme2; |
| 371 | delete theme; |
| 372 | } |
| 373 | |
| 374 | void tst_bars::addCustomItem() |
| 375 | { |
| 376 | QCustom3DItem *item = new QCustom3DItem(); |
| 377 | QCustom3DItem *item2 = new QCustom3DItem(); |
| 378 | |
| 379 | m_graph->addCustomItem(item); |
| 380 | QCOMPARE(m_graph->customItems().length(), 1); |
| 381 | m_graph->addCustomItem(item: item2); |
| 382 | QCOMPARE(m_graph->customItems().length(), 2); |
| 383 | } |
| 384 | |
| 385 | void tst_bars::removeCustomItem() |
| 386 | { |
| 387 | QCustom3DItem *item = new QCustom3DItem(); |
| 388 | QCustom3DItem *item2 = new QCustom3DItem(); |
| 389 | QCustom3DItem *item3 = new QCustom3DItem(); |
| 390 | item3->setPosition(QVector3D(1, 1, 1)); |
| 391 | |
| 392 | m_graph->addCustomItem(item); |
| 393 | m_graph->addCustomItem(item: item2); |
| 394 | m_graph->addCustomItem(item: item3); |
| 395 | |
| 396 | m_graph->releaseCustomItem(item); |
| 397 | QCOMPARE(m_graph->customItems().length(), 2); |
| 398 | m_graph->removeCustomItem(item: item2); |
| 399 | QCOMPARE(m_graph->customItems().length(), 1); |
| 400 | m_graph->addCustomItem(item); |
| 401 | m_graph->removeCustomItemAt(position: QVector3D(1, 1, 1)); |
| 402 | QCOMPARE(m_graph->customItems().length(), 1); |
| 403 | m_graph->removeCustomItems(); |
| 404 | QCOMPARE(m_graph->customItems().length(), 0); |
| 405 | } |
| 406 | |
| 407 | void tst_bars::renderToImage() |
| 408 | { |
| 409 | /* Crashes on some CI machines using Mesa, but can't repro locally, so commented out for now. |
| 410 | m_graph->addSeries(newSeries()); |
| 411 | |
| 412 | QImage image = m_graph->renderToImage(); |
| 413 | QCOMPARE(image.size(), m_graph->size()); |
| 414 | |
| 415 | image = m_graph->renderToImage(8); |
| 416 | QCOMPARE(image.size(), m_graph->size()); |
| 417 | |
| 418 | image = m_graph->renderToImage(4, QSize(300, 300)); |
| 419 | QCOMPARE(image.size(), QSize(300, 300)); |
| 420 | */ |
| 421 | } |
| 422 | |
| 423 | QTEST_MAIN(tst_bars) |
| 424 | #include "tst_bars.moc" |
| 425 | |