1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 
43 #include <QtTest/QtTest>
44 
45 #include <qeasingcurve.h>
46 
47 //TESTED_CLASS=
48 //TESTED_FILES=
49 
50 class tst_QEasingCurve : public QObject {
51   Q_OBJECT
52 
53 public:
54     tst_QEasingCurve();
55     virtual ~tst_QEasingCurve();
56 
57 public Q_SLOTS:
58     void init();
59     void cleanup();
60 
61 private slots:
62     void type();
63     void propertyDefaults();
64     void valueForProgress_data();
65     void valueForProgress();
66     void setCustomType();
67     void operators();
68     void properties();
69     void metaTypes();
70 
71 protected:
72 };
73 
tst_QEasingCurve()74 tst_QEasingCurve::tst_QEasingCurve()
75 {
76 }
77 
~tst_QEasingCurve()78 tst_QEasingCurve::~tst_QEasingCurve()
79 {
80 }
81 
init()82 void tst_QEasingCurve::init()
83 {
84 }
85 
cleanup()86 void tst_QEasingCurve::cleanup()
87 {
88 }
89 #include <qdebug.h>
90 
type()91 void tst_QEasingCurve::type()
92 {
93     {
94     QEasingCurve curve(QEasingCurve::Linear);
95     QCOMPARE(curve.period(), 0.3);
96     QCOMPARE(curve.amplitude(), 1.0);
97 
98     curve.setPeriod(5);
99     curve.setAmplitude(3);
100     QCOMPARE(curve.period(), 5.0);
101     QCOMPARE(curve.amplitude(), 3.0);
102 
103     curve.setType(QEasingCurve::InElastic);
104     QCOMPARE(curve.period(), 5.0);
105     QCOMPARE(curve.amplitude(), 3.0);
106     }
107 
108     {
109     QEasingCurve curve(QEasingCurve::InElastic);
110     QCOMPARE(curve.period(), 0.3);
111     QCOMPARE(curve.amplitude(), 1.0);
112     curve.setAmplitude(2);
113     QCOMPARE(curve.type(), QEasingCurve::InElastic);
114     curve.setType(QEasingCurve::Linear);
115     }
116 
117     {
118     // check bounaries
119     QEasingCurve curve(QEasingCurve::InCubic);
120     QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type 9999");
121     curve.setType((QEasingCurve::Type)9999);
122     QCOMPARE(curve.type(), QEasingCurve::InCubic);
123     QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type -9999");
124     curve.setType((QEasingCurve::Type)-9999);
125     QCOMPARE(curve.type(), QEasingCurve::InCubic);
126     QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1")
127                         .arg(QEasingCurve::NCurveTypes).toLatin1().constData());
128     curve.setType(QEasingCurve::NCurveTypes);
129     QCOMPARE(curve.type(), QEasingCurve::InCubic);
130     QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1")
131                         .arg(QEasingCurve::Custom).toLatin1().constData());
132     curve.setType(QEasingCurve::Custom);
133     QCOMPARE(curve.type(), QEasingCurve::InCubic);
134     QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1")
135                         .arg(-1).toLatin1().constData());
136     curve.setType((QEasingCurve::Type)-1);
137     QCOMPARE(curve.type(), QEasingCurve::InCubic);
138     curve.setType(QEasingCurve::Linear);
139     QCOMPARE(curve.type(), QEasingCurve::Linear);
140     curve.setType(QEasingCurve::CosineCurve);
141     QCOMPARE(curve.type(), QEasingCurve::CosineCurve);
142     }
143 }
144 
propertyDefaults()145 void tst_QEasingCurve::propertyDefaults()
146 {
147     {
148     // checks if the defaults are correct, but also demonstrates a weakness with the API.
149     QEasingCurve curve(QEasingCurve::InElastic);
150     QCOMPARE(curve.period(), 0.3);
151     QCOMPARE(curve.amplitude(), 1.0);
152     QCOMPARE(curve.overshoot(), qreal(1.70158));
153     curve.setType(QEasingCurve::InBounce);
154     QCOMPARE(curve.period(), 0.3);
155     QCOMPARE(curve.amplitude(), 1.0);
156     QCOMPARE(curve.overshoot(), qreal(1.70158));
157     curve.setType(QEasingCurve::Linear);
158     QCOMPARE(curve.period(), 0.3);
159     QCOMPARE(curve.amplitude(), 1.0);
160     QCOMPARE(curve.overshoot(), qreal(1.70158));
161     curve.setType(QEasingCurve::InElastic);
162     QCOMPARE(curve.period(), 0.3);
163     QCOMPARE(curve.amplitude(), 1.0);
164     QCOMPARE(curve.overshoot(), qreal(1.70158));
165     curve.setPeriod(0.4);
166     curve.setAmplitude(0.6);
167     curve.setOvershoot(1.0);
168     curve.setType(QEasingCurve::Linear);
169     QCOMPARE(curve.period(), 0.4);
170     QCOMPARE(curve.amplitude(), 0.6);
171     QCOMPARE(curve.overshoot(), 1.0);
172     curve.setType(QEasingCurve::InElastic);
173     QCOMPARE(curve.period(), 0.4);
174     QCOMPARE(curve.amplitude(), 0.6);
175     QCOMPARE(curve.overshoot(), 1.0);
176     }
177 }
178 
179 typedef QList<int> IntList;
180 typedef QList<qreal> RealList;
181 Q_DECLARE_METATYPE(IntList)
Q_DECLARE_METATYPE(RealList)182 Q_DECLARE_METATYPE(RealList)
183 
184 void tst_QEasingCurve::valueForProgress_data()
185 {
186     QTest::addColumn<int>("type");
187     QTest::addColumn<IntList>("at");
188     QTest::addColumn<RealList>("expected");
189     // automatically generated.
190     // note that values are scaled from range [0,1] to range [0, 100] in order to store them as
191     // integer values and avoid fp inaccuracies
192     QTest::newRow("Linear") << int(QEasingCurve::Linear)
193          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
194          << (RealList() << 0.0000 << 0.1000 << 0.2000 << 0.3000 << 0.4000 << 0.5000 << 0.6000 << 0.7000 << 0.8000 << 0.9000 << 1.0000);
195 
196     QTest::newRow("InQuad") << int(QEasingCurve::InQuad)
197          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
198          << (RealList() << 0.0000 << 0.0100 << 0.0400 << 0.0900 << 0.1600 << 0.2500 << 0.3600 << 0.4900 << 0.6400 << 0.8100 << 1.0000);
199 
200     QTest::newRow("OutQuad") << int(QEasingCurve::OutQuad)
201          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
202          << (RealList() << 0.0000 << 0.1900 << 0.3600 << 0.5100 << 0.6400 << 0.7500 << 0.8400 << 0.9100 << 0.9600 << 0.9900 << 1.0000);
203 
204     QTest::newRow("InOutQuad") << int(QEasingCurve::InOutQuad)
205          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
206          << (RealList() << 0.0000 << 0.0200 << 0.0800 << 0.1800 << 0.3200 << 0.5000 << 0.6800 << 0.8200 << 0.9200 << 0.9800 << 1.0000);
207 
208     QTest::newRow("OutInQuad") << int(QEasingCurve::OutInQuad)
209          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
210          << (RealList() << 0.0000 << 0.1800 << 0.3200 << 0.4200 << 0.4800 << 0.5000 << 0.5200 << 0.5800 << 0.6800 << 0.8200 << 1.0000);
211 
212     QTest::newRow("InCubic") << int(QEasingCurve::InCubic)
213          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
214          << (RealList() << 0.0000 << 0.0010 << 0.0080 << 0.0270 << 0.0640 << 0.1250 << 0.2160 << 0.3430 << 0.5120 << 0.7290 << 1.0000);
215 
216     QTest::newRow("OutCubic") << int(QEasingCurve::OutCubic)
217          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
218          << (RealList() << 0.0000 << 0.2710 << 0.4880 << 0.6570 << 0.7840 << 0.8750 << 0.9360 << 0.9730 << 0.9920 << 0.9990 << 1.0000);
219 
220     QTest::newRow("InOutCubic") << int(QEasingCurve::InOutCubic)
221          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
222          << (RealList() << 0.0000 << 0.0040 << 0.0320 << 0.1080 << 0.2560 << 0.5000 << 0.7440 << 0.8920 << 0.9680 << 0.9960 << 1.0000);
223 
224     QTest::newRow("OutInCubic") << int(QEasingCurve::OutInCubic)
225          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
226          << (RealList() << 0.0000 << 0.2440 << 0.3920 << 0.4680 << 0.4960 << 0.5000 << 0.5040 << 0.5320 << 0.6080 << 0.7560 << 1.0000);
227 
228     QTest::newRow("InQuart") << int(QEasingCurve::InQuart)
229          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
230          << (RealList() << 0.0000 << 0.0001 << 0.0016 << 0.0081 << 0.0256 << 0.0625 << 0.1296 << 0.2401 << 0.4096 << 0.6561 << 1.0000);
231 
232     QTest::newRow("OutQuart") << int(QEasingCurve::OutQuart)
233          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
234          << (RealList() << 0.0000 << 0.3439 << 0.5904 << 0.7599 << 0.8704 << 0.9375 << 0.9744 << 0.9919 << 0.9984 << 0.9999 << 1.0000);
235 
236     QTest::newRow("InOutQuart") << int(QEasingCurve::InOutQuart)
237          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
238          << (RealList() << 0.0000 << 0.0008 << 0.0128 << 0.0648 << 0.2048 << 0.5000 << 0.7952 << 0.9352 << 0.9872 << 0.9992 << 1.0000);
239 
240     QTest::newRow("OutInQuart") << int(QEasingCurve::OutInQuart)
241          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
242          << (RealList() << 0.0000 << 0.2952 << 0.4352 << 0.4872 << 0.4992 << 0.5000 << 0.5008 << 0.5128 << 0.5648 << 0.7048 << 1.0000);
243 
244     QTest::newRow("InQuint") << int(QEasingCurve::InQuint)
245          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
246          << (RealList() << 0.0000 << 0.0000 << 0.0003 << 0.0024 << 0.0102 << 0.0313 << 0.0778 << 0.1681 << 0.3277 << 0.5905 << 1.0000);
247 
248     QTest::newRow("OutQuint") << int(QEasingCurve::OutQuint)
249          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
250          << (RealList() << 0.0000 << 0.4095 << 0.6723 << 0.8319 << 0.9222 << 0.9688 << 0.9898 << 0.9976 << 0.9997 << 1.0000 << 1.0000);
251 
252     QTest::newRow("InOutQuint") << int(QEasingCurve::InOutQuint)
253          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
254          << (RealList() << 0.0000 << 0.0002 << 0.0051 << 0.0389 << 0.1638 << 0.5000 << 0.8362 << 0.9611 << 0.9949 << 0.9998 << 1.0000);
255 
256     QTest::newRow("OutInQuint") << int(QEasingCurve::OutInQuint)
257          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
258          << (RealList() << 0.0000 << 0.3362 << 0.4611 << 0.4949 << 0.4998 << 0.5000 << 0.5002 << 0.5051 << 0.5389 << 0.6638 << 1.0000);
259 
260     QTest::newRow("InSine") << int(QEasingCurve::InSine)
261          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
262          << (RealList() << 0.0000 << 0.0123 << 0.0489 << 0.1090 << 0.1910 << 0.2929 << 0.4122 << 0.5460 << 0.6910 << 0.8436 << 1.0000);
263 
264     QTest::newRow("OutSine") << int(QEasingCurve::OutSine)
265          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
266          << (RealList() << 0.0000 << 0.1564 << 0.3090 << 0.4540 << 0.5878 << 0.7071 << 0.8090 << 0.8910 << 0.9511 << 0.9877 << 1.0000);
267 
268     QTest::newRow("InOutSine") << int(QEasingCurve::InOutSine)
269          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
270          << (RealList() << 0.0000 << 0.0245 << 0.0955 << 0.2061 << 0.3455 << 0.5000 << 0.6545 << 0.7939 << 0.9045 << 0.9755 << 1.0000);
271 
272     QTest::newRow("OutInSine") << int(QEasingCurve::OutInSine)
273          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
274          << (RealList() << 0.0000 << 0.1545 << 0.2939 << 0.4045 << 0.4755 << 0.5000 << 0.5245 << 0.5955 << 0.7061 << 0.8455 << 1.0000);
275 
276     QTest::newRow("InExpo") << int(QEasingCurve::InExpo)
277          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
278          << (RealList() << 0.0000 << 0.0010 << 0.0029 << 0.0068 << 0.0146 << 0.0303 << 0.0615 << 0.1240 << 0.2490 << 0.4990 << 1.0000);
279 
280     QTest::newRow("OutExpo") << int(QEasingCurve::OutExpo)
281          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
282          << (RealList() << 0.0000 << 0.5005 << 0.7507 << 0.8759 << 0.9384 << 0.9697 << 0.9854 << 0.9932 << 0.9971 << 0.9990 << 1.0000);
283 
284     QTest::newRow("InOutExpo") << int(QEasingCurve::InOutExpo)
285          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
286          << (RealList() << 0.0000 << 0.0015 << 0.0073 << 0.0308 << 0.1245 << 0.5003 << 0.8754 << 0.9692 << 0.9927 << 0.9985 << 1.0000);
287 
288     QTest::newRow("OutInExpo") << int(QEasingCurve::OutInExpo)
289          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
290          << (RealList() << 0.0000 << 0.3754 << 0.4692 << 0.4927 << 0.4985 << 0.5000 << 0.5015 << 0.5073 << 0.5308 << 0.6245 << 1.0000);
291 
292     QTest::newRow("InCirc") << int(QEasingCurve::InCirc)
293          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
294          << (RealList() << 0.0000 << 0.0050 << 0.0202 << 0.0461 << 0.0835 << 0.1340 << 0.2000 << 0.2859 << 0.4000 << 0.5641 << 1.0000);
295 
296     QTest::newRow("OutCirc") << int(QEasingCurve::OutCirc)
297          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
298          << (RealList() << 0.0000 << 0.4359 << 0.6000 << 0.7141 << 0.8000 << 0.8660 << 0.9165 << 0.9539 << 0.9798 << 0.9950 << 1.0000);
299 
300     QTest::newRow("InOutCirc") << int(QEasingCurve::InOutCirc)
301          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
302          << (RealList() << 0.0000 << 0.0101 << 0.0417 << 0.1000 << 0.2000 << 0.5000 << 0.8000 << 0.9000 << 0.9583 << 0.9899 << 1.0000);
303 
304     QTest::newRow("OutInCirc") << int(QEasingCurve::OutInCirc)
305          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
306          << (RealList() << 0.0000 << 0.3000 << 0.4000 << 0.4583 << 0.4899 << 0.5000 << 0.5101 << 0.5417 << 0.6000 << 0.7000 << 1.0000);
307 
308     QTest::newRow("InElastic") << int(QEasingCurve::InElastic)
309          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
310          << (RealList() << 0.0000 << 0.0020 << -0.0020 << -0.0039 << 0.0156 << -0.0156 << -0.0313 << 0.1250 << -0.1250 << -0.2500 << 1.0000);
311 
312     QTest::newRow("OutElastic") << int(QEasingCurve::OutElastic)
313          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
314          << (RealList() << 0.0000 << 1.2500 << 1.1250 << 0.8750 << 1.0313 << 1.0156 << 0.9844 << 1.0039 << 1.0020 << 0.9980 << 1.0000);
315 
316     QTest::newRow("InOutElastic") << int(QEasingCurve::InOutElastic)
317          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
318          << (RealList() << 0.0000 << -0.0010 << 0.0078 << -0.0156 << -0.0625 << 0.5000 << 1.0625 << 1.0156 << 0.9922 << 1.0010 << 1.0000);
319 
320     QTest::newRow("OutInElastic") << int(QEasingCurve::OutInElastic)
321          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
322          << (RealList() << 0.0000 << 0.3750 << 0.5625 << 0.4922 << 0.4980 << 0.5000 << 0.4961 << 0.5078 << 0.5313 << 0.2500 << 1.0000);
323 
324     QTest::newRow("InBack") << int(QEasingCurve::InBack)
325          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
326          << (RealList() << 0.0000 << -0.0143 << -0.0465 << -0.0802 << -0.0994 << -0.0877 << -0.0290 << 0.0929 << 0.2942 << 0.5912 << 1.0000);
327 
328     QTest::newRow("OutBack") << int(QEasingCurve::OutBack)
329          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
330          << (RealList() << 0.0000 << 0.4088 << 0.7058 << 0.9071 << 1.0290 << 1.0877 << 1.0994 << 1.0802 << 1.0465 << 1.0143 << 1.0000);
331 
332     QTest::newRow("InOutBack") << int(QEasingCurve::InOutBack)
333          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
334          << (RealList() << 0.0000 << -0.0375 << -0.0926 << -0.0788 << 0.0899 << 0.5000 << 0.9101 << 1.0788 << 1.0926 << 1.0375 << 1.0000);
335 
336     QTest::newRow("OutInBack") << int(QEasingCurve::OutInBack)
337          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
338          << (RealList() << 0.0000 << 0.3529 << 0.5145 << 0.5497 << 0.5232 << 0.5000 << 0.4768 << 0.4503 << 0.4855 << 0.6471 << 1.0000);
339 
340     QTest::newRow("InBounce") << int(QEasingCurve::InBounce)
341          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
342          << (RealList() << 0.0000 << 0.0119 << 0.0600 << 0.0694 << 0.2275 << 0.2344 << 0.0900 << 0.3194 << 0.6975 << 0.9244 << 1.0000);
343 
344     QTest::newRow("OutBounce") << int(QEasingCurve::OutBounce)
345          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
346          << (RealList() << 0.0000 << 0.0756 << 0.3025 << 0.6806 << 0.9100 << 0.7656 << 0.7725 << 0.9306 << 0.9400 << 0.9881 << 1.0000);
347 
348     QTest::newRow("InOutBounce") << int(QEasingCurve::InOutBounce)
349          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
350          << (RealList() << 0.0000 << 0.0300 << 0.1138 << 0.0450 << 0.3488 << 0.5000 << 0.6512 << 0.9550 << 0.8863 << 0.9700 << 1.0000);
351 
352     QTest::newRow("OutInBounce") << int(QEasingCurve::OutInBounce)
353          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
354          << (RealList() << 0.0000 << 0.1513 << 0.4100 << 0.2725 << 0.4400 << 0.5000 << 0.5600 << 0.7275 << 0.5900 << 0.8488 << 1.0000);
355 
356     QTest::newRow("InCurve") << int(QEasingCurve::InCurve)
357          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
358          << (RealList() << 0.0000 << 0.0245 << 0.1059 << 0.2343 << 0.3727 << 0.5000 << 0.6055 << 0.7000 << 0.8000 << 0.9000 << 1.0000);
359 
360     QTest::newRow("OutCurve") << int(QEasingCurve::OutCurve)
361          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
362          << (RealList() << 0.0000 << 0.1000 << 0.2000 << 0.3000 << 0.3945 << 0.5000 << 0.6273 << 0.7657 << 0.8941 << 0.9755 << 1.0000);
363 
364     QTest::newRow("SineCurve") << int(QEasingCurve::SineCurve)
365          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
366          << (RealList() << 0.0000 << 0.0955 << 0.3455 << 0.6545 << 0.9045 << 1.0000 << 0.9045 << 0.6545 << 0.3455 << 0.0955 << 0.0000);
367 
368     QTest::newRow("CosineCurve") << int(QEasingCurve::CosineCurve)
369          << (IntList()  << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100)
370          << (RealList() << 0.5000 << 0.7939 << 0.9755 << 0.9755 << 0.7939 << 0.5000 << 0.2061 << 0.0245 << 0.0245 << 0.2061 << 0.5000);
371 
372 }
373 
374 /*
375   "fixedpoint" number that is scaled up by 10000.
376   This is to work around two bugs (precision and rounding error) in QString::setNum().
377   It does not trim off trailing zeros. This is good, just to emphasize the precision.
378 */
fixedToString(int value)379 QString fixedToString(int value)
380 {
381     QString str;
382     if (value < 0) {
383         str+= QLatin1Char('-');
384         value = -value;
385     }
386 
387     QString digitArg(QLatin1String("%1."));
388     for (int i = 10000; i >= 1; i/=10) {
389         int digit = value/i;
390         value -= digit*i;
391         str.append(digitArg.arg(digit));
392         digitArg = QLatin1String("%1");
393     }
394     return str;
395 }
396 
valueForProgress()397 void tst_QEasingCurve::valueForProgress()
398 {
399 #if 0
400     // used to generate data tables...
401     QFile out;
402     out.open(stdout, QIODevice::WriteOnly);
403     for (int c = QEasingCurve::Linear; c < QEasingCurve::NCurveTypes - 1; ++c) {
404         QEasingCurve curve((QEasingCurve::Type)c);
405         QMetaObject mo = QEasingCurve::staticMetaObject;
406         QString strCurve = QLatin1String(mo.enumerator(mo.indexOfEnumerator("Type")).key(c));
407         QString strInputs;
408         QString strOutputs;
409 
410         for (int t = 0; t <= 100; t+= 10) {
411             qreal ease = curve.valueForProgress(t/qreal(100));
412             strInputs += QString::fromAscii(" << %1").arg(t);
413             strOutputs += " << " + fixedToString(qRound(ease*10000));
414         }
415         QString str = QString::fromAscii("    QTest::newRow(\"%1\") << int(QEasingCurve::%2)\n"
416                                                 "         << (IntList() %3)\n"
417                                                 "         << (RealList()%4);\n\n")
418                                       .arg(strCurve)
419                                       .arg(strCurve)
420                                       .arg(strInputs)
421                                       .arg(strOutputs);
422         out.write(str.toLatin1().constData());
423     }
424     out.close();
425     exit(1);
426 #else
427     QFETCH(int, type);
428     QFETCH(IntList, at);
429     QFETCH(RealList, expected);
430 
431     QEasingCurve curve((QEasingCurve::Type)type);
432     // in theory the baseline should't have an error of more than 0.00005 due to how its rounded,
433     // but due to FP imprecision, we have to adjust the error a bit more.
434     const qreal errorBound = 0.00006;
435     for (int i = 0; i < at.count(); ++i) {
436         const qreal ex = expected.at(i);
437         const qreal error = qAbs(ex - curve.valueForProgress(at.at(i)/qreal(100)));
438         QVERIFY(error <= errorBound);
439     }
440 #endif
441 }
442 
discreteEase(qreal progress)443 static qreal discreteEase(qreal progress)
444 {
445     return qFloor(progress * 10) / qreal(10.0);
446 }
447 
setCustomType()448 void tst_QEasingCurve::setCustomType()
449 {
450     QEasingCurve curve;
451     curve.setCustomType(&discreteEase);
452     QCOMPARE(curve.type(), QEasingCurve::Custom);
453     QCOMPARE(curve.valueForProgress(0.0), 0.0);
454     QCOMPARE(curve.valueForProgress(0.05), 0.0);
455     QCOMPARE(curve.valueForProgress(0.10), 0.1);
456     QCOMPARE(curve.valueForProgress(0.15), 0.1);
457     QCOMPARE(curve.valueForProgress(0.20), 0.2);
458     QCOMPARE(curve.valueForProgress(0.25), 0.2);
459     QCOMPARE(curve.valueForProgress(0.30), 0.3);
460     QCOMPARE(curve.valueForProgress(0.35), 0.3);
461     QCOMPARE(curve.valueForProgress(0.999999), 0.9);
462 
463     curve.setType(QEasingCurve::Linear);
464     QCOMPARE(curve.type(), QEasingCurve::Linear);
465     QCOMPARE(curve.valueForProgress(0.0), 0.0);
466     QCOMPARE(curve.valueForProgress(0.1), 0.1);
467     QCOMPARE(curve.valueForProgress(0.5), 0.5);
468     QCOMPARE(curve.valueForProgress(0.99), 0.99);
469 }
470 
operators()471 void tst_QEasingCurve::operators()
472 {
473     // operator=
474     QEasingCurve curve;
475     QEasingCurve curve2;
476     curve.setCustomType(&discreteEase);
477     curve2 = curve;
478     QCOMPARE(curve2.type(), QEasingCurve::Custom);
479     QCOMPARE(curve2.valueForProgress(0.0), 0.0);
480     QCOMPARE(curve2.valueForProgress(0.05), 0.0);
481     QCOMPARE(curve2.valueForProgress(0.15), 0.1);
482     QCOMPARE(curve2.valueForProgress(0.25), 0.2);
483     QCOMPARE(curve2.valueForProgress(0.35), 0.3);
484     QCOMPARE(curve2.valueForProgress(0.999999), 0.9);
485 
486     // operator==
487     curve.setType(QEasingCurve::InBack);
488     curve2 = curve;
489     curve2.setOvershoot(qreal(1.70158));
490     QCOMPARE(curve.overshoot(), curve2.overshoot());
491     QVERIFY(curve2 == curve);
492 
493     curve.setOvershoot(3.0);
494     QVERIFY(curve2 != curve);
495     curve2.setOvershoot(3.0);
496     QVERIFY(curve2 == curve);
497 
498     curve2.setType(QEasingCurve::Linear);
499     QCOMPARE(curve.overshoot(), curve2.overshoot());
500     QVERIFY(curve2 != curve);
501     curve2.setType(QEasingCurve::InBack);
502     QCOMPARE(curve.overshoot(), curve2.overshoot());
503     QVERIFY(curve2 == curve);
504 
505     QEasingCurve curve3;
506     QEasingCurve curve4;
507     curve4.setAmplitude(curve4.amplitude());
508     QEasingCurve curve5;
509     curve5.setAmplitude(0.12345);
510     QVERIFY(curve3 == curve4); // default value and not assigned
511     QVERIFY(curve3 != curve5); // unassinged and other value
512     QVERIFY(curve4 != curve5);
513 }
514 
515 class tst_QEasingProperties : public QObject
516 {
517     Q_OBJECT
518     Q_PROPERTY(QEasingCurve easing READ easing WRITE setEasing)
519 public:
tst_QEasingProperties(QObject * parent=0)520     tst_QEasingProperties(QObject *parent = 0) : QObject(parent) {}
521 
easing() const522     QEasingCurve easing() const { return e; }
setEasing(const QEasingCurve & value)523     void setEasing(const QEasingCurve& value) { e = value; }
524 
525 private:
526     QEasingCurve e;
527 };
528 
529 // Test getting and setting easing properties via the metaobject system.
properties()530 void tst_QEasingCurve::properties()
531 {
532     tst_QEasingProperties obj;
533 
534     QEasingCurve inOutBack(QEasingCurve::InOutBack);
535     qreal overshoot = 1.5;
536     inOutBack.setOvershoot(overshoot);
537     qreal amplitude = inOutBack.amplitude();
538     qreal period = inOutBack.period();
539 
540     obj.setEasing(inOutBack);
541 
542     QEasingCurve easing = qVariantValue<QEasingCurve>(obj.property("easing"));
543     QCOMPARE(easing.type(), QEasingCurve::InOutBack);
544     QCOMPARE(easing.overshoot(), overshoot);
545     QCOMPARE(easing.amplitude(), amplitude);
546     QCOMPARE(easing.period(), period);
547 
548     QEasingCurve linear(QEasingCurve::Linear);
549     overshoot = linear.overshoot();
550     amplitude = linear.amplitude();
551     period = linear.period();
552 
553     obj.setProperty("easing",
554                     qVariantFromValue(QEasingCurve(QEasingCurve::Linear)));
555 
556     easing = qVariantValue<QEasingCurve>(obj.property("easing"));
557     QCOMPARE(easing.type(), QEasingCurve::Linear);
558     QCOMPARE(easing.overshoot(), overshoot);
559     QCOMPARE(easing.amplitude(), amplitude);
560     QCOMPARE(easing.period(), period);
561 }
562 
metaTypes()563 void tst_QEasingCurve::metaTypes()
564 {
565     QVERIFY(QMetaType::type("QEasingCurve") == QMetaType::QEasingCurve);
566 
567     QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QEasingCurve)),
568              QByteArray("QEasingCurve"));
569 
570     QVERIFY(QMetaType::isRegistered(QMetaType::QEasingCurve));
571 
572     QVERIFY(qMetaTypeId<QEasingCurve>() == QMetaType::QEasingCurve);
573 }
574 
575 QTEST_MAIN(tst_QEasingCurve)
576 #include "tst_qeasingcurve.moc"
577