summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2023-10-29 02:57:57 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2023-11-07 01:59:51 +0800
commit93a6cd8bdfbb7a5bcb03c184cd1457e231a7939f (patch)
tree9f7d88d1a8729b7e141525be0dea000394cb81f8 /tests/auto/corelib/kernel
parente579cdceb8b54fea02c1ed9bfae8b5ef74902270 (diff)
JNI: allow QString as a parameter to native methods
Expect a jstring on the va_list, and implicitly construct a QString from that. As a drive-by, allow native methods to take parameters by reference, and move implementation details into a Detail namespace. Add test coverage. Change-Id: I31214938ccaea3f4d539b432e29d12434dd98377 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Petri Virkkunen <petri.virkkunen@qt.io> Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel')
-rw-r--r--tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java12
-rw-r--r--tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp80
-rw-r--r--tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp9
3 files changed, 74 insertions, 27 deletions
diff --git a/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java b/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java
index b56447198f..ef1c6e2f88 100644
--- a/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java
+++ b/tests/auto/corelib/kernel/qjniobject/testdata/src/org/qtproject/qt/android/testdata/QtJniObjectTestClass.java
@@ -275,6 +275,8 @@ public class QtJniObjectTestClass
// --------------------------------------------------------------------------------------------
native public int callbackWithObject(QtJniObjectTestClass that);
+ native public int callbackWithObjectRef(QtJniObjectTestClass that);
+ native public int callbackWithString(String string);
native public int callbackWithByte(byte value);
native public int callbackWithBoolean(boolean value);
native public int callbackWithInt(int value);
@@ -285,6 +287,16 @@ public class QtJniObjectTestClass
return callbackWithObject(that);
}
+ public int callMeBackWithObjectRef(QtJniObjectTestClass that)
+ {
+ return callbackWithObjectRef(that);
+ }
+
+ public int callMeBackWithString(String string)
+ {
+ return callbackWithString(string);
+ }
+
public int callMeBackWithByte(byte value)
{
return callbackWithByte(value);
diff --git a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
index c658c35bb0..95707c2ae9 100644
--- a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
+++ b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp
@@ -1863,20 +1863,44 @@ void tst_QJniObject::largeObjectArray()
}
}
+enum class CallbackParameterType
+{
+ Object,
+ ObjectRef,
+ String,
+ Byte,
+ Boolean,
+ Int,
+ Double
+};
static std::optional<TestClass> calledWithObject;
static int callbackWithObject(JNIEnv *, jobject, TestClass that)
{
calledWithObject.emplace(that);
- return 42;
+ return int(CallbackParameterType::Object);
}
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithObject)
+static int callbackWithObjectRef(JNIEnv *, jobject, const TestClass &that)
+{
+ calledWithObject.emplace(that);
+ return int(CallbackParameterType::ObjectRef);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithObjectRef)
+
+static std::optional<QString> calledWithString;
+static int callbackWithString(JNIEnv *, jobject, const QString &string)
+{
+ calledWithString.emplace(string);
+ return int(CallbackParameterType::String);
+}
+Q_DECLARE_JNI_NATIVE_METHOD(callbackWithString)
static std::optional<jbyte> calledWithByte;
static int callbackWithByte(JNIEnv *, jobject, jbyte value)
{
calledWithByte.emplace(value);
- return 43;
+ return int(CallbackParameterType::Byte);
}
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithByte)
@@ -1884,7 +1908,7 @@ static std::optional<jbyte> calledWithBoolean;
static int callbackWithBoolean(JNIEnv *, jobject, bool value)
{
calledWithBoolean.emplace(value);
- return 44;
+ return int(CallbackParameterType::Boolean);
}
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithBoolean)
@@ -1892,7 +1916,7 @@ static std::optional<int> calledWithInt;
static int callbackWithInt(JNIEnv *, jobject, int value)
{
calledWithInt.emplace(value);
- return 45;
+ return int(CallbackParameterType::Int);
}
Q_DECLARE_JNI_NATIVE_METHOD(callbackWithInt)
@@ -1900,40 +1924,26 @@ static std::optional<double> calledWithDouble;
static int callbackWithDouble(JNIEnv *, jobject, double value)
{
calledWithDouble.emplace(value);
- return 46;
+ return int(CallbackParameterType::Double);
}
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, {});
+ QTest::addRow("Object") << CallbackParameterType::Object;
+ QTest::addRow("ObjectRef") << CallbackParameterType::ObjectRef;
+ QTest::addRow("String") << CallbackParameterType::String;
+ QTest::addRow("Byte") << CallbackParameterType::Byte;
+ QTest::addRow("Boolean") << CallbackParameterType::Boolean;
+ QTest::addRow("Int") << CallbackParameterType::Int;
+ QTest::addRow("Double") << CallbackParameterType::Double;
}
void tst_QJniObject::callback()
{
QFETCH(const CallbackParameterType, parameterType);
- QFETCH(const int, expectedResult);
TestClass testObject;
QJniEnvironment env;
@@ -1948,6 +1958,22 @@ void tst_QJniObject::callback()
QVERIFY(calledWithObject);
QCOMPARE(calledWithObject.value(), testObject);
break;
+ case CallbackParameterType::ObjectRef:
+ QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
+ Q_JNI_NATIVE_METHOD(callbackWithObjectRef)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithObjectRef", testObject);
+ QVERIFY(calledWithObject);
+ QCOMPARE(calledWithObject.value(), testObject);
+ break;
+ case CallbackParameterType::String:
+ QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
+ Q_JNI_NATIVE_METHOD(callbackWithString)
+ }));
+ result = testObject.callMethod<int>("callMeBackWithString", QString::number(123));
+ QVERIFY(calledWithString);
+ QCOMPARE(calledWithString.value(), "123");
+ break;
case CallbackParameterType::Byte:
QVERIFY(env.registerNativeMethods(testObject.objectClass(), {
Q_JNI_NATIVE_METHOD(callbackWithByte)
@@ -1981,7 +2007,7 @@ void tst_QJniObject::callback()
QCOMPARE(calledWithDouble.value(), 1.2345);
break;
}
- QCOMPARE(result, expectedResult);
+ QCOMPARE(result, int(parameterType));
}
QTEST_MAIN(tst_QJniObject)
diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp
index 58fd6790c1..c332d6634a 100644
--- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp
+++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp
@@ -161,6 +161,15 @@ Q_DECLARE_JNI_NATIVE_METHOD(nativeFunction)
static_assert(QtJniTypes::nativeMethodSignature(nativeFunction) == "(ILjava/lang/String;J)Z");
+static QString nativeFunctionStrings(JNIEnv *, jclass, const QString &, const QtJniTypes::String &)
+{
+ return QString();
+}
+Q_DECLARE_JNI_NATIVE_METHOD(nativeFunctionStrings)
+
+static_assert(QtJniTypes::nativeMethodSignature(nativeFunctionStrings)
+ == "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
+
static int forwardDeclaredNativeFunction(JNIEnv *, jobject, bool);
Q_DECLARE_JNI_NATIVE_METHOD(forwardDeclaredNativeFunction)
static int forwardDeclaredNativeFunction(JNIEnv *, jobject, bool) { return 0; }