| 1 | // Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <QtTest/QSignalSpy> |
| 6 | #include <Qt3DCore/private/qnode_p.h> |
| 7 | #include <Qt3DCore/private/qscene_p.h> |
| 8 | #include <Qt3DRender/QScreenRayCaster> |
| 9 | |
| 10 | #include "testarbiter.h" |
| 11 | |
| 12 | class MyRayCaster : public Qt3DRender::QScreenRayCaster |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | public: |
| 16 | MyRayCaster(Qt3DCore::QNode *parent = nullptr) |
| 17 | : Qt3DRender::QScreenRayCaster(parent) |
| 18 | { |
| 19 | qRegisterMetaType<Qt3DRender::QAbstractRayCaster::Hits>("Hits"); |
| 20 | } |
| 21 | |
| 22 | private: |
| 23 | friend class tst_RayCaster; |
| 24 | }; |
| 25 | |
| 26 | class tst_QScreenRayCaster : public QObject |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | public: |
| 30 | tst_QScreenRayCaster() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | ~tst_QScreenRayCaster() |
| 35 | { |
| 36 | QMetaObject::invokeMethod(this, "_q_cleanup", Qt::DirectConnection); |
| 37 | } |
| 38 | |
| 39 | private Q_SLOTS: |
| 40 | |
| 41 | void checkState() |
| 42 | { |
| 43 | // GIVEN |
| 44 | QScopedPointer<Qt3DRender::QScreenRayCaster> rayCaster(new Qt3DRender::QScreenRayCaster()); |
| 45 | |
| 46 | QVERIFY(!rayCaster->isEnabled()); |
| 47 | QCOMPARE(rayCaster->runMode(), Qt3DRender::QAbstractRayCaster::SingleShot); |
| 48 | |
| 49 | // WHEN |
| 50 | rayCaster->trigger(); |
| 51 | |
| 52 | // THEN |
| 53 | QVERIFY(rayCaster->isEnabled()); |
| 54 | |
| 55 | // WHEN |
| 56 | rayCaster->setEnabled(false); |
| 57 | rayCaster->trigger(QPoint(200, 200)); |
| 58 | |
| 59 | // THEN |
| 60 | QVERIFY(rayCaster->isEnabled()); |
| 61 | QCOMPARE(rayCaster->position(), QPoint(200, 200)); |
| 62 | } |
| 63 | |
| 64 | void checkPropertyUpdates() |
| 65 | { |
| 66 | // GIVEN |
| 67 | TestArbiter arbiter; |
| 68 | QScopedPointer<Qt3DRender::QScreenRayCaster> rayCaster(new Qt3DRender::QScreenRayCaster()); |
| 69 | arbiter.setArbiterOnNode(rayCaster.data()); |
| 70 | |
| 71 | // WHEN |
| 72 | rayCaster->setPosition({200, 200}); |
| 73 | QCoreApplication::processEvents(); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 77 | QCOMPARE(arbiter.dirtyNodes().front(), rayCaster.data()); |
| 78 | |
| 79 | arbiter.clear(); |
| 80 | } |
| 81 | |
| 82 | void checkBackendUpdates_data() |
| 83 | { |
| 84 | QTest::addColumn<QByteArray>("signalPrototype"); |
| 85 | QTest::addColumn<QByteArray>("propertyName"); |
| 86 | |
| 87 | QTest::newRow("hits") |
| 88 | << QByteArray(SIGNAL(hitsChanged(Hits))) |
| 89 | << QByteArrayLiteral("hits"); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | QTEST_MAIN(tst_QScreenRayCaster) |
| 94 | |
| 95 | #include "tst_qscreenraycaster.moc" |