summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-23 06:54:10 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-23 07:21:45 (GMT)
commit1c1491cbbe9fca509ddc292dbeaedbba7b6a2b14 (patch)
tree6831422eb07d4a6f35ad09f69a5b9f8df7ae103b /Swiften
parent0a7336577f9609c70bb4d846c668e7853746cb52 (diff)
downloadswift-1c1491cbbe9fca509ddc292dbeaedbba7b6a2b14.zip
swift-1c1491cbbe9fca509ddc292dbeaedbba7b6a2b14.tar.bz2
Added more LinkLocalServiceBrowser tests.
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/LinkLocal/LinkLocalService.h43
-rw-r--r--Swiften/LinkLocal/LinkLocalServiceBrowser.cpp14
-rw-r--r--Swiften/LinkLocal/LinkLocalServiceBrowser.h3
-rw-r--r--Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp59
-rw-r--r--Swiften/LinkLocal/UnitTest/MockDNSSDService.h10
5 files changed, 98 insertions, 31 deletions
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);
}