|
@@ -1,30 +1,26 @@
|
|
/*
|
|
/*
|
|
- * Author: Patrick-Christopher Mattulat
|
|
|
|
- * Company: Lynar Studios
|
|
|
|
- * E-Mail: webmaster@lynarstudios.com
|
|
|
|
- * Created: 2020-11-27
|
|
|
|
- * Changed: 2023-05-16
|
|
|
|
- *
|
|
|
|
- * */
|
|
|
|
|
|
+* Author: Patrick-Christopher Mattulat
|
|
|
|
+* Company: Lynar Studios
|
|
|
|
+* E-Mail: webmaster@lynarstudios.com
|
|
|
|
+* Created: 2024-05-16
|
|
|
|
+* Changed: 2024-05-30
|
|
|
|
+*
|
|
|
|
+* */
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <gtest/gtest.h>
|
|
#include <ls-std-event-test.hpp>
|
|
#include <ls-std-event-test.hpp>
|
|
|
|
+#include <ls-std/ls-std-core.hpp>
|
|
#include <ls-std/ls-std-event.hpp>
|
|
#include <ls-std/ls-std-event.hpp>
|
|
|
|
+#include <string>
|
|
|
|
|
|
using ls::std::core::EventNotHandledException;
|
|
using ls::std::core::EventNotHandledException;
|
|
-using ls::std::core::EventNotSubscribedException;
|
|
|
|
-using ls::std::core::IllegalArgumentException;
|
|
|
|
-using ls::std::core::type::event_id;
|
|
|
|
-using ls::std::event::Event;
|
|
|
|
-using ls::std::event::EventHandler;
|
|
|
|
using ls::std::event::EventManager;
|
|
using ls::std::event::EventManager;
|
|
-using std::make_shared;
|
|
|
|
-using std::shared_ptr;
|
|
|
|
-using std::string;
|
|
|
|
-using test::event::DailyNewsAgency;
|
|
|
|
-using test::event::GossipNewsAgency;
|
|
|
|
-using test::event::GossipNewsEvent;
|
|
|
|
-using test::event::SeriousNewsEvent;
|
|
|
|
|
|
+using ls::std::event::EventParameter;
|
|
|
|
+using ::std::make_shared;
|
|
|
|
+using ::std::string;
|
|
|
|
+using test::event::Button;
|
|
|
|
+using test::event::ChangeColorEvent;
|
|
|
|
+using test::event::OnClickEvent;
|
|
using testing::Test;
|
|
using testing::Test;
|
|
|
|
|
|
namespace
|
|
namespace
|
|
@@ -35,277 +31,116 @@ namespace
|
|
|
|
|
|
EventManagerTest() = default;
|
|
EventManagerTest() = default;
|
|
~EventManagerTest() override = default;
|
|
~EventManagerTest() override = default;
|
|
|
|
+
|
|
|
|
+ static EventParameter generateParameter(const string &_color)
|
|
|
|
+ {
|
|
|
|
+ EventParameter parameter{};
|
|
|
|
+ parameter.addParameter(_color);
|
|
|
|
+
|
|
|
|
+ return parameter;
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
TEST_F(EventManagerTest, getClassName)
|
|
TEST_F(EventManagerTest, getClassName)
|
|
{
|
|
{
|
|
- EventManager eventManager{};
|
|
|
|
- ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
|
|
|
|
|
|
+ ASSERT_STREQ("EventManager", EventManager().getClassName().c_str());
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, subscribe_empty_id)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, getNextProvisionId)
|
|
{
|
|
{
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.subscribe("", make_shared<DailyNewsAgency>());
|
|
|
|
- }
|
|
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- IllegalArgumentException);
|
|
|
|
|
|
+ auto eventManager = EventManager();
|
|
|
|
+ ASSERT_EQ(1, eventManager.getNextProvisionId());
|
|
|
|
+ ASSERT_EQ(1, eventManager.requestListenerId());
|
|
|
|
+ ASSERT_EQ(2, eventManager.getNextProvisionId());
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, subscribe_no_listener)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, holdsListenerForEvent)
|
|
{
|
|
{
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.subscribe("TMP_ID", nullptr);
|
|
|
|
- }
|
|
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- IllegalArgumentException);
|
|
|
|
|
|
+ auto eventManager = make_shared<EventManager>();
|
|
|
|
+ ASSERT_FALSE(eventManager->holdsListenerForEvent(1, OnClickEvent()));
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, subscribe_no_event_handler_available)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, invoke)
|
|
{
|
|
{
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.subscribe("TMP_DIR", make_shared<DailyNewsAgency>());
|
|
|
|
- }
|
|
|
|
- catch (const EventNotSubscribedException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- EventNotSubscribedException);
|
|
|
|
- }
|
|
|
|
|
|
+ auto eventManager = make_shared<EventManager>();
|
|
|
|
|
|
- TEST_F(EventManagerTest, unsubscribe_empty_id)
|
|
|
|
- {
|
|
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.unsubscribe("", make_shared<DailyNewsAgency>());
|
|
|
|
- }
|
|
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- IllegalArgumentException);
|
|
|
|
- }
|
|
|
|
|
|
+ auto myBlueButton = make_shared<Button>();
|
|
|
|
+ myBlueButton->subscribe(OnClickEvent().supervisedBy(eventManager), [myBlueButton]() mutable { myBlueButton->onClickEvent(); });
|
|
|
|
+ myBlueButton->subscribe(ChangeColorEvent().supervisedBy(eventManager), [myBlueButton]() mutable { myBlueButton->onChangeColorEvent(); });
|
|
|
|
+ auto myRedButton = make_shared<Button>();
|
|
|
|
+ auto myGreenButton = make_shared<Button>();
|
|
|
|
+ myGreenButton->subscribe(ChangeColorEvent().supervisedBy(eventManager), [myGreenButton]() mutable { myGreenButton->onChangeColorEvent(); });
|
|
|
|
|
|
- TEST_F(EventManagerTest, unsubscribe_no_listener)
|
|
|
|
- {
|
|
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.unsubscribe("TMP_ID", nullptr);
|
|
|
|
- }
|
|
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- IllegalArgumentException);
|
|
|
|
- }
|
|
|
|
|
|
+ // status without any event invoked
|
|
|
|
|
|
- TEST_F(EventManagerTest, addEventHandler)
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- ASSERT_TRUE(eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID")));
|
|
|
|
- }
|
|
|
|
|
|
+ ASSERT_FALSE(myBlueButton->isClicked());
|
|
|
|
+ ASSERT_STREQ("black", myBlueButton->getColor().c_str());
|
|
|
|
+ ASSERT_FALSE(myRedButton->isClicked());
|
|
|
|
+ ASSERT_STREQ("black", myRedButton->getColor().c_str());
|
|
|
|
+ ASSERT_FALSE(myGreenButton->isClicked());
|
|
|
|
+ ASSERT_STREQ("black", myGreenButton->getColor().c_str());
|
|
|
|
|
|
- TEST_F(EventManagerTest, addEventHandler_event_handler_already_exists)
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- ASSERT_TRUE(eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID")));
|
|
|
|
- ASSERT_FALSE(eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID")));
|
|
|
|
- }
|
|
|
|
|
|
+ eventManager->invoke(OnClickEvent());
|
|
|
|
+ eventManager->invoke(ChangeColorEvent(), EventManagerTest::generateParameter("pink"));
|
|
|
|
|
|
- TEST_F(EventManagerTest, addEventHandler_no_reference)
|
|
|
|
- {
|
|
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.addEventHandler(nullptr);
|
|
|
|
- }
|
|
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- IllegalArgumentException);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- TEST_F(EventManagerTest, fire_event_handler_not_available)
|
|
|
|
- {
|
|
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.fire(Event{"TMP_ID"});
|
|
|
|
- }
|
|
|
|
- catch (const EventNotHandledException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- EventNotHandledException);
|
|
|
|
- }
|
|
|
|
|
|
+ // status with events invoked
|
|
|
|
|
|
- TEST_F(EventManagerTest, hasEventHandler)
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID"));
|
|
|
|
- ASSERT_TRUE(eventManager.hasEventHandler("TMP_ID"));
|
|
|
|
|
|
+ ASSERT_TRUE(myBlueButton->isClicked());
|
|
|
|
+ ASSERT_STREQ("pink", myBlueButton->getColor().c_str());
|
|
|
|
+ ASSERT_FALSE(myRedButton->isClicked());
|
|
|
|
+ ASSERT_STREQ("black", myRedButton->getColor().c_str());
|
|
|
|
+ ASSERT_FALSE(myGreenButton->isClicked());
|
|
|
|
+ ASSERT_STREQ("pink", myGreenButton->getColor().c_str());
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, hasEventHandler_no_event_handler_available)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, invoke_event_not_known)
|
|
{
|
|
{
|
|
- EventManager eventManager{};
|
|
|
|
- ASSERT_FALSE(eventManager.hasEventHandler("TMP_ID"));
|
|
|
|
- }
|
|
|
|
|
|
+ auto eventManager = make_shared<EventManager>();
|
|
|
|
|
|
- TEST_F(EventManagerTest, hasEventHandler_empty_id)
|
|
|
|
- {
|
|
|
|
EXPECT_THROW(
|
|
EXPECT_THROW(
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- EventManager eventManager{};
|
|
|
|
- bool hasHandler = eventManager.hasEventHandler("");
|
|
|
|
|
|
+ eventManager->invoke(OnClickEvent());
|
|
}
|
|
}
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
|
|
+ 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;
|
|
throw;
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- IllegalArgumentException);
|
|
|
|
|
|
+ EventNotHandledException);
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, removeEventHandler)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, requestListenerId)
|
|
{
|
|
{
|
|
- EventManager eventManager{};
|
|
|
|
- auto eventHandler = make_shared<EventHandler>("TMP_ID");
|
|
|
|
- eventManager.addEventHandler(eventHandler);
|
|
|
|
|
|
+ auto eventManager = EventManager();
|
|
|
|
|
|
- ASSERT_TRUE(eventManager.removeEventHandler(eventHandler));
|
|
|
|
|
|
+ ASSERT_EQ(1, eventManager.getNextProvisionId());
|
|
|
|
+ ASSERT_EQ(1, eventManager.requestListenerId());
|
|
|
|
+ ASSERT_EQ(2, eventManager.getNextProvisionId());
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, removeEventHandler_no_event_handler_available)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, subscribeListenerForEvent)
|
|
{
|
|
{
|
|
- EventManager eventManager{};
|
|
|
|
- ASSERT_FALSE(eventManager.removeEventHandler(make_shared<EventHandler>("TMP_ID")));
|
|
|
|
- }
|
|
|
|
|
|
+ auto eventManager = make_shared<EventManager>();
|
|
|
|
+ auto myButton = make_shared<Button>();
|
|
|
|
|
|
- TEST_F(EventManagerTest, removeEventHandler_no_reference)
|
|
|
|
- {
|
|
|
|
- EXPECT_THROW(
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- EventManager eventManager{};
|
|
|
|
- eventManager.removeEventHandler(nullptr);
|
|
|
|
- }
|
|
|
|
- catch (const IllegalArgumentException &_exception)
|
|
|
|
- {
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- IllegalArgumentException);
|
|
|
|
|
|
+ eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
|
|
|
|
+ ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
|
|
}
|
|
}
|
|
|
|
|
|
- TEST_F(EventManagerTest, production_example)
|
|
|
|
|
|
+ TEST_F(EventManagerTest, unsubscribeListenerForEvent)
|
|
{
|
|
{
|
|
- string news{};
|
|
|
|
- string expectedNews{};
|
|
|
|
- event_id seriousNewsEventId = SeriousNewsEvent{""}.getId();
|
|
|
|
- event_id gossipNewsEventId = GossipNewsEvent{""}.getId();
|
|
|
|
-
|
|
|
|
- // create event handler
|
|
|
|
-
|
|
|
|
- auto seriousNewsEventHandler = make_shared<EventHandler>(seriousNewsEventId); // event id
|
|
|
|
- auto gossipNewsEventHandler = make_shared<EventHandler>(gossipNewsEventId); // event id
|
|
|
|
-
|
|
|
|
- // create and fill event manager with handler
|
|
|
|
-
|
|
|
|
auto eventManager = make_shared<EventManager>();
|
|
auto eventManager = make_shared<EventManager>();
|
|
- eventManager->addEventHandler(seriousNewsEventHandler);
|
|
|
|
- eventManager->addEventHandler(gossipNewsEventHandler);
|
|
|
|
-
|
|
|
|
- // create news agency (listener)
|
|
|
|
-
|
|
|
|
- auto dailyNews = make_shared<DailyNewsAgency>();
|
|
|
|
- auto gossipNews = make_shared<GossipNewsAgency>();
|
|
|
|
-
|
|
|
|
- // fire SeriousNewsEvent event with no effect
|
|
|
|
-
|
|
|
|
- eventManager->fire(SeriousNewsEvent(news)); // event call
|
|
|
|
- ASSERT_TRUE(dailyNews->getNews().empty());
|
|
|
|
- ASSERT_TRUE(gossipNews->getNews().empty());
|
|
|
|
-
|
|
|
|
- // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
|
|
|
|
-
|
|
|
|
- eventManager->subscribe(seriousNewsEventId, dailyNews);
|
|
|
|
- eventManager->subscribe(seriousNewsEventId, gossipNews);
|
|
|
|
- news = "COVID-19 is still going on!";
|
|
|
|
- eventManager->fire(SeriousNewsEvent(news)); // event call
|
|
|
|
-
|
|
|
|
- expectedNews = "DailyNewsAgency: " + news;
|
|
|
|
- ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
|
|
|
|
- expectedNews = "GossipNewsAgency: " + news;
|
|
|
|
- ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
|
|
|
|
-
|
|
|
|
- dailyNews->clear();
|
|
|
|
- gossipNews->clear();
|
|
|
|
-
|
|
|
|
- // unsubscribe SeriousNewsEvent from GossipNewsAgency
|
|
|
|
-
|
|
|
|
- eventManager->unsubscribe(seriousNewsEventId, gossipNews);
|
|
|
|
- eventManager->fire(SeriousNewsEvent(news)); // event call
|
|
|
|
-
|
|
|
|
- expectedNews = "DailyNewsAgency: " + news;
|
|
|
|
- ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
|
|
|
|
- ASSERT_TRUE(gossipNews->getNews().empty());
|
|
|
|
-
|
|
|
|
- dailyNews->clear();
|
|
|
|
- gossipNews->clear();
|
|
|
|
-
|
|
|
|
- // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
|
|
|
|
-
|
|
|
|
- eventManager->subscribe(gossipNewsEventId, gossipNews);
|
|
|
|
- eventManager->subscribe(seriousNewsEventId, gossipNews);
|
|
|
|
-
|
|
|
|
- news = "COVID-19 is still going on!";
|
|
|
|
- eventManager->fire(SeriousNewsEvent(news)); // event call
|
|
|
|
- expectedNews = "GossipNewsAgency: " + news;
|
|
|
|
- ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
|
|
|
|
|
|
+ auto myButton = make_shared<Button>();
|
|
|
|
|
|
- news = "ape likes banana!";
|
|
|
|
- eventManager->fire(GossipNewsEvent(news)); // event call
|
|
|
|
- expectedNews = "GossipNewsAgency: " + news;
|
|
|
|
- ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
|
|
|
|
|
|
+ eventManager->subscribeListenerForEvent(myButton, OnClickEvent(), [myButton]() mutable { myButton->onClickEvent(); });
|
|
|
|
+ ASSERT_TRUE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
|
|
|
|
+ eventManager->unsubscribeListenerForEvent(myButton, OnClickEvent());
|
|
|
|
+ ASSERT_FALSE(eventManager->holdsListenerForEvent(myButton->getId(), OnClickEvent()));
|
|
}
|
|
}
|
|
}
|
|
}
|