1/****************************************************************************
2**
3** Copyright (C) 2017 Paul Lemire <paul.lemire350@gmail.com>
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 <QObject>
31#include <QSignalSpy>
32#include <Qt3DAnimation/private/managers_p.h>
33#include <Qt3DAnimation/private/clipblendnodevisitor_p.h>
34#include <Qt3DAnimation/private/lerpclipblend_p.h>
35#include <Qt3DAnimation/qlerpclipblend.h>
36#include "qbackendnodetester.h"
37
38class tst_ClipBlendNodeVisitor : public Qt3DCore::QBackendNodeTester
39{
40 Q_OBJECT
41
42private Q_SLOTS:
43 void checkVisitAllNodes()
44 {
45 // GIVEN
46 Qt3DAnimation::QLerpClipBlend rootBlendNode;
47 Qt3DAnimation::QLerpClipBlend childBlendNode1(&rootBlendNode);
48 Qt3DAnimation::QLerpClipBlend childBlendNode2(&rootBlendNode);
49 rootBlendNode.setStartClip(&childBlendNode1);
50 rootBlendNode.setEndClip(&childBlendNode2);
51 Qt3DAnimation::QLerpClipBlend childBlendNode11(&childBlendNode1);
52 Qt3DAnimation::QLerpClipBlend childBlendNode12(&childBlendNode1);
53 childBlendNode1.setStartClip(&childBlendNode11);
54 childBlendNode1.setEndClip(&childBlendNode12);
55
56 Qt3DAnimation::Animation::LerpClipBlend *backendRootBlendNode = new Qt3DAnimation::Animation::LerpClipBlend();
57 Qt3DAnimation::Animation::LerpClipBlend *backendChildBlendNode1 = new Qt3DAnimation::Animation::LerpClipBlend();
58 Qt3DAnimation::Animation::LerpClipBlend *backendChildBlendNode2 = new Qt3DAnimation::Animation::LerpClipBlend();
59 Qt3DAnimation::Animation::LerpClipBlend *backendChildBlendNode11 = new Qt3DAnimation::Animation::LerpClipBlend();
60 Qt3DAnimation::Animation::LerpClipBlend *backendChildBlendNode12 = new Qt3DAnimation::Animation::LerpClipBlend();
61
62 Qt3DAnimation::Animation::ClipBlendNodeManager manager;
63 backendRootBlendNode->setClipBlendNodeManager(&manager);
64 backendChildBlendNode1->setClipBlendNodeManager(&manager);
65 backendChildBlendNode2->setClipBlendNodeManager(&manager);
66 backendChildBlendNode11->setClipBlendNodeManager(&manager);
67 backendChildBlendNode12->setClipBlendNodeManager(&manager);
68
69 manager.appendNode(id: rootBlendNode.id(), node: backendRootBlendNode);
70 manager.appendNode(id: childBlendNode1.id(), node: backendChildBlendNode1);
71 manager.appendNode(id: childBlendNode2.id(), node: backendChildBlendNode2);
72 manager.appendNode(id: childBlendNode11.id(), node: backendChildBlendNode11);
73 manager.appendNode(id: childBlendNode12.id(), node: backendChildBlendNode12);
74
75 // WHEN
76 simulateInitializationSync(frontend: &rootBlendNode, backend: backendRootBlendNode);
77 simulateInitializationSync(frontend: &childBlendNode1, backend: backendChildBlendNode1);
78 simulateInitializationSync(frontend: &childBlendNode2, backend: backendChildBlendNode2);
79 simulateInitializationSync(frontend: &childBlendNode11, backend: backendChildBlendNode11);
80 simulateInitializationSync(frontend: &childBlendNode12, backend: backendChildBlendNode12);
81
82 // THEN
83 QCOMPARE(backendRootBlendNode->allDependencyIds().size(), 2);
84 QCOMPARE(backendChildBlendNode1->allDependencyIds().size(), 2);
85 QCOMPARE(backendChildBlendNode2->allDependencyIds().size(), 2);
86 QCOMPARE(backendChildBlendNode11->allDependencyIds().size(), 2);
87 QCOMPARE(backendChildBlendNode12->allDependencyIds().size(), 2);
88
89 // WHEN
90 int i = 0;
91 // Note: post-order traversal
92 auto childCounter = [&] (Qt3DAnimation::Animation::ClipBlendNode *node) {
93 if (i == 0)
94 QCOMPARE(node, backendChildBlendNode11);
95 else if (i == 1)
96 QCOMPARE(node, backendChildBlendNode12);
97 else if (i == 2)
98 QCOMPARE(node, backendChildBlendNode1);
99 else if (i == 3)
100 QCOMPARE(node, backendChildBlendNode2);
101 else if (i == 4)
102 QCOMPARE(node, backendRootBlendNode);
103 ++i;
104 };
105
106 // THEN -> no comparison failure
107 Qt3DAnimation::Animation::ClipBlendNodeVisitor visitor(&manager);
108 visitor.traverse(rootId: rootBlendNode.id(), visitFunction: childCounter);
109 }
110
111 void checkDoesntCrashIfRootNodeIsNotFound()
112 {
113 // GIVEN
114 Qt3DAnimation::QLerpClipBlend rootBlendNode;
115
116 Qt3DAnimation::Animation::LerpClipBlend *backendRootBlendNode = new Qt3DAnimation::Animation::LerpClipBlend();
117
118 Qt3DAnimation::Animation::ClipBlendNodeManager manager;
119 backendRootBlendNode->setClipBlendNodeManager(&manager);
120
121 // We purposely forgot the to do: manager.appendNode(rootBlendNode.id(), backendRootBlendNode);
122
123 // WHEN
124 simulateInitializationSync(frontend: &rootBlendNode, backend: backendRootBlendNode);
125
126 // THEN
127 QCOMPARE(backendRootBlendNode->allDependencyIds().size(), 2);
128
129 // WHEN
130 auto childCounter = [] (Qt3DAnimation::Animation::ClipBlendNode *) {};
131
132 // THEN -> shouldn't crash
133 Qt3DAnimation::Animation::ClipBlendNodeVisitor visitor(&manager);
134 visitor.traverse(rootId: rootBlendNode.id(), visitFunction: childCounter);
135 }
136
137 void checkDoesntCrashIfChildNodeIsNotFound()
138 {
139 // GIVEN
140 Qt3DAnimation::QLerpClipBlend rootBlendNode;
141 Qt3DAnimation::QLerpClipBlend childBlendNode1(&rootBlendNode);
142 Qt3DAnimation::QLerpClipBlend childBlendNode2(&rootBlendNode);
143 rootBlendNode.setStartClip(&childBlendNode1);
144 rootBlendNode.setEndClip(&childBlendNode2);
145
146 Qt3DAnimation::Animation::LerpClipBlend *backendRootBlendNode = new Qt3DAnimation::Animation::LerpClipBlend();
147 Qt3DAnimation::Animation::LerpClipBlend *backendChildBlendNode1 = new Qt3DAnimation::Animation::LerpClipBlend();
148 Qt3DAnimation::Animation::LerpClipBlend *backendChildBlendNode2 = new Qt3DAnimation::Animation::LerpClipBlend();
149
150 Qt3DAnimation::Animation::ClipBlendNodeManager manager;
151 backendRootBlendNode->setClipBlendNodeManager(&manager);
152 backendChildBlendNode1->setClipBlendNodeManager(&manager);
153 backendChildBlendNode2->setClipBlendNodeManager(&manager);
154
155 manager.appendNode(id: rootBlendNode.id(), node: backendRootBlendNode);
156 // We purposely forgot the to do:
157 // manager.appendNode(childBlendNode1.id(), backendChildBlendNode1);
158 // manager.appendNode(childBlendNode2.id(), backendChildBlendNode2);
159
160
161 // WHEN
162 simulateInitializationSync(frontend: &rootBlendNode, backend: backendRootBlendNode);
163 simulateInitializationSync(frontend: &childBlendNode1, backend: backendChildBlendNode1);
164 simulateInitializationSync(frontend: &childBlendNode2, backend: backendChildBlendNode2);
165
166 // THEN
167 QCOMPARE(backendRootBlendNode->allDependencyIds().size(), 2);
168 QCOMPARE(backendChildBlendNode1->allDependencyIds().size(), 2);
169 QCOMPARE(backendChildBlendNode2->allDependencyIds().size(), 2);
170
171 // WHEN
172 int i = 0;
173 auto childCounter = [&] (Qt3DAnimation::Animation::ClipBlendNode *) {
174 ++i;
175 };
176
177 // THEN -> shouldn't crash
178 Qt3DAnimation::Animation::ClipBlendNodeVisitor visitor(&manager);
179 visitor.traverse(rootId: rootBlendNode.id(), visitFunction: childCounter);
180 QCOMPARE(i, 1);
181 }
182
183};
184
185QTEST_MAIN(tst_ClipBlendNodeVisitor)
186
187#include "tst_clipblendnodevisitor.moc"
188

source code of qt3d/tests/auto/animation/clipblendnodevisitor/tst_clipblendnodevisitor.cpp