[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [KSecretService] f2c482a: Add a user interface manager without user
From:       Michael Leupold <lemma () confuego ! org>
Date:       2010-11-09 19:14:28
Message-ID: 20101109191428.5C3D6A60E4 () git ! kde ! org
[Download RAW message or body]


	A	 ui/tests/nouimanagertest.h	 [License: GPL(v2)]


	A	 ui/tests/nouimanagertest.cpp	 [License: GPL(v2)]


	A	 ui/nouimanager.h	 [License: GPL(v2)]


	A	 ui/nouimanager.cpp	 [License: GPL(v2)]

commit f2c482aef48830352cc995c89caa4fd32f331fef
Author: Michael Leupold <lemma@confuego.org>
Date:   Sun Aug 29 16:42:04 2010 +0000

    Add a user interface manager without user interface for unit-testing various \
backend/daemon functionality. Also add a unit-test for the user interface manager \
itself which can be used to unit-test the underlying classes as well.  
    svn path=/trunk/playground/base/ksecretservice/; revision=1169545

diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt
index 6916cfa..23fb0ba 100644
--- a/ui/CMakeLists.txt
+++ b/ui/CMakeLists.txt
@@ -4,6 +4,7 @@ ADD_SUBDIRECTORY (tests)
 SET (ksecretservice_ui_SRCS
    abstractuijobs.cpp
    dialoguimanager.cpp
+   nouimanager.cpp
 )
 
 KDE4_ADD_LIBRARY (ksecretserviceui STATIC ${ksecretservice_ui_SRCS})
diff --git a/ui/dialoguimanager.h b/ui/dialoguimanager.h
index 88ec158..4902787 100644
--- a/ui/dialoguimanager.h
+++ b/ui/dialoguimanager.h
@@ -38,7 +38,7 @@ public:
    DialogAskPasswordJob(AbstractUiManager *manager, const QString &collection, bool \
secondTry);  
    /**
-    * Destructor.Inheritance graph
+    * Destructor.
     */
    ~DialogAskPasswordJob();
 
@@ -101,7 +101,7 @@ public:
    /**
     * Create a job to ask for a user's password to unlock a collection.
     */
-   virtual AbstractAskPasswordJob* createAskPasswordJob(const QString &collection,
+   virtual AbstractAskPasswordJob *createAskPasswordJob(const QString &collection,
                                                         bool secondTry);
                                                         
    /**
diff --git a/ui/nouimanager.cpp b/ui/nouimanager.cpp
new file mode 100644
index 0000000..afaad2f
--- /dev/null
+++ b/ui/nouimanager.cpp
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License or (at your option) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "nouimanager.h"
+
+#include <QtCore/QTimer>
+
+NoUiAskPasswordJob::NoUiAskPasswordJob(AbstractUiManager *manager,
+                                       bool cancel)
+ : AbstractAskPasswordJob(manager, "", false), m_cancel(cancel)
+{
+}
+
+NoUiAskPasswordJob::~NoUiAskPasswordJob()
+{
+}
+
+void NoUiAskPasswordJob::start()
+{
+   QTimer::singleShot(0, this, SLOT(finish()));
+}
+
+void NoUiAskPasswordJob::finish()
+{
+   if (m_cancel) {
+      setCancelled(true);
+   } else {
+      setPassword("default");
+   }
+   emitResult();
+}
+
+NoUiNewPasswordJob::NoUiNewPasswordJob(AbstractUiManager *manager,
+                                       bool cancel)
+ : AbstractNewPasswordJob(manager, ""), m_cancel(cancel)
+{
+}
+
+NoUiNewPasswordJob::~NoUiNewPasswordJob()
+{
+}
+
+void NoUiNewPasswordJob::start()
+{
+   QTimer::singleShot(0, this, SLOT(finish()));
+}
+
+void NoUiNewPasswordJob::finish()
+{
+   if (m_cancel) {
+      setCancelled(true);
+   } else {
+      setPassword("default");
+   }
+   emitResult();
+}
+
+NoUiManager::NoUiManager() : m_cancelAll(false)
+{
+}
+
+NoUiManager::~NoUiManager()
+{
+}
+
+AbstractAskPasswordJob *NoUiManager::createAskPasswordJob(const QString &collection,
+                                                          bool secondTry)
+{
+   Q_UNUSED(collection);
+   Q_UNUSED(secondTry);
+   return new NoUiAskPasswordJob(this, m_cancelAll);
+}
+
+AbstractNewPasswordJob *NoUiManager::createNewPasswordJob(const QString &collection)
+{
+   Q_UNUSED(collection);
+   return new NoUiNewPasswordJob(this, m_cancelAll);
+}
+
+void NoUiManager::setCancelAll(bool cancelAll)
+{
+   m_cancelAll = cancelAll;
+}
+
+bool NoUiManager::cancelAll() const
+{
+   return m_cancelAll;
+}
+
+#include "nouimanager.moc"
diff --git a/ui/nouimanager.h b/ui/nouimanager.h
new file mode 100644
index 0000000..ece89ac
--- /dev/null
+++ b/ui/nouimanager.h
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License or (at your option) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NOUIMANAGER_H
+#define NOUIMANAGER_H
+
+#include "abstractuijobs.h"
+#include "abstractuimanager.h"
+
+class NoUiAskPasswordJob : public AbstractAskPasswordJob
+{
+   Q_OBJECT
+   
+public:
+   /**
+    * Constructor.
+    *
+    * @param manager the manager creating this job
+    * @param cancel if true, this job will be cancelled
+    */
+   NoUiAskPasswordJob(AbstractUiManager *manager, bool cancel);
+   
+   /**
+    * Destructor.
+    */
+   virtual ~NoUiAskPasswordJob();
+   
+   /**
+    * Does basically nothing - no UI, no password to enter.
+    */
+   virtual void start();
+   
+private Q_SLOTS:
+   /**
+    * Called from start to emit the result signal.
+    */
+   void finish();
+   
+private:
+   bool m_cancel;
+};
+
+class NoUiNewPasswordJob : public AbstractNewPasswordJob
+{
+   Q_OBJECT
+   
+public:
+   /**
+    * Constructor.
+    *
+    * @param manager the manager creating this job
+    * @param cancel if true, this job will be cancelled
+    */
+   NoUiNewPasswordJob(AbstractUiManager *manager, bool cancel);
+   
+   /**
+    * Destructor.
+    */
+   virtual ~NoUiNewPasswordJob();
+   
+   /**
+    * Does basically nothing - no UI, no new password to enter.
+    */
+   virtual void start();
+   
+private Q_SLOTS:
+   /**
+    * Called from start to emit the result signal.
+    */
+   void finish();
+   
+private:
+   bool m_cancel;
+};
+
+/**
+ * Implement AbstractUiManager to provide ui jobs which don't show a
+ * user interface. While this might seem paradox at first look, it's highly
+ * useful when implementing automatic unit-tests.
+ *
+ * @remarks a password reported by this ui manager's jobs will always have
+ *          the value "default".
+ */
+class NoUiManager : public AbstractUiManager
+{
+public:
+   /**
+    * Constructor.
+    */
+   NoUiManager();
+   
+   /**
+    * Destructor.
+    */
+   virtual ~NoUiManager();
+   
+   /**
+    * Create a job to ask for a user's password to unlock a collection.
+    */
+   virtual AbstractAskPasswordJob *createAskPasswordJob(const QString &collection,
+                                                        bool secondTry);
+                                                        
+   /**
+    * Create a job to as a user for a new password for a collection.
+    */
+   virtual AbstractNewPasswordJob *createNewPasswordJob(const QString &collection);
+   
+   /**
+    * Set to true to cancel all created jobs.
+    */
+   void setCancelAll(bool cancelAll);
+   
+   /**
+    * True if all created jobs are automatically cancelled, false else.
+    */
+   bool cancelAll() const;
+   
+private:
+   bool m_cancelAll;
+};
+
+#endif
diff --git a/ui/tests/CMakeLists.txt b/ui/tests/CMakeLists.txt
index b23a86f..f988e6e 100644
--- a/ui/tests/CMakeLists.txt
+++ b/ui/tests/CMakeLists.txt
@@ -8,3 +8,12 @@ TARGET_LINK_LIBRARIES (dialoguimanager_test
    ${QT_QTTEST_LIBRARIES}
    ${KDE4_KDEUI_LIBS}
 )
+
+KDE4_ADD_EXECUTABLE (nouimanager_test nouimanagertest.cpp)
+TARGET_LINK_LIBRARIES (nouimanager_test
+   ksecretservicelib
+   ksecretserviceui
+   ${QT_QTTEST_LIBRARIES}
+)
+
+ADD_TEST (NoUiManagerTest nouimanager_test)
diff --git a/ui/tests/nouimanagertest.cpp b/ui/tests/nouimanagertest.cpp
new file mode 100644
index 0000000..09e4836
--- /dev/null
+++ b/ui/tests/nouimanagertest.cpp
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License or (at your option) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "nouimanagertest.h"
+
+#include <QtCrypto/QtCrypto>
+#include <QtTest/QTestEventLoop>
+#include <qtest_kde.h>
+
+#include "../nouimanager.h"
+
+void NoUiManagerTest::initTestCase()
+{
+   QCA::init();
+}
+
+void NoUiManagerTest::testAskPassword()
+{
+   NoUiManager manager;
+   
+   // create a job and start it
+   AbstractAskPasswordJob *job = manager.createAskPasswordJob("TESTCOLLECTION", \
false); +   QVERIFY(!job->isImmediate());
+   
+   QTestEventLoop loop;
+   connect(job, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   job->enqueue();
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+   
+   // verify job result
+   QVERIFY(job->isFinished());
+   QVERIFY(!job->cancelled());
+   QCOMPARE(job->password().toByteArray(), QByteArray("default"));
+   
+   // verify job deletion
+   connect(job, SIGNAL(destroyed(QObject*)), &loop, SLOT(exitLoop()));
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+}
+
+void NoUiManagerTest::testNewPassword()
+{
+   NoUiManager manager;
+   
+   // create a job and start it
+   AbstractNewPasswordJob *job = manager.createNewPasswordJob("TESTCOLLECTION");
+   QVERIFY(!job->isImmediate());
+   
+   QTestEventLoop loop;
+   connect(job, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   job->enqueue();
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+   
+   // verify job result
+   QVERIFY(job->isFinished());
+   QVERIFY(!job->cancelled());
+   QCOMPARE(job->password().toByteArray(), QByteArray("default"));
+   
+   // verify job deletion
+   connect(job, SIGNAL(destroyed(QObject*)), &loop, SLOT(exitLoop()));
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+}
+
+void NoUiManagerTest::testJobOrder()
+{
+   NoUiManager manager;
+   
+   // create 3 jobs
+   AbstractNewPasswordJob *job1 = manager.createNewPasswordJob("1");
+   AbstractNewPasswordJob *job2 = manager.createNewPasswordJob("2");
+   AbstractNewPasswordJob *job3 = manager.createNewPasswordJob("3");
+
+   // enqueue the job so they should end up in order 1 -> 2 -> 3
+   job2->enqueue();
+   job3->enqueue();
+   job1->enqueue(true);
+   
+   QTestEventLoop loop;
+   connect(job1, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   connect(job2, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   connect(job3, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   
+   // check if jobs are executed in the right order
+   // NOTE: once a job is done it will be deleted, so successive tests
+   //       for isFinished() might fail on them.
+   loop.enterLoop(5);
+   QVERIFY(job1->isFinished() && !job2->isFinished() && !job3->isFinished());
+   
+   loop.enterLoop(5);
+   QVERIFY(job2->isFinished() && !job3->isFinished());
+
+   loop.enterLoop(5);
+   QVERIFY(job3->isFinished());
+}
+
+void NoUiManagerTest::testAskPasswordCancelled()
+{
+   NoUiManager manager;
+   manager.setCancelAll(true);
+   
+   // create a job and start it
+   AbstractAskPasswordJob *job = manager.createAskPasswordJob("TESTCOLLECTION", \
false); +   QVERIFY(!job->isImmediate());
+   
+   QTestEventLoop loop;
+   connect(job, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   job->enqueue();
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+   
+   // verify job result
+   QVERIFY(job->isFinished());
+   QVERIFY(job->cancelled());
+   QCOMPARE(job->password().toByteArray(), QByteArray(""));
+   
+   // verify job deletion
+   connect(job, SIGNAL(destroyed(QObject*)), &loop, SLOT(exitLoop()));
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+}
+
+void NoUiManagerTest::testNewPasswordCancelled()
+{
+   NoUiManager manager;
+   manager.setCancelAll(true);
+
+   // create a job and start it
+   AbstractNewPasswordJob *job = manager.createNewPasswordJob("TESTCOLLECTION");
+   QVERIFY(!job->isImmediate());
+   
+   QTestEventLoop loop;
+   connect(job, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   job->enqueue();
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+   
+   // verify job result
+   QVERIFY(job->isFinished());
+   QVERIFY(job->cancelled());
+   QCOMPARE(job->password().toByteArray(), QByteArray(""));
+   
+   // verify job deletion
+   connect(job, SIGNAL(destroyed(QObject*)), &loop, SLOT(exitLoop()));
+   loop.enterLoop(5);
+   QVERIFY(!loop.timeout());
+}
+
+void NoUiManagerTest::testJobOrderCancelled()
+{
+   NoUiManager manager;
+   manager.setCancelAll(true);
+   
+   // create 3 jobs
+   AbstractNewPasswordJob *job1 = manager.createNewPasswordJob("1");
+   AbstractNewPasswordJob *job2 = manager.createNewPasswordJob("2");
+   AbstractNewPasswordJob *job3 = manager.createNewPasswordJob("3");
+
+   // enqueue the job so they should end up in order 1 -> 2 -> 3
+   job2->enqueue();
+   job3->enqueue();
+   job1->enqueue(true);
+   
+   QTestEventLoop loop;
+   connect(job1, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   connect(job2, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   connect(job3, SIGNAL(result(QueuedJob*)), &loop, SLOT(exitLoop()));
+   
+   // check if jobs are executed in the right order
+   // NOTE: once a job is done it will be deleted, so successive tests
+   //       for isFinished() might fail on them.
+   loop.enterLoop(5);
+   QVERIFY(job1->isFinished() && !job2->isFinished() && !job3->isFinished());
+   
+   loop.enterLoop(5);
+   QVERIFY(job2->isFinished() && !job3->isFinished());
+
+   loop.enterLoop(5);
+   QVERIFY(job3->isFinished());
+}
+
+void NoUiManagerTest::cleanupTestCase()
+{
+}
+
+QTEST_KDEMAIN(NoUiManagerTest, GUI)
+#include "nouimanagertest.moc"
diff --git a/ui/tests/nouimanagertest.h b/ui/tests/nouimanagertest.h
new file mode 100644
index 0000000..e1e1b45
--- /dev/null
+++ b/ui/tests/nouimanagertest.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License or (at your option) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NOUIMANAGERTEST_H
+#define NOUIMANAGERTEST_H
+
+#include <QtTest>
+
+class NoUiManagerTest: public QObject
+{
+   Q_OBJECT
+   
+private Q_SLOTS:
+   void initTestCase();
+   
+   void testAskPassword();
+   void testNewPassword();
+   void testJobOrder();
+   
+   void testAskPasswordCancelled();
+   void testNewPasswordCancelled();
+   void testJobOrderCancelled();
+   
+   void cleanupTestCase();
+};
+
+#endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic