summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Krus <mike.krus@kdab.com>2018-01-18 08:36:38 +0000
committerMike Krus <mike.krus@kdab.com>2018-02-01 22:21:51 +0000
commit53ee42eca2faf76a61a591e5251602f64e00bc67 (patch)
tree724fafce547b35e6315d2f3b0c27ef77297b921d
parent2161118a82d0ddb91cbe78cdf530970409a9d204 (diff)
Unit tests for QRayCaster and backend node
Change-Id: Ic3b9676497086b228e467bae3eebeef5c5beb8ce Reviewed-by: Paul Lemire <paul.lemire@kdab.com> Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r--tests/auto/render/qraycaster/qraycaster.pro11
-rw-r--r--tests/auto/render/qraycaster/tst_qraycaster.cpp156
-rw-r--r--tests/auto/render/raycaster/raycaster.pro12
-rw-r--r--tests/auto/render/raycaster/tst_raycaster.cpp130
-rw-r--r--tests/auto/render/render.pro2
5 files changed, 311 insertions, 0 deletions
diff --git a/tests/auto/render/qraycaster/qraycaster.pro b/tests/auto/render/qraycaster/qraycaster.pro
new file mode 100644
index 000000000..035c7a77b
--- /dev/null
+++ b/tests/auto/render/qraycaster/qraycaster.pro
@@ -0,0 +1,11 @@
+TEMPLATE = app
+
+TARGET = tst_qraycaster
+
+QT += core-private 3dcore 3dcore-private 3drender 3drender-private testlib
+
+CONFIG += testcase
+
+SOURCES += tst_qraycaster.cpp
+
+include(../../core/common/common.pri)
diff --git a/tests/auto/render/qraycaster/tst_qraycaster.cpp b/tests/auto/render/qraycaster/tst_qraycaster.cpp
new file mode 100644
index 000000000..021610846
--- /dev/null
+++ b/tests/auto/render/qraycaster/tst_qraycaster.cpp
@@ -0,0 +1,156 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QTest>
+#include <QtTest/QSignalSpy>
+#include <Qt3DCore/private/qnode_p.h>
+#include <Qt3DCore/private/qscene_p.h>
+#include <Qt3DRender/QRayCaster>
+
+#include "testpostmanarbiter.h"
+
+class MyRayCaster : public Qt3DRender::QRayCaster
+{
+ Q_OBJECT
+public:
+ MyRayCaster(Qt3DCore::QNode *parent = nullptr)
+ : Qt3DRender::QRayCaster(parent)
+ {
+ qRegisterMetaType<Qt3DRender::QAbstractRayCaster::Hits>("Hits");
+ }
+
+ void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) final
+ {
+ Qt3DRender::QRayCaster::sceneChangeEvent(change);
+ }
+
+private:
+ friend class tst_RayCaster;
+};
+
+// We need to call QNode::clone which is protected
+// So we sublcass QNode instead of QObject
+class tst_QRayCaster : public Qt3DCore::QNode
+{
+ Q_OBJECT
+public:
+ tst_QRayCaster()
+ {
+ }
+
+ ~tst_QRayCaster()
+ {
+ QMetaObject::invokeMethod(this, "_q_cleanup", Qt::DirectConnection);
+ }
+
+private Q_SLOTS:
+
+ void checkState()
+ {
+ // GIVEN
+ QScopedPointer<Qt3DRender::QRayCaster> rayCaster(new Qt3DRender::QRayCaster());
+
+ QVERIFY(!rayCaster->isEnabled());
+ QVERIFY(rayCaster->direction().length() > 0.);
+ QCOMPARE(rayCaster->runMode(), Qt3DRender::QAbstractRayCaster::SingleShot);
+
+ // WHEN
+ rayCaster->trigger();
+
+ // THEN
+ QVERIFY(rayCaster->isEnabled());
+
+ // WHEN
+ rayCaster->setEnabled(false);
+ rayCaster->trigger(QVector3D(1., 2., 3.), QVector3D(1., 0., 0.), 10.f);
+
+ // THEN
+ QVERIFY(rayCaster->isEnabled());
+ QCOMPARE(rayCaster->origin(), QVector3D(1., 2., 3.));
+ QCOMPARE(rayCaster->direction(), QVector3D(1., 0., 0.));
+ QCOMPARE(rayCaster->length(), 10.f);
+ }
+
+ void checkPropertyUpdates()
+ {
+ // GIVEN
+ TestArbiter arbiter;
+ QScopedPointer<Qt3DRender::QRayCaster> rayCaster(new Qt3DRender::QRayCaster());
+ arbiter.setArbiterOnNode(rayCaster.data());
+
+ // WHEN
+ rayCaster->setOrigin({1., 1., 1.});
+ QCoreApplication::processEvents();
+
+ // THEN
+ QCOMPARE(arbiter.events.size(), 1);
+ Qt3DCore::QPropertyUpdatedChangePtr change = arbiter.events.last().staticCast<Qt3DCore::QPropertyUpdatedChange>();
+ QCOMPARE(change->propertyName(), "origin");
+ QCOMPARE(change->value().value<QVector3D>(), QVector3D(1., 1., 1.));
+ QCOMPARE(change->type(), Qt3DCore::PropertyUpdated);
+
+ arbiter.events.clear();
+ }
+
+ void checkBackendUpdates_data()
+ {
+ QTest::addColumn<QByteArray>("signalPrototype");
+ QTest::addColumn<QByteArray>("propertyName");
+
+ QTest::newRow("hits")
+ << QByteArray(SIGNAL(hitsChanged(const Hits &)))
+ << QByteArrayLiteral("hits");
+ }
+
+ void checkBackendUpdates()
+ {
+ // GIVEN
+ QFETCH(QByteArray, signalPrototype);
+ QFETCH(QByteArray, propertyName);
+ QScopedPointer<MyRayCaster> rayCaster(new MyRayCaster());
+ QSignalSpy spy(rayCaster.data(), signalPrototype.constData());
+ Qt3DRender::QRayCaster::Hits hits;
+
+ // WHEN
+ // Create Backend Change and distribute it to frontend node
+ Qt3DCore::QPropertyUpdatedChangePtr e(new Qt3DCore::QPropertyUpdatedChange(rayCaster->id()));
+ e->setPropertyName(propertyName.constData());
+ QVariant v;
+ v.setValue<Qt3DRender::QRayCaster::Hits>(hits);
+ e->setValue(v);
+ rayCaster->sceneChangeEvent(e);
+
+ // THEN
+ // Check that the QRayCaster triggers the expected signal
+ QCOMPARE(spy.count(), 1);
+ }
+};
+
+QTEST_MAIN(tst_QRayCaster)
+
+#include "tst_qraycaster.moc"
diff --git a/tests/auto/render/raycaster/raycaster.pro b/tests/auto/render/raycaster/raycaster.pro
new file mode 100644
index 000000000..798cc9705
--- /dev/null
+++ b/tests/auto/render/raycaster/raycaster.pro
@@ -0,0 +1,12 @@
+TEMPLATE = app
+
+TARGET = raycaster
+
+QT += 3dcore 3dcore-private 3drender 3drender-private testlib
+
+CONFIG += testcase
+
+SOURCES += tst_raycaster.cpp
+
+include(../../core/common/common.pri)
+include(../commons/commons.pri)
diff --git a/tests/auto/render/raycaster/tst_raycaster.cpp b/tests/auto/render/raycaster/tst_raycaster.cpp
new file mode 100644
index 000000000..4d30c6d45
--- /dev/null
+++ b/tests/auto/render/raycaster/tst_raycaster.cpp
@@ -0,0 +1,130 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QTest>
+#include <qbackendnodetester.h>
+#include <Qt3DRender/private/raycaster_p.h>
+#include <Qt3DRender/qpickevent.h>
+#include <Qt3DRender/qraycaster.h>
+#include <Qt3DCore/private/qbackendnode_p.h>
+#include <Qt3DCore/qpropertyupdatedchange.h>
+#include "testpostmanarbiter.h"
+#include "testrenderer.h"
+
+class tst_RayCaster : public Qt3DCore::QBackendNodeTester
+{
+ Q_OBJECT
+private Q_SLOTS:
+
+ void checkPeerPropertyMirroring()
+ {
+ // GIVEN
+ Qt3DRender::Render::RayCaster rayCaster;
+ Qt3DRender::QRayCaster caster;
+ caster.setRunMode(Qt3DRender::QRayCaster::Continuous);
+ caster.setOrigin(QVector3D(1., 2., 3.));
+ caster.setDirection(QVector3D(1., 0., 0.));
+ caster.setLength(42.f);
+
+ // WHEN
+ simulateInitialization(&caster, &rayCaster);
+
+ // THEN
+ QVERIFY(!rayCaster.peerId().isNull());
+ QCOMPARE(rayCaster.runMode(), Qt3DRender::QRayCaster::Continuous);
+ QCOMPARE(rayCaster.origin(), QVector3D(1., 2., 3.));
+ QCOMPARE(rayCaster.direction(), QVector3D(1., 0., 0.));
+ QCOMPARE(rayCaster.length(), 42.f);
+ }
+
+ void checkInitialAndCleanedUpState()
+ {
+ // GIVEN
+ Qt3DRender::Render::RayCaster rayCaster;
+
+ // THEN
+ QVERIFY(rayCaster.peerId().isNull());
+ QCOMPARE(rayCaster.runMode(), Qt3DRender::QRayCaster::SingleShot);
+
+ // GIVEN
+ Qt3DRender::QRayCaster caster;
+ caster.setRunMode(Qt3DRender::QRayCaster::Continuous);
+
+ // WHEN
+ simulateInitialization(&caster, &rayCaster);
+ rayCaster.cleanup();
+
+ // THEN
+ QCOMPARE(rayCaster.runMode(), Qt3DRender::QRayCaster::SingleShot);
+ }
+
+ void checkPropertyChanges()
+ {
+ // GIVEN
+ TestRenderer renderer;
+ {
+ Qt3DRender::Render::RayCaster rayCaster;
+ rayCaster.setRenderer(&renderer);
+
+ // WHEN
+ Qt3DCore::QPropertyUpdatedChangePtr updateChange(new Qt3DCore::QPropertyUpdatedChange(Qt3DCore::QNodeId()));
+ updateChange->setValue(Qt3DRender::QRayCaster::Continuous);
+ updateChange->setPropertyName("runMode");
+ rayCaster.sceneChangeEvent(updateChange);
+
+ // THEN
+ QCOMPARE(rayCaster.runMode(), Qt3DRender::QRayCaster::Continuous);
+ QVERIFY(renderer.dirtyBits() != 0);
+ }
+ }
+
+ void checkBackendPropertyNotifications()
+ {
+ // GIVEN
+ TestArbiter arbiter;
+ Qt3DRender::Render::RayCaster rayCaster;
+ Qt3DCore::QBackendNodePrivate::get(&rayCaster)->setArbiter(&arbiter);
+ Qt3DRender::QAbstractRayCaster::Hits hits;
+
+ // WHEN
+ rayCaster.dispatchHits(hits);
+
+ // THEN
+ QCOMPARE(arbiter.events.count(), 2);
+ Qt3DCore::QPropertyUpdatedChangePtr change = arbiter.events.first().staticCast<Qt3DCore::QPropertyUpdatedChange>();
+ QCOMPARE(change->propertyName(), "hits");
+ QVERIFY(!rayCaster.isEnabled());
+
+ arbiter.events.clear();
+ }
+};
+
+
+QTEST_APPLESS_MAIN(tst_RayCaster)
+
+#include "tst_raycaster.moc"
diff --git a/tests/auto/render/render.pro b/tests/auto/render/render.pro
index da01dd95f..1918169ca 100644
--- a/tests/auto/render/render.pro
+++ b/tests/auto/render/render.pro
@@ -110,6 +110,8 @@ qtConfig(private_tests) {
proximityfiltering \
qblitframebuffer \
blitframebuffer \
+ qraycaster \
+ raycaster \
qcamera
QT_FOR_CONFIG = 3dcore-private