2013-06-04 13:33:52 +08:00
|
|
|
|
|
|
|
// C++ includes
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
2014-02-28 23:41:01 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2013-06-04 13:33:52 +08:00
|
|
|
// test inclues
|
2014-02-20 21:49:13 +08:00
|
|
|
#include "AppDelegate.h"
|
|
|
|
#include "BaseTest.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "controller.h"
|
|
|
|
#include "testResource.h"
|
|
|
|
#include "tests.h"
|
|
|
|
|
2014-03-22 21:17:34 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-02-19 17:55:32 +08:00
|
|
|
#include <unistd.h>
|
2014-05-12 21:47:55 +08:00
|
|
|
#include <sys/types.h>
|
2014-02-24 12:01:04 +08:00
|
|
|
#include <sys/socket.h>
|
2014-05-12 21:47:55 +08:00
|
|
|
#include <netdb.h>
|
2014-02-19 17:55:32 +08:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
2014-05-13 14:43:11 +08:00
|
|
|
#include <WS2tcpip.h>
|
2014-02-19 17:55:32 +08:00
|
|
|
#endif
|
2014-02-20 13:43:28 +08:00
|
|
|
#include "cocostudio/CocoStudio.h"
|
2014-05-08 14:35:45 +08:00
|
|
|
#include "UITest/UITest.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-19 16:13:32 +08:00
|
|
|
typedef struct _Controller{
|
2013-06-05 01:03:32 +08:00
|
|
|
const char *test_name;
|
|
|
|
std::function<TestScene*()> callback;
|
2014-02-19 16:13:32 +08:00
|
|
|
} Controller;
|
|
|
|
Controller g_aTestNames[] = {
|
2013-11-08 04:42:16 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// TESTS MUST BE ORDERED ALPHABETICALLY
|
|
|
|
// violators will be prosecuted
|
|
|
|
//
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "ActionManager", [](){return new ActionManagerTestScene(); } },
|
|
|
|
{ "Actions - Basic", [](){ return new ActionsTestScene(); } },
|
|
|
|
{ "Actions - Ease", [](){return new ActionsEaseTestScene();} },
|
|
|
|
{ "Actions - Progress", [](){return new ProgressActionsTestScene(); } },
|
|
|
|
{ "Audio - CocosDenshion", []() { return new CocosDenshionTestScene(); } },
|
|
|
|
{ "Box2d - Basic", []() { return new Box2DTestScene(); } },
|
|
|
|
{ "Box2d - TestBed", []() { return new Box2dTestBedScene(); } },
|
|
|
|
{ "Bugs", []() { return new BugsTestScene(); } },
|
|
|
|
{ "Chipmunk", []() { return new ChipmunkAccelTouchTestScene(); } },
|
|
|
|
{ "Click and Move", [](){return new ClickAndMoveTestScene(); } },
|
|
|
|
{ "Configuration", []() { return new ConfigurationTestScene(); } },
|
2014-03-22 21:17:34 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "Console", []() { return new ConsoleTestScene(); } },
|
2014-04-20 01:08:01 +08:00
|
|
|
|
2014-03-22 21:17:34 +08:00
|
|
|
#endif
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2013-06-04 13:33:52 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN)
|
2013-06-21 03:15:42 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_NACL)
|
2012-11-25 03:14:01 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
|
2012-04-19 14:35:52 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "Curl", []() { return new CurlTestScene(); } },
|
2013-06-02 10:28:46 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
2013-06-21 03:15:42 +08:00
|
|
|
#endif
|
2014-03-22 21:17:34 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "Current Language", []() { return new CurrentLanguageTestScene(); } },
|
|
|
|
{ "EventDispatcher", []() { return new EventDispatcherTestScene(); } },
|
|
|
|
{ "Effects - Advanced", []() { return new EffectAdvanceScene(); } },
|
|
|
|
{ "Effects - Basic", [](){return new EffectTestScene();} },
|
|
|
|
{ "Extensions", []() { return new ExtensionsTestScene(); } },
|
|
|
|
{ "FileUtils", []() { return new FileUtilsTestScene(); } },
|
|
|
|
{ "Fonts", []() { return new FontTestScene(); } },
|
|
|
|
{ "Interval", [](){return new IntervalTestScene(); } },
|
|
|
|
{ "Node: Clipping", []() { return new ClippingNodeTestScene(); } },
|
|
|
|
{ "Node: Draw", [](){return new DrawPrimitivesTestScene();} },
|
|
|
|
{ "Node: Label - New API", [](){return new AtlasTestSceneNew(); } },
|
|
|
|
{ "Node: Label - Old API", [](){return new AtlasTestScene(); } },
|
|
|
|
{ "Node: Layer", [](){return new LayerTestScene();} },
|
|
|
|
{ "Node: Menu", [](){return new MenuTestScene();} },
|
|
|
|
{ "Node: MotionStreak", [](){return new MotionStreakTestScene();} },
|
|
|
|
{ "Node: Node", [](){return new CocosNodeTestScene();} },
|
|
|
|
{ "Node: Parallax", [](){return new ParallaxTestScene(); } },
|
|
|
|
{ "Node: Particles", [](){return new ParticleTestScene(); } },
|
|
|
|
{ "Node: Physics", []() { return new PhysicsTestScene(); } },
|
|
|
|
{ "Node: RenderTexture", [](){return new RenderTextureScene(); } },
|
|
|
|
{ "Node: Scene", [](){return new SceneTestScene();} },
|
|
|
|
{ "Node: Spine", []() { return new SpineTestScene(); } },
|
|
|
|
{ "Node: Sprite", [](){return new SpriteTestScene(); } },
|
2014-05-19 05:49:16 +08:00
|
|
|
{ "Node: Sprite3D", [](){ return new Sprite3DTestScene(); }},
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "Node: TileMap", [](){return new TileMapTestScene(); } },
|
2014-06-06 17:33:27 +08:00
|
|
|
{ "Node: TileMapNew", [](){return new TileMapTestSceneNew(); } },
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "Node: Text Input", [](){return new TextInputTestScene(); } },
|
2014-05-19 05:49:16 +08:00
|
|
|
{ "Node: UI", [](){ return new UITestScene(); }},
|
2014-03-06 03:50:12 +08:00
|
|
|
{ "Mouse", []() { return new MouseTestScene(); } },
|
|
|
|
{ "MutiTouch", []() { return new MutiTouchTestScene(); } },
|
|
|
|
{ "Performance tests", []() { return new PerformanceTestScene(); } },
|
|
|
|
{ "Renderer", []() { return new NewRendererTestScene(); } },
|
|
|
|
{ "ReleasePool", [](){ return new ReleasePoolTestScene(); } },
|
|
|
|
{ "Rotate World", [](){return new RotateWorldTestScene(); } },
|
|
|
|
{ "Scheduler", [](){return new SchedulerTestScene(); } },
|
|
|
|
{ "Shader - Basic", []() { return new ShaderTestScene(); } },
|
|
|
|
{ "Shader - Sprite", []() { return new ShaderTestScene2(); } },
|
|
|
|
{ "Texture2D", [](){return new TextureTestScene(); } },
|
|
|
|
{ "TextureCache", []() { return new TextureCacheTestScene(); } },
|
|
|
|
{ "TexturePacker Encryption", []() { return new TextureAtlasEncryptionTestScene(); } },
|
|
|
|
{ "Touches", [](){return new PongScene();} },
|
|
|
|
{ "Transitions", [](){return new TransitionsTestScene();} },
|
|
|
|
{ "Unit Test", []() { return new UnitTestScene(); }},
|
|
|
|
{ "UserDefault", []() { return new UserDefaultTestScene(); } },
|
|
|
|
{ "Zwoptex", []() { return new ZwoptexTestScene(); } },
|
2013-06-04 13:33:52 +08:00
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-05 01:03:32 +08:00
|
|
|
static int g_testCount = sizeof(g_aTestNames) / sizeof(g_aTestNames[0]);
|
2014-02-19 16:13:32 +08:00
|
|
|
static Controller *currentController = nullptr;
|
2013-06-04 13:33:52 +08:00
|
|
|
#define LINE_SPACE 40
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
static Vec2 s_tCurPos = Vec2::ZERO;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-28 23:02:22 +08:00
|
|
|
//sleep for t seconds
|
|
|
|
static void wait(int t)
|
|
|
|
{
|
2014-02-28 23:41:01 +08:00
|
|
|
std::chrono::milliseconds dura( t * 1000 );
|
|
|
|
std::this_thread::sleep_for( dura );
|
2014-02-28 23:02:22 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
TestController::TestController()
|
2014-05-15 01:07:09 +08:00
|
|
|
: _beginPos(Vec2::ZERO)
|
2014-05-22 14:52:19 +08:00
|
|
|
,_exitThread(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// add close menu
|
2013-08-16 16:05:27 +08:00
|
|
|
auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, CC_CALLBACK_1(TestController::closeCallback, this) );
|
|
|
|
auto menu =Menu::create(closeItem, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setPosition( Vec2::ZERO );
|
|
|
|
closeItem->setPosition(Vec2( VisibleRect::right().x - 30, VisibleRect::top().y - 30));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// add menu items for tests
|
2014-03-26 23:33:58 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 24);
|
2013-06-20 14:17:10 +08:00
|
|
|
_itemMenu = Menu::create();
|
2013-06-04 13:33:52 +08:00
|
|
|
for (int i = 0; i < g_testCount; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-03-26 23:33:58 +08:00
|
|
|
auto label = Label::createWithTTF(ttfConfig, g_aTestNames[i].test_name);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(TestController::menuCallback, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-24 06:20:22 +08:00
|
|
|
_itemMenu->addChild(menuItem, i + 10000);
|
2014-05-15 01:07:09 +08:00
|
|
|
menuItem->setPosition( Vec2( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) ));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
_itemMenu->setContentSize(Size(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
|
2013-06-15 14:03:30 +08:00
|
|
|
_itemMenu->setPosition(s_tCurPos);
|
|
|
|
addChild(_itemMenu);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-24 06:20:22 +08:00
|
|
|
addChild(menu, 1);
|
2012-04-27 18:47:49 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
// Register Touch Event
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-03 18:22:03 +08:00
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(TestController::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(TestController::onTouchMoved, this);
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-10-05 01:07:39 +08:00
|
|
|
|
|
|
|
auto mouseListener = EventListenerMouse::create();
|
|
|
|
mouseListener->onMouseScroll = CC_CALLBACK_1(TestController::onMouseScroll, this);
|
2013-10-31 14:19:36 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TestController::~TestController()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void TestController::menuCallback(Ref * sender)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->purgeCachedData();
|
2013-06-04 13:47:28 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// get the userdata, it's the index of the menu item clicked
|
2013-08-16 16:05:27 +08:00
|
|
|
auto menuItem = static_cast<MenuItem *>(sender);
|
2014-01-20 15:03:30 +08:00
|
|
|
int idx = menuItem->getLocalZOrder() - 10000;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// create the test scene and run it
|
2013-08-16 16:05:27 +08:00
|
|
|
auto scene = g_aTestNames[idx].callback();
|
2013-06-04 13:47:28 +08:00
|
|
|
|
2013-11-18 03:43:32 +08:00
|
|
|
if (scene)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
scene->runThisTest();
|
|
|
|
scene->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void TestController::closeCallback(Ref * sender)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-03-22 21:17:34 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->end();
|
2012-04-19 14:35:52 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
exit(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool TestController::onTouchBegan(Touch* touch, Event *event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
_beginPos = touch->getLocation();
|
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TestController::onTouchMoved(Touch* touch, Event *event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto touchLocation = touch->getLocation();
|
2013-06-15 14:03:30 +08:00
|
|
|
float nMoveY = touchLocation.y - _beginPos.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto curPos = _itemMenu->getPosition();
|
2014-05-15 01:07:09 +08:00
|
|
|
auto nextPos = Vec2(curPos.x, curPos.y + nMoveY);
|
2012-10-23 17:48:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (nextPos.y < 0.0f)
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_itemMenu->setPosition(Vec2::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-04 13:33:52 +08:00
|
|
|
if (nextPos.y > ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_itemMenu->setPosition(Vec2(0, ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_itemMenu->setPosition(nextPos);
|
|
|
|
_beginPos = touchLocation;
|
2012-04-19 14:35:52 +08:00
|
|
|
s_tCurPos = nextPos;
|
|
|
|
}
|
2013-10-05 01:07:39 +08:00
|
|
|
|
|
|
|
void TestController::onMouseScroll(Event *event)
|
|
|
|
{
|
|
|
|
auto mouseEvent = static_cast<EventMouse*>(event);
|
2013-10-12 07:33:49 +08:00
|
|
|
float nMoveY = mouseEvent->getScrollY() * 6;
|
2013-10-05 01:07:39 +08:00
|
|
|
|
|
|
|
auto curPos = _itemMenu->getPosition();
|
2014-05-15 01:07:09 +08:00
|
|
|
auto nextPos = Vec2(curPos.x, curPos.y + nMoveY);
|
2013-10-05 01:07:39 +08:00
|
|
|
|
|
|
|
if (nextPos.y < 0.0f)
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_itemMenu->setPosition(Vec2::ZERO);
|
2013-10-05 01:07:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextPos.y > ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_itemMenu->setPosition(Vec2(0, ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
|
2013-10-05 01:07:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_itemMenu->setPosition(nextPos);
|
|
|
|
s_tCurPos = nextPos;
|
2013-10-31 14:19:36 +08:00
|
|
|
}
|
2014-02-19 16:13:32 +08:00
|
|
|
|
2014-04-20 01:08:01 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
|
2014-05-19 13:46:17 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-02-19 16:13:32 +08:00
|
|
|
void TestController::addConsoleAutoTest()
|
|
|
|
{
|
2014-02-22 22:37:12 +08:00
|
|
|
auto console = Director::getInstance()->getConsole();
|
2014-02-19 16:13:32 +08:00
|
|
|
|
|
|
|
static struct Console::Command autotest = {
|
|
|
|
"autotest",
|
2014-02-19 17:55:32 +08:00
|
|
|
"testcpp autotest command, use -h to list available tests",
|
2014-05-22 14:52:19 +08:00
|
|
|
[this](int fd, const std::string& args)
|
2014-02-19 16:13:32 +08:00
|
|
|
{
|
2014-02-22 22:37:12 +08:00
|
|
|
Scheduler *sched = Director::getInstance()->getScheduler();
|
2014-02-19 17:55:32 +08:00
|
|
|
if(args == "help" || args == "-h")
|
|
|
|
{
|
2014-02-20 13:43:28 +08:00
|
|
|
const char msg[] = "usage: autotest ActionsTest\n\tavailable tests: ";
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, msg, sizeof(msg),0);
|
|
|
|
send(fd, "\n",1,0);
|
2014-02-19 17:55:32 +08:00
|
|
|
for(int i = 0; i < g_testCount; i++)
|
|
|
|
{
|
2014-02-24 12:01:04 +08:00
|
|
|
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);
|
2014-02-19 17:55:32 +08:00
|
|
|
}
|
2014-02-20 13:43:28 +08:00
|
|
|
const char help_main[] = "\tmain, return to main menu\n";
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, help_main, sizeof(help_main),0);
|
2014-02-20 21:49:13 +08:00
|
|
|
|
|
|
|
const char help_next[] = "\tnext, run next test\n";
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, help_next, sizeof(help_next),0);
|
2014-02-20 21:49:13 +08:00
|
|
|
|
|
|
|
const char help_back[] = "\tback, run prev test\n";
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, help_back, sizeof(help_back),0);
|
2014-02-20 21:49:13 +08:00
|
|
|
|
|
|
|
const char help_restart[] = "\trestart, restart current test\n";
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, help_restart, sizeof(help_restart),0);
|
2014-02-19 17:55:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-02-20 13:43:28 +08:00
|
|
|
if(args == "main")
|
|
|
|
{
|
2014-02-22 22:37:12 +08:00
|
|
|
|
2014-02-20 13:43:28 +08:00
|
|
|
sched->performFunctionInCocosThread( [&]()
|
|
|
|
{
|
|
|
|
auto scene = Scene::create();
|
|
|
|
auto layer = new TestController();
|
|
|
|
scene->addChild(layer);
|
|
|
|
layer->release();
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
cocostudio::ArmatureDataManager::destroyInstance();
|
|
|
|
} );
|
2014-02-20 16:29:04 +08:00
|
|
|
return;
|
2014-02-20 13:43:28 +08:00
|
|
|
}
|
2014-02-20 21:49:13 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-02-22 22:37:12 +08:00
|
|
|
//currentTest->nextCallback(nullptr);
|
|
|
|
sched->performFunctionInCocosThread( [&](){
|
|
|
|
currentTest->nextCallback(nullptr);
|
|
|
|
} );
|
2014-02-20 21:49:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, msg_notest, sizeof(msg_notest),0);
|
2014-02-20 21:49:13 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(args == "back")
|
|
|
|
{
|
|
|
|
if(currentTest != nullptr)
|
|
|
|
{
|
2014-02-22 22:37:12 +08:00
|
|
|
sched->performFunctionInCocosThread( [&](){
|
|
|
|
currentTest->backCallback(nullptr);
|
|
|
|
} );
|
2014-02-20 21:49:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, msg_notest, sizeof(msg_notest),0);
|
2014-02-20 21:49:13 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(args == "restart")
|
|
|
|
{
|
|
|
|
if(currentTest != nullptr)
|
|
|
|
{
|
2014-02-22 22:37:12 +08:00
|
|
|
sched->performFunctionInCocosThread( [&](){
|
|
|
|
currentTest->restartCallback(nullptr);
|
|
|
|
} );
|
2014-02-20 21:49:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, msg_notest, sizeof(msg_notest),0);
|
2014-02-20 21:49:13 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-28 23:02:22 +08:00
|
|
|
if(args == "run")
|
|
|
|
{
|
2014-05-19 13:46:17 +08:00
|
|
|
_exitThread = false;
|
|
|
|
std::thread t = std::thread( &TestController::runAllTests, this, fd);
|
|
|
|
t.detach();
|
|
|
|
return;
|
|
|
|
}
|
2014-02-28 23:02:22 +08:00
|
|
|
|
2014-05-19 13:46:17 +08:00
|
|
|
if(args == "stop")
|
|
|
|
{
|
|
|
|
_exitThread = true;
|
|
|
|
std::string msg("autotest: autotest stopped!");
|
2014-05-13 17:20:12 +08:00
|
|
|
send(fd, msg.c_str(), strlen(msg.c_str()),0);
|
|
|
|
send(fd, "\n",1,0);
|
2014-04-08 11:29:42 +08:00
|
|
|
return;
|
2014-02-28 23:02:22 +08:00
|
|
|
}
|
|
|
|
|
2014-02-19 16:13:32 +08:00
|
|
|
for(int i = 0; i < g_testCount; i++)
|
|
|
|
{
|
2014-02-19 16:54:42 +08:00
|
|
|
if(args == g_aTestNames[i].test_name)
|
2014-02-19 16:13:32 +08:00
|
|
|
{
|
2014-04-08 11:29:42 +08:00
|
|
|
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);
|
2014-02-20 16:29:04 +08:00
|
|
|
|
2014-04-08 11:29:42 +08:00
|
|
|
|
|
|
|
sched->performFunctionInCocosThread( [&](){
|
|
|
|
auto scene = currentController->callback();
|
|
|
|
if(scene)
|
|
|
|
{
|
|
|
|
scene->runThisTest();
|
|
|
|
scene->release();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return;
|
2014-02-19 16:13:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-20 16:29:04 +08:00
|
|
|
|
|
|
|
//no match found,print warning message
|
|
|
|
std::string msg("autotest: could not find test:");
|
|
|
|
msg += args;
|
2014-02-24 12:01:04 +08:00
|
|
|
send(fd, msg.c_str(), strlen(msg.c_str()),0);
|
|
|
|
send(fd, "\n",1,0);
|
2014-02-19 16:13:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2014-02-22 22:37:12 +08:00
|
|
|
console->addCommand(autotest);
|
2014-02-20 17:58:21 +08:00
|
|
|
}
|
2014-05-12 21:47:55 +08:00
|
|
|
|
|
|
|
void TestController::startAutoRun()
|
|
|
|
{
|
|
|
|
|
|
|
|
std::thread t = std::thread( &TestController::autorun, this);
|
|
|
|
t.detach();
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:20:12 +08:00
|
|
|
ssize_t TestController::readline(int fd, char* ptr, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t n, rc;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
for( n = 0; n < maxlen - 1; n++ ) {
|
|
|
|
if( (rc = recv(fd, &c, 1, 0)) ==1 ) {
|
|
|
|
*ptr++ = c;
|
|
|
|
if(c == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if( rc == 0 ) {
|
|
|
|
return 0;
|
|
|
|
} else if( errno == EINTR ) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = 0;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:47:55 +08:00
|
|
|
void TestController::autorun()
|
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result, *rp;
|
|
|
|
int sfd, s;
|
|
|
|
|
|
|
|
/* Obtain address(es) matching host/port */
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
|
|
|
|
hints.ai_socktype = SOCK_STREAM; /* stream socket */
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
WSADATA wsaData;
|
|
|
|
WSAStartup(MAKEWORD(2, 2),&wsaData);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s = getaddrinfo("localhost", "5678", &hints, &result);
|
|
|
|
if (s != 0)
|
|
|
|
{
|
|
|
|
CCLOG("autotest: getaddrinfo error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
Try each address until we successfully connect(2).
|
|
|
|
If socket(2) (or connect(2)) fails, we (close the socket
|
|
|
|
and) try the next address. */
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
|
|
rp->ai_protocol);
|
|
|
|
if (sfd == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
|
|
|
break; /* Success */
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
closesocket(sfd);
|
|
|
|
#else
|
|
|
|
close(sfd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rp == NULL) { /* No address succeeded */
|
|
|
|
CCLOG("autotest: could not connect!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
|
|
|
|
std::string tmp = "autotest run\n";
|
|
|
|
|
|
|
|
char cmd[512];
|
|
|
|
|
|
|
|
strcpy(cmd, tmp.c_str());
|
|
|
|
wait(3);
|
|
|
|
send(sfd,cmd,strlen(cmd),0);
|
2014-05-13 14:16:06 +08:00
|
|
|
while(true)
|
|
|
|
{
|
2014-05-13 17:20:12 +08:00
|
|
|
char resp[512];
|
|
|
|
readline(sfd, resp, 512);
|
|
|
|
if(strcmp(resp, "autotest run successfully!\n") == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
wait(3);
|
2014-05-13 14:16:06 +08:00
|
|
|
}
|
2014-05-13 17:20:12 +08:00
|
|
|
|
|
|
|
tmp = "director end\n";
|
|
|
|
strcpy(cmd, tmp.c_str());
|
|
|
|
send(sfd,cmd,strlen(cmd),0);
|
|
|
|
wait(1);
|
2014-05-12 21:47:55 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
closesocket(sfd);
|
|
|
|
WSACleanup();
|
|
|
|
#else
|
|
|
|
close(sfd);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-22 21:17:34 +08:00
|
|
|
#endif
|
2014-02-22 22:58:24 +08:00
|
|
|
|