summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/animation/qabstractanimation/tst_qabstractanimation.cpp
blob: 65e57e2d84bda76a9e89247d2219b979ac88295b (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtCore/qabstractanimation.h>
#include <QtCore/qanimationgroup.h>
#include <QTest>
#include <QtTest/private/qpropertytesthelper_p.h>

class tst_QAbstractAnimation : public QObject
{
  Q_OBJECT
private slots:
    void construction();
    void destruction();
    void currentLoop();
    void currentLoopTime();
    void currentTime();
    void direction();
    void group();
    void loopCount();
    void state();
    void totalDuration();
    void avoidJumpAtStart();
    void avoidJumpAtStartWithStop();
    void avoidJumpAtStartWithRunning();
    void stateBinding();
    void loopCountBinding();
    void currentTimeBinding();
    void currentLoopBinding();
    void directionBinding();
};

class TestableQAbstractAnimation : public QAbstractAnimation
{
    Q_OBJECT

public:
    TestableQAbstractAnimation() : m_duration(10) {}
    virtual ~TestableQAbstractAnimation() override { }

    int duration() const override { return m_duration; }
    virtual void updateCurrentTime(int) override {}

    void setDuration(int duration) { m_duration = duration; }
private:
    int m_duration;
};

class DummyQAnimationGroup : public QAnimationGroup
{
    Q_OBJECT
public:
    int duration() const override { return 10; }
    virtual void updateCurrentTime(int) override {}
};

void tst_QAbstractAnimation::construction()
{
    TestableQAbstractAnimation anim;
}

void tst_QAbstractAnimation::destruction()
{
    TestableQAbstractAnimation *anim = new TestableQAbstractAnimation;
    delete anim;

    // Animations should stop when deleted
    auto *stopWhenDeleted = new TestableQAbstractAnimation;
    QAbstractAnimation::State lastOldState, lastNewState;
    QObject::connect(stopWhenDeleted, &QAbstractAnimation::stateChanged,
        [&](QAbstractAnimation::State newState, QAbstractAnimation::State oldState) {
            lastNewState = newState;
            lastOldState = oldState;
    });
    stopWhenDeleted->start();
    QCOMPARE(lastOldState, QAbstractAnimation::Stopped);
    QCOMPARE(lastNewState, QAbstractAnimation::Running);
    delete stopWhenDeleted;
    QCOMPARE(lastOldState, QAbstractAnimation::Running);
    QCOMPARE(lastNewState, QAbstractAnimation::Stopped);
}

void tst_QAbstractAnimation::currentLoop()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.currentLoop(), 0);
}

void tst_QAbstractAnimation::currentLoopTime()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.currentLoopTime(), 0);
}

void tst_QAbstractAnimation::currentTime()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.currentTime(), 0);
    anim.setCurrentTime(10);
    QCOMPARE(anim.currentTime(), 10);
}

void tst_QAbstractAnimation::direction()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.direction(), QAbstractAnimation::Forward);
    anim.setDirection(QAbstractAnimation::Backward);
    QCOMPARE(anim.direction(), QAbstractAnimation::Backward);
    anim.setDirection(QAbstractAnimation::Forward);
    QCOMPARE(anim.direction(), QAbstractAnimation::Forward);
}

void tst_QAbstractAnimation::group()
{
    TestableQAbstractAnimation *anim = new TestableQAbstractAnimation;
    DummyQAnimationGroup group;
    group.addAnimation(anim);
    QCOMPARE(anim->group(), &group);
}

void tst_QAbstractAnimation::loopCount()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.loopCount(), 1);
    anim.setLoopCount(10);
    QCOMPARE(anim.loopCount(), 10);
}

void tst_QAbstractAnimation::state()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.state(), QAbstractAnimation::Stopped);
}

void tst_QAbstractAnimation::totalDuration()
{
    TestableQAbstractAnimation anim;
    QCOMPARE(anim.duration(), 10);
    anim.setLoopCount(5);
    QCOMPARE(anim.totalDuration(), 50);
}

void tst_QAbstractAnimation::avoidJumpAtStart()
{
    TestableQAbstractAnimation anim;
    anim.setDuration(1000);

    /*
        the timer shouldn't actually start until we hit the event loop,
        so the sleep should have no effect
    */
    anim.start();
    QTest::qSleep(300);
    QCoreApplication::processEvents();
    QVERIFY(anim.currentTime() < 50);
}

void tst_QAbstractAnimation::avoidJumpAtStartWithStop()
{
    TestableQAbstractAnimation anim;
    anim.setDuration(1000);

    TestableQAbstractAnimation anim2;
    anim2.setDuration(1000);

    TestableQAbstractAnimation anim3;
    anim3.setDuration(1000);

    anim.start();
    QTest::qWait(300);
    anim.stop();

    /*
        same test as avoidJumpAtStart, but after there is a
        running animation that is stopped
    */
    anim2.start();
    QTest::qSleep(300);
    anim3.start();
    QCoreApplication::processEvents();
    QVERIFY(anim2.currentTime() < 50);
    QVERIFY(anim3.currentTime() < 50);
}

void tst_QAbstractAnimation::avoidJumpAtStartWithRunning()
{
    TestableQAbstractAnimation anim;
    anim.setDuration(2000);

    TestableQAbstractAnimation anim2;
    anim2.setDuration(1000);

    TestableQAbstractAnimation anim3;
    anim3.setDuration(1000);

    anim.start();
    QTest::qWait(300);  //make sure timer has started

    /*
        same test as avoidJumpAtStart, but with an
        existing running animation
    */
    anim2.start();
    QTest::qSleep(300); //force large delta for next tick
    anim3.start();
    QCoreApplication::processEvents();
    QVERIFY(anim2.currentTime() < 50);
    QVERIFY(anim3.currentTime() < 50);
}

void tst_QAbstractAnimation::stateBinding()
{
    TestableQAbstractAnimation animation;
    QTestPrivate::testReadOnlyPropertyBasics(animation, QAbstractAnimation::Stopped,
                                             QAbstractAnimation::Running, "state",
                                             [&] { animation.start(); });
}

void tst_QAbstractAnimation::loopCountBinding()
{
    TestableQAbstractAnimation animation;
    QTestPrivate::testReadWritePropertyBasics(animation, 42, 43, "loopCount");
}

void tst_QAbstractAnimation::currentTimeBinding()
{
    TestableQAbstractAnimation animation;

    QProperty<int> currentTimeProperty;
    animation.bindableCurrentTime().setBinding(Qt::makePropertyBinding(currentTimeProperty));
    QCOMPARE(animation.currentTime(), currentTimeProperty);

    // This should cancel the binding
    animation.start();

    currentTimeProperty = 5;
    QVERIFY(animation.currentTime() != currentTimeProperty);

    QTestPrivate::testReadWritePropertyBasics(animation, 6, 7, "currentTime");
}

void tst_QAbstractAnimation::currentLoopBinding()
{
    TestableQAbstractAnimation animation;

    QTestPrivate::testReadOnlyPropertyBasics(animation, 0, 3, "currentLoop", [&] {
        // Trigger an update of currentLoop
        animation.setLoopCount(4);
        // This brings us to the end of the animation, so currentLoop should be loopCount - 1
        animation.setCurrentTime(42);
    });
}

void tst_QAbstractAnimation::directionBinding()
{
    TestableQAbstractAnimation animation;
    QTestPrivate::testReadWritePropertyBasics(animation, QAbstractAnimation::Backward,
                                              QAbstractAnimation::Forward, "direction");

    // setDirection() may trigger a currentLoop update. Make sure the observers
    // are notified about direction and currentLoop changes only after a consistent
    // state is reached.
    QProperty<int> currLoopObserver;
    currLoopObserver.setBinding([&] { return animation.currentLoop(); });

    QProperty<QAbstractAnimation::Direction> directionObserver;
    directionObserver.setBinding([&] { return animation.direction(); });

    animation.setLoopCount(10);

    bool currentLoopChanged = false;
    auto currentLoopHandler = animation.bindableCurrentLoop().onValueChanged([&] {
        QVERIFY(!currentLoopChanged);
        QCOMPARE(currLoopObserver, 9);
        QCOMPARE(directionObserver, QAbstractAnimation::Backward);
        currentLoopChanged = true;
    });

    bool directionChanged = false;
    auto directionHandler = animation.bindableDirection().onValueChanged([&] {
        QVERIFY(!directionChanged);
        QCOMPARE(currLoopObserver, 9);
        QCOMPARE(directionObserver, QAbstractAnimation::Backward);
        directionChanged = true;
    });

    QCOMPARE(animation.direction(), QAbstractAnimation::Forward);
    // This will set currentLoop to 9
    animation.setDirection(QAbstractAnimation::Backward);

    QVERIFY(currentLoopChanged);
    QVERIFY(directionChanged);
}

QTEST_MAIN(tst_QAbstractAnimation)

#include "tst_qabstractanimation.moc"