~loic.molinari/+junk/qtdeclarative-shadereffectsource-changes

« back to all changes in this revision

Viewing changes to tests/auto/quick/qquickanimatedimage/tst_qquickanimatedimage.cpp

  • Committer: Loïc Molinari
  • Date: 2012-04-21 17:59:51 UTC
  • Revision ID: loic.molinari@canonical.com-20120421175951-bqx68caaf5zrp76l
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** GNU Lesser General Public License Usage
 
10
** This file may be used under the terms of the GNU Lesser General Public
 
11
** License version 2.1 as published by the Free Software Foundation and
 
12
** appearing in the file LICENSE.LGPL included in the packaging of this
 
13
** file. Please review the following information to ensure the GNU Lesser
 
14
** General Public License version 2.1 requirements will be met:
 
15
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
16
**
 
17
** In addition, as a special exception, Nokia gives you certain additional
 
18
** rights. These rights are described in the Nokia Qt LGPL Exception
 
19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
20
**
 
21
** GNU General Public License Usage
 
22
** Alternatively, this file may be used under the terms of the GNU General
 
23
** Public License version 3.0 as published by the Free Software Foundation
 
24
** and appearing in the file LICENSE.GPL included in the packaging of this
 
25
** file. Please review the following information to ensure the GNU General
 
26
** Public License version 3.0 requirements will be met:
 
27
** http://www.gnu.org/copyleft/gpl.html.
 
28
**
 
29
** Other Usage
 
30
** Alternatively, this file may be used in accordance with the terms and
 
31
** conditions contained in a signed written agreement between you and Nokia.
 
32
**
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
#include <qtest.h>
 
42
#include <QtQml/qqmlengine.h>
 
43
#include <QtQml/qqmlcomponent.h>
 
44
#include <QtQuick/qquickview.h>
 
45
#include <QtQuick/private/qquickrectangle_p.h>
 
46
#include <private/qquickimage_p.h>
 
47
#include <private/qquickanimatedimage_p.h>
 
48
#include <QSignalSpy>
 
49
#include <QtQml/qqmlcontext.h>
 
50
 
 
51
#include "../../shared/testhttpserver.h"
 
52
#include "../../shared/util.h"
 
53
 
 
54
Q_DECLARE_METATYPE(QQuickImageBase::Status)
 
55
 
 
56
class tst_qquickanimatedimage : public QQmlDataTest
 
57
{
 
58
    Q_OBJECT
 
59
public:
 
60
    tst_qquickanimatedimage() {}
 
61
 
 
62
private slots:
 
63
    void play();
 
64
    void pause();
 
65
    void stopped();
 
66
    void setFrame();
 
67
    void frameCount();
 
68
    void mirror_running();
 
69
    void mirror_notRunning();
 
70
    void mirror_notRunning_data();
 
71
    void remote();
 
72
    void remote_data();
 
73
    void sourceSize();
 
74
    void sourceSizeReadOnly();
 
75
    void invalidSource();
 
76
    void qtbug_16520();
 
77
    void progressAndStatusChanges();
 
78
 
 
79
};
 
80
 
 
81
void tst_qquickanimatedimage::play()
 
82
{
 
83
    QQmlEngine engine;
 
84
    QQmlComponent component(&engine, testFileUrl("stickman.qml"));
 
85
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
86
    QVERIFY(anim);
 
87
    QVERIFY(anim->isPlaying());
 
88
 
 
89
    delete anim;
 
90
}
 
91
 
 
92
void tst_qquickanimatedimage::pause()
 
93
{
 
94
    QQmlEngine engine;
 
95
    QQmlComponent component(&engine, testFileUrl("stickmanpause.qml"));
 
96
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
97
    QVERIFY(anim);
 
98
    QVERIFY(anim->isPlaying());
 
99
    QVERIFY(anim->isPaused());
 
100
 
 
101
    delete anim;
 
102
}
 
103
 
 
104
void tst_qquickanimatedimage::stopped()
 
105
{
 
106
    QQmlEngine engine;
 
107
    QQmlComponent component(&engine, testFileUrl("stickmanstopped.qml"));
 
108
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
109
    QVERIFY(anim);
 
110
    QVERIFY(!anim->isPlaying());
 
111
    QCOMPARE(anim->currentFrame(), 0);
 
112
 
 
113
    delete anim;
 
114
}
 
115
 
 
116
void tst_qquickanimatedimage::setFrame()
 
117
{
 
118
    QQmlEngine engine;
 
119
    QQmlComponent component(&engine, testFileUrl("stickmanpause.qml"));
 
120
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
121
    QVERIFY(anim);
 
122
    QVERIFY(anim->isPlaying());
 
123
    QCOMPARE(anim->currentFrame(), 2);
 
124
 
 
125
    delete anim;
 
126
}
 
127
 
 
128
void tst_qquickanimatedimage::frameCount()
 
129
{
 
130
    QQmlEngine engine;
 
131
    QQmlComponent component(&engine, testFileUrl("colors.qml"));
 
132
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
133
    QVERIFY(anim);
 
134
    QVERIFY(anim->isPlaying());
 
135
    QCOMPARE(anim->frameCount(), 3);
 
136
 
 
137
    delete anim;
 
138
}
 
139
 
 
140
void tst_qquickanimatedimage::mirror_running()
 
141
{
 
142
    // test where mirror is set to true after animation has started
 
143
 
 
144
    QQuickView canvas;
 
145
    canvas.show();
 
146
 
 
147
    canvas.setSource(testFileUrl("hearts.qml"));
 
148
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(canvas.rootObject());
 
149
    QVERIFY(anim);
 
150
 
 
151
    int width = anim->property("width").toInt();
 
152
 
 
153
    QCOMPARE(anim->currentFrame(), 0);
 
154
    QPixmap frame0 = QPixmap::fromImage(canvas.grabFrameBuffer());
 
155
 
 
156
    anim->setCurrentFrame(1);
 
157
    QPixmap frame1 = QPixmap::fromImage(canvas.grabFrameBuffer());
 
158
 
 
159
    anim->setCurrentFrame(0);
 
160
 
 
161
    QSignalSpy spy(anim, SIGNAL(frameChanged()));
 
162
    anim->setPlaying(true);
 
163
 
 
164
    QTRY_VERIFY(spy.count() == 1); spy.clear();
 
165
    anim->setProperty("mirror", true);
 
166
 
 
167
    QCOMPARE(anim->currentFrame(), 1);
 
168
    QPixmap frame1_flipped = QPixmap::fromImage(canvas.grabFrameBuffer());
 
169
 
 
170
    QTRY_VERIFY(spy.count() == 1); spy.clear();
 
171
    QCOMPARE(anim->currentFrame(), 0);  // animation only has 2 frames, should cycle back to first
 
172
    QPixmap frame0_flipped = QPixmap::fromImage(canvas.grabFrameBuffer());
 
173
 
 
174
    QSKIP("Skip while QTBUG-19351 and QTBUG-19252 are not resolved");
 
175
 
 
176
    QTransform transform;
 
177
    transform.translate(width, 0).scale(-1, 1.0);
 
178
    QPixmap frame0_expected = frame0.transformed(transform);
 
179
    QPixmap frame1_expected = frame1.transformed(transform);
 
180
 
 
181
    QCOMPARE(frame0_flipped, frame0_expected);
 
182
    QCOMPARE(frame1_flipped, frame1_expected);
 
183
}
 
184
 
 
185
void tst_qquickanimatedimage::mirror_notRunning()
 
186
{
 
187
    QFETCH(QUrl, fileUrl);
 
188
 
 
189
    QQuickView canvas;
 
190
    canvas.show();
 
191
 
 
192
    canvas.setSource(fileUrl);
 
193
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(canvas.rootObject());
 
194
    QVERIFY(anim);
 
195
 
 
196
    int width = anim->property("width").toInt();
 
197
    QPixmap screenshot = QPixmap::fromImage(canvas.grabFrameBuffer());
 
198
 
 
199
    QTransform transform;
 
200
    transform.translate(width, 0).scale(-1, 1.0);
 
201
    QPixmap expected = screenshot.transformed(transform);
 
202
 
 
203
    int frame = anim->currentFrame();
 
204
    bool playing = anim->isPlaying();
 
205
    bool paused = anim->isPlaying();
 
206
 
 
207
    anim->setProperty("mirror", true);
 
208
    screenshot = QPixmap::fromImage(canvas.grabFrameBuffer());
 
209
 
 
210
    QSKIP("Skip while QTBUG-19351 and QTBUG-19252 are not resolved");
 
211
    QCOMPARE(screenshot, expected);
 
212
 
 
213
    // mirroring should not change the current frame or playing status
 
214
    QCOMPARE(anim->currentFrame(), frame);
 
215
    QCOMPARE(anim->isPlaying(), playing);
 
216
    QCOMPARE(anim->isPaused(), paused);
 
217
}
 
218
 
 
219
void tst_qquickanimatedimage::mirror_notRunning_data()
 
220
{
 
221
    QTest::addColumn<QUrl>("fileUrl");
 
222
 
 
223
    QTest::newRow("paused") << testFileUrl("stickmanpause.qml");
 
224
    QTest::newRow("stopped") << testFileUrl("stickmanstopped.qml");
 
225
}
 
226
 
 
227
void tst_qquickanimatedimage::remote()
 
228
{
 
229
    QFETCH(QString, fileName);
 
230
    QFETCH(bool, paused);
 
231
 
 
232
    TestHTTPServer server(14449);
 
233
    QVERIFY(server.isValid());
 
234
    server.serveDirectory(dataDirectory());
 
235
 
 
236
    QQmlEngine engine;
 
237
    QQmlComponent component(&engine, QUrl("http://127.0.0.1:14449/" + fileName));
 
238
    QTRY_VERIFY(component.isReady());
 
239
 
 
240
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
241
    QVERIFY(anim);
 
242
 
 
243
    QTRY_VERIFY(anim->isPlaying());
 
244
    if (paused) {
 
245
        QTRY_VERIFY(anim->isPaused());
 
246
        QCOMPARE(anim->currentFrame(), 2);
 
247
    }
 
248
    QVERIFY(anim->status() != QQuickAnimatedImage::Error);
 
249
 
 
250
    delete anim;
 
251
}
 
252
 
 
253
void tst_qquickanimatedimage::sourceSize()
 
254
{
 
255
    QQmlEngine engine;
 
256
    QQmlComponent component(&engine, testFileUrl("stickmanscaled.qml"));
 
257
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
258
    QVERIFY(anim);
 
259
    QCOMPARE(anim->width(),240.0);
 
260
    QCOMPARE(anim->height(),180.0);
 
261
    QCOMPARE(anim->sourceSize(),QSize(160,120));
 
262
 
 
263
    delete anim;
 
264
}
 
265
 
 
266
void tst_qquickanimatedimage::sourceSizeReadOnly()
 
267
{
 
268
    QQmlEngine engine;
 
269
    QQmlComponent component(&engine, testFileUrl("stickmanerror1.qml"));
 
270
    QVERIFY(component.isError());
 
271
    QCOMPARE(component.errors().at(0).description(), QString("Invalid property assignment: \"sourceSize\" is a read-only property"));
 
272
}
 
273
 
 
274
void tst_qquickanimatedimage::remote_data()
 
275
{
 
276
    QTest::addColumn<QString>("fileName");
 
277
    QTest::addColumn<bool>("paused");
 
278
 
 
279
    QTest::newRow("playing") << "stickman.qml" << false;
 
280
    QTest::newRow("paused") << "stickmanpause.qml" << true;
 
281
}
 
282
 
 
283
void tst_qquickanimatedimage::invalidSource()
 
284
{
 
285
    QQmlEngine engine;
 
286
    QQmlComponent component(&engine);
 
287
    component.setData("import QtQuick 2.0\n AnimatedImage { source: \"no-such-file.gif\" }", QUrl::fromLocalFile(""));
 
288
    QVERIFY(component.isReady());
 
289
 
 
290
    QTest::ignoreMessage(QtWarningMsg, "file::2:2: QML AnimatedImage: Error Reading Animated Image File file:no-such-file.gif");
 
291
 
 
292
    QQuickAnimatedImage *anim = qobject_cast<QQuickAnimatedImage *>(component.create());
 
293
    QVERIFY(anim);
 
294
 
 
295
    QVERIFY(!anim->isPlaying());
 
296
    QVERIFY(!anim->isPaused());
 
297
    QCOMPARE(anim->currentFrame(), 0);
 
298
    QCOMPARE(anim->frameCount(), 0);
 
299
    QTRY_VERIFY(anim->status() == 3);
 
300
}
 
301
 
 
302
void tst_qquickanimatedimage::qtbug_16520()
 
303
{
 
304
    TestHTTPServer server(14449);
 
305
    QVERIFY(server.isValid());
 
306
    server.serveDirectory(dataDirectory());
 
307
 
 
308
    QQmlEngine engine;
 
309
    QQmlComponent component(&engine, testFileUrl("qtbug-16520.qml"));
 
310
    QTRY_VERIFY(component.isReady());
 
311
 
 
312
    QQuickRectangle *root = qobject_cast<QQuickRectangle *>(component.create());
 
313
    QVERIFY(root);
 
314
    QQuickAnimatedImage *anim = root->findChild<QQuickAnimatedImage*>("anim");
 
315
 
 
316
    anim->setProperty("source", "http://127.0.0.1:14449/stickman.gif");
 
317
 
 
318
    QTRY_VERIFY(anim->opacity() == 0);
 
319
    QTRY_VERIFY(anim->opacity() == 1);
 
320
 
 
321
    delete anim;
 
322
}
 
323
 
 
324
void tst_qquickanimatedimage::progressAndStatusChanges()
 
325
{
 
326
    TestHTTPServer server(14449);
 
327
    QVERIFY(server.isValid());
 
328
    server.serveDirectory(dataDirectory());
 
329
 
 
330
    QQmlEngine engine;
 
331
    QString componentStr = "import QtQuick 2.0\nAnimatedImage { source: srcImage }";
 
332
    QQmlContext *ctxt = engine.rootContext();
 
333
    ctxt->setContextProperty("srcImage", testFileUrl("stickman.gif"));
 
334
    QQmlComponent component(&engine);
 
335
    component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
 
336
    QQuickImage *obj = qobject_cast<QQuickImage*>(component.create());
 
337
    QVERIFY(obj != 0);
 
338
    QVERIFY(obj->status() == QQuickImage::Ready);
 
339
    QTRY_VERIFY(obj->progress() == 1.0);
 
340
 
 
341
    qRegisterMetaType<QQuickImageBase::Status>();
 
342
    QSignalSpy sourceSpy(obj, SIGNAL(sourceChanged(const QUrl &)));
 
343
    QSignalSpy progressSpy(obj, SIGNAL(progressChanged(qreal)));
 
344
    QSignalSpy statusSpy(obj, SIGNAL(statusChanged(QQuickImageBase::Status)));
 
345
 
 
346
    // Loading local file
 
347
    ctxt->setContextProperty("srcImage", testFileUrl("colors.gif"));
 
348
    QTRY_VERIFY(obj->status() == QQuickImage::Ready);
 
349
    QTRY_VERIFY(obj->progress() == 1.0);
 
350
    QTRY_COMPARE(sourceSpy.count(), 1);
 
351
    QTRY_COMPARE(progressSpy.count(), 0);
 
352
    QTRY_COMPARE(statusSpy.count(), 0);
 
353
 
 
354
    // Loading remote file
 
355
    ctxt->setContextProperty("srcImage", "http://127.0.0.1:14449/stickman.gif");
 
356
    QTRY_VERIFY(obj->status() == QQuickImage::Loading);
 
357
    QTRY_VERIFY(obj->progress() == 0.0);
 
358
    QTRY_VERIFY(obj->status() == QQuickImage::Ready);
 
359
    QTRY_VERIFY(obj->progress() == 1.0);
 
360
    QTRY_COMPARE(sourceSpy.count(), 2);
 
361
    QTRY_VERIFY(progressSpy.count() > 1);
 
362
    QTRY_COMPARE(statusSpy.count(), 2);
 
363
 
 
364
    ctxt->setContextProperty("srcImage", "");
 
365
    QTRY_VERIFY(obj->status() == QQuickImage::Null);
 
366
    QTRY_VERIFY(obj->progress() == 0.0);
 
367
    QTRY_COMPARE(sourceSpy.count(), 3);
 
368
    QTRY_VERIFY(progressSpy.count() > 2);
 
369
    QTRY_COMPARE(statusSpy.count(), 3);
 
370
}
 
371
 
 
372
QTEST_MAIN(tst_qquickanimatedimage)
 
373
 
 
374
#include "tst_qquickanimatedimage.moc"