Disconnect from previous loading image when loading a new image.
authorMichael Brasser <michael.brasser@live.com>
Thu, 24 Jan 2013 04:02:53 +0000 (22:02 -0600)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 24 Jan 2013 11:32:41 +0000 (12:32 +0100)
Change-Id: If2fa95d9715a55d3f07ecf5f232e4f4b9a44a6fb
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>

src/quick/items/qquickimagebase.cpp
tests/auto/quick/qquickimage/data/correctStatus.qml [new file with mode: 0644]
tests/auto/quick/qquickimage/tst_qquickimage.cpp

index 2a9edb3..c07f706 100644 (file)
@@ -207,6 +207,7 @@ void QQuickImageBase::load()
             options |= QQuickPixmap::Asynchronous;
         if (d->cache)
             options |= QQuickPixmap::Cache;
+        d->pix.clear(this);
         d->pix.load(qmlEngine(this), d->url, d->sourcesize, options);
 
         if (d->pix.isLoading()) {
diff --git a/tests/auto/quick/qquickimage/data/correctStatus.qml b/tests/auto/quick/qquickimage/data/correctStatus.qml
new file mode 100644 (file)
index 0000000..2326078
--- /dev/null
@@ -0,0 +1,26 @@
+import QtQuick 2.0
+
+Item {
+    property alias status: image1.status
+
+    Image {
+        id: image1
+        asynchronous: true
+        source: "image://test/first-image.png"
+    }
+
+    Image {
+        id: image2
+        asynchronous: true
+        source: "image://test/first-image.png"
+    }
+
+    Timer {
+        interval: 50
+        running: true
+        repeat: false
+        onTriggered: {
+            image1.source = "image://test/second-image.png"
+        }
+    }
+}
index 7f3f0d5..51ac5c6 100644 (file)
@@ -56,6 +56,7 @@
 #include <QtGui/QPainter>
 #include <QtGui/QImageReader>
 #include <QQuickWindow>
+#include <QQuickImageProvider>
 
 #include "../../shared/util.h"
 #include "../../shared/testhttpserver.h"
@@ -102,6 +103,7 @@ private slots:
     void sourceSize();
     void progressAndStatusChanges();
     void sourceSizeChanges();
+    void correctStatus();
 
 private:
     QQmlEngine engine;
@@ -868,6 +870,55 @@ void tst_qquickimage::progressAndStatusChanges()
     delete obj;
 }
 
+class TestQImageProvider : public QQuickImageProvider
+{
+public:
+    TestQImageProvider() : QQuickImageProvider(Image) {}
+
+    QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize)
+    {
+        if (id == QLatin1String("first-image.png")) {
+            QTest::qWait(50);
+            int width = 100;
+            int height = 100;
+            QImage image(width, height, QImage::Format_RGB32);
+            image.fill(QColor("yellow").rgb());
+            if (size)
+                *size = QSize(width, height);
+            return image;
+        }
+
+        QTest::qWait(400);
+        int width = 100;
+        int height = 100;
+        QImage image(width, height, QImage::Format_RGB32);
+        image.fill(QColor("green").rgb());
+        if (size)
+            *size = QSize(width, height);
+        return image;
+    }
+};
+
+void tst_qquickimage::correctStatus()
+{
+    QQmlEngine engine;
+    engine.addImageProvider(QLatin1String("test"), new TestQImageProvider());
+
+    QQmlComponent component(&engine, testFileUrl("correctStatus.qml"));
+    QObject *obj = component.create();
+    QVERIFY(obj);
+
+    QTest::qWait(200);
+
+    // at this point image1 should be attempting to load second-image.png,
+    // and should be in the loading state. Without a clear prior to that load,
+    // the status can mistakenly be in the ready state.
+    QCOMPARE(obj->property("status").toInt(), int(QQuickImage::Loading));
+
+    QTest::qWait(400);
+    delete obj;
+}
+
 QTEST_MAIN(tst_qquickimage)
 
 #include "tst_qquickimage.moc"