diff --git a/tests/libkopete/CMakeLists.txt b/tests/libkopete/CMakeLists.txt index 6b01a67c7..cba5aaa61 100755 --- a/tests/libkopete/CMakeLists.txt +++ b/tests/libkopete/CMakeLists.txt @@ -1,27 +1,28 @@ 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 ) 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}) ecm_add_test(kopetecontactlistelementtest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopeteglobaltest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopeteidentitytest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopeteonlinestatusmanagertest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopeteinfoeventtest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopetecontacttest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopetemessagetest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) ecm_add_test(kopeteaccounttest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) +ecm_add_test(kopetemetacontacttest.cpp LINK_LIBRARIES ${KOPETE_TEST_LIBRARIES}) diff --git a/tests/libkopete/kopetemetacontacttest.cpp b/tests/libkopete/kopetemetacontacttest.cpp new file mode 100644 index 000000000..4edbeff98 --- /dev/null +++ b/tests/libkopete/kopetemetacontacttest.cpp @@ -0,0 +1,206 @@ +/* + Tests for Kopete::MetaContact + + 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 "kopetecontact.h" +#include "kopetemetacontact.h" +#include "kopeteprotocol.h" +#include "kopeteaccount.h" +#include "kopetechatsession.h" + +#include +#include + +class DummyProtocol : public Kopete::Protocol +{ +public: + DummyProtocol() : Kopete::Protocol(nullptr, false) + { + + } + Kopete::Account *createNewAccount(const QString &accountId) + { + Q_UNUSED(accountId) + return nullptr; + } + AddContactPage *createAddContactWidget(QWidget *parent, Kopete::Account *account) + { + Q_UNUSED(parent) + Q_UNUSED(account) + return nullptr; + } + KopeteEditAccountWidget *createEditAccountWidget(Kopete::Account *account, QWidget *parent) + { + Q_UNUSED(account) + Q_UNUSED(parent) + return nullptr; + } + void setCapability(Protocol::Capabilities capabilities) + { + this->setCapabilities(capabilities); + } +}; + +class DummyAccount : public Kopete::Account +{ + Q_OBJECT +public: + DummyAccount(DummyProtocol *dummyProtocol):Kopete::Account(dummyProtocol, QStringLiteral("Dummy Account")) + { + + } +protected: + bool createContact(const QString &contactId, Kopete::MetaContact *parentContact) + { + Q_UNUSED(contactId); + Q_UNUSED(parentContact); + return true; + } +public Q_SLOTS: + void connect(const Kopete::OnlineStatus &initialStatus = Kopete::OnlineStatus()) + { + Q_UNUSED(initialStatus); + } + void disconnect() + { + + } + void setOnlineStatus(const Kopete::OnlineStatus &status, const Kopete::StatusMessage &reason = Kopete::StatusMessage(), const OnlineStatusOptions &options = None) + { + Q_UNUSED(status); + Q_UNUSED(reason); + Q_UNUSED(options); + } + void setStatusMessage(const Kopete::StatusMessage &statusMessage) + { + Q_UNUSED(statusMessage); + } + void setSelf(Kopete::Contact *contact) + { + setMyself(contact); + } + void settingAccountLabel(const QString &label) + { + setAccountLabel(label); + } +}; + +class DummyContact : public Kopete::Contact +{ +public: + DummyContact(Kopete::Account *account, const QString &contactId, Kopete::MetaContact *parent, const QString &icon) : Kopete::Contact(account, contactId, parent, icon) + { + + }; + Kopete::ChatSession *manager(CanCreateFlags canCreate = CannotCreate) + { + Q_UNUSED(canCreate); + return nullptr; + } +}; + +class MetaContactTest : public QObject +{ + Q_OBJECT +private slots: + void testUUID(); + void testUUID_data(); + void testSignalConnection(); + void testContactList_data(); + void testContactList(); +}; + +void MetaContactTest::testUUID_data() +{ + QTest::addColumn("test"); + QTest::addColumn("expected"); + + QTest::newRow("Ideal Case") << QStringLiteral("UUid") << QUuid(QStringLiteral("UUid")).toString(); + QTest::newRow("Empty Case") << QString() << QUuid(QString()).toString(); +} + +void MetaContactTest::testUUID() +{ + Kopete::MetaContact *dummy = new Kopete::MetaContact(); + QFETCH(QString, test); + QFETCH(QString, expected); + dummy->setMetaContactId(QUuid(test)); + QCOMPARE(dummy->metaContactId().toString(), expected); + delete dummy; +} + +void MetaContactTest::testSignalConnection() +{ + Kopete::MetaContact *dummy = new Kopete::MetaContact(); + QSignalSpy spy(dummy, &Kopete::MetaContact::persistentDataChanged); + emit dummy->pluginDataChanged(); + QCOMPARE(spy.count(), 1); + emit dummy->useCustomIconChanged(true); + QCOMPARE(spy.count(), 2); + emit dummy->displayNameChanged(QString(), QString()); + QCOMPARE(spy.count(), 3); + emit dummy->contactAdded(nullptr); + QCOMPARE(spy.count(), 4); + emit dummy->contactRemoved(nullptr); + QCOMPARE(spy.count(), 5); + emit dummy->iconChanged(Kopete::ContactListElement::Open, QString()); + QCOMPARE(spy.count(), 6); + emit dummy->movedToGroup(nullptr, nullptr, nullptr); + QCOMPARE(spy.count(), 7); + emit dummy->removedFromGroup(nullptr, nullptr); + QCOMPARE(spy.count(), 8); + emit dummy->addedToGroup(nullptr, nullptr); + QCOMPARE(spy.count(), 9); + delete dummy; +} + +void MetaContactTest::testContactList_data() +{ + QTest::addColumn("contactId"); + QTest::addColumn("icon"); + + QTest::newRow("Ideal Case") << QStringLiteral("ContactId") << QStringLiteral("Icon"); + QTest::newRow("Empty Case") << QString() << QString(); +} + + +void MetaContactTest::testContactList() +{ + QFETCH(QString, contactId); + QFETCH(QString, icon); + + Kopete::MetaContact *parentMetaContact = new Kopete::MetaContact(); + DummyProtocol *dummyProtocol = new DummyProtocol(); + DummyAccount *dummyAccount = new DummyAccount(dummyProtocol); + DummyContact *dummyContact = new DummyContact(dummyAccount, contactId, parentMetaContact, icon); + Kopete::MetaContact *dummy = new Kopete::MetaContact(); + QSignalSpy spy(dummy, &Kopete::MetaContact::contactAdded); + dummy->addContact(dummyContact); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QSignalSpy spy1(dummy, &Kopete::MetaContact::contactRemoved); + dummy->removeContact(dummyContact); + QVERIFY(spy1.isValid()); + QCOMPARE(spy1.count(), 1); + delete parentMetaContact; + delete dummyProtocol; + delete dummy; +} + + +QTEST_MAIN(MetaContactTest) +#include "kopetemetacontacttest.moc"