axmol/tests/cpp-tests/Source/controller.cpp

567 lines
18 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "controller.h"
#include <functional>
#include <chrono>
#include "BaseTest.h"
#include "tests.h"
USING_NS_AX;
2019-11-23 20:27:39 +08:00
#define TEST_TIME_OUT 50
#define CREATE_TIME_OUT 25
#define LOG_INDENTATION " "
#define LOG_TAG "[TestController]"
static void initCrashCatch();
static void disableCrashCatch();
class RootTests : public TestList
{
public:
RootTests()
{
2022-11-01 19:50:43 +08:00
#if __has_include("EffekseerForCocos2d-x.h")
#pragma message("The optional extension Effekseer is enabled.")
2022-11-01 19:50:43 +08:00
addTest("Effekseer", []() { return new EffekseerTests(); });
#endif
2021-12-08 00:11:53 +08:00
// addTest("Node: Scene3D", [](){return new Scene3DTests(); });
#if defined(AX_PLATFORM_PC) || (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) || defined(__EMSCRIPTEN__)
addTest("ImGui", []() { return new ImGuiTests(); });
#endif
addTest("Texture2D", []() { return new Texture2DTests(); });
addTest("ActionManager", []() { return new ActionManagerTests(); });
addTest("Actions - Basic", []() { return new ActionsTests(); });
addTest("Actions - Ease", []() { return new ActionsEaseTests(); });
addTest("Actions - Progress", []() { return new ActionsProgressTests(); });
2021-12-08 00:11:53 +08:00
addTest("Audio - NewAudioEngine", []() { return new AudioEngineTests(); });
2021-12-08 00:11:53 +08:00
addTest("Box2D - Basic", []() { return new Box2DTests(); });
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
2021-12-08 00:11:53 +08:00
addTest("Box2D - TestBed", []() { return new Box2DTestBedTests(); });
#endif
addTest("Chipmunk2D - Basic", []() { return new ChipmunkTests(); });
#if defined(AX_PLATFORM_PC) || defined(__EMSCRIPTEN__)
addTest("Chipmunk2D - TestBed", []() { return new ChipmunkTestBedTests(); });
#endif
2019-11-23 20:27:39 +08:00
addTest("Bugs", []() { return new BugsTests(); });
addTest("Click and Move", []() { return new ClickAndMoveTest(); });
2019-11-23 20:27:39 +08:00
addTest("Configuration", []() { return new ConfigurationTests(); });
addTest("Console", []() { return new ConsoleTests(); });
#if !defined(__EMSCRIPTEN__)
2019-11-23 20:27:39 +08:00
addTest("Curl", []() { return new CurlTests(); });
#endif
2019-11-23 20:27:39 +08:00
addTest("Current Language", []() { return new CurrentLanguageTests(); });
2021-07-04 16:40:34 +08:00
addTest("Network Test", []() { return new NetworkTests(); });
2019-11-23 20:27:39 +08:00
addTest("EventDispatcher", []() { return new EventDispatcherTests(); });
addTest("Effects - Advanced", []() { return new EffectAdvanceTests(); });
addTest("Effects - Basic", []() { return new EffectTests(); });
2019-11-23 20:27:39 +08:00
addTest("Extensions", []() { return new ExtensionsTests(); });
addTest("FileUtils", []() { return new FileUtilsTests(); });
addTest("Fonts", []() { return new FontTests(); });
addTest("Interval", []() { return new IntervalTests(); });
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
2019-11-23 20:27:39 +08:00
addTest("JNIHelper", []() { return new JNITests(); });
#endif
addTest("Material System", []() { return new MaterialSystemTest(); });
addTest("Navigation Mesh", []() { return new NavMeshTests(); });
addTest("Node: BillBoard Test", []() { return new BillBoardTests(); });
addTest("Node: Camera3D Test", []() { return new Camera3DTests(); });
2019-11-23 20:27:39 +08:00
addTest("Node: Clipping", []() { return new ClippingNodeTests(); });
addTest("Node: Draw", []() { return new DrawPrimitivesTests(); });
addTest("Node: Label - New API", []() { return new NewLabelTests(); });
addTest("Node: Layer", []() { return new LayerTests(); });
addTest("Node: Light", []() { return new LightTests(); });
addTest("Node: Menu", []() { return new MenuTests(); });
addTest("Node: MotionStreak", []() { return new MotionStreakTests(); });
addTest("Node: Node", []() { return new CocosNodeTests(); });
addTest("Node: Parallax", []() { return new ParallaxTests(); });
addTest("Node: Particles", []() { return new ParticleTests(); });
addTest("Node: Particle3D (PU)", []() { return new Particle3DTests(); });
2022-07-16 10:43:05 +08:00
#if AX_USE_PHYSICS
addTest("Node: Physics", []() { return new PhysicsTests(); });
2019-11-23 20:27:39 +08:00
#endif
addTest("Node: Physics3D", []() { return new Physics3DTests(); });
addTest("Node: RenderTexture", []() { return new RenderTextureTests(); });
addTest("Node: Scene", []() { return new SceneTests(); });
addTest("Node: Spine", []() { return new SpineTests(); });
addTest("Node: Sprite", []() { return new SpriteTests(); });
addTest("Node: MeshRenderer", []() { return new MeshRendererTests(); });
addTest("Node: SpritePolygon", []() { return new SpritePolygonTest(); });
addTest("Node: Terrain", []() { return new TerrainTests(); });
addTest("Node: FastTileMap", []() { return new FastTileMapTests(); });
addTest("Node: Text Input", []() { return new TextInputTests(); });
addTest("Node: UI", []() { return new UITests(); });
2019-11-23 20:27:39 +08:00
addTest("Mouse", []() { return new MouseTests(); });
addTest("MultiTouch", []() { return new MultiTouchTests(); });
addTest("Renderer", []() { return new NewRendererTests(); });
addTest("ReleasePool", []() { return new ReleasePoolTests(); });
addTest("Rotate World", []() { return new RotateWorldTests(); });
addTest("Scheduler", []() { return new SchedulerTests(); });
2019-11-23 20:27:39 +08:00
addTest("Shader - Basic", []() { return new ShaderTests(); });
addTest("Shader - Sprite", []() { return new Shader2Tests(); });
addTest("TextureCache", []() { return new TextureCacheTests(); });
addTest("TexturePacker Encryption", []() { return new TextureAtlasEncryptionTests(); });
addTest("Touches", []() { return new TouchesTests(); });
addTest("Transitions", []() { return new TransitionsTests(); });
2019-11-23 20:27:39 +08:00
addTest("Unit Test", []() { return new UnitTests(); });
addTest("Unzip Test", []() { return new ZipTests(); });
2019-11-23 20:27:39 +08:00
addTest("URL Open Test", []() { return new OpenURLTests(); });
addTest("UserDefault", []() { return new UserDefaultTests(); });
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_IOS || AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
2019-11-23 20:27:39 +08:00
addTest("Vibrate", []() { return new VibrateTests(); });
#endif
addTest("Zwoptex", []() { return new ZwoptexTests(); });
addTest("SpriteFrameCache", []() { return new SpriteFrameCacheTests(); }); // TODO
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC || AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 || \
AX_TARGET_PLATFORM == AX_PLATFORM_LINUX)
addTest("Window Test", []() { return new WindowTests(); }); // TODO wrong effect
2019-11-23 20:27:39 +08:00
#endif
}
};
TestController::TestController() : _stopAutoTest(true), _isRunInBackground(false), _testSuite(nullptr)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_rootTestList = new RootTests;
2019-11-23 20:27:39 +08:00
_rootTestList->runThisTest();
_director = Director::getInstance();
_touchListener = EventListenerTouchOneByOne::create();
2022-07-16 10:43:05 +08:00
_touchListener->onTouchBegan = AX_CALLBACK_2(TestController::blockTouchBegan, this);
2019-11-23 20:27:39 +08:00
_touchListener->setSwallowTouches(true);
_director->getEventDispatcher()->addEventListenerWithFixedPriority(_touchListener, -200);
}
TestController::~TestController()
{
_director->getEventDispatcher()->removeEventListener(_touchListener);
_rootTestList->release();
_rootTestList = nullptr;
}
void TestController::startAutoTest()
{
if (!_autoTestThread.joinable())
{
_stopAutoTest = false;
2019-11-23 20:27:39 +08:00
_logIndentation = "";
_autoTestThread = std::thread(&TestController::traverseThreadFunc, this);
_autoTestThread.detach();
}
}
void TestController::stopAutoTest()
{
_stopAutoTest = true;
if (_autoTestThread.joinable())
{
2019-11-23 20:27:39 +08:00
_sleepCondition.notify_all();
_autoTestThread.join();
}
}
void TestController::traverseThreadFunc()
{
std::mutex sleepMutex;
auto lock = std::unique_lock<std::mutex>(sleepMutex);
2019-11-23 20:27:39 +08:00
_sleepUniqueLock = &lock;
traverseTestList(_rootTestList);
_sleepUniqueLock = nullptr;
}
void TestController::traverseTestList(TestList* testList)
{
if (testList == _rootTestList)
{
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
}
else
{
_logIndentation += LOG_INDENTATION;
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
}
logEx("%s%sBegin traverse TestList:%s", LOG_TAG, _logIndentation.c_str(), testList->getTestName().c_str());
auto scheduler = _director->getScheduler();
int testIndex = 0;
for (auto&& callback : testList->_testCallbacks)
2019-11-23 20:27:39 +08:00
{
if (_stopAutoTest)
break;
2019-11-23 20:27:39 +08:00
while (_isRunInBackground)
{
logEx("_director is paused");
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
}
if (callback)
{
auto test = callback();
test->setTestParent(testList);
test->setTestName(testList->_childTestNames[testIndex++]);
if (test->isTestList())
{
2023-05-27 23:21:25 +08:00
scheduler->runOnAxmolThread([&]() { test->runThisTest(); });
2019-11-23 20:27:39 +08:00
traverseTestList((TestList*)test);
}
else
{
traverseTestSuite((TestSuite*)test);
}
}
}
if (testList == _rootTestList)
{
_stopAutoTest = true;
if (std::getenv("AXMOL_START_AUTOTEST"))
utils::killCurrentProcess();
2019-11-23 20:27:39 +08:00
}
else
{
if (!_stopAutoTest)
{
// Backs up one level and release TestList object.
2023-05-27 23:21:25 +08:00
scheduler->runOnAxmolThread([&]() { testList->_parentTest->runThisTest(); });
2019-11-23 20:27:39 +08:00
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
testList->release();
}
2019-11-23 20:27:39 +08:00
_logIndentation.erase(_logIndentation.rfind(LOG_INDENTATION));
}
}
void TestController::traverseTestSuite(TestSuite* testSuite)
{
auto scheduler = _director->getScheduler();
int testIndex = 0;
2019-11-23 20:27:39 +08:00
float testCaseDuration = 0.0f;
_logIndentation += LOG_INDENTATION;
logEx("%s%sBegin traverse TestSuite:%s", LOG_TAG, _logIndentation.c_str(), testSuite->getTestName().c_str());
_logIndentation += LOG_INDENTATION;
testSuite->_currTestIndex = -1;
auto logIndentation = _logIndentation;
for (auto&& callback : testSuite->_testCallbacks)
2019-11-23 20:27:39 +08:00
{
auto testName = testSuite->_childTestNames[testIndex++];
Scene* testScene = nullptr;
TestCase* testCase = nullptr;
2019-11-23 20:27:39 +08:00
TransitionScene* transitionScene = nullptr;
if (_stopAutoTest)
break;
2019-11-23 20:27:39 +08:00
while (_isRunInBackground)
{
logEx("_director is paused");
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
}
// Run test case in the cocos[GL] thread.
2023-05-27 23:21:25 +08:00
scheduler->runOnAxmolThread([&, logIndentation, testName]() {
if (_stopAutoTest)
return;
2019-11-23 20:27:39 +08:00
logEx("%s%sRun test:%s.", LOG_TAG, logIndentation.c_str(), testName.c_str());
auto scene = callback();
if (_stopAutoTest)
return;
2019-11-23 20:27:39 +08:00
if (scene)
{
transitionScene = dynamic_cast<TransitionScene*>(scene);
if (transitionScene)
{
testCase = (TestCase*)transitionScene->getInScene();
2019-11-23 20:27:39 +08:00
testCaseDuration = transitionScene->getDuration() + 0.5f;
}
else
{
testCase = (TestCase*)scene;
2019-11-23 20:27:39 +08:00
testCaseDuration = testCase->getDuration();
}
testSuite->_currTestIndex++;
testCase->setTestSuite(testSuite);
testCase->setTestCaseName(testName);
_director->replaceScene(scene);
testScene = scene;
}
});
if (_stopAutoTest)
break;
2019-11-23 20:27:39 +08:00
// Wait for the test case be created.
2019-11-23 20:27:39 +08:00
float waitTime = 0.0f;
while (!testScene && !_stopAutoTest)
{
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(50));
if (!_isRunInBackground)
{
waitTime += 0.05f;
}
if (waitTime > CREATE_TIME_OUT)
{
logEx("%sCreate test %s time out", LOG_TAG, testName.c_str());
_stopAutoTest = true;
break;
}
}
if (_stopAutoTest)
break;
2019-11-23 20:27:39 +08:00
// Wait for test completed.
2019-11-23 20:27:39 +08:00
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(int(1000 * testCaseDuration)));
if (transitionScene == nullptr)
{
waitTime = 0.0f;
while (!_stopAutoTest && testCase->getRunTime() < testCaseDuration)
{
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(50));
if (!_isRunInBackground)
{
waitTime += 0.05f;
}
if (waitTime > TEST_TIME_OUT)
{
logEx("%sRun test %s time out", LOG_TAG, testName.c_str());
_stopAutoTest = true;
break;
}
}
if (!_stopAutoTest)
{
// Check the result of test.
2019-11-23 20:27:39 +08:00
checkTest(testCase);
}
}
}
if (!_stopAutoTest)
{
// Backs up one level and release TestSuite object.
2019-11-23 20:27:39 +08:00
auto parentTest = testSuite->_parentTest;
2023-05-27 23:21:25 +08:00
scheduler->runOnAxmolThread([&]() { parentTest->runThisTest(); });
2019-11-23 20:27:39 +08:00
_sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(1000));
testSuite->release();
}
_logIndentation.erase(_logIndentation.rfind(LOG_INDENTATION));
_logIndentation.erase(_logIndentation.rfind(LOG_INDENTATION));
}
bool TestController::checkTest(TestCase* testCase)
{
if (testCase)
{
switch (testCase->getTestType())
{
case TestCase::Type::UNIT:
{
if (testCase && testCase->getExpectedOutput() != testCase->getActualOutput())
{
logEx("%s %s test fail", LOG_TAG, testCase->getTestCaseName().c_str());
}
else
{
logEx("%s %s test pass", LOG_TAG, testCase->getTestCaseName().c_str());
}
break;
}
case TestCase::Type::ROBUSTNESS:
{
break;
}
case TestCase::Type::MANUAL:
{
break;
}
default:
break;
}
}
return true;
}
void TestController::handleCrash()
{
disableCrashCatch();
logEx("%sCatch an crash event", LOG_TAG);
if (!_stopAutoTest)
{
stopAutoTest();
}
}
void TestController::onEnterBackground()
{
_isRunInBackground = true;
}
void TestController::onEnterForeground()
{
_isRunInBackground = false;
}
void TestController::logEx(const char* format, ...)
2019-11-23 20:27:39 +08:00
{
char buff[1024];
va_list args;
va_start(args, format);
vsnprintf(buff, 1020, format, args);
strcat(buff, "\n");
2022-07-16 10:43:05 +08:00
#if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
2022-10-01 16:24:52 +08:00
__android_log_print(ANDROID_LOG_DEBUG, "axmol debug info", "%s", buff);
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#elif AX_TARGET_PLATFORM == AX_PLATFORM_WIN32
WCHAR wszBuf[1024] = {0};
2019-11-23 20:27:39 +08:00
MultiByteToWideChar(CP_UTF8, 0, buff, -1, wszBuf, sizeof(wszBuf));
OutputDebugStringW(wszBuf);
#else
// Linux, Mac, iOS, etc
fprintf(stdout, "%s", buff);
fflush(stdout);
#endif
va_end(args);
}
static TestController* s_testController = nullptr;
TestController* TestController::getInstance()
{
if (s_testController == nullptr)
{
2021-12-08 00:11:53 +08:00
s_testController = new TestController;
2019-11-23 20:27:39 +08:00
initCrashCatch();
}
return s_testController;
}
void TestController::destroyInstance()
{
if (s_testController)
{
s_testController->stopAutoTest();
delete s_testController;
s_testController = nullptr;
}
disableCrashCatch();
}
bool TestController::blockTouchBegan(Touch* touch, Event* event)
{
return !_stopAutoTest;
}
//==================================================================================================
2022-07-16 10:43:05 +08:00
#if AX_TARGET_PLATFORM == AX_PLATFORM_WIN32
# include <windows.h>
2019-11-23 20:27:39 +08:00
static long __stdcall windowExceptionFilter(_EXCEPTION_POINTERS* excp)
2019-11-23 20:27:39 +08:00
{
if (s_testController)
{
s_testController->handleCrash();
}
return EXCEPTION_EXECUTE_HANDLER;
}
static void initCrashCatch()
{
SetUnhandledExceptionFilter(windowExceptionFilter);
}
static void disableCrashCatch()
{
SetUnhandledExceptionFilter(UnhandledExceptionFilter);
}
2022-07-16 10:43:05 +08:00
#elif AX_TARGET_PLATFORM == AX_PLATFORM_MAC || AX_TARGET_PLATFORM == AX_PLATFORM_IOS || \
AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
# if AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
2019-11-23 20:27:39 +08:00
static int s_fatal_signals[] = {
SIGILL, SIGABRT, SIGBUS, SIGFPE, SIGSEGV, SIGSTKFLT, SIGPIPE,
2019-11-23 20:27:39 +08:00
};
# else
2019-11-23 20:27:39 +08:00
static int s_fatal_signals[] = {
SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP, SIGTERM, SIGKILL,
2019-11-23 20:27:39 +08:00
};
# endif
2019-11-23 20:27:39 +08:00
static void signalHandler(int sig)
{
if (s_testController)
{
s_testController->handleCrash();
}
}
static void initCrashCatch()
{
for (auto&& sig : s_fatal_signals)
2019-11-23 20:27:39 +08:00
{
signal(sig, signalHandler);
}
}
static void disableCrashCatch()
{
for (auto&& sig : s_fatal_signals)
2019-11-23 20:27:39 +08:00
{
signal(sig, SIG_DFL);
}
}
#else
static void initCrashCatch() {}
2019-11-23 20:27:39 +08:00
static void disableCrashCatch() {}
2019-11-23 20:27:39 +08:00
#endif