From 1c1491cbbe9fca509ddc292dbeaedbba7b6a2b14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Thu, 23 Jul 2009 08:54:10 +0200
Subject: Added more LinkLocalServiceBrowser tests.


diff --git a/Swiften/LinkLocal/LinkLocalService.h b/Swiften/LinkLocal/LinkLocalService.h
index 446feb8..6616303 100644
--- a/Swiften/LinkLocal/LinkLocalService.h
+++ b/Swiften/LinkLocal/LinkLocalService.h
@@ -2,49 +2,40 @@
 
 #include "Swiften/Base/String.h"
 #include "Swiften/Network/HostAddress.h"
+#include "Swiften/LinkLocal/LinkLocalServiceID.h"
 #include "Swiften/LinkLocal/LinkLocalServiceInfo.h"
 
 namespace Swift {
 	class LinkLocalService {
 		public:
-			LinkLocalService();
+			LinkLocalService(
+					const LinkLocalServiceID& id,
+					const DNSSDService::ResolveResult& info) :
+						id(id),
+						info(info) {}
 
-			const String& getName() const {
-				return name;
-			}
-
-			void setName(const String& n) {
-				name = n;
-			}
-
-			const String& getHostname() const {
-				return hostname;
+			const LinkLocalServiceID& getID() const {
+				return id;
 			}
 
-			void setHostname(const String& h) {
-				hostname = h;
+			const String& getName() const {
+				return id.getName();
 			}
 
-			const HostAddress& getAddress() const {
-				return address;
+			int getPort() const {
+				return info.port;
 			}
 
-			void setAddress(const HostAddress& a) {
-				address = a;
+			const String& getHostname() const {
+				return info.host;
 			}
 
 			const LinkLocalServiceInfo& getInfo() const {
-				return info;
-			}
-
-			void setInfo(const LinkLocalServiceInfo& i) {
-				info = i;
+				return info.info;
 			}
 
 		private:
-			String name;
-			String hostname;
-			LinkLocalServiceInfo info;
-			HostAddress address;
+			LinkLocalServiceID id;
+			DNSSDService::ResolveResult info;
 	};
 }
diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp b/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp
index 63cf075..fbc3022 100644
--- a/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp
+++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.cpp
@@ -15,13 +15,22 @@ LinkLocalServiceBrowser::LinkLocalServiceBrowser(boost::shared_ptr<DNSSDService>
 			boost::bind(&LinkLocalServiceBrowser::handleServiceResolved, this, _1, _2));
 }
 
+std::vector<LinkLocalService> LinkLocalServiceBrowser::getServices() const {
+	std::vector<LinkLocalService> result;
+	for (ServiceMap::const_iterator i = services.begin(); i != services.end(); ++i) {
+		result.push_back(LinkLocalService(i->first, i->second));
+	}
+	return result;
+}
+
 void LinkLocalServiceBrowser::handleServiceAdded(const LinkLocalServiceID& service) {
 	dnsSDService->startResolvingService(service);
 }
 
 void LinkLocalServiceBrowser::handleServiceRemoved(const LinkLocalServiceID& service) {
-	/*dnsSDService->stopResolvingService(service);
-	services.erase(service);*/
+	dnsSDService->stopResolvingService(service);
+	services.erase(service);
+	onServiceRemoved(service);
 }
 
 void LinkLocalServiceBrowser::handleServiceResolved(const LinkLocalServiceID& service, const DNSSDService::ResolveResult& result) {
@@ -30,6 +39,7 @@ void LinkLocalServiceBrowser::handleServiceResolved(const LinkLocalServiceID& se
 		onServiceAdded(service);
 	}
 	else {
+		r.first->second = result;
 		onServiceChanged(service);
 	}
 }
diff --git a/Swiften/LinkLocal/LinkLocalServiceBrowser.h b/Swiften/LinkLocal/LinkLocalServiceBrowser.h
index c0788e6..6107557 100644
--- a/Swiften/LinkLocal/LinkLocalServiceBrowser.h
+++ b/Swiften/LinkLocal/LinkLocalServiceBrowser.h
@@ -16,8 +16,11 @@ namespace Swift {
 		public:
 			LinkLocalServiceBrowser(boost::shared_ptr<DNSSDService> service);
 
+			std::vector<LinkLocalService> getServices() const;
+
 			boost::signal<void (const LinkLocalServiceID&)> onServiceAdded;
 			boost::signal<void (const LinkLocalServiceID&)> onServiceChanged;
+			boost::signal<void (const LinkLocalServiceID&)> onServiceRemoved;
 
 		private:
 			void handleServiceAdded(const LinkLocalServiceID&);
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
index 449a823..59a113d 100644
--- a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
@@ -18,6 +18,7 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST(testServiceAdded);
 		CPPUNIT_TEST(testServiceAdded_NoServiceInfo);
 		CPPUNIT_TEST(testServiceChanged);
+		CPPUNIT_TEST(testServiceRemoved);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -32,6 +33,10 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 		}
 
 		void tearDown() {
+			addedServices.clear();
+			removedServices.clear();
+			changedServices.clear();
+
 			delete testServiceInfo2;
 			delete testServiceInfo;
 			delete testServiceID;
@@ -47,7 +52,13 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
 			CPPUNIT_ASSERT(addedServices[0] == *testServiceID);
-			// TODO: Check getServices
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changedServices.size()));
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(removedServices.size()));
+			std::vector<LinkLocalService> services = testling->getServices();
+			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(services.size()));
+			CPPUNIT_ASSERT(*testServiceID == services[0].getID());
+			CPPUNIT_ASSERT(testServiceInfo->port == services[0].getPort());
+			CPPUNIT_ASSERT(testServiceInfo->host == services[0].getHostname());
 		}
 
 		void testServiceAdded_NoServiceInfo() {
@@ -57,6 +68,8 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 			eventLoop->processEvents();
 
 			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(addedServices.size()));
+			std::vector<LinkLocalService> services = testling->getServices();
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(services.size()));
 		}
 
 		void testServiceChanged() {
@@ -67,7 +80,35 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 
 			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo2);
 			eventLoop->processEvents();
-			// TODO: Check getServices
+
+			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
+			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changedServices.size()));
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(removedServices.size()));
+			CPPUNIT_ASSERT(changedServices[0] == *testServiceID);
+			std::vector<LinkLocalService> services = testling->getServices();
+			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(services.size()));
+			CPPUNIT_ASSERT(*testServiceID == services[0].getID());
+			CPPUNIT_ASSERT(testServiceInfo2->port == services[0].getPort());
+			CPPUNIT_ASSERT(testServiceInfo2->host == services[0].getHostname());
+		}
+
+		void testServiceRemoved() {
+			boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo);
+			dnsSDService->addService(*testServiceID);
+			eventLoop->processEvents();
+
+			dnsSDService->removeService(*testServiceID);
+			eventLoop->processEvents();
+			dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo2);
+			eventLoop->processEvents();
+
+			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changedServices.size()));
+			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(removedServices.size()));
+			CPPUNIT_ASSERT(removedServices[0] == *testServiceID);
+			std::vector<LinkLocalService> services = testling->getServices();
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(services.size()));
 		}
 
 	private:
@@ -76,6 +117,10 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 					new LinkLocalServiceBrowser(dnsSDService));
 			testling->onServiceAdded.connect(boost::bind(
 					&LinkLocalServiceBrowserTest::handleServiceAdded, this, _1));
+			testling->onServiceChanged.connect(boost::bind(
+					&LinkLocalServiceBrowserTest::handleServiceChanged, this, _1));
+			testling->onServiceRemoved.connect(boost::bind(
+					&LinkLocalServiceBrowserTest::handleServiceRemoved, this, _1));
 			return testling;
 		}
 
@@ -83,10 +128,20 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
 			addedServices.push_back(service);
 		}
 
+		void handleServiceRemoved(const LinkLocalServiceID& service) {
+			removedServices.push_back(service);
+		}
+
+		void handleServiceChanged(const LinkLocalServiceID& service) {
+			changedServices.push_back(service);
+		}
+
 	private:
 		DummyEventLoop* eventLoop;
 		boost::shared_ptr<MockDNSSDService> dnsSDService;
 		std::vector<LinkLocalServiceID> addedServices;
+		std::vector<LinkLocalServiceID> changedServices;
+		std::vector<LinkLocalServiceID> removedServices;
 		LinkLocalServiceID* testServiceID;
 		DNSSDService::ResolveResult* testServiceInfo;
 		DNSSDService::ResolveResult* testServiceInfo2;
diff --git a/Swiften/LinkLocal/UnitTest/MockDNSSDService.h b/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
index 5d857fb..56661ee 100644
--- a/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
+++ b/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
@@ -48,8 +48,16 @@ namespace Swift {
 				MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), id));
 			}
 
+			void removeService(const LinkLocalServiceID& id) {
+				serviceInfo.erase(id);
+				MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), id));
+			}
+
 			void setServiceInfo(const LinkLocalServiceID& id, const DNSSDService::ResolveResult& info) {
-				serviceInfo.insert(std::make_pair(id, info));
+				std::pair<ServiceInfoMap::iterator, bool> r = serviceInfo.insert(std::make_pair(id, info));
+				if (!r.second) {
+					r.first->second = info;
+				}
 				broadcastServiceInfo(id);
 			}
 
-- 
cgit v0.10.2-6-g49f6