Qt: Add test for progress signal of encryptjob
authorAndre Heinecke <aheinecke@intevation.de>
Fri, 12 Aug 2016 14:55:51 +0000 (16:55 +0200)
committerAndre Heinecke <aheinecke@intevation.de>
Fri, 12 Aug 2016 14:55:51 +0000 (16:55 +0200)
* lang/qt/tests/t-encrypt.cpp (testProgress): New.

--
This tests that a ByteArray IODevice now gives proper progress
signals.

lang/qt/tests/t-encrypt.cpp

index c6fcaa2..708c205 100644 (file)
@@ -31,6 +31,8 @@
 #include <QDebug>
 #include <QTest>
 #include <QTemporaryDir>
+#include <QSignalSpy>
+#include <QBuffer>
 #include "keylistjob.h"
 #include "encryptjob.h"
 #include "qgpgmeencryptjob.h"
 #include "qgpgmedecryptjob.h"
 #include "qgpgmebackend.h"
 #include "keylistresult.h"
+#include "engineinfo.h"
 #include "t-support.h"
 
+#define PROGRESS_TEST_SIZE 1 * 1024 * 1024
+
 using namespace QGpgME;
 using namespace GpgME;
 
@@ -48,6 +53,9 @@ class EncryptionTest : public QGpgMETest
 {
     Q_OBJECT
 
+Q_SIGNALS:
+    void asyncDone();
+
 private Q_SLOTS:
 
     void testSimpleEncryptDecrypt()
@@ -82,6 +90,60 @@ private Q_SLOTS:
         delete decJob;
     }
 
+    void testProgress()
+    {
+        if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.15") {
+            // We can only test the progress with 2.1.15 as this started to
+            // have total progress for memory callbacks
+            return;
+        }
+        auto listjob = openpgp()->keyListJob(false, false, false);
+        std::vector<Key> keys;
+        auto keylistresult = listjob->exec(QStringList() << QStringLiteral("alfa@example.net"),
+                                          false, keys);
+        Q_ASSERT(!keylistresult.error());
+        Q_ASSERT(keys.size() == 1);
+        delete listjob;
+
+        auto job = openpgp()->encryptJob(/*ASCII Armor */false, /* Textmode */ false);
+        Q_ASSERT(job);
+        QByteArray plainBa;
+        plainBa.fill('X', PROGRESS_TEST_SIZE);
+        QByteArray cipherText;
+
+        bool initSeen = false;
+        bool finishSeen = false;
+        connect(job, &Job::progress, this, [this, &initSeen, &finishSeen] (const QString& what, int current, int total) {
+                // We only check for progress 0 and max progress as the other progress
+                // lines depend on the system speed and are as such unreliable to test.
+                Q_ASSERT(total == PROGRESS_TEST_SIZE);
+                if (current == 0) {
+                    initSeen = true;
+                }
+                if (current == total) {
+                    finishSeen = true;
+                }
+                Q_ASSERT(current >= 0 && current <= total);
+            });
+        connect(job, &EncryptJob::result, this, [this, &initSeen, &finishSeen] (const GpgME::EncryptionResult &result,
+                                                                                const QByteArray &cipherText,
+                                                                                const QString,
+                                                                                const GpgME::Error) {
+                Q_ASSERT(initSeen);
+                Q_ASSERT(finishSeen);
+                Q_EMIT asyncDone();
+            });
+
+        auto inptr  = std::shared_ptr<QIODevice>(new QBuffer(&plainBa));
+        inptr->open(QIODevice::ReadOnly);
+        auto outptr = std::shared_ptr<QIODevice>(new QBuffer(&cipherText));
+        outptr->open(QIODevice::WriteOnly);
+
+        job->start(keys, inptr, outptr, Context::AlwaysTrust);
+        QSignalSpy spy (this, SIGNAL(asyncDone()));
+        Q_ASSERT(spy.wait());
+    }
+
     void testSymmetricEncryptDecrypt()
     {
         auto ctx = Context::createForProtocol(OpenPGP);