PuzzleSDK
DownloaderTest结构体 参考
+ 类 DownloaderTest 继承关系图:
+ DownloaderTest 的协作图:

Public 类型

enum  {
  TAG_TITLE = 1 , TAG_BUTTON , TAG_PROGRESS_BAR , TAG_STATUS ,
  TAG_SPRITE
}
 
- Public 类型 继承自 TestCase
enum class  Type { ROBUSTNESS , UNIT , GRAPHICAL_STATIC , MANUAL }
 

Public 成员函数

 CREATE_FUNC (DownloaderTest)
 
virtual std::string title () const override
 
 DownloaderTest ()
 
Node * createDownloadView (const char *name, const cocos2d::ui::Button::ccWidgetClickCallback &callback)
 
virtual void onEnter () override
 
- Public 成员函数 继承自 TestCase
 TestCase ()
 
 ~TestCase ()
 
virtual std::string subtitle () const
 
virtual Type getTestType () const
 
virtual float getDuration () const
 
virtual std::string getExpectedOutput () const
 
virtual std::string getActualOutput () const
 
virtual void restartTestCallback (cocos2d::Ref *sender)
 
virtual void nextTestCallback (cocos2d::Ref *sender)
 
virtual void priorTestCallback (cocos2d::Ref *sender)
 
virtual void onBackCallback (cocos2d::Ref *sender)
 
void setTestSuite (TestSuite *testSuite)
 
TestSuitegetTestSuite () const
 
float getRunTime () const
 
void setTestCaseName (const std::string &name)
 
std::string getTestCaseName () const
 
const cocos2d::Label * getSubtitleLable () const
 
const cocos2d::MenuItemImage * getRestartTestItem () const
 

Public 属性

std::unique_ptr< network::Downloader > downloader
 
- Public 属性 继承自 TestCase
CC_CONSTRUCTOR_ACCESS __pad0__: virtual bool init() override
 

额外继承的成员函数

- Protected 属性 继承自 TestCase
cocos2d::MenuItemImage * _priorTestItem
 
cocos2d::MenuItemImage * _restartTestItem
 
cocos2d::MenuItemImage * _nextTestItem
 
cocos2d::Label * _titleLabel
 
cocos2d::Label * _subtitleLabel
 

详细描述

在文件 DownloaderTest.cpp53 行定义.

成员枚举类型说明

◆ anonymous enum

anonymous enum
枚举值
TAG_TITLE 
TAG_BUTTON 
TAG_PROGRESS_BAR 
TAG_STATUS 
TAG_SPRITE 

在文件 DownloaderTest.cpp66 行定义.

构造及析构函数说明

◆ DownloaderTest()

DownloaderTest::DownloaderTest ( )
inline

在文件 DownloaderTest.cpp61 行定义.

62 {
63 downloader.reset(new network::Downloader());
64 }
std::unique_ptr< network::Downloader > downloader

引用了 downloader.

成员函数说明

◆ CREATE_FUNC()

DownloaderTest::CREATE_FUNC ( DownloaderTest  )

◆ createDownloadView()

Node * DownloaderTest::createDownloadView ( const char *  name,
const cocos2d::ui::Button::ccWidgetClickCallback &  callback 
)
inline

在文件 DownloaderTest.cpp74 行定义.

75 {
76 Size viewSize(220, 120);
77 float margin = 5;
78
79 // create background
80 auto bg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
81 bg->setContentSize(viewSize);
82
83 // add a title on the top
84 auto title = Label::createWithTTF(name,"fonts/arial.ttf",16);
85 title->setTag(TAG_TITLE);
86 title->setAnchorPoint(Vec2(0.5f, 1.0f));
87 title->setPosition(viewSize.width / 2, viewSize.height - margin);
88 bg->addChild(title, 10);
89
90 // add a button on the bottom
91 auto btn = ui::Button::create("cocosui/animationbuttonnormal.png",
92 "cocosui/animationbuttonpressed.png");
93 btn->setTag(TAG_BUTTON);
94 btn->setTitleText("Download");
95 btn->setAnchorPoint(Vec2(0.5f, 0.0f));
96 btn->setPosition(Vec2(viewSize.width / 2, margin));
97 btn->addClickEventListener(callback);
98 bg->addChild(btn, 10);
99
100 // add a progress bar
101 auto bar = ui::LoadingBar::create("ccs-res/cocosui/sliderProgress.png");
102 bar->setTag(TAG_PROGRESS_BAR);
103 bar->ignoreContentAdaptWithSize(false);
104 bar->setAnchorPoint(Vec2(0.5f, 0.0f));
105 bar->setContentSize(Size(viewSize.width - margin * 2, btn->getContentSize().height));
106 bar->setPosition(btn->getPosition());
107 bar->setVisible(false);
108 bg->addChild(bar, 10);
109
110 // add a status label
111 auto label = Label::createWithTTF("","fonts/arial.ttf",14);
112 label->setTag(TAG_STATUS);
113 label->setAnchorPoint(Vec2(0.5f, 0.5f));
114 label->setPosition(Vec2(viewSize.width / 2, viewSize.height / 2));
115 label->setContentSize(Size(viewSize.width, 0.0f));
116 label->setAlignment(TextHAlignment::CENTER, TextVAlignment::CENTER);
117 label->setDimensions(viewSize.width, viewSize.height);
118 bg->addChild(label, 20);
119
120 return bg;
121 }
virtual std::string title() const override

引用了 TAG_BUTTON, TAG_PROGRESS_BAR, TAG_STATUS, TAG_TITLE , 以及 title().

被这些函数引用 onEnter().

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

◆ onEnter()

virtual void DownloaderTest::onEnter ( )
inlineoverridevirtual

重载 TestCase .

在文件 DownloaderTest.cpp123 行定义.

124 {
126 _restartTestItem->setVisible(true);
127
128 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
129
130 // add four download view in test case
131 Node* topRightView = createDownloadView(sNameList[0], [this](Ref*)
132 {
133 auto view = this->getChildByName(sNameList[0]);
134 auto sprite = view->getChildByTag(TAG_SPRITE);
135 if (sprite)
136 {
137 sprite->removeFromParentAndCleanup(true);
138 }
139 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
140 btn->setEnabled(false);
141 btn->setVisible(false);
142 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
143 bar->setPercent(0);
144 bar->setVisible(true);
145 this->downloader->createDownloadDataTask(sURLList[0], sNameList[0]);
146 });
147 topRightView->setName(sNameList[0]);
148 topRightView->setAnchorPoint(Vec2(0, 0));
149 topRightView->setPosition(VisibleRect::center());
150 this->addChild(topRightView);
151
152 Node* topLeftView = createDownloadView(sNameList[1], [this](Ref*)
153 {
154 auto view = this->getChildByName(sNameList[1]);
155 auto sprite = view->getChildByTag(TAG_SPRITE);
156 if (sprite)
157 {
158 sprite->removeFromParentAndCleanup(true);
159 }
160 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
161 btn->setEnabled(false);
162 btn->setVisible(false);
163 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
164 bar->setPercent(0);
165 bar->setVisible(true);
166 bar->setEnabled(true);
167 this->downloader->createDownloadDataTask(sURLList[1], sNameList[1]);
168 });
169 topLeftView->setName(sNameList[1]);
170 topLeftView->setAnchorPoint(Vec2(1.0f, 0.0f));
171 topLeftView->setPosition(VisibleRect::center());
172 this->addChild(topLeftView);
173
174 Node* bottomLeftView = createDownloadView(sNameList[2], [this](Ref*)
175 {
176 auto view = this->getChildByName(sNameList[2]);
177 auto sprite = view->getChildByTag(TAG_SPRITE);
178 if (sprite)
179 {
180 sprite->removeFromParentAndCleanup(true);
181 }
182 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
183 btn->setEnabled(false);
184 btn->setVisible(false);
185 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
186 bar->setPercent(0);
187 bar->setVisible(true);
188 bar->setEnabled(true);
189 auto path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/" + sNameList[2];
190 this->downloader->createDownloadFileTask(sURLList[2], path, sNameList[2]);
191 });
192 bottomLeftView->setName(sNameList[2]);
193 bottomLeftView->setAnchorPoint(Vec2(1, 1));
194 bottomLeftView->setPosition(VisibleRect::center());
195 this->addChild(bottomLeftView);
196
197 Node* bottomRightView = createDownloadView(sNameList[3], [this](Ref*)
198 {
199 auto view = this->getChildByName(sNameList[3]);
200 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
201 btn->setEnabled(false);
202 btn->setVisible(false);
203 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
204 bar->setPercent(0);
205 bar->setVisible(true);
206 bar->setEnabled(true);
207 auto path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/" + sNameList[3];
208 this->downloader->createDownloadFileTask(sURLList[3], path, sNameList[3]);
209 });
210 bottomRightView->setName(sNameList[3]);
211 bottomRightView->setAnchorPoint(Vec2(0, 1));
212 bottomRightView->setPosition(VisibleRect::center());
213 this->addChild(bottomRightView);
214
215 // define progress callback
216 downloader->onTaskProgress = [this](const network::DownloadTask& task,
217 int64_t bytesReceived,
218 int64_t totalBytesReceived,
219 int64_t totalBytesExpected)
220 {
221 Node* view = this->getChildByName(task.identifier);
222 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
223 float percent = float(totalBytesReceived * 100) / totalBytesExpected;
224 bar->setPercent(percent);
225 char buf[32];
226 sprintf(buf, "%.1f%%[total %d KB]", percent, int(totalBytesExpected/1024));
227 auto status = (Label*)view->getChildByTag(TAG_STATUS);
228 status->setString(buf);
229 };
230
231 // define success callback
232 downloader->onDataTaskSuccess = [this](const cocos2d::network::DownloadTask& task,
233 std::vector<unsigned char>& data)
234 {
235 // create texture from data
236 Texture2D* texture = nullptr;
237 do
238 {
239 Image img;
240 if (false == img.initWithImageData(data.data(), data.size()))
241 {
242 break;
243 }
244
245 texture = new Texture2D();
246 if (false == texture->initWithImage(&img))
247 {
248 break;
249 }
250 auto sprite = Sprite::createWithTexture(texture);
251 auto view = this->getChildByName(task.identifier);
252 auto viewSize = view->getContentSize();
253 sprite->setPosition(viewSize.width / 2, viewSize.height / 2);
254 auto spriteSize = sprite->getContentSize();
255 float scale = MIN((viewSize.height - 20) / spriteSize.height, (viewSize.width - 20) / spriteSize.width);
256 sprite->setScale(scale);
257 view->addChild(sprite, 5, TAG_SPRITE);
258
259 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
260 btn->setEnabled(true);
261 btn->setVisible(true);
262 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
263 bar->setVisible(false);
264 } while (0);
265 CC_SAFE_RELEASE(texture);
266 };
267
268 downloader->onFileTaskSuccess = [this](const cocos2d::network::DownloadTask& task)
269 {
270 Texture2D* texture = nullptr;
271 do
272 {
273 auto view = this->getChildByName(task.identifier);
274 if (std::string::npos == task.storagePath.find(".png"))
275 {
276 // download big file success
277 char buf[32];
278 sprintf(buf, "Download [%s] success.", task.identifier.c_str());
279 auto status = (Label*)view->getChildByTag(TAG_STATUS);
280 status->setString(buf);
281 break;
282 }
283 // create sprite from file
284 auto sprite = Sprite::create(task.storagePath);
285 auto viewSize = view->getContentSize();
286 sprite->setPosition(viewSize.width / 2, viewSize.height / 2);
287 auto spriteSize = sprite->getContentSize();
288 float scale = MIN((viewSize.height - 20) / spriteSize.height, (viewSize.width - 20) / spriteSize.width);
289 sprite->setScale(scale);
290 view->addChild(sprite, 5, TAG_SPRITE);
291
292 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
293 btn->setEnabled(true);
294 btn->setVisible(true);
295 auto bar = (ui::LoadingBar*)view->getChildByTag(TAG_PROGRESS_BAR);
296 bar->setVisible(false);
297 } while (0);
298 CC_SAFE_RELEASE(texture);
299 };
300
301 // define failed callback
302 downloader->onTaskError = [this](const cocos2d::network::DownloadTask& task,
303 int errorCode,
304 int errorCodeInternal,
305 const std::string& errorStr)
306 {
307 log("Failed to download : %s, identifier(%s) error code(%d), internal error code(%d) desc(%s)"
308 , task.requestURL.c_str()
309 , task.identifier.c_str()
310 , errorCode
311 , errorCodeInternal
312 , errorStr.c_str());
313 auto view = this->getChildByName(task.identifier);
314 auto status = (Label*)view->getChildByTag(TAG_STATUS);
315 status->setString(errorStr.length() ? errorStr : "Download failed.");
316
317 auto btn = (ui::Button*)view->getChildByTag(TAG_BUTTON);
318 btn->setEnabled(true);
319 btn->setVisible(true);
320 };
321 }
static const char * sNameList[sListSize]
static const char * sURLList[]
cocos2d::MenuItemImage * _restartTestItem
Definition: BaseTest.h:103
virtual void onEnter() override
Definition: BaseTest.cpp:430
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
Node * createDownloadView(const char *name, const cocos2d::ui::Button::ccWidgetClickCallback &callback)
static const char s_s9s_ui_plist[]
Definition: testResource.h:70

引用了 TestCase::_restartTestItem, VisibleRect::center(), createDownloadView(), downloader, TestCase::onEnter(), s_s9s_ui_plist, sNameList, sURLList, TAG_BUTTON, TAG_PROGRESS_BAR, TAG_SPRITE , 以及 TAG_STATUS.

+ 函数调用图:

◆ title()

virtual std::string DownloaderTest::title ( ) const
inlineoverridevirtual

重载 TestCase .

在文件 DownloaderTest.cpp57 行定义.

57{ return "Downloader Test"; }

被这些函数引用 createDownloadView().

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

类成员变量说明

◆ downloader

std::unique_ptr<network::Downloader> DownloaderTest::downloader

在文件 DownloaderTest.cpp59 行定义.

被这些函数引用 DownloaderTest() , 以及 onEnter().


该结构体的文档由以下文件生成: