Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tests/lua-tests/src/DownloaderTest/DownloaderTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require("json")


local imgURI = "http://forum.cocos.com/images/logo.png"
local notExistURI = "http://www.cocos2d-x.org/attachments/1503/no_exist.txt"

local downloader = cc.Downloader.new()
local writablePath = cc.FileUtils:getInstance():getWritablePath()
local function DownloaderLayer()
local layer = cc.Layer:create()
local winSize = cc.Director:getInstance():getWinSize()
local margin = 40
local space = 35

local task1 = nil
local task2 = nil

local function init()

local label = cc.Label:createWithTTF("Downloader Test", s_arialPath, 28)
label:setAnchorPoint(cc.p(0.5, 0.5))
label:setPosition(cc.p(winSize.width / 2, winSize.height - margin))
layer:addChild(label, 0)

--Response Code Label
local labelStatusCode = cc.Label:createWithTTF("status", s_markerFeltFontPath, 20)
labelStatusCode:setAnchorPoint(cc.p(0.5, 0.5))
labelStatusCode:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 6 * space))
layer:addChild(labelStatusCode)

local menuRequest = cc.Menu:create()
menuRequest:setPosition(cc.p(0,0))
layer:addChild(menuRequest)

--success
local function onDownloadFileClicked()
downloader:createDownloadFileTask(imgURI, writablePath .. "/2dx_icon_512_rounded.png", "task_img");
labelStatusCode:setString("waiting...")
end

local labelGet = cc.Label:createWithTTF("Test Download Image", s_arialPath, 22)
labelGet:setAnchorPoint(cc.p(0.5, 0.5))
local itemGet = cc.MenuItemLabel:create(labelGet)
itemGet:registerScriptTapHandler(onDownloadFileClicked)
itemGet:setPosition(cc.p(winSize.width / 2, winSize.height - margin - space))
menuRequest:addChild(itemGet)

--failure
local function onDownloadTxtClicked()
downloader:createDownloadFileTask(notExistURI, writablePath .. "/no_exists.txt", "task_no_exist");
labelStatusCode:setString("waiting...")
end

local labelPost = cc.Label:createWithTTF("Test Download File Not Exists", s_arialPath, 22)
labelPost:setAnchorPoint(cc.p(0.5, 0.5))
local itemPost = cc.MenuItemLabel:create(labelPost)
itemPost:registerScriptTapHandler(onDownloadTxtClicked)
itemPost:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 2 * space))
menuRequest:addChild(itemPost)

local function onTaskSuccess(task)
if task.identifier == "task_img" then
labelStatusCode:setString("image download success")
elseif task.identifier == "task_no_exist" then
labelStatusCode:setString("txt download success")
end
end
local function onProgress(task, bytesReceived, totalBytesReceived, totalBytesExpected)
if task.identifier == "task_img" then
labelStatusCode:setString("image download progress " .. tostring(math.floor(totalBytesReceived/totalBytesExpected*100)))
elseif task.identifier == "task_no_exist" then
labelStatusCode:setString("txt download progress " .. tostring(math.floor(totalBytesReceived/totalBytesExpected*100)))
end
end
local function onTaskError(task, errorCode, errorCodeInternal, errorStr)
if task.identifier == "task_img" then
labelStatusCode:setString("image download error: "..errorStr)
elseif task.identifier == "task_no_exist" then
labelStatusCode:setString("txt download error: "..errorStr)
end
end
downloader:setOnFileTaskSuccess(onTaskSuccess)
downloader:setOnTaskProgress(onProgress)
downloader:setOnTaskError(onTaskError)

end

local function onNodeEvent(eventName)
if "enter" == eventName then
init()
end
end

layer:registerScriptHandler(onNodeEvent)

return layer
end

function DownloaderTestMain()
local scene = cc.Scene:create()
scene:addChild(DownloaderLayer())
scene:addChild(CreateBackMenuItem())
return scene
end
2 changes: 2 additions & 0 deletions tests/lua-tests/src/mainMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require "ClickAndMoveTest/ClickAndMoveTest"
require "CocosDenshionTest/CocosDenshionTest"
require "ComponentTest/main"
require "CurrentLanguageTest/CurrentLanguageTest"
require "DownloaderTest/DownloaderTest"
require "DrawPrimitivesTest/DrawPrimitivesTest"
require "EffectsTest/EffectsTest"
require "EffectsAdvancedTest/EffectsAdvancedTest"
Expand Down Expand Up @@ -107,6 +108,7 @@ local _allTests = {
{ isSupported = true, name = "CocosDenshionTest" , create_func = CocosDenshionTestMain },
{ isSupported = true, name = "ComponentTest" , create_func = ComponentTestMain },
{ isSupported = false, name = "CurlTest" , create_func= CurlTestMain },
{ isSupported = true, name = "DownloaderTest" , create_func= DownloaderTestMain },
{ isSupported = true, name = "CurrentLanguageTest" , create_func= CurrentLanguageTestMain },
{ isSupported = true, name = "DrawPrimitivesTest" , create_func= DrawPrimitivesTest },
{ isSupported = true, name = "EffectsTest" , create_func = EffectsTest },
Expand Down