1 /*
2     Tests for Kopete Account
3 
4     Copyright (c) 2017      by Vijay Krishnavanshi    <vijaykrishnavashi>
5 
6     Kopete    (c) 2002-2017 by the Kopete developers  <kopete-devel@kde.org>
7 
8     *************************************************************************
9     *                                                                       *
10     * This program is free software; you can redistribute it and/or modify  *
11     * it under the terms of the GNU General Public License as published by  *
12     * the Free Software Foundation; either version 2 of the License, or     *
13     * (at your option) any later version.                                   *
14     *                                                                       *
15     *************************************************************************
16 */
17 
18 
19 #include "kopetecontact.h"
20 #include "kopetemetacontact.h"
21 #include "kopeteprotocol.h"
22 #include "kopeteaccount.h"
23 #include "kopetechatsession.h"
24 
25 #include <QObject>
26 #include <QtTest>
27 
28 
29 class DummyProtocol : public Kopete::Protocol
30 {
31 public:
DummyProtocol()32     DummyProtocol() : Kopete::Protocol(nullptr, false)
33     {
34 
35     }
createNewAccount(const QString & accountId)36     Kopete::Account *createNewAccount(const QString &accountId)
37     {
38         Q_UNUSED(accountId)
39         return nullptr;
40     }
createAddContactWidget(QWidget * parent,Kopete::Account * account)41     AddContactPage *createAddContactWidget(QWidget *parent, Kopete::Account *account)
42     {
43         Q_UNUSED(parent)
44         Q_UNUSED(account)
45         return nullptr;
46     }
createEditAccountWidget(Kopete::Account * account,QWidget * parent)47     KopeteEditAccountWidget *createEditAccountWidget(Kopete::Account *account, QWidget *parent)
48     {
49         Q_UNUSED(account)
50         Q_UNUSED(parent)
51         return nullptr;
52     }
setCapability(Protocol::Capabilities capabilities)53     void setCapability(Protocol::Capabilities capabilities)
54     {
55         this->setCapabilities(capabilities);
56     }
57 };
58 
59 class DummyAccount : public Kopete::Account
60 {
61     Q_OBJECT
62 public:
DummyAccount(DummyProtocol * dummyProtocol)63     DummyAccount(DummyProtocol *dummyProtocol):Kopete::Account(dummyProtocol, QStringLiteral("Dummy Account"))
64     {
65 
66     }
67 
68 protected:
createContact(const QString & contactId,Kopete::MetaContact * parentContact)69     bool createContact(const QString &contactId, Kopete::MetaContact *parentContact)
70     {
71         Q_UNUSED(contactId);
72         Q_UNUSED(parentContact);
73         return true;
74     }
75 public Q_SLOTS:
connect(const Kopete::OnlineStatus & initialStatus=Kopete::OnlineStatus ())76     void connect(const Kopete::OnlineStatus &initialStatus = Kopete::OnlineStatus())
77     {
78         Q_UNUSED(initialStatus);
79     }
disconnect()80     void disconnect()
81     {
82 
83     }
setOnlineStatus(const Kopete::OnlineStatus & status,const Kopete::StatusMessage & reason=Kopete::StatusMessage (),const OnlineStatusOptions & options=None)84     void setOnlineStatus(const Kopete::OnlineStatus &status, const Kopete::StatusMessage &reason = Kopete::StatusMessage(), const OnlineStatusOptions &options = None)
85     {
86         Q_UNUSED(status);
87         Q_UNUSED(reason);
88         Q_UNUSED(options);
89     }
setStatusMessage(const Kopete::StatusMessage & statusMessage)90     void setStatusMessage(const Kopete::StatusMessage &statusMessage)
91     {
92         Q_UNUSED(statusMessage);
93     }
setSelf(Kopete::Contact * contact)94     void setSelf(Kopete::Contact *contact)
95     {
96         setMyself(contact);
97     }
settingAccountLabel(const QString & label)98     void settingAccountLabel(const QString &label)
99     {
100         setAccountLabel(label);
101     }
102 };
103 class DummyContact : public Kopete::Contact
104 {
105 public:
DummyContact(Kopete::Account * account,const QString & contactId,Kopete::MetaContact * parent,const QString & icon)106     DummyContact(Kopete::Account *account, const QString &contactId, Kopete::MetaContact *parent, const QString &icon) : Kopete::Contact(account, contactId, parent, icon)
107     {
108 
109     };
110 
manager(CanCreateFlags canCreate=CannotCreate)111     Kopete::ChatSession *manager(CanCreateFlags canCreate = CannotCreate)
112     {
113         Q_UNUSED(canCreate);
114         return nullptr;
115     }
116 };
117 class AccountTest : public QObject
118 {
119     Q_OBJECT
120 private slots:
121     void testAccountCreation();
122 };
123 
testAccountCreation()124 void AccountTest::testAccountCreation()
125 {
126     Kopete::MetaContact *parentMetaContact = new Kopete::MetaContact();
127     DummyProtocol *dummyProtocol = new DummyProtocol();
128     DummyAccount *dummyAccount = new DummyAccount(dummyProtocol);
129     Kopete::StatusMessage reason(QStringLiteral("I want to test if it suspends the account."));
130     const QString contactId = QStringLiteral("ContactId");
131     const QString icon = QStringLiteral("Icon");
132     DummyContact *dummyContact = new DummyContact(dummyAccount, contactId, parentMetaContact, icon);
133     dummyAccount->setSelf(dummyContact);
134     QVERIFY(dummyAccount->suspend(reason));
135     QVERIFY(dummyAccount->resume());
136     QVERIFY(!dummyAccount->hasCustomStatusMenu());
137     const QString contactId_test = QStringLiteral("Contact Id for test add");
138     bool success = dummyAccount->addContact(contactId_test, parentMetaContact, Kopete::Account::Temporary);
139     QVERIFY(success);
140     Kopete::MetaContact *newMetaContact = new Kopete::MetaContact();
141     const QString contactIdTest = QStringLiteral("Contact ID for Test new ");
142     DummyContact *contactToAdd = new DummyContact(dummyAccount, contactIdTest, newMetaContact, icon);
143     // Here it register itself with this constructor call
144     bool status = dummyAccount->registerContact(contactToAdd);
145     QVERIFY(!status);   // This should be false because this contact already exists because of the constructor
146                         // call to create a new contact where it also registers itself
147 
148     const QHash<QString, Kopete::Contact *> contactList = dummyAccount->contacts();
149     Kopete::Contact *expected = contactList[contactIdTest];
150     QCOMPARE(expected->contactId(), contactIdTest);
151     QCOMPARE(dummyAccount->accountLabel(), QStringLiteral("Dummy Account"));
152     const QString accountLabel = QStringLiteral("Account Label");
153     dummyAccount->settingAccountLabel(accountLabel);
154     QCOMPARE(dummyAccount->accountLabel(), accountLabel);
155     const QString contactBlock = QStringLiteral("Block It");
156     QVERIFY(!dummyAccount->isBlocked(contactBlock));
157     dummyAccount->block(contactBlock);
158     QVERIFY(dummyAccount->isBlocked(contactBlock));
159     dummyAccount->unblock(contactBlock);
160     QVERIFY(!dummyAccount->isBlocked(contactBlock));
161 }
162 
163 QTEST_MAIN(AccountTest)
164 #include "kopeteaccounttest.moc"
165