123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2024-05-16
- * Changed: 2024-05-17
- *
- * */
- #include <gtest/gtest.h>
- #include <ls-std-event-test.hpp>
- #include <ls-std/ls-std-core.hpp>
- #include <ls-std/ls-std-event.hpp>
- #include <string>
- using ls::std::core::EventNotHandledException;
- using ls::std::event::EventManager;
- using ::std::make_shared;
- using ::std::string;
- using test::event::Button;
- using test::event::OnClickEvent;
- using testing::Test;
- namespace
- {
- class EventManagerTest : public Test
- {
- public:
- EventManagerTest() = default;
- ~EventManagerTest() override = default;
- };
- TEST_F(EventManagerTest, getClassName)
- {
- ASSERT_STREQ("EventManager", EventManager().getClassName().c_str());
- }
- TEST_F(EventManagerTest, getNextProvisionId)
- {
- auto eventManager = EventManager();
- ASSERT_EQ(1, eventManager.getNextProvisionId());
- ASSERT_EQ(1, eventManager.requestListenerId());
- ASSERT_EQ(2, eventManager.getNextProvisionId());
- }
- TEST_F(EventManagerTest, holdsListenerForEvent)
- {
- auto eventManager = make_shared<EventManager>();
- ASSERT_FALSE(eventManager->holdsListenerForEvent(1, OnClickEvent()));
- }
- TEST_F(EventManagerTest, invoke)
- {
- auto eventManager = make_shared<EventManager>();
- auto myButton = make_shared<Button>();
- myButton->subscribe(OnClickEvent().of(eventManager), [myButton]() mutable { myButton->onClickEvent(); });
- ASSERT_FALSE(myButton->isClicked());
- eventManager->invoke(OnClickEvent());
- ASSERT_TRUE(myButton->isClicked());
- }
- TEST_F(EventManagerTest, invoke_event_not_known)
- {
- auto eventManager = make_shared<EventManager>();
- EXPECT_THROW(
- {
- try
- {
- eventManager->invoke(OnClickEvent());
- }
- catch (const EventNotHandledException &_exception)
- {
- string actual = _exception.what();
- string expected = _exception.getName() + " thrown - event OnClickEvent is not known by event manager";
- EXPECT_STREQ(expected.c_str(), actual.c_str());
- throw;
- }
- },
- EventNotHandledException);
- }
- TEST_F(EventManagerTest, requestListenerId)
- {
- auto eventManager = EventManager();
- ASSERT_EQ(1, eventManager.getNextProvisionId());
- ASSERT_EQ(1, eventManager.requestListenerId());
- ASSERT_EQ(2, eventManager.getNextProvisionId());
- }
- TEST_F(EventManagerTest, subscribeListenerForEvent)
- {
- auto eventManager = make_shared<EventManager>();
- auto myButton = make_shared<Button>();
- eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
- ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
- }
- }
|