26#include "../testResource.h"
32using namespace cocos2d::ui;
38SchedulerTests::SchedulerTests()
83 CCLOG(
"autoremove scheduler: Time: %f",
accum);
88 CCLOG(
"autoremove scheduler: scheduler removed");
94 CCLOG(
"tick scheduler: This scheduler should not be removed");
99 return "Self-remove an scheduler";
104 return "1 scheduler will be autoremoved in 3 seconds. See console";
132 CCLOG(
"paused, tick1 and tick2 should called six times");
133 Director::getInstance()->getScheduler()->pauseTarget(
this);
138 return "Pause / Resume";
143 return "Scheduler should be paused after 3 seconds. See console";
166 auto sprite = Sprite::create(
"Images/grossinis_sister1.png");
168 this->addChild(sprite);
169 sprite->runAction(RepeatForever::create(RotateBy::create(3.0f, 360.0f)));
186 Director::getInstance()->getScheduler()->resumeTargets(
_pausedTargets);
188 SchedulerTestLayer::onExit();
203 log(
"Pausing, tick1 should be called six times and tick2 three times");
204 auto scheduler = Director::getInstance()->getScheduler();
208 CCASSERT(
_pausedTargets.size() == 2,
"Error: pausedTargets should have only 2 items");
211 getChildByTag(123)->scheduleOnce([
this](
float dt)
214 }, 2.0f,
"test resume");
220 auto director = Director::getInstance();
227 return "Pause / Resume All";
232 return "Everything will pause after 3s, then resume at 5s. See console";
255 auto s = Director::getInstance()->getWinSize();
257 auto sprite = Sprite::create(
"Images/grossinis_sister1.png");
258 sprite->setPosition(Vec2(s.width/2, s.height/2));
260 this->addChild(sprite);
261 sprite->runAction(RepeatForever::create(RotateBy::create(3.0f, 360.0f)));
272 Director::getInstance()->getScheduler()->resumeTargets(
_pausedTargets);
274 SchedulerTestLayer::onExit();
289 log(
"Pausing, tick1 and tick2 should be called three times");
290 auto director = Director::getInstance();
291 _pausedTargets = director->getScheduler()->pauseAllTargetsWithMinPriority(Scheduler::PRIORITY_NON_SYSTEM_MIN);
293 getChildByTag(123)->scheduleOnce([
this](
float dt)
296 }, 2.0f,
"test resume");
308 return "Pause / Resume All User scheduler";
313 return "ticks will pause after 3s, then resume at 5s. See console";
355 unscheduleAllCallbacks();
360 return "Unschedule All selectors";
365 return "All scheduled selectors will be unscheduled in 4 seconds. See console";
377 auto s = Director::getInstance()->getWinSize();
379 auto sprite = Sprite::create(
"Images/grossinis_sister1.png");
380 sprite->setPosition(Vec2(s.width/2, s.height/2));
381 this->addChild(sprite);
382 sprite->runAction(RepeatForever::create(RotateBy::create(3.0f, 360.0f)));
397 auto director = Director::getInstance();
398 director->getScheduler()->scheduleUpdate(director->getActionManager(), Scheduler::PRIORITY_SYSTEM,
false);
401 SchedulerTestLayer::onExit();
426 Director::getInstance()->getScheduler()->unscheduleAll();
432 return "Unschedule All selectors (HARD)";
437 return "Unschedules all user selectors after 4s. Action will stop. See console";
449 auto s = Director::getInstance()->getWinSize();
451 auto sprite = Sprite::create(
"Images/grossinis_sister1.png");
452 sprite->setPosition(Vec2(s.width/2, s.height/2));
453 this->addChild(sprite);
454 sprite->runAction(RepeatForever::create(RotateBy::create(3.0f, 360.0f)));
485 Director::getInstance()->getScheduler()->unscheduleAllWithMinPriority(Scheduler::PRIORITY_NON_SYSTEM_MIN);
490 return "Unschedule All user selectors";
495 return "Unschedules all user selectors after 4s. Action should not stop. See console";
534 return "Schedule from Schedule";
539 return "Will unschedule and schedule selectors in 4s. See console";
560 scheduleUpdateWithPriority(priority);
581 auto d =
new (std::nothrow)
TestNode();
586 auto b =
new (std::nothrow)
TestNode();
591 auto a =
new (std::nothrow)
TestNode();
596 auto c =
new (std::nothrow)
TestNode();
601 auto e =
new (std::nothrow)
TestNode();
606 auto f =
new (std::nothrow)
TestNode();
616 auto& children = getChildren();
618 for (
auto& c : children)
620 auto obj =
static_cast<Ref*
>(c);
621 auto node =
static_cast<Node*
>(obj);
627 node->unscheduleAllCallbacks();
633 return "Schedule update with priority";
638 return "3 scheduled updates. Priority should work. Stops in 4s. See console";
657 CCLOG(
"update called:%f", dt);
662 CCLOG(
"custom selector called:%f",dt);
667 log(
"SchedulerUpdateAndCustom::stopSelectors");
668 unscheduleAllCallbacks();
673 return "Schedule Update + custom selector";
678 return "Update + custom selector at the same time. Stops in 4s. See console";
695 CCLOG(
"update called:%f", dt);
713 return "Schedule Update in 2 sec";
718 return "Update schedules in 2 secs. Stops 2 sec later. See console";
737 return "Reschedule Selector";
742 return "Interval is 1 second, then 2, then 3...";
749 CCLOG(
"schedUpdate: %.4f", dt);
764 CCLOG(
"update is scheduled should begin after 3 seconds");
769 return "Schedule with delay of 3 sec, repeat 4 times";
774 return "After 5 x executed, method unscheduled. See console";
779 log(
"update called:%f", dt);
786 ControlSlider * slider = ControlSlider::create(
"extensions/sliderTrack2.png",
"extensions/sliderProgress2.png" ,
"extensions/sliderThumb.png");
790 slider->setMinimumValue(-3.0f);
791 slider->setMaximumValue(3.0f);
792 slider->setValue(1.0f);
799 ControlSlider* pSliderCtl =
static_cast<ControlSlider*
>(sender);
801 scale = pSliderCtl->getValue();
803 Director::getInstance()->getScheduler()->setTimeScale(scale);
810 auto s = Director::getInstance()->getWinSize();
813 auto jump1 = JumpBy::create(4, Vec2(-s.width+80,0.0f), 100, 4);
814 auto jump2 = jump1->reverse();
815 auto rot1 = RotateBy::create(4, 360*2);
816 auto rot2 = rot1->reverse();
818 auto seq3_1 = Sequence::create(jump2, jump1,
nullptr);
819 auto seq3_2 = Sequence::create(rot1, rot2,
nullptr);
820 auto spawn = Spawn::create(seq3_1, seq3_2,
nullptr);
821 auto action = Repeat::create(spawn, 50);
823 auto action2 = action->clone();
824 auto action3 = action->clone();
826 auto grossini = Sprite::create(
"Images/grossini.png");
827 auto tamara = Sprite::create(
"Images/grossinis_sister1.png");
828 auto kathia = Sprite::create(
"Images/grossinis_sister2.png");
830 grossini->setPosition(Vec2(40.0f,80.0f));
831 tamara->setPosition(Vec2(40.0f,80.0f));
832 kathia->setPosition(Vec2(40.0f,80.0f));
838 grossini->runAction(Speed::create(action, 0.5f));
839 tamara->runAction(Speed::create(action2, 1.5f));
840 kathia->runAction(Speed::create(action3, 1.0f));
842 auto emitter = ParticleFireworks::create();
843 emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(
s_stars1) );
847 _sliderCtl->setPosition(Vec2(s.width / 2.0f, s.height / 3.0f));
855 Director::getInstance()->getScheduler()->setTimeScale(1);
856 SchedulerTestLayer::onExit();
861 return "Scheduler timeScale Test";
866 return "Fast-forward and rewind using scheduler.timeScale";
874 ControlSlider *slider = ControlSlider::create(
"extensions/sliderTrack2.png",
"extensions/sliderProgress2.png" ,
"extensions/sliderThumb.png");
876 slider->addTargetWithActionForControlEvents(
this, cccontrol_selector(
TwoSchedulers::sliderAction), Control::EventType::VALUE_CHANGED);
881 slider->setMinimumValue(0.0f);
882 slider->setMaximumValue(2.0f);
884 slider->setValue(1.0f);
893 ControlSlider *slider =
static_cast<ControlSlider*
>(sender);
894 scale = slider->getValue();
897 sched1->setTimeScale(scale);
899 sched2->setTimeScale(scale);
906 auto s = Director::getInstance()->getWinSize();
909 auto jump1 = JumpBy::create(4, Vec2(0,0), 100, 4);
910 auto jump2 = jump1->reverse();
912 auto seq = Sequence::create(jump2, jump1,
nullptr);
913 auto action = RepeatForever::create(seq);
918 auto grossini = Sprite::create(
"Images/grossini.png");
920 grossini->setPosition(Vec2(s.width/2,100.0f));
921 grossini->runAction(action->clone());
923 auto defaultScheduler = Director::getInstance()->getScheduler();
930 sched1 =
new (std::nothrow) Scheduler();
932 defaultScheduler->scheduleUpdate(
sched1, 0,
false);
938 for(
unsigned int i=0; i < 10; i++ )
940 auto sprite = Sprite::create(
"Images/grossinis_sister1.png");
946 sprite->setPosition(Vec2(30+15*i,100.0f));
948 sprite->runAction(action->clone());
957 sched2 =
new (std::nothrow) Scheduler();
958 defaultScheduler->scheduleUpdate(
sched2, 0,
false);
964 for(
unsigned int i=0; i < 10; i++ ) {
965 auto sprite = Sprite::create(
"Images/grossinis_sister2.png");
971 sprite->setPosition(Vec2(s.width-30-15*i,100.0f));
973 sprite->runAction(action->clone());
990 auto defaultScheduler = Director::getInstance()->getScheduler();
991 defaultScheduler->unscheduleAllForTarget(
sched1);
992 defaultScheduler->unscheduleAllForTarget(
sched2);
1006 return "Two custom schedulers";
1011 return "Three schedulers. 2 custom + 1 default. Two different time scales";
1022 cocos2d::log(
"Delete TestNode (should not crash)");
1023 this->unscheduleAllCallbacks();
1049 testNode->removeFromParentAndCleanup(
false);
1052 testNode->unscheduleAllCallbacks();
1065 return "Issue #2268";
1070 return "Should not crash";
1080 Size widgetSize = getContentSize();
1082 auto status_text = Text::create(
"Checking..",
"fonts/Marker Felt.ttf", 18);
1083 status_text->setColor(Color3B(255, 255, 255));
1084 status_text->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
1085 addChild(status_text);
1088 auto verified = std::make_shared<bool>();
1091 _scheduler->schedule([
this, verified](
float dt){
1092 log(
"SchedulerIssueWithReschedule - first timer");
1094 _scheduler->schedule([verified](
float dt){
1095 log(
"SchedulerIssueWithReschedule - second timer. OK");
1097 },
this, 0.1f, 0, 0,
false,
"test_timer");
1099 },
this, 0.1f, 0, 0,
false,
"test_timer");
1101 _scheduler->schedule([verified, status_text](
float dt){
1104 log(
"SchedulerIssueWithReschedule - test OK");
1105 status_text->setString(
"OK");
1106 status_text->setColor(Color3B(0, 255, 0));
1110 log(
"SchedulerIssueWithReschedule - test failed!");
1111 status_text->setString(
"Failed");
1112 status_text->setColor(Color3B(255, 0, 0));
1115 },
this, 0.5f, 0, 0,
false,
"test_verify_timer");
1120 return "Issue with reschedule";
1125 return "reschedule issue with same key";
1137 return "ScheduleCallbackTest";
1142 return "\n\n\n\nPlease see console.\n\
1143schedule(lambda, ...)\n\
1144schedule(CC_CALLBACK_1(XXX::member_function), this), this, ...)\n\
1145schedule(global_function, ...)\n\
1151 log(
"In the callback of schedule(global_function, ...), dt = %f", dt);
1158 _scheduler->schedule([](
float dt){
1159 log(
"In the callback of schedule(lambda, ...), dt = %f", dt);
1160 },
this, 1.0f,
false,
"lambda");
1169 log(
"In the callback of schedule(CC_CALLBACK_1(XXX::member_function), this), this, ...), dt = %f", dt);
1177 return "ScheduleUpdatePriorityTest";
1182 return "click to change update priority with random value";
1187 int priority =
static_cast<int>(CCRANDOM_0_1() * 11) - 5;
1188 CCLOG(
"change update priority to %d", priority);
1189 scheduleUpdateWithPriority(priority);
1199 auto listener = EventListenerTouchOneByOne::create();
1201 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
1220 this->scheduleOnce([](
float ){
1221 log(
"SchedulerIssue10232:Schedules a lambda function");
1222 }, 0.25f,
"SchedulerIssue10232");
1227 log(
"SchedulerIssue10232:Schedules a selector");
1232 return "Issue #10232";
1237 return "Should not crash";
1244 _sprite = Sprite::create(
"Images/grossinis_sister1.png");
1247 this->scheduleUpdate();
1252 SchedulerTestLayer::onExit();
1253 this->unscheduleUpdate();
1257 Director::getInstance()->getScheduler()->performFunctionInCocosThread([
this] () {
1260 Director::getInstance()->getScheduler()->performFunctionInCocosThread([
this] () {
1263 Director::getInstance()->getScheduler()->performFunctionInCocosThread([
this] () {
1266 Director::getInstance()->getScheduler()->performFunctionInCocosThread([
this] () {
1269 Director::getInstance()->getScheduler()->performFunctionInCocosThread([
this] () {
1273 Director::getInstance()->getScheduler()->removeAllFunctionsToBePerformedInCocosThread();
1278 return "Removing pending main thread tasks";
1283 return "Sprite should be visible";
1329 return "Issue #17149";
1334 return "see console, result should be 'i'm ClassB: 456'";
1346 CCLOG(
"Address one: %p", classa);
1347 Director::getInstance()->getScheduler()->scheduleUpdate(classa, 1,
false);
1348 Director::getInstance()->getScheduler()->unscheduleUpdate(classa);
1351 CCLOG(
"Address one: %p", classb);
1352 Director::getInstance()->getScheduler()->scheduleUpdate(classb, 1,
false);
1365 CCLOG(
"i'm ClassA: %d%d%d", _member1, _member2, _member3);
1376 CCLOG(
"i'm ClassB: %d%d%d", _member1, _member2, _member3);
1378 Director::getInstance()->getScheduler()->unscheduleUpdate(
this);
1389 return "RemoveEntryWhileUpdate";
1394 return "see console, error message must not be shown.";
1400 for (
auto i = 0; i < 500; ++i)
1405 nextObj = _testvector[i - 1];
1407 auto obj =
new TestClass(i, nextObj, getScheduler());
1408 _testvector.push_back(obj);
1409 getScheduler()->scheduleUpdate(obj, 500 - i,
false);
1415 for (
auto obj: _testvector)
1417 getScheduler()->unscheduleUpdate(obj);
1420 _testvector.clear();
1421 SchedulerTestLayer::onExit();
1427, _scheduler(scheduler)
1435 CCLOG(
"Error: cleaned object must not be called.");
1439 if (_index % 50 == 1 && _nextObj !=
nullptr)
1441 _scheduler->unscheduleUpdate(_nextObj);
1442 _nextObj->_cleanedUp =
true;
1454 return "RemoveSelectorDuringCall";
1459 return "see console, error message must not be shown.";
1466 Scheduler*
const scheduler( Director::getInstance()->getScheduler() );
1468 scheduler->setTimeScale( 10 );
1471 0.01f, CC_REPEAT_FOREVER, 0.0f, !isRunning());
1478 Scheduler*
const scheduler( Director::getInstance()->getScheduler() );
1480 scheduler->setTimeScale( 1 );
1481 scheduler->unschedule
1485 SchedulerTestLayer::onExit();
1492 cocos2d::log(
"Error: unscheduled callback must not be called.");
1498 Scheduler*
const scheduler( Director::getInstance()->getScheduler() );
1499 scheduler->unschedule
#define ADD_TEST_CASE(__className__)
static void ScheduleCallbackTest_global_callback(float dt)
virtual std::string title() const override
virtual void onEnter() override
virtual std::string subtitle() const override
void schedUpdate(float dt)
virtual std::string subtitle() const override
virtual std::string title() const override
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
virtual std::string subtitle() const override
virtual void update(float dt) override
virtual std::string title() const override
virtual std::string title() const override
virtual void onEnter() override
void autoremove(float dt)
virtual std::string subtitle() const override
virtual std::string title() const override
void update(float dt) override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
void update(float dt) override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void update(float dt) override
virtual std::string subtitle() const override
virtual std::string title() const override
void update(float dt) override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void update(float delta) override
virtual ~SchedulerPauseResumeAll()
virtual void onEnter() override
std::set< void * > _pausedTargets
virtual std::string title() const override
SchedulerPauseResumeAll()
virtual std::string subtitle() const override
virtual void onExit() override
virtual std::string subtitle() const override
virtual ~SchedulerPauseResumeAllUser()
SchedulerPauseResumeAllUser()
virtual void onEnter() override
virtual void onExit() override
std::set< void * > _pausedTargets
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void onEnter() override
TestClass(int index, TestClass *nextObj, cocos2d::Scheduler *scheduler)
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onExit() override
virtual void onEnter() override
virtual std::string title() const override
virtual void onEnter() override
virtual void onExit() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string title() const override
virtual std::string subtitle() const override
void scheduleAndUnschedule(float dt)
cocos2d::extension::ControlSlider * _sliderCtl
void sliderAction(cocos2d::Ref *sender, cocos2d::extension::Control::EventType controlEvent)
virtual std::string title() const override
cocos2d::extension::ControlSlider * sliderCtl()
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual void onExit() override
virtual std::string title() const override
virtual void onEnter() override
bool _actionManagerActive
void unscheduleAll(float dt)
virtual std::string title() const override
virtual std::string subtitle() const override
void unscheduleAll(float dt)
virtual void onEnter() override
virtual std::string title() const override
void unscheduleAll(float dt)
virtual void onEnter() override
virtual std::string subtitle() const override
void stopSelectors(float dt)
void update(float dt) override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void onEnter() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
void update(float dt) override
void schedUpdate(float dt)
void stopUpdate(float dt)
virtual std::string subtitle() const override
virtual void onEnter() override
void removeUpdates(float dt)
virtual std::string title() const override
virtual void onEnter() override
static TestController * getInstance()
virtual void update(float dt) override
void initWithString(const std::string &str, int priority)
cocos2d::Scheduler * sched2
cocos2d::Scheduler * sched1
cocos2d::ActionManager * actionManager2
cocos2d::extension::ControlSlider * sliderCtl()
cocos2d::ActionManager * actionManager1
cocos2d::extension::ControlSlider * sliderCtl2
cocos2d::extension::ControlSlider * sliderCtl1
virtual std::string subtitle() const override
virtual std::string title() const override
void sliderAction(cocos2d::Ref *sender, cocos2d::extension::Control::EventType controlEvent)
static cocos2d::Vec2 top()
static cocos2d::Vec2 center()
static const char s_stars1[]