Files @ 761c746d1008
Branch filter:

Location: libtransport.git/src/tests/conversationmanager.cpp - annotation

Jan Kaluza
Move headline sending login to backend
  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
0b0e020cdf91
1a11965611f7
1055c577d2d3
c1e478774f7a
eb1df41b3ea2
1a11965611f7
1055c577d2d3
1a11965611f7
4717bd007185
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
c1e478774f7a
eb1df41b3ea2
eb1df41b3ea2
8b6973539f23
8b6973539f23
eb1df41b3ea2
8b6973539f23
8b6973539f23
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
8b6973539f23
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
8b6973539f23
8b6973539f23
8b6973539f23
8b6973539f23
8b6973539f23
8b6973539f23
8b6973539f23
8b6973539f23
eb1df41b3ea2
8b6973539f23
8b6973539f23
8b6973539f23
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
8b6973539f23
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
eb1df41b3ea2
1a11965611f7
1a11965611f7
1a11965611f7
df3276119498
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
9469109d37e4
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
9469109d37e4
1a11965611f7
df3276119498
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
df3276119498
9469109d37e4
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
9469109d37e4
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
df3276119498
1a11965611f7
1a11965611f7
1a11965611f7
df3276119498
df3276119498
df3276119498
df3276119498
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
df3276119498
df3276119498
df3276119498
df3276119498
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
df3276119498
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
761c746d1008
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
761c746d1008
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
761c746d1008
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
761c746d1008
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
0b0e020cdf91
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
88607aff3c0b
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
88607aff3c0b
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1055c577d2d3
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
1a11965611f7
4717bd007185
4717bd007185
4717bd007185
4717bd007185
97a2cd5b2af5
4717bd007185
4717bd007185
4717bd007185
4717bd007185
4717bd007185
4717bd007185
4717bd007185
4717bd007185
4717bd007185
3ff7f9249074
4717bd007185
4717bd007185
1a11965611f7
1a11965611f7
1a11965611f7
#include "transport/userregistry.h"
#include "transport/config.h"
#include "transport/storagebackend.h"
#include "transport/user.h"
#include "transport/transport.h"
#include "transport/conversation.h"
#include "transport/usermanager.h"
#include "transport/conversationmanager.h"
#include "transport/localbuddy.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Swiften/Swiften.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Server/Server.h>
#include <Swiften/Network/DummyNetworkFactories.h>
#include <Swiften/Network/DummyConnectionServer.h>
#include "Swiften/Server/ServerStanzaChannel.h"
#include "Swiften/Server/ServerFromClientSession.h"
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
#include "basictest.h"

using namespace Transport;

class ConversationManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
	CPPUNIT_TEST_SUITE(ConversationManagerTest);
	CPPUNIT_TEST(handleNormalMessages);
	CPPUNIT_TEST(handleNormalMessagesHeadline);
	CPPUNIT_TEST(handleGroupchatMessages);
	CPPUNIT_TEST(handleGroupchatMessagesTwoResources);
	CPPUNIT_TEST(handleChatstateMessages);
	CPPUNIT_TEST(handleSubjectMessages);
	CPPUNIT_TEST(handleParticipantChanged);
	CPPUNIT_TEST(handleParticipantChangedTwoResources);
	CPPUNIT_TEST(handlePMFromXMPP);
	CPPUNIT_TEST(handleGroupchatRemoved);
	CPPUNIT_TEST_SUITE_END();

	public:
		TestingConversation *m_conv;
		boost::shared_ptr<Swift::Message> m_msg;

		void setUp (void) {
			m_conv = NULL;
			m_msg.reset();
			setMeUp();
			connectUser();
			add2Buddies();
			factory->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
			received.clear();
		}

		void tearDown (void) {
			received.clear();
			disconnectUser();
			factory->onMessageToSend.disconnect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
			tearMeDown();
		}

	void handleMessageReceived(TestingConversation *_conv, boost::shared_ptr<Swift::Message> &_msg) {
		m_conv = _conv;
		m_msg = _msg;
	}

	void handleChatstateMessages() {
		User *user = userManager->getUser("user@localhost");

		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "buddy1");
		user->getConversationManager()->addConversation(conv);
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));

		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->addPayload(boost::make_shared<Swift::ChatState>(Swift::ChatState::Composing));

		// Forward it
		conv->handleMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0]))->getPayload<Swift::ChatState>());
		received.clear();

		// send response
		msg->setFrom("user@localhost/resource");
		msg->setTo("buddy1@localhost/bot");
		injectMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
		CPPUNIT_ASSERT(m_msg);
		CPPUNIT_ASSERT(m_msg->getPayload<Swift::ChatState>());

		received.clear();
	}

	void handleSubjectMessages() {
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
		conv->setNickname("nickname");
		conv->addJID("user@localhost/resource");

		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->setSubject("subject");
		msg->setType(Swift::Message::Groupchat);

		conv->handleMessage(msg);
		loop->processEvents();

		// No response, because presence with code 110 has not been sent yet and we must not send
		// subject before this one.
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());

		// this user presence - status code 110
		conv->handleParticipantChanged("nickname", 1, Swift::StatusShow::Away, "my status message");
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[1])));
		CPPUNIT_ASSERT_EQUAL(std::string("subject"), dynamic_cast<Swift::Message *>(getStanza(received[1]))->getSubject());
		received.clear();

		// send response
		msg->setFrom("user@localhost/resource");
		msg->setTo("#room@localhost");
		injectMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
		CPPUNIT_ASSERT(m_msg);
		CPPUNIT_ASSERT_EQUAL(std::string("subject"), m_msg->getSubject());

		received.clear();
	}

	void handleNormalMessages() {
		User *user = userManager->getUser("user@localhost");

		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "buddy1@test");
		user->getConversationManager()->addConversation(conv);
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));

		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->setBody("hi there<>!");

		// Forward it
		conv->handleMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(std::string("hi there<>!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
		
		received.clear();

		// send response
		msg->setFrom("user@localhost/resource");
		msg->setTo("buddy1\\40test@localhost/bot");
		msg->setBody("response<>!");
		injectMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
		CPPUNIT_ASSERT(m_msg);
		CPPUNIT_ASSERT_EQUAL(std::string("response<>!"), m_msg->getBody());

		// send another message from legacy network, should be sent to user@localhost/resource now
		boost::shared_ptr<Swift::Message> msg2(new Swift::Message());
		msg2->setBody("hi there!");

		// Forward it
		conv->handleMessage(msg2);
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
		
		received.clear();

		// disable jid_escaping
		std::istringstream ifs("service.server_mode = 1\nservice.jid_escaping=0\nservice.jid=localhost\nservice.more_resources=1\n");
		cfg->load(ifs);

		// and now to bare JID again...
		user->getConversationManager()->resetResources();
		conv->handleMessage(msg2);
		loop->processEvents();

		// enable jid_escaping again
		std::istringstream ifs2("service.server_mode = 1\nservice.jid_escaping=1\nservice.jid=localhost\nservice.more_resources=1\n");
		cfg->load(ifs2);
		
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("buddy1%test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
		
		received.clear();
	}

	void handleNormalMessagesHeadline() {
		User *user = userManager->getUser("user@localhost");
		user->addUserSetting("send_headlines", "1");

		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "buddy1@test");
		user->getConversationManager()->addConversation(conv);
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));

		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->setBody("hi there<>!");
		msg->setType(Swift::Message::Headline);

		// Forward it
		conv->handleMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::Message::Headline, dynamic_cast<Swift::Message *>(getStanza(received[0]))->getType());
		CPPUNIT_ASSERT_EQUAL(std::string("hi there<>!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());

		received.clear();
		user->addUserSetting("send_headlines", "0");
		// Forward it
		conv->handleMessage(msg);
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::Message::Chat, dynamic_cast<Swift::Message *>(getStanza(received[0]))->getType());
		CPPUNIT_ASSERT_EQUAL(std::string("hi there<>!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());

		received.clear();
		msg->setType(Swift::Message::Chat);
		user->addUserSetting("send_headlines", "1");
		// Forward it
		conv->handleMessage(msg);
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::Message::Chat, dynamic_cast<Swift::Message *>(getStanza(received[0]))->getType());
		CPPUNIT_ASSERT_EQUAL(std::string("hi there<>!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("buddy1\\40test@localhost/bot"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());
	}

	void handleGroupchatMessages() {
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		user->getConversationManager()->addConversation(conv);
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
		conv->setNickname("nickname");
		conv->addJID("user@localhost/resource");

		// reset resources should not touch this resource
		user->getConversationManager()->resetResources();

		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->setBody("hi there!");

		// Forward it
		conv->handleMessage(msg, "anotheruser");

		loop->processEvents();
		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received[0]))->getFrom().toString());

		received.clear();

		// send response
		msg->setFrom("user@localhost/resource");
		msg->setTo("#room@localhost");
		msg->setBody("response!");
		msg->setType(Swift::Message::Groupchat);
		injectMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
		CPPUNIT_ASSERT(m_msg);
		CPPUNIT_ASSERT_EQUAL(std::string("response!"), m_msg->getBody());
	}

	void handleGroupchatMessagesTwoResources() {
		connectSecondResource();
		received2.clear();
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		user->getConversationManager()->addConversation(conv);
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
		conv->setNickname("nickname");
		conv->addJID("user@localhost/resource");
		conv->addJID("user@localhost/resource2");

		// reset resources should not touch this resource
		user->getConversationManager()->resetResources();

		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->setBody("hi there!");

		// Forward it
		conv->handleMessage(msg, "anotheruser");

		loop->processEvents();
		CPPUNIT_ASSERT_EQUAL(1, (int) received2.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Message *>(getStanza(received2[0])));
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), dynamic_cast<Swift::Message *>(getStanza(received2[0]))->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource2"), dynamic_cast<Swift::Message *>(getStanza(received2[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Message *>(getStanza(received2[0]))->getFrom().toString());

		received.clear();

		// send response
		msg->setFrom("user@localhost/resource2");
		msg->setTo("#room@localhost");
		msg->setBody("response!");
		msg->setType(Swift::Message::Groupchat);
		injectMessage(msg);
		loop->processEvents();
		
		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
		CPPUNIT_ASSERT(m_msg);
		CPPUNIT_ASSERT_EQUAL(std::string("response!"), m_msg->getBody());
	}

	void handleParticipantChanged() {
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
		conv->setNickname("nickname");
		conv->addJID("user@localhost/resource");

		// normal presence
		conv->handleParticipantChanged("anotheruser", 0, Swift::StatusShow::Away, "my status message");
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getShow());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getFrom().toString());
		CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::MUCUserPayload>());
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Member, *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].affiliation);
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Participant, *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].role);

		received.clear();

		// this user presence - status code 110
		conv->handleParticipantChanged("nickname", 1, Swift::StatusShow::Away, "my status message");
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getShow());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/nickname"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getFrom().toString());
		CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::MUCUserPayload>());
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Admin, *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].affiliation);
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Moderator, *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].role);
		CPPUNIT_ASSERT_EQUAL(110, getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getStatusCodes()[0].code);

		received.clear();

		// renamed - status code 303
		conv->handleParticipantChanged("anotheruser", 1, Swift::StatusShow::Away, "my status message", "hanzz");
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::Presence::Unavailable, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getFrom().toString());
		CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::MUCUserPayload>());
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Admin, *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].affiliation);
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Moderator, *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].role);
		CPPUNIT_ASSERT_EQUAL(std::string("hanzz"), *getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].nick);
		CPPUNIT_ASSERT_EQUAL(303, getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getStatusCodes()[0].code);
	}

	void handleParticipantChangedTwoResources() {
		connectSecondResource();
		received2.clear();
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
		conv->setNickname("nickname");
		conv->addJID("user@localhost/resource");
		conv->addJID("user@localhost/resource2");

		// normal presence
		conv->handleParticipantChanged("anotheruser", 0, Swift::StatusShow::Away, "my status message");
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(1, (int) received2.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received2[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received2[0]))->getShow());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource2"), dynamic_cast<Swift::Presence *>(getStanza(received2[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/anotheruser"), dynamic_cast<Swift::Presence *>(getStanza(received2[0]))->getFrom().toString());
		CPPUNIT_ASSERT(getStanza(received2[0])->getPayload<Swift::MUCUserPayload>());
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Member, *getStanza(received2[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].affiliation);
		CPPUNIT_ASSERT_EQUAL(Swift::MUCOccupant::Participant, *getStanza(received2[0])->getPayload<Swift::MUCUserPayload>()->getItems()[0].role);
	}

	void handlePMFromXMPP() {
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		user->getConversationManager()->addConversation(conv);
		conv->onMessageToSend.connect(boost::bind(&ConversationManagerTest::handleMessageReceived, this, _1, _2));
		conv->setNickname("nickname");
		conv->setJID("user@localhost/resource");

		conv->handleParticipantChanged("anotheruser", 0, Swift::StatusShow::Away, "my status message");
		loop->processEvents();

		received.clear();
		boost::shared_ptr<Swift::Message> msg(new Swift::Message());
		msg->setBody("hi there!");
		msg->setFrom("user@localhost/resource");
		msg->setTo("#room@localhost/anotheruser");
		msg->setBody("hi there!");
		injectMessage(msg);
		loop->processEvents();

		CPPUNIT_ASSERT_EQUAL(0, (int) received.size());
		CPPUNIT_ASSERT(m_msg);
		CPPUNIT_ASSERT_EQUAL(std::string("hi there!"), m_msg->getBody());
		CPPUNIT_ASSERT_EQUAL(std::string("#room/anotheruser"), m_conv->getLegacyName());

		Conversation *pmconv = user->getConversationManager()->getConversation("#room/anotheruser");

		boost::shared_ptr<Swift::Message> msg2(new Swift::Message());
		msg2->setBody("response!");

		pmconv->handleMessage(msg2);
	}

	void handleGroupchatRemoved() {
		User *user = userManager->getUser("user@localhost");
		TestingConversation *conv = new TestingConversation(user->getConversationManager(), "#room", true);
		conv->setNickname("nickname");
		conv->addJID("user@localhost/resource");
		received.clear();
		conv->destroyRoom();
		delete conv;

		CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
		CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
		CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::None, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getShow());
		CPPUNIT_ASSERT_EQUAL(std::string("user@localhost/resource"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getTo().toString());
		CPPUNIT_ASSERT_EQUAL(std::string("#room@localhost/nickname"), dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getFrom().toString());
		CPPUNIT_ASSERT_EQUAL(332, getStanza(received[0])->getPayload<Swift::MUCUserPayload>()->getStatusCodes()[0].code);
	}

};

CPPUNIT_TEST_SUITE_REGISTRATION (ConversationManagerTest);