void TestController::addConsoleAutoTest() { auto console = Director::getInstance()->getConsole(); static struct Console::Command autotest = { "autotest", "testcpp autotest command, use -h to list available tests", [this](int fd, const std::string& args) { Scheduler *sched = Director::getInstance()->getScheduler(); if(args == "help" || args == "-h") { const char msg[] = "usage: autotest ActionsTest\n\tavailable tests: "; send(fd, msg, sizeof(msg),0); send(fd, "\n",1,0); for(int i = 0; i < g_testCount; i++) { send(fd, "\t",1,0); send(fd, g_aTestNames[i].test_name, strlen(g_aTestNames[i].test_name)+1,0); send(fd, "\n",1,0); } const char help_main[] = "\tmain, return to main menu\n"; send(fd, help_main, sizeof(help_main),0); const char help_next[] = "\tnext, run next test\n"; send(fd, help_next, sizeof(help_next),0); const char help_back[] = "\tback, run prev test\n"; send(fd, help_back, sizeof(help_back),0); const char help_restart[] = "\trestart, restart current test\n"; send(fd, help_restart, sizeof(help_restart),0); return; } if(args == "main") { sched->performFunctionInCocosThread( [&]() { auto scene = Scene::create(); auto layer = new TestController(); scene->addChild(layer); layer->release(); Director::getInstance()->replaceScene(scene); cocostudio::ArmatureDataManager::destroyInstance(); } ); return; } const char msg_notest[] = "autotest: can't detect running test.\n"; AppDelegate* app = (AppDelegate *)Application::getInstance(); BaseTest* currentTest = app->getCurrentTest(); if(args == "next") { if(currentTest != nullptr) { //currentTest->nextCallback(nullptr); sched->performFunctionInCocosThread( [&](){ currentTest->nextCallback(nullptr); } ); } else { send(fd, msg_notest, sizeof(msg_notest),0); } return; } if(args == "back") { if(currentTest != nullptr) { sched->performFunctionInCocosThread( [&](){ currentTest->backCallback(nullptr); } ); } else { send(fd, msg_notest, sizeof(msg_notest),0); } return; } if(args == "restart") { if(currentTest != nullptr) { sched->performFunctionInCocosThread( [&](){ currentTest->restartCallback(nullptr); } ); } else { send(fd, msg_notest, sizeof(msg_notest),0); } return; } if(args == "run") { _exitThread = false; std::thread t = std::thread( &TestController::runAllTests, this, fd); t.detach(); return; } 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; } for(int i = 0; i < g_testCount; i++) { if(args == g_aTestNames[i].test_name) { currentController = &g_aTestNames[i]; std::string msg("autotest: running test:"); msg += args; send(fd, msg.c_str(), strlen(msg.c_str()),0); send(fd, "\n",1,0); sched->performFunctionInCocosThread( [&](){ auto scene = currentController->callback(); if(scene) { scene->runThisTest(); scene->release(); } } ); return; } } //no match found,print warning message std::string msg("autotest: could not find test:"); msg += args; send(fd, msg.c_str(), strlen(msg.c_str()),0); send(fd, "\n",1,0); } }; console->addCommand(autotest); }