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

511 lines
15 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axis-project.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:
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.
****************************************************************************/
#include "BaseTest.h"
#include "testResource.h"
#include "controller.h"
USING_NS_CC;
USING_NS_CC_EXT;
#define TABEL_LABEL_TAG 1024
TestBase::TestBase() : _parentTest(nullptr), _isTestList(false) {}
2019-11-23 20:27:39 +08:00
TestBase::~TestBase() {}
2019-11-23 20:27:39 +08:00
void TestBase::backsUpOneLevel()
{
if (_parentTest)
{
_parentTest->runThisTest();
this->release();
}
}
// TestList
2019-11-23 20:27:39 +08:00
class TestCustomTableView : public TableView
{
public:
static TestCustomTableView* create(TableViewDataSource* dataSource, Size size)
{
2021-12-08 00:11:53 +08:00
auto table = new TestCustomTableView();
2019-11-23 20:27:39 +08:00
table->initWithViewSize(size, nullptr);
table->autorelease();
table->setDataSource(dataSource);
table->_updateCellPositions();
table->_updateContentSize();
return table;
}
virtual void onTouchEnded(Touch* touch, Event* event) override
2019-11-23 20:27:39 +08:00
{
if (!this->isVisible())
{
return;
}
if (_touchedCell)
{
auto label = (Label*)_touchedCell->getChildByTag(TABEL_LABEL_TAG);
Rect bbox = label->getBoundingBox();
2019-11-23 20:27:39 +08:00
bbox.origin = _touchedCell->convertToWorldSpace(bbox.origin);
if (bbox.containsPoint(touch->getLocation()) && _tableViewDelegate != nullptr)
{
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
_tableViewDelegate->tableCellTouched(this, _touchedCell);
}
_touchedCell = nullptr;
}
ScrollView::onTouchEnded(touch, event);
}
void onMouseScroll(Event* event)
2019-11-23 20:27:39 +08:00
{
auto mouseEvent = static_cast<EventMouse*>(event);
float moveY = mouseEvent->getScrollY() * 20;
2019-11-23 20:27:39 +08:00
auto minOffset = this->minContainerOffset();
auto maxOffset = this->maxContainerOffset();
auto offset = this->getContentOffset();
offset.y += moveY;
if (offset.y < minOffset.y)
{
offset.y = minOffset.y;
}
else if (offset.y > maxOffset.y)
{
offset.y = maxOffset.y;
}
this->setContentOffset(offset);
}
protected:
TestCustomTableView()
{
auto mouseListener = EventListenerMouse::create();
2019-11-23 20:27:39 +08:00
mouseListener->onMouseScroll = CC_CALLBACK_1(TestCustomTableView::onMouseScroll, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
}
};
TestList::TestList()
{
_isTestList = true;
2019-11-23 20:27:39 +08:00
_shouldRestoreTableOffset = false;
}
TestList::~TestList()
{
deatchTableView();
}
void TestList::deatchTableView()
{
if (_tableView)
_tableView->setDataSource(nullptr);
CC_SAFE_RELEASE_NULL(_tableView);
}
void TestList::addTest(std::string_view testName, std::function<TestBase*()> callback)
2019-11-23 20:27:39 +08:00
{
if (!testName.empty())
{
_childTestNames.emplace_back(
StringUtils::format("%d:%s", static_cast<int>(_childTestNames.size() + 1), testName.data()));
2019-11-23 20:27:39 +08:00
_testCallbacks.emplace_back(callback);
}
}
void TestList::runThisTest()
{
_cellTouchEnabled = true;
/* Restore default window and design size
* Note: We should change frame and design size before your new scene create
* otherwise, the layout will incorrect
*/
GLViewImpl* glview = (GLViewImpl*)Director::getInstance()->getOpenGLView();
#if defined(CC_PLATFORM_PC)
Size resourceSize(960, 640);
glview->setWindowed(resourceSize.width, resourceSize.height);
#endif
Size designSize(480, 320);
glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
2019-11-23 20:27:39 +08:00
auto director = Director::getInstance();
auto scene = Scene::create();
2019-11-23 20:27:39 +08:00
auto visibleSize = director->getVisibleSize();
auto origin = director->getVisibleOrigin();
deatchTableView();
_tableView = TestCustomTableView::create(this, Size(400, visibleSize.height));
_tableView->retain();
_tableView->setPosition(origin.x + (visibleSize.width - 400) / 2, origin.y);
_tableView->setDirection(ScrollView::Direction::VERTICAL);
_tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
_tableView->setDelegate(this);
scene->addChild(_tableView);
_tableView->reloadData();
2019-11-23 20:27:39 +08:00
if (_shouldRestoreTableOffset)
{
_tableView->setContentOffset(_tableOffset);
2019-11-23 20:27:39 +08:00
}
if (_parentTest)
{
// Add back button.
2019-11-23 20:27:39 +08:00
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label = Label::createWithTTF(ttfConfig, "Back");
auto menuItem = MenuItemLabel::create(label, std::bind(&TestBase::backsUpOneLevel, this));
auto menu = Menu::create(menuItem, nullptr);
2019-11-23 20:27:39 +08:00
menu->setPosition(Vec2::ZERO);
menuItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));
scene->addChild(menu, 1);
}
else
{
// Add close and "Start AutoTest" button.
auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, [](Ref* sender) {
2019-11-23 20:27:39 +08:00
TestController::getInstance()->stopAutoTest();
TestController::destroyInstance();
Director::getInstance()->end();
});
closeItem->setPosition(VisibleRect::right().x - 30, VisibleRect::top().y - 30);
auto autoTestLabel = Label::createWithTTF("Start AutoTest", "fonts/arial.ttf", 16);
auto autoTestItem =
MenuItemLabel::create(autoTestLabel, [&](Ref* sender) { TestController::getInstance()->startAutoTest(); });
2019-11-23 20:27:39 +08:00
autoTestItem->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + 50));
auto menu = Menu::create(closeItem, autoTestItem, nullptr);
menu->setPosition(Vec2::ZERO);
scene->addChild(menu, 1);
}
director->replaceScene(scene);
}
void TestList::tableCellTouched(TableView* table, TableViewCell* cell)
{
if (_cellTouchEnabled)
{
auto index = cell->getIdx();
if (_testCallbacks[index])
{
auto test = _testCallbacks[index]();
if (test->getChildTestCount() > 0)
{
_tableOffset = table->getContentOffset();
2019-11-23 20:27:39 +08:00
_shouldRestoreTableOffset = true;
_cellTouchEnabled = false;
2019-11-23 20:27:39 +08:00
test->setTestParent(this);
test->runThisTest();
}
else
{
delete test;
}
}
}
}
TableViewCell* TestList::tableCellAtIndex(TableView* table, ssize_t idx)
2019-11-23 20:27:39 +08:00
{
auto cell = table->dequeueCell();
if (!cell)
{
cell = TableViewCell::create();
2019-11-23 20:27:39 +08:00
auto label = Label::createWithTTF(_childTestNames[idx], "fonts/arial.ttf", 20.0f);
label->setTag(TABEL_LABEL_TAG);
label->setPosition(200, 15);
cell->addChild(label);
}
else
{
auto label = (Label*)cell->getChildByTag(TABEL_LABEL_TAG);
label->setString(_childTestNames[idx]);
}
return cell;
}
Size TestList::tableCellSizeForIndex(TableView* table, ssize_t idx)
2019-11-23 20:27:39 +08:00
{
return Size(400, 30);
}
ssize_t TestList::numberOfCellsInTableView(TableView* table)
2019-11-23 20:27:39 +08:00
{
return _childTestNames.size();
}
// TestSuite
void TestSuite::addTestCase(std::string_view testName, std::function<Scene*()> callback)
2019-11-23 20:27:39 +08:00
{
if (!testName.empty() && callback)
{
_childTestNames.emplace_back(testName);
_testCallbacks.emplace_back(callback);
}
}
static TestCase* getTestCase(Scene* scene)
{
auto transitionScene = dynamic_cast<TransitionScene*>(scene);
TestCase* testCase = nullptr;
2019-11-23 20:27:39 +08:00
if (transitionScene)
{
testCase = dynamic_cast<TestCase*>(transitionScene->getInScene());
}
else
{
testCase = dynamic_cast<TestCase*>(scene);
}
return testCase;
}
void TestSuite::runThisTest()
{
if (!_childTestNames.empty())
{
TestController::getInstance()->setCurrTestSuite(this);
_currTestIndex = 0;
auto scene = _testCallbacks[0]();
auto testCase = getTestCase(scene);
2019-11-23 20:27:39 +08:00
testCase->setTestSuite(this);
testCase->setTestCaseName(_childTestNames[_currTestIndex]);
Director::getInstance()->replaceScene(scene);
}
}
void TestSuite::restartCurrTest()
{
auto scene = _testCallbacks[_currTestIndex]();
2019-11-23 20:27:39 +08:00
auto testCase = getTestCase(scene);
testCase->setTestSuite(this);
testCase->setTestCaseName(_childTestNames[_currTestIndex]);
Director::getInstance()->replaceScene(scene);
}
void TestSuite::enterNextTest()
{
_currTestIndex = (_currTestIndex + 1) % _childTestNames.size();
auto scene = _testCallbacks[_currTestIndex]();
2019-11-23 20:27:39 +08:00
auto testCase = getTestCase(scene);
testCase->setTestSuite(this);
testCase->setTestCaseName(_childTestNames[_currTestIndex]);
Director::getInstance()->replaceScene(scene);
}
void TestSuite::enterPreviousTest()
{
if (_currTestIndex > 0)
{
_currTestIndex -= 1;
}
else
{
_currTestIndex = (int)_childTestNames.size() - 1;
}
auto scene = _testCallbacks[_currTestIndex]();
2019-11-23 20:27:39 +08:00
auto testCase = getTestCase(scene);
testCase->setTestSuite(this);
testCase->setTestCaseName(_childTestNames[_currTestIndex]);
Director::getInstance()->replaceScene(scene);
}
// TestCase
2019-11-23 20:27:39 +08:00
TestCase::TestCase()
: _priorTestItem(nullptr)
, _restartTestItem(nullptr)
, _nextTestItem(nullptr)
, _titleLabel(nullptr)
, _subtitleLabel(nullptr)
, _testSuite(nullptr)
, _runTime(0.0f)
2019-11-23 20:27:39 +08:00
{
SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
Director::getInstance()->getTextureCache()->removeUnusedTextures();
2019-11-23 20:27:39 +08:00
this->schedule([&](float dt) { _runTime += dt; }, "AccumulatedTimeUse");
2019-11-23 20:27:39 +08:00
}
TestCase::~TestCase()
{
if (_testSuite)
{
_testSuite->release();
_testSuite = nullptr;
}
}
void TestCase::setTestSuite(TestSuite* testSuite)
{
if (_testSuite != testSuite)
{
testSuite->retain();
if (_testSuite)
{
_testSuite->release();
}
_testSuite = testSuite;
}
}
TestCase::Type TestCase::getTestType() const
{
return Type::ROBUSTNESS;
}
float TestCase::getDuration() const
{
return 0.2f;
}
bool TestCase::init()
{
if (Scene::init())
{
// add title and subtitle
TTFConfig ttfConfig("fonts/arial.ttf", 26);
_titleLabel = Label::createWithTTF(ttfConfig, title());
addChild(_titleLabel, 9999);
_titleLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 30);
2019-11-23 20:27:39 +08:00
ttfConfig.fontSize = 16;
_subtitleLabel = Label::createWithTTF(ttfConfig, subtitle());
2019-11-23 20:27:39 +08:00
_subtitleLabel->setMaxLineWidth(VisibleRect::getVisibleRect().size.width);
addChild(_subtitleLabel, 9999);
_subtitleLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60);
2019-11-23 20:27:39 +08:00
_priorTestItem = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(TestCase::priorTestCallback, this));
_restartTestItem =
MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(TestCase::restartTestCallback, this));
2019-11-23 20:27:39 +08:00
_nextTestItem = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(TestCase::nextTestCallback, this));
2019-11-23 20:27:39 +08:00
ttfConfig.fontSize = 20;
auto backLabel = Label::createWithTTF(ttfConfig, "Back");
auto backItem = MenuItemLabel::create(backLabel, CC_CALLBACK_1(TestCase::onBackCallback, this));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(_priorTestItem, _restartTestItem, _nextTestItem, backItem, nullptr);
menu->setPosition(Vec2::ZERO);
_priorTestItem->setPosition(VisibleRect::center().x - _restartTestItem->getContentSize().width * 2,
VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
_restartTestItem->setPosition(VisibleRect::center().x,
VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
_nextTestItem->setPosition(VisibleRect::center().x + _restartTestItem->getContentSize().width * 2,
VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
2019-11-23 20:27:39 +08:00
backItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));
addChild(menu, 9999);
return true;
}
return false;
}
void TestCase::onEnter()
{
Scene::onEnter();
if (_testSuite == nullptr)
{
setTestSuite(TestController::getInstance()->getCurrTestSuite());
}
if (_testSuite)
{
_titleLabel->setString(StringUtils::format("%d", static_cast<int>(_testSuite->getCurrTestIndex() + 1)) + ":" +
title());
2019-11-23 20:27:39 +08:00
}
else
{
_titleLabel->setString(title());
}
_subtitleLabel->setString(subtitle());
if (_testSuite && _testSuite->getChildTestCount() < 2)
{
_priorTestItem->setVisible(false);
_nextTestItem->setVisible(false);
_restartTestItem->setVisible(false);
}
}
void TestCase::restartTestCallback(Ref* sender)
{
if (_testSuite)
{
_testSuite->restartCurrTest();
}
}
void TestCase::nextTestCallback(Ref* sender)
{
if (_testSuite)
{
_testSuite->enterNextTest();
}
}
void TestCase::priorTestCallback(Ref* sender)
{
if (_testSuite)
{
_testSuite->enterPreviousTest();
}
}
void TestCase::onBackCallback(Ref* sender)
{
if (_testSuite)
{
_testSuite->backsUpOneLevel();
}
}