PuzzleSDK
BaseTest.h
浏览该文件的文档.
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#ifndef _CPPTESTS_BASETEST_H__
27#define _CPPTESTS_BASETEST_H__
28
29#include "cocos2d.h"
30 #include "extensions/cocos-ext.h"
31#include "VisibleRect.h"
32
33class TestSuite;
34
38class TestCase : public cocos2d::Scene
39{
40public:
42 enum class Type
43 {
50 UNIT,
54 MANUAL
55 };
56 TestCase();
57 ~TestCase();
58
59 virtual std::string title() const { return ""; }
60 virtual std::string subtitle() const { return ""; }
61
63 virtual Type getTestType() const;
65 virtual float getDuration() const;
66
67
69 virtual std::string getExpectedOutput() const { return ""; }
71 virtual std::string getActualOutput() const { return ""; }
72
74 virtual void restartTestCallback(cocos2d::Ref* sender);
75 virtual void nextTestCallback(cocos2d::Ref* sender);
76 virtual void priorTestCallback(cocos2d::Ref* sender);
77 virtual void onBackCallback(cocos2d::Ref* sender);
78
82 void setTestSuite(TestSuite* testSuite);
83 TestSuite* getTestSuite() const { return _testSuite; }
84
86 float getRunTime() const { return _runTime; }
87
91 void setTestCaseName(const std::string& name) { _testCaseName = name; }
92 std::string getTestCaseName() const { return _testCaseName; }
93
94 const cocos2d::Label* getSubtitleLable() const { return _subtitleLabel; }
95 const cocos2d::MenuItemImage* getRestartTestItem() const { return _restartTestItem; }
96
97 virtual void onEnter() override;
98CC_CONSTRUCTOR_ACCESS:
99 virtual bool init() override;
100
101protected:
102 cocos2d::MenuItemImage* _priorTestItem;
103 cocos2d::MenuItemImage* _restartTestItem;
104 cocos2d::MenuItemImage* _nextTestItem;
105
106 cocos2d::Label* _titleLabel;
107 cocos2d::Label* _subtitleLabel;
108
109private:
111 float _runTime;
112 std::string _testCaseName;
113};
114
123class TestBase : public cocos2d::Ref
124{
125public:
126 virtual ~TestBase();
127
129 void backsUpOneLevel();
130
131 virtual void runThisTest() {}
132
133 bool isTestList() { return _isTestList; }
134
135 ssize_t getChildTestCount() { return _childTestNames.size(); }
136
140 void setTestParent(TestBase* parent) { _parentTest = parent; }
142
143 void setTestName(const std::string& testName) { _testName = testName; }
144 std::string getTestName() const { return _testName; }
145protected:
146 TestBase();
147
148 std::string _testName;
151 std::vector<std::string> _childTestNames;
152};
153
154class TestController;
155
160class TestSuite : public TestBase
161{
162public:
163 void addTestCase(const std::string& testName, std::function<cocos2d::Scene*()> callback);
164
165 virtual void restartCurrTest();
166 virtual void enterNextTest();
167 virtual void enterPreviousTest();
168
170 virtual void runThisTest() override;
171
172private:
173 std::vector<std::function<cocos2d::Scene*()>> _testCallbacks;
174
176 friend class TestController;
177};
178
182class TestList : public TestBase, public cocos2d::extension::TableViewDataSource, public cocos2d::extension::TableViewDelegate
183{
184public:
185 TestList();
186
187 void addTest(const std::string& testName, std::function<TestBase*()> callback);
188
189 virtual void runThisTest() override;
190
191
192 virtual void tableCellTouched(cocos2d::extension::TableView* table, cocos2d::extension::TableViewCell* cell) override;
193 virtual cocos2d::extension::TableViewCell* tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx) override;
194 virtual cocos2d::Size tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) override;
195 virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table) override;
196
197 virtual void scrollViewDidScroll(cocos2d::extension::ScrollView* view) override{}
198 virtual void scrollViewDidZoom(cocos2d::extension::ScrollView* view) override{}
199
200private:
201 std::vector<std::function<TestBase*()>> _testCallbacks;
204 cocos2d::Vec2 _tableOffset;
205 friend class TestController;
206};
207
208
209#define ADD_TEST(__className__) addTest( #__className__, [](){ return new (std::nothrow) __className__;} );
210
211#define ADD_TEST_CASE(__className__) addTestCase( #__className__, [](){ return __className__::create();} );
212
213#define DEFINE_TEST_LIST(__className__) class __className__ : public TestList { public: __className__();}
214
215#define DEFINE_TEST_SUITE(__className__) class __className__ : public TestSuite { public: __className__();}
216
217
222class BaseTest : public cocos2d::Layer
223{
224public:
225 virtual std::string title() const { return ""; }
226 virtual std::string subtitle() const{ return ""; }
227
228 virtual void restartCallback(cocos2d::Ref* sender) {}
229 virtual void nextCallback(cocos2d::Ref* sender){}
230 virtual void backCallback(cocos2d::Ref* sender){}
231
232 virtual void onEnter() override{}
233 virtual void onExit() override{}
234};
235
236#endif /* defined(_CPPTESTS_BASETEST_H__) */
virtual void nextCallback(cocos2d::Ref *sender)
Definition: BaseTest.h:229
virtual void backCallback(cocos2d::Ref *sender)
Definition: BaseTest.h:230
virtual std::string title() const
Definition: BaseTest.h:225
virtual void onExit() override
Definition: BaseTest.h:233
virtual void restartCallback(cocos2d::Ref *sender)
Definition: BaseTest.h:228
virtual void onEnter() override
Definition: BaseTest.h:232
virtual std::string subtitle() const
Definition: BaseTest.h:226
ssize_t getChildTestCount()
Definition: BaseTest.h:135
void backsUpOneLevel()
Definition: BaseTest.cpp:47
void setTestName(const std::string &testName)
Definition: BaseTest.h:143
TestBase * _parentTest
Definition: BaseTest.h:149
virtual ~TestBase()
Definition: BaseTest.cpp:42
void setTestParent(TestBase *parent)
Definition: BaseTest.h:140
virtual void runThisTest()
Definition: BaseTest.h:131
bool isTestList()
Definition: BaseTest.h:133
TestBase * getTestParent()
Definition: BaseTest.h:141
std::string _testName
Definition: BaseTest.h:148
std::string getTestName() const
Definition: BaseTest.h:144
std::vector< std::string > _childTestNames
Definition: BaseTest.h:151
TestBase()
Definition: BaseTest.cpp:35
bool _isTestList
Definition: BaseTest.h:150
virtual std::string getExpectedOutput() const
Definition: BaseTest.h:69
float getRunTime() const
Definition: BaseTest.h:86
virtual std::string title() const
Definition: BaseTest.h:59
cocos2d::MenuItemImage * _restartTestItem
Definition: BaseTest.h:103
TestSuite * _testSuite
Definition: BaseTest.h:110
const cocos2d::Label * getSubtitleLable() const
Definition: BaseTest.h:94
cocos2d::MenuItemImage * _priorTestItem
Definition: BaseTest.h:102
float _runTime
Definition: BaseTest.h:111
cocos2d::MenuItemImage * _nextTestItem
Definition: BaseTest.h:104
std::string getTestCaseName() const
Definition: BaseTest.h:92
virtual Type getTestType() const
Definition: BaseTest.cpp:380
void setTestCaseName(const std::string &name)
Definition: BaseTest.h:91
const cocos2d::MenuItemImage * getRestartTestItem() const
Definition: BaseTest.h:95
TestSuite * getTestSuite() const
Definition: BaseTest.h:83
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
virtual std::string getActualOutput() const
Definition: BaseTest.h:71
cocos2d::Label * _subtitleLabel
Definition: BaseTest.h:107
std::string _testCaseName
Definition: BaseTest.h:112
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
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 void scrollViewDidZoom(cocos2d::extension::ScrollView *view) override
Definition: BaseTest.h:198
virtual cocos2d::Size tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) override
Definition: BaseTest.cpp:248
bool _cellTouchEnabled
Definition: BaseTest.h:202
virtual void scrollViewDidScroll(cocos2d::extension::ScrollView *view) override
Definition: BaseTest.h:197
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