OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #import "ui/base/clipboard/clipboard_mac.h" |
| 6 |
| 7 #include "base/mac/scoped_cftyperef.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/memory/free_deleter.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "testing/platform_test.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "ui/base/clipboard/clipboard_types.h" |
| 14 #include "ui/base/clipboard/clipboard_util_mac.h" |
| 15 |
| 16 namespace ui { |
| 17 |
| 18 class ClipboardMacTest : public PlatformTest { |
| 19 public: |
| 20 ClipboardMacTest() { } |
| 21 |
| 22 base::scoped_nsobject<NSImage> CreateImage(int32_t width, |
| 23 int32_t height, |
| 24 bool retina) { |
| 25 int32_t pixel_width = retina ? width * 2 : width; |
| 26 int32_t pixel_height = retina ? height * 2 : height; |
| 27 |
| 28 // It seems more natural to create an NSBitmapImageRep and set it as the |
| 29 // representation for an NSImage. This doesn't work, because when the |
| 30 // result is written, and then read from an NSPasteboard, the new NSImage |
| 31 // loses its "retina-ness". |
| 32 std::unique_ptr<uint8_t, base::FreeDeleter> buffer( |
| 33 static_cast<uint8_t*>(calloc(pixel_width * pixel_height, 4))); |
| 34 base::ScopedCFTypeRef<CGDataProviderRef> provider( |
| 35 CGDataProviderCreateWithData( |
| 36 nullptr, buffer.get(), (pixel_width * pixel_height * 4), nullptr)); |
| 37 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
| 38 CGColorSpaceCreateWithName(kCGColorSpaceSRGB)); |
| 39 base::ScopedCFTypeRef<CGImageRef> image_ref( |
| 40 CGImageCreate(pixel_width, pixel_height, 8, 32, 4 * pixel_width, |
| 41 color_space.get(), kCGBitmapByteOrderDefault, |
| 42 provider.get(), nullptr, NO, kCGRenderingIntentDefault)); |
| 43 return base::scoped_nsobject<NSImage>([[NSImage alloc] |
| 44 initWithCGImage:image_ref.get() |
| 45 size:NSMakeSize(width, height)]); |
| 46 } |
| 47 }; |
| 48 |
| 49 TEST_F(ClipboardMacTest, ReadImageRetina) { |
| 50 int32_t width = 99; |
| 51 int32_t height = 101; |
| 52 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; |
| 53 base::scoped_nsobject<NSImage> image = CreateImage(width, height, true); |
| 54 [pasteboard->get() writeObjects:@[ image.get() ]]; |
| 55 |
| 56 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 57 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); |
| 58 |
| 59 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 60 pasteboard->get()); |
| 61 EXPECT_EQ(width, bitmap.width()); |
| 62 EXPECT_EQ(height, bitmap.height()); |
| 63 } |
| 64 |
| 65 TEST_F(ClipboardMacTest, ReadImageNonRetina) { |
| 66 int32_t width = 99; |
| 67 int32_t height = 101; |
| 68 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; |
| 69 base::scoped_nsobject<NSImage> image = CreateImage(width, height, false); |
| 70 [pasteboard->get() writeObjects:@[ image.get() ]]; |
| 71 |
| 72 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 73 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); |
| 74 |
| 75 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 76 pasteboard->get()); |
| 77 EXPECT_EQ(width, bitmap.width()); |
| 78 EXPECT_EQ(height, bitmap.height()); |
| 79 } |
| 80 |
| 81 } // namespace ui |
OLD | NEW |