JNI: Add promotion for signed char and extend test case
The jbyte type is a signed char, which also promotes to int in variadic argument functions. Extend the test case to make sure that we don't get any warnings for the most relevant parameter types. Change-Id: I7811e1eebdbc989ab5989eb1a2c502acd0540bc7 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io> Reviewed-by: Zoltan Gera <zoltan.gera@qt.io>
This commit is contained in:
parent
9800c63533
commit
9b37762c55
@ -231,6 +231,7 @@ namespace QtJniMethods {
|
||||
template <typename Arg> struct PromotedType { using Type = Arg; };
|
||||
template <> struct PromotedType<bool> { using Type = int; };
|
||||
template <> struct PromotedType<char> { using Type = int; };
|
||||
template <> struct PromotedType<signed char> { using Type = int; };
|
||||
template <> struct PromotedType<unsigned char> { using Type = unsigned int; };
|
||||
template <> struct PromotedType<short> { using Type = int; };
|
||||
template <> struct PromotedType<unsigned short> { using Type = unsigned int; };
|
||||
|
@ -173,9 +173,33 @@ public class QtJniObjectTestClass
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
native public int callbackWithObject(QtJniObjectTestClass that);
|
||||
native public int callbackWithByte(byte value);
|
||||
native public int callbackWithBoolean(boolean value);
|
||||
native public int callbackWithInt(int value);
|
||||
native public int callbackWithDouble(double value);
|
||||
|
||||
public int callMeBackWithObject(QtJniObjectTestClass that)
|
||||
{
|
||||
return callbackWithObject(that);
|
||||
}
|
||||
|
||||
public int callMeBackWithByte(byte value)
|
||||
{
|
||||
return callbackWithByte(value);
|
||||
}
|
||||
|
||||
public int callMeBackWithBoolean(boolean value)
|
||||
{
|
||||
return callbackWithBoolean(value);
|
||||
}
|
||||
|
||||
public int callMeBackWithInt(int value)
|
||||
{
|
||||
return callbackWithInt(value);
|
||||
}
|
||||
|
||||
public int callMeBackWithDouble(double value)
|
||||
{
|
||||
return callbackWithDouble(value);
|
||||
}
|
||||
}
|
||||
|
@ -113,6 +113,7 @@ private slots:
|
||||
void isClassAvailable();
|
||||
void fromLocalRef();
|
||||
|
||||
void callback_data();
|
||||
void callback();
|
||||
|
||||
void cleanupTestCase();
|
||||
@ -1668,27 +1669,123 @@ void tst_QJniObject::fromLocalRef()
|
||||
|
||||
|
||||
static std::optional<TestClass> calledWithObject;
|
||||
static int callbackWithObject(JNIEnv *env, jobject thiz, TestClass that)
|
||||
static int callbackWithObject(JNIEnv *, jobject, TestClass that)
|
||||
{
|
||||
Q_UNUSED(env);
|
||||
Q_UNUSED(thiz);
|
||||
calledWithObject.emplace(that);
|
||||
return 42;
|
||||
}
|
||||
|
||||
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithObject)
|
||||
|
||||
static std::optional<jbyte> calledWithByte;
|
||||
static int callbackWithByte(JNIEnv *, jobject, jbyte value)
|
||||
{
|
||||
calledWithByte.emplace(value);
|
||||
return 43;
|
||||
}
|
||||
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithByte)
|
||||
|
||||
static std::optional<jbyte> calledWithBoolean;
|
||||
static int callbackWithBoolean(JNIEnv *, jobject, bool value)
|
||||
{
|
||||
calledWithBoolean.emplace(value);
|
||||
return 44;
|
||||
}
|
||||
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithBoolean)
|
||||
|
||||
static std::optional<int> calledWithInt;
|
||||
static int callbackWithInt(JNIEnv *, jobject, int value)
|
||||
{
|
||||
calledWithInt.emplace(value);
|
||||
return 45;
|
||||
}
|
||||
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithInt)
|
||||
|
||||
static std::optional<double> calledWithDouble;
|
||||
static int callbackWithDouble(JNIEnv *, jobject, double value)
|
||||
{
|
||||
calledWithDouble.emplace(value);
|
||||
return 46;
|
||||
}
|
||||
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithDouble)
|
||||
|
||||
enum class CallbackParameterType
|
||||
{
|
||||
Object,
|
||||
Byte,
|
||||
Boolean,
|
||||
Int,
|
||||
Double,
|
||||
};
|
||||
|
||||
void tst_QJniObject::callback_data()
|
||||
{
|
||||
QTest::addColumn<CallbackParameterType>("parameterType");
|
||||
QTest::addColumn<int>("expectedResult");
|
||||
|
||||
QTest::addRow("Object") << CallbackParameterType::Object
|
||||
<< callbackWithObject(nullptr, nullptr, {});
|
||||
QTest::addRow("Byte") << CallbackParameterType::Byte
|
||||
<< callbackWithByte(nullptr, nullptr, {});
|
||||
QTest::addRow("Boolean") << CallbackParameterType::Boolean
|
||||
<< callbackWithBoolean(nullptr, nullptr, {});
|
||||
QTest::addRow("Int") << CallbackParameterType::Int
|
||||
<< callbackWithInt(nullptr, nullptr, {});
|
||||
QTest::addRow("Double") << CallbackParameterType::Double
|
||||
<< callbackWithDouble(nullptr, nullptr, {});
|
||||
}
|
||||
|
||||
void tst_QJniObject::callback()
|
||||
{
|
||||
QFETCH(const CallbackParameterType, parameterType);
|
||||
QFETCH(const int, expectedResult);
|
||||
|
||||
TestClass testObject;
|
||||
QJniEnvironment env;
|
||||
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
|
||||
Q_JNI_NATIVE_METHOD(callbackWithObject)
|
||||
}));
|
||||
int result = testObject.callMethod<int>("callMeBackWithObject", testObject);
|
||||
QVERIFY(calledWithObject);
|
||||
QVERIFY(calledWithObject.value() == testObject);
|
||||
QCOMPARE(result, 42);
|
||||
int result = -1;
|
||||
|
||||
switch (parameterType) {
|
||||
case CallbackParameterType::Object:
|
||||
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
|
||||
Q_JNI_NATIVE_METHOD(callbackWithObject)
|
||||
}));
|
||||
result = testObject.callMethod<int>("callMeBackWithObject", testObject);
|
||||
QVERIFY(calledWithObject);
|
||||
QCOMPARE(calledWithObject.value(), testObject);
|
||||
break;
|
||||
case CallbackParameterType::Byte:
|
||||
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
|
||||
Q_JNI_NATIVE_METHOD(callbackWithByte)
|
||||
}));
|
||||
result = testObject.callMethod<int>("callbackWithByte", jbyte(123));
|
||||
QVERIFY(calledWithByte);
|
||||
QCOMPARE(calledWithByte.value(), 123);
|
||||
break;
|
||||
case CallbackParameterType::Boolean:
|
||||
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
|
||||
Q_JNI_NATIVE_METHOD(callbackWithBoolean)
|
||||
}));
|
||||
result = testObject.callMethod<int>("callbackWithBoolean", true);
|
||||
QVERIFY(calledWithBoolean);
|
||||
QCOMPARE(calledWithBoolean.value(), true);
|
||||
break;
|
||||
case CallbackParameterType::Int:
|
||||
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
|
||||
Q_JNI_NATIVE_METHOD(callbackWithInt)
|
||||
}));
|
||||
result = testObject.callMethod<int>("callbackWithInt", 12345);
|
||||
QVERIFY(calledWithInt);
|
||||
QCOMPARE(calledWithInt.value(), 12345);
|
||||
break;
|
||||
case CallbackParameterType::Double:
|
||||
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
|
||||
Q_JNI_NATIVE_METHOD(callbackWithDouble)
|
||||
}));
|
||||
result = testObject.callMethod<int>("callbackWithDouble", 1.2345);
|
||||
QVERIFY(calledWithDouble);
|
||||
QCOMPARE(calledWithDouble.value(), 1.2345);
|
||||
break;
|
||||
}
|
||||
QCOMPARE(result, expectedResult);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QJniObject)
|
||||
|
Loading…
Reference in New Issue
Block a user