diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 345c9484..620fda5d 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 00000000..ea3a8816 --- /dev/null +++ b/tests/unittests/account_unittest.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2021 by Savoir-faire Linux + * Author: Albert Babí Oller + * Author: Mingrui Zhang + * + * 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 . + */ + +#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 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 3d8efdf0..00000000 --- a/tests/unittests/accountadapter_unittest.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2021 by Savoir-faire Linux - * Author: Albert Babí Oller - * Author: Mingrui Zhang - * - * 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 . - */ - -#include "globaltestenvironment.h" - -#include "mainapplication.h" -#include "qmlregister.h" -#include "appsettingsmanager.h" -#include "connectivitymonitor.h" -#include "systemtray.h" - -#include "accountadapter.h" - -#include - -#ifdef Q_OS_WIN -#include -#endif - -#if defined _MSC_VER && !COMPILE_ONLY -#include -#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_; - - QScopedPointer lrcInstance_; - QScopedPointer connectivityMonitor_; - QScopedPointer settingsManager_; - QScopedPointer 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 00000000..0ab94fe4 --- /dev/null +++ b/tests/unittests/contact_unittest.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2021 by Savoir-faire Linux + * Author: Mingrui Zhang + * + * 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 . + */ + +#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 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 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 b0cf45f6..07fe0c33 100644 --- a/tests/unittests/globaltestenvironment.h +++ b/tests/unittests/globaltestenvironment.h @@ -16,4 +16,70 @@ * along with this program. If not, see . */ -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 +#include + +#include + +#ifdef Q_OS_WIN +#include +#endif + +#if defined _MSC_VER && !COMPILE_ONLY +#include +#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; + + QScopedPointer lrcInstance; + QScopedPointer connectivityMonitor; + QScopedPointer settingsManager; + QScopedPointer 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 8a28e5b4..1d477c15 100644 --- a/tests/unittests/main_unittest.cpp +++ b/tests/unittests/main_unittest.cpp @@ -22,9 +22,7 @@ #include #include -#include - -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; }