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

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

Issue 574273002: Rewrite clipboard write IPC handling to be easier to understand. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 6 years, 2 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
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"
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); 324 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
325 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); 325 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
326 326
327 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) 327 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
328 ascii_text.clear(); 328 ascii_text.clear();
329 clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text); 329 clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text);
330 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); 330 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
331 #endif 331 #endif
332 } 332 }
333 333
334 // TODO(dcheng): The tests for copying to the clipboard also test IPC
335 // interaction... consider moving them to a different layer so we can
336 // consolidate the validation logic.
337 // Note that |bitmap_data| is not premultiplied!
338 static void TestBitmapWrite(Clipboard* clipboard, 334 static void TestBitmapWrite(Clipboard* clipboard,
339 const uint32* bitmap_data, 335 const gfx::Size& size,
340 size_t bitmap_data_size, 336 const uint32* bitmap_data) {
341 const gfx::Size& size) { 337 {
342 // Create shared memory region. 338 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
343 base::SharedMemory shared_buf; 339 SkBitmap bitmap;
344 ASSERT_TRUE(shared_buf.CreateAndMapAnonymous(bitmap_data_size)); 340 ASSERT_TRUE(bitmap.setInfo(
345 memcpy(shared_buf.memory(), bitmap_data, bitmap_data_size); 341 SkImageInfo::MakeN32Premul(size.width(), size.height())));
346 // CBF_SMBITMAP expects premultiplied bitmap data so do that now. 342 bitmap.setPixels(
347 uint32* pixel_buffer = static_cast<uint32*>(shared_buf.memory()); 343 const_cast<void*>(reinterpret_cast<const void*>(bitmap_data)));
348 for (int j = 0; j < size.height(); ++j) { 344 scw.WriteImage(bitmap);
349 for (int i = 0; i < size.width(); ++i) {
350 uint32& pixel = pixel_buffer[i + j * size.width()];
351 pixel = SkPreMultiplyColor(pixel);
352 }
353 } 345 }
354 base::SharedMemoryHandle handle_to_share;
355 base::ProcessHandle current_process = base::kNullProcessHandle;
356 #if defined(OS_WIN)
357 current_process = GetCurrentProcess();
358 #endif
359 shared_buf.ShareToProcess(current_process, &handle_to_share);
360 ASSERT_TRUE(shared_buf.Unmap());
361
362 // Setup data for clipboard().
363 Clipboard::ObjectMapParam placeholder_param;
364 Clipboard::ObjectMapParam size_param;
365 const char* size_data = reinterpret_cast<const char*>(&size);
366 for (size_t i = 0; i < sizeof(size); ++i)
367 size_param.push_back(size_data[i]);
368
369 Clipboard::ObjectMapParams params;
370 params.push_back(placeholder_param);
371 params.push_back(size_param);
372
373 Clipboard::ObjectMap objects;
374 objects[Clipboard::CBF_SMBITMAP] = params;
375 ASSERT_TRUE(Clipboard::ReplaceSharedMemHandle(
376 &objects, handle_to_share, current_process));
377
378 ClipboardTest::WriteObjectsToClipboard(clipboard, objects);
379 346
380 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(), 347 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(),
381 CLIPBOARD_TYPE_COPY_PASTE)); 348 CLIPBOARD_TYPE_COPY_PASTE));
382 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE); 349 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE);
383 EXPECT_EQ(size, gfx::Size(image.width(), image.height())); 350 EXPECT_EQ(size, gfx::Size(image.width(), image.height()));
384 SkAutoLockPixels image_lock(image); 351 SkAutoLockPixels image_lock(image);
385 for (int j = 0; j < image.height(); ++j) { 352 for (int j = 0; j < image.height(); ++j) {
386 const uint32* row_address = image.getAddr32(0, j); 353 const uint32* row_address = image.getAddr32(0, j);
387 for (int i = 0; i < image.width(); ++i) { 354 for (int i = 0; i < image.width(); ++i) {
388 int offset = i + j * image.width(); 355 int offset = i + j * image.width();
389 uint32 pixel = SkPreMultiplyColor(bitmap_data[offset]); 356 EXPECT_EQ(bitmap_data[offset], row_address[i]) << "i = " << i
390 EXPECT_EQ(pixel, row_address[i]) 357 << ", j = " << j;
391 << "i = " << i << ", j = " << j;
392 } 358 }
393 } 359 }
394 } 360 }
395 361
396 TEST_F(ClipboardTest, SharedBitmapTest) { 362 TEST_F(ClipboardTest, SharedBitmapTest) {
397 const uint32 fake_bitmap_1[] = { 363 const uint32 fake_bitmap_1[] = {
398 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, 364 0x46061626, 0xf69f5988, 0x793f2937, 0xfa55b986,
399 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, 365 0x78772152, 0x87692a30, 0x36322a25, 0x4320401b,
400 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, 366 0x91848c21, 0xc3177b3c, 0x6946155c, 0x64171952,
401 }; 367 };
402 { 368 {
403 SCOPED_TRACE("first bitmap"); 369 SCOPED_TRACE("first bitmap");
404 TestBitmapWrite( 370 TestBitmapWrite(&clipboard(), gfx::Size(4, 3), fake_bitmap_1);
405 &clipboard(), fake_bitmap_1, sizeof(fake_bitmap_1), gfx::Size(4, 3));
406 } 371 }
407 372
408 const uint32 fake_bitmap_2[] = { 373 const uint32 fake_bitmap_2[] = {
409 0x46155189, 0xF6A55C8D, 374 0x46061626, 0xf69f5988,
410 0x79845674, 0xFA57BD89, 375 0x793f2937, 0xfa55b986,
411 0x78FD46AE, 0x87C64F5A, 376 0x78772152, 0x87692a30,
412 0x36EDC5AF, 0x4378F568, 377 0x36322a25, 0x4320401b,
413 0x91E9F63A, 0xC31EA14F, 378 0x91848c21, 0xc3177b3c,
414 0x69AB32DF, 0x643A3FD1, 379 0x6946155c, 0x64171952,
415 0xA6DF041D, 0x83046278, 380 0xa6910313, 0x8302323e,
416 }; 381 };
417 { 382 {
418 SCOPED_TRACE("second bitmap"); 383 SCOPED_TRACE("second bitmap");
419 TestBitmapWrite( 384 TestBitmapWrite(&clipboard(), gfx::Size(2, 7), fake_bitmap_2);
420 &clipboard(), fake_bitmap_2, sizeof(fake_bitmap_2), gfx::Size(2, 7));
421 } 385 }
422 } 386 }
423 387
424 namespace {
425 // A size class that just happens to have the same layout as gfx::Size!
426 struct UnsafeSize {
427 int width;
428 int height;
429 };
430 COMPILE_ASSERT(sizeof(UnsafeSize) == sizeof(gfx::Size),
431 UnsafeSize_must_be_same_size_as_gfx_Size);
432 } // namespace
433
434 TEST_F(ClipboardTest, SharedBitmapWithTwoNegativeSizes) {
435 Clipboard::ObjectMapParam placeholder_param;
436 void* crash_me = reinterpret_cast<void*>(57);
437 placeholder_param.resize(sizeof(crash_me));
438 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me));
439
440 Clipboard::ObjectMapParam size_param;
441 UnsafeSize size = {-100, -100};
442 size_param.resize(sizeof(size));
443 memcpy(&size_param.front(), &size, sizeof(size));
444
445 Clipboard::ObjectMapParams params;
446 params.push_back(placeholder_param);
447 params.push_back(size_param);
448
449 Clipboard::ObjectMap objects;
450 objects[Clipboard::CBF_SMBITMAP] = params;
451
452 WriteObjectsToClipboard(objects);
453 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(),
454 CLIPBOARD_TYPE_COPY_PASTE));
455 }
456
457 TEST_F(ClipboardTest, SharedBitmapWithOneNegativeSize) {
458 Clipboard::ObjectMapParam placeholder_param;
459 void* crash_me = reinterpret_cast<void*>(57);
460 placeholder_param.resize(sizeof(crash_me));
461 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me));
462
463 Clipboard::ObjectMapParam size_param;
464 UnsafeSize size = {-100, 100};
465 size_param.resize(sizeof(size));
466 memcpy(&size_param.front(), &size, sizeof(size));
467
468 Clipboard::ObjectMapParams params;
469 params.push_back(placeholder_param);
470 params.push_back(size_param);
471
472 Clipboard::ObjectMap objects;
473 objects[Clipboard::CBF_SMBITMAP] = params;
474
475 WriteObjectsToClipboard(objects);
476 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(),
477 CLIPBOARD_TYPE_COPY_PASTE));
478 }
479
480 TEST_F(ClipboardTest, BitmapWithSuperSize) {
481 Clipboard::ObjectMapParam placeholder_param;
482 void* crash_me = reinterpret_cast<void*>(57);
483 placeholder_param.resize(sizeof(crash_me));
484 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me));
485
486 Clipboard::ObjectMapParam size_param;
487 // Width just big enough that bytes per row won't fit in a 32-bit
488 // representation.
489 gfx::Size size(0x20000000, 1);
490 size_param.resize(sizeof(size));
491 memcpy(&size_param.front(), &size, sizeof(size));
492
493 Clipboard::ObjectMapParams params;
494 params.push_back(placeholder_param);
495 params.push_back(size_param);
496
497 Clipboard::ObjectMap objects;
498 objects[Clipboard::CBF_SMBITMAP] = params;
499
500 WriteObjectsToClipboard(objects);
501 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(),
502 CLIPBOARD_TYPE_COPY_PASTE));
503 }
504
505 TEST_F(ClipboardTest, BitmapWithSuperSize2) {
506 Clipboard::ObjectMapParam placeholder_param;
507 void* crash_me = reinterpret_cast<void*>(57);
508 placeholder_param.resize(sizeof(crash_me));
509 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me));
510
511 Clipboard::ObjectMapParam size_param;
512 // Width and height large enough that SkBitmap::getSize() will be truncated.
513 gfx::Size size(0x0fffffff, 0x0fffffff);
514 size_param.resize(sizeof(size));
515 memcpy(&size_param.front(), &size, sizeof(size));
516
517 Clipboard::ObjectMapParams params;
518 params.push_back(placeholder_param);
519 params.push_back(size_param);
520
521 Clipboard::ObjectMap objects;
522 objects[Clipboard::CBF_SMBITMAP] = params;
523
524 WriteObjectsToClipboard(objects);
525 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(),
526 CLIPBOARD_TYPE_COPY_PASTE));
527 }
528
529 TEST_F(ClipboardTest, DataTest) { 388 TEST_F(ClipboardTest, DataTest) {
530 const ui::Clipboard::FormatType kFormat = 389 const ui::Clipboard::FormatType kFormat =
531 ui::Clipboard::GetFormatType("chromium/x-test-format"); 390 ui::Clipboard::GetFormatType("chromium/x-test-format");
532 std::string payload("test string"); 391 std::string payload("test string");
533 Pickle write_pickle; 392 Pickle write_pickle;
534 write_pickle.WriteString(payload); 393 write_pickle.WriteString(payload);
535 394
536 { 395 {
537 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); 396 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
538 clipboard_writer.WritePickledData(write_pickle, kFormat); 397 clipboard_writer.WritePickledData(write_pickle, kFormat);
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 EXPECT_TRUE(clipboard().IsFormatAvailable( 650 EXPECT_TRUE(clipboard().IsFormatAvailable(
792 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); 651 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
793 652
794 // Make sure the text is what we inserted while simulating the other app 653 // Make sure the text is what we inserted while simulating the other app
795 std::string contents; 654 std::string contents;
796 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); 655 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents);
797 EXPECT_EQ(contents, new_value); 656 EXPECT_EQ(contents, new_value);
798 } 657 }
799 #endif 658 #endif
800 } // namespace ui 659 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698