Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
| 19 | #include <pthread.h> |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame^] | 20 | #include <vector> |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include "jni.h" |
| 23 | |
| 24 | #if defined(NDEBUG) |
| 25 | #error test code compiled without NDEBUG |
| 26 | #endif |
| 27 | |
| 28 | static JavaVM* jvm = NULL; |
| 29 | |
| 30 | extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { |
| 31 | assert(vm != NULL); |
| 32 | assert(jvm == NULL); |
| 33 | jvm = vm; |
| 34 | return JNI_VERSION_1_6; |
| 35 | } |
| 36 | |
| 37 | static void* testFindClassOnAttachedNativeThread(void*) { |
| 38 | assert(jvm != NULL); |
| 39 | |
| 40 | JNIEnv* env = NULL; |
| 41 | JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, NULL }; |
| 42 | int attach_result = jvm->AttachCurrentThread(&env, &args); |
| 43 | assert(attach_result == 0); |
| 44 | |
| 45 | jclass clazz = env->FindClass("JniTest"); |
| 46 | assert(clazz != NULL); |
| 47 | assert(!env->ExceptionCheck()); |
| 48 | |
| 49 | jobjectArray array = env->NewObjectArray(0, clazz, NULL); |
| 50 | assert(array != NULL); |
| 51 | assert(!env->ExceptionCheck()); |
| 52 | |
| 53 | int detach_result = jvm->DetachCurrentThread(); |
| 54 | assert(detach_result == 0); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 58 | // http://b/10994325 |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 59 | extern "C" JNIEXPORT void JNICALL Java_JniTest_testFindClassOnAttachedNativeThread(JNIEnv*, |
| 60 | jclass) { |
| 61 | pthread_t pthread; |
| 62 | int pthread_create_result = pthread_create(&pthread, |
| 63 | NULL, |
| 64 | testFindClassOnAttachedNativeThread, |
| 65 | NULL); |
| 66 | assert(pthread_create_result == 0); |
| 67 | int pthread_join_result = pthread_join(pthread, NULL); |
| 68 | assert(pthread_join_result == 0); |
| 69 | } |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 70 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 71 | static void* testFindFieldOnAttachedNativeThread(void*) { |
| 72 | assert(jvm != NULL); |
| 73 | |
| 74 | JNIEnv* env = NULL; |
| 75 | JavaVMAttachArgs args = { JNI_VERSION_1_6, __FUNCTION__, NULL }; |
| 76 | int attach_result = jvm->AttachCurrentThread(&env, &args); |
| 77 | assert(attach_result == 0); |
| 78 | |
| 79 | jclass clazz = env->FindClass("JniTest"); |
| 80 | assert(clazz != NULL); |
| 81 | assert(!env->ExceptionCheck()); |
| 82 | |
| 83 | jfieldID field = env->GetStaticFieldID(clazz, "testFindFieldOnAttachedNativeThreadField", "Z"); |
| 84 | assert(field != NULL); |
| 85 | assert(!env->ExceptionCheck()); |
| 86 | |
| 87 | env->SetStaticBooleanField(clazz, field, JNI_TRUE); |
| 88 | |
| 89 | int detach_result = jvm->DetachCurrentThread(); |
| 90 | assert(detach_result == 0); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | extern "C" JNIEXPORT void JNICALL Java_JniTest_testFindFieldOnAttachedNativeThreadNative(JNIEnv*, |
| 95 | jclass) { |
| 96 | pthread_t pthread; |
| 97 | int pthread_create_result = pthread_create(&pthread, |
| 98 | NULL, |
| 99 | testFindFieldOnAttachedNativeThread, |
| 100 | NULL); |
| 101 | assert(pthread_create_result == 0); |
| 102 | int pthread_join_result = pthread_join(pthread, NULL); |
| 103 | assert(pthread_join_result == 0); |
| 104 | } |
| 105 | |
| 106 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 107 | // http://b/11243757 |
| 108 | extern "C" JNIEXPORT void JNICALL Java_JniTest_testCallStaticVoidMethodOnSubClassNative(JNIEnv* env, |
| 109 | jclass) { |
| 110 | jclass super_class = env->FindClass("JniTest$testCallStaticVoidMethodOnSubClass_SuperClass"); |
| 111 | assert(super_class != NULL); |
| 112 | |
| 113 | jmethodID execute = env->GetStaticMethodID(super_class, "execute", "()V"); |
| 114 | assert(execute != NULL); |
| 115 | |
| 116 | jclass sub_class = env->FindClass("JniTest$testCallStaticVoidMethodOnSubClass_SubClass"); |
| 117 | assert(sub_class != NULL); |
| 118 | |
| 119 | env->CallStaticVoidMethod(sub_class, execute); |
| 120 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 121 | |
| 122 | extern "C" JNIEXPORT jobject JNICALL Java_JniTest_testGetMirandaMethodNative(JNIEnv* env, jclass) { |
| 123 | jclass abstract_class = env->FindClass("JniTest$testGetMirandaMethod_MirandaAbstract"); |
| 124 | assert(abstract_class != NULL); |
| 125 | jmethodID miranda_method = env->GetMethodID(abstract_class, "inInterface", "()Z"); |
| 126 | assert(miranda_method != NULL); |
| 127 | return env->ToReflectedMethod(abstract_class, miranda_method, JNI_FALSE); |
| 128 | } |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame^] | 129 | |
| 130 | // https://code.google.com/p/android/issues/detail?id=63055 |
| 131 | extern "C" void JNICALL Java_JniTest_testZeroLengthByteBuffers(JNIEnv* env, jclass) { |
| 132 | std::vector<uint8_t> buffer(1); |
| 133 | jobject byte_buffer = env->NewDirectByteBuffer(&buffer[0], 0); |
| 134 | assert(byte_buffer != NULL); |
| 135 | assert(!env->ExceptionCheck()); |
| 136 | |
| 137 | assert(env->GetDirectBufferAddress(byte_buffer) == &buffer[0]); |
| 138 | assert(env->GetDirectBufferCapacity(byte_buffer) == 0); |
| 139 | } |