diff --git a/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 8b3d0a7cd2..c9f1f403d3 100644 --- a/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -24,6 +24,7 @@ std::function createFunctions[] = CL(DirectorEventTest), CL(GlobalZTouchTest), CL(StopPropagationTest), + CL(Issue4129), }; unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]); @@ -1088,3 +1089,66 @@ std::string StopPropagationTest::subtitle() const { return "Shouldn't crash and only blue block could be clicked"; } + +Issue4129::Issue4129() +: _bugFixed(false) +{ + _customlistener = _eventDispatcher->addCustomEventListener(EVENT_COME_TO_BACKGROUND, [this](EventCustom* event){ + + auto label = LabelTTF::create("Yeah, this issue was fixed.", "", 20); + label->setAnchorPoint(Point(0, 0.5f)); + label->setPosition(Point(VisibleRect::left())); + this->addChild(label); + + // After test, remove it. + _eventDispatcher->removeEventListener(_customlistener); + + _bugFixed = true; + }); + + auto removeAllTouchItem = MenuItemFont::create("Remove All Listeners", [this](Ref* sender){ + auto senderItem = static_cast(sender); + senderItem->setString("Only 'Reset' item could be clicked"); + + _eventDispatcher->removeAllEventListeners(); + + auto nextItem = MenuItemFont::create("Reset", [=](Ref* sender){ + CCASSERT(_bugFixed, "This issue was not fixed!"); + this->restartCallback(nullptr); + }); + + nextItem->setFontSizeObj(16); + nextItem->setPosition(VisibleRect::right() + Point(-100, -30)); + + auto menu2 = Menu::create(nextItem, NULL); + menu2->setPosition(Point(0, 0)); + menu2->setAnchorPoint(Point(0, 0)); + this->addChild(menu2); + + // Simulate to dispatch 'come to background' event + _eventDispatcher->dispatchCustomEvent(EVENT_COME_TO_BACKGROUND); + }); + + removeAllTouchItem->setFontSizeObj(16); + removeAllTouchItem->setPosition(VisibleRect::right() + Point(-100, 0)); + + auto menu = Menu::create(removeAllTouchItem, nullptr); + menu->setPosition(Point(0, 0)); + menu->setAnchorPoint(Point(0, 0)); + addChild(menu); +} + +Issue4129::~Issue4129() +{ + _eventDispatcher->removeEventListener(_customlistener); +} + +std::string Issue4129::title() const +{ + return "Issue 4129: Remove All Listeners"; +} + +std::string Issue4129::subtitle() const +{ + return "Should see 'Yeah, this issue was fixed.'"; +} diff --git a/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h b/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h index e24f8488f3..90b48bf22d 100644 --- a/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h +++ b/tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h @@ -165,4 +165,20 @@ protected: bool isPointInTopHalfAreaOfScreen(Point pt); }; +class Issue4129 : public EventDispatcherTestDemo +{ +public: + CREATE_FUNC(Issue4129); + Issue4129(); + virtual ~Issue4129(); + + virtual std::string title() const override; + virtual std::string subtitle() const override; + +private: + EventListenerCustom* _customlistener; + bool _bugFixed; +}; + + #endif /* defined(__samples__NewEventDispatcherTest__) */