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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
// Copyright 2014 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 "content/browser/android/java/gin_java_method_invocation_helper.h"
#include "base/android/jni_android.h"
#include "content/browser/android/java/jni_helper.h"
#include "content/common/android/gin_java_bridge_value.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
class NullObjectDelegate
: public GinJavaMethodInvocationHelper::ObjectDelegate {
public:
NullObjectDelegate() {}
virtual ~NullObjectDelegate() {}
virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef(
JNIEnv* env) override {
return base::android::ScopedJavaLocalRef<jobject>();
}
virtual base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef(
JNIEnv* env) override {
return base::android::ScopedJavaLocalRef<jclass>();
}
virtual const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
return NULL;
}
virtual bool IsObjectGetClassMethod(const JavaMethod* method) override {
return false;
}
virtual const base::android::JavaRef<jclass>& GetSafeAnnotationClass()
override {
return safe_annotation_class_;
}
private:
base::android::ScopedJavaLocalRef<jclass> safe_annotation_class_;
DISALLOW_COPY_AND_ASSIGN(NullObjectDelegate);
};
class NullDispatcherDelegate
: public GinJavaMethodInvocationHelper::DispatcherDelegate {
public:
NullDispatcherDelegate() {}
virtual ~NullDispatcherDelegate() {}
virtual JavaObjectWeakGlobalRef GetObjectWeakRef(
GinJavaBoundObject::ObjectID object_id) override {
return JavaObjectWeakGlobalRef();
}
DISALLOW_COPY_AND_ASSIGN(NullDispatcherDelegate);
};
} // namespace
class GinJavaMethodInvocationHelperTest : public testing::Test {
};
namespace {
class CountingDispatcherDelegate
: public GinJavaMethodInvocationHelper::DispatcherDelegate {
public:
CountingDispatcherDelegate() {}
virtual ~CountingDispatcherDelegate() {}
virtual JavaObjectWeakGlobalRef GetObjectWeakRef(
GinJavaBoundObject::ObjectID object_id) override {
counters_[object_id]++;
return JavaObjectWeakGlobalRef();
}
void AssertInvocationsCount(GinJavaBoundObject::ObjectID begin_object_id,
GinJavaBoundObject::ObjectID end_object_id) {
EXPECT_EQ(end_object_id - begin_object_id,
static_cast<int>(counters_.size()));
for (GinJavaBoundObject::ObjectID i = begin_object_id;
i < end_object_id; ++i) {
EXPECT_LT(0, counters_[i]) << "ObjectID: " << i;
}
}
private:
typedef std::map<GinJavaBoundObject::ObjectID, int> Counters;
Counters counters_;
DISALLOW_COPY_AND_ASSIGN(CountingDispatcherDelegate);
};
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, RetrievalOfObjectsNoObjects) {
base::ListValue no_objects;
for (int i = 0; i < 10; ++i) {
no_objects.AppendInteger(i);
}
scoped_refptr<GinJavaMethodInvocationHelper> helper =
new GinJavaMethodInvocationHelper(
scoped_ptr<GinJavaMethodInvocationHelper::ObjectDelegate>(
new NullObjectDelegate()),
"foo",
no_objects);
CountingDispatcherDelegate counter;
helper->Init(&counter);
counter.AssertInvocationsCount(0, 0);
}
TEST_F(GinJavaMethodInvocationHelperTest, RetrievalOfObjectsHaveObjects) {
base::ListValue objects;
objects.AppendInteger(100);
objects.Append(GinJavaBridgeValue::CreateObjectIDValue(1).release());
base::ListValue* sub_list = new base::ListValue();
sub_list->AppendInteger(200);
sub_list->Append(GinJavaBridgeValue::CreateObjectIDValue(2).release());
objects.Append(sub_list);
base::DictionaryValue* sub_dict = new base::DictionaryValue();
sub_dict->SetInteger("1", 300);
sub_dict->Set("2", GinJavaBridgeValue::CreateObjectIDValue(3).release());
objects.Append(sub_dict);
base::ListValue* sub_list_with_dict = new base::ListValue();
base::DictionaryValue* sub_sub_dict = new base::DictionaryValue();
sub_sub_dict->Set("1", GinJavaBridgeValue::CreateObjectIDValue(4).release());
sub_list_with_dict->Append(sub_sub_dict);
objects.Append(sub_list_with_dict);
base::DictionaryValue* sub_dict_with_list = new base::DictionaryValue();
base::ListValue* sub_sub_list = new base::ListValue();
sub_sub_list->Append(GinJavaBridgeValue::CreateObjectIDValue(5).release());
sub_dict_with_list->Set("1", sub_sub_list);
objects.Append(sub_dict_with_list);
scoped_refptr<GinJavaMethodInvocationHelper> helper =
new GinJavaMethodInvocationHelper(
scoped_ptr<GinJavaMethodInvocationHelper::ObjectDelegate>(
new NullObjectDelegate()),
"foo",
objects);
CountingDispatcherDelegate counter;
helper->Init(&counter);
counter.AssertInvocationsCount(1, 6);
}
namespace {
class ObjectIsGoneObjectDelegate : public NullObjectDelegate {
public:
ObjectIsGoneObjectDelegate() :
get_local_ref_called_(false) {
// We need a Java Method object to create a valid JavaMethod instance.
JNIEnv* env = base::android::AttachCurrentThread();
jmethodID method_id =
GetMethodIDFromClassName(env, "java/lang/Object", "hashCode", "()I");
EXPECT_TRUE(method_id);
base::android::ScopedJavaLocalRef<jobject> method_obj(
env,
env->ToReflectedMethod(
base::android::GetClass(env, "java/lang/Object").obj(),
method_id,
false));
EXPECT_TRUE(method_obj.obj());
method_.reset(new JavaMethod(method_obj));
}
virtual ~ObjectIsGoneObjectDelegate() {}
virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef(
JNIEnv* env) override {
get_local_ref_called_ = true;
return NullObjectDelegate::GetLocalRef(env);
}
virtual const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
return method_.get();
}
bool get_local_ref_called() { return get_local_ref_called_; }
const std::string& get_method_name() { return method_->name(); }
protected:
scoped_ptr<JavaMethod> method_;
bool get_local_ref_called_;
private:
DISALLOW_COPY_AND_ASSIGN(ObjectIsGoneObjectDelegate);
};
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, HandleObjectIsGone) {
base::ListValue no_objects;
ObjectIsGoneObjectDelegate* object_delegate =
new ObjectIsGoneObjectDelegate();
scoped_refptr<GinJavaMethodInvocationHelper> helper =
new GinJavaMethodInvocationHelper(
scoped_ptr<GinJavaMethodInvocationHelper::ObjectDelegate>(
object_delegate),
object_delegate->get_method_name(),
no_objects);
NullDispatcherDelegate dispatcher;
helper->Init(&dispatcher);
EXPECT_FALSE(object_delegate->get_local_ref_called());
EXPECT_EQ(kGinJavaBridgeNoError, helper->GetInvocationError());
helper->Invoke();
EXPECT_TRUE(object_delegate->get_local_ref_called());
EXPECT_TRUE(helper->HoldsPrimitiveResult());
EXPECT_TRUE(helper->GetPrimitiveResult().empty());
EXPECT_EQ(kGinJavaBridgeObjectIsGone, helper->GetInvocationError());
}
namespace {
class MethodNotFoundObjectDelegate : public NullObjectDelegate {
public:
MethodNotFoundObjectDelegate() : find_method_called_(false) {}
virtual ~MethodNotFoundObjectDelegate() {}
virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef(
JNIEnv* env) override {
return base::android::ScopedJavaLocalRef<jobject>(
env, static_cast<jobject>(env->FindClass("java/lang/String")));
}
virtual const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
find_method_called_ = true;
return NULL;
}
bool find_method_called() const { return find_method_called_; }
protected:
bool find_method_called_;
private:
DISALLOW_COPY_AND_ASSIGN(MethodNotFoundObjectDelegate);
};
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, HandleMethodNotFound) {
base::ListValue no_objects;
MethodNotFoundObjectDelegate* object_delegate =
new MethodNotFoundObjectDelegate();
scoped_refptr<GinJavaMethodInvocationHelper> helper =
new GinJavaMethodInvocationHelper(
scoped_ptr<GinJavaMethodInvocationHelper::ObjectDelegate>(
object_delegate),
"foo",
no_objects);
NullDispatcherDelegate dispatcher;
helper->Init(&dispatcher);
EXPECT_FALSE(object_delegate->find_method_called());
EXPECT_EQ(kGinJavaBridgeNoError, helper->GetInvocationError());
helper->Invoke();
EXPECT_TRUE(object_delegate->find_method_called());
EXPECT_TRUE(helper->HoldsPrimitiveResult());
EXPECT_TRUE(helper->GetPrimitiveResult().empty());
EXPECT_EQ(kGinJavaBridgeMethodNotFound, helper->GetInvocationError());
}
namespace {
class GetClassObjectDelegate : public MethodNotFoundObjectDelegate {
public:
GetClassObjectDelegate() : get_class_called_(false) {}
virtual ~GetClassObjectDelegate() {}
virtual const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
find_method_called_ = true;
return kFakeGetClass;
}
virtual bool IsObjectGetClassMethod(const JavaMethod* method) override {
get_class_called_ = true;
return kFakeGetClass == method;
}
bool get_class_called() const { return get_class_called_; }
private:
static const JavaMethod* kFakeGetClass;
bool get_class_called_;
DISALLOW_COPY_AND_ASSIGN(GetClassObjectDelegate);
};
// We don't expect GinJavaMethodInvocationHelper to actually invoke the
// method, since the point of the test is to verify whether calls to
// 'getClass' get blocked.
const JavaMethod* GetClassObjectDelegate::kFakeGetClass =
(JavaMethod*)0xdeadbeef;
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, HandleGetClassInvocation) {
base::ListValue no_objects;
GetClassObjectDelegate* object_delegate =
new GetClassObjectDelegate();
scoped_refptr<GinJavaMethodInvocationHelper> helper =
new GinJavaMethodInvocationHelper(
scoped_ptr<GinJavaMethodInvocationHelper::ObjectDelegate>(
object_delegate),
"foo",
no_objects);
NullDispatcherDelegate dispatcher;
helper->Init(&dispatcher);
EXPECT_FALSE(object_delegate->find_method_called());
EXPECT_FALSE(object_delegate->get_class_called());
EXPECT_EQ(kGinJavaBridgeNoError, helper->GetInvocationError());
helper->Invoke();
EXPECT_TRUE(object_delegate->find_method_called());
EXPECT_TRUE(object_delegate->get_class_called());
EXPECT_TRUE(helper->HoldsPrimitiveResult());
EXPECT_TRUE(helper->GetPrimitiveResult().empty());
EXPECT_EQ(kGinJavaBridgeAccessToObjectGetClassIsBlocked,
helper->GetInvocationError());
}
} // namespace content
|