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:BSD$
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** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtTest/QtTest>
52
53#include <Qt3DCore/QEntity>
54#include <Qt3DCore/QTransform>
55#include <Qt3DCore/QAspectEngine>
56
57#include <Qt3DInput/QInputAspect>
58#include <Qt3DInput/QInputSettings>
59
60#include <Qt3DLogic/QLogicAspect>
61
62#include <Qt3DRender/QCamera>
63#include <Qt3DRender/QCameraLens>
64#include <Qt3DRender/QRenderAspect>
65#include <Qt3DRender/QRenderSettings>
66
67#include <Qt3DExtras/QForwardRenderer>
68#include <Qt3DExtras/Qt3DWindow>
69#include <Qt3DExtras/QPhongMaterial>
70#include <Qt3DExtras/QSphereMesh>
71#include <Qt3DExtras/QTorusMesh>
72
73#include <QPropertyAnimation>
74
75namespace {
76
77Qt3DCore::QEntity *createScene(QWindow *w)
78{
79 // Root entity
80 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
81
82 // Camera
83 Qt3DRender::QCamera *camera = new Qt3DRender::QCamera(rootEntity);
84 camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f);
85 camera->setPosition(QVector3D(0, 0, 40.0f));
86 camera->setViewCenter(QVector3D(0, 0, 0));
87
88 // FrameGraph
89 Qt3DRender::QRenderSettings *renderSettings = new Qt3DRender::QRenderSettings();
90 Qt3DExtras::QForwardRenderer *forwardRenderer = new Qt3DExtras::QForwardRenderer();
91 forwardRenderer->setSurface(w);
92 forwardRenderer->setCamera(camera);
93 forwardRenderer->setClearColor(QColor(Qt::blue));
94 renderSettings->setActiveFrameGraph(forwardRenderer);
95 rootEntity->addComponent(comp: renderSettings);
96
97 // InputSettings
98 Qt3DInput::QInputSettings *inputSettigns = new Qt3DInput::QInputSettings();
99 rootEntity->addComponent(comp: inputSettigns);
100
101 // Material
102 Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
103
104 // Torus
105 Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity);
106 Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh;
107 torusMesh->setRadius(5);
108 torusMesh->setMinorRadius(1);
109 torusMesh->setRings(100);
110 torusMesh->setSlices(20);
111
112 Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform;
113 torusTransform->setScale3D(QVector3D(1.5, 1, 0.5));
114 torusTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1, 0, 0), angle: 45.0f));
115
116 torusEntity->addComponent(comp: torusMesh);
117 torusEntity->addComponent(comp: torusTransform);
118 torusEntity->addComponent(comp: material);
119
120 // Sphere
121 Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
122 Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
123 sphereMesh->setRadius(3);
124
125 Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
126
127 sphereEntity->addComponent(comp: sphereMesh);
128 sphereEntity->addComponent(comp: sphereTransform);
129 sphereEntity->addComponent(comp: material);
130
131 return rootEntity;
132}
133
134} // anonymous
135
136class tst_AspectsStartupShutdown : public QObject
137{
138 Q_OBJECT
139
140public:
141
142private slots:
143
144 void checkStartupAndShutdownImmediately()
145 {
146 QSKIP("Fails on CI for some unexplained reason");
147
148 // GIVEN
149 QWindow *win = new QWindow();
150 win->setSurfaceType(QSurface::OpenGLSurface);
151 win->resize(w: 1024, h: 768);
152 win->show();
153
154 // WHEN
155 Qt3DCore::QAspectEngine *engine = new Qt3DCore::QAspectEngine();
156 engine->registerAspect(aspect: new Qt3DRender::QRenderAspect());
157 engine->registerAspect(aspect: new Qt3DInput::QInputAspect());
158 engine->registerAspect(aspect: new Qt3DLogic::QLogicAspect());
159 QPointer<Qt3DCore::QEntity> scene = createScene(w: win);
160 engine->setRootEntity(Qt3DCore::QEntityPtr(scene.data()));
161
162 // THEN
163 QCOMPARE(engine->rootEntity().data(), scene.data());
164
165 // WHEN
166 win->close();
167 delete engine;
168
169 // THEN -> shouldn't crash or deadlock
170 delete win;
171 }
172
173 void checkStartupAndShutdownAfterAFewFrames()
174 {
175 QSKIP("Fails on CI for some unexplained reason");
176
177 // GIVEN
178 QWindow *win = new QWindow();
179 win->setSurfaceType(QSurface::OpenGLSurface);
180 win->resize(w: 1024, h: 768);
181 win->show();
182
183 // WHEN
184 Qt3DCore::QAspectEngine *engine = new Qt3DCore::QAspectEngine();
185 engine->registerAspect(aspect: new Qt3DRender::QRenderAspect());
186 engine->registerAspect(aspect: new Qt3DInput::QInputAspect());
187 engine->registerAspect(aspect: new Qt3DLogic::QLogicAspect());
188 QPointer<Qt3DCore::QEntity> scene = createScene(w: win);
189 engine->setRootEntity(Qt3DCore::QEntityPtr(scene.data()));
190
191 // THEN
192 QCOMPARE(engine->rootEntity().data(), scene.data());
193
194 // Allow a few frames
195 for (int i = 0; i < 10; ++i) {
196 QCoreApplication::processEvents();
197 QThread::msleep(16);
198 }
199
200 // WHEN
201 // Right now we need to close the window before destroying
202 // the engine to prevent the render thread from trying to render
203 // while things are being destroyed
204 win->close();
205 delete engine;
206
207 // THEN -> shouldn't crash or deadlock
208 delete win;
209 }
210};
211
212QTEST_MAIN(tst_AspectsStartupShutdown)
213
214#include "tst_aspects_startup_shutdown.moc"
215

source code of qt3d/tests/auto/global/aspects_startup_shutdown/tst_aspects_startup_shutdown.cpp