EventManagerTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ::std::make_shared;
  17. using ::std::string;
  18. using test::event::Button;
  19. using test::event::ChangeColorEvent;
  20. using test::event::OnClickEvent;
  21. using testing::Test;
  22. namespace
  23. {
  24. class EventManagerTest : public Test
  25. {
  26. public:
  27. EventManagerTest() = default;
  28. ~EventManagerTest() override = default;
  29. };
  30. TEST_F(EventManagerTest, getClassName)
  31. {
  32. ASSERT_STREQ("EventManager", EventManager().getClassName().c_str());
  33. }
  34. TEST_F(EventManagerTest, getNextProvisionId)
  35. {
  36. auto eventManager = EventManager();
  37. ASSERT_EQ(1, eventManager.getNextProvisionId());
  38. ASSERT_EQ(1, eventManager.requestListenerId());
  39. ASSERT_EQ(2, eventManager.getNextProvisionId());
  40. }
  41. TEST_F(EventManagerTest, holdsListenerForEvent)
  42. {
  43. auto eventManager = make_shared<EventManager>();
  44. ASSERT_FALSE(eventManager->holdsListenerForEvent(1, OnClickEvent()));
  45. }
  46. TEST_F(EventManagerTest, invoke)
  47. {
  48. auto eventManager = make_shared<EventManager>();
  49. auto myBlueButton = make_shared<Button>();
  50. myBlueButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myBlueButton]() mutable { myBlueButton->onClickEvent(); });
  51. myBlueButton->subscribe(ChangeColorEvent().supervisedBy(eventManager), [myBlueButton]() mutable { myBlueButton->onChangeColorEvent(); });
  52. auto myRedButton = make_shared<Button>();
  53. auto myGreenButton = make_shared<Button>();
  54. myGreenButton->subscribe(ChangeColorEvent().supervisedBy(eventManager), [myGreenButton]() mutable { myGreenButton->onChangeColorEvent(); });
  55. // status without any event invoked
  56. ASSERT_FALSE(myBlueButton->isClicked());
  57. ASSERT_STREQ("black", myBlueButton->getColor().c_str());
  58. ASSERT_FALSE(myRedButton->isClicked());
  59. ASSERT_STREQ("black", myRedButton->getColor().c_str());
  60. ASSERT_FALSE(myGreenButton->isClicked());
  61. ASSERT_STREQ("black", myGreenButton->getColor().c_str());
  62. eventManager->invoke(OnClickEvent());
  63. eventManager->invoke(ChangeColorEvent());
  64. // status with events invoked
  65. ASSERT_TRUE(myBlueButton->isClicked());
  66. ASSERT_STREQ("blue", myBlueButton->getColor().c_str());
  67. ASSERT_FALSE(myRedButton->isClicked());
  68. ASSERT_STREQ("black", myRedButton->getColor().c_str());
  69. ASSERT_FALSE(myGreenButton->isClicked());
  70. ASSERT_STREQ("blue", myGreenButton->getColor().c_str());
  71. }
  72. TEST_F(EventManagerTest, invoke_event_not_known)
  73. {
  74. auto eventManager = make_shared<EventManager>();
  75. EXPECT_THROW(
  76. {
  77. try
  78. {
  79. eventManager->invoke(OnClickEvent());
  80. }
  81. catch (const EventNotHandledException &_exception)
  82. {
  83. string actual = _exception.what();
  84. string expected = _exception.getName() + " thrown - event OnClickEvent is not known by event manager";
  85. EXPECT_STREQ(expected.c_str(), actual.c_str());
  86. throw;
  87. }
  88. },
  89. EventNotHandledException);
  90. }
  91. TEST_F(EventManagerTest, requestListenerId)
  92. {
  93. auto eventManager = EventManager();
  94. ASSERT_EQ(1, eventManager.getNextProvisionId());
  95. ASSERT_EQ(1, eventManager.requestListenerId());
  96. ASSERT_EQ(2, eventManager.getNextProvisionId());
  97. }
  98. TEST_F(EventManagerTest, subscribeListenerForEvent)
  99. {
  100. auto eventManager = make_shared<EventManager>();
  101. auto myButton = make_shared<Button>();
  102. eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
  103. ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
  104. }
  105. TEST_F(EventManagerTest, unsubscribeListenerForEvent)
  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. eventManager->unsubscribeListenerForEvent(myButton, OnClickEvent());
  112. ASSERT_FALSE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
  113. }
  114. }