EventListenerTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <memory>
  14. using ls::std::core::NullPointerException;
  15. using ls::std::event::EventListener;
  16. using ls::std::event::EventManager;
  17. using ::std::make_shared;
  18. using ::std::string;
  19. using test::event::Button;
  20. using test::event::OnClickEvent;
  21. using testing::Test;
  22. namespace
  23. {
  24. class EventListenerTest : public Test
  25. {
  26. public:
  27. EventListenerTest() = default;
  28. ~EventListenerTest() override = default;
  29. };
  30. TEST_F(EventListenerTest, getClassName)
  31. {
  32. ASSERT_STREQ("EventListener", EventListener().getClassName().c_str());
  33. }
  34. TEST_F(EventListenerTest, getId)
  35. {
  36. ASSERT_EQ(0, EventListener().getId());
  37. }
  38. TEST_F(EventListenerTest, setId)
  39. {
  40. auto listener = EventListener();
  41. ASSERT_EQ(0, listener.getId());
  42. listener.setId(17);
  43. ASSERT_EQ(17, listener.getId());
  44. }
  45. TEST_F(EventListenerTest, subscribe_with_missing_event_manager)
  46. {
  47. auto myButton = make_shared<Button>();
  48. EXPECT_THROW(
  49. {
  50. try
  51. {
  52. myButton->subscribe(OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
  53. }
  54. catch (const NullPointerException &_exception)
  55. {
  56. string actual = _exception.what();
  57. string expected = _exception.getName() + " thrown - no event manager is provided for OnClickEvent";
  58. EXPECT_STREQ(expected.c_str(), actual.c_str());
  59. throw;
  60. }
  61. },
  62. NullPointerException);
  63. }
  64. TEST_F(EventListenerTest, subscribe)
  65. {
  66. auto myButton = make_shared<Button>();
  67. ASSERT_TRUE(myButton->subscribe(OnClickEvent().supervisedBy(make_shared<EventManager>()), [myButton]() mutable { myButton->onClickEvent(); }));
  68. }
  69. TEST_F(EventListenerTest, subscribe_second_time)
  70. {
  71. auto myButton = make_shared<Button>();
  72. auto eventManager = make_shared<EventManager>();
  73. ASSERT_TRUE(myButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myButton]() mutable { myButton->onClickEvent(); }));
  74. ASSERT_FALSE(myButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myButton]() mutable { myButton->onClickEvent(); }));
  75. }
  76. TEST_F(EventListenerTest, unsubscribe_with_missing_event_manager)
  77. {
  78. auto myButton = make_shared<Button>();
  79. EXPECT_THROW(
  80. {
  81. try
  82. {
  83. myButton->unsubscribe(OnClickEvent());
  84. }
  85. catch (const NullPointerException &_exception)
  86. {
  87. string actual = _exception.what();
  88. string expected = _exception.getName() + " thrown - no event manager is provided for OnClickEvent";
  89. EXPECT_STREQ(expected.c_str(), actual.c_str());
  90. throw;
  91. }
  92. },
  93. NullPointerException);
  94. }
  95. TEST_F(EventListenerTest, unsubscribe)
  96. {
  97. auto myButton = make_shared<Button>();
  98. auto eventManager = make_shared<EventManager>();
  99. ASSERT_TRUE(myButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myButton]() mutable { myButton->onClickEvent(); }));
  100. ASSERT_TRUE(myButton->unsubscribe(OnClickEvent().supervisedBy(eventManager)));
  101. }
  102. TEST_F(EventListenerTest, unsubscribe_second_time)
  103. {
  104. auto myButton = make_shared<Button>();
  105. auto eventManager = make_shared<EventManager>();
  106. ASSERT_TRUE(myButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myButton]() mutable { myButton->onClickEvent(); }));
  107. ASSERT_TRUE(myButton->unsubscribe(OnClickEvent().supervisedBy(eventManager)));
  108. ASSERT_FALSE(myButton->unsubscribe(OnClickEvent().supervisedBy(eventManager)));
  109. }
  110. }