OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 unsigned int fake_bitmap[] = { | |
dcheng
2012/07/02 22:50:19
const unsigned int kFakeBitmap
cjhopman
2012/07/02 23:26:51
unsigned int fake_bitmap is consistent with the re
dcheng
2012/07/02 23:40:26
Hm. Well... how about fixing this one, and I'll fi
cjhopman
2012/07/02 23:53:28
Done.
| |
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(fake_bitmap, 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 DCHECK(env); | |
dcheng
2012/07/02 22:50:19
ASSERT instead of DCHECK in unit tests.
cjhopman
2012/07/02 23:26:51
Done.
| |
683 // Get the context. | |
684 jobject context = base::android::GetApplicationContext(); | |
685 DCHECK(context); | |
686 | |
687 // Get the context class. | |
688 ScopedJavaLocalRef<jclass> context_class = | |
689 base::android::GetClass(env, "android/content/Context"); | |
690 | |
691 // Get the system service method. | |
692 jmethodID get_system_service = base::android::GetMethodID(env, context_class, | |
693 "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); | |
694 | |
695 // Retrieve the system service. | |
696 ScopedJavaLocalRef<jstring> service_name(env, env->NewStringUTF("clipboard")); | |
697 ScopedJavaLocalRef<jobject> clipboard_manager( | |
698 env, env->CallObjectMethod( | |
699 context, get_system_service, service_name.obj())); | |
700 DCHECK(clipboard_manager.obj() && !base::android::ClearException(env)); | |
701 | |
702 // Retain a few methods we'll keep using. | |
703 ScopedJavaLocalRef<jclass> clipboard_class = | |
704 base::android::GetClass(env, "android/text/ClipboardManager"); | |
705 jmethodID set_text = base::android::GetMethodID(env, clipboard_class, | |
706 "setText", "(Ljava/lang/CharSequence;)V"); | |
707 | |
708 // Will need to call toString as CharSequence is not always a String. | |
709 env->CallVoidMethod(clipboard_manager.obj(), | |
710 set_text, | |
711 env->NewStringUTF(new_value.c_str())); | |
712 | |
713 // The bitmap that should have been available should be gone. | |
714 EXPECT_FALSE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), | |
715 Clipboard::BUFFER_STANDARD)); | |
716 | |
717 // Make sure some text is available | |
718 EXPECT_TRUE(clipboard.IsFormatAvailable( | |
719 Clipboard::GetPlainTextWFormatType(), Clipboard::BUFFER_STANDARD)); | |
720 | |
721 // Make sure the text is what we inserted while simulating the other app | |
722 std::string contents; | |
723 clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &contents); | |
724 EXPECT_TRUE(!contents.compare(new_value)); | |
dcheng
2012/07/02 22:50:19
EXPECT_EQ() might read a bit more naturally.
cjhopman
2012/07/02 23:26:51
Done.
| |
725 } | |
726 #endif | |
652 } // namespace ui | 727 } // namespace ui |
OLD | NEW |