ctkEventBusManagerTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * ctkEventBusManagerTest.cpp
  3. * ctkEventBusManagerTest
  4. *
  5. * Created by Paolo Quadrani on 27/03/09.
  6. * Copyright 2009 B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #include "ctkTestSuite.h"
  12. #include <ctkEventBusManager.h>
  13. #include <ctkEventDefinitions.h>
  14. #include <ctkNetworkConnector.h>
  15. using namespace ctkEventBus;
  16. //-------------------------------------------------------------------------
  17. /**
  18. Class name: testObjectCustom
  19. Custom object needed for testing.
  20. */
  21. class testObjectCustom : public QObject {
  22. Q_OBJECT
  23. public:
  24. /// constructor.
  25. testObjectCustom();
  26. /// Return tha var's value.
  27. int var() {return m_Var;}
  28. /// register a custom callback
  29. void registerCustomCallback();
  30. public Q_SLOTS:
  31. /// Test slot that will increment the value of m_Var when an UPDATE_OBJECT event is raised.
  32. void updateObject();
  33. void setObjectValue(int v);
  34. int returnObjectValue();
  35. Q_SIGNALS:
  36. void valueModified(int v);
  37. void objectModified();
  38. int returnObjectValueSignal();
  39. private:
  40. int m_Var; ///< Test var.
  41. };
  42. testObjectCustom::testObjectCustom() : m_Var(0) {
  43. }
  44. void testObjectCustom::updateObject() {
  45. m_Var++;
  46. }
  47. void testObjectCustom::setObjectValue(int v) {
  48. m_Var = v;
  49. }
  50. int testObjectCustom::returnObjectValue() {
  51. int value = 5;
  52. return value;
  53. }
  54. void testObjectCustom::registerCustomCallback() {
  55. ctkRegisterLocalCallback("ctk/local/custom/topic", this, "updateObject()");
  56. }
  57. //------------------------------------------------------------------------------------------
  58. /**
  59. Class name: testNetworkConnectorForEventBus
  60. This class implements the network connector to be tested.
  61. */
  62. class testNetworkConnectorForEventBus : public ctkNetworkConnector {
  63. Q_OBJECT
  64. public:
  65. /// Object constructor.
  66. testNetworkConnectorForEventBus();
  67. /// Create and initialize client
  68. /*virtual*/ void createClient(QString hostName, unsigned int port);
  69. /// Return the string variable initializated and updated from the data pipe.
  70. /*virtual*/ void createServer(unsigned int port);
  71. /// Allow to send a network request.
  72. /*virtual*/ void send(const QString event_id, ctkEventArgumentsList *params);
  73. /// Start the server.
  74. /*virtual*/ void startListen();
  75. /// Return connector status.
  76. QString connectorStatus();
  77. /// retrieve instance of object
  78. /*virtual*/ ctkNetworkConnector *clone();
  79. /// register all the signals and slots
  80. /*virtual*/ void initializeForEventBus();
  81. private:
  82. QString m_ConnectorStatus; ///< Test Var.
  83. };
  84. ctkNetworkConnector *testNetworkConnectorForEventBus::clone() {
  85. return new testNetworkConnectorForEventBus();
  86. }
  87. void testNetworkConnectorForEventBus::initializeForEventBus() {
  88. }
  89. testNetworkConnectorForEventBus::testNetworkConnectorForEventBus() : ctkNetworkConnector(), m_ConnectorStatus("") {
  90. m_Protocol = "FakeProtocol";
  91. }
  92. void testNetworkConnectorForEventBus::createServer(unsigned int port) {
  93. m_ConnectorStatus = "Server Created - Port: ";
  94. m_ConnectorStatus.append(QString::number(port));
  95. }
  96. void testNetworkConnectorForEventBus::startListen() {
  97. m_ConnectorStatus = "Server Listening";
  98. }
  99. void testNetworkConnectorForEventBus::createClient(QString hostName, unsigned int port) {
  100. m_ConnectorStatus = "Client Created - Host: ";
  101. m_ConnectorStatus.append(hostName);
  102. m_ConnectorStatus.append(" Port: ");
  103. m_ConnectorStatus.append(QString::number(port));
  104. }
  105. void testNetworkConnectorForEventBus::send(const QString event_id, ctkEventArgumentsList *params) {
  106. Q_UNUSED(params);
  107. m_ConnectorStatus = "Event sent with ID: ";
  108. m_ConnectorStatus.append(event_id);
  109. }
  110. QString testNetworkConnectorForEventBus::connectorStatus() {
  111. return m_ConnectorStatus;
  112. }
  113. //-------------------------------------------------------------------------
  114. /**
  115. Class name: ctkEventBusManagerTest
  116. This class implements the test suite for ctkEventBusManager.
  117. */
  118. //! <title>
  119. //ctkEventBusManager
  120. //! </title>
  121. //! <description>
  122. //ctkEventBusManager provides the access point of the Communication Bus for ctk framework and
  123. //allows dispatching events coming from local application to attached observers.
  124. //It provides APIs to add a new event property (observer or event) to the event bus hash or
  125. //to remove the event property from the event bus hash.
  126. //It also provides APIs to add or remove observer to the events and to register custom signals
  127. //use by objects to raise them events.
  128. //
  129. //Sender has to:
  130. //- Register a signal (with argument if necessary)
  131. //
  132. //Observer has to:
  133. //- Register the callback that will be called when event will be notified (with argument if necessary)
  134. // with the same Event ID used by the sender
  135. //
  136. //The method "notifyEventLocal(QString topic, ctkEventArgumentsList *argList)" accept a string and a list
  137. //of QGenericArgument: to set QGenericArgument use the ctkEventArgument() macros.
  138. //ctkEventArgument() takes a type name and a const reference of that type.
  139. //! </description>
  140. class ctkEventBusManagerTest : public QObject {
  141. Q_OBJECT
  142. private Q_SLOTS:
  143. /// Initialize test variables
  144. void initTestCase() {
  145. m_EventBus = ctkEventBusManager::instance();
  146. m_ObjTestObserver = new testObjectCustom();
  147. m_ObjTestObserver2 = new testObjectCustom();
  148. }
  149. /// Cleanup tes variables memory allocation.
  150. void cleanupTestCase() {
  151. delete m_ObjTestObserver;
  152. delete m_ObjTestObserver2;
  153. m_EventBus->shutdown();
  154. }
  155. /// Check the existence of the ctkEventBusManager singletone creation.
  156. void ctkEventBusManagerConstructorTest();
  157. /// Check the event observing registration and notification.
  158. void eventBusRegistrationNotificationTest();
  159. /// Check the event observing registration and notification passing one argument.
  160. void eventBusWithArgumentTest();
  161. /// Check the event observing registration and notification returning one argument.
  162. void eventBusWithReturnArgumentTest();
  163. /// Event notification benchmarks.
  164. void eventNotificationBenchmarkTest();
  165. /// Test eventbus with remote connection (xmlrpc test)
  166. void remoteConnectionTest();
  167. /// test plugNetworkConnector
  168. void plugNetworkConnectorTest();
  169. /// test method for check if the signal is present.
  170. void isLocalSignalPresentTest();
  171. /// test registration reversing the order of signal and callback
  172. void reverseOrderRegistrationTest();
  173. private:
  174. testObjectCustom *m_ObjTestObserver; ///< Test variable.
  175. testObjectCustom *m_ObjTestObserver2; ///< Test variable.
  176. ctkEventBusManager *m_EventBus; ///< EventBus test variable instance.
  177. };
  178. void ctkEventBusManagerTest::ctkEventBusManagerConstructorTest() {
  179. QVERIFY(m_EventBus != NULL);
  180. }
  181. void ctkEventBusManagerTest::eventBusRegistrationNotificationTest() {
  182. int status = m_ObjTestObserver->var();
  183. QVERIFY(status == 0);
  184. // Create new Event ID used for callback and event notification.
  185. QString updateID = "ctk/local/eventBus/globalUpdate";
  186. ctkRegisterLocalCallback(updateID, m_ObjTestObserver, "updateObject()");
  187. // Register also the second test observer to the global update event
  188. ctkRegisterLocalCallback(updateID, m_ObjTestObserver2, "updateObject()");
  189. // Notify the update event... (event logging is disabled)
  190. m_EventBus->notifyEvent("ctk/local/eventBus/globalUpdate");
  191. status = m_ObjTestObserver->var();
  192. QVERIFY(status == 1);
  193. // Notify again but with event logging enabled...
  194. m_EventBus->enableEventLogging();
  195. m_EventBus->notifyEvent(updateID);
  196. // ... and now filter events only with ID == "CUSTOM_SIGNAL"
  197. //ctkId customID = idProvider->createNewId("CUSTOM_SIGNAL");
  198. QString customID = "CUSTOM_SIGNAL";
  199. // ...and enable event logging for that id.
  200. m_EventBus->logEventTopic(customID);
  201. // Previous ID notification should be skipped by logger (so only one log in the console)
  202. m_EventBus->notifyEvent(updateID);
  203. }
  204. void ctkEventBusManagerTest::eventBusWithArgumentTest() {
  205. testObjectCustom *ObjTestSender = new testObjectCustom();
  206. ObjTestSender->setObjectValue(52);
  207. // Create new Event ID used for callback and event notification.
  208. QString setValueID = "SETVALUE_SIGNAL";
  209. ctkRegisterLocalSignal(setValueID, ObjTestSender, "valueModified(int)");
  210. // Register the callback to update the object custom:
  211. ctkRegisterLocalCallback(setValueID, m_ObjTestObserver, "setObjectValue(int)");
  212. // Register also the second observer...
  213. ctkRegisterLocalCallback(setValueID, m_ObjTestObserver2, "setObjectValue(int)");
  214. //! <snippet>
  215. ctkEventArgumentsList list;
  216. list.append(ctkEventArgument(int, ObjTestSender->var()));
  217. m_EventBus->notifyEvent(setValueID, ctkEventTypeLocal, &list);
  218. //! </snippet>
  219. int status = m_ObjTestObserver->var();
  220. QVERIFY(status == ObjTestSender->var());
  221. delete ObjTestSender;
  222. ctkEventBusManager::instance()->removeSignal(ObjTestSender, setValueID);
  223. }
  224. void ctkEventBusManagerTest::eventBusWithReturnArgumentTest() {
  225. testObjectCustom *ObjTestSender = new testObjectCustom();
  226. ObjTestSender->setObjectValue(52);
  227. // Create new Event ID used for callback and event notification.
  228. QString returnValueID = "RETURNVALUE_SIGNAL";
  229. ctkRegisterLocalSignal(returnValueID, ObjTestSender, "returnObjectValueSignal()");
  230. ctkRegisterLocalCallback(returnValueID, ObjTestSender, "returnObjectValue()");
  231. //Notify event with return argument
  232. int returnValue = 0;
  233. ctkGenericReturnArgument ret_val = ctkEventReturnArgument(int,returnValue);
  234. m_EventBus->notifyEvent(returnValueID, ctkEventTypeLocal, NULL, &ret_val);
  235. QVERIFY(returnValue == 5);
  236. delete ObjTestSender;
  237. }
  238. void ctkEventBusManagerTest::eventNotificationBenchmarkTest() {
  239. QString updateID = "ctk/local/eventBus/globalUpdate";
  240. m_EventBus->logAllEvents();
  241. QBENCHMARK {
  242. m_EventBus->notifyEvent(updateID);
  243. }
  244. }
  245. void ctkEventBusManagerTest::remoteConnectionTest() {
  246. m_EventBus->createServer("XMLRPC", 8000);
  247. m_EventBus->startListen();
  248. m_EventBus->createClient("XMLRPC", "localhost", 8000);
  249. //create list to send from the client
  250. //first parameter is a list which contains event properties
  251. QVariantList eventParameters;
  252. eventParameters.append("ctk/local/eventBus/globalUpdate");
  253. eventParameters.append(ctkEventTypeLocal);
  254. eventParameters.append(ctkSignatureTypeCallback);
  255. eventParameters.append("updateObject()");
  256. QVariantList dataParameters;
  257. ctkEventArgumentsList listToSend;
  258. listToSend.append(ctkEventArgument(QVariantList, eventParameters));
  259. listToSend.append(ctkEventArgument(QVariantList, dataParameters));
  260. //with eventbus
  261. QString topic = "ctk/remote/eventBus/comunication/send/xmlrpc";
  262. m_EventBus->notifyEvent(topic, ctkEventTypeRemote , &listToSend);
  263. QTime dieTime = QTime::currentTime().addSecs(3);
  264. while(QTime::currentTime() < dieTime) {
  265. QCoreApplication::processEvents(QEventLoop::AllEvents, 3);
  266. }
  267. }
  268. void ctkEventBusManagerTest::plugNetworkConnectorTest() {
  269. m_EventBus->plugNetworkConnector("CUSTOM_CONNECTOR", NULL);
  270. }
  271. void ctkEventBusManagerTest::isLocalSignalPresentTest() {
  272. QVERIFY(m_EventBus->isLocalSignalPresent("ctk/invalid/signal") == false);
  273. }
  274. void ctkEventBusManagerTest::reverseOrderRegistrationTest() {
  275. testObjectCustom *ObjTestSender = new testObjectCustom();
  276. int startValue = 42;
  277. m_ObjTestObserver->setObjectValue(startValue);
  278. //register a custom callback
  279. m_ObjTestObserver->registerCustomCallback();
  280. //register custom signal
  281. ctkRegisterLocalSignal("ctk/local/custom/topic", ObjTestSender, "objectModified()");
  282. //notify event
  283. m_EventBus->notifyEvent("ctk/local/custom/topic");
  284. startValue++; //notify will update the value adding 1
  285. int check = m_ObjTestObserver->var();
  286. QVERIFY(startValue == check);
  287. delete ObjTestSender;
  288. }
  289. CTK_REGISTER_TEST(ctkEventBusManagerTest);
  290. #include "ctkEventBusManagerTest.moc"