File indexing completed on 2024-05-12 05:13:45
0001 /* 0002 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KAndroidExtras/JniArray> 0008 #include <KAndroidExtras/JniProperty> 0009 #include <KAndroidExtras/JniTypeTraits> 0010 #include <KAndroidExtras/JavaTypes> 0011 #include <KAndroidExtras/Uri> 0012 0013 #include <QtTest/qtest.h> 0014 0015 using namespace KAndroidExtras; 0016 0017 class TestClass 0018 { 0019 JNI_OBJECT(TestClass, android::content::Context) 0020 JNI_PROPERTY(bool, myBoolProp) 0021 }; 0022 0023 class JniArrayTest : public QObject 0024 { 0025 Q_OBJECT 0026 private Q_SLOTS: 0027 void testTypeTrait() 0028 { 0029 static_assert(!Jni::is_array<int>::value); 0030 static_assert(Jni::is_array<Jni::Array<int>>::value); 0031 } 0032 0033 void testFromArray() 0034 { 0035 #ifndef Q_OS_ANDROID 0036 JNIEnv::m_arrayLength = 3; 0037 0038 QJniObject array; 0039 const auto a1 = Jni::fromArray<std::vector<QJniObject>>(array); 0040 QCOMPARE(a1.size(), 3); 0041 0042 JNIEnv::m_arrayLength = 2; 0043 const auto a2 = Jni::fromArray<QStringList>(array); 0044 QCOMPARE(a2.size(), 2); 0045 QCOMPARE(a2, QStringList({ QStringLiteral("ctor: 0"), QStringLiteral("ctor: 1") })); 0046 0047 JNIEnv::m_arrayLength = 4; 0048 const auto a3 = Jni::fromArray<std::vector<int>>(array); 0049 QCOMPARE(a3.size(), 4); 0050 0051 JNIEnv::m_arrayLength = 1; 0052 const auto a4 = Jni::fromArray<QVector<QUrl>>(array); 0053 QCOMPARE(a4.size(), 1); 0054 #endif 0055 } 0056 0057 void testArrayWrapper() 0058 { 0059 #ifndef Q_OS_ANDROID 0060 JNIEnv::m_arrayLength = 3; 0061 0062 QJniObject array; 0063 // primitive types 0064 const auto a1 = Jni::Array<jint>(array); 0065 QCOMPARE(a1.size(), 3); 0066 QCOMPARE(std::distance(a1.begin(), a1.end()), 3); 0067 QCOMPARE(a1[0], 0); 0068 QCOMPARE(a1[2], 2); 0069 for (jint i : a1) { 0070 QVERIFY(i >= 0 && i < 3); 0071 } 0072 0073 // complex types without wrappers 0074 const auto a2 = Jni::Array<java::lang::String>(array); 0075 JNIEnv::m_arrayLength = 2; 0076 QCOMPARE(a2.size(), 2); 0077 QCOMPARE(std::distance(a2.begin(), a2.end()), 2); 0078 QCOMPARE(a2[0].toString(), QLatin1StringView("ctor: 0")); 0079 QCOMPARE(a2[1].toString(), QLatin1StringView("ctor: 1")); 0080 for (const auto &i : a2) { 0081 QVERIFY(i.isValid()); 0082 } 0083 for (const QString &i : a2) { 0084 QVERIFY(!i.isEmpty()); 0085 } 0086 0087 // complex type with custom wrapper 0088 static_assert(Jni::is_object_wrapper<TestClass>::value); 0089 JNIEnv::m_arrayLength = 1; 0090 const auto a3 = Jni::Array<TestClass>(array); 0091 QCOMPARE(a3.size(), 1); 0092 QCOMPARE(std::distance(a3.begin(), a3.end()), 1); 0093 const auto v = a3[0]; 0094 bool b = a3[0].myBoolProp; 0095 for (const TestClass &i : a3) { 0096 bool b = i.myBoolProp; // verifies this is of the correct type 0097 } 0098 #endif 0099 } 0100 0101 void testArrayCreate() 0102 { 0103 auto a1 = Jni::Array<jshort>(10); 0104 auto a2 = Jni::Array<java::lang::String>(5); 0105 a2[2] = QStringLiteral("test"); 0106 auto a3 = Jni::Array<java::lang::String>(3, QStringLiteral("test")); 0107 } 0108 }; 0109 0110 QTEST_GUILESS_MAIN(JniArrayTest) 0111 0112 #include "jniarraytest.moc"