| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/base/clipboard/clipboard.h" | 16 #include "ui/base/clipboard/clipboard.h" |
| 16 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 17 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 17 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
| 18 | 19 |
| 19 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 20 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
| 21 #include "ui/base/clipboard/clipboard_util_win.h" | 22 #include "ui/base/clipboard/clipboard_util_win.h" |
| 22 #endif | 23 #endif |
| 23 | 24 |
| 24 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_MACOSX)) | 25 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_MACOSX)) |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 objects[Clipboard::CBF_SMBITMAP] = params; | 287 objects[Clipboard::CBF_SMBITMAP] = params; |
| 287 Clipboard::ReplaceSharedMemHandle(&objects, handle_to_share, current_process); | 288 Clipboard::ReplaceSharedMemHandle(&objects, handle_to_share, current_process); |
| 288 | 289 |
| 289 Clipboard clipboard; | 290 Clipboard clipboard; |
| 290 clipboard.WriteObjects(objects); | 291 clipboard.WriteObjects(objects); |
| 291 | 292 |
| 292 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), | 293 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), |
| 293 Clipboard::BUFFER_STANDARD)); | 294 Clipboard::BUFFER_STANDARD)); |
| 294 } | 295 } |
| 295 | 296 |
| 297 // The following test somehow fails on GTK. The image when read back from the |
| 298 // clipboard has the alpha channel set to 0xFF for some reason. The other |
| 299 // channels stay intact. So I am turning this on only for aura. |
| 300 #if defined(USE_AURA) |
| 301 TEST_F(ClipboardTest, MultipleBitmapReadWriteTest) { |
| 302 Clipboard clipboard; |
| 303 |
| 304 // Test first bitmap |
| 305 unsigned int fake_bitmap_1[] = { |
| 306 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, |
| 307 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, |
| 308 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, |
| 309 }; |
| 310 gfx::Size fake_bitmap_1_size(3, 4); |
| 311 { |
| 312 ScopedClipboardWriter clipboard_writer(&clipboard); |
| 313 clipboard_writer.WriteBitmapFromPixels(fake_bitmap_1, fake_bitmap_1_size); |
| 314 } |
| 315 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), |
| 316 Clipboard::BUFFER_STANDARD)); |
| 317 SkBitmap image_1 = clipboard.ReadImage(Clipboard::BUFFER_STANDARD); |
| 318 EXPECT_EQ(fake_bitmap_1_size, gfx::Size(image_1.width(), image_1.height())); |
| 319 unsigned int* pixels_1 = reinterpret_cast<unsigned int*>(image_1.getPixels()); |
| 320 for (int i = 0; i < fake_bitmap_1_size.width(); ++i) { |
| 321 for (int j = 0; j < fake_bitmap_1_size.height(); ++j) { |
| 322 int id = i * fake_bitmap_1_size.height() + j; |
| 323 EXPECT_EQ(fake_bitmap_1[id], pixels_1[id]); |
| 324 } |
| 325 } |
| 326 |
| 327 // Test second bitmap |
| 328 unsigned int fake_bitmap_2[] = { |
| 329 0x46155189, 0xF6A55C8D, |
| 330 0x79845674, 0xFA57BD89, |
| 331 0x78FD46AE, 0x87C64F5A, |
| 332 0x36EDC5AF, 0x4378F568, |
| 333 0x91E9F63A, 0xC31EA14F, |
| 334 0x69AB32DF, 0x643A3FD1, |
| 335 0xA6DF041D, 0x83046278, |
| 336 }; |
| 337 gfx::Size fake_bitmap_2_size(7, 2); |
| 338 { |
| 339 ScopedClipboardWriter clipboard_writer(&clipboard); |
| 340 clipboard_writer.WriteBitmapFromPixels(fake_bitmap_2, fake_bitmap_2_size); |
| 341 } |
| 342 EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetBitmapFormatType(), |
| 343 Clipboard::BUFFER_STANDARD)); |
| 344 SkBitmap image_2 = clipboard.ReadImage(Clipboard::BUFFER_STANDARD); |
| 345 EXPECT_EQ(fake_bitmap_2_size, gfx::Size(image_2.width(), image_2.height())); |
| 346 unsigned int* pixels_2 = reinterpret_cast<unsigned int*>(image_2.getPixels()); |
| 347 for (int i = 0; i < fake_bitmap_2_size.width(); ++i) { |
| 348 for (int j = 0; j < fake_bitmap_2_size.height(); ++j) { |
| 349 int id = i * fake_bitmap_2_size.height() + j; |
| 350 EXPECT_EQ(fake_bitmap_2[id], pixels_2[id]); |
| 351 } |
| 352 } |
| 353 } |
| 354 #endif |
| 355 |
| 296 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_MACOSX)) | 356 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_MACOSX)) |
| 297 TEST_F(ClipboardTest, DataTest) { | 357 TEST_F(ClipboardTest, DataTest) { |
| 298 Clipboard clipboard; | 358 Clipboard clipboard; |
| 299 const char* kFormat = "chromium/x-test-format"; | 359 const char* kFormat = "chromium/x-test-format"; |
| 300 std::string payload("test string"); | 360 std::string payload("test string"); |
| 301 Pickle write_pickle; | 361 Pickle write_pickle; |
| 302 write_pickle.WriteString(payload); | 362 write_pickle.WriteString(payload); |
| 303 | 363 |
| 304 { | 364 { |
| 305 ScopedClipboardWriter clipboard_writer(&clipboard); | 365 ScopedClipboardWriter clipboard_writer(&clipboard); |
| 306 clipboard_writer.WritePickledData(write_pickle, kFormat); | 366 clipboard_writer.WritePickledData(write_pickle, kFormat); |
| 307 } | 367 } |
| 308 | 368 |
| 309 ASSERT_TRUE(clipboard.IsFormatAvailableByString( | 369 ASSERT_TRUE(clipboard.IsFormatAvailableByString( |
| 310 kFormat, Clipboard::BUFFER_STANDARD)); | 370 kFormat, Clipboard::BUFFER_STANDARD)); |
| 311 std::string output; | 371 std::string output; |
| 312 clipboard.ReadData(kFormat, &output); | 372 clipboard.ReadData(kFormat, &output); |
| 313 ASSERT_FALSE(output.empty()); | 373 ASSERT_FALSE(output.empty()); |
| 314 | 374 |
| 315 Pickle read_pickle(output.data(), output.size()); | 375 Pickle read_pickle(output.data(), output.size()); |
| 316 void* iter = NULL; | 376 void* iter = NULL; |
| 317 std::string unpickled_string; | 377 std::string unpickled_string; |
| 318 ASSERT_TRUE(read_pickle.ReadString(&iter, &unpickled_string)); | 378 ASSERT_TRUE(read_pickle.ReadString(&iter, &unpickled_string)); |
| 319 EXPECT_EQ(payload, unpickled_string); | 379 EXPECT_EQ(payload, unpickled_string); |
| 320 } | 380 } |
| 381 |
| 382 TEST_F(ClipboardTest, MultipleDataTest) { |
| 383 Clipboard clipboard; |
| 384 const char* kFormat1 = "chromium/x-test-format1"; |
| 385 std::string payload1("test string1"); |
| 386 Pickle write_pickle1; |
| 387 write_pickle1.WriteString(payload1); |
| 388 |
| 389 const char* kFormat2 = "chromium/x-test-format2"; |
| 390 std::string payload2("test string2"); |
| 391 Pickle write_pickle2; |
| 392 write_pickle2.WriteString(payload2); |
| 393 |
| 394 { |
| 395 ScopedClipboardWriter clipboard_writer(&clipboard); |
| 396 clipboard_writer.WritePickledData(write_pickle1, kFormat1); |
| 397 // overwrite the previous pickle for fun |
| 398 clipboard_writer.WritePickledData(write_pickle2, kFormat2); |
| 399 } |
| 400 |
| 401 ASSERT_TRUE(clipboard.IsFormatAvailableByString( |
| 402 kFormat2, Clipboard::BUFFER_STANDARD)); |
| 403 |
| 404 // Check string 2. |
| 405 std::string output2; |
| 406 clipboard.ReadData(kFormat2, &output2); |
| 407 ASSERT_FALSE(output2.empty()); |
| 408 |
| 409 Pickle read_pickle2(output2.data(), output2.size()); |
| 410 void* iter2 = NULL; |
| 411 std::string unpickled_string2; |
| 412 ASSERT_TRUE(read_pickle2.ReadString(&iter2, &unpickled_string2)); |
| 413 EXPECT_EQ(payload2, unpickled_string2); |
| 414 |
| 415 { |
| 416 ScopedClipboardWriter clipboard_writer(&clipboard); |
| 417 clipboard_writer.WritePickledData(write_pickle2, kFormat2); |
| 418 // overwrite the previous pickle for fun |
| 419 clipboard_writer.WritePickledData(write_pickle1, kFormat1); |
| 420 } |
| 421 |
| 422 ASSERT_TRUE(clipboard.IsFormatAvailableByString( |
| 423 kFormat1, Clipboard::BUFFER_STANDARD)); |
| 424 |
| 425 // Check string 1. |
| 426 std::string output1; |
| 427 clipboard.ReadData(kFormat1, &output1); |
| 428 ASSERT_FALSE(output1.empty()); |
| 429 |
| 430 Pickle read_pickle1(output1.data(), output1.size()); |
| 431 void* iter1 = NULL; |
| 432 std::string unpickled_string1; |
| 433 ASSERT_TRUE(read_pickle1.ReadString(&iter1, &unpickled_string1)); |
| 434 EXPECT_EQ(payload1, unpickled_string1); |
| 435 } |
| 321 #endif | 436 #endif |
| 322 | 437 |
| 323 #if defined(OS_WIN) // Windows only tests. | 438 #if defined(OS_WIN) // Windows only tests. |
| 324 TEST_F(ClipboardTest, HyperlinkTest) { | 439 TEST_F(ClipboardTest, HyperlinkTest) { |
| 325 Clipboard clipboard; | 440 Clipboard clipboard; |
| 326 | 441 |
| 327 const std::string kTitle("The Example Company"); | 442 const std::string kTitle("The Example Company"); |
| 328 const std::string kUrl("http://www.example.com/"); | 443 const std::string kUrl("http://www.example.com/"); |
| 329 const std::string kExpectedHtml("<a href=\"http://www.example.com/\">" | 444 const std::string kExpectedHtml("<a href=\"http://www.example.com/\">" |
| 330 "The Example Company</a>"); | 445 "The Example Company</a>"); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar"); | 544 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar"); |
| 430 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar"); | 545 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar"); |
| 431 writer.WriteWebSmartPaste(); | 546 writer.WriteWebSmartPaste(); |
| 432 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData. | 547 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData. |
| 433 } | 548 } |
| 434 | 549 |
| 435 // Passes if we don't crash. | 550 // Passes if we don't crash. |
| 436 } | 551 } |
| 437 | 552 |
| 438 } // namespace ui | 553 } // namespace ui |
| OLD | NEW |