From f11e2ef009c7e39f927c28d8d9d37b9dabf29f80 Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Wed, 8 Sep 2010 15:17:09 +1000 Subject: Changing the Image 'source' should not go through the 'Loading' state if the image is cached. Task-number: QTBUG-13454 Reviewed-by: Martin Jones --- .../graphicsitems/qdeclarativeimagebase.cpp | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativeimagebase.cpp b/src/declarative/graphicsitems/qdeclarativeimagebase.cpp index f0293d6..02b4807 100644 --- a/src/declarative/graphicsitems/qdeclarativeimagebase.cpp +++ b/src/declarative/graphicsitems/qdeclarativeimagebase.cpp @@ -129,27 +129,25 @@ QSize QDeclarativeImageBase::sourceSize() const void QDeclarativeImageBase::load() { Q_D(QDeclarativeImageBase); - if (d->progress != 0.0) { - d->progress = 0.0; - emit progressChanged(d->progress); - } if (d->url.isEmpty()) { d->pix.clear(); d->status = Null; + d->progress = 0.0; setImplicitWidth(0); setImplicitHeight(0); + emit progressChanged(d->progress); emit statusChanged(d->status); pixmapChange(); update(); } else { - - d->status = Loading; - emit statusChanged(d->status); - d->pix.load(qmlEngine(this), d->url, d->sourcesize, d->async); if (d->pix.isLoading()) { + d->progress = 0.0; + d->status = Loading; + emit progressChanged(d->progress); + emit statusChanged(d->status); static int thisRequestProgress = -1; static int thisRequestFinished = -1; @@ -173,6 +171,9 @@ void QDeclarativeImageBase::requestFinished() { Q_D(QDeclarativeImageBase); + QDeclarativeImageBase::Status oldStatus = d->status; + qreal oldProgress = d->progress; + if (d->pix.isError()) { d->status = Error; qmlInfo(this) << d->pix.error(); @@ -191,8 +192,10 @@ void QDeclarativeImageBase::requestFinished() emit sourceSizeChanged(); } - emit statusChanged(d->status); - emit progressChanged(d->progress); + if (d->status != oldStatus) + emit statusChanged(d->status); + if (d->progress != oldProgress) + emit progressChanged(d->progress); pixmapChange(); update(); } -- cgit v0.12 From 4c62387c530787b0db8b159b8a535401a5bb7310 Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Fri, 10 Sep 2010 14:25:19 +1000 Subject: Autotest for QTBUG-13454. --- .../graphicsitems/qdeclarativeimagebase_p.h | 3 +- .../qdeclarativeimage/tst_qdeclarativeimage.cpp | 68 +++++++++++++++++----- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativeimagebase_p.h b/src/declarative/graphicsitems/qdeclarativeimagebase_p.h index f5896b1..68eb8d0 100644 --- a/src/declarative/graphicsitems/qdeclarativeimagebase_p.h +++ b/src/declarative/graphicsitems/qdeclarativeimagebase_p.h @@ -58,7 +58,6 @@ class Q_AUTOTEST_EXPORT QDeclarativeImageBase : public QDeclarativeItem Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) - Q_PROPERTY(QSize sourceSize READ sourceSize WRITE setSourceSize NOTIFY sourceSizeChanged) public: @@ -79,7 +78,7 @@ public: Q_SIGNALS: void sourceChanged(const QUrl &); void sourceSizeChanged(); - void statusChanged(Status); + void statusChanged(QDeclarativeImageBase::Status); void progressChanged(qreal progress); void asynchronousChanged(); diff --git a/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp b/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp index 8cfb487..2686127 100644 --- a/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp +++ b/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp @@ -52,7 +52,9 @@ #include #include #include +#include +#include "../../../shared/util.h" #include "../shared/testhttpserver.h" #ifdef Q_OS_SYMBIAN @@ -63,18 +65,7 @@ #define SERVER_PORT 14451 #define SERVER_ADDR "http://127.0.0.1:14451" -#define TRY_WAIT(expr) \ - do { \ - for (int ii = 0; ii < 60; ++ii) { \ - if ((expr)) break; \ - QTest::qWait(50); \ - } \ - QVERIFY((expr)); \ - } while (false) - - class tst_qdeclarativeimage : public QObject - { Q_OBJECT public: @@ -91,6 +82,7 @@ private slots: void svg(); void big(); void tiling_QTBUG_6716(); + void noLoading(); private: template @@ -173,18 +165,18 @@ void tst_qdeclarativeimage::imageSource() QVERIFY(obj->asynchronous() == true); if (remote || async) - TRY_WAIT(obj->status() == QDeclarativeImage::Loading); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Loading); QCOMPARE(obj->source(), remote ? source : QUrl(source)); if (error.isEmpty()) { - TRY_WAIT(obj->status() == QDeclarativeImage::Ready); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready); QCOMPARE(obj->width(), qreal(width)); QCOMPARE(obj->height(), qreal(height)); QCOMPARE(obj->fillMode(), QDeclarativeImage::Stretch); QCOMPARE(obj->progress(), 1.0); } else { - TRY_WAIT(obj->status() == QDeclarativeImage::Error); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Error); } delete obj; @@ -355,6 +347,54 @@ void tst_qdeclarativeimage::tiling_QTBUG_6716() } } +void tst_qdeclarativeimage::noLoading() +{ + TestHTTPServer server(SERVER_PORT); + QVERIFY(server.isValid()); + server.serveDirectory(SRCDIR "/data"); + server.addRedirect("oldcolors.png", SERVER_ADDR "/colors.png"); + + QString componentStr = "import Qt 4.7\nImage { source: srcImage }"; + QDeclarativeContext *ctxt = engine.rootContext(); + ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/heart.png")); + QDeclarativeComponent component(&engine); + component.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QDeclarativeImage *obj = qobject_cast(component.create()); + QVERIFY(obj != 0); + QVERIFY(obj->status() == QDeclarativeImage::Ready); + + QSignalSpy sourceSpy(obj, SIGNAL(sourceChanged(const QUrl &))); + QSignalSpy progressSpy(obj, SIGNAL(progressChanged(qreal))); + QSignalSpy statusSpy(obj, SIGNAL(statusChanged(QDeclarativeImageBase::Status))); + + // Loading local file + ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png")); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready); + QTRY_VERIFY(obj->progress() == 1.0); + QTRY_COMPARE(sourceSpy.count(), 1); + QTRY_COMPARE(progressSpy.count(), 0); + QTRY_COMPARE(statusSpy.count(), 0); + + // Loading remote file + ctxt->setContextProperty("srcImage", QString(SERVER_ADDR) + "/oldcolors.png"); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Loading); + QTRY_VERIFY(obj->progress() == 0.0); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready); + QTRY_VERIFY(obj->progress() == 1.0); + QTRY_COMPARE(sourceSpy.count(), 2); + QTRY_COMPARE(progressSpy.count(), 2); + QTRY_COMPARE(statusSpy.count(), 2); + + // Loading remote file again - should not go through 'Loading' state. + ctxt->setContextProperty("srcImage", QUrl::fromLocalFile(SRCDIR "/data/colors.png")); + ctxt->setContextProperty("srcImage", QString(SERVER_ADDR) + "/oldcolors.png"); + QTRY_VERIFY(obj->status() == QDeclarativeImage::Ready); + QTRY_VERIFY(obj->progress() == 1.0); + QTRY_COMPARE(sourceSpy.count(), 4); + QTRY_COMPARE(progressSpy.count(), 2); + QTRY_COMPARE(statusSpy.count(), 2); +} + /* Find an item with the specified objectName. If index is supplied then the item must also evaluate the {index} expression equal to index -- cgit v0.12