ctkNetworkConnectorTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * ctkNetworkConnectorTest.cpp
  3. * ctkResourcesTest
  4. *
  5. * Created by Daniele Giunchi on 20/04/10.
  6. * Copyright 2009 B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #include "ctkTestSuite.h"
  12. #include <ctkNetworkConnector.h>
  13. using namespace ctkEventBus;
  14. //------------------------------------------------------------------------------------------
  15. /**
  16. Class name: testNetworkConnectorCustom
  17. This class implements the network connector to be tested.
  18. */
  19. class testNetworkConnectorCustom : public ctkNetworkConnector {
  20. Q_OBJECT
  21. public:
  22. /// Object constructor.
  23. testNetworkConnectorCustom();
  24. /// Create and initialize client
  25. /*virtual*/ void createClient(const QString hostName, const unsigned int port);
  26. /// Return the string variable initializated and updated from the data pipe.
  27. /*virtual*/ void createServer(const unsigned int port);
  28. /// Allow to send a network request.
  29. /*virtual*/ void send(const QString event_id, ctkEventArgumentsList *params);
  30. /// Start the server.
  31. /*virtual*/ void startListen();
  32. /// Return connector status.
  33. QString connectorStatus();
  34. /// retrieve instance of object
  35. /*virtual*/ ctkNetworkConnector *clone();
  36. /// register all the signals and slots
  37. /*virtual*/ void initializeForEventBus();
  38. private:
  39. QString m_ConnectorStatus; ///< Test Var.
  40. };
  41. ctkNetworkConnector *testNetworkConnectorCustom::clone() {
  42. return new testNetworkConnectorCustom();
  43. }
  44. void testNetworkConnectorCustom::initializeForEventBus() {
  45. }
  46. testNetworkConnectorCustom::testNetworkConnectorCustom() : ctkNetworkConnector(), m_ConnectorStatus("") {
  47. m_Protocol = "FakeProtocol";
  48. }
  49. void testNetworkConnectorCustom::createServer(const unsigned int port) {
  50. m_ConnectorStatus = "Server Created - Port: ";
  51. m_ConnectorStatus.append(QString::number(port));
  52. }
  53. void testNetworkConnectorCustom::startListen() {
  54. m_ConnectorStatus = "Server Listening";
  55. }
  56. void testNetworkConnectorCustom::createClient(const QString hostName, const unsigned int port) {
  57. m_ConnectorStatus = "Client Created - Host: ";
  58. m_ConnectorStatus.append(hostName);
  59. m_ConnectorStatus.append(" Port: ");
  60. m_ConnectorStatus.append(QString::number(port));
  61. }
  62. void testNetworkConnectorCustom::send(const QString event_id, ctkEventArgumentsList *params) {
  63. Q_UNUSED(params);
  64. m_ConnectorStatus = "Event sent with ID: ";
  65. m_ConnectorStatus.append(event_id);
  66. }
  67. QString testNetworkConnectorCustom::connectorStatus() {
  68. return m_ConnectorStatus;
  69. }
  70. //------------------------------------------------------------------------------------------
  71. /**
  72. Class name: ctkNetworkConnectorTest
  73. This class implements the test suite for ctkNetworkConnector.
  74. */
  75. //! <title>
  76. //ctkNetworkConnector
  77. //! </title>
  78. //! <description>
  79. //ctkNetworkConnector is the interface class for client/server objects that
  80. //works over network.
  81. //! </description>
  82. class ctkNetworkConnectorTest : public QObject {
  83. Q_OBJECT
  84. private Q_SLOTS:
  85. /// Initialize test variables
  86. void initTestCase() {
  87. m_NetworkConnector = new testNetworkConnectorCustom();
  88. }
  89. /// Cleanup test variables memory allocation.
  90. void cleanupTestCase() {
  91. if(m_NetworkConnector) delete m_NetworkConnector;
  92. }
  93. /// ctkNetworkConnector allocation test case.
  94. void ctkNetworkConnectorAllocationTest();
  95. /// Test the creation of client and server.
  96. void ctkNetworkConnectorCreateClientAndServerTest();
  97. /// test the function that retrive protocol type
  98. void retrieveProtocolTest();
  99. private:
  100. ctkNetworkConnector *m_NetworkConnector; ///< Test var.
  101. };
  102. void ctkNetworkConnectorTest::ctkNetworkConnectorAllocationTest() {
  103. QVERIFY(m_NetworkConnector != NULL);
  104. }
  105. void ctkNetworkConnectorTest::ctkNetworkConnectorCreateClientAndServerTest() {
  106. QString res;
  107. res = "Server Created - Port: 8000";
  108. m_NetworkConnector->createServer(8000);
  109. testNetworkConnectorCustom *conn = (testNetworkConnectorCustom *)m_NetworkConnector;
  110. QCOMPARE(conn->connectorStatus(), res);
  111. res = "Client Created - Host: localhost Port: 8000";
  112. m_NetworkConnector->createClient("localhost", 8000);
  113. QCOMPARE(conn->connectorStatus(), res);
  114. }
  115. void ctkNetworkConnectorTest::retrieveProtocolTest() {
  116. QString res = "FakeProtocol";
  117. QString check = m_NetworkConnector->protocol();
  118. QCOMPARE(check, res);
  119. }
  120. CTK_REGISTER_TEST(ctkNetworkConnectorTest);
  121. #include "ctkNetworkConnectorTest.moc"