| 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 | |
| 33 | #include <QIODevice> |
| 34 | #ifndef QT_NO_WIDGETS |
| 35 | #include <QLabel> |
| 36 | #endif |
| 37 | #include <QMovie> |
| 38 | |
| 39 | class tst_QMovie : public QObject |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | |
| 43 | public: |
| 44 | tst_QMovie(); |
| 45 | virtual ~tst_QMovie(); |
| 46 | |
| 47 | public slots: |
| 48 | void init(); |
| 49 | void cleanup(); |
| 50 | |
| 51 | protected slots: |
| 52 | void exitLoopSlot(); |
| 53 | |
| 54 | private slots: |
| 55 | void getSetCheck(); |
| 56 | void construction(); |
| 57 | void playMovie_data(); |
| 58 | void playMovie(); |
| 59 | void jumpToFrame_data(); |
| 60 | void jumpToFrame(); |
| 61 | void changeMovieFile(); |
| 62 | #ifndef QT_NO_WIDGETS |
| 63 | void infiniteLoop(); |
| 64 | #endif |
| 65 | void emptyMovie(); |
| 66 | }; |
| 67 | |
| 68 | // Testing get/set functions |
| 69 | void tst_QMovie::getSetCheck() |
| 70 | { |
| 71 | QMovie obj1; |
| 72 | // QIODevice * QMovie::device() |
| 73 | // void QMovie::setDevice(QIODevice *) |
| 74 | QFile *var1 = new QFile; |
| 75 | obj1.setDevice(var1); |
| 76 | QCOMPARE(obj1.device(), (QIODevice *)var1); |
| 77 | obj1.setDevice((QIODevice *)0); |
| 78 | QCOMPARE(obj1.device(), (QIODevice *)0); |
| 79 | delete var1; |
| 80 | |
| 81 | // CacheMode QMovie::cacheMode() |
| 82 | // void QMovie::setCacheMode(CacheMode) |
| 83 | obj1.setCacheMode(QMovie::CacheMode(QMovie::CacheNone)); |
| 84 | QCOMPARE(QMovie::CacheMode(QMovie::CacheNone), obj1.cacheMode()); |
| 85 | obj1.setCacheMode(QMovie::CacheMode(QMovie::CacheAll)); |
| 86 | QCOMPARE(QMovie::CacheMode(QMovie::CacheAll), obj1.cacheMode()); |
| 87 | |
| 88 | // int QMovie::speed() |
| 89 | // void QMovie::setSpeed(int) |
| 90 | obj1.setSpeed(0); |
| 91 | QCOMPARE(0, obj1.speed()); |
| 92 | obj1.setSpeed(INT_MIN); |
| 93 | QCOMPARE(INT_MIN, obj1.speed()); |
| 94 | obj1.setSpeed(INT_MAX); |
| 95 | QCOMPARE(INT_MAX, obj1.speed()); |
| 96 | } |
| 97 | |
| 98 | tst_QMovie::tst_QMovie() |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | tst_QMovie::~tst_QMovie() |
| 103 | { |
| 104 | |
| 105 | } |
| 106 | |
| 107 | void tst_QMovie::init() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | void tst_QMovie::cleanup() |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | void tst_QMovie::exitLoopSlot() |
| 116 | { |
| 117 | QTestEventLoop::instance().exitLoop(); |
| 118 | } |
| 119 | |
| 120 | void tst_QMovie::construction() |
| 121 | { |
| 122 | QMovie movie; |
| 123 | QCOMPARE(movie.device(), (QIODevice *)0); |
| 124 | QCOMPARE(movie.fileName(), QString()); |
| 125 | QCOMPARE(movie.state(), QMovie::NotRunning); |
| 126 | } |
| 127 | |
| 128 | void tst_QMovie::playMovie_data() |
| 129 | { |
| 130 | QTest::addColumn<QString>(name: "fileName" ); |
| 131 | QTest::addColumn<int>(name: "frameCount" ); |
| 132 | #ifdef QTEST_HAVE_GIF |
| 133 | QTest::newRow(dataTag: "comicsecard" ) << QString("animations/comicsecard.gif" ) << 5; |
| 134 | QTest::newRow(dataTag: "trolltech" ) << QString("animations/trolltech.gif" ) << 34; |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | void tst_QMovie::playMovie() |
| 139 | { |
| 140 | QFETCH(QString, fileName); |
| 141 | QFETCH(int, frameCount); |
| 142 | |
| 143 | QMovie movie(QFINDTESTDATA(fileName)); |
| 144 | |
| 145 | QCOMPARE(movie.state(), QMovie::NotRunning); |
| 146 | movie.setSpeed(1000); |
| 147 | movie.start(); |
| 148 | QCOMPARE(movie.state(), QMovie::Running); |
| 149 | movie.setPaused(true); |
| 150 | QCOMPARE(movie.state(), QMovie::Paused); |
| 151 | movie.start(); |
| 152 | QCOMPARE(movie.state(), QMovie::Running); |
| 153 | movie.stop(); |
| 154 | QCOMPARE(movie.state(), QMovie::NotRunning); |
| 155 | movie.jumpToFrame(frameNumber: 0); |
| 156 | QCOMPARE(movie.state(), QMovie::NotRunning); |
| 157 | movie.start(); |
| 158 | QCOMPARE(movie.state(), QMovie::Running); |
| 159 | |
| 160 | connect(sender: &movie, SIGNAL(finished()), receiver: this, SLOT(exitLoopSlot())); |
| 161 | |
| 162 | #ifndef QT_NO_WIDGETS |
| 163 | QLabel label; |
| 164 | label.setMovie(&movie); |
| 165 | label.show(); |
| 166 | |
| 167 | QTestEventLoop::instance().enterLoop(secs: 20); |
| 168 | QVERIFY2(!QTestEventLoop::instance().timeout(), |
| 169 | "Timed out while waiting for finished() signal" ); |
| 170 | |
| 171 | QCOMPARE(movie.state(), QMovie::NotRunning); |
| 172 | QCOMPARE(movie.frameCount(), frameCount); |
| 173 | #endif |
| 174 | |
| 175 | movie.stop(); |
| 176 | QSignalSpy finishedSpy(&movie, &QMovie::finished); |
| 177 | movie.setSpeed(0); |
| 178 | movie.start(); |
| 179 | QCOMPARE(movie.state(), QMovie::Running); |
| 180 | QTestEventLoop::instance().enterLoop(secs: 2); |
| 181 | QCOMPARE(finishedSpy.count(), 0); |
| 182 | QCOMPARE(movie.state(), QMovie::Running); |
| 183 | QCOMPARE(movie.currentFrameNumber(), 0); |
| 184 | } |
| 185 | |
| 186 | void tst_QMovie::jumpToFrame_data() |
| 187 | { |
| 188 | playMovie_data(); |
| 189 | } |
| 190 | |
| 191 | void tst_QMovie::jumpToFrame() |
| 192 | { |
| 193 | QFETCH(QString, fileName); |
| 194 | QMovie movie(QFINDTESTDATA(fileName)); |
| 195 | movie.start(); |
| 196 | movie.stop(); |
| 197 | QVERIFY(!movie.jumpToFrame(-1)); |
| 198 | QCOMPARE(movie.currentFrameNumber(), 0); |
| 199 | } |
| 200 | |
| 201 | void tst_QMovie::changeMovieFile() |
| 202 | { |
| 203 | QMovie movie(QFINDTESTDATA("animations/comicsecard.gif" )); |
| 204 | movie.start(); |
| 205 | movie.stop(); |
| 206 | movie.setFileName(QFINDTESTDATA("animations/trolltech.gif" )); |
| 207 | QCOMPARE(movie.currentFrameNumber(), -1); |
| 208 | } |
| 209 | |
| 210 | #ifndef QT_NO_WIDGETS |
| 211 | void tst_QMovie::infiniteLoop() |
| 212 | { |
| 213 | QLabel label; |
| 214 | label.show(); |
| 215 | QMovie *movie = new QMovie(QLatin1String(":animations/corrupt.gif" ), QByteArray(), &label); |
| 216 | label.setMovie(movie); |
| 217 | movie->start(); |
| 218 | |
| 219 | QTestEventLoop::instance().enterLoop(secs: 1); |
| 220 | QTestEventLoop::instance().timeout(); |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | void tst_QMovie::emptyMovie() |
| 225 | { |
| 226 | QMovie movie; |
| 227 | movie.setCacheMode(QMovie::CacheAll); |
| 228 | movie.jumpToFrame(frameNumber: 100); |
| 229 | QCOMPARE(movie.currentFrameNumber(), -1); |
| 230 | } |
| 231 | |
| 232 | QTEST_MAIN(tst_QMovie) |
| 233 | #include "tst_qmovie.moc" |
| 234 | |