Commit 3a6a6f6b authored by Volker Krause's avatar Volker Krause
Browse files

Rework the fake QAndroidJniObject to behave closer to the original

This allows to simplify the code to access the owner handle in the
property wrappers.
parent ba47da67
......@@ -20,9 +20,9 @@ private Q_SLOTS:
{
#ifndef Q_OS_ANDROID
const auto pn = Context::getPackageName();
QCOMPARE(pn.m_protocol.size(), 2);
QCOMPARE(pn.m_protocol.at(0), QLatin1String("global androidContext()"));
QCOMPARE(pn.m_protocol.at(1), QLatin1String("callObjectMethod: getPackageName ()Ljava/lang/String;"));
QCOMPARE(pn.protocol().size(), 2);
QCOMPARE(pn.protocol().at(0), QLatin1String("global androidContext()"));
QCOMPARE(pn.protocol().at(1), QLatin1String("callObjectMethod: getPackageName ()Ljava/lang/String;"));
#endif
}
......
......@@ -61,13 +61,13 @@ private Q_SLOTS:
const QAndroidJniObject bla = obj.myUriField;
obj.myUriField = bla;
QCOMPARE(obj.m_handle.m_protocol.size(), 6);
QCOMPARE(obj.m_handle.m_protocol.at(0), QLatin1String("getObjectField: myStringField Ljava/lang/String;"));
QCOMPARE(obj.m_handle.m_protocol.at(1), QLatin1String("setField: myStringField Ljava/lang/String;"));
QCOMPARE(obj.m_handle.m_protocol.at(2), QLatin1String("getField: myIntField I"));
QCOMPARE(obj.m_handle.m_protocol.at(3), QLatin1String("setField: myIntField I"));
QCOMPARE(obj.m_handle.m_protocol.at(4), QLatin1String("getObjectField: myUriField Landroid/net/Uri;"));
QCOMPARE(obj.m_handle.m_protocol.at(5), QLatin1String("setField: myUriField Landroid/net/Uri;"));
QCOMPARE(obj.m_handle.protocol().size(), 6);
QCOMPARE(obj.m_handle.protocol().at(0), QLatin1String("getObjectField: myStringField Ljava/lang/String;"));
QCOMPARE(obj.m_handle.protocol().at(1), QLatin1String("setField: myStringField Ljava/lang/String;"));
QCOMPARE(obj.m_handle.protocol().at(2), QLatin1String("getField: myIntField I"));
QCOMPARE(obj.m_handle.protocol().at(3), QLatin1String("setField: myIntField I"));
QCOMPARE(obj.m_handle.protocol().at(4), QLatin1String("getObjectField: myUriField Landroid/net/Uri;"));
QCOMPARE(obj.m_handle.protocol().at(5), QLatin1String("setField: myUriField Landroid/net/Uri;"));
#endif
}
};
......
......@@ -18,7 +18,10 @@ target_sources(KAndroidExtras PRIVATE
)
if (NOT ANDROID)
target_sources(KAndroidExtras PRIVATE fake/mock_impl.cpp)
target_sources(KAndroidExtras PRIVATE
fake/mock_impl.cpp
fake/qandroidjniobject.cpp
)
endif()
generate_export_header(KAndroidExtras BASE_NAME KAndroidExtras)
......
......@@ -3,111 +3,4 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef FAKE_QANDROIDJNIOBJECT_H
#define FAKE_QANDROIDJNIOBJECT_H
#include "kandroidextras_export.h"
#include "jni.h"
#include <kandroidextras/jnisignature.h>
#include <kandroidextras/jnitypes.h>
#include <QStringList>
/** Mock object for QAndroidJniObject outside of Android, for automated testing. */
class KANDROIDEXTRAS_EXPORT QAndroidJniObject {
public:
QAndroidJniObject() = default;
QAndroidJniObject(const char *className)
{
m_protocol.push_back(QLatin1String("ctor: ") + QLatin1String(className));
}
QAndroidJniObject(const char *className, const char *signature, ...)
{
m_protocol.push_back(QLatin1String("ctor: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(signature));
}
bool isValid() const { return true; }
static QAndroidJniObject fromString(const QString &s) { Q_UNUSED(s); return {}; }
static QAndroidJniObject fromLocalRef(jobject o)
{
QAndroidJniObject obj;
obj.m_protocol.push_back(QLatin1String("ctor: ") + QString::number((qulonglong)o));
return obj;
}
QString toString() const { return m_protocol.join(QLatin1Char('\n')); }
jobject object() const { return nullptr; }
template <typename T>
T object() const { return {}; }
template <typename T>
T callMethod(const char *methodName, const char *signature, ...) const
{
m_protocol.push_back(QLatin1String("callMethod: ") + QLatin1String(methodName) + QLatin1Char(' ') + QLatin1String(signature));
return {};
}
QAndroidJniObject callObjectMethod(const char *methodName, const char *signature, ...) const
{
QAndroidJniObject obj;
obj.m_protocol = m_protocol;
obj.m_protocol.push_back(QLatin1String("callObjectMethod: ") + QLatin1String(methodName) + QLatin1Char(' ') + QLatin1String(signature));
return obj;
}
template <typename T>
T getField(const char *fieldName) const
{
m_protocol.push_back(QLatin1String("getField: ") + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(KAndroidExtras::Jni::signature<T>()));
return {};
}
QAndroidJniObject getObjectField(const char *fieldName, const char *signature) const
{
m_protocol.push_back(QLatin1String("getObjectField: ") + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(signature));
return {};
}
template <typename T>
void setField(const char *fieldName, const char *signature, T value)
{
Q_UNUSED(value);
m_protocol.push_back(QLatin1String("setField: ") + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(signature));
}
static QAndroidJniObject callStaticObjectMethod(const char *methodName, const char *signature, ...)
{
QAndroidJniObject obj;
obj.m_protocol.push_back(QLatin1String("callStaticObjectMethod: ") + QLatin1String(methodName) + QLatin1Char(' ') + QLatin1String(signature));
return obj;
}
static QAndroidJniObject getStaticObjectField(const char *className, const char *fieldName, const char *signature)
{
m_staticProtocol.push_back(QLatin1String("getStaticObjectField: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(signature));
return {};
}
template <typename T>
static QAndroidJniObject getStaticObjectField(const char *className, const char *fieldName)
{
m_staticProtocol.push_back(QLatin1String("getStaticObjectField<>: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(fieldName));
return {};
}
template <typename T>
static T getStaticField(const char *className, const char *fieldName)
{
m_staticProtocol.push_back(QLatin1String("getStaticField<>: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(KAndroidExtras::Jni::signature<T>()));
return {};
}
mutable QStringList m_protocol;
static QStringList m_staticProtocol;
};
#endif
#include "qandroidjniobject.h"
......@@ -14,14 +14,14 @@ namespace QtAndroid {
inline QAndroidJniObject androidContext()
{
QAndroidJniObject obj;
obj.m_protocol.push_back(QStringLiteral("global androidContext()"));
obj.addToProtocol(QStringLiteral("global androidContext()"));
return obj;
}
inline QAndroidJniObject androidActivity()
{
QAndroidJniObject obj;
obj.m_protocol.push_back(QStringLiteral("global androidActivity()"));
obj.addToProtocol(QStringLiteral("global androidActivity()"));
return obj;
}
......
......@@ -7,4 +7,3 @@
#include "QAndroidJniObject"
int JNIEnv::m_arrayLength = 0;
QStringList QAndroidJniObject::m_staticProtocol;
/*
SPDX-FileCopyrightText: 2020-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "qandroidjniobject.h"
QStringList QAndroidJniObject::m_staticProtocol;
class QAndroidJniObjectPrivate : public QSharedData
{
public:
QStringList protocol;
};
QAndroidJniObject::QAndroidJniObject()
: d(new QAndroidJniObjectPrivate)
{
}
QAndroidJniObject::QAndroidJniObject(const char *className)
: d(new QAndroidJniObjectPrivate)
{
addToProtocol(QLatin1String("ctor: ") + QLatin1String(className));
}
QAndroidJniObject::QAndroidJniObject(const char* className, const char* signature, ...)
: d(new QAndroidJniObjectPrivate)
{
addToProtocol(QLatin1String("ctor: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(signature));
}
QAndroidJniObject::QAndroidJniObject(const QAndroidJniObject&) = default;
QAndroidJniObject & QAndroidJniObject::operator=(const QAndroidJniObject&) = default;
QAndroidJniObject::~QAndroidJniObject() = default;
QStringList QAndroidJniObject::protocol() const
{
return d->protocol;
}
void QAndroidJniObject::addToProtocol(const QString &line) const
{
d->protocol.push_back(line);
}
void QAndroidJniObject::setProtocol(const QStringList &protocol)
{
d->protocol = protocol;
}
/*
SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef FAKE_QANDROIDJNIOBJECT_H
#define FAKE_QANDROIDJNIOBJECT_H
#include "kandroidextras_export.h"
#include "jni.h"
#include <kandroidextras/jnisignature.h>
#include <kandroidextras/jnitypes.h>
#include <QExplicitlySharedDataPointer>
#include <QStringList>
class QAndroidJniObjectPrivate;
/** Mock object for QAndroidJniObject outside of Android, for automated testing. */
class KANDROIDEXTRAS_EXPORT QAndroidJniObject {
public:
QAndroidJniObject();
QAndroidJniObject(const QAndroidJniObject&);
~QAndroidJniObject();
QAndroidJniObject& operator=(const QAndroidJniObject&);
QAndroidJniObject(const char *className);
QAndroidJniObject(const char *className, const char *signature, ...);
bool isValid() const { return true; }
static QAndroidJniObject fromString(const QString &s) { Q_UNUSED(s); return {}; }
static QAndroidJniObject fromLocalRef(jobject o)
{
QAndroidJniObject obj;
obj.addToProtocol(QLatin1String("ctor: ") + QString::number((qulonglong)o));
return obj;
}
QString toString() const { return protocol().join(QLatin1Char('\n')); }
jobject object() const { return nullptr; }
template <typename T>
T object() const { return {}; }
template <typename T>
T callMethod(const char *methodName, const char *signature, ...) const
{
addToProtocol(QLatin1String("callMethod: ") + QLatin1String(methodName) + QLatin1Char(' ') + QLatin1String(signature));
return {};
}
QAndroidJniObject callObjectMethod(const char *methodName, const char *signature, ...) const
{
QAndroidJniObject obj;
obj.setProtocol(protocol());
obj.addToProtocol(QLatin1String("callObjectMethod: ") + QLatin1String(methodName) + QLatin1Char(' ') + QLatin1String(signature));
return obj;
}
template <typename T>
T getField(const char *fieldName) const
{
addToProtocol(QLatin1String("getField: ") + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(KAndroidExtras::Jni::signature<T>()));
return {};
}
QAndroidJniObject getObjectField(const char *fieldName, const char *signature) const
{
addToProtocol(QLatin1String("getObjectField: ") + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(signature));
return {};
}
template <typename T>
void setField(const char *fieldName, const char *signature, T value)
{
Q_UNUSED(value);
addToProtocol(QLatin1String("setField: ") + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(signature));
}
static QAndroidJniObject callStaticObjectMethod(const char *methodName, const char *signature, ...)
{
QAndroidJniObject obj;
obj.addToProtocol(QLatin1String("callStaticObjectMethod: ") + QLatin1String(methodName) + QLatin1Char(' ') + QLatin1String(signature));
return obj;
}
static QAndroidJniObject getStaticObjectField(const char *className, const char *fieldName, const char *signature)
{
m_staticProtocol.push_back(QLatin1String("getStaticObjectField: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(signature));
return {};
}
template <typename T>
static QAndroidJniObject getStaticObjectField(const char *className, const char *fieldName)
{
m_staticProtocol.push_back(QLatin1String("getStaticObjectField<>: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(fieldName));
return {};
}
template <typename T>
static T getStaticField(const char *className, const char *fieldName)
{
m_staticProtocol.push_back(QLatin1String("getStaticField<>: ") + QLatin1String(className) + QLatin1Char(' ') + QLatin1String(fieldName) + QLatin1Char(' ') + QLatin1String(KAndroidExtras::Jni::signature<T>()));
return {};
}
static QStringList m_staticProtocol;
QStringList protocol() const;
void addToProtocol(const QString &line) const;
private:
void setProtocol(const QStringList &protocol);
QExplicitlySharedDataPointer<QAndroidJniObjectPrivate> d;
};
#endif
......@@ -70,11 +70,7 @@ struct StaticProperty<PropType, ClassType, NameHolder, true> {
template <typename ClassType, typename OffsetHolder>
class PropertyBase {
protected:
inline QAndroidJniObject& handle() {
const auto owner = reinterpret_cast<ClassType*>(reinterpret_cast<char*>(this) - OffsetHolder::offset());
return owner->m_handle;
}
inline const QAndroidJniObject& handle() const {
inline QAndroidJniObject handle() const {
const auto owner = reinterpret_cast<const ClassType*>(reinterpret_cast<const char*>(this) - OffsetHolder::offset());
return owner->m_handle;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment