EventListenerTest.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <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().of(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().of(eventManager), [myButton]() mutable { myButton->onClickEvent(); }));
  74. ASSERT_FALSE(myButton->subscribe(OnClickEvent().of(eventManager), [myButton]() mutable { myButton->onClickEvent(); }));
  75. }
  76. }