2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2020-02-04 22:28:54 +08:00
|
|
|
Copyright (c) 2020 c4games.com.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
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:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "UserDefaultTest.h"
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
2015-05-22 02:22:37 +08:00
|
|
|
#include <vector>
|
2015-05-22 03:20:43 +08:00
|
|
|
#include <sstream>
|
2015-05-22 03:39:00 +08:00
|
|
|
#include <iomanip>
|
2015-05-22 02:22:37 +08:00
|
|
|
|
|
|
|
using namespace std;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
UserDefaultTests::UserDefaultTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UserDefaultTest);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
UserDefaultTest::UserDefaultTest()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2015-07-06 17:01:17 +08:00
|
|
|
auto label = Label::createWithTTF("CCUserDefault test Log data see console", "fonts/arial.ttf", 22);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 0);
|
2021-12-28 16:06:23 +08:00
|
|
|
label->setPosition(Vec2(s.width / 2, s.height - 50));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
this->_label = Label::createWithTTF("result", "fonts/arial.ttf", 12);
|
|
|
|
addChild(this->_label, 0);
|
|
|
|
|
|
|
|
label->setPosition(Vec2(s.width / 2, s.height - 50));
|
|
|
|
this->_label->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
doTest();
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// template<typename T>
|
|
|
|
// void logData(const char* key)
|
2020-02-04 22:28:54 +08:00
|
|
|
//{
|
2021-12-28 16:06:23 +08:00
|
|
|
// Data data = UserDefault::getInstance()->getDataForKey(key);
|
|
|
|
// T* buffer = (T*) data.getBytes();
|
|
|
|
// ssize_t length = data.getSize() / sizeof(T);
|
2020-02-04 22:28:54 +08:00
|
|
|
//
|
2021-12-28 16:06:23 +08:00
|
|
|
// std::ostringstream ss;
|
|
|
|
// ss << setprecision(2) << std::fixed;
|
|
|
|
// for (int i = 0; i < length; i++)
|
|
|
|
// {
|
|
|
|
// ss << buffer[i] << " ";
|
|
|
|
// }
|
2020-02-04 22:28:54 +08:00
|
|
|
//
|
2021-12-28 16:06:23 +08:00
|
|
|
// CCLOG("%s is %s", key, ss.str().c_str());
|
|
|
|
// }
|
2020-02-04 22:28:54 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// template<typename T>
|
|
|
|
// void setData(const char* key)
|
2020-02-04 22:28:54 +08:00
|
|
|
//{
|
2021-12-28 16:06:23 +08:00
|
|
|
// Data data;
|
|
|
|
// vector<T> v;
|
2020-02-04 22:28:54 +08:00
|
|
|
//
|
2021-12-28 16:06:23 +08:00
|
|
|
// for (int i = 0; i <= 5; i++)
|
|
|
|
// {
|
|
|
|
// v.push_back(static_cast<T>(i));
|
|
|
|
// }
|
|
|
|
// data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
|
|
|
|
// UserDefault::getInstance()->setDataForKey(key, data);
|
|
|
|
// }
|
2020-02-04 22:28:54 +08:00
|
|
|
//
|
2021-12-28 16:06:23 +08:00
|
|
|
// template<typename T>
|
|
|
|
// void setData2(const char* key)
|
2020-02-04 22:28:54 +08:00
|
|
|
//{
|
2021-12-28 16:06:23 +08:00
|
|
|
// Data data;
|
|
|
|
// vector<T> v;
|
2020-02-04 22:28:54 +08:00
|
|
|
//
|
2021-12-28 16:06:23 +08:00
|
|
|
// for (int i = 5; i >= 0; i--)
|
|
|
|
// {
|
|
|
|
// v.push_back(static_cast<T>(i));
|
|
|
|
// }
|
|
|
|
// data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
|
|
|
|
// UserDefault::getInstance()->setDataForKey(key, data);
|
|
|
|
// }
|
2015-05-22 02:22:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void UserDefaultTest::doTest()
|
|
|
|
{
|
2021-12-28 17:20:17 +08:00
|
|
|
std::string strVal{this->_label->getString()};
|
|
|
|
this->_label->setString(strVal.append("\n"
|
|
|
|
"********************** init value ***********************"));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// set default value
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault::getInstance()->setStringForKey("string", "value1");
|
|
|
|
UserDefault::getInstance()->setIntegerForKey("integer", 10);
|
|
|
|
UserDefault::getInstance()->setFloatForKey("float", 2.3f);
|
|
|
|
UserDefault::getInstance()->setDoubleForKey("double", 2.4);
|
|
|
|
UserDefault::getInstance()->setBoolForKey("bool", true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-05-22 02:22:37 +08:00
|
|
|
// test saving of Data buffers
|
2021-12-28 16:06:23 +08:00
|
|
|
// setData<int>("int_data");
|
|
|
|
// setData<float>("float_data");
|
|
|
|
// setData<double>("double_data");
|
2015-05-22 02:22:37 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
printValue();
|
2015-05-22 02:22:37 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// logData<int>("int_data");
|
|
|
|
// logData<float>("float_data");
|
|
|
|
// logData<double>("double_data");
|
2015-05-22 02:22:37 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// CCUserDefault::getInstance()->flush();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 17:20:17 +08:00
|
|
|
strVal = this->_label->getString();
|
|
|
|
this->_label->setString(strVal.append("\n"
|
|
|
|
"********************** after change value ***********************"));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// change the value
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault::getInstance()->setStringForKey("string", "value2");
|
|
|
|
UserDefault::getInstance()->setIntegerForKey("integer", 11);
|
|
|
|
UserDefault::getInstance()->setFloatForKey("float", 2.5f);
|
|
|
|
UserDefault::getInstance()->setDoubleForKey("double", 2.6);
|
|
|
|
UserDefault::getInstance()->setBoolForKey("bool", false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// setData2<int>("int_data");
|
|
|
|
// setData2<float>("float_data");
|
|
|
|
// setData2<double>("double_data");
|
2015-05-22 02:22:37 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault::getInstance()->flush();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// print value
|
2015-07-06 17:01:17 +08:00
|
|
|
printValue();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// logData<int>("int_data");
|
|
|
|
// logData<float>("float_data");
|
|
|
|
// logData<double>("double_data");
|
2015-07-06 17:01:17 +08:00
|
|
|
|
2021-12-28 17:20:17 +08:00
|
|
|
strVal = this->_label->getString();
|
|
|
|
this->_label->setString(strVal.append("\n"
|
|
|
|
"********************** after delete value ***********************"));
|
2015-07-06 17:01:17 +08:00
|
|
|
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("string");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("integer");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("float");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("double");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("bool");
|
|
|
|
|
|
|
|
// print value
|
|
|
|
printValue();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
void UserDefaultTest::printValue()
|
|
|
|
{
|
|
|
|
char strTemp[256] = "";
|
|
|
|
// print value
|
2021-12-28 17:20:17 +08:00
|
|
|
std::string_view ret = UserDefault::getInstance()->getStringForKey("string");
|
|
|
|
sprintf(strTemp, "string is %s", ret.data());
|
|
|
|
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
double d = UserDefault::getInstance()->getDoubleForKey("double");
|
|
|
|
sprintf(strTemp, "double is %f", d);
|
2021-12-28 17:20:17 +08:00
|
|
|
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
int i = UserDefault::getInstance()->getIntegerForKey("integer");
|
|
|
|
sprintf(strTemp, "integer is %d", i);
|
2021-12-28 17:20:17 +08:00
|
|
|
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
float f = UserDefault::getInstance()->getFloatForKey("float");
|
|
|
|
sprintf(strTemp, "float is %f", f);
|
2021-12-28 17:20:17 +08:00
|
|
|
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
bool b = UserDefault::getInstance()->getBoolForKey("bool");
|
2012-04-19 14:35:52 +08:00
|
|
|
if (b)
|
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
sprintf(strTemp, "bool is true");
|
2021-12-28 17:20:17 +08:00
|
|
|
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
sprintf(strTemp, "bool is false");
|
2021-12-28 17:20:17 +08:00
|
|
|
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UserDefaultTest::~UserDefaultTest() {}
|