blob: 33af94b2ab07393cfa0a24a4398f09c22f36a848 [file] [log] [blame]
Brian Carlstromce888532013-10-10 00:32:58 -07001/*
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 Kamathef809d02013-12-19 17:52:47 +000020#include <vector>
Brian Carlstromce888532013-10-10 00:32:58 -070021
22#include "jni.h"
23
24#if defined(NDEBUG)
25#error test code compiled without NDEBUG
26#endif
27
28static JavaVM* jvm = NULL;
29
30extern "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
37static 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 Carlstrom67fe2b42013-10-15 18:51:42 -070058// http://b/10994325
Brian Carlstromce888532013-10-10 00:32:58 -070059extern "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 Carlstrom67fe2b42013-10-15 18:51:42 -070070
Jeff Hao62509b62013-12-10 17:44:56 -080071static 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
94extern "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 Carlstrom67fe2b42013-10-15 18:51:42 -0700107// http://b/11243757
108extern "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 Hao201803f2013-11-20 18:11:39 -0800121
122extern "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 Kamathef809d02013-12-19 17:52:47 +0000129
130// https://code.google.com/p/android/issues/detail?id=63055
131extern "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}