summaryrefslogtreecommitdiffstats
path: root/base/android/jni_android_unittest.cc
blob: 920b395659025c4784a774439237fb645bcb4831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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.

#include "base/android/jni_android.h"

#include "base/at_exit.h"
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace android {

namespace {

const char kJavaLangObject[] = "java/lang/Object";
const char kGetClass[] = "getClass";
const char kToString[] = "toString";
const char kReturningJavaLangClass[] = "()Ljava/lang/Class;";
const char kReturningJavaLangString[] = "()Ljava/lang/String;";

const char* g_last_method;
const char* g_last_jni_signature;
jmethodID g_last_method_id;

const JNINativeInterface* g_previous_functions;

jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method,
                             const char* jni_signature) {
  g_last_method = method;
  g_last_jni_signature = jni_signature;
  g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method,
                                                       jni_signature);
  return g_last_method_id;
}

}  // namespace

class JNIAndroidTest : public testing::Test {
 protected:
  virtual void SetUp() {
    JNIEnv* env = AttachCurrentThread();
    g_previous_functions = env->functions;
    hooked_functions = *g_previous_functions;
    env->functions = &hooked_functions;
    hooked_functions.GetMethodID = &GetMethodIDWrapper;
  }

  virtual void TearDown() {
    JNIEnv* env = AttachCurrentThread();
    env->functions = g_previous_functions;
    Reset();
  }

  void Reset() {
    g_last_method = 0;
    g_last_jni_signature = 0;
    g_last_method_id = NULL;
  }
  // Needed to cleanup the cached method map in the implementation between
  // runs (e.g. if using --gtest_repeat)
  base::ShadowingAtExitManager exit_manager;
  // From JellyBean release, the instance of this struct provided in JNIEnv is
  // read-only, so we deep copy it to allow individual functions to be hooked.
  JNINativeInterface hooked_functions;
};

TEST_F(JNIAndroidTest, GetMethodIDFromClassNameCaching) {
  JNIEnv* env = AttachCurrentThread();

  Reset();
  jmethodID id1 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass,
                                           kReturningJavaLangClass);
  EXPECT_STREQ(kGetClass, g_last_method);
  EXPECT_STREQ(kReturningJavaLangClass, g_last_jni_signature);
  EXPECT_EQ(g_last_method_id, id1);

  Reset();
  jmethodID id2 = GetMethodIDFromClassName(env, kJavaLangObject, kGetClass,
                                           kReturningJavaLangClass);
  EXPECT_STREQ(0, g_last_method);
  EXPECT_STREQ(0, g_last_jni_signature);
  EXPECT_EQ(NULL, g_last_method_id);
  EXPECT_EQ(id1, id2);

  Reset();
  jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString,
                                           kReturningJavaLangString);
  EXPECT_STREQ(kToString, g_last_method);
  EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature);
  EXPECT_EQ(g_last_method_id, id3);
}

namespace {

base::subtle::AtomicWord g_atomic_id = 0;
int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) {
  jmethodID id = base::android::MethodID::LazyGet<
      base::android::MethodID::TYPE_STATIC>(
      env, clazz,
      "abs",
      "(I)I",
      &g_atomic_id);

  return env->CallStaticIntMethod(clazz, id, p);
}

int MethodIDCall(JNIEnv* env, jclass clazz, jmethodID id, int p) {
  return env->CallStaticIntMethod(clazz, id, p);
}

}  // namespace

TEST(JNIAndroidMicrobenchmark, MethodId) {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math"));
  base::Time start_lazy = base::Time::Now();
  int o = 0;
  for (int i = 0; i < 1024; ++i)
    o += LazyMethodIDCall(env, clazz.obj(), i);
  base::Time end_lazy = base::Time::Now();

  jmethodID id = reinterpret_cast<jmethodID>(g_atomic_id);
  base::Time start = base::Time::Now();
  for (int i = 0; i < 1024; ++i)
    o += MethodIDCall(env, clazz.obj(), id, i);
  base::Time end = base::Time::Now();

  // On a Galaxy Nexus, results were in the range of:
  // JNI LazyMethodIDCall (us) 1984
  // JNI MethodIDCall (us) 1861
  LOG(ERROR) << "JNI LazyMethodIDCall (us) " <<
      base::TimeDelta(end_lazy - start_lazy).InMicroseconds();
  LOG(ERROR) << "JNI MethodIDCall (us) " <<
      base::TimeDelta(end - start).InMicroseconds();
  LOG(ERROR) << "JNI " << o;
}


}  // namespace android
}  // namespace base