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

Unified Diff: android_webview/native/callback_jni_bridge_unittest.cc

Issue 12313042: [android_webview] Add a generic callback JNI bridge. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: android_webview/native/callback_jni_bridge_unittest.cc
diff --git a/android_webview/native/callback_jni_bridge_unittest.cc b/android_webview/native/callback_jni_bridge_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2ec2eb66cc3538e2c7a97e31ed0ded791a7d036e
--- /dev/null
+++ b/android_webview/native/callback_jni_bridge_unittest.cc
@@ -0,0 +1,77 @@
+// Copyright 2013 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 "android_webview/native/callback_jni_bridge.h"
+#include "base/android/jni_android.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/memory/scoped_ptr.h"
+#include "jni/CallbackJNIBridgeUnittest_jni.h"
+
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using android_webview::CallbackJNIBridge;
+using base::Callback;
+using base::android::AttachCurrentThread;
+using base::android::ScopedJavaLocalRef;
+using testing::NotNull;
+using testing::Test;
+using testing::_;
+
+class CallbackJNIBridgeTest : public Test {
+ public:
+ CallbackJNIBridgeTest() {
+ }
+ protected:
+ virtual void SetUp() {
+ env_ = AttachCurrentThread();
+ ASSERT_THAT(env_, NotNull());
+ ASSERT_TRUE(android_webview::RegisterCallbackJNIBridge(env_));
+ ASSERT_TRUE(RegisterNativesImpl(env_));
+ }
+
+ JNIEnv* env_;
+};
+
+TEST_F(CallbackJNIBridgeTest, JavaToNativeIntCallback) {
+ const int expected_value = 42;
+ ScopedJavaLocalRef<jobject> jvalue_callback =
+ Java_CallbackJNIBridgeUnittest_createValueCallbackInt(env_);
+
+ Callback<void(int)> callback =
+ CallbackJNIBridge::FromJavaValueCallbackInt(env_, jvalue_callback);
+ callback.Run(expected_value);
+
+ EXPECT_TRUE(Java_CallbackJNIBridgeUnittest_checkValueCallbackIntValue(
+ env_, jvalue_callback.obj(), expected_value));
+}
+
+template<typename T>
+class TestValueCallbackImpl {
+ public:
+ T getValue() const { return value_; }
+ void setValue(T value) { value_ = value; }
+ private:
+ T value_;
+};
+
+TEST_F(CallbackJNIBridgeTest, NativeToJavaIntCallback) {
+ const int expected_value = 142;
+ TestValueCallbackImpl<int> callback_target;
+ callback_target.setValue(0);
+
+ Callback<void(int)> test_callback =
+ base::Bind(&TestValueCallbackImpl<int>::setValue,
+ base::Unretained(&callback_target));
+
+ ScopedJavaLocalRef<jobject> jvalue_callback =
+ CallbackJNIBridge::ToJavaValueCallbackInt(env_, test_callback);
+
+ Java_CallbackJNIBridgeUnittest_invokeValueCallbackInt(
+ env_, jvalue_callback.obj(), expected_value);
+
+ EXPECT_EQ(expected_value, callback_target.getValue());
+}

Powered by Google App Engine
This is Rietveld 408576698