blob: 024ba537083c55ef2ca13ff251a7c65c2745aa79 [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}
Andreas Gamped1104322014-05-01 14:38:56 -0700140
141constexpr size_t kByteReturnSize = 7;
142jbyte byte_returns[kByteReturnSize] = { 0, 1, 2, 127, -1, -2, -128 };
143
144extern "C" jbyte JNICALL Java_JniTest_byteMethod(JNIEnv* env, jclass klass, jbyte b1, jbyte b2,
145 jbyte b3, jbyte b4, jbyte b5, jbyte b6,
146 jbyte b7, jbyte b8, jbyte b9, jbyte b10) {
147 // We use b1 to drive the output.
148 assert(b2 == 2);
149 assert(b3 == -3);
150 assert(b4 == 4);
151 assert(b5 == -5);
152 assert(b6 == 6);
153 assert(b7 == -7);
154 assert(b8 == 8);
155 assert(b9 == -9);
156 assert(b10 == 10);
157
158 assert(0 <= b1);
159 assert(b1 < static_cast<jbyte>(kByteReturnSize));
160
161 return byte_returns[b1];
162}
163
164constexpr size_t kShortReturnSize = 9;
165jshort short_returns[kShortReturnSize] = { 0, 1, 2, 127, 32767, -1, -2, -128,
166 static_cast<jshort>(0x8000) };
167// The weird static_cast is because short int is only guaranteed down to -32767, not Java's -32768.
168
169extern "C" jshort JNICALL Java_JniTest_shortMethod(JNIEnv* env, jclass klass, jshort s1, jshort s2,
170 jshort s3, jshort s4, jshort s5, jshort s6,
171 jshort s7, jshort s8, jshort s9, jshort s10) {
172 // We use s1 to drive the output.
173 assert(s2 == 2);
174 assert(s3 == -3);
175 assert(s4 == 4);
176 assert(s5 == -5);
177 assert(s6 == 6);
178 assert(s7 == -7);
179 assert(s8 == 8);
180 assert(s9 == -9);
181 assert(s10 == 10);
182
183 assert(0 <= s1);
184 assert(s1 < static_cast<jshort>(kShortReturnSize));
185
186 return short_returns[s1];
187}
188
189extern "C" jboolean JNICALL Java_JniTest_booleanMethod(JNIEnv* env, jclass klass, jboolean b1,
190 jboolean b2, jboolean b3, jboolean b4,
191 jboolean b5, jboolean b6, jboolean b7,
192 jboolean b8, jboolean b9, jboolean b10) {
193 // We use b1 to drive the output.
194 assert(b2 == JNI_TRUE);
195 assert(b3 == JNI_FALSE);
196 assert(b4 == JNI_TRUE);
197 assert(b5 == JNI_FALSE);
198 assert(b6 == JNI_TRUE);
199 assert(b7 == JNI_FALSE);
200 assert(b8 == JNI_TRUE);
201 assert(b9 == JNI_FALSE);
202 assert(b10 == JNI_TRUE);
203
204 assert(b1 == JNI_TRUE || b1 == JNI_FALSE);
205 return b1;
206}
207
208constexpr size_t kCharReturnSize = 8;
209jchar char_returns[kCharReturnSize] = { 0, 1, 2, 127, 255, 256, 15000, 34000 };
210
211extern "C" jchar JNICALL Java_JniTest_charMethod(JNIEnv* env, jclass klacc, jchar c1, jchar c2,
212 jchar c3, jchar c4, jchar c5, jchar c6,
213 jchar c7, jchar c8, jchar c9, jchar c10) {
214 // We use c1 to drive the output.
215 assert(c2 == 'a');
216 assert(c3 == 'b');
217 assert(c4 == 'c');
218 assert(c5 == '0');
219 assert(c6 == '1');
220 assert(c7 == '2');
221 assert(c8 == 1234);
222 assert(c9 == 2345);
223 assert(c10 == 3456);
224
225 assert(c1 < static_cast<jchar>(kCharReturnSize));
226
227 return char_returns[c1];
228}