/* * Copyright (c) 2010 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include #include #include #include #include #include #include using namespace Swift; class GetPrivateStorageRequestTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(GetPrivateStorageRequestTest); CPPUNIT_TEST(testSend); CPPUNIT_TEST(testHandleResponse); CPPUNIT_TEST(testHandleResponse_Error); CPPUNIT_TEST_SUITE_END(); public: class MyPayload : public Payload { public: MyPayload(const std::string& text = "") : text(text) {} std::string text; }; public: void setUp() { channel = new DummyIQChannel(); router = new IQRouter(channel); } void tearDown() { delete router; delete channel; } void testSend() { GetPrivateStorageRequest::ref request = GetPrivateStorageRequest::create(router); request->send(); CPPUNIT_ASSERT_EQUAL(1, static_cast(channel->iqs_.size())); CPPUNIT_ASSERT_EQUAL(JID(), channel->iqs_[0]->getTo()); CPPUNIT_ASSERT_EQUAL(IQ::Get, channel->iqs_[0]->getType()); boost::shared_ptr storage = channel->iqs_[0]->getPayload(); CPPUNIT_ASSERT(storage); boost::shared_ptr payload = boost::dynamic_pointer_cast(storage->getPayload()); CPPUNIT_ASSERT(payload); } void testHandleResponse() { GetPrivateStorageRequest::ref testling = GetPrivateStorageRequest::create(router); testling->onResponse.connect(boost::bind(&GetPrivateStorageRequestTest::handleResponse, this, _1, _2)); testling->send(); channel->onIQReceived(createResponse("test-id", "foo")); CPPUNIT_ASSERT_EQUAL(1, static_cast(responses.size())); CPPUNIT_ASSERT_EQUAL(std::string("foo"), boost::dynamic_pointer_cast(responses[0])->text); } void testHandleResponse_Error() { GetPrivateStorageRequest::ref testling = GetPrivateStorageRequest::create(router); testling->onResponse.connect(boost::bind(&GetPrivateStorageRequestTest::handleResponse, this, _1, _2)); testling->send(); channel->onIQReceived(createError("test-id")); CPPUNIT_ASSERT_EQUAL(0, static_cast(responses.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast(errors.size())); } private: void handleResponse(boost::shared_ptr p, ErrorPayload::ref e) { if (e) { errors.push_back(*e); } else { responses.push_back(p); } } boost::shared_ptr createResponse(const std::string& id, const std::string& text) { boost::shared_ptr iq(new IQ(IQ::Result)); boost::shared_ptr storage(new PrivateStorage()); storage->setPayload(boost::shared_ptr(new MyPayload(text))); iq->addPayload(storage); iq->setID(id); return iq; } boost::shared_ptr createError(const std::string& id) { boost::shared_ptr iq(new IQ(IQ::Error)); iq->setID(id); return iq; } private: IQRouter* router; DummyIQChannel* channel; std::vector< ErrorPayload > errors; std::vector< boost::shared_ptr > responses; }; CPPUNIT_TEST_SUITE_REGISTRATION(GetPrivateStorageRequestTest);