From 872fbeb5d30c3359ea3c09ca068f580f1b677be4 Mon Sep 17 00:00:00 2001 From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com> Date: Tue, 15 Jun 2021 11:13:22 -0400 Subject: [PATCH] unittest: add SIP account creation and contact adding tests Add global test environment for google test Change-Id: Id035ebf31498e11574b6bf4671b4c6e12d29ddc7 --- tests/CMakeLists.txt | 3 +- tests/unittests/account_unittest.cpp | 94 ++++++++++++++++++ tests/unittests/accountadapter_unittest.cpp | 89 ----------------- tests/unittests/contact_unittest.cpp | 104 ++++++++++++++++++++ tests/unittests/globaltestenvironment.h | 68 ++++++++++++- tests/unittests/main_unittest.cpp | 13 ++- 6 files changed, 275 insertions(+), 96 deletions(-) create mode 100644 tests/unittests/account_unittest.cpp delete mode 100644 tests/unittests/accountadapter_unittest.cpp create mode 100644 tests/unittests/contact_unittest.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 345c9484f..620fda5da 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -63,7 +63,8 @@ set(UNIT_TESTS_HEADER_FILES ${CMAKE_SOURCE_DIR}/tests/unittests/globaltestenviro set(UNIT_TESTS_SOURCE_FILES ${CMAKE_SOURCE_DIR}/tests/unittests/main_unittest.cpp - ${CMAKE_SOURCE_DIR}/tests/unittests/accountadapter_unittest.cpp) + ${CMAKE_SOURCE_DIR}/tests/unittests/account_unittest.cpp + ${CMAKE_SOURCE_DIR}/tests/unittests/contact_unittest.cpp) add_executable(unittests ${UNIT_TESTS_HEADER_FILES} diff --git a/tests/unittests/account_unittest.cpp b/tests/unittests/account_unittest.cpp new file mode 100644 index 000000000..ea3a8816b --- /dev/null +++ b/tests/unittests/account_unittest.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2021 by Savoir-faire Linux + * Author: Albert Babà Oller <albert.babi@savoirfairelinux.com> + * Author: Mingrui Zhang <mingrui.zhang@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, see <https://www.gnu.org/licenses/>. + */ + +#include "globaltestenvironment.h" + +TestEnvironment globalEnv; + +/*! + * Test fixture for AccountAdapter testing + */ +class AccountFixture : public ::testing::Test +{ +public: + // Prepare unit test context. Called at + // prior each unit test execution + void SetUp() override {} + + // Close unit test context. Called + // after each unit test ending + void TearDown() override {} +}; + +/*! + * WHEN There is no account initially. + * THEN Account list should be empty. + */ +TEST_F(AccountFixture, InitialAccountListCheck) +{ + auto accountListSize = globalEnv.lrcInstance->accountModel().getAccountList().size(); + + ASSERT_EQ(accountListSize, 0); +} + +/*! + * WHEN An SIP account is created. + * THEN The size of the account list should be one. + */ +TEST_F(AccountFixture, CreateSIPAccountTest) +{ + // AccountAdded signal spy + QSignalSpy accountAddedSpy(&globalEnv.lrcInstance->accountModel(), + &lrc::api::NewAccountModel::accountAdded); + + // Create SIP Acc + globalEnv.accountAdapter->createSIPAccount(QVariantMap()); + + if (accountAddedSpy.count() < 1) + QVERIFY(accountAddedSpy.wait()); + + QList<QVariant> accountAddedArguments = accountAddedSpy.takeFirst(); + QVERIFY(accountAddedArguments.at(0).type() == QVariant::String); + + // Select the created account + globalEnv.lrcInstance->setCurrentAccountId(accountAddedArguments.at(0).toString()); + + auto accountListSize = globalEnv.lrcInstance->accountModel().getAccountList().size(); + ASSERT_EQ(accountListSize, 1); + + // Make sure the account setup is done + QSignalSpy accountStatusChangedSpy(&globalEnv.lrcInstance->accountModel(), + &lrc::api::NewAccountModel::accountStatusChanged); + + if (accountStatusChangedSpy.count() < 1) + QVERIFY(accountStatusChangedSpy.wait()); + + // Remove the account + globalEnv.lrcInstance->accountModel().removeAccount( + globalEnv.lrcInstance->getCurrentAccountId()); + + QSignalSpy accountRemovedSpy(&globalEnv.lrcInstance->accountModel(), + &lrc::api::NewAccountModel::accountRemoved); + + if (accountRemovedSpy.count() < 1) + QVERIFY(accountRemovedSpy.wait()); + + accountListSize = globalEnv.lrcInstance->accountModel().getAccountList().size(); + ASSERT_EQ(accountListSize, 0); +} \ No newline at end of file diff --git a/tests/unittests/accountadapter_unittest.cpp b/tests/unittests/accountadapter_unittest.cpp deleted file mode 100644 index 3d8efdf06..000000000 --- a/tests/unittests/accountadapter_unittest.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2021 by Savoir-faire Linux - * Author: Albert Babà Oller <albert.babi@savoirfairelinux.com> - * Author: Mingrui Zhang <mingrui.zhang@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, see <https://www.gnu.org/licenses/>. - */ - -#include "globaltestenvironment.h" - -#include "mainapplication.h" -#include "qmlregister.h" -#include "appsettingsmanager.h" -#include "connectivitymonitor.h" -#include "systemtray.h" - -#include "accountadapter.h" - -#include <gtest/gtest.h> - -#ifdef Q_OS_WIN -#include <windows.h> -#endif - -#if defined _MSC_VER && !COMPILE_ONLY -#include <gnutls/gnutls.h> -#endif - -/*! - * Test fixture for AccountAdapter testing - */ -class AccountAdapterFixture : public ::testing::Test -{ -public: - // Prepare unit test context. Called at - // prior each unit test execution - void SetUp() override - { - connectivityMonitor_.reset(new ConnectivityMonitor(nullptr)); - settingsManager_.reset(new AppSettingsManager(nullptr)); - systemTray_.reset(new SystemTray(settingsManager_.get(), nullptr)); - -#if defined _MSC_VER && !COMPILE_ONLY - gnutls_global_init(); -#endif - - std::atomic_bool isMigrating(false); - lrcInstance_.reset( - new LRCInstance(nullptr, nullptr, "", connectivityMonitor_.get(), muteDring)); - lrcInstance_->subscribeToDebugReceived(); - - // setup the adapters (their lifetimes are that of MainApplication) - accountAdapter_.reset( - new AccountAdapter(settingsManager_.get(), lrcInstance_.data(), nullptr)); - } - - // Close unit test context. Called - // after each unit test ending - void TearDown() override {} - - QScopedPointer<AccountAdapter> accountAdapter_; - - QScopedPointer<LRCInstance> lrcInstance_; - QScopedPointer<ConnectivityMonitor> connectivityMonitor_; - QScopedPointer<AppSettingsManager> settingsManager_; - QScopedPointer<SystemTray> systemTray_; -}; - -/*! - * WHEN There is no account initially. - * THEN Account list should be empty. - */ -TEST_F(AccountAdapterFixture, InitialAccountListCheck) -{ - auto accountListSize = lrcInstance_->accountModel().getAccountList().size(); - - ASSERT_EQ(accountListSize, 0); -} \ No newline at end of file diff --git a/tests/unittests/contact_unittest.cpp b/tests/unittests/contact_unittest.cpp new file mode 100644 index 000000000..0ab94fe40 --- /dev/null +++ b/tests/unittests/contact_unittest.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2021 by Savoir-faire Linux + * Author: Mingrui Zhang <mingrui.zhang@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, see <https://www.gnu.org/licenses/>. + */ + +#include "globaltestenvironment.h" + +TestEnvironment globalEnv; + +/*! + * Test fixture for AccountAdapter testing + */ +class ContactFixture : public ::testing::Test +{ +public: + // Prepare unit test context. Called at + // prior each unit test execution + void SetUp() override {} + + // Close unit test context. Called + // after each unit test ending + void TearDown() override {} +}; + +/*! + * WHEN Add an SIP contact. + * THEN ContactAdded signal should be emitted once. + */ +TEST_F(ContactFixture, AddSIPContactTest) +{ + // AccountAdded signal spy + QSignalSpy accountAddedSpy(&globalEnv.lrcInstance->accountModel(), + &lrc::api::NewAccountModel::accountAdded); + + // Create SIP Acc + globalEnv.accountAdapter->createSIPAccount(QVariantMap()); + + if (accountAddedSpy.count() < 1) + QVERIFY(accountAddedSpy.wait()); + + QList<QVariant> accountAddedArguments = accountAddedSpy.takeFirst(); + QVERIFY(accountAddedArguments.at(0).type() == QVariant::String); + + // Select the created account + globalEnv.lrcInstance->setCurrentAccountId(accountAddedArguments.at(0).toString()); + + // Make sure the account setup is done + QSignalSpy accountStatusChangedSpy(&globalEnv.lrcInstance->accountModel(), + &lrc::api::NewAccountModel::accountStatusChanged); + + if (accountStatusChangedSpy.count() < 1) + QVERIFY(accountStatusChangedSpy.wait()); + + // ModelUpdated signal spy + QSignalSpy modelUpdatedSpy(globalEnv.lrcInstance->getCurrentContactModel(), + &lrc::api::ContactModel::modelUpdated); + + // Add temp contact test + globalEnv.lrcInstance->getCurrentConversationModel()->setFilter("test"); + + if (modelUpdatedSpy.count() < 1) + QVERIFY(modelUpdatedSpy.wait()); + + QList<QVariant> modelUpdatedArguments = modelUpdatedSpy.takeFirst(); + QVERIFY(modelUpdatedArguments.at(0).type() == QVariant::String); + + // Get conversation id + auto convId = globalEnv.lrcInstance + ->getConversationFromPeerUri(modelUpdatedArguments.at(0).toString()) + .uid; + ASSERT_EQ(convId.isEmpty(), false); + + // ContactAdded signal spy + QSignalSpy contactAddedSpy(globalEnv.lrcInstance->getCurrentContactModel(), + &lrc::api::ContactModel::contactAdded); + + globalEnv.lrcInstance->getCurrentConversationModel()->makePermanent(convId); + + if (contactAddedSpy.count() < 1) + QVERIFY(contactAddedSpy.wait()); + + // Remove the account + globalEnv.lrcInstance->accountModel().removeAccount( + globalEnv.lrcInstance->getCurrentAccountId()); + + QSignalSpy accountRemovedSpy(&globalEnv.lrcInstance->accountModel(), + &lrc::api::NewAccountModel::accountRemoved); + + if (accountRemovedSpy.count() < 1) + QVERIFY(accountRemovedSpy.wait()); +} \ No newline at end of file diff --git a/tests/unittests/globaltestenvironment.h b/tests/unittests/globaltestenvironment.h index b0cf45f66..07fe0c33b 100644 --- a/tests/unittests/globaltestenvironment.h +++ b/tests/unittests/globaltestenvironment.h @@ -16,4 +16,70 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -extern bool muteDring; \ No newline at end of file +#include "mainapplication.h" +#include "qmlregister.h" +#include "appsettingsmanager.h" +#include "connectivitymonitor.h" +#include "systemtray.h" + +#include "accountadapter.h" + +#include <QTest> +#include <QSignalSpy> + +#include <gtest/gtest.h> + +#ifdef Q_OS_WIN +#include <windows.h> +#endif + +#if defined _MSC_VER && !COMPILE_ONLY +#include <gnutls/gnutls.h> +#endif + +class TestEnvironment +{ +public: + TestEnvironment() = default; + ~TestEnvironment() = default; + + void SetUp() + { + connectivityMonitor.reset(new ConnectivityMonitor(nullptr)); + settingsManager.reset(new AppSettingsManager(nullptr)); + systemTray.reset(new SystemTray(settingsManager.get(), nullptr)); + +#if defined _MSC_VER && !COMPILE_ONLY + gnutls_global_init(); +#endif + + std::atomic_bool isMigrating(false); + lrcInstance.reset( + new LRCInstance(nullptr, nullptr, "", connectivityMonitor.get(), muteDring)); + lrcInstance->subscribeToDebugReceived(); + + // setup the adapters (their lifetimes are that of MainApplication) + accountAdapter.reset(new AccountAdapter(settingsManager.get(), lrcInstance.data(), nullptr)); + } + + void TearDown() + { + accountAdapter.reset(); + + systemTray.reset(); + settingsManager.reset(); + lrcInstance.reset(); + connectivityMonitor.reset(); + } + + bool muteDring {false}; + + QScopedPointer<AccountAdapter> accountAdapter; + + QScopedPointer<LRCInstance> lrcInstance; + QScopedPointer<ConnectivityMonitor> connectivityMonitor; + QScopedPointer<AppSettingsManager> settingsManager; + QScopedPointer<SystemTray> systemTray; +}; + +extern TestEnvironment globalEnv; \ No newline at end of file diff --git a/tests/unittests/main_unittest.cpp b/tests/unittests/main_unittest.cpp index 8a28e5b45..1d477c15d 100644 --- a/tests/unittests/main_unittest.cpp +++ b/tests/unittests/main_unittest.cpp @@ -22,9 +22,7 @@ #include <QApplication> #include <QStandardPaths> -#include <gtest/gtest.h> - -bool muteDring; +TestEnvironment globalEnv; int main(int argc, char* argv[]) @@ -36,7 +34,7 @@ main(int argc, char* argv[]) }); if (end != argv + argc) { - muteDring = true; + globalEnv.muteDring = true; // Adjust the argument count. argc = std::distance(argv, end); @@ -46,6 +44,11 @@ main(int argc, char* argv[]) QApplication a(argc, argv); a.processEvents(); + ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + globalEnv.SetUp(); + auto result = RUN_ALL_TESTS(); + globalEnv.TearDown(); + + return result; } -- GitLab