From 2ddfc45feefc1548fa2a58f415359b9be2145f24 Mon Sep 17 00:00:00 2001 From: heliclei Date: Mon, 19 May 2014 13:46:17 +0800 Subject: [PATCH 1/2] support autotest stop --- tests/cpp-tests/Classes/controller.cpp | 142 +++++++++++++++---------- tests/cpp-tests/Classes/controller.h | 2 + 2 files changed, 85 insertions(+), 59 deletions(-) diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index 2eb8c52ce7..1099d576d0 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -122,7 +122,8 @@ static void wait(int t) } TestController::TestController() -: _beginPos(Vec2::ZERO) +: _beginPos(Vec2::ZERO), +_exitThread(false) { // add close menu auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, CC_CALLBACK_1(TestController::closeCallback, this) ); @@ -254,6 +255,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(); @@ -261,7 +333,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) + [&](int fd, const std::string& args) { Scheduler *sched = Director::getInstance()->getScheduler(); if(args == "help" || args == "-h") @@ -352,64 +424,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 From 5e4f481ae55760da274b6a05a306e63f68ad3f08 Mon Sep 17 00:00:00 2001 From: heliclei Date: Thu, 22 May 2014 14:52:19 +0800 Subject: [PATCH 2/2] capture this instead of reference in lambda --- tests/cpp-tests/Classes/controller.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index 1099d576d0..5b71f02a29 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -122,8 +122,8 @@ static void wait(int t) } TestController::TestController() -: _beginPos(Vec2::ZERO), -_exitThread(false) +: _beginPos(Vec2::ZERO) +,_exitThread(false) { // add close menu auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, CC_CALLBACK_1(TestController::closeCallback, this) ); @@ -333,7 +333,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")