diff --git a/tests/libkopete/CMakeLists.txt b/tests/libkopete/CMakeLists.txt index 236284b8c..08593085e 100755 --- a/tests/libkopete/CMakeLists.txt +++ b/tests/libkopete/CMakeLists.txt @@ -1,11 +1,18 @@ include_directories( ${KOPETE_INCLUDES} + ${CMAKE_SOURCE_DIR}/libkopete +) + +set(BLACKLIST_LIB + kopeteblacklistertest.cpp + ${CMAKE_SOURCE_DIR}/libkopete/kopeteblacklister.cpp ) set( KOPETE_TEST_LIBRARIES + kopete Qt5::Test KF5::KIOCore - kopete ) ecm_add_test(kopetetasktest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopetestatusmessagetest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) +ecm_add_test(${BLACKLIST_LIB} TEST_NAME blacklisttest LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) diff --git a/tests/libkopete/kopeteblacklistertest.cpp b/tests/libkopete/kopeteblacklistertest.cpp new file mode 100644 index 000000000..e9948d946 --- /dev/null +++ b/tests/libkopete/kopeteblacklistertest.cpp @@ -0,0 +1,98 @@ +/* + Tests for Kopete BlackLister + + Copyright (c) 2017 by Vijay Krishnavanshi + + Kopete (c) 2002-2017 by the Kopete developers + + ************************************************************************* + * * + * 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) any later version. * + * * + ************************************************************************* +*/ + + +#include "kopeteblacklister.h" + +#include +#include +class BlackListerTest : public QObject +{ + Q_OBJECT +private slots: + void testAddAndRemoveContact(); + void testPersistanceOfBlackListContacts(); +}; + +void BlackListerTest::testAddAndRemoveContact() +{ + const QString ProtocolID = QStringLiteral("TestBed"); + const QString AccountID = QStringLiteral("Default"); + const QString Contact1 = QStringLiteral("Dummy User 1"); + const QString Contact2 = QStringLiteral("Dummy User 2"); + const QString Contact3 = QStringLiteral("Dummy User 3"); + const QString Contact4 = QStringLiteral("Dummy User 4"); + + Kopete::BlackLister *A = new Kopete::BlackLister(ProtocolID, AccountID, nullptr); + // Cleanup is required as it may already be present in memory because of unsuccessful test/ interruption + if(A->isBlocked(Contact1)) A->removeContact(Contact1); + if(A->isBlocked(Contact2)) A->removeContact(Contact2); + if(A->isBlocked(Contact3)) A->removeContact(Contact3); + if(A->isBlocked(Contact4)) A->removeContact(Contact4); + + QSignalSpy spyAdd(A, &Kopete::BlackLister::contactAdded); + QSignalSpy spyRemove(A, &Kopete::BlackLister::contactRemoved); + QVERIFY(!A->isBlocked(Contact1)); + A->addContact(Contact1); + QVERIFY(A->isBlocked(Contact1)); + QVERIFY(spyAdd.count() == 1); + A->addContact(Contact2); + QVERIFY(spyAdd.count() == 2); + A->addContact(Contact3); + QVERIFY(spyAdd.count() == 3); + A->addContact(Contact4); + QVERIFY(spyAdd.count() == 4); + A->removeContact(Contact1); + QVERIFY(spyRemove.count() == 1); + QVERIFY(!A->isBlocked(Contact1)); + A->removeContact(Contact2); + QVERIFY(spyRemove.count() == 2); + QVERIFY(!A->isBlocked(Contact2)); + A->removeContact(Contact3); + QVERIFY(spyRemove.count() == 3); + QVERIFY(!A->isBlocked(Contact3)); + A->removeContact(Contact4); + QVERIFY(spyRemove.count() == 4); + QVERIFY(!A->isBlocked(Contact4)); +} + + +void BlackListerTest::testPersistanceOfBlackListContacts() +{ + const QString ProtocolID = QStringLiteral("TestBed"); + const QString AccountID = QStringLiteral("Default"); + const QString Contact1 = QStringLiteral("Dummy User 1"); + const QString Contact2 = QStringLiteral("Dummy User 2"); + Kopete::BlackLister *A = new Kopete::BlackLister(ProtocolID, AccountID, nullptr); + // Cleanup is required as it may already be present in memory because of unsuccessful test/ interruption + if(A->isBlocked(Contact1)) A->removeContact(Contact1); + if(A->isBlocked(Contact2)) A->removeContact(Contact2); + A->addContact(Contact1); + + // They use same config file should have same records + Kopete::BlackLister *B = new Kopete::BlackLister(ProtocolID, AccountID, nullptr); + QVERIFY(B->isBlocked(Contact1)); + B->addContact(Contact2); + QVERIFY(B->isBlocked(Contact2)); + A->removeContact(Contact1); + B->removeContact(Contact2); + QVERIFY(!A->isBlocked(Contact1)); + QVERIFY(!B->isBlocked(Contact2)); +} + +QTEST_MAIN(BlackListerTest) +#include "kopeteblacklistertest.moc"