summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/chatwindow/tests/chatwindowstylerendering_test.cpp
blob: 53f6ec71494a6928b78fff48c4c81b388cb06eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
    Adium(and Kopete) ChatWindowStyle format rendering test suite

    Copyright (c) 2005      by Michaƫl Larouche       <michael.larouche@kdemail.net>

    Kopete    (c) 2002-2005 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * 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 "chatwindowstylerendering_test.h"

#include <stdlib.h>

// TQt includes
#include <tqdir.h>
#include <tqfile.h>
#include <tqdatetime.h>
#include <tqtextstream.h>

// KDE includes
#include <kapplication.h>
#include <kunittest/module.h>
#include <kinstance.h>
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>

#include <kopetechatwindowstyle.h>

// Libkopete includes
#include <kopeteprotocol.h>
#include <kopetemetacontact.h>
#include <kopeteaccount.h>
#include <kopetecontact.h>
#include <kopetechatsession.h>
#include <kopetechatsessionmanager.h>

using namespace Kopete;

KUNITTEST_MODULE( kunittest_chatwindowstylerendering_test, "KopeteChatWindowTestSuite");
KUNITTEST_MODULE_REGISTER_TESTER( ChatWindowStyleRendering_Test );

// Reimplement Kopete::Contact and its abstract method
class FakeContact : public Kopete::Contact
{
public:
	FakeContact (Kopete::Account *account, const TQString &id, Kopete::MetaContact *mc ) : Kopete::Contact( account, id, mc ) {}
	virtual Kopete::ChatSession *manager(Kopete::Contact::CanCreateFlags /*c*/) { return 0L; }
	virtual void slotUserInfo() {};
};

class FakeProtocol : public Kopete::Protocol
{
public:
FakeProtocol( KInstance *instance, TQObject *tqparent, const char *name ) : Kopete::Protocol(instance, tqparent, name)
{
	
}

Account* createNewAccount( const TQString &/*accountId*/ )
{
	return 0L;
}

AddContactPage* createAddContactWidget( TQWidget */*tqparent*/, Kopete::Account */*account*/)
{
	return 0L;
}

KopeteEditAccountWidget* createEditAccountWidget( Kopete::Account */*account*/, TQWidget */*tqparent */)
{
	return 0L;
}

};

class FakeAccount : public Kopete::Account
{
public:
FakeAccount(Kopete::Protocol *tqparent, const TQString &accountID, const char *name) : Kopete::Account(tqparent, accountID, name)
{

}

~FakeAccount()
{

}

bool createContact( const TQString &/*contactId*/, Kopete::MetaContact */*parentContact*/ )
{
	return true;
}

void connect( const Kopete::OnlineStatus& /*initialtqStatus*/)
{
	// do nothing
}

void disconnect()
{
	// do nothing
}

void setOnlineStatus( const Kopete::OnlineStatus& /*status*/ , const TQString &/*reason*/)
{
	// do nothing
}
};

class ChatWindowStyleRendering_Test::Private
{
public:
	Private()
	{
		protocol = new FakeProtocol( new KInstance(TQCString("test-kopete-message")), 0L, "test-kopete-message");
		account = new FakeAccount(protocol, TQString("testaccount"), 0);

		// Create fake meta/contacts
		myselfMetaContact = new Kopete::MetaContact();
		myself = new FakeContact(account, "bob@localhost", myselfMetaContact);
		myself->setNickName("Bob");
		otherMetaContact = new Kopete::MetaContact();
		other = new FakeContact(account, "audrey@localhost", otherMetaContact);
		other->setNickName("Audrey");
		myselfMetaContact->setDisplayName("Bob");
		myselfMetaContact->setDisplayNameSource(Kopete::MetaContact::SourceCustom);
		otherMetaContact->setDisplayName("Audrey");
		otherMetaContact->setDisplayNameSource(Kopete::MetaContact::SourceCustom);

		Kopete::ContactPtrList contactList;
		contactList.append(other);
		// Create fakeChatSession
		fakeChatSession = Kopete::ChatSessionManager::self()->create(myself, contactList, 0);
		fakeChatSession->setDisplayName("Test Session");

		// Create testStyle
		testStyle = new ChatWindowStyle(TQString(SRCDIR)+TQString("/TestStyle"));
	}
	~Private()
	{
		delete myselfMetaContact;
		delete otherMetaContact;
		delete myself;
		delete other;
		delete fakeChatSession;
	}
	FakeProtocol *protocol;
	FakeAccount *account;
	Kopete::MetaContact *myselfMetaContact;
	Kopete::MetaContact *otherMetaContact;
	FakeContact *myself;
	FakeContact *other;
	Kopete::ChatSession *fakeChatSession;
	ChatWindowStyle *testStyle;

	TQString resultHTML;
};



ChatWindowStyleRendering_Test::ChatWindowStyleRendering_Test()
{
	d = new Private;
}

ChatWindowStyleRendering_Test::~ChatWindowStyleRendering_Test()
{
	delete d;
}

void ChatWindowStyleRendering_Test::allTests()
{
	// change user data dir to avoid messing with user's .kde dir
	setenv( "KDEHOME", TQFile::encodeName( TQDir::homeDirPath() + "/.kopete-unittest" ), true );

	KApplication::disableAutoDcopRegistration();
	//KCmdLineArgs::init(argc,argv,"testkopetemessage", 0, 0, 0, 0);
	KApplication app;

	chatPart = new ChatMessagePart(d->fakeChatSession, 0, 0);

	testHeaderRendering();
	testMessageRendering();
	testStatusRendering();
	//testFullRendering();
}

void ChatWindowStyleRendering_Test::testHeaderRendering()
{
	TQString expectedHtml = TQString(
"<div><span id=\"KopeteHeaderChatNameInternal\">Test Session</span></div>\n"
"<div>Bob</div>\n"
"<div>Audrey</div>\n"
"<div>Incoming/buddy_icon.png</div>\n"
"<div>Outgoing/buddy_icon.png</div>\n"
"<div>%1</div>\n"
"<div>%2</div>"
	).tqarg(KGlobal::locale()->formatDateTime( TQDateTime::tqcurrentDateTime(), true, true ) )
	.tqarg(TQDateTime::tqcurrentDateTime().toString("hh:mm"));

	TQString headerHtml = d->testStyle->getHeaderHtml();
	TQString resultHtml;

	resultHtml = chatPart->formatStyleKeywords(headerHtml);

	kdDebug(14000) << "Result HTML: " << resultHtml << endl;

	CHECK(resultHtml, expectedHtml);
}

void ChatWindowStyleRendering_Test::testMessageRendering()
{
	TQString expectedIncomingHtml = TQString(
"Incoming:\n"
"<div>Incoming/buddy_icon.png</div>\n"
"<div><a href=\"kopetemessage://audrey@localhost/?protocolId=Kopete::Protocol&amp;accountId=testaccount\" class=\"KopeteDisplayName\">audrey@localhost</a></div>\n"
"<div><a href=\"kopetemessage://audrey@localhost/?protocolId=Kopete::Protocol&amp;accountId=testaccount\" class=\"KopeteDisplayName\">Audrey</a></div>\n"
"<div>Kopete</div>\n"
"<div><span style=\"\"class=\"KopeteMessageBody\">Test</span></div>\n"
"<div>%1</div>\n"
"<div>%2</div>\n"
"<div id=\"insert\">"
	).tqarg( TQDateTime::tqcurrentDateTime().toString( "hh:mm:ss" ), TQDateTime::tqcurrentDateTime().toString( "hh:mm" ) );

	TQString expectedOutgoingHtml = TQString(
"Outgoing:\n"
"<div>Outgoing/buddy_icon.png</div>\n"
"<div><a href=\"kopetemessage://bob@localhost/?protocolId=Kopete::Protocol&amp;accountId=testaccount\" class=\"KopeteDisplayName\">bob@localhost</a></div>\n"
"<div><a href=\"kopetemessage://bob@localhost/?protocolId=Kopete::Protocol&amp;accountId=testaccount\" class=\"KopeteDisplayName\">Bob</a></div>\n"
"<div>Kopete</div>\n"
"<div><span style=\"\"class=\"KopeteMessageBody\">Hello there</span></div>\n"
"<div>%1</div>\n"
"<div>%2</div>\n"
"<div id=\"insert\">"
	).tqarg( TQDateTime::tqcurrentDateTime().toString( "hh:mm:ss" ), TQDateTime::tqcurrentDateTime().toString( "hh:mm" ) );


	TQString tempHtml;
	TQString resultHtml;
	
	Kopete::Message msgIn(d->other, d->myself, TQString::fromUtf8("Test"), Kopete::Message::Inbound );
	Kopete::Message msgOut(d->myself, d->other, TQString::fromUtf8("Hello there"), Kopete::Message::Outbound);

	tempHtml = d->testStyle->getIncomingHtml();
	resultHtml = chatPart->formatStyleKeywords(tempHtml, msgIn);

	kdDebug(14000) << "Message incoming HTML: " << resultHtml << endl;

	CHECK(resultHtml, expectedIncomingHtml);

	tempHtml = d->testStyle->getOutgoingHtml();
	resultHtml = chatPart->formatStyleKeywords(tempHtml, msgOut);

	kdDebug(14000) << "Message outgoing HTML: " << resultHtml << endl;

	CHECK(resultHtml, expectedOutgoingHtml);
}

void ChatWindowStyleRendering_Test::testStatusRendering()
{
	TQString expectedStatusHtml = TQString(
"<div><span style=\"\"class=\"KopeteMessageBody\">A contact went offline.</span></div>\n"
"<div>%1</div>\n"
"<div>%2</div>"
	).tqarg( TQDateTime::tqcurrentDateTime().toString( "hh:mm:ss" ), TQDateTime::tqcurrentDateTime().toString( "hh:mm" ) );

	TQString statusHtml = d->testStyle->getStatusHtml();
	TQString resultHtml;
	
	Kopete::Message msgtqStatus(0,0, TQString::fromUtf8("A contact went offline."), Kopete::Message::Internal);
	resultHtml = chatPart->formatStyleKeywords(statusHtml, msgtqStatus);

	CHECK(resultHtml, expectedStatusHtml);
}

void ChatWindowStyleRendering_Test::testFullRendering()
{
	TQString expectedFullHtml;
	TQString resultHtml;

	Kopete::Message msgIn1(d->myself, d->other, TQString("Hello there !"), Kopete::Message::Inbound);
	Kopete::Message msgIn2(d->myself, d->other, TQString("How are you doing ?"), Kopete::Message::Inbound);
	Kopete::Message msgOut1(d->other, d->myself, TQString("Fine and you ?"), Kopete::Message::Outbound);
	Kopete::Message msgtqStatus1(d->myself,d->other, TQString("You are now marked as away."), Kopete::Message::Internal);
	Kopete::Message msgtqStatus2(d->myself,d->other, TQString("You are now marked as online."), Kopete::Message::Internal);
	Kopete::Message msgIn3(d->myself, d->other, TQString("Well, doing some tests."), Kopete::Message::Internal);
	Kopete::Message msgOut2(d->other, d->myself, TQString("All your bases are belong to us."), Kopete::Message::Outbound);
	Kopete::Message msgOut3(d->other, d->myself, TQString("You are on the way to destruction"), Kopete::Message::Outbound);

	// Change style on the fly in ChatMessagePart so this test would run
	chatPart->setStyle(d->testStyle);
	
	// Simulate a consersation
	chatPart->appendMessage(msgIn1);
	chatPart->appendMessage(msgIn2);
	chatPart->appendMessage(msgOut1);
	chatPart->appendMessage(msgtqStatus1);
	chatPart->appendMessage(msgtqStatus2);
	chatPart->appendMessage(msgIn3);
	chatPart->appendMessage(msgOut2);
	chatPart->appendMessage(msgOut3);

	resultHtml = chatPart->htmlDocument().toHTML();

	// Read the expected(sample) HTML from file.
	TQFile sampleHtml(TQString(SRCDIR)+"sample.html");
	if(sampleHtml.open(IO_ReadOnly))
	{
		TQTextStream stream(&sampleHtml);
		stream.setEncoding(TQTextStream::UnicodeUTF8);
		expectedFullHtml = stream.read();
		sampleHtml.close();
	}

	CHECK(resultHtml, expectedFullHtml);
}