From 7fe127240ecebd163f65a5078f8dc927fe17e47b Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 11 Feb 2011 17:19:59 +0000
Subject: Adding more roster unit tests


diff --git a/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp b/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp
index 22a55d4..cabf2bf 100644
--- a/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp
+++ b/Swiften/Roster/UnitTest/XMPPRosterControllerTest.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Kevin Smith
+ * Copyright (c) 2010-2011 Kevin Smith
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -8,6 +8,7 @@
 #include <cppunit/extensions/TestFactoryRegistry.h>
 #include <boost/smart_ptr/make_shared.hpp>
 
+#include "Swiften/Roster/UnitTest/XMPPRosterSignalHandler.h"
 #include "Swiften/Roster/XMPPRosterController.h"
 #include "Swiften/Elements/Payload.h"
 #include "Swiften/Elements/RosterItemPayload.h"
@@ -24,6 +25,7 @@ class XMPPRosterControllerTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST(testAdd);
 		CPPUNIT_TEST(testModify);
 		CPPUNIT_TEST(testRemove);
+		CPPUNIT_TEST(testMany);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -31,9 +33,14 @@ class XMPPRosterControllerTest : public CppUnit::TestFixture {
 			channel_ = new DummyStanzaChannel();
 			router_ = new IQRouter(channel_);
 			xmppRoster_ = new XMPPRosterImpl();
+			handler_ = new XMPPRosterSignalHandler(xmppRoster_);
+			jid1_ = JID("foo@bar.com");
+			jid2_ = JID("alice@wonderland.lit");
+			jid3_ = JID("jane@austen.lit");
 		}
 
 		void tearDown() {
+			delete handler_;
 			delete xmppRoster_;
 			delete router_;
 			delete channel_;
@@ -51,42 +58,128 @@ class XMPPRosterControllerTest : public CppUnit::TestFixture {
 			XMPPRosterController controller(router_, xmppRoster_);
 
 			boost::shared_ptr<RosterPayload> payload(new RosterPayload());
-			payload->addItem(RosterItemPayload(JID("foo@bar.com"), "Bob", RosterItemPayload::Both));
+			payload->addItem(RosterItemPayload(jid1_, "Bob", RosterItemPayload::Both));
 			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "eou", payload));
 
-			CPPUNIT_ASSERT(xmppRoster_->containsJID(JID("foo@bar.com")));
-			CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com")));
+			CPPUNIT_ASSERT_EQUAL(Add, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), xmppRoster_->getGroupsForJID(jid1_).size());
+			CPPUNIT_ASSERT(xmppRoster_->containsJID(jid1_));
+			CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(jid1_));
 		}
 
 		void testModify() {
 			XMPPRosterController controller(router_, xmppRoster_);
 			boost::shared_ptr<RosterPayload> payload1(new RosterPayload());
-			payload1->addItem(RosterItemPayload(JID("foo@bar"), "Bob", RosterItemPayload::Both));
+			payload1->addItem(RosterItemPayload(jid1_, "Bob", RosterItemPayload::Both));
 			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id1", payload1));
 
+			CPPUNIT_ASSERT_EQUAL(Add, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+			handler_->reset();
+
 			boost::shared_ptr<RosterPayload> payload2(new RosterPayload());
-			payload2->addItem(RosterItemPayload(JID("foo@bar"), "Bob2", RosterItemPayload::Both));
+			payload2->addItem(RosterItemPayload(jid1_, "Bob2", RosterItemPayload::Both));
 			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id2", payload2));
 
-			CPPUNIT_ASSERT_EQUAL(String("Bob2"), xmppRoster_->getNameForJID(JID("foo@bar")));
+			CPPUNIT_ASSERT_EQUAL(Update, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+
+			CPPUNIT_ASSERT_EQUAL(String("Bob2"), xmppRoster_->getNameForJID(jid1_));
 		}
 
 		void testRemove() {
 			XMPPRosterController controller(router_, xmppRoster_);
 			boost::shared_ptr<RosterPayload> payload1(new RosterPayload());
-			payload1->addItem(RosterItemPayload(JID("foo@bar"), "Bob", RosterItemPayload::Both));
+			payload1->addItem(RosterItemPayload(jid1_, "Bob", RosterItemPayload::Both));
 			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id1", payload1));
 
+			CPPUNIT_ASSERT_EQUAL(Add, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+			handler_->reset();
+
 			boost::shared_ptr<RosterPayload> payload2(new RosterPayload());
-			payload2->addItem(RosterItemPayload(JID("foo@bar"), "Bob", RosterItemPayload::Remove));
+			payload2->addItem(RosterItemPayload(jid1_, "Bob", RosterItemPayload::Remove));
 			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id2", payload2));
-			CPPUNIT_ASSERT(!xmppRoster_->containsJID(JID("foo@bar")));
+			CPPUNIT_ASSERT(!xmppRoster_->containsJID(jid1_));
+			CPPUNIT_ASSERT_EQUAL(Remove, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+
+		}
+
+		void testMany() {
+			XMPPRosterController controller(router_, xmppRoster_);
+			boost::shared_ptr<RosterPayload> payload1(new RosterPayload());
+			payload1->addItem(RosterItemPayload(jid1_, "Bob", RosterItemPayload::Both));
+			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id1", payload1));
+
+			CPPUNIT_ASSERT_EQUAL(Add, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+			handler_->reset();
+
+			boost::shared_ptr<RosterPayload> payload2(new RosterPayload());
+			payload2->addItem(RosterItemPayload(jid2_, "Alice", RosterItemPayload::Both));
+			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id2", payload2));
+
+			CPPUNIT_ASSERT_EQUAL(Add, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid2_, handler_->getLastJID());
+			handler_->reset();
+
+			boost::shared_ptr<RosterPayload> payload3(new RosterPayload());
+			payload3->addItem(RosterItemPayload(jid1_, "Ernie", RosterItemPayload::Both));
+			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id3", payload3));
+
+			CPPUNIT_ASSERT_EQUAL(Update, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+			handler_->reset();
+
+			boost::shared_ptr<RosterPayload> payload4(new RosterPayload());
+			RosterItemPayload item(jid3_, "Jane", RosterItemPayload::Both);
+			String janesGroup("Jane's Group");
+			item.addGroup(janesGroup);
+			payload4->addItem(item);
+			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id4", payload4));
+
+			CPPUNIT_ASSERT_EQUAL(Add, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid3_, handler_->getLastJID());
+			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), xmppRoster_->getGroupsForJID(jid3_).size());
+			CPPUNIT_ASSERT_EQUAL(janesGroup, xmppRoster_->getGroupsForJID(jid3_)[0]);
+			handler_->reset();
+
+			boost::shared_ptr<RosterPayload> payload5(new RosterPayload());
+			payload5->addItem(RosterItemPayload(jid1_, "Bob", RosterItemPayload::Remove));
+			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id5", payload5));
+			CPPUNIT_ASSERT(!xmppRoster_->containsJID(jid1_));
+			CPPUNIT_ASSERT_EQUAL(Remove, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid1_, handler_->getLastJID());
+			handler_->reset();
+
+			boost::shared_ptr<RosterPayload> payload6(new RosterPayload());
+			RosterItemPayload item2(jid2_, "Little Alice", RosterItemPayload::Both);
+			String alicesGroup("Alice's Group");
+			item2.addGroup(alicesGroup);
+			payload6->addItem(item2);
+			channel_->onIQReceived(IQ::createRequest(IQ::Set, JID(), "id6", payload6));
+			CPPUNIT_ASSERT_EQUAL(Update, handler_->getLastEvent());
+			CPPUNIT_ASSERT_EQUAL(jid2_, handler_->getLastJID());
+			CPPUNIT_ASSERT_EQUAL(String("Little Alice"), xmppRoster_->getNameForJID(jid2_));
+			CPPUNIT_ASSERT_EQUAL(String("Jane"), xmppRoster_->getNameForJID(jid3_));
+			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), xmppRoster_->getGroupsForJID(jid2_).size());
+			CPPUNIT_ASSERT_EQUAL(alicesGroup, xmppRoster_->getGroupsForJID(jid2_)[0]);
+			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), xmppRoster_->getGroupsForJID(jid3_).size());
+			CPPUNIT_ASSERT_EQUAL(janesGroup, xmppRoster_->getGroupsForJID(jid3_)[0]);
+			handler_->reset();
+
 		}
 
 	private:
 		DummyStanzaChannel* channel_;
 		IQRouter* router_;
 		XMPPRosterImpl* xmppRoster_;
+		XMPPRosterSignalHandler* handler_;
+		JID jid1_;
+		JID jid2_;
+		JID jid3_;
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(XMPPRosterControllerTest);
diff --git a/Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp b/Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp
index 5f0c9fd..77993ea 100644
--- a/Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp
+++ b/Swiften/Roster/UnitTest/XMPPRosterImplTest.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2011 Remko Tronçon
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -9,69 +9,13 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/bind.hpp>
 
-#include <vector>
-
+#include "Swiften/Roster/UnitTest/XMPPRosterSignalHandler.h"
 #include "Swiften/Roster/XMPPRosterImpl.h"
 
 
 using namespace Swift;
 
-enum XMPPRosterEvents {None, Add, Remove, Update};
-
-class XMPPRosterSignalHandler {
-public:
-	XMPPRosterSignalHandler(XMPPRoster* roster) {
-		lastEvent_ = None;
-		roster->onJIDAdded.connect(boost::bind(&XMPPRosterSignalHandler::handleJIDAdded, this, _1));
-		roster->onJIDRemoved.connect(boost::bind(&XMPPRosterSignalHandler::handleJIDRemoved, this, _1));
-		roster->onJIDUpdated.connect(boost::bind(&XMPPRosterSignalHandler::handleJIDUpdated, this, _1, _2, _3));
-	}
-
-	XMPPRosterEvents getLastEvent() {
-		return lastEvent_;
-	}
-
-	JID getLastJID() {
-		return lastJID_;
-	}
-
-	String getLastOldName() {
-		return lastOldName_;
-	}
-
-	std::vector<String> getLastOldGroups() {
-		return lastOldGroups_;
-	}
 
-	void reset() {
-		lastEvent_ = None;
-	}
-
-private:
-	void handleJIDAdded(const JID& jid) {
-		lastJID_ = jid;
-		lastEvent_ = Add;
-	}
-
-	void handleJIDRemoved(const JID& jid) {
-		lastJID_ = jid;
-		lastEvent_ = Remove;
-	}
-
-	void handleJIDUpdated(const JID& jid, const String& oldName, const std::vector<String>& oldGroups) {
-		CPPUNIT_ASSERT_EQUAL(None, lastEvent_);
-		lastJID_ = jid;
-		lastOldName_ = oldName;
-		lastOldGroups_ = oldGroups;
-		lastEvent_ = Update;
-	}
-
-	XMPPRosterEvents lastEvent_;
-	JID lastJID_;
-	String lastOldName_;
-	std::vector<String> lastOldGroups_;
-
-};
 
 class XMPPRosterImplTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST_SUITE(XMPPRosterImplTest);
diff --git a/Swiften/Roster/UnitTest/XMPPRosterSignalHandler.h b/Swiften/Roster/UnitTest/XMPPRosterSignalHandler.h
new file mode 100644
index 0000000..5e15e9f
--- /dev/null
+++ b/Swiften/Roster/UnitTest/XMPPRosterSignalHandler.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2010-2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+#pragma once
+#include <boost/shared_ptr.hpp>
+#include <boost/bind.hpp>
+
+#include <vector>
+
+
+#include "Swiften/Roster/XMPPRosterImpl.h"
+
+using namespace Swift;
+
+
+enum XMPPRosterEvents {None, Add, Remove, Update};
+
+class XMPPRosterSignalHandler {
+public:
+	XMPPRosterSignalHandler(XMPPRoster* roster) {
+		lastEvent_ = None;
+		roster->onJIDAdded.connect(boost::bind(&XMPPRosterSignalHandler::handleJIDAdded, this, _1));
+		roster->onJIDRemoved.connect(boost::bind(&XMPPRosterSignalHandler::handleJIDRemoved, this, _1));
+		roster->onJIDUpdated.connect(boost::bind(&XMPPRosterSignalHandler::handleJIDUpdated, this, _1, _2, _3));
+	}
+
+	XMPPRosterEvents getLastEvent() {
+		return lastEvent_;
+	}
+
+	JID getLastJID() {
+		return lastJID_;
+	}
+
+	String getLastOldName() {
+		return lastOldName_;
+	}
+
+	std::vector<String> getLastOldGroups() {
+		return lastOldGroups_;
+	}
+
+	void reset() {
+		lastEvent_ = None;
+	}
+
+private:
+	void handleJIDAdded(const JID& jid) {
+		lastJID_ = jid;
+		lastEvent_ = Add;
+	}
+
+	void handleJIDRemoved(const JID& jid) {
+		lastJID_ = jid;
+		lastEvent_ = Remove;
+	}
+
+	void handleJIDUpdated(const JID& jid, const String& oldName, const std::vector<String>& oldGroups) {
+		CPPUNIT_ASSERT_EQUAL(None, lastEvent_);
+		lastJID_ = jid;
+		lastOldName_ = oldName;
+		lastOldGroups_ = oldGroups;
+		lastEvent_ = Update;
+	}
+
+	XMPPRosterEvents lastEvent_;
+	JID lastJID_;
+	String lastOldName_;
+	std::vector<String> lastOldGroups_;
+
+};
-- 
cgit v0.10.2-6-g49f6