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 <Qt3DAnimation/private/clipanimator_p.h>
31#include <Qt3DAnimation/qanimationcliploader.h>
32#include <Qt3DAnimation/qchannelmapper.h>
33#include <Qt3DAnimation/qclipanimator.h>
34#include <Qt3DAnimation/qclock.h>
35#include <Qt3DCore/private/qnode_p.h>
36#include <Qt3DCore/private/qscene_p.h>
37#include <Qt3DCore/private/qbackendnode_p.h>
38#include <qbackendnodetester.h>
39#include <testpostmanarbiter.h>
40
41class tst_ClipAnimator: public Qt3DCore::QBackendNodeTester
42{
43 Q_OBJECT
44
45private Q_SLOTS:
46 void checkPeerPropertyMirroring()
47 {
48 // GIVEN
49 Qt3DAnimation::Animation::Handler handler;
50 Qt3DAnimation::Animation::ClipAnimator backendAnimator;
51 backendAnimator.setHandler(&handler);
52 Qt3DAnimation::QClipAnimator animator;
53 auto clip = new Qt3DAnimation::QAnimationClipLoader();
54 auto clock = new Qt3DAnimation::QClock();
55
56 animator.setClip(clip);
57 animator.setClock(clock);
58 animator.setLoopCount(10);
59 animator.setNormalizedTime(0.5f);
60
61 // WHEN
62 simulateInitializationSync(frontend: &animator, backend: &backendAnimator);
63
64 // THEN
65 QCOMPARE(backendAnimator.peerId(), animator.id());
66 QCOMPARE(backendAnimator.isEnabled(), animator.isEnabled());
67 QCOMPARE(backendAnimator.clipId(), clip->id());
68 QCOMPARE(backendAnimator.clockId(), clock->id());
69 QCOMPARE(backendAnimator.isRunning(), animator.isRunning());
70 QCOMPARE(backendAnimator.loops(), animator.loopCount());
71 QCOMPARE(backendAnimator.normalizedLocalTime(), animator.normalizedTime());
72 }
73
74 void checkInitialAndCleanedUpState()
75 {
76 // GIVEN
77 Qt3DAnimation::Animation::Handler handler;
78 Qt3DAnimation::Animation::ClipAnimator backendAnimator;
79 backendAnimator.setHandler(&handler);
80
81 // THEN
82 QVERIFY(backendAnimator.peerId().isNull());
83 QCOMPARE(backendAnimator.isEnabled(), false);
84 QCOMPARE(backendAnimator.clipId(), Qt3DCore::QNodeId());
85 QCOMPARE(backendAnimator.clockId(), Qt3DCore::QNodeId());
86 QCOMPARE(backendAnimator.isRunning(), false);
87 QCOMPARE(backendAnimator.loops(), 1);
88 QCOMPARE(backendAnimator.normalizedLocalTime(), -1.0);
89
90 // GIVEN
91 Qt3DAnimation::QClipAnimator animator;
92 auto clip = new Qt3DAnimation::QAnimationClipLoader();
93 auto clock = new Qt3DAnimation::QClock();
94 animator.setClip(clip);
95 animator.setClock(clock);
96 animator.setRunning(true);
97 animator.setLoopCount(25);
98 animator.setNormalizedTime(1.0f);
99
100 // WHEN
101 simulateInitializationSync(frontend: &animator, backend: &backendAnimator);
102 backendAnimator.setClipId(Qt3DCore::QNodeId::createId());
103 backendAnimator.setClockId(Qt3DCore::QNodeId::createId());
104 backendAnimator.cleanup();
105
106 // THEN
107 QCOMPARE(backendAnimator.clipId(), Qt3DCore::QNodeId());
108 QCOMPARE(backendAnimator.clockId(), Qt3DCore::QNodeId());
109 QCOMPARE(backendAnimator.isEnabled(), false);
110 QCOMPARE(backendAnimator.isRunning(), false);
111 QCOMPARE(backendAnimator.loops(), 1);
112 QCOMPARE(backendAnimator.normalizedLocalTime(), -1.0f);
113 }
114
115 void checkPropertyChanges()
116 {
117 // GIVEN
118 Qt3DAnimation::QClipAnimator animator;
119 Qt3DAnimation::Animation::Handler handler;
120 Qt3DAnimation::Animation::ClipAnimator backendAnimator;
121 backendAnimator.setHandler(&handler);
122 simulateInitializationSync(frontend: &animator, backend: &backendAnimator);
123
124 // WHEN
125 animator.setEnabled(false);
126 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
127
128 // THEN
129 QCOMPARE(backendAnimator.isEnabled(), false);
130
131 // WHEN
132 auto newClip = new Qt3DAnimation::QAnimationClipLoader();
133 animator.setClip(newClip);
134 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
135
136 // THEN
137 QCOMPARE(backendAnimator.clipId(), newClip->id());
138
139 // WHEN
140 auto newMapper = new Qt3DAnimation::QChannelMapper();
141 animator.setChannelMapper(newMapper);
142 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
143
144 // THEN
145 QCOMPARE(backendAnimator.mapperId(), newMapper->id());
146
147 // WHEN
148 auto clock = new Qt3DAnimation::QClock();
149 animator.setClock(clock);
150 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
151
152 // THEN
153 QCOMPARE(backendAnimator.clockId(), clock->id());
154
155 // WHEN
156 animator.setRunning(true);
157 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
158
159 // THEN
160 QCOMPARE(backendAnimator.isRunning(), true);
161
162 // WHEN
163 animator.setLoopCount(64);
164 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
165
166 // THEN
167 QCOMPARE(backendAnimator.loops(), 64);
168
169 // WHEN
170 animator.setNormalizedTime(0.5f);
171 backendAnimator.syncFromFrontEnd(frontEnd: &animator, firstTime: false);
172
173 // THEN
174 QVERIFY(qFuzzyCompare(backendAnimator.normalizedLocalTime(), 0.5f));
175 }
176};
177
178QTEST_APPLESS_MAIN(tst_ClipAnimator)
179
180#include "tst_clipanimator.moc"
181

source code of qt3d/tests/auto/animation/clipanimator/tst_clipanimator.cpp