diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2015-10-20 16:17:11 +0200 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2015-10-24 14:05:41 +0000 |
commit | 25f0914cc2bcc83bba9bdb8344971cfa6d0afe81 (patch) | |
tree | c2f5a40771b4ec14337664bee75ec293a7e2cbb9 /tests | |
parent | 1f58bd56479dff5ccaad7446ae92a27e60ec0b73 (diff) |
Add Picking unit tests
Change-Id: Ide5ea4fe6321e02c0066cdd57488d2a6713f0dc6
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/render/picking/picking.pro | 9 | ||||
-rw-r--r-- | tests/auto/render/picking/tst_picking.cpp | 280 | ||||
-rw-r--r-- | tests/auto/render/render.pro | 3 |
3 files changed, 291 insertions, 1 deletions
diff --git a/tests/auto/render/picking/picking.pro b/tests/auto/render/picking/picking.pro new file mode 100644 index 000000000..4745033d3 --- /dev/null +++ b/tests/auto/render/picking/picking.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +TARGET = tst_picking + +QT += core-private 3dcore 3dcore-private 3drender 3drender-private testlib + +CONFIG += testcase + +SOURCES += tst_picking.cpp diff --git a/tests/auto/render/picking/tst_picking.cpp b/tests/auto/render/picking/tst_picking.cpp new file mode 100644 index 000000000..90d059cd2 --- /dev/null +++ b/tests/auto/render/picking/tst_picking.cpp @@ -0,0 +1,280 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <Qt3DCore/qentity.h> +#include <Qt3DCore/qtransform.h> +#include <Qt3DCore/QScenePropertyChange> +#include <Qt3DCore/qtranslatetransform.h> +#include <Qt3DCore/qboundingvolumeprovider.h> +#include <Qt3DRender/private/pickboundingvolumejob_p.h> +#include <Qt3DRender/private/objectpicker_p.h> +#include <Qt3DRender/qobjectpicker.h> +#include <Qt3DRender/qspheremesh.h> +#include <Qt3DRender/qattribute.h> +#include <Qt3DRender/qbuffer.h> +#include <Qt3DRender/qbufferfunctor.h> +#include <Qt3DRender/qspheregeometry.h> +#include <Qt3DRender/qpickevent.h> +#include <Qt3DCore/qbackendscenepropertychange.h> + +using namespace Qt3DCore; +using namespace Qt3DRender; +using namespace Qt3DRender::Render; + +class MyObjectPicker : public Qt3DRender::QObjectPicker +{ + Q_OBJECT +public: + MyObjectPicker(Qt3DCore::QNode *parent = Q_NULLPTR) + : Qt3DRender::QObjectPicker(parent) + {} + + void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) Q_DECL_FINAL + { + Qt3DRender::QObjectPicker::sceneChangeEvent(change); + } +}; + +class PickableEntity : public QEntity +{ + Q_OBJECT +public: + explicit PickableEntity(const QVector3D &position, float radius, QEntity *parent = Q_NULLPTR) + : QEntity(parent) + , picker(new MyObjectPicker(this)) + , mesh(new QSphereMesh(this)) + , transform(new Qt3DCore::QTransform(this)) + , translate(new QTranslateTransform(transform)) + , acceptsEvents(true) + , pressedCalled(0) + , releasedCalled(0) + , clickedCalled(0) + { + mesh->setRadius(radius); + QSphereGeometry *g = static_cast<QSphereGeometry *>(mesh->geometry()); + QAttribute *positionAttr = static_cast<QAttribute *>(g->attributes().first()); + Qt3DRender::QBuffer *vertexBuffer = static_cast<Qt3DRender::QBuffer *>(positionAttr->buffer()); + + // Load the geometry + const QByteArray data = (*vertexBuffer->bufferFunctor())(); + vertexBuffer->setData(data); + + picker->setPickAttribute(positionAttr); + + translate->setTranslation(position); + transform->addTransform(translate); + + addComponent(picker); + addComponent(mesh); + addComponent(transform); + + QObject::connect(picker, &QObjectPicker::pressed, this, &PickableEntity::onPressed); + QObject::connect(picker, &QObjectPicker::released, this, &PickableEntity::onReleased); + QObject::connect(picker, &QObjectPicker::clicked, this, &PickableEntity::onClicked); + } + + MyObjectPicker *picker; + QSphereMesh *mesh; + Qt3DCore::QTransform *transform; + QTranslateTransform *translate; + + bool acceptsEvents; + int pressedCalled; + int releasedCalled; + int clickedCalled; + +private Q_SLOTS: + void onPressed(QPickEvent *e) + { + e->setAccepted(acceptsEvents); + ++pressedCalled; + } + + void onReleased(QPickEvent *e) + { + e->setAccepted(acceptsEvents); + if (acceptsEvents) + ++releasedCalled; + } + + void onClicked(QPickEvent *e) + { + e->setAccepted(acceptsEvents); + ++clickedCalled; + } +}; + +class tst_Picking : public QObject +{ + Q_OBJECT +public: + tst_Picking() {} + ~tst_Picking() {} + +private Q_SLOTS: + void testEventPressedAcceptPropagation() + { + // GIVEN + PickableEntity root(QVector3D(), 5.0f); + PickableEntity child1(QVector3D(), 5.0f, &root); + PickableEntity child2(QVector3D(), 5.0f, &root); + PickableEntity child11(QVector3D(), 5.0f, &child1); + + // WHEN + Qt3DCore::QBackendScenePropertyChangePtr e(new Qt3DCore::QBackendScenePropertyChange(Qt3DCore::NodeUpdated, child11.id())); + e->setPropertyName("pressed"); + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.pressedCalled, 0); + QCOMPARE(child1.pressedCalled, 0); + QCOMPARE(child2.pressedCalled, 0); + QCOMPARE(child11.pressedCalled, 1); + + // WHEN + child11.pressedCalled = 0; + child11.acceptsEvents = false; + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.pressedCalled, 0); + QCOMPARE(child1.pressedCalled, 1); + QCOMPARE(child2.pressedCalled, 0); + QCOMPARE(child11.pressedCalled, 1); + + // WHEN + child1.acceptsEvents = false; + child1.pressedCalled = 0; + child11.acceptsEvents = false; + child11.pressedCalled = 0; + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.pressedCalled, 1); + QCOMPARE(child1.pressedCalled, 1); + QCOMPARE(child2.pressedCalled, 0); + QCOMPARE(child11.pressedCalled, 1); + } + + void testEventReleasedAcceptPropagation() + { + // GIVEN + PickableEntity root(QVector3D(), 5.0f); + PickableEntity child1(QVector3D(), 5.0f, &root); + PickableEntity child2(QVector3D(), 5.0f, &root); + PickableEntity child11(QVector3D(), 5.0f, &child1); + + // WHEN + Qt3DCore::QBackendScenePropertyChangePtr e(new Qt3DCore::QBackendScenePropertyChange(Qt3DCore::NodeUpdated, child11.id())); + e->setPropertyName("pressed"); + child11.picker->sceneChangeEvent(e); + e->setPropertyName("released"); + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.releasedCalled, 0); + QCOMPARE(child1.releasedCalled, 0); + QCOMPARE(child2.releasedCalled, 0); + QCOMPARE(child11.releasedCalled, 1); + + // WHEN + child11.releasedCalled = 0; + child11.pressedCalled = 0; + child11.acceptsEvents = false; + e->setPropertyName("pressed"); + child11.picker->sceneChangeEvent(e); + e->setPropertyName("released"); + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(child1.pressedCalled, 1); + QCOMPARE(child11.pressedCalled, 1); + + QCOMPARE(root.releasedCalled, 0); + QCOMPARE(child1.releasedCalled, 1); + QCOMPARE(child2.releasedCalled, 0); + QCOMPARE(child11.releasedCalled, 0); + } + + void testEventClickedAcceptPropagation() + { + // GIVEN + PickableEntity root(QVector3D(), 5.0f); + PickableEntity child1(QVector3D(), 5.0f, &root); + PickableEntity child2(QVector3D(), 5.0f, &root); + PickableEntity child11(QVector3D(), 5.0f, &child1); + + // WHEN + Qt3DCore::QBackendScenePropertyChangePtr e(new Qt3DCore::QBackendScenePropertyChange(Qt3DCore::NodeUpdated, child11.id())); + e->setPropertyName("clicked"); + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.clickedCalled, 0); + QCOMPARE(child1.clickedCalled, 0); + QCOMPARE(child2.clickedCalled, 0); + QCOMPARE(child11.clickedCalled, 1); + + // WHEN + child11.clickedCalled = 0; + child11.acceptsEvents = false; + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.clickedCalled, 0); + QCOMPARE(child1.clickedCalled, 1); + QCOMPARE(child2.clickedCalled, 0); + QCOMPARE(child11.clickedCalled, 1); + + // WHEN + child1.acceptsEvents = false; + child1.clickedCalled = 0; + child11.acceptsEvents = false; + child11.clickedCalled = 0; + child11.picker->sceneChangeEvent(e); + + // THEN + QCOMPARE(root.clickedCalled, 1); + QCOMPARE(child1.clickedCalled, 1); + QCOMPARE(child2.clickedCalled, 0); + QCOMPARE(child11.clickedCalled, 1); + } +}; + +QTEST_APPLESS_MAIN(tst_Picking) + +#include "tst_picking.moc" diff --git a/tests/auto/render/render.pro b/tests/auto/render/render.pro index e38b77c1a..2610dceb1 100644 --- a/tests/auto/render/render.pro +++ b/tests/auto/render/render.pro @@ -36,5 +36,6 @@ contains(QT_CONFIG, private_tests) { qviewport \ framegraphnode \ qobjectpicker \ - objectpicker + objectpicker \ + picking } |