/* * EventBusTest.cpp, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #include "StdInc.h" #include #include namespace test { using namespace ::testing; using namespace ::events; class EventExample : public Event { public: using PreHandler = SubscriptionRegistry::PreHandler; using PostHandler = SubscriptionRegistry::PostHandler; using ExecHandler = SubscriptionRegistry::ExecHandler; MOCK_CONST_METHOD0(isEnabled, bool()); public: static SubscriptionRegistry * getRegistry() { static auto Instance = std::make_unique>(); return Instance.get(); } friend class SubscriptionRegistry; }; class ListenerMock { public: MOCK_METHOD1(beforeEvent, void(EventExample &)); MOCK_METHOD1(onEvent, void(EventExample &)); MOCK_METHOD1(afterEvent, void(const EventExample &)); }; class EventBusTest : public Test { public: EventExample event1; EventExample event2; EventBus subject1; EventBus subject2; StrictMock listener; StrictMock listener1; StrictMock listener2; }; TEST_F(EventBusTest, ExecuteNoListeners) { EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true)); subject1.executeEvent(event1); } TEST_F(EventBusTest, ExecuteNoListenersWithHandler) { EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true)); EXPECT_CALL(listener, onEvent(Ref(event1))).Times(1); subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1)); } TEST_F(EventBusTest, ExecuteIgnoredSubscription) { EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true)); subject1.subscribeBefore(std::bind(&ListenerMock::beforeEvent, &listener, _1)); subject1.subscribeAfter(std::bind(&ListenerMock::afterEvent, &listener, _1)); EXPECT_CALL(listener, beforeEvent(_)).Times(0); EXPECT_CALL(listener, afterEvent(_)).Times(0); subject1.executeEvent(event1); } TEST_F(EventBusTest, ExecuteSequence) { EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true)); auto subscription1 = subject1.subscribeBefore(std::bind(&ListenerMock::beforeEvent, &listener1, _1)); auto subscription2 = subject1.subscribeAfter(std::bind(&ListenerMock::afterEvent, &listener1, _1)); auto subscription3 = subject1.subscribeBefore(std::bind(&ListenerMock::beforeEvent, &listener2, _1)); auto subscription4 = subject1.subscribeAfter(std::bind(&ListenerMock::afterEvent, &listener2, _1)); { InSequence sequence; EXPECT_CALL(listener1, beforeEvent(Ref(event1))).Times(1); EXPECT_CALL(listener2, beforeEvent(Ref(event1))).Times(1); EXPECT_CALL(listener, onEvent(Ref(event1))).Times(1); EXPECT_CALL(listener1, afterEvent(Ref(event1))).Times(1); EXPECT_CALL(listener2, afterEvent(Ref(event1))).Times(1); } subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1)); } TEST_F(EventBusTest, BusesAreIndependent) { EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true)); auto subscription1 = subject1.subscribeBefore(std::bind(&ListenerMock::beforeEvent, &listener1, _1)); auto subscription2 = subject1.subscribeAfter(std::bind(&ListenerMock::afterEvent, &listener1, _1)); auto subscription3 = subject2.subscribeBefore(std::bind(&ListenerMock::beforeEvent, &listener2, _1)); auto subscription4 = subject2.subscribeAfter(std::bind(&ListenerMock::afterEvent, &listener2, _1)); EXPECT_CALL(listener1, beforeEvent(_)).Times(1); EXPECT_CALL(listener2, beforeEvent(_)).Times(0); EXPECT_CALL(listener1, afterEvent(_)).Times(1); EXPECT_CALL(listener2, afterEvent(_)).Times(0); subject1.executeEvent(event1); } TEST_F(EventBusTest, DisabledTestDontExecute) { EXPECT_CALL(event1, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(false)); EXPECT_CALL(listener, onEvent(Ref(event1))).Times(0); subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1)); } TEST_F(EventBusTest, DisabledTestDontExecutePostHandler) { EXPECT_CALL(event1, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(false)); EXPECT_CALL(listener, onEvent(Ref(event1))).WillRepeatedly(Return()); EXPECT_CALL(listener1, afterEvent(Ref(event1))).Times(0); auto subscription1 = subject1.subscribeAfter(std::bind(&ListenerMock::afterEvent, &listener1, _1)); subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1)); } TEST_F(EventBusTest, DisabledTestExecutePreHandler) { EXPECT_CALL(event1, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(false)); EXPECT_CALL(listener, onEvent(Ref(event1))).WillRepeatedly(Return()); EXPECT_CALL(listener1, beforeEvent(Ref(event1))).Times(1); auto subscription1 = subject1.subscribeBefore(std::bind(&ListenerMock::beforeEvent, &listener1, _1)); subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1)); } //TODO: test sending event to destroyed event bus }