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/qentity.h>
33#include <Qt3DCore/qpropertyvalueaddedchange.h>
34#include <Qt3DCore/qpropertyvalueremovedchange.h>
35#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
36
37#include <Qt3DRender/qsortpolicy.h>
38#include <Qt3DRender/private/qsortpolicy_p.h>
39
40#include "testpostmanarbiter.h"
41
42// We need to call QNode::clone which is protected
43// So we sublcass QNode instead of QObject
44class tst_QSortPolicy: public Qt3DCore::QNode
45{
46 Q_OBJECT
47
48private Q_SLOTS:
49
50 void checkSaneDefaults()
51 {
52 QScopedPointer<Qt3DRender::QSortPolicy> defaultsortPolicy(new Qt3DRender::QSortPolicy);
53
54 QVERIFY(defaultsortPolicy->sortTypes().isEmpty());
55 }
56
57 void checkCloning_data()
58 {
59 QTest::addColumn<Qt3DRender::QSortPolicy *>(name: "sortPolicy");
60 QTest::addColumn<QVector<Qt3DRender::QSortPolicy::SortType> >(name: "sortTypes");
61
62 Qt3DRender::QSortPolicy *defaultConstructed = new Qt3DRender::QSortPolicy();
63 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed << QVector<Qt3DRender::QSortPolicy::SortType>();
64
65 Qt3DRender::QSortPolicy *sortPolicyWithSortTypes = new Qt3DRender::QSortPolicy();
66 auto sortTypes = QVector<Qt3DRender::QSortPolicy::SortType>() << Qt3DRender::QSortPolicy::BackToFront
67 << Qt3DRender::QSortPolicy::Material
68 << Qt3DRender::QSortPolicy::FrontToBack;
69 sortPolicyWithSortTypes->setSortTypes(sortTypes);
70 QTest::newRow(dataTag: "sortPolicyWithSortTypes") << sortPolicyWithSortTypes << sortTypes ;
71 }
72
73 void checkCloning()
74 {
75 // GIVEN
76 QFETCH(Qt3DRender::QSortPolicy*, sortPolicy);
77 QFETCH(QVector<Qt3DRender::QSortPolicy::SortType>, sortTypes);
78
79 // THEN
80 QCOMPARE(sortPolicy->sortTypes(), sortTypes);
81
82 // WHEN
83 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(sortPolicy);
84 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
85
86 // THEN
87 QCOMPARE(creationChanges.size(), 1);
88
89 const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QSortPolicyData> creationChangeData =
90 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QSortPolicyData>>(src: creationChanges.first());
91 const Qt3DRender::QSortPolicyData &cloneData = creationChangeData->data;
92
93 QCOMPARE(sortPolicy->id(), creationChangeData->subjectId());
94 QCOMPARE(sortPolicy->isEnabled(), creationChangeData->isNodeEnabled());
95 QCOMPARE(sortPolicy->metaObject(), creationChangeData->metaObject());
96 QCOMPARE(sortPolicy->sortTypes().count(), cloneData.sortTypes.count());
97 QCOMPARE(sortPolicy->sortTypes(), cloneData.sortTypes);
98
99 delete sortPolicy;
100 }
101
102 void checkPropertyUpdates()
103 {
104 // GIVEN
105 TestArbiter arbiter;
106 QScopedPointer<Qt3DRender::QSortPolicy> sortPolicy(new Qt3DRender::QSortPolicy());
107 arbiter.setArbiterOnNode(sortPolicy.data());
108
109 // WHEN
110 auto sortTypes = QVector<Qt3DRender::QSortPolicy::SortType>() << Qt3DRender::QSortPolicy::BackToFront
111 << Qt3DRender::QSortPolicy::Material
112 << Qt3DRender::QSortPolicy::Material
113 << Qt3DRender::QSortPolicy::FrontToBack;
114 auto sortTypesInt = QVector<int>();
115 transformVector(input: sortTypes, output&: sortTypesInt);
116 sortPolicy->setSortTypes(sortTypes);
117 QCoreApplication::processEvents();
118
119 // THEN
120 QCOMPARE(arbiter.events.size(), 0);
121 QCOMPARE(arbiter.dirtyNodes.size(), 1);
122 QCOMPARE(arbiter.dirtyNodes.front(), sortPolicy.data());
123
124 arbiter.dirtyNodes.clear();
125 }
126};
127
128QTEST_MAIN(tst_QSortPolicy)
129
130#include "tst_qsortpolicy.moc"
131

source code of qt3d/tests/auto/render/qsortpolicy/tst_qsortpolicy.cpp