Skip to content

Commit

Permalink
Add tests for NewWeakGlobalRef and DeleteWeakGlobalRef
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 6, 2017
1 parent c3bba1d commit cc7ce5c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
6 changes: 3 additions & 3 deletions include/jni/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ namespace jni
template < class T >
UniqueWeakGlobalRef<T> NewWeakGlobalRef(JNIEnv& env, T* t)
{
T* result = Wrap<T*>(env.NewWeakGlobalRef(Unwrap(t)));
jobject* obj = Wrap<jobject*>(env.NewWeakGlobalRef(Unwrap(t)));
CheckJavaException(env);
if (!result)
if (t && !obj)
throw std::bad_alloc();
return UniqueWeakGlobalRef<T>(result, WeakGlobalRefDeleter(env));
return UniqueWeakGlobalRef<T>(reinterpret_cast<T*>(obj), WeakGlobalRefDeleter(env));
}

inline void DeleteWeakGlobalRef(JNIEnv& env, UniqueWeakGlobalRef<jobject>&& ref)
Expand Down
13 changes: 13 additions & 0 deletions test/high_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ int main()
assert(classValue == testClass);

static bool calledNewGlobalRef = false;
static bool calledNewWeakGlobalRef = false;

env.functions->NewGlobalRef = [] (JNIEnv*, jobject obj) -> jobject
{
Expand All @@ -82,6 +83,16 @@ int main()
{
};

env.functions->NewWeakGlobalRef = [] (JNIEnv*, jobject obj) -> jobject
{
calledNewWeakGlobalRef = true;
return obj;
};

env.functions->DeleteWeakGlobalRef = [] (JNIEnv*, jobject) -> void
{
};

testClass.NewGlobalRef(env);
assert(calledNewGlobalRef);

Expand All @@ -90,9 +101,11 @@ int main()

jni::Object<Test> object { objectValue.Ptr() };
object.NewGlobalRef(env);
object.NewWeakGlobalRef(env);

jni::String string { stringValue.Ptr() };
string.NewGlobalRef(env);
string.NewWeakGlobalRef(env);


/// Constructor
Expand Down
35 changes: 32 additions & 3 deletions test/low_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ static void TestNewAndDeleteGlobalRef()
[] { jni::DeleteGlobalRef(env, jni::NewGlobalRef(env, failureValue.Ptr())); }));
}

static void TestNewAndDeleteWeakGlobalRef()
{
static Testable<jni::jobject> objectValue;
static Testable<jni::jclass> classValue;
static Testable<jni::jobject> failureValue;
static TestEnv env;

env.functions->NewWeakGlobalRef = [] (JNIEnv*, jobject obj) -> jobject
{
if (obj == Unwrap(objectValue.Ptr()))
return Unwrap(objectValue.Ptr());
if (obj == Unwrap(classValue.Ptr()))
return Unwrap(classValue.Ptr());
env.exception = true;
return nullptr;
};

env.functions->DeleteWeakGlobalRef = [] (JNIEnv*, jobject obj) -> void
{
if (obj == Unwrap(objectValue.Ptr()))
return;
env.exception = true;
};

assert(objectValue == *jni::NewWeakGlobalRef(env, objectValue.Ptr()));
assert(classValue == *jni::NewWeakGlobalRef(env, classValue.Ptr()));
assert(Throws<jni::PendingJavaException>([] { jni::NewWeakGlobalRef(env, failureValue.Ptr()); }));
assert(Throws<jni::PendingJavaException>(
[] { jni::DeleteWeakGlobalRef(env, jni::NewWeakGlobalRef(env, failureValue.Ptr())); }));
}

static void TestNewObject()
{
static Testable<jni::jclass> classValue;
Expand Down Expand Up @@ -227,6 +258,7 @@ int main()
*/

TestNewAndDeleteGlobalRef();
TestNewAndDeleteWeakGlobalRef();

/*
jobject (*NewLocalRef)(JNIEnv*, jobject);
Expand Down Expand Up @@ -303,9 +335,6 @@ int main()
const jchar* (*GetStringCritical)(JNIEnv*, jstring, jboolean*);
void (*ReleaseStringCritical)(JNIEnv*, jstring, const jchar*);
jweak (*NewWeakGlobalRef)(JNIEnv*, jobject);
void (*DeleteWeakGlobalRef)(JNIEnv*, jweak);
*/

/*
Expand Down

0 comments on commit cc7ce5c

Please sign in to comment.