EventManagerTest.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2022-05-20
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_event.hpp>
  11. #include <ls_std_event_test.hpp>
  12. namespace
  13. {
  14. class EventManagerTest : public ::testing::Test
  15. {
  16. protected:
  17. EventManagerTest() = default;
  18. ~EventManagerTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(EventManagerTest, getClassName)
  25. {
  26. ls::std::event::EventManager eventManager{};
  27. ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
  28. }
  29. TEST_F(EventManagerTest, subscribe_empty_id)
  30. {
  31. EXPECT_THROW({
  32. try
  33. {
  34. ls::std::event::EventManager eventManager{};
  35. eventManager.subscribe("", ::std::make_shared<ls_std_event_test::DailyNewsAgency>());
  36. }
  37. catch (const ls::std::core::IllegalArgumentException &_exception)
  38. {
  39. throw;
  40. }
  41. }, ls::std::core::IllegalArgumentException);
  42. }
  43. TEST_F(EventManagerTest, subscribe_no_listener)
  44. {
  45. EXPECT_THROW({
  46. try
  47. {
  48. ls::std::event::EventManager eventManager{};
  49. eventManager.subscribe("TMP_ID", nullptr);
  50. }
  51. catch (const ls::std::core::IllegalArgumentException &_exception)
  52. {
  53. throw;
  54. }
  55. }, ls::std::core::IllegalArgumentException);
  56. }
  57. TEST_F(EventManagerTest, subscribe_no_event_handler_available)
  58. {
  59. EXPECT_THROW({
  60. try
  61. {
  62. ls::std::event::EventManager eventManager{};
  63. eventManager.subscribe("TMP_DIR", ::std::make_shared<ls_std_event_test::DailyNewsAgency>());
  64. }
  65. catch (const ls::std::core::EventNotSubscribedException &_exception)
  66. {
  67. throw;
  68. }
  69. }, ls::std::core::EventNotSubscribedException);
  70. }
  71. TEST_F(EventManagerTest, unsubscribe_empty_id)
  72. {
  73. EXPECT_THROW({
  74. try
  75. {
  76. ls::std::event::EventManager eventManager{};
  77. eventManager.unsubscribe("", ::std::make_shared<ls_std_event_test::DailyNewsAgency>());
  78. }
  79. catch (const ls::std::core::IllegalArgumentException &_exception)
  80. {
  81. throw;
  82. }
  83. }, ls::std::core::IllegalArgumentException);
  84. }
  85. TEST_F(EventManagerTest, unsubscribe_no_listener)
  86. {
  87. EXPECT_THROW({
  88. try
  89. {
  90. ls::std::event::EventManager eventManager{};
  91. eventManager.unsubscribe("TMP_ID", nullptr);
  92. }
  93. catch (const ls::std::core::IllegalArgumentException &_exception)
  94. {
  95. throw;
  96. }
  97. }, ls::std::core::IllegalArgumentException);
  98. }
  99. TEST_F(EventManagerTest, addEventHandler)
  100. {
  101. ls::std::event::EventManager eventManager{};
  102. ASSERT_TRUE(eventManager.addEventHandler(::std::make_shared<ls::std::event::EventHandler>("TMP_ID")));
  103. }
  104. TEST_F(EventManagerTest, addEventHandler_event_handler_already_exists)
  105. {
  106. ls::std::event::EventManager eventManager{};
  107. ASSERT_TRUE(eventManager.addEventHandler(::std::make_shared<ls::std::event::EventHandler>("TMP_ID")));
  108. ASSERT_FALSE(eventManager.addEventHandler(::std::make_shared<ls::std::event::EventHandler>("TMP_ID")));
  109. }
  110. TEST_F(EventManagerTest, addEventHandler_no_reference)
  111. {
  112. EXPECT_THROW({
  113. try
  114. {
  115. ls::std::event::EventManager eventManager{};
  116. eventManager.addEventHandler(nullptr);
  117. }
  118. catch (const ls::std::core::IllegalArgumentException &_exception)
  119. {
  120. throw;
  121. }
  122. }, ls::std::core::IllegalArgumentException);
  123. }
  124. TEST_F(EventManagerTest, fire_event_handler_not_available)
  125. {
  126. EXPECT_THROW({
  127. try
  128. {
  129. ls::std::event::EventManager eventManager{};
  130. eventManager.fire(ls::std::event::Event{"TMP_ID"});
  131. }
  132. catch (const ls::std::core::EventNotHandledException &_exception)
  133. {
  134. throw;
  135. }
  136. }, ls::std::core::EventNotHandledException);
  137. }
  138. TEST_F(EventManagerTest, hasEventHandler)
  139. {
  140. ls::std::event::EventManager eventManager{};
  141. eventManager.addEventHandler(::std::make_shared<ls::std::event::EventHandler>("TMP_ID"));
  142. ASSERT_TRUE(eventManager.hasEventHandler("TMP_ID"));
  143. }
  144. TEST_F(EventManagerTest, hasEventHandler_no_event_handler_available)
  145. {
  146. ls::std::event::EventManager eventManager{};
  147. ASSERT_FALSE(eventManager.hasEventHandler("TMP_ID"));
  148. }
  149. TEST_F(EventManagerTest, hasEventHandler_empty_id)
  150. {
  151. EXPECT_THROW({
  152. try
  153. {
  154. ls::std::event::EventManager eventManager{};
  155. eventManager.hasEventHandler("");
  156. }
  157. catch (const ls::std::core::IllegalArgumentException &_exception)
  158. {
  159. throw;
  160. }
  161. }, ls::std::core::IllegalArgumentException);
  162. }
  163. TEST_F(EventManagerTest, removeEventHandler)
  164. {
  165. ls::std::event::EventManager eventManager{};
  166. ::std::shared_ptr<ls::std::event::EventHandler> eventHandler = ::std::make_shared<ls::std::event::EventHandler>("TMP_ID");
  167. eventManager.addEventHandler(eventHandler);
  168. ASSERT_TRUE(eventManager.removeEventHandler(eventHandler));
  169. }
  170. TEST_F(EventManagerTest, removeEventHandler_no_event_handler_available)
  171. {
  172. ls::std::event::EventManager eventManager{};
  173. ASSERT_FALSE(eventManager.removeEventHandler(::std::make_shared<ls::std::event::EventHandler>("TMP_ID")));
  174. }
  175. TEST_F(EventManagerTest, removeEventHandler_no_reference)
  176. {
  177. EXPECT_THROW({
  178. try
  179. {
  180. ls::std::event::EventManager eventManager{};
  181. eventManager.removeEventHandler(nullptr);
  182. }
  183. catch (const ls::std::core::IllegalArgumentException &_exception)
  184. {
  185. throw;
  186. }
  187. }, ls::std::core::IllegalArgumentException);
  188. }
  189. TEST_F(EventManagerTest, production_example)
  190. {
  191. ::std::string news, expectedNews{};
  192. ls::std::core::type::event_id seriousNewsEventId = ls_std_event_test::SeriousNewsEvent{""}.getId();
  193. ls::std::core::type::event_id gossipNewsEventId = ls_std_event_test::GossipNewsEvent{""}.getId();
  194. // create event handler
  195. ::std::shared_ptr<ls::std::event::EventHandler> seriousNewsEventHandler = ::std::make_shared<ls::std::event::EventHandler>(seriousNewsEventId); // event id
  196. ::std::shared_ptr<ls::std::event::EventHandler> gossipNewsEventHandler = ::std::make_shared<ls::std::event::EventHandler>(gossipNewsEventId); // event id
  197. // create and fill event manager with handler
  198. ::std::shared_ptr<ls::std::event::EventManager> eventManager = ::std::make_shared<ls::std::event::EventManager>();
  199. eventManager->addEventHandler(seriousNewsEventHandler);
  200. eventManager->addEventHandler(gossipNewsEventHandler);
  201. // create news agency (listener)
  202. ::std::shared_ptr<ls_std_event_test::DailyNewsAgency> dailyNews = ::std::make_shared<ls_std_event_test::DailyNewsAgency>();
  203. ::std::shared_ptr<ls_std_event_test::GossipNewsAgency> gossipNews = ::std::make_shared<ls_std_event_test::GossipNewsAgency>();
  204. // fire SeriousNewsEvent event with no effect
  205. eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  206. ASSERT_TRUE(dailyNews->getNews().empty());
  207. ASSERT_TRUE(gossipNews->getNews().empty());
  208. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  209. eventManager->subscribe(seriousNewsEventId, dailyNews);
  210. eventManager->subscribe(seriousNewsEventId, gossipNews);
  211. news = "COVID-19 is still going on!";
  212. eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  213. expectedNews = "DailyNewsAgency: " + news;
  214. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  215. expectedNews = "GossipNewsAgency: " + news;
  216. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  217. dailyNews->clear();
  218. gossipNews->clear();
  219. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  220. eventManager->unsubscribe(seriousNewsEventId, gossipNews);
  221. eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  222. expectedNews = "DailyNewsAgency: " + news;
  223. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  224. ASSERT_TRUE(gossipNews->getNews().empty());
  225. dailyNews->clear();
  226. gossipNews->clear();
  227. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  228. eventManager->subscribe(gossipNewsEventId, gossipNews);
  229. eventManager->subscribe(seriousNewsEventId, gossipNews);
  230. news = "COVID-19 is still going on!";
  231. eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  232. expectedNews = "GossipNewsAgency: " + news;
  233. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  234. news = "ape likes banana!";
  235. eventManager->fire(ls_std_event_test::GossipNewsEvent(news)); // event call
  236. expectedNews = "GossipNewsAgency: " + news;
  237. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  238. }
  239. }