ctkEATopicWildcardTestSuite.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkEATopicWildcardTestSuite_p.h"
  16. #include <ctkPluginContext.h>
  17. #include <ctkPluginConstants.h>
  18. #include <service/event/ctkEventAdmin.h>
  19. #include <service/event/ctkEventConstants.h>
  20. #include <QTest>
  21. #include <QDebug>
  22. //----------------------------------------------------------------------------
  23. ctkEvent ctkEATopicWildcardTestHelper::clearLastEvent()
  24. {
  25. QWriteLocker l(&rwlock);
  26. ctkEvent result = last;
  27. last = ctkEvent();
  28. return result;
  29. }
  30. //----------------------------------------------------------------------------
  31. void ctkEATopicWildcardTestHelper::handleEvent(const ctkEvent& event)
  32. {
  33. QWriteLocker l(&rwlock);
  34. last = event;
  35. }
  36. //----------------------------------------------------------------------------
  37. ctkEvent ctkEATopicWildcardTestHelper::lastEvent() const
  38. {
  39. QReadLocker l(&rwlock);
  40. return last;
  41. }
  42. //----------------------------------------------------------------------------
  43. ctkEATopicWildcardTestSuite::ctkEATopicWildcardTestSuite(
  44. ctkPluginContext* pc, long eventPluginId, bool useSignalSlot)
  45. : context(pc), eventPluginId(eventPluginId),
  46. useSignalSlot(useSignalSlot), eventAdmin(0)
  47. {
  48. }
  49. //----------------------------------------------------------------------------
  50. void ctkEATopicWildcardTestSuite::initTestCase()
  51. {
  52. qDebug() << "Using" << (useSignalSlot ? "" : "no") << "Qt signal/slot mechanism";
  53. }
  54. //----------------------------------------------------------------------------
  55. void ctkEATopicWildcardTestSuite::init()
  56. {
  57. context->getPlugin(eventPluginId)->start();
  58. reference = context->getServiceReference<ctkEventAdmin>();
  59. eventAdmin = context->getService<ctkEventAdmin>(reference);
  60. if (useSignalSlot)
  61. {
  62. eventAdmin->publishSignal(this, SIGNAL(syncSignal(ctkDictionary)), "a/b/c", Qt::DirectConnection);
  63. }
  64. }
  65. //----------------------------------------------------------------------------
  66. void ctkEATopicWildcardTestSuite::cleanup()
  67. {
  68. context->ungetService(reference);
  69. context->getPlugin(eventPluginId)->stop();
  70. }
  71. //----------------------------------------------------------------------------
  72. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic1()
  73. {
  74. ctkDictionary properties;
  75. properties.insert(ctkEventConstants::EVENT_TOPIC, "a/b/c/*");
  76. ctkEATopicWildcardTestHelper handler;
  77. ctkEvent event("a/b/c");
  78. if (useSignalSlot)
  79. {
  80. qlonglong id = eventAdmin->subscribeSlot(&handler, SLOT(handleEvent(ctkEvent)), properties);
  81. emit syncSignal(ctkDictionary());
  82. eventAdmin->unsubscribeSlot(id);
  83. }
  84. else
  85. {
  86. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  87. eventAdmin->sendEvent(event);
  88. handlerRegistration.unregister();
  89. }
  90. QVERIFY2(handler.lastEvent().isNull(), "Received event published to topic 'a/b/c' while listening to 'a/b/c/*'");
  91. }
  92. //----------------------------------------------------------------------------
  93. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic2()
  94. {
  95. ctkDictionary properties;
  96. properties.insert(ctkEventConstants::EVENT_TOPIC, "a/b/c/*");
  97. ctkEATopicWildcardTestHelper handler;
  98. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  99. ctkEvent event("a/b");
  100. eventAdmin->sendEvent(event);
  101. QVERIFY2(handler.lastEvent().isNull(), "Received event published to topic 'a/b' while listening to 'a/b/c/*'");
  102. handlerRegistration.unregister();
  103. }
  104. //----------------------------------------------------------------------------
  105. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic3()
  106. {
  107. ctkDictionary properties;
  108. properties.insert(ctkEventConstants::EVENT_TOPIC, "a/b/c/*");
  109. ctkEATopicWildcardTestHelper handler;
  110. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  111. ctkEvent event("a");
  112. eventAdmin->sendEvent(event);
  113. QVERIFY2(handler.lastEvent().isNull(), "Received event published to topic 'a' while listening to 'a/b/c/*'");
  114. handlerRegistration.unregister();
  115. }
  116. //----------------------------------------------------------------------------
  117. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic4()
  118. {
  119. ctkDictionary properties;
  120. properties.insert(ctkEventConstants::EVENT_TOPIC, "a/b/c/*");
  121. ctkEATopicWildcardTestHelper handler;
  122. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  123. ctkEvent event("a/b/c/d");
  124. eventAdmin->sendEvent(event);
  125. QVERIFY2(!handler.lastEvent().isNull(), "Did not receive event published to topic 'a/b/c/d' while listening to 'a/b/c/*'");
  126. handlerRegistration.unregister();
  127. }
  128. //----------------------------------------------------------------------------
  129. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic5()
  130. {
  131. ctkDictionary properties;
  132. properties.insert(ctkEventConstants::EVENT_TOPIC, "a/b/c/*");
  133. ctkEATopicWildcardTestHelper handler;
  134. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  135. ctkEvent event("a/b/c/d/e");
  136. eventAdmin->sendEvent(event);
  137. QVERIFY2(!handler.lastEvent().isNull(), "Did not receive event published to topic 'a/b/c/d/e' while listening to 'a/b/c/*'");
  138. handlerRegistration.unregister();
  139. }
  140. //----------------------------------------------------------------------------
  141. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic6()
  142. {
  143. ctkDictionary properties;
  144. properties.insert(ctkEventConstants::EVENT_TOPIC, "a/b/c/*");
  145. ctkEATopicWildcardTestHelper handler;
  146. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  147. ctkEvent event("a/b/c/d/e/f");
  148. eventAdmin->sendEvent(event);
  149. QVERIFY2(!handler.lastEvent().isNull(), "Did not receive event published to topic 'a/b/c/d/e/f' while listening to 'a/b/c/*'");
  150. handlerRegistration.unregister();
  151. }
  152. //----------------------------------------------------------------------------
  153. void ctkEATopicWildcardTestSuite::testEventDeliveryForWildcardTopic7()
  154. {
  155. ctkDictionary properties;
  156. QStringList topics("a/b/c");
  157. topics << "a/b/c/*";
  158. properties.insert(ctkEventConstants::EVENT_TOPIC, topics);
  159. ctkEATopicWildcardTestHelper handler;
  160. ctkServiceRegistration handlerRegistration = context->registerService<ctkEventHandler>(&handler, properties);
  161. ctkEvent event("a/b/c");
  162. eventAdmin->sendEvent(event);
  163. QVERIFY2(!handler.clearLastEvent().isNull(), "Did not receive event published to topic 'a/b/c' while listening to 'a/b/c'");
  164. event = ctkEvent("a/b/c/d");
  165. eventAdmin->sendEvent(event);
  166. QVERIFY2(!handler.lastEvent().isNull(), "Did not receive event published to topic 'a/b/c/d' while listening to 'a/b/c/*'");
  167. handlerRegistration.unregister();
  168. }