Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: base/android/jni_android_unittest.cc

Issue 23835020: Android: cleanup jni_android to minimize external dependencies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Small fix for clang Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/jni_android.h" 5 #include "base/android/jni_android.h"
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/logging.h" 8 #include "base/logging.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 15
16 const char kJavaLangObject[] = "java/lang/Object";
17 const char kGetClass[] = "getClass";
18 const char kToString[] = "toString";
19 const char kReturningJavaLangClass[] = "()Ljava/lang/Class;";
20 const char kReturningJavaLangString[] = "()Ljava/lang/String;";
21
22 const char* g_last_method;
23 const char* g_last_jni_signature;
24 jmethodID g_last_method_id;
25
26 const JNINativeInterface* g_previous_functions;
27
28 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method,
29 const char* jni_signature) {
30 g_last_method = method;
31 g_last_jni_signature = jni_signature;
32 g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method,
33 jni_signature);
34 return g_last_method_id;
35 }
36
37 } // namespace
38
39 class JNIAndroidTest : public testing::Test {
40 protected:
41 virtual void SetUp() {
42 JNIEnv* env = AttachCurrentThread();
43 g_previous_functions = env->functions;
44 hooked_functions = *g_previous_functions;
45 env->functions = &hooked_functions;
46 hooked_functions.GetMethodID = &GetMethodIDWrapper;
47 }
48
49 virtual void TearDown() {
50 JNIEnv* env = AttachCurrentThread();
51 env->functions = g_previous_functions;
52 Reset();
53 }
54
55 void Reset() {
56 g_last_method = 0;
57 g_last_jni_signature = 0;
58 g_last_method_id = NULL;
59 }
60 // Needed to cleanup the cached method map in the implementation between
61 // runs (e.g. if using --gtest_repeat)
62 base::ShadowingAtExitManager exit_manager;
63 // From JellyBean release, the instance of this struct provided in JNIEnv is
64 // read-only, so we deep copy it to allow individual functions to be hooked.
65 JNINativeInterface hooked_functions;
66 };
67
68 TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) {
69 JNIEnv* env = AttachCurrentThread();
70
71 Reset();
72 jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass,
73 kReturningJavaLangClass);
74 EXPECT_STREQ(kGetClass, g_last_method);
75 EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature);
76 EXPECT_EQ(g_last_method_id, id1);
77
78 Reset();
79 jmethodID id2 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass,
80 kReturningJavaLangClass);
81 EXPECT_STREQ(0, g_last_method);
82 EXPECT_STREQ(0, g_last_jni_signature);
83 EXPECT_EQ(NULL, g_last_method_id);
84 EXPECT_EQ(id1, id2);
85
86 Reset();
87 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString,
88 kReturningJavaLangString);
89 EXPECT_STREQ(kToString, g_last_method);
90 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature);
91 EXPECT_EQ(g_last_method_id, id3);
92 }
93
94 namespace {
95
96 base::subtle::AtomicWord g_atomic_id = 0; 16 base::subtle::AtomicWord g_atomic_id = 0;
97 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) { 17 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) {
98 jmethodID id = base::android::MethodID::LazyGet< 18 jmethodID id = base::android::MethodID::LazyGet<
99 base::android::MethodID::TYPE_STATIC>( 19 base::android::MethodID::TYPE_STATIC>(
100 env, clazz, 20 env, clazz,
101 "abs", 21 "abs",
102 "(I)I", 22 "(I)I",
103 &g_atomic_id); 23 &g_atomic_id);
104 24
105 return env->CallStaticIntMethod(clazz, id, p); 25 return env->CallStaticIntMethod(clazz, id, p);
(...skipping 26 matching lines...) Expand all
132 LOG(ERROR) << "JNI LazyMethodIDCall (us) " << 52 LOG(ERROR) << "JNI LazyMethodIDCall (us) " <<
133 base::TimeDelta(end_lazy - start_lazy).InMicroseconds(); 53 base::TimeDelta(end_lazy - start_lazy).InMicroseconds();
134 LOG(ERROR) << "JNI MethodIDCall (us) " << 54 LOG(ERROR) << "JNI MethodIDCall (us) " <<
135 base::TimeDelta(end - start).InMicroseconds(); 55 base::TimeDelta(end - start).InMicroseconds();
136 LOG(ERROR) << "JNI " << o; 56 LOG(ERROR) << "JNI " << o;
137 } 57 }
138 58
139 59
140 } // namespace android 60 } // namespace android
141 } // namespace base 61 } // namespace base
OLDNEW
« no previous file with comments | « base/android/jni_android.cc ('k') | components/web_contents_delegate_android/web_contents_delegate_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698