EventManagerTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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-17
  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::OnClickEvent;
  20. using testing::Test;
  21. namespace
  22. {
  23. class EventManagerTest : public Test
  24. {
  25. public:
  26. EventManagerTest() = default;
  27. ~EventManagerTest() override = default;
  28. };
  29. TEST_F(EventManagerTest, getClassName)
  30. {
  31. ASSERT_STREQ("EventManager", EventManager().getClassName().c_str());
  32. }
  33. TEST_F(EventManagerTest, getNextProvisionId)
  34. {
  35. auto eventManager = EventManager();
  36. ASSERT_EQ(1, eventManager.getNextProvisionId());
  37. ASSERT_EQ(1, eventManager.requestListenerId());
  38. ASSERT_EQ(2, eventManager.getNextProvisionId());
  39. }
  40. TEST_F(EventManagerTest, holdsListenerForEvent)
  41. {
  42. auto eventManager = make_shared<EventManager>();
  43. ASSERT_FALSE(eventManager->holdsListenerForEvent(1, OnClickEvent()));
  44. }
  45. TEST_F(EventManagerTest, invoke)
  46. {
  47. auto eventManager = make_shared<EventManager>();
  48. auto myButton = make_shared<Button>();
  49. myButton->subscribe(OnClickEvent().of(eventManager), [myButton]() mutable { myButton->onClickEvent(); });
  50. ASSERT_FALSE(myButton->isClicked());
  51. eventManager->invoke(OnClickEvent());
  52. ASSERT_TRUE(myButton->isClicked());
  53. }
  54. TEST_F(EventManagerTest, invoke_event_not_known)
  55. {
  56. auto eventManager = make_shared<EventManager>();
  57. EXPECT_THROW(
  58. {
  59. try
  60. {
  61. eventManager->invoke(OnClickEvent());
  62. }
  63. catch (const EventNotHandledException &_exception)
  64. {
  65. string actual = _exception.what();
  66. string expected = _exception.getName() + " thrown - event OnClickEvent is not known by event manager";
  67. EXPECT_STREQ(expected.c_str(), actual.c_str());
  68. throw;
  69. }
  70. },
  71. EventNotHandledException);
  72. }
  73. TEST_F(EventManagerTest, requestListenerId)
  74. {
  75. auto eventManager = EventManager();
  76. ASSERT_EQ(1, eventManager.getNextProvisionId());
  77. ASSERT_EQ(1, eventManager.requestListenerId());
  78. ASSERT_EQ(2, eventManager.getNextProvisionId());
  79. }
  80. TEST_F(EventManagerTest, subscribeListenerForEvent)
  81. {
  82. auto eventManager = make_shared<EventManager>();
  83. auto myButton = make_shared<Button>();
  84. eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
  85. ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
  86. }
  87. }