PuzzleSDK
TestController类 参考

#include <controller.h>

+ TestController 的协作图:

Public 成员函数

 ~TestController ()
 
void startAutoTest ()
 
void stopAutoTest ()
 
void handleCrash ()
 
void onEnterBackground ()
 
void onEnterForeground ()
 
bool blockTouchBegan (cocos2d::Touch *touch, cocos2d::Event *event)
 
void setCurrTestSuite (TestSuite *testSuite)
 
TestSuitegetCurrTestSuite ()
 
bool isAutoTestRunning () const
 

静态 Public 成员函数

static TestControllergetInstance ()
 
static void destroyInstance ()
 

Private 成员函数

 TestController ()
 
void traverseThreadFunc ()
 
void traverseTestList (TestList *testList)
 
void traverseTestSuite (TestSuite *testSuite)
 
bool checkTest (TestCase *testCase)
 
void logEx (const char *format,...)
 

Private 属性

std::atomic< bool > _stopAutoTest
 
bool _isRunInBackground
 
TestList_rootTestList
 
TestSuite_testSuite
 
std::thread _autoTestThread
 
std::condition_variable _sleepCondition
 
std::unique_lock< std::mutex > * _sleepUniqueLock
 
cocos2d::Director * _director
 
cocos2d::EventListenerTouchOneByOne * _touchListener
 
std::string _logIndentation
 

详细描述

在文件 controller.h46 行定义.

构造及析构函数说明

◆ ~TestController()

TestController::~TestController ( )

在文件 controller.cpp149 行定义.

150{
151 _director->getEventDispatcher()->removeEventListener(_touchListener);
152
153 _rootTestList->release();
154 _rootTestList = nullptr;
155}
cocos2d::Director * _director
Definition: controller.h:89
cocos2d::EventListenerTouchOneByOne * _touchListener
Definition: controller.h:90
TestList * _rootTestList
Definition: controller.h:81

引用了 _director, _rootTestList , 以及 _touchListener.

◆ TestController()

TestController::TestController ( )
private

在文件 controller.cpp133 行定义.

134: _stopAutoTest(true)
135, _isRunInBackground(false)
136, _testSuite(nullptr)
137{
138 _rootTestList = new (std::nothrow) RootTests;
140 _director = Director::getInstance();
141
142 _touchListener = EventListenerTouchOneByOne::create();
143 _touchListener->onTouchBegan = CC_CALLBACK_2(TestController::blockTouchBegan, this);
144 _touchListener->setSwallowTouches(true);
145
146 _director->getEventDispatcher()->addEventListenerWithFixedPriority(_touchListener, -200);
147}
bool blockTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
Definition: controller.cpp:482
bool _isRunInBackground
Definition: controller.h:79
std::atomic< bool > _stopAutoTest
Definition: controller.h:78
TestSuite * _testSuite
Definition: controller.h:82
virtual void runThisTest() override
Definition: BaseTest.cpp:144

引用了 _director, _rootTestList, _touchListener, blockTouchBegan() , 以及 TestList::runThisTest().

+ 函数调用图:

成员函数说明

◆ blockTouchBegan()

bool TestController::blockTouchBegan ( cocos2d::Touch *  touch,
cocos2d::Event *  event 
)

在文件 controller.cpp482 行定义.

483{
484 return !_stopAutoTest;
485}

引用了 _stopAutoTest.

被这些函数引用 TestController().

+ 这是这个函数的调用关系图:

◆ checkTest()

bool TestController::checkTest ( TestCase testCase)
private

在文件 controller.cpp375 行定义.

376{
377 if (testCase)
378 {
379 switch (testCase->getTestType())
380 {
382 {
383 if (testCase && testCase->getExpectedOutput() != testCase->getActualOutput())
384 {
385 logEx("%s %s test fail", LOG_TAG, testCase->getTestCaseName().c_str());
386 }
387 else
388 {
389 logEx("%s %s test pass", LOG_TAG, testCase->getTestCaseName().c_str());
390 }
391 break;
392 }
394 {
395 break;
396 }
398 {
399 break;
400 }
401 default:
402 break;
403 }
404 }
405
406 return true;
407}
virtual std::string getExpectedOutput() const
Definition: BaseTest.h:69
std::string getTestCaseName() const
Definition: BaseTest.h:92
virtual Type getTestType() const
Definition: BaseTest.cpp:380
virtual std::string getActualOutput() const
Definition: BaseTest.h:71
void logEx(const char *format,...)
Definition: controller.cpp:431
#define LOG_TAG
Definition: controller.cpp:36

引用了 TestCase::getActualOutput(), TestCase::getExpectedOutput(), TestCase::getTestCaseName(), TestCase::getTestType(), LOG_TAG, logEx(), TestCase::MANUAL, TestCase::ROBUSTNESS , 以及 TestCase::UNIT.

被这些函数引用 traverseTestSuite().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ destroyInstance()

void TestController::destroyInstance ( )
static

在文件 controller.cpp470 行定义.

471{
473 {
475 delete s_testController;
476 s_testController = nullptr;
477 }
478
480}
void stopAutoTest()
Definition: controller.cpp:168
static TestController * s_testController
Definition: controller.cpp:456
static void disableCrashCatch()
Definition: controller.cpp:505

引用了 disableCrashCatch(), s_testController , 以及 stopAutoTest().

被这些函数引用 TestList::runThisTest().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ getCurrTestSuite()

TestSuite * TestController::getCurrTestSuite ( )
inline

在文件 controller.h65 行定义.

65{ return _testSuite; }

引用了 _testSuite.

◆ getInstance()

TestController * TestController::getInstance ( )
static

在文件 controller.cpp458 行定义.

459{
460 if (s_testController == nullptr)
461 {
462 s_testController = new (std::nothrow) TestController;
463
465 }
466
467 return s_testController;
468}
static void initCrashCatch()
Definition: controller.cpp:501

引用了 initCrashCatch() , 以及 s_testController.

被这些函数引用 AppDelegate::applicationDidFinishLaunching(), TestCase::onEnter(), TestSuite::runThisTest(), TestList::runThisTest() , 以及 SchedulerRemoveAllFunctionsToBePerformedInCocosThread::update().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ handleCrash()

void TestController::handleCrash ( )

在文件 controller.cpp409 行定义.

410{
412
413 logEx("%sCatch an crash event", LOG_TAG);
414
415 if (!_stopAutoTest)
416 {
417 stopAutoTest();
418 }
419}

引用了 _stopAutoTest, disableCrashCatch(), LOG_TAG, logEx() , 以及 stopAutoTest().

被这些函数引用 windowExceptionFilter().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ isAutoTestRunning()

bool TestController::isAutoTestRunning ( ) const
inline

在文件 controller.h66 行定义.

66{ return !_stopAutoTest; }

引用了 _stopAutoTest.

◆ logEx()

void TestController::logEx ( const char *  format,
  ... 
)
private

在文件 controller.cpp431 行定义.

432{
433 char buff[1024];
434
435 va_list args;
436 va_start(args, format);
437 vsnprintf(buff, 1020, format, args);
438 strcat(buff, "\n");
439
440#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
441 __android_log_print(ANDROID_LOG_DEBUG, "cocos2d-x debug info", "%s", buff);
442
443#elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
444 WCHAR wszBuf[1024] = { 0 };
445 MultiByteToWideChar(CP_UTF8, 0, buff, -1, wszBuf, sizeof(wszBuf));
446 OutputDebugStringW(wszBuf);
447
448#else
449 // Linux, Mac, iOS, etc
450 fprintf(stdout, "%s", buff);
451 fflush(stdout);
452#endif
453 va_end(args);
454}

被这些函数引用 checkTest(), handleCrash(), traverseTestList() , 以及 traverseTestSuite().

+ 这是这个函数的调用关系图:

◆ onEnterBackground()

void TestController::onEnterBackground ( )

在文件 controller.cpp421 行定义.

422{
423 _isRunInBackground = true;
424}

引用了 _isRunInBackground.

◆ onEnterForeground()

void TestController::onEnterForeground ( )

在文件 controller.cpp426 行定义.

427{
428 _isRunInBackground = false;
429}

引用了 _isRunInBackground.

◆ setCurrTestSuite()

void TestController::setCurrTestSuite ( TestSuite testSuite)
inline

在文件 controller.h64 行定义.

64{ _testSuite = testSuite; }

引用了 _testSuite.

被这些函数引用 TestSuite::runThisTest().

+ 这是这个函数的调用关系图:

◆ startAutoTest()

void TestController::startAutoTest ( )

在文件 controller.cpp157 行定义.

158{
159 if (!_autoTestThread.joinable())
160 {
161 _stopAutoTest = false;
162 _logIndentation = "";
164 _autoTestThread.detach();
165 }
166}
std::string _logIndentation
Definition: controller.h:92
std::thread _autoTestThread
Definition: controller.h:84
void traverseThreadFunc()
Definition: controller.cpp:178

引用了 _autoTestThread, _logIndentation, _stopAutoTest , 以及 traverseThreadFunc().

被这些函数引用 TestList::runThisTest().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ stopAutoTest()

void TestController::stopAutoTest ( )

在文件 controller.cpp168 行定义.

169{
170 _stopAutoTest = true;
171
172 if (_autoTestThread.joinable()) {
173 _sleepCondition.notify_all();
174 _autoTestThread.join();
175 }
176}
std::condition_variable _sleepCondition
Definition: controller.h:86

引用了 _autoTestThread, _sleepCondition , 以及 _stopAutoTest.

被这些函数引用 destroyInstance(), handleCrash() , 以及 TestList::runThisTest().

+ 这是这个函数的调用关系图:

◆ traverseTestList()

void TestController::traverseTestList ( TestList testList)
private

在文件 controller.cpp187 行定义.

188{
189 if (testList == _rootTestList)
190 {
191 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
192 }
193 else
194 {
196 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
197 }
198 logEx("%s%sBegin traverse TestList:%s", LOG_TAG, _logIndentation.c_str(), testList->getTestName().c_str());
199
200 auto scheduler = _director->getScheduler();
201 int testIndex = 0;
202 for (auto& callback : testList->_testCallbacks)
203 {
204 if (_stopAutoTest) break;
205 while (_isRunInBackground)
206 {
207 logEx("_director is paused");
208 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
209 }
210 if (callback)
211 {
212 auto test = callback();
213 test->setTestParent(testList);
214 test->setTestName(testList->_childTestNames[testIndex++]);
215 if (test->isTestList())
216 {
217 scheduler->performFunctionInCocosThread([&](){
218 test->runThisTest();
219 });
220
222 }
223 else
224 {
226 }
227 }
228 }
229
230 if (testList == _rootTestList)
231 {
232 _stopAutoTest = true;
233 }
234 else
235 {
236 if (!_stopAutoTest)
237 {
238 //Backs up one level and release TestList object.
239 scheduler->performFunctionInCocosThread([&](){
240 testList->_parentTest->runThisTest();
241 });
242 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
243 testList->release();
244 }
245
247 }
248}
TestBase * _parentTest
Definition: BaseTest.h:149
virtual void runThisTest()
Definition: BaseTest.h:131
std::string getTestName() const
Definition: BaseTest.h:144
std::vector< std::string > _childTestNames
Definition: BaseTest.h:151
void traverseTestList(TestList *testList)
Definition: controller.cpp:187
std::unique_lock< std::mutex > * _sleepUniqueLock
Definition: controller.h:87
void traverseTestSuite(TestSuite *testSuite)
Definition: controller.cpp:250
std::vector< std::function< TestBase *()> > _testCallbacks
Definition: BaseTest.h:201
#define LOG_INDENTATION
Definition: controller.cpp:35

引用了 TestBase::_childTestNames, _director, _isRunInBackground, _logIndentation, TestBase::_parentTest, _rootTestList, _sleepCondition, _sleepUniqueLock, _stopAutoTest, TestList::_testCallbacks, TestBase::getTestName(), LOG_INDENTATION, LOG_TAG, logEx(), TestBase::runThisTest(), traverseTestList() , 以及 traverseTestSuite().

被这些函数引用 traverseTestList() , 以及 traverseThreadFunc().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ traverseTestSuite()

void TestController::traverseTestSuite ( TestSuite testSuite)
private

在文件 controller.cpp250 行定义.

251{
252 auto scheduler = _director->getScheduler();
253 int testIndex = 0;
254 float testCaseDuration = 0.0f;
256 logEx("%s%sBegin traverse TestSuite:%s", LOG_TAG, _logIndentation.c_str(), testSuite->getTestName().c_str());
257
259 testSuite->_currTestIndex = -1;
260
261 auto logIndentation = _logIndentation;
262 for (auto& callback : testSuite->_testCallbacks)
263 {
264 auto testName = testSuite->_childTestNames[testIndex++];
265
266 Scene* testScene = nullptr;
267 TestCase* testCase = nullptr;
268 TransitionScene* transitionScene = nullptr;
269
270 if (_stopAutoTest) break;
271
272 while (_isRunInBackground)
273 {
274 logEx("_director is paused");
275 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(500));
276 }
277 //Run test case in the cocos[GL] thread.
278 scheduler->performFunctionInCocosThread([&, logIndentation, testName](){
279 if (_stopAutoTest) return;
280 logEx("%s%sRun test:%s.", LOG_TAG, logIndentation.c_str(), testName.c_str());
281
282 auto scene = callback();
283 if (_stopAutoTest) return;
284
285 if (scene)
286 {
287 transitionScene = dynamic_cast<TransitionScene*>(scene);
288 if (transitionScene)
289 {
290 testCase = (TestCase*)transitionScene->getInScene();
291 testCaseDuration = transitionScene->getDuration() + 0.5f;
292 }
293 else
294 {
295 testCase = (TestCase*)scene;
296 testCaseDuration = testCase->getDuration();
297 }
298 testSuite->_currTestIndex++;
299 testCase->setTestSuite(testSuite);
300 testCase->setTestCaseName(testName);
301 _director->replaceScene(scene);
302
303 testScene = scene;
304 }
305 });
306
307 if (_stopAutoTest) break;
308
309 //Wait for the test case be created.
310 float waitTime = 0.0f;
311 while (!testScene && !_stopAutoTest)
312 {
313 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(50));
315 {
316 waitTime += 0.05f;
317 }
318
319 if (waitTime > CREATE_TIME_OUT)
320 {
321 logEx("%sCreate test %s time out", LOG_TAG, testName.c_str());
322 _stopAutoTest = true;
323 break;
324 }
325 }
326
327 if (_stopAutoTest) break;
328
329 //Wait for test completed.
330 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(int(1000 * testCaseDuration)));
331
332 if (transitionScene == nullptr)
333 {
334 waitTime = 0.0f;
335 while (!_stopAutoTest && testCase->getRunTime() < testCaseDuration)
336 {
337 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(50));
339 {
340 waitTime += 0.05f;
341 }
342
343 if (waitTime > TEST_TIME_OUT)
344 {
345 logEx("%sRun test %s time out", LOG_TAG, testName.c_str());
346 _stopAutoTest = true;
347 break;
348 }
349 }
350
351 if (!_stopAutoTest)
352 {
353 //Check the result of test.
354 checkTest(testCase);
355 }
356 }
357 }
358
359 if (!_stopAutoTest)
360 {
361 //Backs up one level and release TestSuite object.
362 auto parentTest = testSuite->_parentTest;
363 scheduler->performFunctionInCocosThread([&](){
364 parentTest->runThisTest();
365 });
366
367 _sleepCondition.wait_for(*_sleepUniqueLock, std::chrono::milliseconds(1000));
368 testSuite->release();
369 }
370
373}
float getRunTime() const
Definition: BaseTest.h:86
void setTestCaseName(const std::string &name)
Definition: BaseTest.h:91
virtual float getDuration() const
Definition: BaseTest.cpp:385
void setTestSuite(TestSuite *testSuite)
Definition: BaseTest.cpp:367
bool checkTest(TestCase *testCase)
Definition: controller.cpp:375
std::vector< std::function< cocos2d::Scene *()> > _testCallbacks
Definition: BaseTest.h:173
int _currTestIndex
Definition: BaseTest.h:175
#define TEST_TIME_OUT
Definition: controller.cpp:33
#define CREATE_TIME_OUT
Definition: controller.cpp:34

引用了 TestBase::_childTestNames, TestSuite::_currTestIndex, _director, _isRunInBackground, _logIndentation, TestBase::_parentTest, _sleepCondition, _sleepUniqueLock, _stopAutoTest, TestSuite::_testCallbacks, checkTest(), CREATE_TIME_OUT, TestCase::getDuration(), TestCase::getRunTime(), TestBase::getTestName(), LOG_INDENTATION, LOG_TAG, logEx(), TestCase::setTestCaseName(), TestCase::setTestSuite() , 以及 TEST_TIME_OUT.

被这些函数引用 traverseTestList().

+ 函数调用图:
+ 这是这个函数的调用关系图:

◆ traverseThreadFunc()

void TestController::traverseThreadFunc ( )
private

在文件 controller.cpp178 行定义.

179{
180 std::mutex sleepMutex;
181 auto lock = std::unique_lock<std::mutex>(sleepMutex);
182 _sleepUniqueLock = &lock;
184 _sleepUniqueLock = nullptr;
185}

引用了 _rootTestList, _sleepUniqueLock , 以及 traverseTestList().

被这些函数引用 startAutoTest().

+ 函数调用图:
+ 这是这个函数的调用关系图:

类成员变量说明

◆ _autoTestThread

std::thread TestController::_autoTestThread
private

在文件 controller.h84 行定义.

被这些函数引用 startAutoTest() , 以及 stopAutoTest().

◆ _director

cocos2d::Director* TestController::_director
private

在文件 controller.h89 行定义.

被这些函数引用 TestController(), traverseTestList(), traverseTestSuite() , 以及 ~TestController().

◆ _isRunInBackground

bool TestController::_isRunInBackground
private

在文件 controller.h79 行定义.

被这些函数引用 onEnterBackground(), onEnterForeground(), traverseTestList() , 以及 traverseTestSuite().

◆ _logIndentation

std::string TestController::_logIndentation
private

在文件 controller.h92 行定义.

被这些函数引用 startAutoTest(), traverseTestList() , 以及 traverseTestSuite().

◆ _rootTestList

TestList* TestController::_rootTestList
private

在文件 controller.h81 行定义.

被这些函数引用 TestController(), traverseTestList(), traverseThreadFunc() , 以及 ~TestController().

◆ _sleepCondition

std::condition_variable TestController::_sleepCondition
private

在文件 controller.h86 行定义.

被这些函数引用 stopAutoTest(), traverseTestList() , 以及 traverseTestSuite().

◆ _sleepUniqueLock

std::unique_lock<std::mutex>* TestController::_sleepUniqueLock
private

在文件 controller.h87 行定义.

被这些函数引用 traverseTestList(), traverseTestSuite() , 以及 traverseThreadFunc().

◆ _stopAutoTest

std::atomic<bool> TestController::_stopAutoTest
private

◆ _testSuite

TestSuite* TestController::_testSuite
private

在文件 controller.h82 行定义.

被这些函数引用 getCurrTestSuite() , 以及 setCurrTestSuite().

◆ _touchListener

cocos2d::EventListenerTouchOneByOne* TestController::_touchListener
private

在文件 controller.h90 行定义.

被这些函数引用 TestController() , 以及 ~TestController().


该类的文档由以下文件生成: