axmol/samples/Cpp/TestCpp/Classes/ExtensionsTest/ExtensionsTest.cpp

164 lines
5.2 KiB
C++
Raw Normal View History

#include "ExtensionsTest.h"
#include "../testResource.h"
#include "NotificationCenterTest/NotificationCenterTest.h"
#include "ControlExtensionTest/CCControlSceneManager.h"
#include "CocosBuilderTest/CocosBuilderTest.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN) && (CC_TARGET_PLATFORM != CC_PLATFORM_NACL)
#include "NetworkTest/HttpClientTest.h"
#endif
#include "TableViewTest/TableViewTestScene.h"
2013-06-04 17:38:43 +08:00
#include "ComponentsTest/ComponentsTestScene.h"
#include "ArmatureTest/ArmatureScene.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "NetworkTest/WebSocketTest.h"
Implements a socket.io client extension, adds a test case and updates the android makefiles. Contains the following tasks: -initial socket.io extension commit -sioclientimpl subclasses websocket:delegate to respond to websocket events -implement static connect methods and basic client creation -move SocketIO class files into correct extension folder (network) -create SocketIO test in TestCpp -update project references -add missing static modifier to connect method -implement basic test methods -update extensions tests with SocketIO test entry -implement basic handshake and opensocket methods for SocketIO -add Delegate class to handle callbacks, implement virtual Delegate methods in test -implement socket and client registries for lookup when connecting to hosts and endpoints -connect delegate onOpen method by separating impl creation from connection -update test to demonstrate onOpen callback -create send and emit methods, move SIOClient into header file, add send and emit methods to test -implement basic socket.io message parsing -improve logging for events and messages -add logic to pull event name from payload -schedule heartbeat to keep connection alive, scheduled for 90% of the heartbeat interval from the server for safety -add onConnect handler to to catch socket.io onconnect vs websocket onopen -add disconnect and disconnectFromEndpoint methods to properly disconnect and destroy objects -modify SIOClientImpl to track _uri for easier lookup in registries -connect handler for onMessage to message event from socket.io, modify onError handler to take a string instead of WebSocket error code -create SIOEvent callback type, implement event registry in clients, add test for event registration and callback -update SIOEvent to use std::function and c++11, utilize cocos2d CC_CALLBACK method to bind selectors, this ensures that the *this reference is properly passed -check for connect before sending or emitting in the client, cleanup some codes -change connect logic to reuse existing socket connections instead of opening a new one -implements get and set Tag methods for clients for easy reference -improve endpoint handling, add endpoint tests to test layer -additional error handling within socket disconnect error and failure to open connection -fixes extracting endpoint from socket.io messages (in cases of the connect message, where there is 1 less colon for the connect message to the default namespace). Also fixes connecting to the default namespace "/" in the connectToEndpoint method -add disconnect and onClose handlers to client so that onClose is called in the delegate -add disconnect test methods to test layers -change c-style casts to static_casts when using a CCDICT_FOREACH -remove some unneeded namespace completion -add usage documentation -add handling for disconnect from server, cleanup some codes -update comments and documentation in the socketiotest -update includes so the NDK doesn't complain when compiling with c++11 -add socketio.cpp and test.cpp to the android makefiles -update test URL to my public server, test script can also be found in my repo at https://github.com/hannon235/socket.io-testserver.git Signed-off-by: Chris Hannon <himynameschris@gmail.com>
2013-06-26 10:40:01 +08:00
#include "NetworkTest/SocketIOTest.h"
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
#include "EditBoxTest/EditBoxTest.h"
#endif
#include "Scale9SpriteTest/Scale9SpriteTest.h"
enum
{
LINE_SPACE = 40,
kItemTagBasic = 1000,
};
static struct {
const char *name;
std::function<void(Object* sender)> callback;
} g_extensionsTests[] = {
{ "NotificationCenterTest", [](Object* sender) { runNotificationCenterTest(); }
},
{ "Scale9SpriteTest", [](Object* sender) {
S9SpriteTestScene* pScene = new S9SpriteTestScene();
if (pScene)
{
pScene->runThisTest();
pScene->release();
}
}
},
{ "CCControlButtonTest", [](Object *sender){
ControlSceneManager* pManager = ControlSceneManager::sharedControlSceneManager();
Scene* pScene = pManager->currentControlScene();
Director::getInstance()->replaceScene(pScene);
}},
{ "CocosBuilderTest", [](Object *sender) {
TestScene* pScene = new CocosBuilderTestScene();
if (pScene)
{
pScene->runThisTest();
pScene->release();
}
}},
#if (CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN) && (CC_TARGET_PLATFORM != CC_PLATFORM_NACL)
{ "HttpClientTest", [](Object *sender){ runHttpClientTest();}
},
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
{ "WebSocketTest", [](Object *sender){ runWebSocketTest();}
},
Implements a socket.io client extension, adds a test case and updates the android makefiles. Contains the following tasks: -initial socket.io extension commit -sioclientimpl subclasses websocket:delegate to respond to websocket events -implement static connect methods and basic client creation -move SocketIO class files into correct extension folder (network) -create SocketIO test in TestCpp -update project references -add missing static modifier to connect method -implement basic test methods -update extensions tests with SocketIO test entry -implement basic handshake and opensocket methods for SocketIO -add Delegate class to handle callbacks, implement virtual Delegate methods in test -implement socket and client registries for lookup when connecting to hosts and endpoints -connect delegate onOpen method by separating impl creation from connection -update test to demonstrate onOpen callback -create send and emit methods, move SIOClient into header file, add send and emit methods to test -implement basic socket.io message parsing -improve logging for events and messages -add logic to pull event name from payload -schedule heartbeat to keep connection alive, scheduled for 90% of the heartbeat interval from the server for safety -add onConnect handler to to catch socket.io onconnect vs websocket onopen -add disconnect and disconnectFromEndpoint methods to properly disconnect and destroy objects -modify SIOClientImpl to track _uri for easier lookup in registries -connect handler for onMessage to message event from socket.io, modify onError handler to take a string instead of WebSocket error code -create SIOEvent callback type, implement event registry in clients, add test for event registration and callback -update SIOEvent to use std::function and c++11, utilize cocos2d CC_CALLBACK method to bind selectors, this ensures that the *this reference is properly passed -check for connect before sending or emitting in the client, cleanup some codes -change connect logic to reuse existing socket connections instead of opening a new one -implements get and set Tag methods for clients for easy reference -improve endpoint handling, add endpoint tests to test layer -additional error handling within socket disconnect error and failure to open connection -fixes extracting endpoint from socket.io messages (in cases of the connect message, where there is 1 less colon for the connect message to the default namespace). Also fixes connecting to the default namespace "/" in the connectToEndpoint method -add disconnect and onClose handlers to client so that onClose is called in the delegate -add disconnect test methods to test layers -change c-style casts to static_casts when using a CCDICT_FOREACH -remove some unneeded namespace completion -add usage documentation -add handling for disconnect from server, cleanup some codes -update comments and documentation in the socketiotest -update includes so the NDK doesn't complain when compiling with c++11 -add socketio.cpp and test.cpp to the android makefiles -update test URL to my public server, test script can also be found in my repo at https://github.com/hannon235/socket.io-testserver.git Signed-off-by: Chris Hannon <himynameschris@gmail.com>
2013-06-26 10:40:01 +08:00
{ "SocketIOTest", [](Object *sender){ runSocketIOTest();}
},
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
{ "EditBoxTest", [](Object *sender){ runEditBoxTest();}
},
#endif
{ "TableViewTest", [](Object *sender){ runTableViewTest();}
},
{ "CommponentTest", [](Object *sender) { runComponentsTestLayerTest(); }
},
{ "ArmatureTest", [](Object *sender) { ArmatureTestScene *pScene = new ArmatureTestScene();
pScene->runThisTest();
pScene->release();
}
},
};
static const int g_maxTests = sizeof(g_extensionsTests) / sizeof(g_extensionsTests[0]);
static Point s_tCurPos = Point::ZERO;
////////////////////////////////////////////////////////
//
// ExtensionsMainLayer
//
////////////////////////////////////////////////////////
void ExtensionsMainLayer::onEnter()
{
Layer::onEnter();
Size s = Director::getInstance()->getWinSize();
_itemMenu = Menu::create();
_itemMenu->setPosition( Point::ZERO );
MenuItemFont::setFontName("Arial");
MenuItemFont::setFontSize(24);
for (int i = 0; i < g_maxTests; ++i)
{
MenuItemFont* pItem = MenuItemFont::create(g_extensionsTests[i].name, g_extensionsTests[i].callback);
2013-07-12 14:11:55 +08:00
pItem->setPosition(Point(s.width / 2, s.height - (i + 1) * LINE_SPACE));
_itemMenu->addChild(pItem, kItemTagBasic + i);
}
setTouchEnabled(true);
addChild(_itemMenu);
}
void ExtensionsMainLayer::ccTouchesBegan(Set *pTouches, Event *pEvent)
{
Touch* touch = static_cast<Touch*>(pTouches->anyObject());
_beginPos = touch->getLocation();
}
void ExtensionsMainLayer::ccTouchesMoved(Set *pTouches, Event *pEvent)
{
Touch* touch = static_cast<Touch*>(pTouches->anyObject());
Point touchLocation = touch->getLocation();
float nMoveY = touchLocation.y - _beginPos.y;
Point curPos = _itemMenu->getPosition();
2013-07-12 14:11:55 +08:00
Point nextPos = Point(curPos.x, curPos.y + nMoveY);
if (nextPos.y < 0.0f)
{
_itemMenu->setPosition(Point::ZERO);
return;
}
if (nextPos.y > ((g_maxTests + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
{
2013-07-12 14:11:55 +08:00
_itemMenu->setPosition(Point(0, ((g_maxTests + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
return;
}
_itemMenu->setPosition(nextPos);
_beginPos = touchLocation;
s_tCurPos = nextPos;
}
////////////////////////////////////////////////////////
//
// ExtensionsTestScene
//
////////////////////////////////////////////////////////
void ExtensionsTestScene::runThisTest()
{
Layer* pLayer = new ExtensionsMainLayer();
addChild(pLayer);
pLayer->release();
Director::getInstance()->replaceScene(this);
}