Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 18 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 21 | #include <cmath> |
| 22 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 23 | #include "common_test.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 28 | class JniInternalTest : public CommonTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 29 | protected: |
| 30 | virtual void SetUp() { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 31 | CommonTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 33 | vm_ = Runtime::Current()->GetJavaVM(); |
| 34 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 35 | // Turn on -verbose:jni for the JNI tests. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 36 | gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 38 | env_ = Thread::Current()->GetJniEnv(); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 40 | ScopedLocalRef<jclass> aioobe(env_, env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
| 41 | CHECK(aioobe.get() != NULL); |
| 42 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 44 | ScopedLocalRef<jclass> sioobe(env_, env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
| 45 | CHECK(sioobe.get() != NULL); |
| 46 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 47 | } |
| 48 | |
| 49 | virtual void TearDown() { |
| 50 | env_->DeleteGlobalRef(aioobe_); |
| 51 | env_->DeleteGlobalRef(sioobe_); |
| 52 | CommonTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 53 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 55 | JavaVMExt* vm_; |
| 56 | JNIEnvExt* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 57 | jclass aioobe_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 58 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 61 | TEST_F(JniInternalTest, AllocObject) { |
| 62 | jclass c = env_->FindClass("java/lang/String"); |
| 63 | ASSERT_TRUE(c != NULL); |
| 64 | jobject o = env_->AllocObject(c); |
| 65 | ASSERT_TRUE(o != NULL); |
| 66 | |
| 67 | // We have an instance of the class we asked for... |
| 68 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 69 | // ...whose fields haven't been initialized because |
| 70 | // we didn't call a constructor. |
| 71 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 72 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
| 73 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL); |
| 74 | } |
| 75 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 76 | TEST_F(JniInternalTest, GetVersion) { |
| 77 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 78 | } |
| 79 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 80 | #define EXPECT_CLASS_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 81 | EXPECT_TRUE(env_->FindClass(NAME) != NULL); \ |
| 82 | EXPECT_FALSE(env_->ExceptionCheck()) |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 83 | |
| 84 | #define EXPECT_CLASS_NOT_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(env_->FindClass(NAME) == NULL); \ |
| 86 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 87 | env_->ExceptionClear() |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 88 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 89 | std::string gCheckJniAbortMessage; |
| 90 | void TestCheckJniAbortHook(const std::string& reason) { |
| 91 | gCheckJniAbortMessage = reason; |
| 92 | } |
| 93 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 94 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 95 | // Reference types... |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 96 | EXPECT_CLASS_FOUND("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 97 | // ...for arrays too, where you must include "L;". |
| 98 | EXPECT_CLASS_FOUND("[Ljava/lang/String;"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 99 | |
| 100 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 101 | // We support . as well as / for compatibility, if -Xcheck:jni is off. |
| 102 | EXPECT_CLASS_FOUND("java.lang.String"); |
| 103 | EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 104 | EXPECT_CLASS_FOUND("[Ljava.lang.String;"); |
| 105 | EXPECT_CLASS_NOT_FOUND("[java.lang.String"); |
| 106 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 107 | // You can't include the "L;" in a JNI class descriptor. |
| 108 | EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;"); |
| 109 | // But you must include it for an array of any reference type. |
| 110 | EXPECT_CLASS_NOT_FOUND("[java/lang/String"); |
| 111 | vm_->check_jni_abort_hook = NULL; |
| 112 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 113 | // Primitive arrays are okay (if the primitive type is valid)... |
| 114 | EXPECT_CLASS_FOUND("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 115 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 116 | EXPECT_CLASS_NOT_FOUND("[K"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 117 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 118 | // But primitive types aren't allowed... |
| 119 | EXPECT_CLASS_NOT_FOUND("C"); |
| 120 | EXPECT_CLASS_NOT_FOUND("K"); |
| 121 | } |
| 122 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 123 | #define EXPECT_EXCEPTION(exception_class) \ |
| 124 | do { \ |
| 125 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 126 | jthrowable exception = env_->ExceptionOccurred(); \ |
| 127 | EXPECT_NE(static_cast<jthrowable>(NULL), exception); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 128 | env_->ExceptionClear(); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 129 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 130 | } while (false) |
| 131 | |
| 132 | TEST_F(JniInternalTest, GetFieldID) { |
| 133 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 134 | ASSERT_TRUE(jlnsfe != NULL); |
| 135 | jclass c = env_->FindClass("java/lang/String"); |
| 136 | ASSERT_TRUE(c != NULL); |
| 137 | |
| 138 | // Wrong type. |
| 139 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
| 140 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 141 | EXPECT_EXCEPTION(jlnsfe); |
| 142 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 143 | // Wrong type where type doesn't exist. |
| 144 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
| 145 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 146 | EXPECT_EXCEPTION(jlnsfe); |
| 147 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 148 | // Wrong name. |
| 149 | fid = env_->GetFieldID(c, "Count", "I"); |
| 150 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 151 | EXPECT_EXCEPTION(jlnsfe); |
| 152 | |
| 153 | // Good declared field lookup. |
| 154 | fid = env_->GetFieldID(c, "count", "I"); |
| 155 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 156 | EXPECT_TRUE(fid != NULL); |
| 157 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 158 | |
| 159 | // Good superclass field lookup. |
| 160 | c = env_->FindClass("java/lang/StringBuilder"); |
| 161 | fid = env_->GetFieldID(c, "count", "I"); |
| 162 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 163 | EXPECT_TRUE(fid != NULL); |
| 164 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 165 | |
| 166 | // Not instance. |
| 167 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 168 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 169 | EXPECT_EXCEPTION(jlnsfe); |
| 170 | } |
| 171 | |
| 172 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 173 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 174 | ASSERT_TRUE(jlnsfe != NULL); |
| 175 | jclass c = env_->FindClass("java/lang/String"); |
| 176 | ASSERT_TRUE(c != NULL); |
| 177 | |
| 178 | // Wrong type. |
| 179 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
| 180 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 181 | EXPECT_EXCEPTION(jlnsfe); |
| 182 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 183 | // Wrong type where type doesn't exist. |
| 184 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
| 185 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 186 | EXPECT_EXCEPTION(jlnsfe); |
| 187 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 188 | // Wrong name. |
| 189 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 190 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 191 | EXPECT_EXCEPTION(jlnsfe); |
| 192 | |
| 193 | // Good declared field lookup. |
| 194 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 195 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 196 | EXPECT_TRUE(fid != NULL); |
| 197 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 198 | |
| 199 | // Not static. |
| 200 | fid = env_->GetStaticFieldID(c, "count", "I"); |
| 201 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 202 | EXPECT_EXCEPTION(jlnsfe); |
| 203 | } |
| 204 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 205 | TEST_F(JniInternalTest, GetMethodID) { |
| 206 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 207 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 208 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 209 | |
| 210 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 211 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 212 | |
| 213 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 214 | // a pending exception |
| 215 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
| 216 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 217 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 218 | |
| 219 | // Check that java.lang.Object.equals() does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 220 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 221 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 222 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 223 | |
| 224 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
| 225 | // method is static |
| 226 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 227 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 228 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 232 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 233 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 234 | |
| 235 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 236 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 237 | |
| 238 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 239 | // a pending exception |
| 240 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
| 241 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 242 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 243 | |
| 244 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 245 | // the method is not static |
| 246 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 247 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 248 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 249 | |
| 250 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 251 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 252 | method = env_->GetStaticMethodID(jlstring, "valueOf", |
| 253 | "(I)Ljava/lang/String;"); |
| 254 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 255 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 258 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 259 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 260 | jclass c = env_->FindClass("java/lang/String"); |
| 261 | ASSERT_TRUE(c != NULL); |
| 262 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 263 | ASSERT_TRUE(fid != NULL); |
| 264 | // Turn the fid into a java.lang.reflect.Field... |
| 265 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
| 266 | ASSERT_TRUE(c != NULL); |
| 267 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 268 | // ...and back again. |
| 269 | jfieldID fid2 = env_->FromReflectedField(field); |
| 270 | ASSERT_TRUE(fid2 != NULL); |
| 271 | } |
| 272 | |
| 273 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 274 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
| 275 | jclass c = env_->FindClass("java/lang/String"); |
| 276 | ASSERT_TRUE(c != NULL); |
| 277 | jmethodID mid = env_->GetMethodID(c, "length", "()I"); |
| 278 | ASSERT_TRUE(mid != NULL); |
| 279 | // Turn the mid into a java.lang.reflect.Method... |
| 280 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 281 | ASSERT_TRUE(c != NULL); |
| 282 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 283 | // ...and back again. |
| 284 | jmethodID mid2 = env_->FromReflectedMethod(method); |
| 285 | ASSERT_TRUE(mid2 != NULL); |
| 286 | } |
| 287 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 288 | void BogusMethod() { |
| 289 | // You can't pass NULL function pointers to RegisterNatives. |
| 290 | } |
| 291 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 292 | TEST_F(JniInternalTest, RegisterNatives) { |
| 293 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 294 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 295 | |
| 296 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 297 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 298 | |
| 299 | // Check that registering to a non-existent java.lang.Object.foo() causes a |
| 300 | // NoSuchMethodError |
| 301 | { |
| 302 | JNINativeMethod methods[] = {{"foo", "()V", NULL}}; |
| 303 | env_->RegisterNatives(jlobject, methods, 1); |
| 304 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 305 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 306 | |
| 307 | // Check that registering non-native methods causes a NoSuchMethodError |
| 308 | { |
| 309 | JNINativeMethod methods[] = {{"equals", "(Ljava/lang/Object;)Z", NULL}}; |
| 310 | env_->RegisterNatives(jlobject, methods, 1); |
| 311 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 312 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 313 | |
| 314 | // Check that registering native methods is successful |
| 315 | { |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 316 | JNINativeMethod methods[] = {{"getClass", "()Ljava/lang/Class;", reinterpret_cast<void*>(BogusMethod)}}; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 317 | env_->RegisterNatives(jlobject, methods, 1); |
| 318 | } |
| 319 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 320 | |
| 321 | env_->UnregisterNatives(jlobject); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 324 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, get_region_fn, set_region_fn, get_elements_fn, release_elements_fn, scalar_type, expected_class_descriptor) \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 325 | jsize size = 4; \ |
| 326 | /* Allocate an array and check it has the right type and length. */ \ |
| 327 | scalar_type ## Array a = env_->new_fn(size); \ |
| 328 | EXPECT_TRUE(a != NULL); \ |
| 329 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 330 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
| 331 | /* AIOOBE for negative start offset. */ \ |
| 332 | env_->get_region_fn(a, -1, 1, NULL); \ |
| 333 | EXPECT_EXCEPTION(aioobe_); \ |
| 334 | env_->set_region_fn(a, -1, 1, NULL); \ |
| 335 | EXPECT_EXCEPTION(aioobe_); \ |
| 336 | /* AIOOBE for negative length. */ \ |
| 337 | env_->get_region_fn(a, 0, -1, NULL); \ |
| 338 | EXPECT_EXCEPTION(aioobe_); \ |
| 339 | env_->set_region_fn(a, 0, -1, NULL); \ |
| 340 | EXPECT_EXCEPTION(aioobe_); \ |
| 341 | /* AIOOBE for buffer overrun. */ \ |
| 342 | env_->get_region_fn(a, size - 1, size, NULL); \ |
| 343 | EXPECT_EXCEPTION(aioobe_); \ |
| 344 | env_->set_region_fn(a, size - 1, size, NULL); \ |
| 345 | EXPECT_EXCEPTION(aioobe_); \ |
| 346 | /* Prepare a couple of buffers. */ \ |
| 347 | scalar_type src_buf[size]; \ |
| 348 | scalar_type dst_buf[size]; \ |
| 349 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 350 | for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \ |
| 351 | /* Copy all of src_buf onto the heap. */ \ |
| 352 | env_->set_region_fn(a, 0, size, src_buf); \ |
| 353 | /* Copy back only part. */ \ |
| 354 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 355 | EXPECT_NE(memcmp(src_buf, dst_buf, sizeof(src_buf)), 0) << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 356 | /* Copy the missing pieces. */ \ |
| 357 | env_->get_region_fn(a, 0, 1, dst_buf); \ |
| 358 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 359 | EXPECT_EQ(memcmp(src_buf, dst_buf, sizeof(src_buf)), 0) << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 360 | /* Copy back the whole array. */ \ |
| 361 | env_->get_region_fn(a, 0, size, dst_buf); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 362 | EXPECT_EQ(memcmp(src_buf, dst_buf, sizeof(src_buf)), 0) << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 363 | /* GetPrimitiveArrayCritical */ \ |
| 364 | void* v = env_->GetPrimitiveArrayCritical(a, NULL); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 365 | EXPECT_EQ(memcmp(src_buf, v, sizeof(src_buf)), 0) << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 366 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 367 | /* GetXArrayElements */ \ |
| 368 | scalar_type* xs = env_->get_elements_fn(a, NULL); \ |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 369 | EXPECT_EQ(memcmp(src_buf, xs, sizeof(src_buf)), 0) << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 370 | env_->release_elements_fn(a, xs, 0); \ |
| 371 | EXPECT_EQ(reinterpret_cast<uintptr_t>(v), reinterpret_cast<uintptr_t>(xs)) |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 372 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 373 | TEST_F(JniInternalTest, BooleanArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 374 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 375 | } |
| 376 | TEST_F(JniInternalTest, ByteArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 377 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 378 | } |
| 379 | TEST_F(JniInternalTest, CharArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 380 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 381 | } |
| 382 | TEST_F(JniInternalTest, DoubleArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 383 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 384 | } |
| 385 | TEST_F(JniInternalTest, FloatArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 386 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 387 | } |
| 388 | TEST_F(JniInternalTest, IntArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 389 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 390 | } |
| 391 | TEST_F(JniInternalTest, LongArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 392 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 393 | } |
| 394 | TEST_F(JniInternalTest, ShortArrays) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 395 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 398 | TEST_F(JniInternalTest, NewObjectArray) { |
| 399 | // TODO: death tests for negative array sizes. |
| 400 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 401 | // TODO: check non-NULL initial elements. |
| 402 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 403 | jclass element_class = env_->FindClass("java/lang/String"); |
| 404 | ASSERT_TRUE(element_class != NULL); |
| 405 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
| 406 | ASSERT_TRUE(array_class != NULL); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 407 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 408 | jobjectArray a; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 409 | |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 410 | a = env_->NewObjectArray(0, element_class, NULL); |
| 411 | EXPECT_TRUE(a != NULL); |
| 412 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 413 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 414 | |
| 415 | a = env_->NewObjectArray(1, element_class, NULL); |
| 416 | EXPECT_TRUE(a != NULL); |
| 417 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 418 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 419 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), NULL)); |
| 420 | |
| 421 | jstring s = env_->NewStringUTF("poop"); |
| 422 | a = env_->NewObjectArray(2, element_class, s); |
| 423 | EXPECT_TRUE(a != NULL); |
| 424 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 425 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 426 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 427 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | TEST_F(JniInternalTest, GetArrayLength) { |
| 431 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 434 | TEST_F(JniInternalTest, GetObjectClass) { |
| 435 | jclass string_class = env_->FindClass("java/lang/String"); |
| 436 | ASSERT_TRUE(string_class != NULL); |
| 437 | jclass class_class = env_->FindClass("java/lang/Class"); |
| 438 | ASSERT_TRUE(class_class != NULL); |
| 439 | |
| 440 | jstring s = env_->NewStringUTF("poop"); |
| 441 | jclass c = env_->GetObjectClass(s); |
| 442 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 443 | |
| 444 | jclass c2 = env_->GetObjectClass(c); |
| 445 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
| 446 | } |
| 447 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 448 | TEST_F(JniInternalTest, GetSuperclass) { |
| 449 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 450 | ASSERT_TRUE(object_class != NULL); |
| 451 | jclass string_class = env_->FindClass("java/lang/String"); |
| 452 | ASSERT_TRUE(string_class != NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 453 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
| 454 | ASSERT_TRUE(runnable_interface != NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 455 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
| 456 | ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 457 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 460 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 461 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 462 | ASSERT_TRUE(object_class != NULL); |
| 463 | jclass string_class = env_->FindClass("java/lang/String"); |
| 464 | ASSERT_TRUE(string_class != NULL); |
| 465 | |
| 466 | ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class)); |
| 467 | ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class)); |
| 468 | } |
| 469 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 470 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 471 | jclass local = env_->FindClass("java/lang/Object"); |
| 472 | ASSERT_TRUE(local != NULL); |
| 473 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 474 | |
| 475 | jobject global = env_->NewGlobalRef(local); |
| 476 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 477 | |
| 478 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 479 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 480 | |
| 481 | jobject invalid = reinterpret_cast<jobject>(this); |
| 482 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 483 | |
| 484 | // TODO: invoke a native method and test that its arguments are considered local references. |
| 485 | } |
| 486 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 487 | TEST_F(JniInternalTest, NewStringUTF) { |
| 488 | EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 489 | jstring s; |
| 490 | |
| 491 | s = env_->NewStringUTF(""); |
| 492 | EXPECT_TRUE(s != NULL); |
| 493 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 494 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 495 | s = env_->NewStringUTF("hello"); |
| 496 | EXPECT_TRUE(s != NULL); |
| 497 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 498 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 499 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 500 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 503 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 504 | jchar chars[] = { 'h', 'i' }; |
| 505 | jstring s; |
| 506 | s = env_->NewString(chars, 0); |
| 507 | EXPECT_TRUE(s != NULL); |
| 508 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 509 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 510 | s = env_->NewString(chars, 2); |
| 511 | EXPECT_TRUE(s != NULL); |
| 512 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 513 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 514 | |
| 515 | // TODO: check some non-ASCII strings. |
| 516 | } |
| 517 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 518 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
| 519 | jstring s = env_->NewString(NULL, 0); |
| 520 | EXPECT_TRUE(s != NULL); |
| 521 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 522 | } |
| 523 | |
Brian Carlstrom | 3625812 | 2011-12-09 12:55:51 -0800 | [diff] [blame] | 524 | // TODO: fix gtest death tests on host http://b/5690440 (and target) |
| 525 | TEST_F(JniInternalTest, DISABLED_NewStringNullCharsNonzeroLength) { |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 526 | ASSERT_DEATH(env_->NewString(NULL, 1), ""); |
| 527 | } |
| 528 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 529 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 530 | // Already tested in the NewString/NewStringUTF tests. |
| 531 | } |
| 532 | |
| 533 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 534 | jstring s = env_->NewStringUTF("hello"); |
| 535 | ASSERT_TRUE(s != NULL); |
| 536 | |
| 537 | env_->GetStringRegion(s, -1, 0, NULL); |
| 538 | EXPECT_EXCEPTION(sioobe_); |
| 539 | env_->GetStringRegion(s, 0, -1, NULL); |
| 540 | EXPECT_EXCEPTION(sioobe_); |
| 541 | env_->GetStringRegion(s, 0, 10, NULL); |
| 542 | EXPECT_EXCEPTION(sioobe_); |
| 543 | env_->GetStringRegion(s, 10, 1, NULL); |
| 544 | EXPECT_EXCEPTION(sioobe_); |
| 545 | |
| 546 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 547 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 548 | EXPECT_EQ('x', chars[0]); |
| 549 | EXPECT_EQ('e', chars[1]); |
| 550 | EXPECT_EQ('l', chars[2]); |
| 551 | EXPECT_EQ('x', chars[3]); |
| 552 | |
| 553 | env_->GetStringUTFRegion(s, -1, 0, NULL); |
| 554 | EXPECT_EXCEPTION(sioobe_); |
| 555 | env_->GetStringUTFRegion(s, 0, -1, NULL); |
| 556 | EXPECT_EXCEPTION(sioobe_); |
| 557 | env_->GetStringUTFRegion(s, 0, 10, NULL); |
| 558 | EXPECT_EXCEPTION(sioobe_); |
| 559 | env_->GetStringUTFRegion(s, 10, 1, NULL); |
| 560 | EXPECT_EXCEPTION(sioobe_); |
| 561 | |
| 562 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 563 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 564 | EXPECT_EQ('x', bytes[0]); |
| 565 | EXPECT_EQ('e', bytes[1]); |
| 566 | EXPECT_EQ('l', bytes[2]); |
| 567 | EXPECT_EQ('x', bytes[3]); |
| 568 | } |
| 569 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 570 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 571 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
| 572 | // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni. |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 573 | EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 574 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 575 | |
| 576 | jstring s = env_->NewStringUTF("hello"); |
| 577 | ASSERT_TRUE(s != NULL); |
| 578 | |
| 579 | const char* utf = env_->GetStringUTFChars(s, NULL); |
| 580 | EXPECT_STREQ("hello", utf); |
| 581 | env_->ReleaseStringUTFChars(s, utf); |
| 582 | |
| 583 | jboolean is_copy = JNI_FALSE; |
| 584 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 585 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 586 | EXPECT_STREQ("hello", utf); |
| 587 | env_->ReleaseStringUTFChars(s, utf); |
| 588 | } |
| 589 | |
| 590 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 591 | jstring s = env_->NewStringUTF("hello"); |
| 592 | ASSERT_TRUE(s != NULL); |
| 593 | |
| 594 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 595 | const jchar* chars = env_->GetStringChars(s, NULL); |
| 596 | EXPECT_EQ(expected[0], chars[0]); |
| 597 | EXPECT_EQ(expected[1], chars[1]); |
| 598 | EXPECT_EQ(expected[2], chars[2]); |
| 599 | EXPECT_EQ(expected[3], chars[3]); |
| 600 | EXPECT_EQ(expected[4], chars[4]); |
| 601 | env_->ReleaseStringChars(s, chars); |
| 602 | |
| 603 | jboolean is_copy = JNI_FALSE; |
| 604 | chars = env_->GetStringChars(s, &is_copy); |
| 605 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 606 | EXPECT_EQ(expected[0], chars[0]); |
| 607 | EXPECT_EQ(expected[1], chars[1]); |
| 608 | EXPECT_EQ(expected[2], chars[2]); |
| 609 | EXPECT_EQ(expected[3], chars[3]); |
| 610 | EXPECT_EQ(expected[4], chars[4]); |
| 611 | env_->ReleaseStringChars(s, chars); |
| 612 | } |
| 613 | |
| 614 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 615 | jstring s = env_->NewStringUTF("hello"); |
| 616 | ASSERT_TRUE(s != NULL); |
| 617 | |
| 618 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 619 | const jchar* chars = env_->GetStringCritical(s, NULL); |
| 620 | EXPECT_EQ(expected[0], chars[0]); |
| 621 | EXPECT_EQ(expected[1], chars[1]); |
| 622 | EXPECT_EQ(expected[2], chars[2]); |
| 623 | EXPECT_EQ(expected[3], chars[3]); |
| 624 | EXPECT_EQ(expected[4], chars[4]); |
| 625 | env_->ReleaseStringCritical(s, chars); |
| 626 | |
| 627 | jboolean is_copy = JNI_FALSE; |
| 628 | chars = env_->GetStringCritical(s, &is_copy); |
| 629 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 630 | EXPECT_EQ(expected[0], chars[0]); |
| 631 | EXPECT_EQ(expected[1], chars[1]); |
| 632 | EXPECT_EQ(expected[2], chars[2]); |
| 633 | EXPECT_EQ(expected[3], chars[3]); |
| 634 | EXPECT_EQ(expected[4], chars[4]); |
| 635 | env_->ReleaseStringCritical(s, chars); |
| 636 | } |
| 637 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 638 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 639 | jclass c = env_->FindClass("java/lang/Object"); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 640 | ASSERT_TRUE(c != NULL); |
| 641 | |
| 642 | jobjectArray array = env_->NewObjectArray(1, c, NULL); |
| 643 | EXPECT_TRUE(array != NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 644 | EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 645 | env_->SetObjectArrayElement(array, 0, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 646 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), c)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 647 | |
| 648 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 649 | env_->SetObjectArrayElement(array, -1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 650 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 651 | |
| 652 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 653 | env_->SetObjectArrayElement(array, 1, c); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 654 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 655 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 656 | // TODO: check ArrayStoreException thrown for bad types. |
| 657 | } |
| 658 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 659 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 660 | do { \ |
| 661 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
| 662 | EXPECT_TRUE(fid != NULL); \ |
| 663 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 664 | EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 665 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 666 | EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 667 | } while (false) |
| 668 | |
| 669 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 670 | do { \ |
| 671 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
| 672 | EXPECT_TRUE(fid != NULL); \ |
| 673 | env_->Set ## type ## Field(instance, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 674 | EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 675 | env_->Set ## type ## Field(instance, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 676 | EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 677 | } while (false) |
| 678 | |
| 679 | |
| 680 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 681 | SirtRef<ClassLoader> class_loader(LoadDex("AllFields")); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 682 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 683 | |
| 684 | jclass c = env_->FindClass("AllFields"); |
| 685 | ASSERT_TRUE(c != NULL); |
| 686 | jobject o = env_->AllocObject(c); |
| 687 | ASSERT_TRUE(o != NULL); |
| 688 | |
| 689 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false); |
| 690 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 691 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 692 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 693 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 694 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 695 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 696 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 697 | |
| 698 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false); |
| 699 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 700 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 701 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 702 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 703 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 704 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 705 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 706 | } |
| 707 | |
| 708 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 709 | SirtRef<ClassLoader> class_loader(LoadDex("AllFields")); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 710 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 711 | |
| 712 | jclass c = env_->FindClass("AllFields"); |
| 713 | ASSERT_TRUE(c != NULL); |
| 714 | jobject o = env_->AllocObject(c); |
| 715 | ASSERT_TRUE(o != NULL); |
| 716 | |
| 717 | jstring s1 = env_->NewStringUTF("hello"); |
| 718 | ASSERT_TRUE(s1 != NULL); |
| 719 | jstring s2 = env_->NewStringUTF("world"); |
| 720 | ASSERT_TRUE(s2 != NULL); |
| 721 | |
| 722 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
| 723 | ASSERT_TRUE(s_fid != NULL); |
| 724 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
| 725 | ASSERT_TRUE(i_fid != NULL); |
| 726 | |
| 727 | env_->SetStaticObjectField(c, s_fid, s1); |
| 728 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 729 | env_->SetStaticObjectField(c, s_fid, s2); |
| 730 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 731 | |
| 732 | env_->SetObjectField(o, i_fid, s1); |
| 733 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 734 | env_->SetObjectField(o, i_fid, s2); |
| 735 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 736 | } |
| 737 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 738 | TEST_F(JniInternalTest, NewLocalRef_NULL) { |
| 739 | EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL); |
| 740 | } |
| 741 | |
| 742 | TEST_F(JniInternalTest, NewLocalRef) { |
| 743 | jstring s = env_->NewStringUTF(""); |
| 744 | ASSERT_TRUE(s != NULL); |
| 745 | jobject o = env_->NewLocalRef(s); |
| 746 | EXPECT_TRUE(o != NULL); |
| 747 | EXPECT_TRUE(o != s); |
| 748 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 749 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | TEST_F(JniInternalTest, DeleteLocalRef_NULL) { |
| 753 | env_->DeleteLocalRef(NULL); |
| 754 | } |
| 755 | |
| 756 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 757 | jstring s = env_->NewStringUTF(""); |
| 758 | ASSERT_TRUE(s != NULL); |
| 759 | env_->DeleteLocalRef(s); |
| 760 | |
| 761 | // Currently, deleting an already-deleted reference is just a warning. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 762 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 763 | env_->DeleteLocalRef(s); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 764 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 765 | |
| 766 | s = env_->NewStringUTF(""); |
| 767 | ASSERT_TRUE(s != NULL); |
| 768 | jobject o = env_->NewLocalRef(s); |
| 769 | ASSERT_TRUE(o != NULL); |
| 770 | |
| 771 | env_->DeleteLocalRef(s); |
| 772 | env_->DeleteLocalRef(o); |
| 773 | } |
| 774 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 775 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 776 | jobject original = env_->NewStringUTF(""); |
| 777 | ASSERT_TRUE(original != NULL); |
| 778 | |
| 779 | jobject outer; |
| 780 | jobject inner1, inner2; |
| 781 | Object* inner2_direct_pointer; |
| 782 | { |
| 783 | env_->PushLocalFrame(4); |
| 784 | outer = env_->NewLocalRef(original); |
| 785 | |
| 786 | { |
| 787 | env_->PushLocalFrame(4); |
| 788 | inner1 = env_->NewLocalRef(outer); |
| 789 | inner2 = env_->NewStringUTF("survivor"); |
| 790 | inner2_direct_pointer = Decode<Object*>(env_, inner2); |
| 791 | env_->PopLocalFrame(inner2); |
| 792 | } |
| 793 | |
| 794 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 795 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
| 796 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 797 | |
| 798 | // Our local reference for the survivor is invalid because the survivor |
| 799 | // gets a new local reference... |
| 800 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 801 | // ...but the survivor should be in the local reference table. |
| 802 | EXPECT_TRUE(env_->locals.ContainsDirectPointer(inner2_direct_pointer)); |
| 803 | |
| 804 | env_->PopLocalFrame(NULL); |
| 805 | } |
| 806 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 807 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
| 808 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 809 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 810 | } |
| 811 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 812 | TEST_F(JniInternalTest, NewGlobalRef_NULL) { |
| 813 | EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL); |
| 814 | } |
| 815 | |
| 816 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 817 | jstring s = env_->NewStringUTF(""); |
| 818 | ASSERT_TRUE(s != NULL); |
| 819 | jobject o = env_->NewGlobalRef(s); |
| 820 | EXPECT_TRUE(o != NULL); |
| 821 | EXPECT_TRUE(o != s); |
| 822 | |
| 823 | // TODO: check that o is a global reference. |
| 824 | } |
| 825 | |
| 826 | TEST_F(JniInternalTest, DeleteGlobalRef_NULL) { |
| 827 | env_->DeleteGlobalRef(NULL); |
| 828 | } |
| 829 | |
| 830 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 831 | jstring s = env_->NewStringUTF(""); |
| 832 | ASSERT_TRUE(s != NULL); |
| 833 | |
| 834 | jobject o = env_->NewGlobalRef(s); |
| 835 | ASSERT_TRUE(o != NULL); |
| 836 | env_->DeleteGlobalRef(o); |
| 837 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 838 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 839 | // Currently, deleting an already-deleted reference is just a warning. |
| 840 | env_->DeleteGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 841 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 842 | |
| 843 | jobject o1 = env_->NewGlobalRef(s); |
| 844 | ASSERT_TRUE(o1 != NULL); |
| 845 | jobject o2 = env_->NewGlobalRef(s); |
| 846 | ASSERT_TRUE(o2 != NULL); |
| 847 | |
| 848 | env_->DeleteGlobalRef(o1); |
| 849 | env_->DeleteGlobalRef(o2); |
| 850 | } |
| 851 | |
| 852 | TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) { |
| 853 | EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL); |
| 854 | } |
| 855 | |
| 856 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 857 | jstring s = env_->NewStringUTF(""); |
| 858 | ASSERT_TRUE(s != NULL); |
| 859 | jobject o = env_->NewWeakGlobalRef(s); |
| 860 | EXPECT_TRUE(o != NULL); |
| 861 | EXPECT_TRUE(o != s); |
| 862 | |
| 863 | // TODO: check that o is a weak global reference. |
| 864 | } |
| 865 | |
| 866 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) { |
| 867 | env_->DeleteWeakGlobalRef(NULL); |
| 868 | } |
| 869 | |
| 870 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 871 | jstring s = env_->NewStringUTF(""); |
| 872 | ASSERT_TRUE(s != NULL); |
| 873 | |
| 874 | jobject o = env_->NewWeakGlobalRef(s); |
| 875 | ASSERT_TRUE(o != NULL); |
| 876 | env_->DeleteWeakGlobalRef(o); |
| 877 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 878 | vm_->check_jni_abort_hook = TestCheckJniAbortHook; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 879 | // Currently, deleting an already-deleted reference is just a warning. |
| 880 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 881 | vm_->check_jni_abort_hook = NULL; |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 882 | |
| 883 | jobject o1 = env_->NewWeakGlobalRef(s); |
| 884 | ASSERT_TRUE(o1 != NULL); |
| 885 | jobject o2 = env_->NewWeakGlobalRef(s); |
| 886 | ASSERT_TRUE(o2 != NULL); |
| 887 | |
| 888 | env_->DeleteWeakGlobalRef(o1); |
| 889 | env_->DeleteWeakGlobalRef(o2); |
| 890 | } |
| 891 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 892 | #if defined(__arm__) |
| 893 | TEST_F(JniInternalTest, StaticMainMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 894 | SirtRef<ClassLoader> class_loader(LoadDex("Main")); |
| 895 | CompileDirectMethod(class_loader.get(), "Main", "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 896 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 897 | Class* klass = class_linker_->FindClass("LMain;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 898 | ASSERT_TRUE(klass != NULL); |
| 899 | |
| 900 | Method* method = klass->FindDirectMethod("main", "([Ljava/lang/String;)V"); |
| 901 | ASSERT_TRUE(method != NULL); |
| 902 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 903 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 904 | |
| 905 | Object* arg = NULL; |
| 906 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 907 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | TEST_F(JniInternalTest, StaticNopMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 911 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 912 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "nop", "()V"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 913 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 914 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 915 | ASSERT_TRUE(klass != NULL); |
| 916 | |
| 917 | Method* method = klass->FindDirectMethod("nop", "()V"); |
| 918 | ASSERT_TRUE(method != NULL); |
| 919 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 920 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 921 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 922 | (*stub)(method, NULL, Thread::Current(), NULL, NULL); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | TEST_F(JniInternalTest, StaticIdentityByteMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 926 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 927 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "identity", "(B)B"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 928 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 929 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 930 | ASSERT_TRUE(klass != NULL); |
| 931 | |
| 932 | Method* method = klass->FindDirectMethod("identity", "(B)B"); |
| 933 | ASSERT_TRUE(method != NULL); |
| 934 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 935 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 936 | |
| 937 | int arg; |
| 938 | JValue result; |
| 939 | |
| 940 | arg = 0; |
| 941 | result.b = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 942 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 943 | EXPECT_EQ(0, result.b); |
| 944 | |
| 945 | arg = -1; |
| 946 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 947 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 948 | EXPECT_EQ(-1, result.b); |
| 949 | |
| 950 | arg = SCHAR_MAX; |
| 951 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 952 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 953 | EXPECT_EQ(SCHAR_MAX, result.b); |
| 954 | |
| 955 | arg = SCHAR_MIN; |
| 956 | result.b = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 957 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 958 | EXPECT_EQ(SCHAR_MIN, result.b); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | TEST_F(JniInternalTest, StaticIdentityIntMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 962 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 963 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "identity", "(I)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 964 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 965 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 966 | ASSERT_TRUE(klass != NULL); |
| 967 | |
| 968 | Method* method = klass->FindDirectMethod("identity", "(I)I"); |
| 969 | ASSERT_TRUE(method != NULL); |
| 970 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 971 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 972 | |
| 973 | int arg; |
| 974 | JValue result; |
| 975 | |
| 976 | arg = 0; |
| 977 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 978 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 979 | EXPECT_EQ(0, result.i); |
| 980 | |
| 981 | arg = -1; |
| 982 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 983 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 984 | EXPECT_EQ(-1, result.i); |
| 985 | |
| 986 | arg = INT_MAX; |
| 987 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 988 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 989 | EXPECT_EQ(INT_MAX, result.i); |
| 990 | |
| 991 | arg = INT_MIN; |
| 992 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 993 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 994 | EXPECT_EQ(INT_MIN, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | TEST_F(JniInternalTest, StaticIdentityDoubleMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 998 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 999 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "identity", "(D)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1000 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1001 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1002 | ASSERT_TRUE(klass != NULL); |
| 1003 | |
| 1004 | Method* method = klass->FindDirectMethod("identity", "(D)D"); |
| 1005 | ASSERT_TRUE(method != NULL); |
| 1006 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1007 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1008 | |
| 1009 | double arg; |
| 1010 | JValue result; |
| 1011 | |
| 1012 | arg = 0.0; |
| 1013 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1014 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1015 | EXPECT_EQ(0.0, result.d); |
| 1016 | |
| 1017 | arg = -1.0; |
| 1018 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1019 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1020 | EXPECT_EQ(-1.0, result.d); |
| 1021 | |
| 1022 | arg = DBL_MAX; |
| 1023 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1024 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1025 | EXPECT_EQ(DBL_MAX, result.d); |
| 1026 | |
| 1027 | arg = DBL_MIN; |
| 1028 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1029 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(&arg), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1030 | EXPECT_EQ(DBL_MIN, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | TEST_F(JniInternalTest, StaticSumIntIntMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1034 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1035 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(II)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1036 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1037 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1038 | ASSERT_TRUE(klass != NULL); |
| 1039 | |
| 1040 | Method* method = klass->FindDirectMethod("sum", "(II)I"); |
| 1041 | ASSERT_TRUE(method != NULL); |
| 1042 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1043 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1044 | |
| 1045 | int args[2]; |
| 1046 | JValue result; |
| 1047 | |
| 1048 | args[0] = 0; |
| 1049 | args[1] = 0; |
| 1050 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1051 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1052 | EXPECT_EQ(0, result.i); |
| 1053 | |
| 1054 | args[0] = 1; |
| 1055 | args[1] = 2; |
| 1056 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1057 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1058 | EXPECT_EQ(3, result.i); |
| 1059 | |
| 1060 | args[0] = -2; |
| 1061 | args[1] = 5; |
| 1062 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1063 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1064 | EXPECT_EQ(3, result.i); |
| 1065 | |
| 1066 | args[0] = INT_MAX; |
| 1067 | args[1] = INT_MIN; |
| 1068 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1069 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1070 | EXPECT_EQ(-1, result.i); |
| 1071 | |
| 1072 | args[0] = INT_MAX; |
| 1073 | args[1] = INT_MAX; |
| 1074 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1075 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1076 | EXPECT_EQ(-2, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | TEST_F(JniInternalTest, StaticSumIntIntIntMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1080 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1081 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(III)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1082 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1083 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1084 | ASSERT_TRUE(klass != NULL); |
| 1085 | |
| 1086 | Method* method = klass->FindDirectMethod("sum", "(III)I"); |
| 1087 | ASSERT_TRUE(method != NULL); |
| 1088 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1089 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1090 | |
| 1091 | int args[3]; |
| 1092 | JValue result; |
| 1093 | |
| 1094 | args[0] = 0; |
| 1095 | args[1] = 0; |
| 1096 | args[2] = 0; |
| 1097 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1098 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1099 | EXPECT_EQ(0, result.i); |
| 1100 | |
| 1101 | args[0] = 1; |
| 1102 | args[1] = 2; |
| 1103 | args[2] = 3; |
| 1104 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1105 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1106 | EXPECT_EQ(6, result.i); |
| 1107 | |
| 1108 | args[0] = -1; |
| 1109 | args[1] = 2; |
| 1110 | args[2] = -3; |
| 1111 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1112 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1113 | EXPECT_EQ(-2, result.i); |
| 1114 | |
| 1115 | args[0] = INT_MAX; |
| 1116 | args[1] = INT_MIN; |
| 1117 | args[2] = INT_MAX; |
| 1118 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1119 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1120 | EXPECT_EQ(2147483646, result.i); |
| 1121 | |
| 1122 | args[0] = INT_MAX; |
| 1123 | args[1] = INT_MAX; |
| 1124 | args[2] = INT_MAX; |
| 1125 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1126 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1127 | EXPECT_EQ(2147483645, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | TEST_F(JniInternalTest, StaticSumIntIntIntIntMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1131 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1132 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(IIII)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1133 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1134 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1135 | ASSERT_TRUE(klass != NULL); |
| 1136 | |
| 1137 | Method* method = klass->FindDirectMethod("sum", "(IIII)I"); |
| 1138 | ASSERT_TRUE(method != NULL); |
| 1139 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1140 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1141 | |
| 1142 | int args[4]; |
| 1143 | JValue result; |
| 1144 | |
| 1145 | args[0] = 0; |
| 1146 | args[1] = 0; |
| 1147 | args[2] = 0; |
| 1148 | args[3] = 0; |
| 1149 | result.i = -1; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1150 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1151 | EXPECT_EQ(0, result.i); |
| 1152 | |
| 1153 | args[0] = 1; |
| 1154 | args[1] = 2; |
| 1155 | args[2] = 3; |
| 1156 | args[3] = 4; |
| 1157 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1158 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1159 | EXPECT_EQ(10, result.i); |
| 1160 | |
| 1161 | args[0] = -1; |
| 1162 | args[1] = 2; |
| 1163 | args[2] = -3; |
| 1164 | args[3] = 4; |
| 1165 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1166 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1167 | EXPECT_EQ(2, result.i); |
| 1168 | |
| 1169 | args[0] = INT_MAX; |
| 1170 | args[1] = INT_MIN; |
| 1171 | args[2] = INT_MAX; |
| 1172 | args[3] = INT_MIN; |
| 1173 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1174 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1175 | EXPECT_EQ(-2, result.i); |
| 1176 | |
| 1177 | args[0] = INT_MAX; |
| 1178 | args[1] = INT_MAX; |
| 1179 | args[2] = INT_MAX; |
| 1180 | args[3] = INT_MAX; |
| 1181 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1182 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1183 | EXPECT_EQ(-4, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | TEST_F(JniInternalTest, StaticSumIntIntIntIntIntMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1187 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1188 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(IIIII)I"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1189 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1190 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1191 | ASSERT_TRUE(klass != NULL); |
| 1192 | |
| 1193 | Method* method = klass->FindDirectMethod("sum", "(IIIII)I"); |
| 1194 | ASSERT_TRUE(method != NULL); |
| 1195 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1196 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1197 | |
| 1198 | int args[5]; |
| 1199 | JValue result; |
| 1200 | |
| 1201 | args[0] = 0; |
| 1202 | args[1] = 0; |
| 1203 | args[2] = 0; |
| 1204 | args[3] = 0; |
| 1205 | args[4] = 0; |
| 1206 | result.i = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1207 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1208 | EXPECT_EQ(0, result.i); |
| 1209 | |
| 1210 | args[0] = 1; |
| 1211 | args[1] = 2; |
| 1212 | args[2] = 3; |
| 1213 | args[3] = 4; |
| 1214 | args[4] = 5; |
| 1215 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1216 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1217 | EXPECT_EQ(15, result.i); |
| 1218 | |
| 1219 | args[0] = -1; |
| 1220 | args[1] = 2; |
| 1221 | args[2] = -3; |
| 1222 | args[3] = 4; |
| 1223 | args[4] = -5; |
| 1224 | result.i = 0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1225 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1226 | EXPECT_EQ(-3, result.i); |
| 1227 | |
| 1228 | args[0] = INT_MAX; |
| 1229 | args[1] = INT_MIN; |
| 1230 | args[2] = INT_MAX; |
| 1231 | args[3] = INT_MIN; |
| 1232 | args[4] = INT_MAX; |
| 1233 | result.i = 1234; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1234 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1235 | EXPECT_EQ(2147483645, result.i); |
| 1236 | |
| 1237 | args[0] = INT_MAX; |
| 1238 | args[1] = INT_MAX; |
| 1239 | args[2] = INT_MAX; |
| 1240 | args[3] = INT_MAX; |
| 1241 | args[4] = INT_MAX; |
| 1242 | result.i = INT_MIN; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1243 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1244 | EXPECT_EQ(2147483643, result.i); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | TEST_F(JniInternalTest, StaticSumDoubleDoubleMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1248 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1249 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(DD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1250 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1251 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1252 | ASSERT_TRUE(klass != NULL); |
| 1253 | |
| 1254 | Method* method = klass->FindDirectMethod("sum", "(DD)D"); |
| 1255 | ASSERT_TRUE(method != NULL); |
| 1256 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1257 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1258 | |
| 1259 | double args[2]; |
| 1260 | JValue result; |
| 1261 | |
| 1262 | args[0] = 0.0; |
| 1263 | args[1] = 0.0; |
| 1264 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1265 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1266 | EXPECT_EQ(0.0, result.d); |
| 1267 | |
| 1268 | args[0] = 1.0; |
| 1269 | args[1] = 2.0; |
| 1270 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1271 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1272 | EXPECT_EQ(3.0, result.d); |
| 1273 | |
| 1274 | args[0] = 1.0; |
| 1275 | args[1] = -2.0; |
| 1276 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1277 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1278 | EXPECT_EQ(-1.0, result.d); |
| 1279 | |
| 1280 | args[0] = DBL_MAX; |
| 1281 | args[1] = DBL_MIN; |
| 1282 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1283 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1284 | EXPECT_EQ(1.7976931348623157e308, result.d); |
| 1285 | |
| 1286 | args[0] = DBL_MAX; |
| 1287 | args[1] = DBL_MAX; |
| 1288 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1289 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1290 | EXPECT_EQ(INFINITY, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1294 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1295 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(DDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1296 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1297 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1298 | ASSERT_TRUE(klass != NULL); |
| 1299 | |
| 1300 | Method* method = klass->FindDirectMethod("sum", "(DDD)D"); |
| 1301 | ASSERT_TRUE(method != NULL); |
| 1302 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1303 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1304 | |
| 1305 | double args[3]; |
| 1306 | JValue result; |
| 1307 | |
| 1308 | args[0] = 0.0; |
| 1309 | args[1] = 0.0; |
| 1310 | args[2] = 0.0; |
| 1311 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1312 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1313 | EXPECT_EQ(0.0, result.d); |
| 1314 | |
| 1315 | args[0] = 1.0; |
| 1316 | args[1] = 2.0; |
| 1317 | args[2] = 3.0; |
| 1318 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1319 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1320 | EXPECT_EQ(6.0, result.d); |
| 1321 | |
| 1322 | args[0] = 1.0; |
| 1323 | args[1] = -2.0; |
| 1324 | args[2] = 3.0; |
| 1325 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1326 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1327 | EXPECT_EQ(2.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1331 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1332 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(DDDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1333 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1334 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1335 | ASSERT_TRUE(klass != NULL); |
| 1336 | |
| 1337 | Method* method = klass->FindDirectMethod("sum", "(DDDD)D"); |
| 1338 | ASSERT_TRUE(method != NULL); |
| 1339 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1340 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1341 | |
| 1342 | double args[4]; |
| 1343 | JValue result; |
| 1344 | |
| 1345 | args[0] = 0.0; |
| 1346 | args[1] = 0.0; |
| 1347 | args[2] = 0.0; |
| 1348 | args[3] = 0.0; |
| 1349 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1350 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1351 | EXPECT_EQ(0.0, result.d); |
| 1352 | |
| 1353 | args[0] = 1.0; |
| 1354 | args[1] = 2.0; |
| 1355 | args[2] = 3.0; |
| 1356 | args[3] = 4.0; |
| 1357 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1358 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1359 | EXPECT_EQ(10.0, result.d); |
| 1360 | |
| 1361 | args[0] = 1.0; |
| 1362 | args[1] = -2.0; |
| 1363 | args[2] = 3.0; |
| 1364 | args[3] = -4.0; |
| 1365 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1366 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1367 | EXPECT_EQ(-2.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | TEST_F(JniInternalTest, StaticSumDoubleDoubleDoubleDoubleDoubleMethod) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1371 | SirtRef<ClassLoader> class_loader(LoadDex("StaticLeafMethods")); |
| 1372 | CompileDirectMethod(class_loader.get(), "StaticLeafMethods", "sum", "(DDDDD)D"); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1373 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1374 | Class* klass = class_linker_->FindClass("LStaticLeafMethods;", class_loader.get()); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1375 | ASSERT_TRUE(klass != NULL); |
| 1376 | |
| 1377 | Method* method = klass->FindDirectMethod("sum", "(DDDDD)D"); |
| 1378 | ASSERT_TRUE(method != NULL); |
| 1379 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 1380 | Method::InvokeStub* stub = method->GetInvokeStub(); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1381 | |
| 1382 | double args[5]; |
| 1383 | JValue result; |
| 1384 | |
| 1385 | args[0] = 0.0; |
| 1386 | args[1] = 0.0; |
| 1387 | args[2] = 0.0; |
| 1388 | args[3] = 0.0; |
| 1389 | args[4] = 0.0; |
| 1390 | result.d = -1.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1391 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1392 | EXPECT_EQ(0.0, result.d); |
| 1393 | |
| 1394 | args[0] = 1.0; |
| 1395 | args[1] = 2.0; |
| 1396 | args[2] = 3.0; |
| 1397 | args[3] = 4.0; |
| 1398 | args[4] = 5.0; |
| 1399 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1400 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1401 | EXPECT_EQ(15.0, result.d); |
| 1402 | |
| 1403 | args[0] = 1.0; |
| 1404 | args[1] = -2.0; |
| 1405 | args[2] = 3.0; |
| 1406 | args[3] = -4.0; |
| 1407 | args[4] = 5.0; |
| 1408 | result.d = 0.0; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1409 | (*stub)(method, NULL, Thread::Current(), reinterpret_cast<byte*>(args), &result); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1410 | EXPECT_EQ(3.0, result.d); |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 1411 | } |
| 1412 | #endif // __arm__ |
| 1413 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1414 | TEST_F(JniInternalTest, Throw) { |
| 1415 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1416 | |
| 1417 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1418 | ASSERT_TRUE(exception_class != NULL); |
| 1419 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
| 1420 | ASSERT_TRUE(exception != NULL); |
| 1421 | |
| 1422 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1423 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1424 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1425 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1426 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | TEST_F(JniInternalTest, ThrowNew) { |
| 1430 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1431 | |
| 1432 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1433 | ASSERT_TRUE(exception_class != NULL); |
| 1434 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1435 | jthrowable thrown_exception; |
| 1436 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1437 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1438 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1439 | thrown_exception = env_->ExceptionOccurred(); |
| 1440 | env_->ExceptionClear(); |
| 1441 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1442 | |
| 1443 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL)); |
| 1444 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1445 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1446 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1447 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1450 | // TODO: this test is DISABLED until we can actually run java.nio.Buffer's <init>. |
| 1451 | TEST_F(JniInternalTest, DISABLED_NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1452 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
| 1453 | ASSERT_TRUE(buffer_class != NULL); |
| 1454 | |
| 1455 | char bytes[1024]; |
| 1456 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
| 1457 | ASSERT_TRUE(buffer != NULL); |
| 1458 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
| 1459 | ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes); |
| 1460 | ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes)); |
| 1461 | } |
| 1462 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1463 | } // namespace art |