OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/jni_android.h" | 5 #include "base/android/jni_android.h" |
6 | 6 |
| 7 #include "base/at_exit.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
8 | 9 |
9 namespace base { | 10 namespace base { |
10 namespace android { | 11 namespace android { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 const char kJavaLangObject[] = "java/lang/Object"; | 15 const char kJavaLangObject[] = "java/lang/Object"; |
15 const char kGetClass[] = "getClass"; | 16 const char kGetClass[] = "getClass"; |
16 const char kToString[] = "toString"; | 17 const char kToString[] = "toString"; |
17 const char kReturningJavaLangClass[] = "()Ljava/lang/Class;"; | 18 const char kReturningJavaLangClass[] = "()Ljava/lang/Class;"; |
18 const char kReturningJavaLangString[] = "()Ljava/lang/String;"; | 19 const char kReturningJavaLangString[] = "()Ljava/lang/String;"; |
19 | 20 |
20 const char* g_last_method; | 21 const char* g_last_method; |
21 const char* g_last_jni_signature; | 22 const char* g_last_jni_signature; |
22 jmethodID g_last_method_id; | 23 jmethodID g_last_method_id; |
23 | 24 |
24 JNINativeInterface g_previous_functions = {0}; | 25 const JNINativeInterface* g_previous_functions; |
25 | 26 |
26 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method, | 27 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method, |
27 const char* jni_signature) { | 28 const char* jni_signature) { |
28 g_last_method = method; | 29 g_last_method = method; |
29 g_last_jni_signature = jni_signature; | 30 g_last_jni_signature = jni_signature; |
30 g_last_method_id = g_previous_functions.GetMethodID(env, clazz, method, | 31 g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method, |
31 jni_signature); | 32 jni_signature); |
32 return g_last_method_id; | 33 return g_last_method_id; |
33 } | 34 } |
34 | 35 |
35 } // namespace | 36 } // namespace |
36 | 37 |
37 class JNIAndroidTest : public testing::Test { | 38 class JNIAndroidTest : public testing::Test { |
38 protected: | 39 protected: |
39 virtual void SetUp() { | 40 virtual void SetUp() { |
40 JNIEnv* env = AttachCurrentThread(); | 41 JNIEnv* env = AttachCurrentThread(); |
41 g_previous_functions = *env->functions; | 42 g_previous_functions = env->functions; |
42 JNINativeInterface* native_interface = | 43 hooked_functions = *g_previous_functions; |
43 const_cast<JNINativeInterface*>(env->functions); | 44 env->functions = &hooked_functions; |
44 native_interface->GetMethodID = &GetMethodIDWrapper; | 45 hooked_functions.GetMethodID = &GetMethodIDWrapper; |
45 } | 46 } |
46 | 47 |
47 virtual void TearDown() { | 48 virtual void TearDown() { |
48 JNIEnv* env = AttachCurrentThread(); | 49 JNIEnv* env = AttachCurrentThread(); |
49 *(const_cast<JNINativeInterface*>(env->functions)) = g_previous_functions; | 50 env->functions = g_previous_functions; |
| 51 Reset(); |
50 } | 52 } |
51 | 53 |
52 void Reset() { | 54 void Reset() { |
53 g_last_method = 0; | 55 g_last_method = 0; |
54 g_last_jni_signature = 0; | 56 g_last_jni_signature = 0; |
55 g_last_method_id = NULL; | 57 g_last_method_id = NULL; |
56 } | 58 } |
| 59 // Needed to cleanup the cached method map in the implementation between |
| 60 // runs (e.g. if using --gtest_repeat) |
| 61 base::ShadowingAtExitManager exit_manager; |
| 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; |
57 }; | 65 }; |
58 | 66 |
59 TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) { | 67 TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) { |
60 JNIEnv* env = AttachCurrentThread(); | 68 JNIEnv* env = AttachCurrentThread(); |
61 | 69 |
62 Reset(); | 70 Reset(); |
63 jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, | 71 jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass, |
64 kReturningJavaLangClass); | 72 kReturningJavaLangClass); |
65 EXPECT_STREQ(kGetClass, g_last_method); | 73 EXPECT_STREQ(kGetClass, g_last_method); |
66 EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature); | 74 EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature); |
(...skipping 10 matching lines...) Expand all Loading... |
77 Reset(); | 85 Reset(); |
78 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, | 86 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, |
79 kReturningJavaLangString); | 87 kReturningJavaLangString); |
80 EXPECT_STREQ(kToString, g_last_method); | 88 EXPECT_STREQ(kToString, g_last_method); |
81 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); | 89 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); |
82 EXPECT_EQ(g_last_method_id, id3); | 90 EXPECT_EQ(g_last_method_id, id3); |
83 } | 91 } |
84 | 92 |
85 } // namespace android | 93 } // namespace android |
86 } // namespace base | 94 } // namespace base |
OLD | NEW |