tests/auto/declarative/qdeclarativeanimatedimage/tst_qdeclarativeanimatedimage.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <qtest.h>
       
    42 #include <QtDeclarative/qdeclarativeengine.h>
       
    43 #include <QtDeclarative/qdeclarativecomponent.h>
       
    44 #include <QtDeclarative/qdeclarativeview.h>
       
    45 #include <private/qdeclarativerectangle_p.h>
       
    46 #include <private/qdeclarativeimage_p.h>
       
    47 #include <private/qdeclarativeanimatedimage_p.h>
       
    48 
       
    49 #include "../shared/testhttpserver.h"
       
    50 
       
    51 #define TRY_WAIT(expr) \
       
    52     do { \
       
    53         for (int ii = 0; ii < 6; ++ii) { \
       
    54             if ((expr)) break; \
       
    55             QTest::qWait(50); \
       
    56         } \
       
    57         QVERIFY((expr)); \
       
    58     } while (false)
       
    59 
       
    60 
       
    61 class tst_qdeclarativeanimatedimage : public QObject
       
    62 {
       
    63     Q_OBJECT
       
    64 public:
       
    65     tst_qdeclarativeanimatedimage() {}
       
    66 
       
    67 private slots:
       
    68     void play();
       
    69     void pause();
       
    70     void stopped();
       
    71     void setFrame();
       
    72     void frameCount();
       
    73     void remote();
       
    74     void remote_data();
       
    75     void sourceSize();
       
    76     void sourceSizeReadOnly();
       
    77     void invalidSource();
       
    78 };
       
    79 
       
    80 void tst_qdeclarativeanimatedimage::play()
       
    81 {
       
    82     QDeclarativeEngine engine;
       
    83     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickman.qml"));
       
    84     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
    85     QVERIFY(anim);
       
    86     QVERIFY(anim->isPlaying());
       
    87 
       
    88     delete anim;
       
    89 }
       
    90 
       
    91 void tst_qdeclarativeanimatedimage::pause()
       
    92 {
       
    93     QDeclarativeEngine engine;
       
    94     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml"));
       
    95     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
    96     QVERIFY(anim);
       
    97     QVERIFY(anim->isPlaying());
       
    98     QVERIFY(anim->isPaused());
       
    99 
       
   100     delete anim;
       
   101 }
       
   102 
       
   103 void tst_qdeclarativeanimatedimage::stopped()
       
   104 {
       
   105     QDeclarativeEngine engine;
       
   106     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanstopped.qml"));
       
   107     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
   108     QVERIFY(anim);
       
   109     QVERIFY(!anim->isPlaying());
       
   110     QCOMPARE(anim->currentFrame(), 0);
       
   111 
       
   112     delete anim;
       
   113 }
       
   114 
       
   115 void tst_qdeclarativeanimatedimage::setFrame()
       
   116 {
       
   117     QDeclarativeEngine engine;
       
   118     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanpause.qml"));
       
   119     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
   120     QVERIFY(anim);
       
   121     QVERIFY(anim->isPlaying());
       
   122     QCOMPARE(anim->currentFrame(), 2);
       
   123 
       
   124     delete anim;
       
   125 }
       
   126 
       
   127 void tst_qdeclarativeanimatedimage::frameCount()
       
   128 {
       
   129     QDeclarativeEngine engine;
       
   130     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/colors.qml"));
       
   131     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
   132     QVERIFY(anim);
       
   133     QVERIFY(anim->isPlaying());
       
   134     QCOMPARE(anim->frameCount(), 3);
       
   135 
       
   136     delete anim;
       
   137 }
       
   138 
       
   139 void tst_qdeclarativeanimatedimage::remote()
       
   140 {
       
   141     QFETCH(QString, fileName);
       
   142     QFETCH(bool, paused);
       
   143 
       
   144     TestHTTPServer server(14449);
       
   145     QVERIFY(server.isValid());
       
   146     server.serveDirectory(SRCDIR "/data");
       
   147 
       
   148     QDeclarativeEngine engine;
       
   149     QDeclarativeComponent component(&engine, QUrl("http://127.0.0.1:14449/" + fileName));
       
   150     TRY_WAIT(component.isReady());
       
   151 
       
   152     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
   153     QVERIFY(anim);
       
   154 
       
   155     TRY_WAIT(anim->isPlaying());
       
   156     if (paused) {
       
   157         TRY_WAIT(anim->isPaused());
       
   158         QCOMPARE(anim->currentFrame(), 2);
       
   159     }
       
   160     QVERIFY(anim->status() != QDeclarativeAnimatedImage::Error);
       
   161 
       
   162     delete anim;
       
   163 }
       
   164 
       
   165 void tst_qdeclarativeanimatedimage::sourceSize()
       
   166 {
       
   167     QDeclarativeEngine engine;
       
   168     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanscaled.qml"));
       
   169     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
   170     QVERIFY(anim);
       
   171     QCOMPARE(anim->width(),240.0);
       
   172     QCOMPARE(anim->height(),180.0);
       
   173     QCOMPARE(anim->sourceSize(),QSize(160,120));
       
   174 
       
   175     delete anim;
       
   176 }
       
   177 
       
   178 void tst_qdeclarativeanimatedimage::sourceSizeReadOnly()
       
   179 {
       
   180     QDeclarativeEngine engine;
       
   181     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/stickmanerror1.qml"));
       
   182     QVERIFY(component.isError());
       
   183     QCOMPARE(component.errors().at(0).description(), QString("Invalid property assignment: \"sourceSize\" is a read-only property"));
       
   184 }
       
   185 
       
   186 void tst_qdeclarativeanimatedimage::remote_data()
       
   187 {
       
   188     QTest::addColumn<QString>("fileName");
       
   189     QTest::addColumn<bool>("paused");
       
   190 
       
   191     QTest::newRow("playing") << "stickman.qml" << false;
       
   192     QTest::newRow("paused") << "stickmanpause.qml" << true;
       
   193 }
       
   194 
       
   195 void tst_qdeclarativeanimatedimage::invalidSource()
       
   196 {
       
   197     QDeclarativeEngine engine;
       
   198     QDeclarativeComponent component(&engine);
       
   199     component.setData("import Qt 4.7\n AnimatedImage { source: \"no-such-file.gif\" }", QUrl::fromLocalFile(""));
       
   200     QVERIFY(component.isReady());
       
   201 
       
   202     QTest::ignoreMessage(QtWarningMsg, "file::2:2: QML AnimatedImage: Error Reading Animated Image File file:no-such-file.gif");
       
   203 
       
   204     QDeclarativeAnimatedImage *anim = qobject_cast<QDeclarativeAnimatedImage *>(component.create());
       
   205     QVERIFY(anim);
       
   206 
       
   207     QVERIFY(!anim->isPlaying());
       
   208     QVERIFY(!anim->isPaused());
       
   209     QCOMPARE(anim->currentFrame(), 0);
       
   210     QCOMPARE(anim->frameCount(), 0);
       
   211 }
       
   212 
       
   213 QTEST_MAIN(tst_qdeclarativeanimatedimage)
       
   214 
       
   215 #include "tst_qdeclarativeanimatedimage.moc"