From 193ef339d88c6d6beb45a02488dd1bb54b716452 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 18 Oct 2017 16:45:21 -0400
Subject: [PATCH] tests: add tests for newcallmodel
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Change-Id: Idf704fca78065ba0db7824e55df66642fdc1d4c3
Reviewed-by: Nicolas Jäger <nicolas.jager@savoirfairelinux.com>
---
 CMakeLists.txt                         |  2 +
 test/mocks/callmanager_mock.h          | 21 +++---
 test/mocks/configurationmanager_mock.h |  5 +-
 test/newcallmodeltester.cpp            | 93 ++++++++++++++++++++++++++
 test/newcallmodeltester.h              | 77 +++++++++++++++++++++
 5 files changed, 188 insertions(+), 10 deletions(-)
 create mode 100644 test/newcallmodeltester.cpp
 create mode 100644 test/newcallmodeltester.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c2306e38..df48e295 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -881,6 +881,8 @@ IF((${ENABLE_TEST} MATCHES true))
       test/conversationmodeltester.cpp
       test/contactmodeltester.h
       test/contactmodeltester.cpp
+      test/newcallmodeltester.h
+      test/newcallmodeltester.cpp
       ${libringclient_LIB_SRCS}
       ${libringclient_PRIVATE_HDRS}
     )
diff --git a/test/mocks/callmanager_mock.h b/test/mocks/callmanager_mock.h
index ef562db1..522860fb 100644
--- a/test/mocks/callmanager_mock.h
+++ b/test/mocks/callmanager_mock.h
@@ -54,8 +54,8 @@ public:
 public Q_SLOTS: // METHODS
     bool accept(const QString &callID)
     {
-        Q_UNUSED(callID)
-        return false;
+        emit callStateChanged(callID, "CURRENT", -1);
+        return true;
     }
 
     bool addMainParticipant(const QString &confID)
@@ -143,8 +143,8 @@ public Q_SLOTS: // METHODS
 
     bool hangUp(const QString &callID)
     {
-        Q_UNUSED(callID)
-        return false;
+        emit callStateChanged(callID, "OVER", -1);
+        return true;
     }
 
     bool hangUpConference(const QString &confID)
@@ -155,8 +155,8 @@ public Q_SLOTS: // METHODS
 
     bool hold(const QString &callID)
     {
-        Q_UNUSED(callID)
-        return false;
+        emit callStateChanged(callID, "HOLD", -1);
+        return true;
     }
 
     bool holdConference(const QString &confID)
@@ -245,8 +245,8 @@ public Q_SLOTS: // METHODS
 
     bool unhold(const QString &callID)
     {
-        Q_UNUSED(callID)
-        return false;
+        emit callStateChanged(callID, "CURRENT", -1);
+        return true;
     }
 
     bool unholdConference(const QString &confID)
@@ -272,6 +272,11 @@ public Q_SLOTS: // METHODS
     {
     }
 
+    void emitIncomingCall(const QString &accountID, const QString &callID, const QString &from)
+    {
+        emit incomingCall(accountID, callID, from);
+    }
+
 Q_SIGNALS: // SIGNALS
     void callStateChanged(const QString &callID, const QString &state, int code);
     void transferFailed();
diff --git a/test/mocks/configurationmanager_mock.h b/test/mocks/configurationmanager_mock.h
index 24209dd4..dfed8e01 100644
--- a/test/mocks/configurationmanager_mock.h
+++ b/test/mocks/configurationmanager_mock.h
@@ -142,8 +142,9 @@ public Q_SLOTS: // METHODS
     QStringList getAccountList()
     {
         auto accountList = QStringList();
-        accountList << QString("ring0");
-        accountList << QString("ring1");
+        accountList << QString("ring0"); // Used in conversationmodeltester
+        accountList << QString("ring1"); // Used in contactmodeltester
+        accountList << QString("ring2"); // Used in newcallmodeltester
         accountList << QString("sip0");
         accountList << QString("sip1");
         return accountList;
diff --git a/test/newcallmodeltester.cpp b/test/newcallmodeltester.cpp
new file mode 100644
index 00000000..13cb7ed8
--- /dev/null
+++ b/test/newcallmodeltester.cpp
@@ -0,0 +1,93 @@
+/*
+ *  Copyright (C) 2017 Savoir-faire Linux Inc.
+ *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
+ *
+ *  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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ */
+#include "newcallmodeltester.h"
+
+// Qt
+#include <QString>
+#include "utils/waitforsignalhelper.h"
+
+// Lrc
+#include <api/newaccountmodel.h>
+#include <api/newcallmodel.h>
+#include <api/conversationmodel.h>
+#include <api/call.h>
+#include <dbus/callmanager.h>
+
+namespace ring
+{
+namespace test
+{
+
+CPPUNIT_TEST_SUITE_REGISTRATION(NewCallModelTester);
+
+NewCallModelTester::NewCallModelTester()
+: lrc_(new lrc::api::Lrc())
+, accInfo_(lrc_->getAccountModel().getAccountInfo("ring2"))
+{
+
+}
+
+void
+NewCallModelTester::setUp()
+{
+
+}
+
+void
+NewCallModelTester::testCreateAndGetCall()
+{
+    auto callId = accInfo_.callModel->createCall("ring:contact0");
+    CPPUNIT_ASSERT(!callId.empty());
+    CPPUNIT_ASSERT(accInfo_.callModel->hasCall(callId));
+    auto& call = accInfo_.callModel->getCallFromURI("ring:contact0");
+    auto& callFromId = accInfo_.callModel->getCall(call.id);
+    CPPUNIT_ASSERT_EQUAL(callFromId.peer, std::string("ring:contact0"));
+}
+
+void
+NewCallModelTester::testAcceptHoldUnholdHangupCall()
+{
+    std::string callId = "ring:contact1";
+    CallManager::instance().emitIncomingCall("ring2", callId.c_str(), "ring:contact1");
+    WaitForSignalHelper(*accInfo_.callModel,
+        SIGNAL(newIncomingCall(const std::string& callId, const std::string& fromId))).wait(1000);
+    CPPUNIT_ASSERT(accInfo_.callModel->hasCall(callId));
+    accInfo_.callModel->accept(callId);
+    auto& call = accInfo_.callModel->getCallFromURI("ring:contact1");
+    CPPUNIT_ASSERT_EQUAL((int)call.status, (int)lrc::api::call::Status::IN_PROGRESS);
+    accInfo_.callModel->togglePause(callId);
+    auto& callHold = accInfo_.callModel->getCallFromURI("ring:contact1");
+    CPPUNIT_ASSERT_EQUAL((int)callHold.status, (int)lrc::api::call::Status::PAUSED);
+    accInfo_.callModel->togglePause(callId);
+    auto& callUnhold = accInfo_.callModel->getCallFromURI("ring:contact1");
+    CPPUNIT_ASSERT_EQUAL((int)callUnhold.status, (int)lrc::api::call::Status::IN_PROGRESS);
+    accInfo_.callModel->hangUp(callId);
+    auto& callOver = accInfo_.callModel->getCallFromURI("ring:contact1");
+    CPPUNIT_ASSERT_EQUAL((int)callOver.status, (int)lrc::api::call::Status::ENDED);
+}
+
+
+void
+NewCallModelTester::tearDown()
+{
+
+}
+
+} // namespace test
+} // namespace ring
diff --git a/test/newcallmodeltester.h b/test/newcallmodeltester.h
new file mode 100644
index 00000000..eeff50c2
--- /dev/null
+++ b/test/newcallmodeltester.h
@@ -0,0 +1,77 @@
+/*
+ *  Copyright (C) 2017 Savoir-faire Linux Inc.
+ *
+ *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
+ *
+ *  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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ */
+
+#pragma once
+
+// cppunit
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+// std
+#include <memory>
+
+// Qt
+#include <QObject>
+
+// lrc
+#include "api/lrc.h"
+#include "api/account.h"
+
+namespace ring
+{
+namespace test
+{
+
+class NewCallModelTester :  public CppUnit::TestFixture {
+    CPPUNIT_TEST_SUITE(NewCallModelTester);
+    CPPUNIT_TEST(testCreateAndGetCall);
+    CPPUNIT_TEST(testAcceptHoldUnholdHangupCall);
+    CPPUNIT_TEST_SUITE_END();
+
+public:
+    NewCallModelTester();
+    /**
+     * Method automatically called before each test by CppUnit
+     */
+    void setUp();
+    /**
+     * Create a call between "ring2" and "contact0" and retrieve it.
+     */
+    void testCreateAndGetCall();
+    /**
+     * Simulate an incoming call from "contact1" for "ring2"
+     * Accept the call, call status should be IN_PROGRESS
+     * Hold the call, call status should be HOLD
+     * Unhold the call, call status should be IN_PROGRESS
+     * then hangUp.
+     */
+    void testAcceptHoldUnholdHangupCall();
+    /**
+     * Method automatically called after each test by CppUnit
+     */
+    void tearDown();
+
+protected:
+    std::unique_ptr<lrc::api::Lrc> lrc_;
+    const lrc::api::account::Info& accInfo_;
+};
+
+} // namespace test
+} // namespace ring
-- 
GitLab