2015-08-13 15:14:10 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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 "DownloaderTest.h"
|
|
|
|
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
|
|
|
#include "network/CCDownloader.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
DownloaderTests::DownloaderTests()
|
|
|
|
{
|
2015-09-06 15:57:51 +08:00
|
|
|
ADD_TEST_CASE(DownloaderAsyncTest);
|
2015-09-07 10:08:28 +08:00
|
|
|
ADD_TEST_CASE(DownloaderSyncTest);
|
2015-08-13 15:14:10 +08:00
|
|
|
ADD_TEST_CASE(DownloaderBatchSyncTest);
|
|
|
|
ADD_TEST_CASE(DownloaderBatchAsyncTest);
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
DownloaderBaseTest::DownloaderBaseTest()
|
|
|
|
{
|
|
|
|
_downloader = std::shared_ptr<network::Downloader>(new network::Downloader);
|
2015-08-28 16:40:11 +08:00
|
|
|
_downloader->onFileTaskProgress =
|
|
|
|
std::bind(&DownloaderBaseTest::progressCallback,
|
|
|
|
this,
|
|
|
|
std::placeholders::_1,
|
|
|
|
std::placeholders::_2,
|
|
|
|
std::placeholders::_3,
|
|
|
|
std::placeholders::_4);
|
|
|
|
|
|
|
|
// _downloader->setErrorCallback(std::bind(&DownloaderBaseTest::errorCallback, this, std::placeholders::_1));
|
|
|
|
// _downloader->setProgressCallback(std::bind(&DownloaderBaseTest::progressCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
|
|
|
_downloader->onFileTaskSuccess = std::bind(&DownloaderBaseTest::successCallback, this, std::placeholders::_1);
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderBaseTest::title() const
|
|
|
|
{
|
|
|
|
return "Downloader Test";
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
//void DownloaderBaseTest::errorCallback(const cocos2d::network::Downloader::Error& error)
|
|
|
|
//{
|
|
|
|
// cocos2d::log("error downloading: %s - %s", error.url.c_str(), error.message.c_str());
|
|
|
|
//}
|
2015-08-13 15:14:10 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
void DownloaderBaseTest::progressCallback(const network::DownloadTask& task,
|
|
|
|
int64_t bytesWritten,
|
|
|
|
int64_t totalBytesWritten,
|
|
|
|
int64_t totalBytesExpectedToWrite)
|
2015-08-13 15:14:10 +08:00
|
|
|
{
|
2015-09-06 15:57:51 +08:00
|
|
|
cocos2d::log("test(%s) progress: dl(%lld) td(%lld) total(%lld)", task.identifier.c_str(), bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
void DownloaderBaseTest::successCallback(const network::DownloadTask& task)
|
2015-08-13 15:14:10 +08:00
|
|
|
{
|
2015-09-10 15:56:06 +08:00
|
|
|
cocos2d::log("download successed: %s", task.storagePath.c_str());
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// DownloaderSyncTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
void DownloaderSyncTest::onEnter()
|
|
|
|
{
|
|
|
|
DownloaderBaseTest::onEnter();
|
|
|
|
|
|
|
|
auto menuItem = MenuItemFont::create("start download", [=](Ref* sender){
|
|
|
|
|
|
|
|
if (_downloader)
|
|
|
|
{
|
|
|
|
|
2015-09-06 15:57:51 +08:00
|
|
|
// std::string path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/cocos2d_logo_sync.jpg";
|
|
|
|
// std::string remote = "http://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png";
|
|
|
|
std::string path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/sdkbox-iap_v1.2.3.3.tar.gz";
|
|
|
|
std::string remote = "http://download.sdkbox.com/installer/v1/sdkbox-iap_v1.2.3.3.tar.gz";
|
2015-08-13 15:14:10 +08:00
|
|
|
cocos2d::log("Downloading '%s' into '%s'", remote.c_str(), path.c_str());
|
|
|
|
|
2015-09-06 15:57:51 +08:00
|
|
|
_downloader->createDownloadFileTask(remote, path, "Download Resume Tesk(9M)");
|
2015-08-13 15:14:10 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
addChild(menu);
|
|
|
|
menu->setNormalizedPosition(Vec2(0.5, 0.5));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderSyncTest::title() const
|
|
|
|
{
|
|
|
|
return "Downloader";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderSyncTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Sync test";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// DownloaderAsyncTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
void DownloaderAsyncTest::onEnter()
|
|
|
|
{
|
|
|
|
DownloaderBaseTest::onEnter();
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
_downloader->onDataTaskProgress = [this](const network::DownloadTask& task,
|
2015-09-02 18:26:42 +08:00
|
|
|
int64_t bytesReceived,
|
2015-08-28 16:40:11 +08:00
|
|
|
int64_t totalBytesReceived,
|
2015-09-02 18:26:42 +08:00
|
|
|
int64_t totalBytesExpected,
|
|
|
|
std::function<int64_t(void *buffer, int64_t len)>& transferDataToBuffer){
|
|
|
|
CCLOG("DownloaderAsyncTest onDataTaskProgress(bytesReceived: %lld, totalBytesReceived:%lld, totalBytesExpected:%lld)"
|
|
|
|
, bytesReceived
|
2015-08-28 16:40:11 +08:00
|
|
|
, totalBytesReceived
|
|
|
|
, totalBytesExpected);
|
2015-09-10 18:03:15 +08:00
|
|
|
if (transferDataToBuffer)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> buf;
|
|
|
|
buf.reserve(bytesReceived);
|
|
|
|
int64_t transfered = transferDataToBuffer(buf.data(), buf.capacity());
|
|
|
|
assert(transfered == bytesReceived);
|
|
|
|
}
|
2015-08-28 16:40:11 +08:00
|
|
|
};
|
2015-09-02 18:26:42 +08:00
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
_downloader->onDataTaskSuccess = [this](const network::DownloadTask& task,
|
2015-09-02 18:26:42 +08:00
|
|
|
std::vector<unsigned char>& data){
|
2015-09-10 18:03:15 +08:00
|
|
|
CCLOG("DownloaderAsyncTest onDataTaskSuccess(dataLen:%ld)", data.size());
|
2015-08-28 16:40:11 +08:00
|
|
|
};
|
2015-09-02 18:26:42 +08:00
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
auto menuItem = MenuItemFont::create("start download", [=](Ref* sender){
|
|
|
|
|
|
|
|
if (_downloader)
|
|
|
|
{
|
2015-08-28 16:40:11 +08:00
|
|
|
std::string path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/cocos2d_logo_sync.jpg";
|
2015-08-13 15:14:10 +08:00
|
|
|
std::string remote = "http://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png";
|
2015-08-28 16:40:11 +08:00
|
|
|
|
|
|
|
_downloader->createDownloadDataTask(remote, "download_async_test");
|
2015-08-13 15:14:10 +08:00
|
|
|
|
|
|
|
cocos2d::log("Downloading '%s' into '%s'", remote.c_str(), path.c_str());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
addChild(menu);
|
|
|
|
menu->setNormalizedPosition(Vec2(0.5, 0.5));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderAsyncTest::title() const
|
|
|
|
{
|
|
|
|
return "Downloader";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderAsyncTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Async test";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// DownloaderBatchSyncTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
void DownloaderBatchSyncTest::onEnter()
|
|
|
|
{
|
|
|
|
DownloaderBaseTest::onEnter();
|
|
|
|
|
|
|
|
auto menuItem = MenuItemFont::create("start download", [=](Ref* sender){
|
|
|
|
|
|
|
|
if (_downloader)
|
|
|
|
{
|
|
|
|
std::vector<std::string> images = {
|
|
|
|
"http://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png",
|
|
|
|
"http://www.cocos2d-x.org/docs/manual/framework/native/wiki/logo-resources-of-cocos2d-x/res/2dx_icon_512_rounded.png",
|
|
|
|
"http://www.cocos2d-x.org/attachments/1503/Cocos2CoordinateRelease.png"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> names = {
|
|
|
|
"cocos2dx_landscape.png",
|
|
|
|
"2dx_icon_512_rounded.png",
|
|
|
|
"Cocos2CoordinateRelease.png"
|
|
|
|
};
|
|
|
|
|
|
|
|
network::DownloadUnits units;
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
for(const auto& image: images)
|
|
|
|
{
|
|
|
|
network::DownloadUnit unit;
|
|
|
|
unit.storagePath = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/Sync/" + names[i];
|
|
|
|
unit.srcUrl = image;
|
|
|
|
unit.customId = image;
|
|
|
|
i++;
|
|
|
|
units[image] = unit;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
// _downloader->batchDownloadSync(units, "sync_download");
|
2015-08-13 15:14:10 +08:00
|
|
|
cocos2d::log("Downloading...");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
addChild(menu);
|
|
|
|
menu->setNormalizedPosition(Vec2(0.5, 0.5));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderBatchSyncTest::title() const
|
|
|
|
{
|
|
|
|
return "Downloader";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderBatchSyncTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Batch Sync test";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// DownloaderBatchAsyncTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
void DownloaderBatchAsyncTest::onEnter()
|
|
|
|
{
|
|
|
|
DownloaderBaseTest::onEnter();
|
|
|
|
|
|
|
|
auto menuItem = MenuItemFont::create("start download", [=](Ref* sender){
|
|
|
|
|
|
|
|
if (_downloader)
|
|
|
|
{
|
|
|
|
std::vector<std::string> images = {
|
|
|
|
"http://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png",
|
|
|
|
"http://www.cocos2d-x.org/docs/manual/framework/native/wiki/logo-resources-of-cocos2d-x/res/2dx_icon_512_rounded.png",
|
|
|
|
"http://www.cocos2d-x.org/attachments/1503/Cocos2CoordinateRelease.png"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> names = {
|
|
|
|
"cocos2dx_landscape.png",
|
|
|
|
"2dx_icon_512_rounded.png",
|
|
|
|
"Cocos2CoordinateRelease.png"
|
|
|
|
};
|
|
|
|
|
|
|
|
network::DownloadUnits units;
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
for(const auto& image: images)
|
|
|
|
{
|
|
|
|
network::DownloadUnit unit;
|
|
|
|
unit.storagePath = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/Async/" + names[i];
|
|
|
|
unit.srcUrl = image;
|
|
|
|
unit.customId = image;
|
|
|
|
i++;
|
|
|
|
units[image] = unit;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:40:11 +08:00
|
|
|
// _downloader->batchDownloadAsync(units, "sync_download");
|
2015-08-13 15:14:10 +08:00
|
|
|
cocos2d::log("Downloading...");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
addChild(menu);
|
|
|
|
menu->setNormalizedPosition(Vec2(0.5, 0.5));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderBatchAsyncTest::title() const
|
|
|
|
{
|
|
|
|
return "Downloader";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DownloaderBatchAsyncTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Batch Async test";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|