From 6dcca70b2f9ed13817e3600d96dbc9af3b597241 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Mon, 12 Apr 2010 09:50:20 +0000
Subject: RosterController testing.


diff --git a/Swift/Controllers/UnitTest/RosterControllerTest.cpp b/Swift/Controllers/UnitTest/RosterControllerTest.cpp
index e4d6775..1177bd7 100644
--- a/Swift/Controllers/UnitTest/RosterControllerTest.cpp
+++ b/Swift/Controllers/UnitTest/RosterControllerTest.cpp
@@ -29,6 +29,8 @@ class RosterControllerTest : public CppUnit::TestFixture
 {
 		CPPUNIT_TEST_SUITE(RosterControllerTest);
 		CPPUNIT_TEST(testAdd);
+		CPPUNIT_TEST(testAddSubscription);
+		CPPUNIT_TEST(testRename);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -55,7 +57,6 @@ class RosterControllerTest : public CppUnit::TestFixture
 		void tearDown() {
 			delete rosterController_;
 			delete nickResolver_;
-			delete treeWidgetFactory_;
 			delete mainWindowFactory_;
 			delete avatarManager_;
 			delete channel_;
@@ -64,6 +65,7 @@ class RosterControllerTest : public CppUnit::TestFixture
 			delete presenceOracle_;
 			delete stanzaChannel_;
 			delete uiEventStream_;
+			delete treeWidgetFactory_;
 		};
 
 		void testAdd() {
@@ -76,6 +78,36 @@ class RosterControllerTest : public CppUnit::TestFixture
 			//CPPUNIT_ASSERT_EQUAL(String("Bob"), xmppRoster_->getNameForJID(JID("foo@bar.com")));
 		};
 
+ 		void testAddSubscription() {
+			std::vector<String> groups;
+			JID jid("test@testdomain.com");
+			xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::None);
+			
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroups().size());
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroupMembers("Contacts").size());
+			xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::To);
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroups().size());
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroupMembers("Contacts").size());
+
+			xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both);
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroups().size());
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroupMembers("Contacts").size());
+
+		};
+
+		void testRename() {
+			std::vector<String> groups;
+			JID jid("test@testdomain.com");
+			xmppRoster_->addContact(jid, "name", groups, RosterItemPayload::Both);
+			
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroups().size());
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroupMembers("Contacts").size());
+			xmppRoster_->addContact(jid, "NewName", groups, RosterItemPayload::Both);
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroups().size());
+			CPPUNIT_ASSERT_EQUAL(1, (int)treeWidgetFactory_->getGroupMembers("Contacts").size());
+			CPPUNIT_ASSERT_EQUAL(String("NewName"), treeWidgetFactory_->getGroupMembers("Contacts")[0]->getText());
+		};
+
 	private:
 		JID jid_;
 		boost::shared_ptr<XMPPRoster> xmppRoster_;
@@ -91,3 +123,5 @@ class RosterControllerTest : public CppUnit::TestFixture
 		EventController* eventController_;
 		UIEventStream* uiEventStream_;
 };
+
+CPPUNIT_TEST_SUITE_REGISTRATION(RosterControllerTest);
diff --git a/Swiften/Roster/UnitTest/MockTreeWidget.h b/Swiften/Roster/UnitTest/MockTreeWidget.h
index b766aeb..97dd796 100644
--- a/Swiften/Roster/UnitTest/MockTreeWidget.h
+++ b/Swiften/Roster/UnitTest/MockTreeWidget.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010 Kevin Smith
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
diff --git a/Swiften/Roster/UnitTest/MockTreeWidgetFactory.h b/Swiften/Roster/UnitTest/MockTreeWidgetFactory.h
index 935423c..d94c859 100644
--- a/Swiften/Roster/UnitTest/MockTreeWidgetFactory.h
+++ b/Swiften/Roster/UnitTest/MockTreeWidgetFactory.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010 Kevin Smith
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -27,12 +27,12 @@ class MockTreeWidgetFactory : public TreeWidgetFactory {
 			return root_;
 		};
 		virtual TreeWidgetItem* createTreeWidgetItem(TreeWidgetItem* group) {
-			MockTreeWidgetItem* entry = new MockTreeWidgetItem();
+			MockTreeWidgetItem* entry = new MockTreeWidgetItem(this);
 			groupMembers_[group].push_back(entry);
 			return entry;
 		};
 		virtual TreeWidgetItem* createTreeWidgetItem(TreeWidget*) {
-			MockTreeWidgetItem* group = new MockTreeWidgetItem();
+			MockTreeWidgetItem* group = new MockTreeWidgetItem(this);
 			groups_.push_back(group);
 			return group;
 		};
@@ -43,6 +43,24 @@ class MockTreeWidgetFactory : public TreeWidgetFactory {
 			}
 			return groupNames;
 		};
+
+		typedef std::map<TreeWidgetItem*, std::vector<MockTreeWidgetItem*> > itemMap;
+
+		virtual std::vector<MockTreeWidgetItem*> getGroupMembers(const String& group) {
+			for (itemMap::iterator it = groupMembers_.begin(); it != groupMembers_.end(); it++) {
+				if (((MockTreeWidgetItem*)(it->first))->getText() == group) {
+					return it->second;
+				}
+			}
+			return std::vector<MockTreeWidgetItem*>();
+		};
+
+		virtual void removeItem(MockTreeWidgetItem* item) {
+			foreach (TreeWidgetItem* groupItem, groups_) {
+				std::vector<MockTreeWidgetItem*>& members = groupMembers_[groupItem];
+				members.erase(std::remove(members.begin(), members.end(), item), members.end());
+			}
+		};
 	private:
 		std::vector<MockTreeWidgetItem*> groups_;
 		std::map<TreeWidgetItem*, std::vector<MockTreeWidgetItem*> > groupMembers_;
diff --git a/Swiften/Roster/UnitTest/MockTreeWidgetItem.cpp b/Swiften/Roster/UnitTest/MockTreeWidgetItem.cpp
new file mode 100644
index 0000000..4238ead
--- /dev/null
+++ b/Swiften/Roster/UnitTest/MockTreeWidgetItem.cpp
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+#include "Swiften/Roster/UnitTest/MockTreeWidgetItem.h"
+
+#include "Swiften/Roster/UnitTest/MockTreeWidgetFactory.h"
+
+namespace Swift {
+
+MockTreeWidgetItem::~MockTreeWidgetItem() {
+	factory_->removeItem(this);
+}
+
+}
+
+
+
diff --git a/Swiften/Roster/UnitTest/MockTreeWidgetItem.h b/Swiften/Roster/UnitTest/MockTreeWidgetItem.h
index 1ec1441..08b9f1a 100644
--- a/Swiften/Roster/UnitTest/MockTreeWidgetItem.h
+++ b/Swiften/Roster/UnitTest/MockTreeWidgetItem.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010 Kevin Smith
  * Licensed under the GNU General Public License v3.
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
@@ -14,10 +14,11 @@
 #include <boost/shared_ptr.hpp>
 
 namespace Swift {
-
+	class MockTreeWidgetFactory;
 class MockTreeWidgetItem : public TreeWidgetItem {
 	public:
-		virtual ~MockTreeWidgetItem() {};
+		MockTreeWidgetItem(MockTreeWidgetFactory* factory) {factory_ = factory;};
+		virtual ~MockTreeWidgetItem();
 		virtual void setText(const String& text) {text_ = text;};
 		String getText() {return text_;};
 		virtual void setStatusText(const String&) {};
@@ -28,6 +29,7 @@ class MockTreeWidgetItem : public TreeWidgetItem {
 		virtual void hide() {};
 	private:
 		String text_;
+		MockTreeWidgetFactory* factory_;
 };
 
 }
diff --git a/Swiften/SConscript b/Swiften/SConscript
index 82c75d5..edc1885 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -189,6 +189,7 @@ if env["SCONS_STAGE"] == "build" :
 			File("Roster/UnitTest/OfflineRosterFilterTest.cpp"),
 			File("Roster/UnitTest/RosterTest.cpp"),
 			File("Roster/UnitTest/XMPPRosterTest.cpp"),
+			File("Roster/UnitTest/MockTreeWidgetItem.cpp"),
 			File("SASL/UnitTest/PLAINMessageTest.cpp"),
 			File("SASL/UnitTest/PLAINClientAuthenticatorTest.cpp"),
 			File("SASL/UnitTest/SCRAMSHA1ClientAuthenticatorTest.cpp"),
-- 
cgit v0.10.2-6-g49f6