1/****************************************************************************
2**
3** Copyright (C) 2015 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/QObjectPicker>
34#include <Qt3DRender/private/qobjectpicker_p.h>
35#include <Qt3DRender/QPickEvent>
36#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
37#include <Qt3DCore/qnodecreatedchange.h>
38#include "testpostmanarbiter.h"
39
40class MyObjectPicker : public Qt3DRender::QObjectPicker
41{
42 Q_OBJECT
43public:
44 MyObjectPicker(Qt3DCore::QNode *parent = nullptr)
45 : Qt3DRender::QObjectPicker(parent)
46 {}
47
48 void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) final
49 {
50 Qt3DRender::QObjectPicker::sceneChangeEvent(change);
51 }
52
53private:
54 friend class tst_ObjectPicker;
55
56};
57
58// We need to call QNode::clone which is protected
59// So we sublcass QNode instead of QObject
60class tst_QObjectPicker : public Qt3DCore::QNode
61{
62 Q_OBJECT
63public:
64 tst_QObjectPicker()
65 {
66 qRegisterMetaType<Qt3DRender::QPickEvent*>(typeName: "Qt3DRender::QPickEvent*");
67 }
68
69 ~tst_QObjectPicker()
70 {
71 QMetaObject::invokeMethod(obj: this, member: "_q_cleanup", type: Qt::DirectConnection);
72 }
73
74private Q_SLOTS:
75
76 void checkInitialState()
77 {
78 // GIVEN
79 Qt3DRender::QObjectPicker picker;
80
81 // THEN
82 QCOMPARE(picker.priority(), 0);
83 QCOMPARE(picker.isDragEnabled(), false);
84 QCOMPARE(picker.isHoverEnabled(), false);
85 }
86
87 void checkCreationData()
88 {
89 // GIVEN
90 Qt3DRender::QObjectPicker picker;
91
92 picker.setPriority(1584);
93 picker.setDragEnabled(true);
94 picker.setHoverEnabled(true);
95
96 // WHEN
97 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges;
98
99 {
100 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&picker);
101 creationChanges = creationChangeGenerator.creationChanges();
102 }
103
104 // THEN
105 {
106 QCOMPARE(creationChanges.size(), 1);
107
108 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QObjectPickerData>>(src: creationChanges.first());
109 const Qt3DRender::QObjectPickerData cloneData = creationChangeData->data;
110
111 QCOMPARE(cloneData.priority, 1584);
112 QCOMPARE(cloneData.hoverEnabled, true);
113 QCOMPARE(cloneData.dragEnabled, true);
114 QCOMPARE(picker.id(), creationChangeData->subjectId());
115 QCOMPARE(picker.isEnabled(), true);
116 QCOMPARE(picker.isEnabled(), creationChangeData->isNodeEnabled());
117 QCOMPARE(picker.metaObject(), creationChangeData->metaObject());
118 }
119
120 // WHEN
121 picker.setEnabled(false);
122
123 {
124 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&picker);
125 creationChanges = creationChangeGenerator.creationChanges();
126 }
127
128 // THEN
129 {
130 QCOMPARE(creationChanges.size(), 1);
131
132 const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QObjectPickerData>>(src: creationChanges.first());
133 const Qt3DRender::QObjectPickerData cloneData = creationChangeData->data;
134
135 QCOMPARE(cloneData.priority, 1584);
136 QCOMPARE(cloneData.hoverEnabled, true);
137 QCOMPARE(cloneData.dragEnabled, true);
138 QCOMPARE(picker.id(), creationChangeData->subjectId());
139 QCOMPARE(picker.isEnabled(), false);
140 QCOMPARE(picker.isEnabled(), creationChangeData->isNodeEnabled());
141 QCOMPARE(picker.metaObject(), creationChangeData->metaObject());
142 }
143 }
144
145 void checkPropertyUpdate()
146 {
147 // GIVEN
148 TestArbiter arbiter;
149 Qt3DRender::QObjectPicker picker;
150 arbiter.setArbiterOnNode(&picker);
151
152 {
153 {
154 // WHEN
155 picker.setPriority(883);
156 QCoreApplication::processEvents();
157
158 // THEN
159 QCOMPARE(arbiter.events.size(), 0);
160 QCOMPARE(arbiter.dirtyNodes.size(), 1);
161 QCOMPARE(arbiter.dirtyNodes.front(), &picker);
162
163 arbiter.dirtyNodes.clear();
164 }
165
166 {
167 // WHEN
168 picker.setPriority(883);
169 QCoreApplication::processEvents();
170
171 // THEN
172 QCOMPARE(arbiter.events.size(), 0);
173 QCOMPARE(arbiter.dirtyNodes.size(), 0);
174 }
175 }
176 {
177 {
178 // WHEN
179 picker.setDragEnabled(true);
180 QCoreApplication::processEvents();
181
182 // THEN
183 QCOMPARE(arbiter.events.size(), 0);
184 QCOMPARE(arbiter.dirtyNodes.size(), 1);
185 QCOMPARE(arbiter.dirtyNodes.front(), &picker);
186
187 arbiter.dirtyNodes.clear();
188 }
189
190 {
191 // WHEN
192 picker.setDragEnabled(true);
193 QCoreApplication::processEvents();
194
195 // THEN
196 QCOMPARE(arbiter.events.size(), 0);
197 QCOMPARE(arbiter.dirtyNodes.size(), 0);
198 }
199 }
200 {
201 {
202 // WHEN
203 picker.setHoverEnabled(true);
204 QCoreApplication::processEvents();
205
206 // THEN
207 QCOMPARE(arbiter.events.size(), 0);
208 QCOMPARE(arbiter.dirtyNodes.size(), 1);
209 QCOMPARE(arbiter.dirtyNodes.front(), &picker);
210
211 arbiter.dirtyNodes.clear();
212 }
213
214 {
215 // WHEN
216 picker.setHoverEnabled(true);
217 QCoreApplication::processEvents();
218
219 // THEN
220 QCOMPARE(arbiter.events.size(), 0);
221 QCOMPARE(arbiter.dirtyNodes.size(), 0);
222 }
223 }
224 }
225
226 void checkCloning_data()
227 {
228 QTest::addColumn<Qt3DRender::QObjectPicker *>(name: "objectPicker");
229
230 Qt3DRender::QObjectPicker *objectPicker = new Qt3DRender::QObjectPicker();
231 QTest::newRow(dataTag: "empty objectPicker") << objectPicker;
232 objectPicker = new Qt3DRender::QObjectPicker();
233 objectPicker->setHoverEnabled(true);
234 QTest::newRow(dataTag: "objectPicker_all_true") << objectPicker;
235 }
236
237 void checkPropertyUpdates()
238 {
239 // GIVEN
240 TestArbiter arbiter;
241 Qt3DCore::QScene scene;
242 QScopedPointer<Qt3DRender::QObjectPicker> objectPicker(new Qt3DRender::QObjectPicker());
243 arbiter.setArbiterOnNode(objectPicker.data());
244 Qt3DCore::QNodePrivate::get(q: objectPicker.data())->setScene(&scene);
245
246 // WHEN
247 objectPicker->setHoverEnabled(true);
248 QCoreApplication::processEvents();
249
250 // THEN
251 QCOMPARE(arbiter.events.size(), 0);
252 QCOMPARE(arbiter.dirtyNodes.size(), 1);
253 QCOMPARE(arbiter.dirtyNodes.front(), objectPicker.data());
254
255 arbiter.dirtyNodes.clear();
256 }
257};
258
259QTEST_MAIN(tst_QObjectPicker)
260
261#include "tst_qobjectpicker.moc"
262

source code of qt3d/tests/auto/render/qobjectpicker/tst_qobjectpicker.cpp