Chromium Code Reviews| 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 "ui/base/clipboard/clipboard_android.h" | 5 #include "ui/base/clipboard/clipboard_android.h" |
| 6 | 6 |
| 7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 // Simulate that another application copied something in the Clipboard | 37 // Simulate that another application copied something in the Clipboard |
| 38 // | 38 // |
| 39 std::string new_value("Some text copied by some other app"); | 39 std::string new_value("Some text copied by some other app"); |
| 40 using base::android::ConvertUTF8ToJavaString; | 40 using base::android::ConvertUTF8ToJavaString; |
| 41 using base::android::MethodID; | 41 using base::android::MethodID; |
| 42 using base::android::ScopedJavaLocalRef; | 42 using base::android::ScopedJavaLocalRef; |
| 43 | 43 |
| 44 JNIEnv* env = base::android::AttachCurrentThread(); | 44 JNIEnv* env = base::android::AttachCurrentThread(); |
| 45 ASSERT_TRUE(env); | 45 ASSERT_TRUE(env); |
| 46 | 46 |
| 47 jobject context = base::android::GetApplicationContext(); | |
| 48 ASSERT_TRUE(context); | |
|
Theresa
2016/08/15 17:54:14
Why is this check no longer necessary?
| |
| 49 | |
| 50 ScopedJavaLocalRef<jclass> context_class = | 47 ScopedJavaLocalRef<jclass> context_class = |
| 51 base::android::GetClass(env, "android/content/Context"); | 48 base::android::GetClass(env, "android/content/Context"); |
| 52 | 49 |
| 53 jmethodID get_system_service = MethodID::Get<MethodID::TYPE_INSTANCE>( | 50 jmethodID get_system_service = MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 54 env, context_class.obj(), "getSystemService", | 51 env, context_class.obj(), "getSystemService", |
| 55 "(Ljava/lang/String;)Ljava/lang/Object;"); | 52 "(Ljava/lang/String;)Ljava/lang/Object;"); |
| 56 | 53 |
| 57 // Retrieve the system service. | 54 // Retrieve the system service. |
| 58 ScopedJavaLocalRef<jstring> service_name = | 55 ScopedJavaLocalRef<jstring> service_name = |
| 59 ConvertUTF8ToJavaString(env, "clipboard"); | 56 ConvertUTF8ToJavaString(env, "clipboard"); |
| 60 ScopedJavaLocalRef<jobject> clipboard_manager( | 57 ScopedJavaLocalRef<jobject> clipboard_manager( |
| 61 env, | 58 env, |
| 62 env->CallObjectMethod(context, get_system_service, service_name.obj())); | 59 env->CallObjectMethod(base::android::GetApplicationContext(), |
| 60 get_system_service, service_name.obj())); | |
| 63 ASSERT_TRUE(clipboard_manager.obj() && !base::android::ClearException(env)); | 61 ASSERT_TRUE(clipboard_manager.obj() && !base::android::ClearException(env)); |
| 64 | 62 |
| 65 ScopedJavaLocalRef<jclass> clipboard_class = | 63 ScopedJavaLocalRef<jclass> clipboard_class = |
| 66 base::android::GetClass(env, "android/text/ClipboardManager"); | 64 base::android::GetClass(env, "android/text/ClipboardManager"); |
| 67 jmethodID set_text = MethodID::Get<MethodID::TYPE_INSTANCE>( | 65 jmethodID set_text = MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 68 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); | 66 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); |
| 69 ScopedJavaLocalRef<jstring> new_value_string = | 67 ScopedJavaLocalRef<jstring> new_value_string = |
| 70 ConvertUTF8ToJavaString(env, new_value.c_str()); | 68 ConvertUTF8ToJavaString(env, new_value.c_str()); |
| 71 | 69 |
| 72 // Will need to call toString as CharSequence is not always a String. | 70 // Will need to call toString as CharSequence is not always a String. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 83 | 81 |
| 84 // Make sure the text is what we inserted while simulating the other app | 82 // Make sure the text is what we inserted while simulating the other app |
| 85 std::string contents; | 83 std::string contents; |
| 86 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); | 84 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); |
| 87 EXPECT_EQ(contents, new_value); | 85 EXPECT_EQ(contents, new_value); |
| 88 } | 86 } |
| 89 | 87 |
| 90 } // namespace ui | 88 } // namespace ui |
| 91 | 89 |
| 92 #include "ui/base/clipboard/clipboard_test_template.h" | 90 #include "ui/base/clipboard/clipboard_test_template.h" |
| OLD | NEW |