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/qviewport.h>
35#include <Qt3DRender/private/qviewport_p.h>
36
37#include "testpostmanarbiter.h"
38
39class tst_QViewport: public QObject
40{
41 Q_OBJECT
42
43private Q_SLOTS:
44
45 void checkCloning_data()
46 {
47 QTest::addColumn<Qt3DRender::QViewport *>(name: "viewport");
48 QTest::addColumn<QRectF>(name: "normalizedRect");
49 QTest::addColumn<float>(name: "gamma");
50
51 Qt3DRender::QViewport *defaultConstructed = new Qt3DRender::QViewport();
52 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed << QRectF(0.0f, 0.0f, 1.0f, 1.0f) << 2.2f;
53
54 Qt3DRender::QViewport *smallGreenViewport = new Qt3DRender::QViewport();
55 smallGreenViewport->setNormalizedRect(QRectF(0.2f, 0.2f, 0.6f, 0.6f));
56 smallGreenViewport->setGamma(1.8f);
57 QTest::newRow(dataTag: "smallGreenViewport") << smallGreenViewport << QRectF(0.2f, 0.2f, 0.6f, 0.6f) << 1.8f;
58 }
59
60 void checkCloning()
61 {
62 // GIVEN
63 QFETCH(Qt3DRender::QViewport *, viewport);
64 QFETCH(QRectF, normalizedRect);
65 QFETCH(float, gamma);
66
67 // THEN
68 QCOMPARE(viewport->normalizedRect(), normalizedRect);
69 QCOMPARE(viewport->gamma(), gamma);
70
71 // WHEN
72 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(viewport);
73 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
74
75 // THEN
76 QCOMPARE(creationChanges.size(), 1);
77
78 const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QViewportData> creationChangeData =
79 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QViewportData>>(src: creationChanges.first());
80 const Qt3DRender::QViewportData &cloneData = creationChangeData->data;
81
82 QCOMPARE(viewport->id(), creationChangeData->subjectId());
83 QCOMPARE(viewport->isEnabled(), creationChangeData->isNodeEnabled());
84 QCOMPARE(viewport->metaObject(), creationChangeData->metaObject());
85 QCOMPARE(viewport->normalizedRect(), cloneData.normalizedRect);
86 QCOMPARE(viewport->gamma(), cloneData.gamma);
87
88 delete viewport;
89 }
90
91 void checkPropertyUpdates()
92 {
93 // GIVEN
94 TestArbiter arbiter;
95 QScopedPointer<Qt3DRender::QViewport> viewport(new Qt3DRender::QViewport());
96 arbiter.setArbiterOnNode(viewport.data());
97
98 {
99 // WHEN
100 viewport->setNormalizedRect(QRectF(0.5, 0.5, 1.0, 1.0));
101 QCoreApplication::processEvents();
102
103 // THEN
104 QCOMPARE(arbiter.events.size(), 0);
105 QCOMPARE(arbiter.dirtyNodes.size(), 1);
106 QCOMPARE(arbiter.dirtyNodes.front(), viewport.data());
107
108 arbiter.dirtyNodes.clear();
109
110 // WHEN
111 viewport->setNormalizedRect(QRectF(0.5, 0.5, 1.0, 1.0));
112 QCoreApplication::processEvents();
113
114 // THEN
115 QCOMPARE(arbiter.events.size(), 0);
116 QCOMPARE(arbiter.dirtyNodes.size(), 0);
117
118 // WHEN
119 viewport->setNormalizedRect(QRectF(0.0, 0.0, 1.0, 1.0));
120 QCoreApplication::processEvents();
121
122 // THEN
123 QCOMPARE(arbiter.events.size(), 0);
124 QCOMPARE(arbiter.dirtyNodes.size(), 1);
125 QCOMPARE(arbiter.dirtyNodes.front(), viewport.data());
126 }
127
128 arbiter.events.clear();
129
130 {
131 // WHEN
132 viewport->setGamma(1.8f);
133 QCoreApplication::processEvents();
134
135 // THEN
136 QCOMPARE(arbiter.events.size(), 0);
137 QCOMPARE(arbiter.dirtyNodes.size(), 1);
138 QCOMPARE(arbiter.dirtyNodes.front(), viewport.data());
139
140 arbiter.dirtyNodes.clear();
141
142 // WHEN
143 viewport->setGamma(1.8f);
144 QCoreApplication::processEvents();
145
146 // THEN
147 QCOMPARE(arbiter.events.size(), 0);
148 QCOMPARE(arbiter.dirtyNodes.size(), 0);
149
150 // WHEN
151 viewport->setGamma(2.0f);
152 QCoreApplication::processEvents();
153
154 // THEN
155 QCOMPARE(arbiter.events.size(), 0);
156 QCOMPARE(arbiter.dirtyNodes.size(), 1);
157 QCOMPARE(arbiter.dirtyNodes.front(), viewport.data());
158 }
159 }
160};
161
162QTEST_MAIN(tst_QViewport)
163
164#include "tst_qviewport.moc"
165

source code of qt3d/tests/auto/render/qviewport/tst_qviewport.cpp