1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 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 <qbackendnodetester.h> |
31 | #include <Qt3DAnimation/private/handler_p.h> |
32 | #include <Qt3DAnimation/private/channelmapping_p.h> |
33 | #include <Qt3DAnimation/qchannelmapping.h> |
34 | #include <Qt3DAnimation/qskeletonmapping.h> |
35 | #include <Qt3DAnimation/private/qchannelmapping_p.h> |
36 | #include <Qt3DCore/qentity.h> |
37 | #include <Qt3DCore/qtransform.h> |
38 | #include <Qt3DCore/qskeleton.h> |
39 | #include <Qt3DCore/private/qnode_p.h> |
40 | #include <Qt3DCore/private/qscene_p.h> |
41 | #include <Qt3DCore/private/qbackendnode_p.h> |
42 | #include "testpostmanarbiter.h" |
43 | |
44 | class tst_TargetEntity : public Qt3DCore::QEntity |
45 | { |
46 | Q_OBJECT |
47 | Q_PROPERTY(QVector2D foo MEMBER m_foo NOTIFY fooChanged) |
48 | |
49 | signals: |
50 | void fooChanged(); |
51 | |
52 | private: |
53 | QVector2D m_foo; |
54 | }; |
55 | |
56 | class tst_ChannelMapping : public Qt3DCore::QBackendNodeTester |
57 | { |
58 | Q_OBJECT |
59 | |
60 | private Q_SLOTS: |
61 | void checkPeerPropertyMirroring() |
62 | { |
63 | // GIVEN |
64 | Qt3DAnimation::Animation::Handler handler; |
65 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
66 | backendMapping.setHandler(&handler); |
67 | Qt3DAnimation::QChannelMapping mapping; |
68 | auto target = new tst_TargetEntity; |
69 | |
70 | mapping.setChannelName(QLatin1String("Location" )); |
71 | mapping.setTarget(target); |
72 | mapping.setProperty(QLatin1String("foo" )); |
73 | |
74 | // WHEN |
75 | simulateInitializationSync(frontend: &mapping, backend: &backendMapping); |
76 | |
77 | // THEN |
78 | QCOMPARE(backendMapping.peerId(), mapping.id()); |
79 | QCOMPARE(backendMapping.isEnabled(), mapping.isEnabled()); |
80 | QCOMPARE(backendMapping.channelName(), mapping.channelName()); |
81 | QCOMPARE(backendMapping.targetId(), mapping.target()->id()); |
82 | QVERIFY(qstrcmp(backendMapping.propertyName(), mapping.property().toLatin1().constData()) == 0); |
83 | QVERIFY(qstrcmp(backendMapping.propertyName(), "foo" ) == 0); |
84 | QCOMPARE(backendMapping.componentCount(), 2); |
85 | QCOMPARE(backendMapping.type(), static_cast<int>(QVariant::Vector2D)); |
86 | QCOMPARE(backendMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::ChannelMappingType); |
87 | |
88 | // GIVEN |
89 | Qt3DAnimation::Animation::ChannelMapping backendSkeletonMapping; |
90 | backendSkeletonMapping.setHandler(&handler); |
91 | Qt3DAnimation::QSkeletonMapping skeletonMapping; |
92 | auto skeleton = new Qt3DCore::QSkeleton; |
93 | skeletonMapping.setSkeleton(skeleton); |
94 | |
95 | // WHEN |
96 | simulateInitializationSync(frontend: &skeletonMapping, backend: &backendSkeletonMapping); |
97 | |
98 | // THEN |
99 | QCOMPARE(backendSkeletonMapping.peerId(), skeletonMapping.id()); |
100 | QCOMPARE(backendSkeletonMapping.isEnabled(), skeletonMapping.isEnabled()); |
101 | QCOMPARE(backendSkeletonMapping.skeletonId(), skeletonMapping.skeleton()->id()); |
102 | QCOMPARE(backendSkeletonMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::SkeletonMappingType); |
103 | } |
104 | |
105 | void checkInitialAndCleanedUpState() |
106 | { |
107 | // GIVEN |
108 | Qt3DAnimation::Animation::Handler handler; |
109 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
110 | backendMapping.setHandler(&handler); |
111 | |
112 | // THEN |
113 | QVERIFY(backendMapping.peerId().isNull()); |
114 | QCOMPARE(backendMapping.isEnabled(), false); |
115 | QCOMPARE(backendMapping.channelName(), QString()); |
116 | QCOMPARE(backendMapping.targetId(), Qt3DCore::QNodeId()); |
117 | QCOMPARE(backendMapping.propertyName(), nullptr); |
118 | QCOMPARE(backendMapping.componentCount(), 0); |
119 | QCOMPARE(backendMapping.type(), static_cast<int>(QVariant::Invalid)); |
120 | QCOMPARE(backendMapping.skeletonId(), Qt3DCore::QNodeId()); |
121 | QCOMPARE(backendMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::ChannelMappingType); |
122 | |
123 | // GIVEN |
124 | Qt3DAnimation::QChannelMapping mapping; |
125 | auto target = new tst_TargetEntity; |
126 | mapping.setChannelName(QLatin1String("Location" )); |
127 | mapping.setTarget(target); |
128 | mapping.setProperty(QLatin1String("foo" )); |
129 | |
130 | // WHEN |
131 | simulateInitializationSync(frontend: &mapping, backend: &backendMapping); |
132 | backendMapping.setSkeletonId(Qt3DCore::QNodeId::createId()); |
133 | backendMapping.cleanup(); |
134 | |
135 | // THEN |
136 | QCOMPARE(backendMapping.isEnabled(), false); |
137 | QCOMPARE(backendMapping.channelName(), QString()); |
138 | QCOMPARE(backendMapping.targetId(), Qt3DCore::QNodeId()); |
139 | QCOMPARE(backendMapping.propertyName(), nullptr); |
140 | QCOMPARE(backendMapping.componentCount(), 0); |
141 | QCOMPARE(backendMapping.type(), static_cast<int>(QVariant::Invalid)); |
142 | QCOMPARE(backendMapping.skeletonId(), Qt3DCore::QNodeId()); |
143 | QCOMPARE(backendMapping.mappingType(), Qt3DAnimation::Animation::ChannelMapping::ChannelMappingType); |
144 | } |
145 | |
146 | void checkPropertyChanges() |
147 | { |
148 | // GIVEN |
149 | Qt3DAnimation::QChannelMapping mapping; |
150 | Qt3DAnimation::Animation::Handler handler; |
151 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
152 | backendMapping.setHandler(&handler); |
153 | simulateInitializationSync(frontend: &mapping, backend: &backendMapping); |
154 | |
155 | // WHEN |
156 | mapping.setEnabled(false); |
157 | backendMapping.syncFromFrontEnd(frontEnd: &mapping, firstTime: false); |
158 | |
159 | // THEN |
160 | QCOMPARE(backendMapping.isEnabled(), false); |
161 | |
162 | // WHEN |
163 | const QString channelName(QLatin1String("Translation" )); |
164 | mapping.setChannelName(channelName); |
165 | backendMapping.syncFromFrontEnd(frontEnd: &mapping, firstTime: false); |
166 | |
167 | // THEN |
168 | QCOMPARE(backendMapping.channelName(), channelName); |
169 | |
170 | // WHEN |
171 | const auto target = new Qt3DCore::QTransform(); |
172 | mapping.setTarget(target); |
173 | mapping.setProperty("translation" ); |
174 | backendMapping.syncFromFrontEnd(frontEnd: &mapping, firstTime: false); |
175 | |
176 | // THEN |
177 | QCOMPARE(backendMapping.targetId(), target->id()); |
178 | QCOMPARE(backendMapping.type(), static_cast<int>(QVariant::Vector3D)); |
179 | QCOMPARE(backendMapping.componentCount(), 3); |
180 | |
181 | const char *testName = "translation" ; |
182 | QCOMPARE(qstrcmp(testName, backendMapping.propertyName()), 0); |
183 | } |
184 | |
185 | void checkSkeletonPropertyUpdate() |
186 | { |
187 | // GIVEN |
188 | Qt3DAnimation::QSkeletonMapping mapping; |
189 | Qt3DAnimation::Animation::Handler handler; |
190 | Qt3DAnimation::Animation::ChannelMapping backendMapping; |
191 | backendMapping.setHandler(&handler); |
192 | simulateInitializationSync(frontend: &mapping, backend: &backendMapping); |
193 | |
194 | // WHEN |
195 | mapping.setEnabled(false); |
196 | backendMapping.syncFromFrontEnd(frontEnd: &mapping, firstTime: false); |
197 | |
198 | // THEN |
199 | QCOMPARE(backendMapping.isEnabled(), false); |
200 | |
201 | // WHEN |
202 | auto skeleton = new Qt3DCore::QSkeleton; |
203 | mapping.setSkeleton(skeleton); |
204 | backendMapping.syncFromFrontEnd(frontEnd: &mapping, firstTime: false); |
205 | |
206 | // THEN |
207 | QCOMPARE(backendMapping.skeletonId(), skeleton->id()); |
208 | } |
209 | }; |
210 | |
211 | QTEST_MAIN(tst_ChannelMapping) |
212 | |
213 | #include "tst_channelmapping.moc" |
214 | |