1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/QtTest>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlcomponent.h>
#include <QEasingCurve>
#include <QVector3D>
inline QUrl testFileUrl(const QString &fileName)
{
static const QString dir = QTest::qFindTestData("data");
QString result = dir;
result += QLatin1Char('/');
result += fileName;
return QUrl::fromLocalFile(result);
}
class Tst_BlendTrees : public QObject
{
Q_OBJECT
private Q_SLOTS:
void checkImport();
void testBlendAnimationNode();
};
void Tst_BlendTrees::checkImport()
{
QQmlEngine engine;
QQmlComponent component(&engine);
component.setData("import QtQuick; import QtQuick.Timeline; import QtQuick.Timeline.BlendTrees; Item { }", QUrl());
QScopedPointer<QObject> object(component.create());
QVERIFY2(!object.isNull(), qPrintable(component.errorString()));
}
void Tst_BlendTrees::testBlendAnimationNode()
{
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(testFileUrl("BlendTreeTest.qml"));
QScopedPointer<QObject> object(component.create());
QVERIFY2(!object.isNull(), qPrintable(component.errorString()));
// Get all of the necessary objects
auto *timeline = object->findChild<QObject * >("timeline");
auto *timelineAnimation1 = object->findChild<QObject *>("animation1");
QVERIFY2(timelineAnimation1, "Could not find animation1");
auto *timelineAnimation2 = object->findChild<QObject *>("animation2");
QVERIFY2(timelineAnimation2, "Could not find animation2");
auto *animation1Node = object->findChild<QObject *>("animation1Node");
QVERIFY2(animation1Node, "Could not find animation1Node");
auto *animation2Node = object->findChild<QObject *>("animation2Node");
QVERIFY2(animation2Node, "Could not find animation2Node");
auto *blendAnimation = object->findChild<QObject *>("blendAnimation");
QVERIFY2(blendAnimation, "Could not find blendAnimation");
auto *rectangle = object->findChild<QObject *>("rectangle");
QVERIFY2(rectangle, "Could not find rectangle");
auto *animation1Controller = object->findChild<QObject *>("animation1Controller");
QVERIFY2(animation1Controller, "Could not find animation1Controller");
auto *animation2Controller = object->findChild<QObject *>("animation2Controller");
QVERIFY2(animation2Controller, "Could not find animation2Controller");
// At this point nothing should be happening because the animations have controllers
// attached to this which forces them to always be paused. Starting states is:
// animation1Node.currentFrame == 0
// animation2Node.currentFrame == 100
// rectangle.x = 100
// rectangle.y = 100
// rectangle.color = #ff7f00
QCOMPARE(animation1Node->property("currentFrame").toInt(), 0);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 100);
QCOMPARE(rectangle->property("y").toInt(), 100);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff7f00"));
// Push animation1 to end
// Should be a blend of 50% blend of animation1 and and 50% blend of animation2
// animation 1 should be at frame 100 (100%)
// animation 2 should be at frame 100 (0%)
animation1Controller->setProperty("progress", 1.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 200);
QCOMPARE(rectangle->property("y").toInt(), 200);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ffff00"));
// Push animation2 to end
// Should be a blend of 50% blend of animation1 and and 50% blend of animation2
// animation 1 should be at frame 100 (100%)
// animation 2 should be at frame 200 (100%)
animation2Controller->setProperty("progress", 1.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 100);
QCOMPARE(rectangle->property("y").toInt(), 300);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff7f7f"));
// Change weight to 0.0
// Should be a blend of 100% blend of animation1 and and 0% blend of animation2
// animation 1 should be at frame 100 (100%)
// animation 2 should be at frame 200 (100%)
blendAnimation->setProperty("weight", 0.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 200);
QCOMPARE(rectangle->property("y").toInt(), 200);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ffff00"));
// Change weight to 1.0
// Should be a blend of 0% blend of animation1 and and 100% blend of animation2
// animation 1 should be at frame 100 (100%)
// animation 2 should be at frame 200 (100%)
blendAnimation->setProperty("weight", 1.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 400);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff00ff"));
// Change animation1 to start (should be the same as previous)
// Should be a blend of 0% blend of animation1 and and 100% blend of animation2
// animation 1 should be at frame 0 (0%)
// animation 2 should be at frame 200 (100%)
animation1Controller->setProperty("progress", 0.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 0);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 400);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff00ff"));
// Change weight to 0.0
// Should be a blend of 100% blend of animation1 and and 0% blend of animation2
// animation 1 should be at frame 0 (0%)
// animation 2 should be at frame 200 (100%)
blendAnimation->setProperty("weight", 0.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 0);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 0);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff0000"));
// Change animation2 to start (should be the same as previous)
// Should be a blend of 100% blend of animation1 and and 0% blend of animation2
// animation 1 should be at frame 0 (0%)
// animation 2 should be at frame 100 (0%)
animation2Controller->setProperty("progress", 0.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 0);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 0);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff0000"));
// Disable committing of changes by animationBlendNode
blendAnimation->setProperty("outputEnabled", false);
// Now changing either animation progress or weight should have no effect on the scene
animation1Controller->setProperty("progress", 1.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 0);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff0000"));
animation2Controller->setProperty("progress", 1.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 0);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff0000"));
blendAnimation->setProperty("weight", 0.5f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 0);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff0000"));
// re-enable committing of changes by animationBlendNode
// This should cause the animation to blend to the new state
blendAnimation->setProperty("outputEnabled", true);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 100);
QCOMPARE(rectangle->property("y").toInt(), 300);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff7f7f"));
// Test disconnecting animation1
blendAnimation->setProperty("source1", QVariant());
animation1Controller->setProperty("progress", 0.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 0);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 100);
QCOMPARE(rectangle->property("y").toInt(), 300);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff7f7f"));
// Test disconnecting animation2
blendAnimation->setProperty("source2", QVariant());
animation2Controller->setProperty("progress", 0.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 0);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 100);
QCOMPARE(rectangle->property("y").toInt(), 300);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff7f7f"));
// Disable committing of changes by animationBlendNode
blendAnimation->setProperty("outputEnabled", false);
// Try outputting dirrectly from animation1
animation1Node->setProperty("outputEnabled", true);
// Should have changed to be the value of animation1 at frame 0
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 0);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff0000"));
animation1Controller->setProperty("progress", 1.0f);
// Should have changed to be the value of animation1 at frame 100
QCOMPARE(rectangle->property("x").toInt(), 200);
QCOMPARE(rectangle->property("y").toInt(), 200);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ffff00"));
// Disable outputting dirrectly from animation1
animation1Node->setProperty("outputEnabled", false);
// Try outputting dirrectly from animation2
animation2Node->setProperty("outputEnabled", true);
// Nothing should have changed since both would be at frame 100 anyway
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 200);
QCOMPARE(rectangle->property("y").toInt(), 200);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ffff00"));
animation2Controller->setProperty("progress", 1.0f);
QCOMPARE(animation1Node->property("currentFrame").toInt(), 100);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 200);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 400);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff00ff"));
// Try breaking animation2Node's connection to timeline (the source of frame data)
// currentFrame should change but the output should not
animation2Node->setProperty("timeline", QVariant());
animation2Controller->setProperty("progress", 0.0f);
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 400);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff00ff"));
// reattach the timeline
animation2Node->setProperty("timeline", QVariant::fromValue(timeline));
// Should update the output now that we can fetch frameData for frame 100
QCOMPARE(animation2Node->property("currentFrame").toInt(), 100);
QCOMPARE(rectangle->property("x").toInt(), 200);
QCOMPARE(rectangle->property("y").toInt(), 200);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ffff00"));
// Try breaking animation2Node's connection to animation
animation2Node->setProperty("animation", QVariant());
animation2Controller->setProperty("progress", 1.0f);
QCOMPARE(rectangle->property("x").toInt(), 200);
QCOMPARE(rectangle->property("y").toInt(), 200);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ffff00"));
// reattach the animation
animation2Node->setProperty("animation", QVariant::fromValue(timelineAnimation2));
// Should update the output now that we can fetch frameData for frame 200
QCOMPARE(rectangle->property("x").toInt(), 0);
QCOMPARE(rectangle->property("y").toInt(), 400);
QCOMPARE(rectangle->property("color").value<QColor>(), QColor("#ff00ff"));
}
QTEST_MAIN(Tst_BlendTrees)
#include "tst_blendtrees.moc"
|