2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-29 16:22:07 +08:00
|
|
|
Copyright (c) 2021 Bytedance inc.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-04-28 09:49:55 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
# include "JNITest.h"
|
2016-04-28 09:49:55 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
# include "platform/android/jni/JniHelper.h"
|
2015-11-08 04:57:05 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
# include <string>
|
2015-11-08 04:57:05 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-11-08 04:57:05 +08:00
|
|
|
|
|
|
|
JNITests::JNITests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(JNITest);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNITest::JNITest()
|
|
|
|
{
|
|
|
|
auto nameLabel = Label::createWithTTF("JNI Test", "fonts/arial.ttf", 28);
|
|
|
|
nameLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 50);
|
|
|
|
addChild(nameLabel);
|
|
|
|
|
|
|
|
auto checkLabel = Label::createWithTTF("Please check console output", "fonts/arial.ttf", 22);
|
|
|
|
checkLabel->setPosition(VisibleRect::center());
|
|
|
|
addChild(checkLabel);
|
|
|
|
|
2021-12-29 16:22:07 +08:00
|
|
|
const char* classPath = "org.cocos2dx.cpp_tests.JNITest";
|
2015-11-08 04:57:05 +08:00
|
|
|
|
|
|
|
JniHelper::callStaticVoidMethod(classPath, "voidMethod1");
|
|
|
|
|
|
|
|
JniHelper::callStaticVoidMethod(classPath, "voidMethod2", "JNI is easy");
|
|
|
|
|
|
|
|
JniHelper::callStaticVoidMethod(classPath, "voidMethod3", int(4), float(2.5), "JNI is really easy");
|
|
|
|
|
|
|
|
bool b1 = JniHelper::callStaticBooleanMethod(classPath, "booleanMethod", int(5));
|
|
|
|
CC_ASSERT(b1 == true);
|
|
|
|
bool b2 = JniHelper::callStaticBooleanMethod(classPath, "booleanMethod", int(-3));
|
|
|
|
CC_ASSERT(b2 == false);
|
|
|
|
|
|
|
|
int i = JniHelper::callStaticIntMethod(classPath, "intMethod", int(10), int(10));
|
|
|
|
CC_ASSERT(i == 20);
|
|
|
|
|
|
|
|
float f = JniHelper::callStaticFloatMethod(classPath, "floatMethod", float(2.35), float(7.65));
|
2021-12-28 16:06:23 +08:00
|
|
|
CC_ASSERT(f == 10.0);
|
|
|
|
|
2015-11-08 04:57:05 +08:00
|
|
|
double d = JniHelper::callStaticDoubleMethod(classPath, "doubleMethod", double(2.5), int(4));
|
2021-12-28 16:06:23 +08:00
|
|
|
CC_ASSERT(d == 10.0);
|
2015-11-08 04:57:05 +08:00
|
|
|
|
|
|
|
std::string str = "ABCDEF";
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string s1 = JniHelper::callStaticStringMethod(classPath, "stringMethod", str, true);
|
2015-11-08 04:57:05 +08:00
|
|
|
CC_ASSERT(s1 == "FEDCBA");
|
|
|
|
std::string s2 = JniHelper::callStaticStringMethod(classPath, "stringMethod", str, false);
|
|
|
|
CC_ASSERT(s2 == "ABCDEF");
|
|
|
|
|
|
|
|
const char* cstr = "XYZ";
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string s3 = JniHelper::callStaticStringMethod(classPath, "stringMethod", cstr, true);
|
2015-11-08 04:57:05 +08:00
|
|
|
CC_ASSERT(s3 == "ZYX");
|
2015-11-11 04:39:16 +08:00
|
|
|
|
|
|
|
// should not crash
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
{
|
2015-11-11 04:39:16 +08:00
|
|
|
JniHelper::callStaticVoidMethod(classPath, "voidMethod4", "ABCDEF");
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not compile
|
|
|
|
// JniHelper::callStaticVoidMethod(classPath, "voidMethod4", std::vector<int>());
|
2015-11-08 04:57:05 +08:00
|
|
|
}
|
2016-04-28 09:49:55 +08:00
|
|
|
|
|
|
|
#endif
|