EventManagerTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2020-11-29
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <ls_std_test.hpp>
  12. namespace {
  13. class EventManagerTest : public ::testing::Test {
  14. protected:
  15. EventManagerTest() = default;
  16. ~EventManagerTest() override = default;
  17. void SetUp() override
  18. {}
  19. void TearDown() override
  20. {}
  21. };
  22. TEST_F(EventManagerTest, getClassName)
  23. {
  24. ls_std::EventManager eventManager {};
  25. ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
  26. }
  27. TEST_F(EventManagerTest, production_example)
  28. {
  29. std::string news, expectedNews {};
  30. ls_std::event_id seriousNewsEventId = ls_std_test::SeriousNewsEvent {""}.getId();
  31. ls_std::event_id gossipNewsEventId = ls_std_test::GossipNewsEvent {""}.getId();
  32. // create event handler
  33. std::shared_ptr<ls_std::EventHandler> seriousNewsEventHandler = std::make_shared<ls_std::EventHandler>(seriousNewsEventId); // event id
  34. std::shared_ptr<ls_std::EventHandler> gossipNewsEventHandler = std::make_shared<ls_std::EventHandler>(gossipNewsEventId); // event id
  35. // create and fill event manger with handler
  36. std::shared_ptr<ls_std::EventManager> eventManager = std::make_shared<ls_std::EventManager>();
  37. eventManager->addEventHandler(seriousNewsEventHandler);
  38. eventManager->addEventHandler(gossipNewsEventHandler);
  39. // create news agency (listener)
  40. std::shared_ptr<ls_std_test::DailyNewsAgency> dailyNews = std::make_shared<ls_std_test::DailyNewsAgency>();
  41. std::shared_ptr<ls_std_test::GossipNewsAgency> gossipNews = std::make_shared<ls_std_test::GossipNewsAgency>();
  42. // add handler of those events, every news agency should know
  43. // this is how agencies know about events
  44. dailyNews->addEventHandler(seriousNewsEventHandler);
  45. gossipNews->addEventHandler(seriousNewsEventHandler);
  46. gossipNews->addEventHandler(gossipNewsEventHandler);
  47. // fire SeriousNewsEvent event with no effect
  48. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  49. ASSERT_TRUE(dailyNews->getNews().empty());
  50. ASSERT_TRUE(gossipNews->getNews().empty());
  51. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  52. eventManager->subscribe(seriousNewsEventId, dailyNews);
  53. eventManager->subscribe(seriousNewsEventId, gossipNews);
  54. news = "COVID-19 is still going on!";
  55. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  56. expectedNews = "DailyNewsAgency: " + news;
  57. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  58. expectedNews = "GossipNewsAgency: " + news;
  59. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  60. dailyNews->clear();
  61. gossipNews->clear();
  62. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  63. eventManager->unsubscribe(seriousNewsEventId, gossipNews);
  64. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  65. expectedNews = "DailyNewsAgency: " + news;
  66. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  67. ASSERT_TRUE(gossipNews->getNews().empty());
  68. dailyNews->clear();
  69. gossipNews->clear();
  70. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  71. eventManager->subscribe(gossipNewsEventId, gossipNews);
  72. eventManager->subscribe(seriousNewsEventId, gossipNews);
  73. news = "COVID-19 is still going on!";
  74. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  75. expectedNews = "GossipNewsAgency: " + news;
  76. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  77. news = "ape likes banana!";
  78. eventManager->fire(ls_std_test::GossipNewsEvent(news)); // event call
  79. expectedNews = "GossipNewsAgency: " + news;
  80. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  81. }
  82. }