OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/base/clipboard/clipboard_android.h" | |
6 | |
7 #include "base/android/context_utils.h" | |
8 #include "base/android/jni_android.h" | |
9 #include "base/android/jni_string.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
12 | |
13 namespace ui { | |
14 | |
15 class ClipboardAndroidTest : public ::testing::Test { | |
16 public: | |
17 ~ClipboardAndroidTest() override { | |
18 Clipboard::DestroyClipboardForCurrentThread(); | |
19 }; | |
20 | |
21 protected: | |
22 Clipboard& clipboard() { return *Clipboard::GetForCurrentThread(); } | |
23 }; | |
24 | |
25 // Test that if another application writes some text to the pasteboard the | |
26 // clipboard properly invalidates other types. | |
27 TEST_F(ClipboardAndroidTest, InternalClipboardInvalidation) { | |
28 // Write a Webkit smart paste tag to our clipboard. | |
29 { | |
30 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
31 clipboard_writer.WriteWebSmartPaste(); | |
32 } | |
33 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
34 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
35 | |
36 // | |
37 // Simulate that another application copied something in the Clipboard | |
38 // | |
39 std::string new_value("Some text copied by some other app"); | |
40 using base::android::ConvertUTF8ToJavaString; | |
41 using base::android::MethodID; | |
42 using base::android::ScopedJavaLocalRef; | |
43 | |
44 JNIEnv* env = base::android::AttachCurrentThread(); | |
45 ASSERT_TRUE(env); | |
46 | |
47 ScopedJavaLocalRef<jclass> context_class = | |
48 base::android::GetClass(env, "android/content/Context"); | |
49 | |
50 jmethodID get_system_service = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
51 env, context_class.obj(), "getSystemService", | |
52 "(Ljava/lang/String;)Ljava/lang/Object;"); | |
53 | |
54 // Retrieve the system service. | |
55 ScopedJavaLocalRef<jstring> service_name = | |
56 ConvertUTF8ToJavaString(env, "clipboard"); | |
57 ScopedJavaLocalRef<jobject> clipboard_manager( | |
58 env, | |
59 env->CallObjectMethod(base::android::GetApplicationContext(), | |
60 get_system_service, service_name.obj())); | |
61 ASSERT_TRUE(clipboard_manager.obj() && !base::android::ClearException(env)); | |
62 | |
63 ScopedJavaLocalRef<jclass> clipboard_class = | |
64 base::android::GetClass(env, "android/text/ClipboardManager"); | |
65 jmethodID set_text = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
66 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); | |
67 ScopedJavaLocalRef<jstring> new_value_string = | |
68 ConvertUTF8ToJavaString(env, new_value.c_str()); | |
69 | |
70 // Will need to call toString as CharSequence is not always a String. | |
71 env->CallVoidMethod(clipboard_manager.obj(), set_text, | |
72 new_value_string.obj()); | |
73 | |
74 // The WebKit smart paste tag should now be gone. | |
75 EXPECT_FALSE(clipboard().IsFormatAvailable( | |
76 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
77 | |
78 // Make sure some text is available | |
79 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
80 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
81 | |
82 // Make sure the text is what we inserted while simulating the other app | |
83 std::string contents; | |
84 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); | |
85 EXPECT_EQ(contents, new_value); | |
86 } | |
87 | |
88 } // namespace ui | |
89 | |
90 #include "ui/base/clipboard/clipboard_test_template.h" | |
OLD | NEW |