| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/android/scoped_java_ref.h" | 5 #include "base/android/scoped_java_ref.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 namespace android { | 12 namespace android { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 int g_local_refs = 0; | 15 int g_local_refs = 0; |
| 16 int g_global_refs = 0; | 16 int g_global_refs = 0; |
| 17 | 17 |
| 18 JNINativeInterface g_previous_functions = {0}; | 18 const JNINativeInterface* g_previous_functions; |
| 19 | 19 |
| 20 jobject NewGlobalRef(JNIEnv* env, jobject obj) { | 20 jobject NewGlobalRef(JNIEnv* env, jobject obj) { |
| 21 ++g_global_refs; | 21 ++g_global_refs; |
| 22 return g_previous_functions.NewGlobalRef(env, obj); | 22 return g_previous_functions->NewGlobalRef(env, obj); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void DeleteGlobalRef(JNIEnv* env, jobject obj) { | 25 void DeleteGlobalRef(JNIEnv* env, jobject obj) { |
| 26 --g_global_refs; | 26 --g_global_refs; |
| 27 return g_previous_functions.DeleteGlobalRef(env, obj); | 27 return g_previous_functions->DeleteGlobalRef(env, obj); |
| 28 } | 28 } |
| 29 | 29 |
| 30 jobject NewLocalRef(JNIEnv* env, jobject obj) { | 30 jobject NewLocalRef(JNIEnv* env, jobject obj) { |
| 31 ++g_local_refs; | 31 ++g_local_refs; |
| 32 return g_previous_functions.NewLocalRef(env, obj); | 32 return g_previous_functions->NewLocalRef(env, obj); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void DeleteLocalRef(JNIEnv* env, jobject obj) { | 35 void DeleteLocalRef(JNIEnv* env, jobject obj) { |
| 36 --g_local_refs; | 36 --g_local_refs; |
| 37 return g_previous_functions.DeleteLocalRef(env, obj); | 37 return g_previous_functions->DeleteLocalRef(env, obj); |
| 38 } | 38 } |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 class ScopedJavaRefTest : public testing::Test { | 41 class ScopedJavaRefTest : public testing::Test { |
| 42 protected: | 42 protected: |
| 43 virtual void SetUp() { | 43 virtual void SetUp() { |
| 44 g_local_refs = 0; | 44 g_local_refs = 0; |
| 45 g_global_refs = 0; | 45 g_global_refs = 0; |
| 46 JNIEnv* env = AttachCurrentThread(); | 46 JNIEnv* env = AttachCurrentThread(); |
| 47 g_previous_functions = *env->functions; | 47 g_previous_functions = env->functions; |
| 48 hooked_functions = *g_previous_functions; |
| 49 env->functions = &hooked_functions; |
| 48 // We inject our own functions in JNINativeInterface so we can keep track | 50 // We inject our own functions in JNINativeInterface so we can keep track |
| 49 // of the reference counting ourselves. | 51 // of the reference counting ourselves. |
| 50 JNINativeInterface* native_interface = | 52 hooked_functions.NewGlobalRef = &NewGlobalRef; |
| 51 const_cast<JNINativeInterface*>(env->functions); | 53 hooked_functions.DeleteGlobalRef = &DeleteGlobalRef; |
| 52 native_interface->NewGlobalRef = &NewGlobalRef; | 54 hooked_functions.NewLocalRef = &NewLocalRef; |
| 53 native_interface->DeleteGlobalRef = &DeleteGlobalRef; | 55 hooked_functions.DeleteLocalRef = &DeleteLocalRef; |
| 54 native_interface->NewLocalRef = &NewLocalRef; | |
| 55 native_interface->DeleteLocalRef = &DeleteLocalRef; | |
| 56 } | 56 } |
| 57 | 57 |
| 58 virtual void TearDown() { | 58 virtual void TearDown() { |
| 59 JNIEnv* env = AttachCurrentThread(); | 59 JNIEnv* env = AttachCurrentThread(); |
| 60 *(const_cast<JNINativeInterface*>(env->functions)) = g_previous_functions; | 60 env->functions = g_previous_functions; |
| 61 } | 61 } |
| 62 // From JellyBean release, the instance of this struct provided in JNIEnv is |
| 63 // read-only, so we deep copy it to allow individual functions to be hooked. |
| 64 JNINativeInterface hooked_functions; |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 // The main purpose of this is testing the various conversions compile. | 67 // The main purpose of this is testing the various conversions compile. |
| 65 TEST_F(ScopedJavaRefTest, Conversions) { | 68 TEST_F(ScopedJavaRefTest, Conversions) { |
| 66 JNIEnv* env = AttachCurrentThread(); | 69 JNIEnv* env = AttachCurrentThread(); |
| 67 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, "string"); | 70 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, "string"); |
| 68 ScopedJavaGlobalRef<jstring> global(str); | 71 ScopedJavaGlobalRef<jstring> global(str); |
| 69 { | 72 { |
| 70 ScopedJavaGlobalRef<jobject> global_obj(str); | 73 ScopedJavaGlobalRef<jobject> global_obj(str); |
| 71 ScopedJavaLocalRef<jobject> local_obj(global); | 74 ScopedJavaLocalRef<jobject> local_obj(global); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 ScopedJavaGlobalRef<jobject> global_obj2(global_obj); | 113 ScopedJavaGlobalRef<jobject> global_obj2(global_obj); |
| 111 EXPECT_EQ(2, g_global_refs); | 114 EXPECT_EQ(2, g_global_refs); |
| 112 } | 115 } |
| 113 | 116 |
| 114 EXPECT_EQ(0, g_local_refs); | 117 EXPECT_EQ(0, g_local_refs); |
| 115 EXPECT_EQ(0, g_global_refs); | 118 EXPECT_EQ(0, g_global_refs); |
| 116 } | 119 } |
| 117 | 120 |
| 118 } // namespace android | 121 } // namespace android |
| 119 } // namespace base | 122 } // namespace base |
| OLD | NEW |