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