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

Side by Side Diff: ui/base/clipboard/clipboard_unittest.cc

Issue 10695051: Upstream the Android clipboard implementation (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 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 unified diff | Download patch
« no previous file with comments | « ui/base/clipboard/clipboard_android.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/pickle.h" 11 #include "base/pickle.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h" 15 #include "testing/platform_test.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/base/clipboard/clipboard.h" 17 #include "ui/base/clipboard/clipboard.h"
18 #include "ui/base/clipboard/scoped_clipboard_writer.h" 18 #include "ui/base/clipboard/scoped_clipboard_writer.h"
19 #include "ui/gfx/size.h" 19 #include "ui/gfx/size.h"
20 20
21 #if defined(OS_WIN) 21 #if defined(OS_WIN)
22 #include "base/message_loop.h" 22 #include "base/message_loop.h"
23 #include "ui/base/clipboard/clipboard_util_win.h" 23 #include "ui/base/clipboard/clipboard_util_win.h"
24 #endif 24 #endif
25 25
26 #if defined(OS_ANDROID)
27 #include "base/android/jni_android.h"
28 #endif
29
26 namespace ui { 30 namespace ui {
27 31
28 #if defined(OS_WIN) 32 #if defined(OS_WIN)
29 class ClipboardTest : public PlatformTest { 33 class ClipboardTest : public PlatformTest {
30 protected: 34 protected:
31 virtual void SetUp() { 35 virtual void SetUp() {
32 message_loop_.reset(new MessageLoopForUI()); 36 message_loop_.reset(new MessageLoopForUI());
33 } 37 }
34 virtual void TearDown() { 38 virtual void TearDown() {
35 } 39 }
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 Clipboard::BUFFER_STANDARD)); 331 Clipboard::BUFFER_STANDARD));
328 string16 text_result; 332 string16 text_result;
329 clipboard.ReadText(Clipboard::BUFFER_STANDARD, &text_result); 333 clipboard.ReadText(Clipboard::BUFFER_STANDARD, &text_result);
330 334
331 EXPECT_EQ(text_result, url); 335 EXPECT_EQ(text_result, url);
332 336
333 std::string ascii_text; 337 std::string ascii_text;
334 clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &ascii_text); 338 clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &ascii_text);
335 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); 339 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
336 340
337 #if defined(OS_POSIX) && !defined(OS_MACOSX) 341 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
338 ascii_text.clear(); 342 ascii_text.clear();
339 clipboard.ReadAsciiText(Clipboard::BUFFER_SELECTION, &ascii_text); 343 clipboard.ReadAsciiText(Clipboard::BUFFER_SELECTION, &ascii_text);
340 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); 344 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
341 #endif 345 #endif
342 } 346 }
343 347
344 TEST_F(ClipboardTest, SharedBitmapTest) { 348 TEST_F(ClipboardTest, SharedBitmapTest) {
345 unsigned int fake_bitmap[] = { 349 unsigned int fake_bitmap[] = {
346 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, 350 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89,
347 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, 351 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 Clipboard clipboard; 384 Clipboard clipboard;
381 clipboard.WriteObjects(Clipboard::BUFFER_STANDARD, objects); 385 clipboard.WriteObjects(Clipboard::BUFFER_STANDARD, objects);
382 386
383 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), 387 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(),
384 Clipboard::BUFFER_STANDARD)); 388 Clipboard::BUFFER_STANDARD));
385 } 389 }
386 390
387 // The following test somehow fails on GTK. The image when read back from the 391 // The following test somehow fails on GTK. The image when read back from the
388 // clipboard has the alpha channel set to 0xFF for some reason. The other 392 // clipboard has the alpha channel set to 0xFF for some reason. The other
389 // channels stay intact. So I am turning this on only for aura. 393 // channels stay intact. So I am turning this on only for aura.
390 #if defined(USE_AURA) && !defined(OS_WIN) 394 #if (defined(USE_AURA) && !defined(OS_WIN)) || defined(OS_ANDROID)
391 TEST_F(ClipboardTest, MultipleBitmapReadWriteTest) { 395 TEST_F(ClipboardTest, MultipleBitmapReadWriteTest) {
392 Clipboard clipboard; 396 Clipboard clipboard;
393 397
394 // Test first bitmap 398 // Test first bitmap
395 unsigned int fake_bitmap_1[] = { 399 unsigned int fake_bitmap_1[] = {
396 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, 400 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89,
397 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, 401 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568,
398 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, 402 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1,
399 }; 403 };
400 gfx::Size fake_bitmap_1_size(3, 4); 404 gfx::Size fake_bitmap_1_size(3, 4);
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 writer.WriteHTML(UTF8ToUTF16("foo"), "bar"); 646 writer.WriteHTML(UTF8ToUTF16("foo"), "bar");
643 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar"); 647 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar");
644 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar"); 648 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar");
645 writer.WriteWebSmartPaste(); 649 writer.WriteWebSmartPaste();
646 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData. 650 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData.
647 } 651 }
648 652
649 // Passes if we don't crash. 653 // Passes if we don't crash.
650 } 654 }
651 655
656 #if defined(OS_ANDROID)
657
658 // Test that if another application writes some text to the pasteboard the
659 // clipboard properly invalidates other types.
660 TEST_F(ClipboardTest, InternalClipboardInvalidation) {
661 const unsigned int kFakeBitmap[] = {
662 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89,
663 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568,
664 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1,
665 };
666
667 // Write a bitmap in our clipboard.
668 Clipboard clipboard;
669 {
670 ScopedClipboardWriter clipboard_writer(&clipboard,
671 Clipboard::BUFFER_STANDARD);
672 clipboard_writer.WriteBitmapFromPixels(kFakeBitmap, gfx::Size(3, 4));
673 }
674
675 //
676 // Simulate that another application copied something in the Clipboard
677 //
678 std::string new_value("Some text copied by some other app");
679 using base::android::ScopedJavaLocalRef;
680
681 JNIEnv* env = base::android::AttachCurrentThread();
682 ASSERT_TRUE(env);
683
684 jobject context = base::android::GetApplicationContext();
685 ASSERT_TRUE(context);
686
687 ScopedJavaLocalRef<jclass> context_class =
688 base::android::GetClass(env, "android/content/Context");
689
690 jmethodID get_system_service = base::android::GetMethodID(env, context_class,
691 "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
692
693 // Retrieve the system service.
694 ScopedJavaLocalRef<jstring> service_name(env, env->NewStringUTF("clipboard"));
695 ScopedJavaLocalRef<jobject> clipboard_manager(
696 env, env->CallObjectMethod(
697 context, get_system_service, service_name.obj()));
698 ASSERT_TRUE(clipboard_manager.obj() && !base::android::ClearException(env));
699
700 ScopedJavaLocalRef<jclass> clipboard_class =
701 base::android::GetClass(env, "android/text/ClipboardManager");
702 jmethodID set_text = base::android::GetMethodID(env, clipboard_class,
703 "setText", "(Ljava/lang/CharSequence;)V");
704
705 // Will need to call toString as CharSequence is not always a String.
706 env->CallVoidMethod(clipboard_manager.obj(),
707 set_text,
708 env->NewStringUTF(new_value.c_str()));
709
710 // The bitmap that should have been available should be gone.
711 EXPECT_FALSE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(),
712 Clipboard::BUFFER_STANDARD));
713
714 // Make sure some text is available
715 EXPECT_TRUE(clipboard.IsFormatAvailable(
716 Clipboard::GetPlainTextWFormatType(), Clipboard::BUFFER_STANDARD));
717
718 // Make sure the text is what we inserted while simulating the other app
719 std::string contents;
720 clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &contents);
721 EXPECT_EQ(contents, new_value);
722 }
723 #endif
652 } // namespace ui 724 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/clipboard/clipboard_android.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698