axmol/tests/cpp-tests/Classes/BaseTest.h

256 lines
8.2 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
2015-04-03 11:54:39 +08:00
#ifndef _CPPTESTS_BASETEST_H__
#define _CPPTESTS_BASETEST_H__
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "GUI/CCControlExtension/CCControlExtensions.h"
#include "GUI/CCScrollView/CCScrollView.h"
#include "GUI/CCScrollView/CCTableView.h"
2015-04-03 11:54:39 +08:00
#include "VisibleRect.h"
2015-04-03 11:54:39 +08:00
class TestSuite;
/**
* Each test case should inherit from TestCase, and add to a TestSuite object.
*/
class TestCase : public axis::Scene
{
public:
2015-04-03 11:54:39 +08:00
/** TestCase test type.*/
enum class Type
{
/** For testing whether test case not crash.*/
ROBUSTNESS,
/**
2021-12-28 16:06:23 +08:00
* For check the correctness of regular test cases.
2015-04-03 11:54:39 +08:00
* A test case passes only if the actual output equal to the expected output.
*/
UNIT,
/** @warning The test type is not achieved.*/
GRAPHICAL_STATIC,
/** @note It's mean the test case need test manually.*/
MANUAL
};
TestCase();
~TestCase();
virtual std::string title() const { return ""; }
virtual std::string subtitle() const { return ""; }
/** Returns the test type, the default type is Type::ROBUSTNESS.*/
virtual Type getTestType() const;
/** Returns the time the test case needs.*/
virtual float getDuration() const;
/** Returns the expected output.*/
virtual std::string getExpectedOutput() const { return ""; }
/** Returns the actual output.*/
virtual std::string getActualOutput() const { return ""; }
2015-04-03 11:54:39 +08:00
/** Callback functions.*/
virtual void restartTestCallback(axis::Ref* sender);
virtual void nextTestCallback(axis::Ref* sender);
virtual void priorTestCallback(axis::Ref* sender);
virtual void onBackCallback(axis::Ref* sender);
2015-04-03 11:54:39 +08:00
/**
* You should NEVER call this method, unless you know what you are doing.
*/
void setTestSuite(TestSuite* testSuite);
TestSuite* getTestSuite() const { return _testSuite; }
/** Returns the run time of test case.*/
float getRunTime() const { return _runTime; }
/**
* You should NEVER call this method, unless you know what you are doing.
*/
2021-12-28 15:58:56 +08:00
void setTestCaseName(std::string_view name) { _testCaseName = name; }
2015-04-03 11:54:39 +08:00
std::string getTestCaseName() const { return _testCaseName; }
const axis::Label* getSubtitleLable() const { return _subtitleLabel; }
const axis::MenuItemImage* getRestartTestItem() const { return _restartTestItem; }
virtual void onEnter() override;
virtual bool init() override;
2015-04-03 11:54:39 +08:00
protected:
axis::MenuItemImage* _priorTestItem;
axis::MenuItemImage* _restartTestItem;
axis::MenuItemImage* _nextTestItem;
2015-04-03 11:54:39 +08:00
axis::Label* _titleLabel;
axis::Label* _subtitleLabel;
2015-04-03 11:54:39 +08:00
private:
TestSuite* _testSuite;
float _runTime;
std::string _testCaseName;
};
/**
* A TestBase object stores the following information about a test:
* - A pointer to the parent test(TestList or TestSuite).
* - Array of children test names.
* - A flag to indicate whether the test is a TestList object.
*
* @note You should not inherit from TestBase directly.
*/
class TestBase : public axis::Ref
2015-04-03 11:54:39 +08:00
{
public:
virtual ~TestBase();
/** Backs up one level. */
void backsUpOneLevel();
virtual void runThisTest() {}
bool isTestList() { return _isTestList; }
ssize_t getChildTestCount() { return _childTestNames.size(); }
/**
2021-12-28 16:06:23 +08:00
* You should NEVER call this method.
*/
2015-04-03 11:54:39 +08:00
void setTestParent(TestBase* parent) { _parentTest = parent; }
TestBase* getTestParent() { return _parentTest; }
2021-12-28 15:58:56 +08:00
void setTestName(std::string_view testName) { _testName = testName; }
2015-04-03 11:54:39 +08:00
std::string getTestName() const { return _testName; }
2021-12-28 16:06:23 +08:00
2015-04-03 11:54:39 +08:00
protected:
TestBase();
std::string _testName;
TestBase* _parentTest;
bool _isTestList;
std::vector<std::string> _childTestNames;
};
2015-04-03 11:54:39 +08:00
class TestController;
/**
2021-12-28 16:06:23 +08:00
* TestSuite correspond to a group of test cases.
* @note Each test case should add to a TestSuite object.
*/
2015-04-03 11:54:39 +08:00
class TestSuite : public TestBase
{
public:
void addTestCase(std::string_view testName, std::function<axis::Scene*()> callback);
2015-04-03 11:54:39 +08:00
virtual void restartCurrTest();
virtual void enterNextTest();
virtual void enterPreviousTest();
2015-08-18 22:19:24 +08:00
int getCurrTestIndex() { return _currTestIndex; }
2015-04-03 11:54:39 +08:00
virtual void runThisTest() override;
private:
std::vector<std::function<axis::Scene*()>> _testCallbacks;
2015-04-03 11:54:39 +08:00
int _currTestIndex;
friend class TestController;
};
class TestCustomTableView;
2015-04-03 11:54:39 +08:00
/**
* An instance of TestList is a means for displaying hierarchical lists of TestSuite.
*/
2021-12-28 16:06:23 +08:00
class TestList : public TestBase,
public axis::extension::TableViewDataSource,
public axis::extension::TableViewDelegate
2015-04-03 11:54:39 +08:00
{
public:
TestList();
~TestList();
2015-04-03 11:54:39 +08:00
2021-12-28 15:58:56 +08:00
void addTest(std::string_view testName, std::function<TestBase*()> callback);
2015-04-03 11:54:39 +08:00
virtual void runThisTest() override;
virtual void tableCellTouched(axis::extension::TableView* table,
axis::extension::TableViewCell* cell) override;
virtual axis::extension::TableViewCell* tableCellAtIndex(axis::extension::TableView* table,
2021-12-28 16:06:23 +08:00
ssize_t idx) override;
virtual axis::Size tableCellSizeForIndex(axis::extension::TableView* table, ssize_t idx) override;
virtual ssize_t numberOfCellsInTableView(axis::extension::TableView* table) override;
2015-04-03 11:54:39 +08:00
virtual void scrollViewDidScroll(axis::extension::ScrollView* view) override {}
virtual void scrollViewDidZoom(axis::extension::ScrollView* view) override {}
2015-04-03 11:54:39 +08:00
void deatchTableView();
2021-12-28 16:06:23 +08:00
2015-04-03 11:54:39 +08:00
private:
std::vector<std::function<TestBase*()>> _testCallbacks;
bool _cellTouchEnabled;
bool _shouldRestoreTableOffset;
axis::Vec2 _tableOffset;
2015-04-03 11:54:39 +08:00
friend class TestController;
TestCustomTableView* _tableView{};
2015-04-03 11:54:39 +08:00
};
2021-12-28 16:06:23 +08:00
#define ADD_TEST(__className__) addTest(#__className__, []() { return new __className__; });
2015-04-03 11:54:39 +08:00
2021-12-28 16:06:23 +08:00
#define ADD_TEST_CASE(__className__) addTestCase(#__className__, []() { return __className__::create(); });
2015-04-03 11:54:39 +08:00
2021-12-28 16:06:23 +08:00
#define DEFINE_TEST_LIST(__className__) \
class __className__ : public TestList \
{ \
public: \
__className__(); \
}
2015-04-03 11:54:39 +08:00
2021-12-28 16:06:23 +08:00
#define DEFINE_TEST_SUITE(__className__) \
class __className__ : public TestSuite \
{ \
public: \
__className__(); \
}
2015-04-03 11:54:39 +08:00
/**
* BaseTest is retained for compatibility with older versions.
2021-12-28 16:06:23 +08:00
* @warning It should soon be removed.
2015-04-03 11:54:39 +08:00
*/
class BaseTest : public axis::Layer
2015-04-03 11:54:39 +08:00
{
public:
virtual std::string title() const { return ""; }
2021-12-28 16:06:23 +08:00
virtual std::string subtitle() const { return ""; }
2015-04-03 11:54:39 +08:00
virtual void restartCallback(axis::Ref* sender) {}
virtual void nextCallback(axis::Ref* sender) {}
virtual void backCallback(axis::Ref* sender) {}
2015-04-03 11:54:39 +08:00
2021-12-28 16:06:23 +08:00
virtual void onEnter() override {}
virtual void onExit() override {}
2015-04-03 11:54:39 +08:00
};
2015-04-03 11:54:39 +08:00
#endif /* defined(_CPPTESTS_BASETEST_H__) */