1/****************************************************************************
2**
3** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QTest>
30#include <QtTest/QSignalSpy>
31#include <Qt3DCore/private/qnode_p.h>
32#include <Qt3DCore/private/qscene_p.h>
33#include <Qt3DRender/QScreenRayCaster>
34
35#include "testpostmanarbiter.h"
36
37class MyRayCaster : public Qt3DRender::QScreenRayCaster
38{
39 Q_OBJECT
40public:
41 MyRayCaster(Qt3DCore::QNode *parent = nullptr)
42 : Qt3DRender::QScreenRayCaster(parent)
43 {
44 qRegisterMetaType<Qt3DRender::QAbstractRayCaster::Hits>(typeName: "Hits");
45 }
46
47 void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) final
48 {
49 Qt3DRender::QScreenRayCaster::sceneChangeEvent(change);
50 }
51
52private:
53 friend class tst_RayCaster;
54};
55
56class tst_QScreenRayCaster : public QObject
57{
58 Q_OBJECT
59public:
60 tst_QScreenRayCaster()
61 {
62 }
63
64 ~tst_QScreenRayCaster()
65 {
66 QMetaObject::invokeMethod(obj: this, member: "_q_cleanup", type: Qt::DirectConnection);
67 }
68
69private Q_SLOTS:
70
71 void checkState()
72 {
73 // GIVEN
74 QScopedPointer<Qt3DRender::QScreenRayCaster> rayCaster(new Qt3DRender::QScreenRayCaster());
75
76 QVERIFY(!rayCaster->isEnabled());
77 QCOMPARE(rayCaster->runMode(), Qt3DRender::QAbstractRayCaster::SingleShot);
78
79 // WHEN
80 rayCaster->trigger();
81
82 // THEN
83 QVERIFY(rayCaster->isEnabled());
84
85 // WHEN
86 rayCaster->setEnabled(false);
87 rayCaster->trigger(position: QPoint(200, 200));
88
89 // THEN
90 QVERIFY(rayCaster->isEnabled());
91 QCOMPARE(rayCaster->position(), QPoint(200, 200));
92 }
93
94 void checkPropertyUpdates()
95 {
96 // GIVEN
97 TestArbiter arbiter;
98 QScopedPointer<Qt3DRender::QScreenRayCaster> rayCaster(new Qt3DRender::QScreenRayCaster());
99 arbiter.setArbiterOnNode(rayCaster.data());
100
101 // WHEN
102 rayCaster->setPosition({200, 200});
103 QCoreApplication::processEvents();
104
105 // THEN
106 QCOMPARE(arbiter.events.size(), 0);
107 QCOMPARE(arbiter.dirtyNodes.size(), 1);
108 QCOMPARE(arbiter.dirtyNodes.front(), rayCaster.data());
109
110 arbiter.dirtyNodes.clear();
111 }
112
113 void checkBackendUpdates_data()
114 {
115 QTest::addColumn<QByteArray>(name: "signalPrototype");
116 QTest::addColumn<QByteArray>(name: "propertyName");
117
118 QTest::newRow(dataTag: "hits")
119 << QByteArray(SIGNAL(hitsChanged(const Hits &)))
120 << QByteArrayLiteral("hits");
121 }
122};
123
124QTEST_MAIN(tst_QScreenRayCaster)
125
126#include "tst_qscreenraycaster.moc"
127

source code of qt3d/tests/auto/render/qscreenraycaster/tst_qscreenraycaster.cpp