/* Adium(and Kopete) ChatWindowStyle format rendering test suite Copyright (c) 2005 by Michaƫl Larouche Kopete (c) 2002-2005 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 "chatwindowstylerendering_test.h" #include // TQt includes #include #include #include #include // KDE includes #include #include #include #include #include #include #include // Libkopete includes #include #include #include #include #include #include using namespace Kopete; KUNITTEST_MODULE( tdeunittest_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( TDEInstance *instance, TQObject *parent, const char *name ) : Kopete::Protocol(instance, parent, name) { } Account* createNewAccount( const TQString &/*accountId*/ ) { return 0L; } AddContactPage* createAddContactWidget( TQWidget */*parent*/, Kopete::Account */*account*/) { return 0L; } KopeteEditAccountWidget* createEditAccountWidget( Kopete::Account */*account*/, TQWidget */*parent */) { return 0L; } }; class FakeAccount : public Kopete::Account { public: FakeAccount(Kopete::Protocol *parent, const TQString &accountID, const char *name) : Kopete::Account(parent, accountID, name) { } ~FakeAccount() { } bool createContact( const TQString &/*contactId*/, Kopete::MetaContact */*parentContact*/ ) { return true; } void connect( const Kopete::OnlineStatus& /*initialStatus*/) { // 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 TDEInstance(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( "TDEHOME", TQFile::encodeName( TQDir::homeDirPath() + "/.kopete-unittest" ), true ); TDEApplication::disableAutoDcopRegistration(); //TDECmdLineArgs::init(argc,argv,"testkopetemessage", 0, 0, 0, 0); TDEApplication app; chatPart = new ChatMessagePart(d->fakeChatSession, 0, 0); testHeaderRendering(); testMessageRendering(); testStatusRendering(); //testFullRendering(); } void ChatWindowStyleRendering_Test::testHeaderRendering() { TQString expectedHtml = TQString( "
Test Session
\n" "
Bob
\n" "
Audrey
\n" "
Incoming/buddy_icon.png
\n" "
Outgoing/buddy_icon.png
\n" "
%1
\n" "
%2
" ).arg(TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime(), true, true ) ) .arg(TQDateTime::currentDateTime().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" "
Incoming/buddy_icon.png
\n" "\n" "\n" "
Kopete
\n" "
Test
\n" "
%1
\n" "
%2
\n" "
" ).arg( TQDateTime::currentDateTime().toString( "hh:mm:ss" ), TQDateTime::currentDateTime().toString( "hh:mm" ) ); TQString expectedOutgoingHtml = TQString( "Outgoing:\n" "
Outgoing/buddy_icon.png
\n" "\n" "\n" "
Kopete
\n" "
Hello there
\n" "
%1
\n" "
%2
\n" "
" ).arg( TQDateTime::currentDateTime().toString( "hh:mm:ss" ), TQDateTime::currentDateTime().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( "
A contact went offline.
\n" "
%1
\n" "
%2
" ).arg( TQDateTime::currentDateTime().toString( "hh:mm:ss" ), TQDateTime::currentDateTime().toString( "hh:mm" ) ); TQString statusHtml = d->testStyle->getStatusHtml(); TQString resultHtml; Kopete::Message msgStatus(0,0, TQString::fromUtf8("A contact went offline."), Kopete::Message::Internal); resultHtml = chatPart->formatStyleKeywords(statusHtml, msgStatus); 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 msgStatus1(d->myself,d->other, TQString("You are now marked as away."), Kopete::Message::Internal); Kopete::Message msgStatus2(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(msgStatus1); chatPart->appendMessage(msgStatus2); 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); }