axmol/tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp

394 lines
11 KiB
C++
Raw Normal View History

2013-01-28 15:17:20 +08:00
#include "FileUtilsTest.h"
2013-11-23 04:15:41 +08:00
static std::function<Layer*()> createFunctions[] = {
CL(TestResolutionDirectories),
CL(TestSearchPath),
CL(TestFilenameLookup),
CL(TestIsFileExist),
CL(TextWritePlist),
2013-01-28 15:17:20 +08:00
};
static int sceneIdx=-1;
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
static Layer* nextAction()
2013-01-28 15:17:20 +08:00
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
auto layer = (createFunctions[sceneIdx])();
return layer;
2013-01-28 15:17:20 +08:00
}
static Layer* backAction()
2013-01-28 15:17:20 +08:00
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
auto layer = (createFunctions[sceneIdx])();
return layer;
2013-01-28 15:17:20 +08:00
}
static Layer* restartAction()
2013-01-28 15:17:20 +08:00
{
auto layer = (createFunctions[sceneIdx])();
return layer;
2013-01-28 15:17:20 +08:00
}
void FileUtilsTestScene::runThisTest()
{
auto layer = nextAction();
addChild(layer);
2013-01-28 15:17:20 +08:00
Director::getInstance()->replaceScene(this);
2013-01-28 15:17:20 +08:00
}
// #pragma mark - FileUtilsDemo
void FileUtilsDemo::onEnter()
{
BaseTest::onEnter();
2013-01-28 15:17:20 +08:00
}
void FileUtilsDemo::backCallback(Ref* sender)
2013-01-28 15:17:20 +08:00
{
auto scene = new FileUtilsTestScene();
auto layer = backAction();
2013-01-28 15:17:20 +08:00
scene->addChild(layer);
Director::getInstance()->replaceScene(scene);
scene->release();
2013-01-28 15:17:20 +08:00
}
void FileUtilsDemo::nextCallback(Ref* sender)
2013-01-28 15:17:20 +08:00
{
auto scene = new FileUtilsTestScene();
auto layer = nextAction();
2013-01-28 15:17:20 +08:00
scene->addChild(layer);
Director::getInstance()->replaceScene(scene);
scene->release();
2013-01-28 15:17:20 +08:00
}
void FileUtilsDemo::restartCallback(Ref* sender)
2013-01-28 15:17:20 +08:00
{
auto scene = new FileUtilsTestScene();
auto layer = restartAction();
2013-01-28 15:17:20 +08:00
scene->addChild(layer);
Director::getInstance()->replaceScene(scene);
scene->release();
2013-01-28 15:17:20 +08:00
}
std::string FileUtilsDemo::title() const
2013-01-28 15:17:20 +08:00
{
return "No title";
}
std::string FileUtilsDemo::subtitle() const
2013-01-28 15:17:20 +08:00
{
return "";
}
//#pragma mark - TestResolutionDirectories
void TestResolutionDirectories::onEnter()
{
FileUtilsDemo::onEnter();
auto sharedFileUtils = FileUtils::getInstance();
2013-01-28 15:17:20 +08:00
std::string ret;
2013-01-28 15:17:20 +08:00
sharedFileUtils->purgeCachedEntries();
_defaultSearchPathArray = sharedFileUtils->getSearchPaths();
std::vector<std::string> searchPaths = _defaultSearchPathArray;
2013-01-28 15:17:20 +08:00
searchPaths.insert(searchPaths.begin(), "Misc");
sharedFileUtils->setSearchPaths(searchPaths);
2013-01-28 15:17:20 +08:00
_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray;
2013-01-28 15:17:20 +08:00
resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd");
resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad");
resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd");
resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide");
resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd");
resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone");
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
for( int i=1; i<7; i++) {
auto filename = String::createWithFormat("test%d.txt", i);
2013-01-28 15:17:20 +08:00
ret = sharedFileUtils->fullPathForFilename(filename->getCString());
log("%s -> %s", filename->getCString(), ret.c_str());
2013-01-28 15:17:20 +08:00
}
}
void TestResolutionDirectories::onExit()
{
auto sharedFileUtils = FileUtils::getInstance();
2013-01-28 15:17:20 +08:00
// reset search path
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
sharedFileUtils->setSearchResolutionsOrder(_defaultResolutionsOrderArray);
2013-01-28 15:17:20 +08:00
FileUtilsDemo::onExit();
}
std::string TestResolutionDirectories::title() const
2013-01-28 15:17:20 +08:00
{
return "FileUtils: resolutions in directories";
}
std::string TestResolutionDirectories::subtitle() const
2013-01-28 15:17:20 +08:00
{
return "See the console";
}
//#pragma mark - TestSearchPath
void TestSearchPath::onEnter()
{
FileUtilsDemo::onEnter();
auto sharedFileUtils = FileUtils::getInstance();
2013-01-28 15:17:20 +08:00
std::string ret;
2013-01-28 15:17:20 +08:00
sharedFileUtils->purgeCachedEntries();
_defaultSearchPathArray = sharedFileUtils->getSearchPaths();
std::vector<std::string> searchPaths = _defaultSearchPathArray;
std::string writablePath = sharedFileUtils->getWritablePath();
std::string fileName = writablePath+"external.txt";
char szBuf[100] = "Hello Cocos2d-x!";
FILE* fp = fopen(fileName.c_str(), "wb");
if (fp)
{
2013-06-01 08:06:11 +08:00
size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp);
2013-07-28 10:22:12 +08:00
CCASSERT(ret != 0, "fwrite function returned zero value");
fclose(fp);
2013-07-28 10:22:12 +08:00
if (ret != 0)
log("Writing file to writable path succeed.");
}
searchPaths.insert(searchPaths.begin(), writablePath);
searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1");
searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2");
sharedFileUtils->setSearchPaths(searchPaths);
_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
std::vector<std::string> resolutionsOrder = _defaultResolutionsOrderArray;
2013-01-28 15:17:20 +08:00
resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad");
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
for( int i=1; i<3; i++) {
auto filename = String::createWithFormat("file%d.txt", i);
2013-01-28 15:17:20 +08:00
ret = sharedFileUtils->fullPathForFilename(filename->getCString());
log("%s -> %s", filename->getCString(), ret.c_str());
2013-01-28 15:17:20 +08:00
}
// Gets external.txt from writable path
std::string fullPath = sharedFileUtils->fullPathForFilename("external.txt");
log("external file path = %s", fullPath.c_str());
if (fullPath.length() > 0)
{
fp = fopen(fullPath.c_str(), "rb");
if (fp)
{
char szReadBuf[100] = {0};
int read = fread(szReadBuf, 1, strlen(szBuf), fp);
if (read > 0)
log("The content of file from writable path: %s", szReadBuf);
fclose(fp);
}
}
2013-01-28 15:17:20 +08:00
}
void TestSearchPath::onExit()
{
FileUtils *sharedFileUtils = FileUtils::getInstance();
2013-01-28 15:17:20 +08:00
// reset search path
sharedFileUtils->setSearchPaths(_defaultSearchPathArray);
sharedFileUtils->setSearchResolutionsOrder(_defaultResolutionsOrderArray);
2013-01-28 15:17:20 +08:00
FileUtilsDemo::onExit();
}
std::string TestSearchPath::title() const
2013-01-28 15:17:20 +08:00
{
return "FileUtils: search path";
}
std::string TestSearchPath::subtitle() const
2013-01-28 15:17:20 +08:00
{
return "See the console";
}
//#pragma mark - TestFilenameLookup
void TestFilenameLookup::onEnter()
{
FileUtilsDemo::onEnter();
auto sharedFileUtils = FileUtils::getInstance();
2013-01-28 15:17:20 +08:00
ValueMap dict;
dict["grossini.bmp"] = Value("Images/grossini.png");
dict["grossini.xcf"] = Value("Images/grossini.png");
2013-01-28 15:17:20 +08:00
sharedFileUtils->setFilenameLookupDictionary(dict);
// Instead of loading carlitos.xcf, it will load grossini.png
auto sprite = Sprite::create("grossini.xcf");
2013-01-28 15:17:20 +08:00
this->addChild(sprite);
auto s = Director::getInstance()->getWinSize();
2013-07-12 14:11:55 +08:00
sprite->setPosition(Point(s.width/2, s.height/2));
2013-01-28 15:17:20 +08:00
}
void TestFilenameLookup::onExit()
{
FileUtils *sharedFileUtils = FileUtils::getInstance();
2013-01-28 15:17:20 +08:00
// reset filename lookup
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
2013-01-28 15:17:20 +08:00
FileUtilsDemo::onExit();
}
std::string TestFilenameLookup::title() const
2013-01-28 15:17:20 +08:00
{
return "FileUtils: filename lookup";
}
//#pragma mark - TestIsFileExist
void TestIsFileExist::onEnter()
{
FileUtilsDemo::onEnter();
auto s = Director::getInstance()->getWinSize();
auto sharedFileUtils = FileUtils::getInstance();
LabelTTF* pTTF = NULL;
bool isExist = false;
isExist = sharedFileUtils->isFileExist("Images/grossini.png");
pTTF = LabelTTF::create(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20);
2013-07-12 14:11:55 +08:00
pTTF->setPosition(Point(s.width/2, s.height/3));
this->addChild(pTTF);
isExist = sharedFileUtils->isFileExist("Images/grossini.xcf");
pTTF = LabelTTF::create(isExist ? "Images/grossini.xcf exists" : "Images/grossini.xcf doesn't exist", "", 20);
2013-07-12 14:11:55 +08:00
pTTF->setPosition(Point(s.width/2, s.height/3*2));
this->addChild(pTTF);
}
void TestIsFileExist::onExit()
{
FileUtils *sharedFileUtils = FileUtils::getInstance();
// reset filename lookup
sharedFileUtils->setFilenameLookupDictionary(ValueMap());
FileUtilsDemo::onExit();
}
std::string TestIsFileExist::title() const
{
return "FileUtils: check whether the file exists";
}
std::string TestIsFileExist::subtitle() const
{
return "";
}
2013-05-07 14:36:01 +08:00
//#pragma mark - TestWritePlist
void TextWritePlist::onEnter()
{
FileUtilsDemo::onEnter();
auto root = Dictionary::create();
auto string = String::create("string element value");
root->setObject(string, "string element key");
auto array = Array::create();
auto dictInArray = Dictionary::create();
dictInArray->setObject(String::create("string in dictInArray value 0"), "string in dictInArray key 0");
dictInArray->setObject(String::create("string in dictInArray value 1"), "string in dictInArray key 1");
array->addObject(dictInArray);
array->addObject(String::create("string in array"));
auto arrayInArray = Array::create();
arrayInArray->addObject(String::create("string 0 in arrayInArray"));
arrayInArray->addObject(String::create("string 1 in arrayInArray"));
array->addObject(arrayInArray);
root->setObject(array, "array");
auto dictInDict = Dictionary::create();
dictInDict->setObject(String::create("string in dictInDict value"), "string in dictInDict key");
2014-03-18 14:59:59 +08:00
//add boolean to the plist
auto booleanObject = Bool::create(true);
dictInDict->setObject(booleanObject, "bool");
2014-03-18 14:59:59 +08:00
//add interger to the plist
auto intObject = Integer::create(1024);
dictInDict->setObject(intObject, "integer");
//add float to the plist
auto floatObject = Float::create(1024.1024f);
dictInDict->setObject(floatObject, "float");
//add double to the plist
auto doubleObject = Double::create(1024.123);
dictInDict->setObject(doubleObject, "double");
root->setObject(dictInDict, "dictInDict, Hello World");
// end with /
std::string writablePath = FileUtils::getInstance()->getWritablePath();
std::string fullPath = writablePath + "text.plist";
if(root->writeToFile(fullPath.c_str()))
log("see the plist file at %s", fullPath.c_str());
else
log("write plist file failed");
auto label = LabelTTF::create(fullPath.c_str(), "Thonburi", 6);
this->addChild(label);
auto winSize = Director::getInstance()->getWinSize();
2013-07-12 14:11:55 +08:00
label->setPosition(Point(winSize.width/2, winSize.height/3));
// auto loadDict = Dictionary::createWithContentsOfFile("/Users/guanghui/Library/Application Support/iPhone Simulator/7.1-64/Applications/92E7DFA5-5B92-44FE-AF61-798F5A50B091/Documents/text.plist");
// auto loadDictInDict = (Dictionary*)loadDict->objectForKey("dictInDict, Hello World");
// auto boolValue = (Bool*)loadDictInDict->objectForKey("bool");
// if (boolValue->getValue()) {
// CCLOG("true");
// }
}
void TextWritePlist::onExit()
{
FileUtilsDemo::onExit();
}
std::string TextWritePlist::title() const
{
return "FileUtils: Dictionary to plist";
}
std::string TextWritePlist::subtitle() const
{
std::string writablePath = FileUtils::getInstance()->getWritablePath().c_str();
return ("See plist file at your writablePath");
}