/* * Copyright (c) 2012 Mateusz Piękos * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include #include #include #include using namespace Swift; class WhiteboardClientTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(WhiteboardClientTest); CPPUNIT_TEST(testNoninterrupedSynchronization); CPPUNIT_TEST(testInterrupedSynchronization); CPPUNIT_TEST_SUITE_END(); public: void testNoninterrupedSynchronization() { WhiteboardClient client; WhiteboardInsertOperation::ref serverOp; serverOp = createInsertOperation("0", ""); serverOp->setPos(0); std::pair pairResult = client.handleServerOperationReceived(serverOp); CPPUNIT_ASSERT_EQUAL(serverOp, boost::dynamic_pointer_cast(pairResult.first)); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); WhiteboardInsertOperation::ref clientOp; clientOp = createInsertOperation("a", "0"); clientOp->setPos(1); clientOp->setOrigin(WhiteboardOperation::Local); WhiteboardEllipseElement::ref aElement = boost::make_shared(0,0,0,0); clientOp->setElement(aElement); WhiteboardInsertOperation::ref result = boost::dynamic_pointer_cast(client.handleLocalOperationReceived(clientOp)); CPPUNIT_ASSERT_EQUAL(clientOp, boost::dynamic_pointer_cast(result)); CPPUNIT_ASSERT_EQUAL(aElement, boost::dynamic_pointer_cast(result->getElement())); clientOp = createInsertOperation("b", "a"); clientOp->setPos(2); clientOp->setOrigin(WhiteboardOperation::Local); WhiteboardEllipseElement::ref bElement = boost::make_shared(0,0,0,0); clientOp->setElement(bElement); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), client.handleLocalOperationReceived(clientOp)); serverOp = createInsertOperation("c", "0"); serverOp->setPos(1); serverOp->setOrigin(WhiteboardOperation::Other); WhiteboardEllipseElement::ref cElement = boost::make_shared(0,0,0,0); serverOp->setElement(cElement); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.first); CPPUNIT_ASSERT_EQUAL(3, result->getPos()); CPPUNIT_ASSERT_EQUAL(cElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("b"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); serverOp = createInsertOperation("d", "c"); serverOp->setPos(2); serverOp->setOrigin(WhiteboardOperation::Other); WhiteboardEllipseElement::ref dElement = boost::make_shared(0,0,0,0); serverOp->setElement(dElement); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.first); CPPUNIT_ASSERT_EQUAL(4, result->getPos()); CPPUNIT_ASSERT_EQUAL(dElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("c"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); serverOp = createInsertOperation("a", "d"); serverOp->setPos(1); serverOp->setOrigin(WhiteboardOperation::Other); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.second); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.first); CPPUNIT_ASSERT_EQUAL(std::string("b"), result->getID()); CPPUNIT_ASSERT_EQUAL(bElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("a"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::Other, result->getOrigin()); serverOp = createInsertOperation("b", "a"); serverOp->setPos(2); serverOp->setOrigin(WhiteboardOperation::Other); pairResult = client.handleServerOperationReceived(serverOp); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.first); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); } void testInterrupedSynchronization() { WhiteboardClient client; WhiteboardInsertOperation::ref serverOp; serverOp = createInsertOperation("0", ""); serverOp->setPos(0); std::pair pairResult = client.handleServerOperationReceived(serverOp); CPPUNIT_ASSERT_EQUAL(serverOp, boost::dynamic_pointer_cast(pairResult.first)); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); WhiteboardInsertOperation::ref clientOp; clientOp = createInsertOperation("a", "0"); clientOp->setPos(1); clientOp->setOrigin(WhiteboardOperation::Local); WhiteboardEllipseElement::ref aElement = boost::make_shared(0,0,0,0); clientOp->setElement(aElement); WhiteboardInsertOperation::ref result = boost::dynamic_pointer_cast(client.handleLocalOperationReceived(clientOp)); CPPUNIT_ASSERT_EQUAL(clientOp, boost::dynamic_pointer_cast(result)); CPPUNIT_ASSERT_EQUAL(aElement, boost::dynamic_pointer_cast(result->getElement())); clientOp = createInsertOperation("b", "a"); clientOp->setPos(2); clientOp->setOrigin(WhiteboardOperation::Local); WhiteboardEllipseElement::ref bElement = boost::make_shared(0,0,0,0); clientOp->setElement(bElement); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), client.handleLocalOperationReceived(clientOp)); serverOp = createInsertOperation("c", "0"); serverOp->setPos(1); serverOp->setOrigin(WhiteboardOperation::Other); WhiteboardEllipseElement::ref cElement = boost::make_shared(0,0,0,0); serverOp->setElement(cElement); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.first); CPPUNIT_ASSERT_EQUAL(3, result->getPos()); CPPUNIT_ASSERT_EQUAL(cElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("b"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); clientOp = createInsertOperation("e", "a"); clientOp->setPos(4); clientOp->setOrigin(WhiteboardOperation::Local); WhiteboardEllipseElement::ref eElement = boost::make_shared(0,0,0,0); clientOp->setElement(eElement); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), client.handleLocalOperationReceived(clientOp)); serverOp = createInsertOperation("d", "c"); serverOp->setPos(2); serverOp->setOrigin(WhiteboardOperation::Other); WhiteboardEllipseElement::ref dElement = boost::make_shared(0,0,0,0); serverOp->setElement(dElement); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.first); CPPUNIT_ASSERT_EQUAL(5, result->getPos()); CPPUNIT_ASSERT_EQUAL(dElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("e"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); serverOp = createInsertOperation("a", "d"); serverOp->setPos(1); serverOp->setOrigin(WhiteboardOperation::Other); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.second); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.first); CPPUNIT_ASSERT_EQUAL(std::string("b"), result->getID()); CPPUNIT_ASSERT_EQUAL(bElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("a"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::Other, result->getOrigin()); serverOp = createInsertOperation("b", "a"); serverOp->setPos(2); serverOp->setOrigin(WhiteboardOperation::Other); pairResult = client.handleServerOperationReceived(serverOp); result = boost::dynamic_pointer_cast(pairResult.second); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.first); CPPUNIT_ASSERT_EQUAL(std::string("e"), result->getID()); CPPUNIT_ASSERT_EQUAL(eElement, boost::dynamic_pointer_cast(result->getElement())); CPPUNIT_ASSERT_EQUAL(std::string("b"), result->getParentID()); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::Other, result->getOrigin()); serverOp = createInsertOperation("e", "b"); serverOp->setPos(4); serverOp->setOrigin(WhiteboardOperation::Other); pairResult = client.handleServerOperationReceived(serverOp); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.first); CPPUNIT_ASSERT_EQUAL(WhiteboardOperation::ref(), pairResult.second); } WhiteboardInsertOperation::ref createInsertOperation(std::string id, std::string parent) { WhiteboardInsertOperation::ref operation = boost::make_shared(); operation->setParentID(parent); operation->setID(id); return operation; } }; CPPUNIT_TEST_SUITE_REGISTRATION(WhiteboardClientTest);