diff options
author | kh1 <karsten.heimrich@digia.com> | 2014-01-16 12:55:00 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@digia.com> | 2014-01-22 17:19:53 +0100 |
commit | dab03d22574735c50c8429f1797b3f0a7bd599b0 (patch) | |
tree | ca53f81eef5a0129c00a254cef812fc08da8b782 /tests | |
parent | fe92a09482c5abf7f2c56901f2d60e287282f939 (diff) |
QFuture based asynchronous Task implementation.
Change-Id: I538a2fc40b67d6d27f120afe3705065ab98f8f99
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/installer/installer.pro | 3 | ||||
-rw-r--r-- | tests/auto/installer/task/task.pro | 7 | ||||
-rw-r--r-- | tests/auto/installer/task/tst_task.cpp | 143 |
3 files changed, 152 insertions, 1 deletions
diff --git a/tests/auto/installer/installer.pro b/tests/auto/installer/installer.pro index f4c4bbaf4..c565561b4 100644 --- a/tests/auto/installer/installer.pro +++ b/tests/auto/installer/installer.pro @@ -15,4 +15,5 @@ SUBDIRS += \ solver \ binaryformat \ packagemanagercore \ - settingsoperation + settingsoperation \ + task diff --git a/tests/auto/installer/task/task.pro b/tests/auto/installer/task/task.pro new file mode 100644 index 000000000..52d12db46 --- /dev/null +++ b/tests/auto/installer/task/task.pro @@ -0,0 +1,7 @@ +include(../../qttest.pri) + +QT -= gui +isEqual(QT_MAJOR_VERSION, 5) { + QT += concurrent network +} +SOURCES += tst_task.cpp diff --git a/tests/auto/installer/task/tst_task.cpp b/tests/auto/installer/task/tst_task.cpp new file mode 100644 index 000000000..945c2a5f6 --- /dev/null +++ b/tests/auto/installer/task/tst_task.cpp @@ -0,0 +1,143 @@ +/************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Installer Framework. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +**************************************************************************/ + +#include <copyfiletask.h> +#include <downloadfiletask.h> +#include <fileutils.h> + +#include <QFutureWatcher> +#include <QSignalSpy> +#include <QTest> +#include <QTemporaryFile> + +using namespace QInstaller; + +static const qint64 scLargeSize = 4194304LL; + +class tst_Task : public QObject +{ + Q_OBJECT + +private slots: + void copyFile() + { + QTemporaryFile file; + file.setAutoRemove(false); + + if (file.open()) { + const QString filename = file.fileName(); + QInstaller::blockingWrite(&file, QByteArray(scLargeSize, '1')); + file.close(); + + CopyFileTask fileTask(filename); + QFutureWatcher<FileTaskResult> watcher; + + QSignalSpy started(&watcher, SIGNAL(started())); + QSignalSpy finished(&watcher, SIGNAL(finished())); + QSignalSpy progress(&watcher, SIGNAL(progressValueChanged(int))); + + watcher.setFuture(QtConcurrent::run(&CopyFileTask::doTask, &fileTask)); + QTest::qWait(10); // Spin the event loop to deliver queued signals. + + QCOMPARE(started.count(), 1); + QCOMPARE(finished.count(), 0); + QCOMPARE(progress.count(), 1); + + watcher.waitForFinished(); + QTest::qWait(10); // Spin the event loop to deliver queued signals. + + QCOMPARE(started.count(), 1); + QCOMPARE(finished.count(), 1); + QVERIFY(progress.count() > 2); + + FileTaskResult result = watcher.result(); + QCOMPARE(watcher.future().resultCount(), 1); + + QVERIFY(QFile(result.target()).exists()); + QCOMPARE(file.size(), QFile(result.target()).size()); + QCOMPARE(result.checkSum().toHex(), QByteArray("85304f87b8d90554a63c6f6d1e9cc974fbef8d32")); + } + } + + void downloadFile() + { + QTemporaryFile file; + file.setAutoRemove(false); + + if (file.open()) { + const QString filename = file.fileName(); + QInstaller::blockingWrite(&file, QByteArray(scLargeSize, '1')); + file.close(); + + DownloadFileTask fileTask(QLatin1String("file:///") + filename); + QFutureWatcher<FileTaskResult> watcher; + + QSignalSpy started(&watcher, SIGNAL(started())); + QSignalSpy finished(&watcher, SIGNAL(finished())); + QSignalSpy progress(&watcher, SIGNAL(progressValueChanged(int))); + + watcher.setFuture(QtConcurrent::run(&DownloadFileTask::doTask, &fileTask)); + QTest::qWait(10); // Spin the event loop to deliver queued signals. + + QCOMPARE(started.count(), 1); + QCOMPARE(finished.count(), 0); + QCOMPARE(progress.count(), 1); + + watcher.waitForFinished(); + QTest::qWait(10); // Spin the event loop to deliver queued signals. + + QCOMPARE(started.count(), 1); + QCOMPARE(finished.count(), 1); + QVERIFY(progress.count() > 2); + + FileTaskResult result = watcher.result(); + QCOMPARE(watcher.future().resultCount(), 1); + + QVERIFY(QFile(result.target()).exists()); + QCOMPARE(file.size(), QFile(result.target()).size()); + QCOMPARE(result.checkSum().toHex(), QByteArray("85304f87b8d90554a63c6f6d1e9cc974fbef8d32")); + } + } +}; + +QTEST_MAIN(tst_Task) + +#include "tst_task.moc" |