diff options
author | Albert Astals Cid <albert.astals@canonical.com> | 2015-03-13 17:29:57 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-03-26 12:45:21 +0000 |
commit | f9c1b6e9c7ad3fbceef32590c5b7b6a9719fd453 (patch) | |
tree | 41cbacb3106d3481cd343e231063c7f77d67897b /tests/auto/quick/qquickimageprovider | |
parent | 6c66b0e91961d35a209c97b8424af746f6378077 (diff) |
Add QQuickAsyncImageProvider
It allows for providers to implement threading on their side
Change-Id: I34042b213ce7697a3e39470387357d733e15723c
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
Diffstat (limited to 'tests/auto/quick/qquickimageprovider')
-rw-r--r-- | tests/auto/quick/qquickimageprovider/tst_qquickimageprovider.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickimageprovider/tst_qquickimageprovider.cpp b/tests/auto/quick/qquickimageprovider/tst_qquickimageprovider.cpp index dae46b5c3d..80406be753 100644 --- a/tests/auto/quick/qquickimageprovider/tst_qquickimageprovider.cpp +++ b/tests/auto/quick/qquickimageprovider/tst_qquickimageprovider.cpp @@ -37,6 +37,7 @@ #include <private/qquickimage_p.h> #include <QImageReader> #include <QWaitCondition> +#include <QThreadPool> Q_DECLARE_METATYPE(QQuickImageProvider*); @@ -68,6 +69,8 @@ private slots: void threadTest(); + void asyncTextureTest(); + private: QString newImageFileName() const; void fillRequestTestsData(const QString &id); @@ -457,6 +460,101 @@ void tst_qquickimageprovider::threadTest() } } +class TestImageResponse : public QQuickImageResponse, public QRunnable +{ + public: + TestImageResponse(QMutex *lock, QWaitCondition *condition, bool *ok, const QString &id, const QSize &requestedSize) + : m_lock(lock), m_condition(condition), m_ok(ok), m_id(id), m_requestedSize(requestedSize), m_texture(0) + { + setAutoDelete(false); + } + + QQuickTextureFactory *textureFactory() const + { + return m_texture; + } + + void run() + { + m_lock->lock(); + if (!(*m_ok)) { + m_condition->wait(m_lock); + } + m_lock->unlock(); + QImage image(50, 50, QImage::Format_RGB32); + image.fill(QColor(m_id).rgb()); + if (m_requestedSize.isValid()) + image = image.scaled(m_requestedSize); + m_texture = QQuickTextureFactory::textureFactoryForImage(image); + emit finished(); + } + + QMutex *m_lock; + QWaitCondition *m_condition; + bool *m_ok; + QString m_id; + QSize m_requestedSize; + QQuickTextureFactory *m_texture; +}; + +class TestAsyncProvider : public QQuickAsyncImageProvider +{ + public: + TestAsyncProvider() : ok(false) + { + pool.setMaxThreadCount(4); + } + + ~TestAsyncProvider() {} + + QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) + { + TestImageResponse *response = new TestImageResponse(&lock, &condition, &ok, id, requestedSize); + pool.start(response); + return response; + } + + QThreadPool pool; + QMutex lock; + QWaitCondition condition; + bool ok; +}; + + +void tst_qquickimageprovider::asyncTextureTest() +{ + QQmlEngine engine; + + TestAsyncProvider *provider = new TestAsyncProvider; + + engine.addImageProvider("test_async", provider); + QVERIFY(engine.imageProvider("test_async") != 0); + + QString componentStr = "import QtQuick 2.0\nItem { \n" + "Image { source: \"image://test_async/blue\"; }\n" + "Image { source: \"image://test_async/red\"; }\n" + "Image { source: \"image://test_async/green\"; }\n" + "Image { source: \"image://test_async/yellow\"; }\n" + " }"; + QQmlComponent component(&engine); + component.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QObject *obj = component.create(); + //MUST not deadlock + QVERIFY(obj != 0); + QList<QQuickImage *> images = obj->findChildren<QQuickImage *>(); + QCOMPARE(images.count(), 4); + + QTRY_VERIFY(provider->pool.activeThreadCount() == 4); + foreach (QQuickImage *img, images) { + QTRY_VERIFY(img->status() == QQuickImage::Loading); + } + provider->ok = true; + provider->condition.wakeAll(); + foreach (QQuickImage *img, images) { + QTRY_VERIFY(img->status() == QQuickImage::Ready); + } +} + QTEST_MAIN(tst_qquickimageprovider) |