diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index 5946fff6ee..30ec74c1eb 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -124,6 +124,7 @@ static void wait(int t) TestController::TestController() : _beginPos(Vec2::ZERO) +,_exitThread(false) { // add close menu auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, CC_CALLBACK_1(TestController::closeCallback, this) ); @@ -255,6 +256,77 @@ void TestController::onMouseScroll(Event *event) } #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) +void TestController::runAllTests(int fd) +{ + AppDelegate* app = (AppDelegate *)Application::getInstance(); + Scheduler *sched = Director::getInstance()->getScheduler(); + for (int i = 0; i < g_testCount; i++) + { + + // create the test scene and run it + std::string msg("autotest: running test:"); + msg += g_aTestNames[i].test_name; + send(fd, msg.c_str(), strlen(msg.c_str()),0); + send(fd, "\n",1,0); + + currentController = &g_aTestNames[i]; + sched->performFunctionInCocosThread( [&](){ + auto scene = currentController->callback(); + if(scene) + { + scene->runThisTest(); + scene->release(); + } + } ); + wait(1); + BaseTest* firstTest = app->getCurrentTest(); + if(firstTest == nullptr) + { + continue; + } + std::string t1(""); + t1 += firstTest->subtitle(); + send(fd, t1.c_str(), strlen(t1.c_str()),0); + send(fd, "\n",1,0); + wait(2); + + while(1) + { + if(_exitThread) + { + return; + } + //currentTest->nextCallback(nullptr); + sched->performFunctionInCocosThread( [&](){ + BaseTest *t = app->getCurrentTest(); + if(t != nullptr) + { + t->nextCallback(nullptr); + } + } ); + wait(1); + BaseTest * curTest = app->getCurrentTest(); + if(curTest == nullptr) + { + break; + } + std::string title(""); + title += curTest->subtitle(); + send(fd, title.c_str(), strlen(title.c_str()),0); + send(fd, "\n",1,0); + wait(2); + + if(t1 == title) + { + break; + } + } + } + std::string msg("autotest run successfully!"); + send(fd, msg.c_str(), strlen(msg.c_str()),0); + send(fd, "\n",1,0); + return; +} void TestController::addConsoleAutoTest() { auto console = Director::getInstance()->getConsole(); @@ -262,7 +334,7 @@ void TestController::addConsoleAutoTest() static struct Console::Command autotest = { "autotest", "testcpp autotest command, use -h to list available tests", - [](int fd, const std::string& args) + [this](int fd, const std::string& args) { Scheduler *sched = Director::getInstance()->getScheduler(); if(args == "help" || args == "-h") @@ -353,64 +425,16 @@ void TestController::addConsoleAutoTest() if(args == "run") { - for (int i = 0; i < g_testCount; i++) - { - // create the test scene and run it - std::string msg("autotest: running test:"); - msg += g_aTestNames[i].test_name; - send(fd, msg.c_str(), strlen(msg.c_str()),0); - send(fd, "\n",1,0); + _exitThread = false; + std::thread t = std::thread( &TestController::runAllTests, this, fd); + t.detach(); + return; + } - currentController = &g_aTestNames[i]; - sched->performFunctionInCocosThread( [&](){ - auto scene = currentController->callback(); - if(scene) - { - scene->runThisTest(); - scene->release(); - } - } ); - wait(1); - BaseTest* firstTest = app->getCurrentTest(); - if(firstTest == nullptr) - { - continue; - } - std::string t1(""); - t1 += firstTest->subtitle(); - send(fd, t1.c_str(), strlen(t1.c_str()),0); - send(fd, "\n",1,0); - wait(2); - - while(1) - { - //currentTest->nextCallback(nullptr); - sched->performFunctionInCocosThread( [&](){ - BaseTest *t = app->getCurrentTest(); - if(t != nullptr) - { - t->nextCallback(nullptr); - } - } ); - wait(1); - BaseTest * curTest = app->getCurrentTest(); - if(curTest == nullptr) - { - break; - } - std::string title(""); - title += curTest->subtitle(); - send(fd, title.c_str(), strlen(title.c_str()),0); - send(fd, "\n",1,0); - wait(2); - - if(t1 == title) - { - break; - } - } - } - std::string msg("autotest run successfully!"); + if(args == "stop") + { + _exitThread = true; + std::string msg("autotest: autotest stopped!"); send(fd, msg.c_str(), strlen(msg.c_str()),0); send(fd, "\n",1,0); return; diff --git a/tests/cpp-tests/Classes/controller.h b/tests/cpp-tests/Classes/controller.h index 136dd0cccc..74b73e9d4c 100644 --- a/tests/cpp-tests/Classes/controller.h +++ b/tests/cpp-tests/Classes/controller.h @@ -21,10 +21,12 @@ public: void addConsoleAutoTest(); void autorun(); void startAutoRun(); + void runAllTests(int fd); ssize_t readline(int fd, char* ptr, size_t maxlen); private: Vec2 _beginPos; Menu* _itemMenu; + bool _exitThread; }; #endif