1 /*
2 Tests for Kopete Message
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 MessageTest : public QObject
118 {
119 Q_OBJECT
120 private slots:
121 void testMessageCreation();
122 void testMessageCreation_data();
123 };
124
testMessageCreation_data()125 void MessageTest::testMessageCreation_data()
126 {
127 QTest::addColumn<QString>("escapedTest");
128 QTest::addColumn<QString>("escapedExpected");
129 QTest::addColumn<QString>("unescapedTest");
130 QTest::addColumn<QString>("unescapedExpected");
131 QTest::addColumn<QString>("toContactId");
132 QTest::addColumn<QString>("fromContactId");
133 QTest::addColumn<QString>("icon");
134
135 QTest::newRow("Ideal Case") << QStringLiteral("Hello\nThere") << QStringLiteral("Hello<br />There") << QStringLiteral("Hello<br />There") << QStringLiteral("Hello\nThere") << QStringLiteral("Roman") << QStringLiteral("Gaurav") << QStringLiteral("Icon");
136 QTest::newRow("Negative Case") << QStringLiteral("Hello\tThere") << QStringLiteral("Hello There") << QStringLiteral("Hello There") << QStringLiteral("Hello\tThere") << QStringLiteral("Roman") << QStringLiteral("Gaurav") << QStringLiteral("Icon");
137 QTest::newRow("Empty Case") << QString() << QString() << QString() << QString() << QString() << QString() << QString();
138 }
139
testMessageCreation()140 void MessageTest::testMessageCreation()
141 {
142 QFETCH(QString, fromContactId);
143 QFETCH(QString, toContactId);
144 QFETCH(QString, icon);
145 QFETCH(QString, escapedTest);
146 QFETCH(QString, unescapedTest);
147 QFETCH(QString, escapedExpected);
148 QFETCH(QString, unescapedExpected);
149
150 Kopete::MetaContact *parentMetaContact = new Kopete::MetaContact();
151 DummyProtocol *dummyProtocol = new DummyProtocol();
152 DummyAccount *dummyAccount = new DummyAccount(dummyProtocol);
153 DummyContact *fromContact = new DummyContact(dummyAccount, fromContactId, parentMetaContact, icon);
154 DummyContact *toContact = new DummyContact(dummyAccount, toContactId, parentMetaContact, icon);
155 Kopete::Message msg(fromContact, toContact);
156 QCOMPARE(escapedExpected, msg.escape(escapedTest));
157 QCOMPARE(unescapedExpected, msg.unescape(unescapedTest));
158 delete dummyAccount; // Contact gets automatically deleted
159 delete dummyProtocol;
160 delete parentMetaContact;
161 }
162
163 QTEST_MAIN(MessageTest)
164 #include "kopetemessagetest.moc"
165