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 <Qt3DCore/private/qnode_p.h> |
31 | #include <Qt3DCore/private/qscene_p.h> |
32 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
33 | |
34 | #include <Qt3DRender/qcameraselector.h> |
35 | #include <Qt3DRender/private/qcameraselector_p.h> |
36 | #include <Qt3DCore/qentity.h> |
37 | |
38 | #include "testpostmanarbiter.h" |
39 | |
40 | class tst_QCameraSelector: public QObject |
41 | { |
42 | Q_OBJECT |
43 | |
44 | private Q_SLOTS: |
45 | |
46 | void checkCloning_data() |
47 | { |
48 | QTest::addColumn<Qt3DRender::QCameraSelector *>(name: "cameraSelector" ); |
49 | QTest::addColumn<Qt3DCore::QEntity *>(name: "camera" ); |
50 | |
51 | Qt3DRender::QCameraSelector *defaultConstructed = new Qt3DRender::QCameraSelector(); |
52 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed << static_cast<Qt3DCore::QEntity *>(nullptr); |
53 | |
54 | Qt3DRender::QCameraSelector *selector1 = new Qt3DRender::QCameraSelector(); |
55 | Qt3DCore::QEntity *camera1 = new Qt3DCore::QEntity(); |
56 | QTest::newRow(dataTag: "valid camera" ) << selector1 << camera1; |
57 | selector1->setCamera(camera1); |
58 | } |
59 | |
60 | void checkCloning() |
61 | { |
62 | // GIVEN |
63 | QFETCH(Qt3DRender::QCameraSelector *, cameraSelector); |
64 | QFETCH(Qt3DCore::QEntity *, camera); |
65 | |
66 | // WHEN |
67 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(cameraSelector); |
68 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
69 | |
70 | // THEN |
71 | QCOMPARE(creationChanges.size(), 1 + (camera ? 1 : 0)); |
72 | |
73 | const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QCameraSelectorData> creationChangeData = |
74 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QCameraSelectorData>>(src: creationChanges.first()); |
75 | const Qt3DRender::QCameraSelectorData &cloneData = creationChangeData->data; |
76 | |
77 | QCOMPARE(cameraSelector->id(), creationChangeData->subjectId()); |
78 | QCOMPARE(cameraSelector->isEnabled(), creationChangeData->isNodeEnabled()); |
79 | QCOMPARE(cameraSelector->metaObject(), creationChangeData->metaObject()); |
80 | QCOMPARE(cameraSelector->camera() ? cameraSelector->camera()->id() : Qt3DCore::QNodeId(), cloneData.cameraId); |
81 | |
82 | delete cameraSelector; |
83 | } |
84 | |
85 | void checkPropertyUpdates() |
86 | { |
87 | // GIVEN |
88 | TestArbiter arbiter; |
89 | QScopedPointer<Qt3DRender::QCameraSelector> cameraSelector(new Qt3DRender::QCameraSelector()); |
90 | arbiter.setArbiterOnNode(cameraSelector.data()); |
91 | |
92 | // WHEN |
93 | Qt3DCore::QEntity *camera = new Qt3DCore::QEntity(); |
94 | cameraSelector->setCamera(camera); |
95 | |
96 | // THEN |
97 | QCOMPARE(arbiter.events.size(), 0); |
98 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
99 | QCOMPARE(arbiter.dirtyNodes.front(), cameraSelector.data()); |
100 | |
101 | arbiter.dirtyNodes.clear(); |
102 | |
103 | // WHEN |
104 | cameraSelector->setCamera(camera); |
105 | |
106 | // THEN |
107 | QCOMPARE(arbiter.events.size(), 0); |
108 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
109 | |
110 | // WHEN |
111 | Qt3DCore::QEntity *camera2 = new Qt3DCore::QEntity(); |
112 | cameraSelector->setCamera(camera2); |
113 | |
114 | // THEN |
115 | QCOMPARE(arbiter.events.size(), 0); |
116 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
117 | QCOMPARE(arbiter.dirtyNodes.front(), cameraSelector.data()); |
118 | |
119 | arbiter.dirtyNodes.clear(); |
120 | |
121 | // WHEN |
122 | cameraSelector->setCamera(nullptr); |
123 | |
124 | // THEN |
125 | QCOMPARE(arbiter.events.size(), 0); |
126 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
127 | QCOMPARE(arbiter.dirtyNodes.front(), cameraSelector.data()); |
128 | |
129 | arbiter.dirtyNodes.clear(); |
130 | } |
131 | |
132 | void checkCameraBookkeeping() |
133 | { |
134 | // GIVEN |
135 | QScopedPointer<Qt3DRender::QCameraSelector> cameraSelector(new Qt3DRender::QCameraSelector); |
136 | { |
137 | // WHEN |
138 | Qt3DCore::QEntity camera; |
139 | cameraSelector->setCamera(&camera); |
140 | |
141 | // THEN |
142 | QCOMPARE(camera.parent(), cameraSelector.data()); |
143 | QCOMPARE(cameraSelector->camera(), &camera); |
144 | } |
145 | // THEN (Should not crash and parameter be unset) |
146 | QVERIFY(cameraSelector->camera() == nullptr); |
147 | |
148 | { |
149 | // WHEN |
150 | Qt3DRender::QCameraSelector someOtherCameraSelector; |
151 | QScopedPointer<Qt3DCore::QEntity> camera(new Qt3DCore::QEntity(&someOtherCameraSelector)); |
152 | cameraSelector->setCamera(camera.data()); |
153 | |
154 | // THEN |
155 | QCOMPARE(camera->parent(), &someOtherCameraSelector); |
156 | QCOMPARE(cameraSelector->camera(), camera.data()); |
157 | |
158 | // WHEN |
159 | cameraSelector.reset(); |
160 | camera.reset(); |
161 | |
162 | // THEN Should not crash when the camera is destroyed (tests for failed removal of destruction helper) |
163 | } |
164 | } |
165 | }; |
166 | |
167 | QTEST_MAIN(tst_QCameraSelector) |
168 | |
169 | #include "tst_qcameraselector.moc" |
170 | |