| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/android/java/gin_java_method_invocation_helper.h" | 5 #include "content/browser/android/java/gin_java_method_invocation_helper.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "content/browser/android/java/jni_helper.h" | 8 #include "content/browser/android/java/jni_helper.h" |
| 9 #include "content/common/android/gin_java_bridge_value.h" | 9 #include "content/common/android/gin_java_bridge_value.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class NullObjectDelegate | 16 class NullObjectDelegate |
| 17 : public GinJavaMethodInvocationHelper::ObjectDelegate { | 17 : public GinJavaMethodInvocationHelper::ObjectDelegate { |
| 18 public: | 18 public: |
| 19 NullObjectDelegate() {} | 19 NullObjectDelegate() {} |
| 20 | 20 |
| 21 virtual ~NullObjectDelegate() {} | 21 ~NullObjectDelegate() override {} |
| 22 | 22 |
| 23 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( | 23 base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) override { |
| 24 JNIEnv* env) override { | |
| 25 return base::android::ScopedJavaLocalRef<jobject>(); | 24 return base::android::ScopedJavaLocalRef<jobject>(); |
| 26 } | 25 } |
| 27 | 26 |
| 28 virtual base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef( | 27 base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef( |
| 29 JNIEnv* env) override { | 28 JNIEnv* env) override { |
| 30 return base::android::ScopedJavaLocalRef<jclass>(); | 29 return base::android::ScopedJavaLocalRef<jclass>(); |
| 31 } | 30 } |
| 32 | 31 |
| 33 virtual const JavaMethod* FindMethod(const std::string& method_name, | 32 const JavaMethod* FindMethod(const std::string& method_name, |
| 34 size_t num_parameters) override { | 33 size_t num_parameters) override { |
| 35 return NULL; | 34 return NULL; |
| 36 } | 35 } |
| 37 | 36 |
| 38 virtual bool IsObjectGetClassMethod(const JavaMethod* method) override { | 37 bool IsObjectGetClassMethod(const JavaMethod* method) override { |
| 39 return false; | 38 return false; |
| 40 } | 39 } |
| 41 | 40 |
| 42 virtual const base::android::JavaRef<jclass>& GetSafeAnnotationClass() | 41 const base::android::JavaRef<jclass>& GetSafeAnnotationClass() override { |
| 43 override { | |
| 44 return safe_annotation_class_; | 42 return safe_annotation_class_; |
| 45 } | 43 } |
| 46 | 44 |
| 47 private: | 45 private: |
| 48 base::android::ScopedJavaLocalRef<jclass> safe_annotation_class_; | 46 base::android::ScopedJavaLocalRef<jclass> safe_annotation_class_; |
| 49 | 47 |
| 50 DISALLOW_COPY_AND_ASSIGN(NullObjectDelegate); | 48 DISALLOW_COPY_AND_ASSIGN(NullObjectDelegate); |
| 51 }; | 49 }; |
| 52 | 50 |
| 53 class NullDispatcherDelegate | 51 class NullDispatcherDelegate |
| 54 : public GinJavaMethodInvocationHelper::DispatcherDelegate { | 52 : public GinJavaMethodInvocationHelper::DispatcherDelegate { |
| 55 public: | 53 public: |
| 56 NullDispatcherDelegate() {} | 54 NullDispatcherDelegate() {} |
| 57 | 55 |
| 58 virtual ~NullDispatcherDelegate() {} | 56 ~NullDispatcherDelegate() override {} |
| 59 | 57 |
| 60 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( | 58 JavaObjectWeakGlobalRef GetObjectWeakRef( |
| 61 GinJavaBoundObject::ObjectID object_id) override { | 59 GinJavaBoundObject::ObjectID object_id) override { |
| 62 return JavaObjectWeakGlobalRef(); | 60 return JavaObjectWeakGlobalRef(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 DISALLOW_COPY_AND_ASSIGN(NullDispatcherDelegate); | 63 DISALLOW_COPY_AND_ASSIGN(NullDispatcherDelegate); |
| 66 }; | 64 }; |
| 67 | 65 |
| 68 } // namespace | 66 } // namespace |
| 69 | 67 |
| 70 class GinJavaMethodInvocationHelperTest : public testing::Test { | 68 class GinJavaMethodInvocationHelperTest : public testing::Test { |
| 71 }; | 69 }; |
| 72 | 70 |
| 73 namespace { | 71 namespace { |
| 74 | 72 |
| 75 class CountingDispatcherDelegate | 73 class CountingDispatcherDelegate |
| 76 : public GinJavaMethodInvocationHelper::DispatcherDelegate { | 74 : public GinJavaMethodInvocationHelper::DispatcherDelegate { |
| 77 public: | 75 public: |
| 78 CountingDispatcherDelegate() {} | 76 CountingDispatcherDelegate() {} |
| 79 | 77 |
| 80 virtual ~CountingDispatcherDelegate() {} | 78 ~CountingDispatcherDelegate() override {} |
| 81 | 79 |
| 82 virtual JavaObjectWeakGlobalRef GetObjectWeakRef( | 80 JavaObjectWeakGlobalRef GetObjectWeakRef( |
| 83 GinJavaBoundObject::ObjectID object_id) override { | 81 GinJavaBoundObject::ObjectID object_id) override { |
| 84 counters_[object_id]++; | 82 counters_[object_id]++; |
| 85 return JavaObjectWeakGlobalRef(); | 83 return JavaObjectWeakGlobalRef(); |
| 86 } | 84 } |
| 87 | 85 |
| 88 void AssertInvocationsCount(GinJavaBoundObject::ObjectID begin_object_id, | 86 void AssertInvocationsCount(GinJavaBoundObject::ObjectID begin_object_id, |
| 89 GinJavaBoundObject::ObjectID end_object_id) { | 87 GinJavaBoundObject::ObjectID end_object_id) { |
| 90 EXPECT_EQ(end_object_id - begin_object_id, | 88 EXPECT_EQ(end_object_id - begin_object_id, |
| 91 static_cast<int>(counters_.size())); | 89 static_cast<int>(counters_.size())); |
| 92 for (GinJavaBoundObject::ObjectID i = begin_object_id; | 90 for (GinJavaBoundObject::ObjectID i = begin_object_id; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 base::android::ScopedJavaLocalRef<jobject> method_obj( | 167 base::android::ScopedJavaLocalRef<jobject> method_obj( |
| 170 env, | 168 env, |
| 171 env->ToReflectedMethod( | 169 env->ToReflectedMethod( |
| 172 base::android::GetClass(env, "java/lang/Object").obj(), | 170 base::android::GetClass(env, "java/lang/Object").obj(), |
| 173 method_id, | 171 method_id, |
| 174 false)); | 172 false)); |
| 175 EXPECT_TRUE(method_obj.obj()); | 173 EXPECT_TRUE(method_obj.obj()); |
| 176 method_.reset(new JavaMethod(method_obj)); | 174 method_.reset(new JavaMethod(method_obj)); |
| 177 } | 175 } |
| 178 | 176 |
| 179 virtual ~ObjectIsGoneObjectDelegate() {} | 177 ~ObjectIsGoneObjectDelegate() override {} |
| 180 | 178 |
| 181 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( | 179 base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) override { |
| 182 JNIEnv* env) override { | |
| 183 get_local_ref_called_ = true; | 180 get_local_ref_called_ = true; |
| 184 return NullObjectDelegate::GetLocalRef(env); | 181 return NullObjectDelegate::GetLocalRef(env); |
| 185 } | 182 } |
| 186 | 183 |
| 187 virtual const JavaMethod* FindMethod(const std::string& method_name, | 184 const JavaMethod* FindMethod(const std::string& method_name, |
| 188 size_t num_parameters) override { | 185 size_t num_parameters) override { |
| 189 return method_.get(); | 186 return method_.get(); |
| 190 } | 187 } |
| 191 | 188 |
| 192 bool get_local_ref_called() { return get_local_ref_called_; } | 189 bool get_local_ref_called() { return get_local_ref_called_; } |
| 193 | 190 |
| 194 const std::string& get_method_name() { return method_->name(); } | 191 const std::string& get_method_name() { return method_->name(); } |
| 195 | 192 |
| 196 protected: | 193 protected: |
| 197 scoped_ptr<JavaMethod> method_; | 194 scoped_ptr<JavaMethod> method_; |
| 198 bool get_local_ref_called_; | 195 bool get_local_ref_called_; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 223 EXPECT_TRUE(helper->GetPrimitiveResult().empty()); | 220 EXPECT_TRUE(helper->GetPrimitiveResult().empty()); |
| 224 EXPECT_EQ(kGinJavaBridgeObjectIsGone, helper->GetInvocationError()); | 221 EXPECT_EQ(kGinJavaBridgeObjectIsGone, helper->GetInvocationError()); |
| 225 } | 222 } |
| 226 | 223 |
| 227 namespace { | 224 namespace { |
| 228 | 225 |
| 229 class MethodNotFoundObjectDelegate : public NullObjectDelegate { | 226 class MethodNotFoundObjectDelegate : public NullObjectDelegate { |
| 230 public: | 227 public: |
| 231 MethodNotFoundObjectDelegate() : find_method_called_(false) {} | 228 MethodNotFoundObjectDelegate() : find_method_called_(false) {} |
| 232 | 229 |
| 233 virtual ~MethodNotFoundObjectDelegate() {} | 230 ~MethodNotFoundObjectDelegate() override {} |
| 234 | 231 |
| 235 virtual base::android::ScopedJavaLocalRef<jobject> GetLocalRef( | 232 base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) override { |
| 236 JNIEnv* env) override { | |
| 237 return base::android::ScopedJavaLocalRef<jobject>( | 233 return base::android::ScopedJavaLocalRef<jobject>( |
| 238 env, static_cast<jobject>(env->FindClass("java/lang/String"))); | 234 env, static_cast<jobject>(env->FindClass("java/lang/String"))); |
| 239 } | 235 } |
| 240 | 236 |
| 241 virtual const JavaMethod* FindMethod(const std::string& method_name, | 237 const JavaMethod* FindMethod(const std::string& method_name, |
| 242 size_t num_parameters) override { | 238 size_t num_parameters) override { |
| 243 find_method_called_ = true; | 239 find_method_called_ = true; |
| 244 return NULL; | 240 return NULL; |
| 245 } | 241 } |
| 246 | 242 |
| 247 bool find_method_called() const { return find_method_called_; } | 243 bool find_method_called() const { return find_method_called_; } |
| 248 | 244 |
| 249 protected: | 245 protected: |
| 250 bool find_method_called_; | 246 bool find_method_called_; |
| 251 | 247 |
| 252 private: | 248 private: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 275 EXPECT_TRUE(helper->GetPrimitiveResult().empty()); | 271 EXPECT_TRUE(helper->GetPrimitiveResult().empty()); |
| 276 EXPECT_EQ(kGinJavaBridgeMethodNotFound, helper->GetInvocationError()); | 272 EXPECT_EQ(kGinJavaBridgeMethodNotFound, helper->GetInvocationError()); |
| 277 } | 273 } |
| 278 | 274 |
| 279 namespace { | 275 namespace { |
| 280 | 276 |
| 281 class GetClassObjectDelegate : public MethodNotFoundObjectDelegate { | 277 class GetClassObjectDelegate : public MethodNotFoundObjectDelegate { |
| 282 public: | 278 public: |
| 283 GetClassObjectDelegate() : get_class_called_(false) {} | 279 GetClassObjectDelegate() : get_class_called_(false) {} |
| 284 | 280 |
| 285 virtual ~GetClassObjectDelegate() {} | 281 ~GetClassObjectDelegate() override {} |
| 286 | 282 |
| 287 virtual const JavaMethod* FindMethod(const std::string& method_name, | 283 const JavaMethod* FindMethod(const std::string& method_name, |
| 288 size_t num_parameters) override { | 284 size_t num_parameters) override { |
| 289 find_method_called_ = true; | 285 find_method_called_ = true; |
| 290 return kFakeGetClass; | 286 return kFakeGetClass; |
| 291 } | 287 } |
| 292 | 288 |
| 293 virtual bool IsObjectGetClassMethod(const JavaMethod* method) override { | 289 bool IsObjectGetClassMethod(const JavaMethod* method) override { |
| 294 get_class_called_ = true; | 290 get_class_called_ = true; |
| 295 return kFakeGetClass == method; | 291 return kFakeGetClass == method; |
| 296 } | 292 } |
| 297 | 293 |
| 298 bool get_class_called() const { return get_class_called_; } | 294 bool get_class_called() const { return get_class_called_; } |
| 299 | 295 |
| 300 private: | 296 private: |
| 301 static const JavaMethod* kFakeGetClass; | 297 static const JavaMethod* kFakeGetClass; |
| 302 bool get_class_called_; | 298 bool get_class_called_; |
| 303 | 299 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 330 helper->Invoke(); | 326 helper->Invoke(); |
| 331 EXPECT_TRUE(object_delegate->find_method_called()); | 327 EXPECT_TRUE(object_delegate->find_method_called()); |
| 332 EXPECT_TRUE(object_delegate->get_class_called()); | 328 EXPECT_TRUE(object_delegate->get_class_called()); |
| 333 EXPECT_TRUE(helper->HoldsPrimitiveResult()); | 329 EXPECT_TRUE(helper->HoldsPrimitiveResult()); |
| 334 EXPECT_TRUE(helper->GetPrimitiveResult().empty()); | 330 EXPECT_TRUE(helper->GetPrimitiveResult().empty()); |
| 335 EXPECT_EQ(kGinJavaBridgeAccessToObjectGetClassIsBlocked, | 331 EXPECT_EQ(kGinJavaBridgeAccessToObjectGetClassIsBlocked, |
| 336 helper->GetInvocationError()); | 332 helper->GetInvocationError()); |
| 337 } | 333 } |
| 338 | 334 |
| 339 } // namespace content | 335 } // namespace content |
| OLD | NEW |