EventManagerTest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2024-05-16
  6. * Changed: 2024-05-30
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std-event-test.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-event.hpp>
  13. #include <string>
  14. using ls::std::core::EventNotHandledException;
  15. using ls::std::event::EventManager;
  16. using ls::std::event::EventParameter;
  17. using ::std::make_shared;
  18. using ::std::string;
  19. using test::event::Button;
  20. using test::event::ChangeColorEvent;
  21. using test::event::OnClickEvent;
  22. using testing::Test;
  23. namespace
  24. {
  25. class EventManagerTest : public Test
  26. {
  27. public:
  28. EventManagerTest() = default;
  29. ~EventManagerTest() override = default;
  30. static EventParameter generateParameter(const string &_color)
  31. {
  32. EventParameter parameter{};
  33. parameter.addParameter(_color);
  34. return parameter;
  35. }
  36. };
  37. TEST_F(EventManagerTest, getClassName)
  38. {
  39. ASSERT_STREQ("EventManager", EventManager().getClassName().c_str());
  40. }
  41. TEST_F(EventManagerTest, getNextProvisionId)
  42. {
  43. auto eventManager = EventManager();
  44. ASSERT_EQ(1, eventManager.getNextProvisionId());
  45. ASSERT_EQ(1, eventManager.requestListenerId());
  46. ASSERT_EQ(2, eventManager.getNextProvisionId());
  47. }
  48. TEST_F(EventManagerTest, holdsListenerForEvent)
  49. {
  50. auto eventManager = make_shared<EventManager>();
  51. ASSERT_FALSE(eventManager->holdsListenerForEvent(1, OnClickEvent()));
  52. }
  53. TEST_F(EventManagerTest, invoke)
  54. {
  55. auto eventManager = make_shared<EventManager>();
  56. auto myBlueButton = make_shared<Button>();
  57. myBlueButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myBlueButton]() mutable { myBlueButton->onClickEvent(); });
  58. myBlueButton->subscribe(ChangeColorEvent().supervisedBy(eventManager), [myBlueButton]() mutable { myBlueButton->onChangeColorEvent(); });
  59. auto myRedButton = make_shared<Button>();
  60. auto myGreenButton = make_shared<Button>();
  61. myGreenButton->subscribe(ChangeColorEvent().supervisedBy(eventManager), [myGreenButton]() mutable { myGreenButton->onChangeColorEvent(); });
  62. // status without any event invoked
  63. ASSERT_FALSE(myBlueButton->isClicked());
  64. ASSERT_STREQ("black", myBlueButton->getColor().c_str());
  65. ASSERT_FALSE(myRedButton->isClicked());
  66. ASSERT_STREQ("black", myRedButton->getColor().c_str());
  67. ASSERT_FALSE(myGreenButton->isClicked());
  68. ASSERT_STREQ("black", myGreenButton->getColor().c_str());
  69. eventManager->invoke(OnClickEvent());
  70. eventManager->invoke(ChangeColorEvent(), EventManagerTest::generateParameter("pink"));
  71. // status with events invoked
  72. ASSERT_TRUE(myBlueButton->isClicked());
  73. ASSERT_STREQ("pink", myBlueButton->getColor().c_str());
  74. ASSERT_FALSE(myRedButton->isClicked());
  75. ASSERT_STREQ("black", myRedButton->getColor().c_str());
  76. ASSERT_FALSE(myGreenButton->isClicked());
  77. ASSERT_STREQ("pink", myGreenButton->getColor().c_str());
  78. }
  79. TEST_F(EventManagerTest, invoke_event_not_known)
  80. {
  81. auto eventManager = make_shared<EventManager>();
  82. EXPECT_THROW(
  83. {
  84. try
  85. {
  86. eventManager->invoke(OnClickEvent());
  87. }
  88. catch (const EventNotHandledException &_exception)
  89. {
  90. string actual = _exception.what();
  91. string expected = _exception.getName() + " thrown - event OnClickEvent is not known by event manager";
  92. EXPECT_STREQ(expected.c_str(), actual.c_str());
  93. throw;
  94. }
  95. },
  96. EventNotHandledException);
  97. }
  98. TEST_F(EventManagerTest, requestListenerId)
  99. {
  100. auto eventManager = EventManager();
  101. ASSERT_EQ(1, eventManager.getNextProvisionId());
  102. ASSERT_EQ(1, eventManager.requestListenerId());
  103. ASSERT_EQ(2, eventManager.getNextProvisionId());
  104. }
  105. TEST_F(EventManagerTest, subscribeListenerForEvent)
  106. {
  107. auto eventManager = make_shared<EventManager>();
  108. auto myButton = make_shared<Button>();
  109. eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
  110. ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
  111. }
  112. TEST_F(EventManagerTest, unsubscribeListenerForEvent)
  113. {
  114. auto eventManager = make_shared<EventManager>();
  115. auto myButton = make_shared<Button>();
  116. eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
  117. ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
  118. eventManager->unsubscribeListenerForEvent(myButton, OnClickEvent());
  119. ASSERT_FALSE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
  120. }
  121. }