29#include "../testResource.h"
31#include "ui/UILoadingBar.h"
32#include "ui/UIButton.h"
33#include "network/CCDownloader.h"
39 "https://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png",
40 "https://cocos2d-x.org/images/logo.png",
41 "https://www.cocos2d-x.org/attachments/1503/no_exist.txt",
42 "https://github.com/openssl/openssl/archive/OpenSSL_1_1_1a.zip",
47 "cocos2dx_landscape.png",
57 virtual std::string
title()
const override {
return "Downloader Test"; }
74 Node*
createDownloadView(
const char *name,
const cocos2d::ui::Button::ccWidgetClickCallback &callback)
76 Size viewSize(220, 120);
80 auto bg = ui::Scale9Sprite::createWithSpriteFrameName(
"button_actived.png");
81 bg->setContentSize(viewSize);
84 auto title = Label::createWithTTF(name,
"fonts/arial.ttf",16);
86 title->setAnchorPoint(Vec2(0.5f, 1.0f));
87 title->setPosition(viewSize.width / 2, viewSize.height - margin);
88 bg->addChild(
title, 10);
91 auto btn = ui::Button::create(
"cocosui/animationbuttonnormal.png",
92 "cocosui/animationbuttonpressed.png");
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);
101 auto bar = ui::LoadingBar::create(
"ccs-res/cocosui/sliderProgress.png");
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);
111 auto label = Label::createWithTTF(
"",
"fonts/arial.ttf",14);
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);
128 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
s_s9s_ui_plist);
133 auto view = this->getChildByName(
sNameList[0]);
134 auto sprite = view->getChildByTag(
TAG_SPRITE);
137 sprite->removeFromParentAndCleanup(
true);
139 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
140 btn->setEnabled(
false);
141 btn->setVisible(
false);
144 bar->setVisible(
true);
148 topRightView->setAnchorPoint(Vec2(0, 0));
150 this->addChild(topRightView);
154 auto view = this->getChildByName(
sNameList[1]);
155 auto sprite = view->getChildByTag(
TAG_SPRITE);
158 sprite->removeFromParentAndCleanup(
true);
160 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
161 btn->setEnabled(
false);
162 btn->setVisible(
false);
165 bar->setVisible(
true);
166 bar->setEnabled(
true);
170 topLeftView->setAnchorPoint(Vec2(1.0f, 0.0f));
172 this->addChild(topLeftView);
176 auto view = this->getChildByName(
sNameList[2]);
177 auto sprite = view->getChildByTag(
TAG_SPRITE);
180 sprite->removeFromParentAndCleanup(
true);
182 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
183 btn->setEnabled(
false);
184 btn->setVisible(
false);
187 bar->setVisible(
true);
188 bar->setEnabled(
true);
189 auto path = FileUtils::getInstance()->getWritablePath() +
"CppTests/DownloaderTest/" +
sNameList[2];
193 bottomLeftView->setAnchorPoint(Vec2(1, 1));
195 this->addChild(bottomLeftView);
199 auto view = this->getChildByName(
sNameList[3]);
200 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
201 btn->setEnabled(
false);
202 btn->setVisible(
false);
205 bar->setVisible(
true);
206 bar->setEnabled(
true);
207 auto path = FileUtils::getInstance()->getWritablePath() +
"CppTests/DownloaderTest/" +
sNameList[3];
211 bottomRightView->setAnchorPoint(Vec2(0, 1));
213 this->addChild(bottomRightView);
216 downloader->onTaskProgress = [
this](
const network::DownloadTask& task,
217 int64_t bytesReceived,
218 int64_t totalBytesReceived,
219 int64_t totalBytesExpected)
221 Node* view = this->getChildByName(task.identifier);
223 float percent = float(totalBytesReceived * 100) / totalBytesExpected;
224 bar->setPercent(percent);
226 sprintf(buf,
"%.1f%%[total %d KB]", percent,
int(totalBytesExpected/1024));
227 auto status = (Label*)view->getChildByTag(
TAG_STATUS);
228 status->setString(buf);
232 downloader->onDataTaskSuccess = [
this](
const cocos2d::network::DownloadTask& task,
233 std::vector<unsigned char>& data)
236 Texture2D* texture =
nullptr;
240 if (
false == img.initWithImageData(data.data(), data.size()))
245 texture =
new Texture2D();
246 if (
false == texture->initWithImage(&img))
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);
259 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
260 btn->setEnabled(
true);
261 btn->setVisible(
true);
263 bar->setVisible(
false);
265 CC_SAFE_RELEASE(texture);
268 downloader->onFileTaskSuccess = [
this](
const cocos2d::network::DownloadTask& task)
270 Texture2D* texture =
nullptr;
273 auto view = this->getChildByName(task.identifier);
274 if (std::string::npos == task.storagePath.find(
".png"))
278 sprintf(buf,
"Download [%s] success.", task.identifier.c_str());
279 auto status = (Label*)view->getChildByTag(
TAG_STATUS);
280 status->setString(buf);
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);
292 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
293 btn->setEnabled(
true);
294 btn->setVisible(
true);
296 bar->setVisible(
false);
298 CC_SAFE_RELEASE(texture);
302 downloader->onTaskError = [
this](
const cocos2d::network::DownloadTask& task,
304 int errorCodeInternal,
305 const std::string& errorStr)
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()
313 auto view = this->getChildByName(task.identifier);
314 auto status = (Label*)view->getChildByTag(
TAG_STATUS);
315 status->setString(errorStr.length() ? errorStr :
"Download failed.");
317 auto btn = (ui::Button*)view->getChildByTag(
TAG_BUTTON);
318 btn->setEnabled(
true);
319 btn->setVisible(
true);
328 virtual std::string
title()
const override {
return "Downloader Multi Task"; }
329 virtual std::string
subtitle()
const override {
return "see the console output"; }
336 network::DownloaderHints hints = {32, 60,
".going"};
337 downloader.reset(
new network::Downloader(hints));
346 for(
int i=0; i< 64;i++){
348 sprintf(path,
"%sCppTests/DownloaderTest/%s", FileUtils::getInstance()->getWritablePath().c_str(), name);
349 log(
"downloader task create: %s", name);
350 this->downloader->createDownloadFileTask(
sURLList[0], path, name);
353 downloader->onFileTaskSuccess = ([] (
const network::DownloadTask& task) {
354 log(
"downloader task success: %s", task.identifier.c_str());
357 downloader->onTaskError = ([] (
const network::DownloadTask& task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr) {
358 log(
"downloader task failed : %s, identifier(%s) error code(%d), internal error code(%d) desc(%s)"
359 , task.requestURL.c_str()
360 , task.identifier.c_str()
368DownloaderTests::DownloaderTests()
#define ADD_TEST_CASE(__className__)
static const char * sNameList[sListSize]
static const int sListSize
static const char * sURLList[]
cocos2d::MenuItemImage * _restartTestItem
virtual void onEnter() override
static cocos2d::Vec2 center()
virtual void onEnter() override
std::unique_ptr< network::Downloader > downloader
virtual std::string title() const override
CREATE_FUNC(DownloaderMultiTask)
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string title() const override
std::unique_ptr< network::Downloader > downloader
CREATE_FUNC(DownloaderTest)
Node * createDownloadView(const char *name, const cocos2d::ui::Button::ccWidgetClickCallback &callback)
static const char s_s9s_ui_plist[]