ctkEventDispatcherTest.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * ctkEventDispatcherTest.cpp
  3. * ctkEventBusTest
  4. *
  5. * Created by Daniele Giunchi on 22/09/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. using namespace ctkEventBus;
  14. //-------------------------------------------------------------------------
  15. /**
  16. Class name: ctkObjectCustom
  17. Custom object needed for testing.
  18. */
  19. class testObjectCustomForDispatcher : public QObject {
  20. Q_OBJECT
  21. public:
  22. /// constructor.
  23. testObjectCustomForDispatcher();
  24. /// Return the var's value.
  25. int var() {return m_Var;}
  26. /// register a custom callback
  27. void registerCustomCallback();
  28. public Q_SLOTS:
  29. /// Test slot that will increment the value of m_Var when an UPDATE_OBJECT event is raised.
  30. void updateObject();
  31. void updateObject2();
  32. void setObjectValue(int v);
  33. Q_SIGNALS:
  34. void valueModify(int v);
  35. void objectModify();
  36. private:
  37. int m_Var; ///< Test var.
  38. };
  39. testObjectCustomForDispatcher::testObjectCustomForDispatcher() : QObject(), m_Var(0) {
  40. }
  41. void testObjectCustomForDispatcher::updateObject() {
  42. m_Var++;
  43. }
  44. void testObjectCustomForDispatcher::updateObject2() {
  45. }
  46. void testObjectCustomForDispatcher::setObjectValue(int v) {
  47. m_Var = v;
  48. }
  49. void testObjectCustomForDispatcher::registerCustomCallback() {
  50. ctkRegisterLocalCallback("ctk/local/custom/topic", this, "updateObject()");
  51. }
  52. //-------------------------------------------------------------------------
  53. /**
  54. Class name: ctkEventDispatcherTest
  55. This class implements the test suite for ctkEventDispatcher.
  56. */
  57. //! <title>
  58. //ctkEventDispatcher
  59. //! </title>
  60. //! <description>
  61. //ctkEventDispatcher allows dispatching events coming from local application
  62. //to attached observers.
  63. //! </description>
  64. class ctkEventDispatcherTest : public QObject {
  65. Q_OBJECT
  66. private Q_SLOTS:
  67. /// Initialize test variables
  68. void initTestCase() {
  69. m_ObjTestObserver = new testObjectCustomForDispatcher;
  70. m_ObjTestObserver->setObjectName("TestObserver");
  71. m_EventDispatcher = new ctkEventDispatcher;
  72. m_EventDispatcher->setObjectName("EventDispatcher");
  73. }
  74. /// Cleanup test variables memory allocation.
  75. void cleanupTestCase() {
  76. delete m_ObjTestObserver;
  77. delete m_EventDispatcher;
  78. }
  79. /// ctkEventDispatcher allocation test case.
  80. void ctkEventDispatcherAllocationTest();
  81. /// Test the add and remove observer.
  82. void ctkEventDispatcherAddAndRemoveObserverAndNotifyEventTest();
  83. /// Test the add and remove signal.
  84. void ctkEventDispatcherRegisterAndRemoveSignalAndNotifyEventTest();
  85. /// Test if the signal is present after registration
  86. void isSignalPresentTest();
  87. /// Remove observer given a pointer.
  88. void removeObserverTest();
  89. /// Remove item from the dispatcher.
  90. void removeItemTest();
  91. /// Remove signal given a pointer.
  92. void removeSignalTest();
  93. /// test if the local signal is present
  94. void isLocalSignalPresentTest();
  95. /// reverse order registration test
  96. void reverseOrderRegistrationTest();
  97. private:
  98. ctkEventDispatcher *m_EventDispatcher; ///< Test var.
  99. testObjectCustomForDispatcher *m_ObjTestObserver; ///< Test Object var
  100. };
  101. void ctkEventDispatcherTest::ctkEventDispatcherAllocationTest() {
  102. QVERIFY(m_EventDispatcher != NULL);
  103. }
  104. void ctkEventDispatcherTest::ctkEventDispatcherAddAndRemoveObserverAndNotifyEventTest() {
  105. // Create new Event ID used for callback and event notification.
  106. QString updateID = "ctk/local/dispatcherTest/update";
  107. ctkBusEvent *properties = new ctkBusEvent(updateID, ctkDictionary());
  108. (*properties)[TYPE] = ctkEventTypeLocal;
  109. (*properties)[SIGTYPE] = ctkSignatureTypeSignal;
  110. QVariant var;
  111. var.setValue((QObject*)m_ObjTestObserver);
  112. (*properties)[OBJECT] = var;
  113. (*properties)[SIGNATURE] = "objectModify()";
  114. QVERIFY(m_EventDispatcher->registerSignal(*properties));
  115. ctkBusEvent *propCallback = new ctkBusEvent(updateID, ctkDictionary());
  116. (*propCallback)[TYPE] = ctkEventTypeLocal;
  117. (*propCallback)[SIGTYPE] = ctkSignatureTypeCallback;
  118. QVariant varobserver;
  119. varobserver.setValue((QObject*)m_ObjTestObserver);
  120. (*propCallback)[OBJECT] = varobserver;
  121. (*propCallback)[SIGNATURE] = "updateObject()";
  122. QVERIFY(m_EventDispatcher->addObserver(*propCallback));
  123. }
  124. void ctkEventDispatcherTest::ctkEventDispatcherRegisterAndRemoveSignalAndNotifyEventTest() {
  125. QString updateID = "ctk/local/dispatcherTest/update";
  126. ctkBusEvent *properties = new ctkBusEvent(updateID, ctkDictionary());
  127. (*properties)[TYPE] = ctkEventTypeLocal;
  128. (*properties)[SIGTYPE] = ctkSignatureTypeSignal;
  129. QVariant var;
  130. var.setValue((QObject*)m_ObjTestObserver);
  131. (*properties)[OBJECT] = var;
  132. (*properties)[SIGNATURE] = "objectModify()";
  133. QVERIFY(m_EventDispatcher->removeSignal(*properties));
  134. QVERIFY(m_EventDispatcher->registerSignal(*properties));
  135. // Register the callback to update the object custom:
  136. ctkBusEvent *propCallback = new ctkBusEvent(updateID, ctkDictionary());
  137. (*propCallback)[TYPE] = ctkEventTypeLocal;
  138. (*propCallback)[SIGTYPE] = ctkSignatureTypeCallback;
  139. QVariant varobserver;
  140. varobserver.setValue((QObject*)m_ObjTestObserver);
  141. (*propCallback)[OBJECT] = varobserver;
  142. (*propCallback)[SIGNATURE] = "updateObject()";
  143. QVERIFY(m_EventDispatcher->addObserver(*propCallback));
  144. }
  145. void ctkEventDispatcherTest::isSignalPresentTest() {
  146. QString updateID = "ctk/local/dispatcherTest/update";
  147. // Register the callback to update the object custom:
  148. QVERIFY(m_EventDispatcher->isLocalSignalPresent(updateID));
  149. }
  150. void ctkEventDispatcherTest::removeObserverTest() {
  151. QString updateID = "ctk/local/dispatcherTest/update";
  152. // remove the observer from the updateID topics...
  153. QVERIFY(m_EventDispatcher->removeObserver(m_ObjTestObserver, updateID));
  154. // Add again the test object as observer...
  155. ctkBusEvent *propCallback = new ctkBusEvent(updateID, ctkDictionary());
  156. (*propCallback)[TYPE] = ctkEventTypeLocal;
  157. (*propCallback)[SIGTYPE] = ctkSignatureTypeCallback;
  158. QVariant varobserver;
  159. varobserver.setValue((QObject*)m_ObjTestObserver);
  160. (*propCallback)[OBJECT] = varobserver;
  161. (*propCallback)[SIGNATURE] = "updateObject()";
  162. QVERIFY(m_EventDispatcher->addObserver(*propCallback));
  163. // remove the observer from all the topics...
  164. QVERIFY(m_EventDispatcher->removeObserver(m_ObjTestObserver, ""));
  165. }
  166. void ctkEventDispatcherTest::removeItemTest() {
  167. QString updateID = "ctk/local/dispatcherTest/update";
  168. // Add again the test object as observer...
  169. ctkBusEvent *propCallback = new ctkBusEvent(updateID, ctkDictionary());
  170. (*propCallback)[TYPE] = ctkEventTypeLocal;
  171. (*propCallback)[SIGTYPE] = ctkSignatureTypeCallback;
  172. QVariant varobserver;
  173. varobserver.setValue((QObject*)m_ObjTestObserver);
  174. (*propCallback)[OBJECT] = varobserver;
  175. (*propCallback)[SIGNATURE] = "updateObject()";
  176. QVERIFY(m_EventDispatcher->addObserver(*propCallback));
  177. // remove the observer from all the topics...
  178. ctkBusEvent *propCallback2 = new ctkBusEvent(updateID, ctkDictionary());
  179. (*propCallback2)[TYPE] = ctkEventTypeLocal;
  180. (*propCallback2)[SIGTYPE] = ctkSignatureTypeCallback;
  181. (*propCallback2)[OBJECT] = varobserver;
  182. (*propCallback2)[SIGNATURE] = "updateObject2()";
  183. QVERIFY(m_EventDispatcher->addObserver(*propCallback2));
  184. //this will be removed
  185. QVERIFY(m_EventDispatcher->removeObserver(*propCallback));
  186. // this will be removed and will cover the code of iterator which simple go to the next element
  187. QVERIFY(m_EventDispatcher->removeObserver(*propCallback2));
  188. }
  189. void ctkEventDispatcherTest::removeSignalTest() {
  190. QString updateID = "ctk/local/dispatcherTest/update";
  191. // remove the signal from the updateID topics...
  192. // ...but don't need to make a qt disconnect because all observer has been disconnected already on previous test case.
  193. QVERIFY(m_EventDispatcher->removeSignal(m_ObjTestObserver, updateID, false));
  194. ctkBusEvent *properties = new ctkBusEvent(updateID, ctkDictionary());
  195. (*properties)[TYPE] = ctkEventTypeLocal;
  196. (*properties)[SIGTYPE] = ctkSignatureTypeSignal;
  197. QVariant var;
  198. var.setValue((QObject*)m_ObjTestObserver);
  199. (*properties)[OBJECT] = var;
  200. (*properties)[SIGNATURE] = "objectModify()";
  201. QVERIFY(m_EventDispatcher->registerSignal(*properties));
  202. QVERIFY(m_EventDispatcher->removeSignal(m_ObjTestObserver, "", false));
  203. }
  204. void ctkEventDispatcherTest::reverseOrderRegistrationTest() {
  205. QString updateID = "ctk/local/dispatcherTest/custom";
  206. // Register the callback to update the object custom:
  207. ctkBusEvent *propCallback = new ctkBusEvent(updateID, ctkDictionary());
  208. (*propCallback)[TYPE] = ctkEventTypeLocal;
  209. (*propCallback)[SIGTYPE] = ctkSignatureTypeCallback;
  210. QVariant varobserver;
  211. varobserver.setValue((QObject*)m_ObjTestObserver);
  212. (*propCallback)[OBJECT] = varobserver;
  213. (*propCallback)[SIGNATURE] = "updateObject()";
  214. QVERIFY(m_EventDispatcher->addObserver(*propCallback));
  215. ctkBusEvent *properties = new ctkBusEvent(updateID, ctkDictionary());
  216. (*properties)[TYPE] = ctkEventTypeLocal;
  217. (*properties)[SIGTYPE] = ctkSignatureTypeSignal;
  218. QVariant var;
  219. var.setValue((QObject*)m_ObjTestObserver);
  220. (*properties)[OBJECT] = var;
  221. (*properties)[SIGNATURE] = "objectModify()";
  222. QVERIFY(m_EventDispatcher->registerSignal(*properties));
  223. }
  224. void ctkEventDispatcherTest::isLocalSignalPresentTest() {
  225. QVERIFY(m_EventDispatcher->isLocalSignalPresent("ctk/wrong/topic") == false);
  226. }
  227. //CTK_REGISTER_TEST(ctkEventDispatcherTest);
  228. #include "ctkEventDispatcherTest.moc"