summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/tests/kopetemessage_test.cpp
blob: df675fa188d65de447aea729f0fadd9697702a87 (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
/*
    Tests for Kopete::Message

    Copyright (c) 2005   by Tommi Rantala  <tommi.rantala@cs.helsinki.fi>
    Copyright (c) 2005   by Duncan Mac-Vicar Prett  <duncan@kde.org>
    Copyright (c) 2004   by Richard Smith <kde@metafoo.co.uk>

    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 <stdlib.h>

#include <tqdir.h>
#include <tqfile.h>
#include <kapplication.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include <kprocess.h>
#include <kunittest/module.h>
#include <kdebug.h>

#include "kopetemessage_test.h"
#include "kopeteaccount_mock.h"
#include "kopeteprotocol_mock.h"
#include "kopetecontact_mock.h"
#include "kopetemetacontact_mock.h"
#include "kopeteaccount_mock.h"

using namespace KUnitTest;

KUNITTEST_MODULE( kunittest_kopetemessage_test, "KopeteSuite");
KUNITTEST_MODULE_REGISTER_TESTER( KopeteMessage_Test );

/*
  There are four sets of tests: for each of plain text and html, we have those
  known to work in the current codebase, and those known to fail right now.

  the name convention is working|broken-plaintext|html-number.input|output
*/

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

	// create fake objects needed to build a reasonable testeable message
	m_protocol = new Kopete::Test::Mock::Protocol( new KInstance(TQCString("test-kopete-message")), 0L, "test-kopete-message");
	m_account = new Kopete::Test::Mock::Account(m_protocol, "testaccount");
	m_metaContactMyself = new Kopete::Test::Mock::MetaContact();
	m_metaContactOther = new Kopete::Test::Mock::MetaContact();
	m_contactFrom = new Kopete::Test::Mock::Contact(m_account, TQString::fromLatin1("test-myself"), m_metaContactMyself, TQString());
	m_contactTo = new Kopete::Test::Mock::Contact(m_account, TQString::fromLatin1("test-dest"), m_metaContactOther, TQString());
	m_message = new Kopete::Message( m_contactFrom, m_contactTo, TQString(), Kopete::Message::Outbound, Kopete::Message::PlainText);
}

void KopeteMessage_Test::allTests()
{
	KApplication::disableAutoDcopRegistration();
	//TDECmdLineArgs::init(argc,argv,"testkopetemessage", 0, 0, 0, 0);

	// At least Kopete::Message::asXML() seems to require that a TQApplication
	// is created. Running the console version doesn't create it, but the GUI
	// version does.

	if (!kapp)
		new KApplication();

	testPrimitives();
	testLinkParser();
}

void KopeteMessage_Test::testPrimitives()
{
	/**********************************************
	 * from(), to()
	 *********************************************/

	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foobar", Kopete::Message::Inbound, Kopete::Message::PlainText);
		Q_ASSERT(msg.from());
		Q_ASSERT(!msg.to().isEmpty());
	}

	/**********************************************
	 * Direction
	 *********************************************/

	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foobar", Kopete::Message::Inbound, Kopete::Message::PlainText);
		CHECK(Kopete::Message::Inbound, msg.direction());
	}
	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foobar", Kopete::Message::Outbound, Kopete::Message::RichText);
		CHECK(Kopete::Message::Outbound, msg.direction());
	}
	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foobar", Kopete::Message::Internal, Kopete::Message::RichText);
		CHECK(Kopete::Message::Internal, msg.direction());
	}

	/**********************************************
	 * Message Format
	 *********************************************/

	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foobar", Kopete::Message::Inbound, Kopete::Message::PlainText);
		CHECK(Kopete::Message::PlainText, msg.format());
	}
	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foobar", Kopete::Message::Inbound, Kopete::Message::RichText);
		CHECK(Kopete::Message::RichText, msg.format());
	}
	{
		TQString m = "foobar";
		Kopete::Message msg( m_contactFrom, m_contactTo, m, Kopete::Message::Inbound, Kopete::Message::RichText);

		msg.setBody(m, Kopete::Message::PlainText);
		CHECK(Kopete::Message::PlainText, msg.format());

		msg.setBody(m, Kopete::Message::RichText);
		CHECK(Kopete::Message::RichText, msg.format());

		msg.setBody(m, Kopete::Message::ParsedHTML);
		CHECK(Kopete::Message::ParsedHTML, msg.format());

		msg.setBody(m, Kopete::Message::Crypted);
		CHECK(Kopete::Message::Crypted, msg.format());
	}


	/**********************************************
	 * setBody()
	 *********************************************/

	{
		TQString m = "foobar";
		Kopete::Message msg( m_contactFrom, m_contactTo, m, Kopete::Message::Inbound, Kopete::Message::RichText);

		msg.setBody("NEW", Kopete::Message::PlainText);
		CHECK(TQString("NEW"), msg.plainBody());

		msg.setBody("NEW_NEW", Kopete::Message::RichText);
		CHECK(TQString("NEW_NEW"), msg.plainBody());
	}
	{
		TQString m = "foobar";
		Kopete::Message msg( m_contactFrom, m_contactTo, m, Kopete::Message::Inbound, Kopete::Message::PlainText);

		msg.setBody("NEW", Kopete::Message::PlainText);
		CHECK(TQString("NEW"), msg.plainBody());

		msg.setBody("NEW_NEW", Kopete::Message::RichText);
		CHECK(TQString("NEW_NEW"), msg.plainBody());
	}
	{
		TQString m = "<html><head></head><body foo=\"bar\">   <b>HELLO WORLD</b>   </body></html>";
		Kopete::Message msg( m_contactFrom, m_contactTo, m, Kopete::Message::Inbound, Kopete::Message::PlainText);
		CHECK(m, msg.plainBody());

		msg.setBody("<simple> SIMPLE", Kopete::Message::PlainText);
		CHECK(msg.plainBody(),   TQString("<simple> SIMPLE") );
		CHECK(msg.escapedBody(), TQString("&lt;simple&gt; SIMPLE") );

		msg.setBody("<simple>SIMPLE</simple>", Kopete::Message::RichText);
		CHECK(msg.plainBody(),   TQString("SIMPLE") );
		CHECK(msg.escapedBody(), TQString("<simple>SIMPLE</simple>") );

		CHECK(Kopete::Message::unescape( TQString( "<simple>SIMPLE</simple>" ) ), TQString("SIMPLE") );
		CHECK(Kopete::Message::unescape( TQString( "Foo <img src=\"foo.png\" />" ) ), TQString("Foo ") );
		CHECK(Kopete::Message::unescape( TQString( "Foo <img src=\"foo.png\" title=\"Bar\" />" ) ), TQString("Foo Bar") );

		msg.setBody(m, Kopete::Message::RichText);

		// FIXME: Should setBody() also strip extra white space?
		//CHECK(msg.plainBody(),   TQString("HELLO WORLD"));
		//CHECK(msg.escapedBody(), TQString("<b>HELLO WORLD</b>"));

		CHECK(msg.escapedBody(),                   TQString(" &nbsp; <b>HELLO WORLD</b> &nbsp; "));
		CHECK(msg.plainBody(),                     TQString("   HELLO WORLD   "));
		CHECK(msg.plainBody().stripWhiteSpace(),   TQString("HELLO WORLD"));
		CHECK(msg.escapedBody().stripWhiteSpace(), TQString("&nbsp; <b>HELLO WORLD</b> &nbsp;"));
	}
	{
		Kopete::Message msg( m_contactFrom, m_contactTo, "foo", Kopete::Message::Inbound, Kopete::Message::PlainText);

		msg.setBody("<p>foo", Kopete::Message::RichText);
		CHECK(msg.escapedBody(), TQString("foo"));

		msg.setBody("<p>foo</p>", Kopete::Message::RichText);
		CHECK(msg.escapedBody(), TQString("foo"));

		msg.setBody("\n<p>foo</p>\n<br/>", Kopete::Message::RichText);
		CHECK(msg.escapedBody(), TQString("foo<br/>"));
	}

	/**********************************************
	 * Copy constructor
	 *********************************************/

	{
		Kopete::Message msg1(m_contactFrom, m_contactTo, "foo", Kopete::Message::Inbound, Kopete::Message::RichText);
		Kopete::Message msg2(msg1);

		CHECK(msg1.plainBody(), msg2.plainBody());
		CHECK(msg1.escapedBody(), msg2.escapedBody());

		msg1.setBody("NEW", Kopete::Message::PlainText);
		CHECK(msg1.plainBody(), TQString("NEW"));
		CHECK(msg2.plainBody(), TQString("foo"));
	}

	/**********************************************
	 * operator=
	 *********************************************/

	{
		Kopete::Message msg1(m_contactFrom, m_contactTo, "foo", Kopete::Message::Inbound, Kopete::Message::RichText);
		{
			Kopete::Message msg2;

			CHECK(msg2.plainBody(), TQString());

			msg2 = msg1;

			CHECK(msg1.plainBody(), msg2.plainBody());
			CHECK(msg1.escapedBody(), msg2.escapedBody());

			msg1.setBody("NEW", Kopete::Message::PlainText);
			CHECK(msg1.plainBody(), TQString("NEW"));
			CHECK(msg2.plainBody(), TQString("foo"));
		}
		CHECK(msg1.plainBody(), TQString("NEW"));

		msg1 = msg1;
		CHECK(msg1.plainBody(), TQString("NEW"));
	}
}

void KopeteMessage_Test::setup()
{
}

void KopeteMessage_Test::testLinkParser()
{
	TQString basePath = TQString::fromLatin1( SRCDIR ) + TQString::fromLatin1("/link-parser-testcases");
	TQDir testCasesDir(basePath);
	
	TQStringList inputFileNames = testCasesDir.entryList("*.input");
	for ( TQStringList::ConstIterator it = inputFileNames.begin(); it != inputFileNames.end(); ++it)
	{
		TQString fileName = *it;
		TQString outputFileName = fileName;
		outputFileName.replace("input","output");
		// open the input file
		TQFile inputFile(basePath + TQString::fromLatin1("/") + fileName);
		TQFile expectedFile(basePath + TQString::fromLatin1("/") + outputFileName);
		// check if the expected output file exists
		// if it doesn't, skip the testcase
		if ( ! expectedFile.exists() )
		{
			SKIP("Warning! expected output for testcase "+ *it + " not found. Skiping testcase");
			continue;
		}
		if ( inputFile.open( IO_ReadOnly ) && expectedFile.open( IO_ReadOnly ))
		{
			TQTextStream inputStream(&inputFile);
			TQTextStream expectedStream(&expectedFile);
			TQString inputData;
			TQString expectedData;
			inputData = inputStream.read();
			expectedData = expectedStream.read();

			inputFile.close();
			expectedFile.close();

			// use a concrete url
			inputData.replace( "$URL","http://www.kde.org" );
			expectedData.replace( "$URL","http://www.kde.org" );

			// set message format for parsing according to textcase filename convention
			Kopete::Message::MessageFormat format;
			if ( fileName.section("-", 1, 1) == TQString::fromLatin1("plaintext") )
				format = Kopete::Message::PlainText;
			else
				format = Kopete::Message::RichText;		
	
			TQString result = Kopete::Message::parseLinks( inputData, format );

			// HACK to know the test case we applied, concatenate testcase name to both
			// input and expected string. WIll remove when I can add some sort of metadata
			// to a CHECK so debug its origin testcase
			result = fileName + TQString::fromLatin1(": ") + result;
			expectedData = fileName + TQString::fromLatin1(": ") + expectedData;
			// if the test case begins with broken, we expect it to fail, then use XFAIL
			// otherwise use CHECK
			if ( fileName.section("-", 0, 0) == TQString::fromLatin1("broken") )
			{
				//kdDebug() << "checking known-broken testcase: " << fileName << endl;
				XFAIL(result, expectedData);
			}
			else
			{
				//kdDebug() << "checking known-working testcase: " << fileName << endl;
				CHECK(result, expectedData);
			}
		}
		else
		{
			SKIP("Warning! can't open testcase files for "+ *it + ". Skiping testcase");
			continue;
		}
	}
}

// vim: set noet ts=4 sts=4 sw=4: