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 |
(...skipping 22 matching lines...) Expand all Loading... |
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 void SetUp() override { |
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; | 48 hooked_functions = *g_previous_functions; |
49 env->functions = &hooked_functions; | 49 env->functions = &hooked_functions; |
50 // 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 |
51 // of the reference counting ourselves. | 51 // of the reference counting ourselves. |
52 hooked_functions.NewGlobalRef = &NewGlobalRef; | 52 hooked_functions.NewGlobalRef = &NewGlobalRef; |
53 hooked_functions.DeleteGlobalRef = &DeleteGlobalRef; | 53 hooked_functions.DeleteGlobalRef = &DeleteGlobalRef; |
54 hooked_functions.NewLocalRef = &NewLocalRef; | 54 hooked_functions.NewLocalRef = &NewLocalRef; |
55 hooked_functions.DeleteLocalRef = &DeleteLocalRef; | 55 hooked_functions.DeleteLocalRef = &DeleteLocalRef; |
56 } | 56 } |
57 | 57 |
58 virtual void TearDown() { | 58 void TearDown() override { |
59 JNIEnv* env = AttachCurrentThread(); | 59 JNIEnv* env = AttachCurrentThread(); |
60 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 | 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. | 63 // read-only, so we deep copy it to allow individual functions to be hooked. |
64 JNINativeInterface hooked_functions; | 64 JNINativeInterface hooked_functions; |
65 }; | 65 }; |
66 | 66 |
67 // The main purpose of this is testing the various conversions compile. | 67 // The main purpose of this is testing the various conversions compile. |
68 TEST_F(ScopedJavaRefTest, Conversions) { | 68 TEST_F(ScopedJavaRefTest, Conversions) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 ScopedJavaGlobalRef<jobject> global_obj2(global_obj); | 113 ScopedJavaGlobalRef<jobject> global_obj2(global_obj); |
114 EXPECT_EQ(2, g_global_refs); | 114 EXPECT_EQ(2, g_global_refs); |
115 } | 115 } |
116 | 116 |
117 EXPECT_EQ(0, g_local_refs); | 117 EXPECT_EQ(0, g_local_refs); |
118 EXPECT_EQ(0, g_global_refs); | 118 EXPECT_EQ(0, g_global_refs); |
119 } | 119 } |
120 | 120 |
121 } // namespace android | 121 } // namespace android |
122 } // namespace base | 122 } // namespace base |
OLD | NEW |