| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite 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 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | #include <qgraphicsitemanimation.h> |
| 33 | #include <QtCore/qtimeline.h> |
| 34 | |
| 35 | class tst_QGraphicsItemAnimation : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | private slots: |
| 40 | void construction(); |
| 41 | void linearMove(); |
| 42 | void linearRotation(); |
| 43 | void checkReturnedLists(); |
| 44 | void overwriteValueForStep(); |
| 45 | void setTimeLine(); |
| 46 | }; |
| 47 | |
| 48 | void tst_QGraphicsItemAnimation::construction() |
| 49 | { |
| 50 | QGraphicsItemAnimation animation; |
| 51 | QVERIFY(!animation.item()); |
| 52 | QVERIFY(!animation.timeLine()); |
| 53 | QCOMPARE(animation.posAt(0), QPointF()); |
| 54 | QCOMPARE(animation.posAt(0.5), QPointF()); |
| 55 | QCOMPARE(animation.posAt(1), QPointF()); |
| 56 | QCOMPARE(animation.transformAt(0), QTransform()); |
| 57 | QCOMPARE(animation.transformAt(0.5), QTransform()); |
| 58 | QCOMPARE(animation.transformAt(1), QTransform()); |
| 59 | QCOMPARE(animation.rotationAt(0), qreal(0.0)); |
| 60 | QCOMPARE(animation.rotationAt(0.5), qreal(0.0)); |
| 61 | QCOMPARE(animation.rotationAt(1), qreal(0.0)); |
| 62 | QCOMPARE(animation.xTranslationAt(0), qreal(0.0)); |
| 63 | QCOMPARE(animation.xTranslationAt(0.5), qreal(0.0)); |
| 64 | QCOMPARE(animation.xTranslationAt(1), qreal(0.0)); |
| 65 | QCOMPARE(animation.yTranslationAt(0), qreal(0.0)); |
| 66 | QCOMPARE(animation.yTranslationAt(0.5), qreal(0.0)); |
| 67 | QCOMPARE(animation.yTranslationAt(1), qreal(0.0)); |
| 68 | QCOMPARE(animation.verticalScaleAt(0), qreal(1.0)); |
| 69 | QCOMPARE(animation.horizontalScaleAt(0), qreal(1.0)); |
| 70 | QCOMPARE(animation.verticalShearAt(0), qreal(0.0)); |
| 71 | QCOMPARE(animation.horizontalShearAt(0), qreal(0.0)); |
| 72 | animation.clear(); // don't crash |
| 73 | } |
| 74 | |
| 75 | void tst_QGraphicsItemAnimation::linearMove() |
| 76 | { |
| 77 | QGraphicsItemAnimation animation; |
| 78 | |
| 79 | for (int i = 0; i <= 10; ++i) { |
| 80 | QCOMPARE(animation.posAt(i / 10.0).x(), qreal(0)); |
| 81 | QCOMPARE(animation.posAt(i / 10.0).y(), qreal(0)); |
| 82 | } |
| 83 | |
| 84 | animation.setPosAt(step: 1, pos: QPointF(10, -10)); |
| 85 | |
| 86 | for (int i = 0; i <= 10; ++i) { |
| 87 | QCOMPARE(animation.posAt(i / 10.0).x(), qreal(i)); |
| 88 | QCOMPARE(animation.posAt(i / 10.0).y(), qreal(-i)); |
| 89 | } |
| 90 | |
| 91 | animation.setPosAt(step: 2, pos: QPointF(10, -10)); |
| 92 | |
| 93 | QCOMPARE(animation.posAt(11).x(), qreal(10)); |
| 94 | } |
| 95 | |
| 96 | void tst_QGraphicsItemAnimation::linearRotation() |
| 97 | { |
| 98 | QGraphicsItemAnimation animation; |
| 99 | animation.setRotationAt(step: 1, angle: 1); |
| 100 | |
| 101 | for (int i = 0; i <= 10; ++i) |
| 102 | QCOMPARE(animation.rotationAt(i / 10.0), qreal(i / 10.0)); |
| 103 | } |
| 104 | |
| 105 | void tst_QGraphicsItemAnimation::checkReturnedLists() |
| 106 | { |
| 107 | QGraphicsItemAnimation animation; |
| 108 | |
| 109 | animation.setPosAt(step: 1.0, pos: QPointF(10, -10)); |
| 110 | animation.setPosAt(step: 0.5, pos: QPointF(5, -5)); |
| 111 | |
| 112 | animation.setRotationAt(step: 0.3, angle: 2.3); |
| 113 | animation.setTranslationAt(step: 0.3, dx: 15, dy: 15); |
| 114 | animation.setScaleAt(step: 0.3, sx: 2.5, sy: 1.8); |
| 115 | animation.setShearAt(step: 0.3, sh: 5, sv: 5); |
| 116 | |
| 117 | QCOMPARE(animation.posList().at(0), (QPair<qreal, QPointF>(0.5, QPointF(5, -5)))); |
| 118 | QCOMPARE(animation.posList().at(1), (QPair<qreal, QPointF>(1.0, QPointF(10, -10)))); |
| 119 | QCOMPARE(animation.rotationList().at(0), (QPair<qreal, qreal>(0.3, 2.3))); |
| 120 | QCOMPARE(animation.translationList().at(0), (QPair<qreal, QPointF>(0.3, QPointF(15, 15)))); |
| 121 | QCOMPARE(animation.scaleList().at(0), (QPair<qreal, QPointF>(0.3, QPointF(2.5, 1.8)))); |
| 122 | QCOMPARE(animation.shearList().at(0), (QPair<qreal, QPointF>(0.3, QPointF(5, 5)))); |
| 123 | |
| 124 | QCOMPARE(animation.posList().size(), 2); |
| 125 | QCOMPARE(animation.rotationList().size(), 1); |
| 126 | QCOMPARE(animation.translationList().size(), 1); |
| 127 | QCOMPARE(animation.scaleList().size(), 1); |
| 128 | QCOMPARE(animation.shearList().size(), 1); |
| 129 | } |
| 130 | |
| 131 | void tst_QGraphicsItemAnimation::overwriteValueForStep() |
| 132 | { |
| 133 | QGraphicsItemAnimation animation; |
| 134 | |
| 135 | for (int i=0; i<3; i++){ |
| 136 | animation.setPosAt(step: 0.3, pos: QPointF(3, -3.1)); |
| 137 | animation.setRotationAt(step: 0.3, angle: 2.3); |
| 138 | animation.setTranslationAt(step: 0.3, dx: 15, dy: 15); |
| 139 | animation.setScaleAt(step: 0.3, sx: 2.5, sy: 1.8); |
| 140 | animation.setShearAt(step: 0.3, sh: 5, sv: 5); |
| 141 | |
| 142 | QCOMPARE(animation.posList().size(), 1); |
| 143 | QCOMPARE(animation.rotationList().size(), 1); |
| 144 | QCOMPARE(animation.translationList().size(), 1); |
| 145 | QCOMPARE(animation.scaleList().size(), 1); |
| 146 | QCOMPARE(animation.shearList().size(), 1); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void tst_QGraphicsItemAnimation::setTimeLine() |
| 151 | { |
| 152 | QGraphicsItemAnimation animation; |
| 153 | QCOMPARE(animation.timeLine(), nullptr); |
| 154 | |
| 155 | QPointer<QTimeLine> line1 = new QTimeLine; |
| 156 | animation.setTimeLine(line1); |
| 157 | QCOMPARE(animation.timeLine(), (QTimeLine *)line1); |
| 158 | animation.setTimeLine(line1); |
| 159 | QVERIFY(line1); |
| 160 | QCOMPARE(animation.timeLine(), (QTimeLine *)line1); |
| 161 | |
| 162 | animation.setTimeLine(0); |
| 163 | QCOMPARE(animation.timeLine(), nullptr); |
| 164 | QVERIFY(!line1); |
| 165 | |
| 166 | QTimeLine *line2 = new QTimeLine; |
| 167 | animation.setTimeLine(line2); |
| 168 | QCOMPARE(animation.timeLine(), (QTimeLine *)line2); |
| 169 | |
| 170 | delete line2; |
| 171 | QCOMPARE(animation.timeLine(), nullptr); |
| 172 | } |
| 173 | |
| 174 | QTEST_MAIN(tst_QGraphicsItemAnimation) |
| 175 | #include "tst_qgraphicsitemanimation.moc" |
| 176 | |