PuzzleSDK
BaseTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2013-2016 Chukong Technologies Inc.
3 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4
5 http://www.cocos2d-x.org
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 ****************************************************************************/
25
26#include "BaseTest.h"
27#include "testResource.h"
28#include "controller.h"
29
32
33#define TABEL_LABEL_TAG 1024
34
36: _parentTest(nullptr)
37, _isTestList(false)
38{
39
40}
41
43{
44
45}
46
48{
49 if (_parentTest)
50 {
52 this->release();
53 }
54}
55
56//TestList
57class TestCustomTableView : public TableView
58{
59public:
60 static TestCustomTableView* create(TableViewDataSource* dataSource, Size size)
61 {
62 auto table = new (std::nothrow) TestCustomTableView();
63 table->initWithViewSize(size, nullptr);
64 table->autorelease();
65 table->setDataSource(dataSource);
66 table->_updateCellPositions();
67 table->_updateContentSize();
68
69 return table;
70 }
71
72 virtual void onTouchEnded(Touch *touch, Event *event) override
73 {
74 if (!this->isVisible())
75 {
76 return;
77 }
78
79 if (_touchedCell)
80 {
81 auto label = (Label*)_touchedCell->getChildByTag(TABEL_LABEL_TAG);
82
83 Rect bbox = label->getBoundingBox();
84 bbox.origin = _touchedCell->convertToWorldSpace(bbox.origin);
85
86 if (bbox.containsPoint(touch->getLocation()) && _tableViewDelegate != nullptr)
87 {
88 _tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
89 _tableViewDelegate->tableCellTouched(this, _touchedCell);
90 }
91
92 _touchedCell = nullptr;
93 }
94
95 ScrollView::onTouchEnded(touch, event);
96 }
97
98 void onMouseScroll(Event *event)
99 {
100 auto mouseEvent = static_cast<EventMouse*>(event);
101 float moveY = mouseEvent->getScrollY() * 20;
102
103 auto minOffset = this->minContainerOffset();
104 auto maxOffset = this->maxContainerOffset();
105
106 auto offset = this->getContentOffset();
107 offset.y += moveY;
108
109 if (offset.y < minOffset.y)
110 {
111 offset.y = minOffset.y;
112 }
113 else if (offset.y > maxOffset.y)
114 {
115 offset.y = maxOffset.y;
116 }
117 this->setContentOffset(offset);
118 }
119
120protected:
122 {
123 auto mouseListener = EventListenerMouse::create();
124 mouseListener->onMouseScroll = CC_CALLBACK_1(TestCustomTableView::onMouseScroll, this);
125 _eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
126 }
127};
128
130{
131 _isTestList = true;
133}
134
135void TestList::addTest(const std::string& testName, std::function<TestBase*()> callback)
136{
137 if (!testName.empty())
138 {
139 _childTestNames.emplace_back(StringUtils::format("%d", static_cast<int>(_childTestNames.size() + 1)) + ":" + testName);
140 _testCallbacks.emplace_back(callback);
141 }
142}
143
145{
146 _cellTouchEnabled = true;
147 auto director = Director::getInstance();
148 auto scene = Scene::create();
149
150 auto visibleSize = director->getVisibleSize();
151 auto origin = director->getVisibleOrigin();
152
153 auto tableView = TestCustomTableView::create(this, Size(400, visibleSize.height));
154 tableView->setPosition(origin.x + (visibleSize.width - 400) / 2, origin.y);
155 tableView->setDirection(ScrollView::Direction::VERTICAL);
156 tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
157 tableView->setDelegate(this);
158 scene->addChild(tableView);
159 tableView->reloadData();
160
162 {
163 tableView->setContentOffset(_tableOffset);
164 }
165
166 if (_parentTest)
167 {
168 //Add back button.
169 TTFConfig ttfConfig("fonts/arial.ttf", 20);
170 auto label = Label::createWithTTF(ttfConfig, "Back");
171
172 auto menuItem = MenuItemLabel::create(label, std::bind(&TestBase::backsUpOneLevel, this));
173 auto menu = Menu::create(menuItem, nullptr);
174
175 menu->setPosition(Vec2::ZERO);
176 menuItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));
177
178 scene->addChild(menu, 1);
179 }
180 else
181 {
182 //Add close and "Start AutoTest" button.
183 auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, [](Ref* sender){
186 Director::getInstance()->end();
187 });
188 closeItem->setPosition(VisibleRect::right().x - 30, VisibleRect::top().y - 30);
189
190 auto autoTestLabel = Label::createWithTTF("Start AutoTest","fonts/arial.ttf",16);
191 auto autoTestItem = MenuItemLabel::create(autoTestLabel, [&](Ref* sender){
193 });
194 autoTestItem->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + 50));
195
196 auto menu = Menu::create(closeItem, autoTestItem, nullptr);
197 menu->setPosition(Vec2::ZERO);
198 scene->addChild(menu, 1);
199 }
200
201 director->replaceScene(scene);
202}
203
204void TestList::tableCellTouched(TableView* table, TableViewCell* cell)
205{
207 {
208 auto index = cell->getIdx();
209 if (_testCallbacks[index])
210 {
211 auto test = _testCallbacks[index]();
212 if (test->getChildTestCount() > 0)
213 {
214 _tableOffset = table->getContentOffset();
216 _cellTouchEnabled = false;
217 test->setTestParent(this);
218 test->runThisTest();
219 }
220 else
221 {
222 delete test;
223 }
224 }
225 }
226}
227
228TableViewCell* TestList::tableCellAtIndex(TableView *table, ssize_t idx)
229{
230 auto cell = table->dequeueCell();
231 if (!cell)
232 {
233 cell = TableViewCell::create();
234 auto label = Label::createWithTTF(_childTestNames[idx], "fonts/arial.ttf", 20.0f);
235 label->setTag(TABEL_LABEL_TAG);
236 label->setPosition(200, 15);
237 cell->addChild(label);
238 }
239 else
240 {
241 auto label = (Label*)cell->getChildByTag(TABEL_LABEL_TAG);
242 label->setString(_childTestNames[idx]);
243 }
244
245 return cell;
246}
247
248Size TestList::tableCellSizeForIndex(TableView *table, ssize_t idx)
249{
250 return Size(400, 30);
251}
252
253ssize_t TestList::numberOfCellsInTableView(TableView *table)
254{
255 return _childTestNames.size();
256}
257
258//TestSuite
259void TestSuite::addTestCase(const std::string& testName, std::function<Scene*()> callback)
260{
261 if (!testName.empty() && callback)
262 {
263 _childTestNames.emplace_back(testName);
264 _testCallbacks.emplace_back(callback);
265 }
266}
267
268static TestCase* getTestCase(Scene* scene)
269{
270 auto transitionScene = dynamic_cast<TransitionScene*>(scene);
271 TestCase* testCase = nullptr;
272 if (transitionScene)
273 {
274 testCase = dynamic_cast<TestCase*>(transitionScene->getInScene());
275 }
276 else
277 {
278 testCase = dynamic_cast<TestCase*>(scene);
279 }
280
281 return testCase;
282}
283
285{
286 if (!_childTestNames.empty())
287 {
289
290 _currTestIndex = 0;
291 auto scene = _testCallbacks[0]();
292 auto testCase = getTestCase(scene);
293 testCase->setTestSuite(this);
294 testCase->setTestCaseName(_childTestNames[_currTestIndex]);
295 Director::getInstance()->replaceScene(scene);
296 }
297}
298
300{
301 auto scene = _testCallbacks[_currTestIndex]();
302 auto testCase = getTestCase(scene);
303 testCase->setTestSuite(this);
304 testCase->setTestCaseName(_childTestNames[_currTestIndex]);
305
306 Director::getInstance()->replaceScene(scene);
307}
308
310{
312
313 auto scene = _testCallbacks[_currTestIndex]();
314 auto testCase = getTestCase(scene);
315 testCase->setTestSuite(this);
316 testCase->setTestCaseName(_childTestNames[_currTestIndex]);
317
318 Director::getInstance()->replaceScene(scene);
319}
320
322{
323 if (_currTestIndex > 0)
324 {
325 _currTestIndex -= 1;
326 }
327 else
328 {
329 _currTestIndex = (int)_childTestNames.size() - 1;
330 }
331
332 auto scene = _testCallbacks[_currTestIndex]();
333 auto testCase = getTestCase(scene);
334 testCase->setTestSuite(this);
335 testCase->setTestCaseName(_childTestNames[_currTestIndex]);
336
337 Director::getInstance()->replaceScene(scene);
338}
339
340//TestCase
342: _priorTestItem(nullptr)
343, _restartTestItem(nullptr)
344, _nextTestItem(nullptr)
345, _titleLabel(nullptr)
346, _subtitleLabel(nullptr)
347, _testSuite(nullptr)
348, _runTime(0.0f)
349{
350 Director::getInstance()->getTextureCache()->removeUnusedTextures();
351 SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
352
353 this->schedule([&](float dt){
354 _runTime += dt;
355 }, "AccumulatedTimeUse");
356}
357
359{
360 if (_testSuite)
361 {
362 _testSuite->release();
363 _testSuite = nullptr;
364 }
365}
366
368{
369 if (_testSuite != testSuite)
370 {
371 testSuite->retain();
372 if (_testSuite)
373 {
374 _testSuite->release();
375 }
376 _testSuite = testSuite;
377 }
378}
379
381{
382 return Type::ROBUSTNESS;
383}
384
386{
387 return 0.2f;
388}
389
390bool TestCase::init()
391{
392 if (Scene::init())
393 {
394 // add title and subtitle
395 TTFConfig ttfConfig("fonts/arial.ttf", 26);
396 _titleLabel = Label::createWithTTF(ttfConfig, title());
397 addChild(_titleLabel, 9999);
398 _titleLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 30);
399
400 ttfConfig.fontSize = 16;
401 _subtitleLabel = Label::createWithTTF(ttfConfig, subtitle());
402 _subtitleLabel->setMaxLineWidth(VisibleRect::getVisibleRect().size.width);
403 addChild(_subtitleLabel, 9999);
404 _subtitleLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60);
405
406 _priorTestItem = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(TestCase::priorTestCallback, this));
407 _restartTestItem = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(TestCase::restartTestCallback, this));
408 _nextTestItem = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(TestCase::nextTestCallback, this));
409
410 ttfConfig.fontSize = 20;
411 auto backLabel = Label::createWithTTF(ttfConfig, "Back");
412 auto backItem = MenuItemLabel::create(backLabel, CC_CALLBACK_1(TestCase::onBackCallback, this));
413
414 auto menu = Menu::create(_priorTestItem, _restartTestItem, _nextTestItem, backItem, nullptr);
415
416 menu->setPosition(Vec2::ZERO);
417 _priorTestItem->setPosition(VisibleRect::center().x - _restartTestItem->getContentSize().width * 2, VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
418 _restartTestItem->setPosition(VisibleRect::center().x, VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
419 _nextTestItem->setPosition(VisibleRect::center().x + _restartTestItem->getContentSize().width * 2, VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
420 backItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));
421
422 addChild(menu, 9999);
423
424 return true;
425 }
426
427 return false;
428}
429
431{
432 Scene::onEnter();
433
434 if (_testSuite == nullptr)
435 {
436 setTestSuite(TestController::getInstance()->getCurrTestSuite());
437 }
438
439 if (_testSuite)
440 {
441 _titleLabel->setString(StringUtils::format("%d", static_cast<int>(_testSuite->getCurrTestIndex() + 1)) + ":" + title());
442 }
443 else
444 {
445 _titleLabel->setString(title());
446 }
447 _subtitleLabel->setString(subtitle());
448
450 {
451 _priorTestItem->setVisible(false);
452 _nextTestItem->setVisible(false);
453 _restartTestItem->setVisible(false);
454 }
455}
456
458{
459 if (_testSuite)
460 {
462 }
463}
464
466{
467 if (_testSuite)
468 {
470 }
471}
472
474{
475 if (_testSuite)
476 {
478 }
479}
480
482{
483 if (_testSuite)
484 {
486 }
487}
#define TABEL_LABEL_TAG
Definition: BaseTest.cpp:33
static TestCase * getTestCase(Scene *scene)
Definition: BaseTest.cpp:268
USING_NS_CC_EXT
Definition: BaseTest.cpp:31
USING_NS_CC
Definition: BaseTest.cpp:30
ssize_t getChildTestCount()
Definition: BaseTest.h:135
void backsUpOneLevel()
Definition: BaseTest.cpp:47
TestBase * _parentTest
Definition: BaseTest.h:149
virtual ~TestBase()
Definition: BaseTest.cpp:42
virtual void runThisTest()
Definition: BaseTest.h:131
std::vector< std::string > _childTestNames
Definition: BaseTest.h:151
TestBase()
Definition: BaseTest.cpp:35
bool _isTestList
Definition: BaseTest.h:150
virtual std::string title() const
Definition: BaseTest.h:59
cocos2d::MenuItemImage * _restartTestItem
Definition: BaseTest.h:103
TestSuite * _testSuite
Definition: BaseTest.h:110
cocos2d::MenuItemImage * _priorTestItem
Definition: BaseTest.h:102
float _runTime
Definition: BaseTest.h:111
cocos2d::MenuItemImage * _nextTestItem
Definition: BaseTest.h:104
virtual Type getTestType() const
Definition: BaseTest.cpp:380
virtual std::string subtitle() const
Definition: BaseTest.h:60
virtual void onEnter() override
Definition: BaseTest.cpp:430
virtual float getDuration() const
Definition: BaseTest.cpp:385
virtual void restartTestCallback(cocos2d::Ref *sender)
Definition: BaseTest.cpp:457
cocos2d::Label * _subtitleLabel
Definition: BaseTest.h:107
void setTestSuite(TestSuite *testSuite)
Definition: BaseTest.cpp:367
virtual void onBackCallback(cocos2d::Ref *sender)
Definition: BaseTest.cpp:481
virtual void priorTestCallback(cocos2d::Ref *sender)
Definition: BaseTest.cpp:473
cocos2d::Label * _titleLabel
Definition: BaseTest.h:106
virtual void nextTestCallback(cocos2d::Ref *sender)
Definition: BaseTest.cpp:465
void startAutoTest()
Definition: controller.cpp:157
static void destroyInstance()
Definition: controller.cpp:470
void stopAutoTest()
Definition: controller.cpp:168
static TestController * getInstance()
Definition: controller.cpp:458
void setCurrTestSuite(TestSuite *testSuite)
Definition: controller.h:64
void onMouseScroll(Event *event)
Definition: BaseTest.cpp:98
virtual void onTouchEnded(Touch *touch, Event *event) override
Definition: BaseTest.cpp:72
static TestCustomTableView * create(TableViewDataSource *dataSource, Size size)
Definition: BaseTest.cpp:60
virtual void runThisTest() override
Definition: BaseTest.cpp:144
std::vector< std::function< TestBase *()> > _testCallbacks
Definition: BaseTest.h:201
virtual cocos2d::extension::TableViewCell * tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx) override
Definition: BaseTest.cpp:228
cocos2d::Vec2 _tableOffset
Definition: BaseTest.h:204
virtual cocos2d::Size tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) override
Definition: BaseTest.cpp:248
bool _cellTouchEnabled
Definition: BaseTest.h:202
virtual void tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell) override
Definition: BaseTest.cpp:204
virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table) override
Definition: BaseTest.cpp:253
bool _shouldRestoreTableOffset
Definition: BaseTest.h:203
void addTest(const std::string &testName, std::function< TestBase *()> callback)
Definition: BaseTest.cpp:135
std::vector< std::function< cocos2d::Scene *()> > _testCallbacks
Definition: BaseTest.h:173
void addTestCase(const std::string &testName, std::function< cocos2d::Scene *()> callback)
Definition: BaseTest.cpp:259
virtual void runThisTest() override
Definition: BaseTest.cpp:284
int getCurrTestIndex()
Definition: BaseTest.h:169
virtual void restartCurrTest()
Definition: BaseTest.cpp:299
int _currTestIndex
Definition: BaseTest.h:175
virtual void enterNextTest()
Definition: BaseTest.cpp:309
virtual void enterPreviousTest()
Definition: BaseTest.cpp:321
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Rect getVisibleRect()
Definition: VisibleRect.cpp:39
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45
static cocos2d::Vec2 right()
Definition: VisibleRect.cpp:51
static const char s_pathClose[]
Definition: testResource.h:55
static const char s_pathB2[]
Definition: testResource.h:32
static const char s_pathB1[]
Definition: testResource.h:31
static const char s_pathF1[]
Definition: testResource.h:35
static const char s_pathR1[]
Definition: testResource.h:33
static const char s_pathR2[]
Definition: testResource.h:34
static const char s_pathF2[]
Definition: testResource.h:36