EventManagerTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 manager 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. // fire SeriousNewsEvent event with no effect
  43. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  44. ASSERT_TRUE(dailyNews->getNews().empty());
  45. ASSERT_TRUE(gossipNews->getNews().empty());
  46. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  47. eventManager->subscribe(seriousNewsEventId, dailyNews);
  48. eventManager->subscribe(seriousNewsEventId, gossipNews);
  49. news = "COVID-19 is still going on!";
  50. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  51. expectedNews = "DailyNewsAgency: " + news;
  52. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  53. expectedNews = "GossipNewsAgency: " + news;
  54. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  55. dailyNews->clear();
  56. gossipNews->clear();
  57. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  58. eventManager->unsubscribe(seriousNewsEventId, gossipNews);
  59. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  60. expectedNews = "DailyNewsAgency: " + news;
  61. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  62. ASSERT_TRUE(gossipNews->getNews().empty());
  63. dailyNews->clear();
  64. gossipNews->clear();
  65. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  66. eventManager->subscribe(gossipNewsEventId, gossipNews);
  67. eventManager->subscribe(seriousNewsEventId, gossipNews);
  68. news = "COVID-19 is still going on!";
  69. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  70. expectedNews = "GossipNewsAgency: " + news;
  71. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  72. news = "ape likes banana!";
  73. eventManager->fire(ls_std_test::GossipNewsEvent(news)); // event call
  74. expectedNews = "GossipNewsAgency: " + news;
  75. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  76. }
  77. }