EventManagerTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <ls_std_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::EventManager eventManager{};
  27. ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
  28. }
  29. TEST_F(EventManagerTest, production_example)
  30. {
  31. std::string news, expectedNews{};
  32. ls_std::event_id seriousNewsEventId = ls_std_test::SeriousNewsEvent{""}.getId();
  33. ls_std::event_id gossipNewsEventId = ls_std_test::GossipNewsEvent{""}.getId();
  34. // create event handler
  35. std::shared_ptr<ls_std::EventHandler> seriousNewsEventHandler = std::make_shared<ls_std::EventHandler>(seriousNewsEventId); // event id
  36. std::shared_ptr<ls_std::EventHandler> gossipNewsEventHandler = std::make_shared<ls_std::EventHandler>(gossipNewsEventId); // event id
  37. // create and fill event manager with handler
  38. std::shared_ptr<ls_std::EventManager> eventManager = std::make_shared<ls_std::EventManager>();
  39. eventManager->addEventHandler(seriousNewsEventHandler);
  40. eventManager->addEventHandler(gossipNewsEventHandler);
  41. // create news agency (listener)
  42. std::shared_ptr<ls_std_test::DailyNewsAgency> dailyNews = std::make_shared<ls_std_test::DailyNewsAgency>();
  43. std::shared_ptr<ls_std_test::GossipNewsAgency> gossipNews = std::make_shared<ls_std_test::GossipNewsAgency>();
  44. // fire SeriousNewsEvent event with no effect
  45. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  46. ASSERT_TRUE(dailyNews->getNews().empty());
  47. ASSERT_TRUE(gossipNews->getNews().empty());
  48. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  49. eventManager->subscribe(seriousNewsEventId, dailyNews);
  50. eventManager->subscribe(seriousNewsEventId, gossipNews);
  51. news = "COVID-19 is still going on!";
  52. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  53. expectedNews = "DailyNewsAgency: " + news;
  54. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  55. expectedNews = "GossipNewsAgency: " + news;
  56. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  57. dailyNews->clear();
  58. gossipNews->clear();
  59. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  60. eventManager->unsubscribe(seriousNewsEventId, gossipNews);
  61. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  62. expectedNews = "DailyNewsAgency: " + news;
  63. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  64. ASSERT_TRUE(gossipNews->getNews().empty());
  65. dailyNews->clear();
  66. gossipNews->clear();
  67. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  68. eventManager->subscribe(gossipNewsEventId, gossipNews);
  69. eventManager->subscribe(seriousNewsEventId, gossipNews);
  70. news = "COVID-19 is still going on!";
  71. eventManager->fire(ls_std_test::SeriousNewsEvent(news)); // event call
  72. expectedNews = "GossipNewsAgency: " + news;
  73. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  74. news = "ape likes banana!";
  75. eventManager->fire(ls_std_test::GossipNewsEvent(news)); // event call
  76. expectedNews = "GossipNewsAgency: " + news;
  77. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  78. }
  79. }