summaryrefslogtreecommitdiffstats
path: root/base/android/scoped_java_ref_unittest.cc
diff options
context:
space:
mode:
authorpeter@chromium.org <peter@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-09 21:02:27 +0000
committerpeter@chromium.org <peter@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-09 21:02:27 +0000
commitfe0f1aba6584072a969924656771becb71589fec (patch)
treef74019055cf4b2193f4b05e247b0e42c3d676c0b /base/android/scoped_java_ref_unittest.cc
parent1153596a4543e779244f410b2ecf804d9c8c54e9 (diff)
downloadchromium_src-fe0f1aba6584072a969924656771becb71589fec.zip
chromium_src-fe0f1aba6584072a969924656771becb71589fec.tar.gz
chromium_src-fe0f1aba6584072a969924656771becb71589fec.tar.bz2
Upstream Android JNI code, allowing us to use more ScopedJava references.
BUG= TEST= Review URL: http://codereview.chromium.org/9358028 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@121284 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/android/scoped_java_ref_unittest.cc')
-rw-r--r--base/android/scoped_java_ref_unittest.cc53
1 files changed, 29 insertions, 24 deletions
diff --git a/base/android/scoped_java_ref_unittest.cc b/base/android/scoped_java_ref_unittest.cc
index a6dfc7a..f8bc675 100644
--- a/base/android/scoped_java_ref_unittest.cc
+++ b/base/android/scoped_java_ref_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -15,24 +15,26 @@ namespace {
int g_local_refs = 0;
int g_global_refs = 0;
+JNINativeInterface g_previous_functions = {0};
+
jobject NewGlobalRef(JNIEnv* env, jobject obj) {
++g_global_refs;
- return AttachCurrentThread()->NewGlobalRef(obj);
+ return g_previous_functions.NewGlobalRef(env, obj);
}
void DeleteGlobalRef(JNIEnv* env, jobject obj) {
--g_global_refs;
- return AttachCurrentThread()->DeleteGlobalRef(obj);
+ return g_previous_functions.DeleteGlobalRef(env, obj);
}
jobject NewLocalRef(JNIEnv* env, jobject obj) {
++g_local_refs;
- return AttachCurrentThread()->NewLocalRef(obj);
+ return g_previous_functions.NewLocalRef(env, obj);
}
void DeleteLocalRef(JNIEnv* env, jobject obj) {
--g_local_refs;
- return AttachCurrentThread()->DeleteLocalRef(obj);
+ return g_previous_functions.DeleteLocalRef(env, obj);
}
} // namespace
@@ -42,27 +44,27 @@ class ScopedJavaRefTest : public testing::Test {
g_local_refs = 0;
g_global_refs = 0;
JNIEnv* env = AttachCurrentThread();
- counting_env = *env;
- counting_functions = *counting_env.functions;
- counting_functions.NewGlobalRef = &NewGlobalRef;
- counting_functions.DeleteGlobalRef = &DeleteGlobalRef;
- counting_functions.NewLocalRef = &NewLocalRef;
- counting_functions.DeleteLocalRef = &DeleteLocalRef;
- counting_env.functions = &counting_functions;
+ g_previous_functions = *env->functions;
+ // We inject our own functions in JNINativeInterface so we can keep track
+ // of the reference counting ourselves.
+ JNINativeInterface* native_interface =
+ const_cast<JNINativeInterface*>(env->functions);
+ native_interface->NewGlobalRef = &NewGlobalRef;
+ native_interface->DeleteGlobalRef = &DeleteGlobalRef;
+ native_interface->NewLocalRef = &NewLocalRef;
+ native_interface->DeleteLocalRef = &DeleteLocalRef;
}
- // Special JNI env configured in SetUp to count in and out all local & global
- // reference instances. Be careful to only use this with the ScopedJavaRef
- // classes under test, else it's easy to get system references counted in
- // here too.
- JNIEnv counting_env;
- JNINativeInterface counting_functions;
+ virtual void TearDown() {
+ JNIEnv* env = AttachCurrentThread();
+ *(const_cast<JNINativeInterface*>(env->functions)) = g_previous_functions;
+ }
};
// The main purpose of this is testing the various conversions compile.
TEST_F(ScopedJavaRefTest, Conversions) {
JNIEnv* env = AttachCurrentThread();
- ScopedJavaLocalRef<jstring> str(env, ConvertUTF8ToJavaString(env, "string"));
+ ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, "string");
ScopedJavaGlobalRef<jstring> global(str);
{
ScopedJavaGlobalRef<jobject> global_obj(str);
@@ -74,24 +76,27 @@ TEST_F(ScopedJavaRefTest, Conversions) {
}
global.Reset(str);
const JavaRef<jstring>& str_ref = str;
- EXPECT_EQ("string", ConvertJavaStringToUTF8(env, str_ref.obj()));
+ EXPECT_EQ("string", ConvertJavaStringToUTF8(str_ref));
str.Reset();
}
TEST_F(ScopedJavaRefTest, RefCounts) {
+ JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> str;
- str.Reset(&counting_env, ConvertUTF8ToJavaString(AttachCurrentThread(),
- "string"));
+ // The ConvertJavaStringToUTF8 below creates a new string that would normally
+ // return a local ref. We simulate that by starting the g_local_refs count at
+ // 1.
+ g_local_refs = 1;
+ str.Reset(ConvertUTF8ToJavaString(env, "string"));
EXPECT_EQ(1, g_local_refs);
EXPECT_EQ(0, g_global_refs);
-
{
ScopedJavaGlobalRef<jstring> global_str(str);
ScopedJavaGlobalRef<jobject> global_obj(global_str);
EXPECT_EQ(1, g_local_refs);
EXPECT_EQ(2, g_global_refs);
- ScopedJavaLocalRef<jstring> str2(&counting_env, str.Release());
+ ScopedJavaLocalRef<jstring> str2(env, str.Release());
EXPECT_EQ(1, g_local_refs);
{
ScopedJavaLocalRef<jstring> str3(str2);