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) { | |
Avi (use Gerrit)
2016/08/25 18:53:46
It feels weird to be creating an NSImage via this
erikchen
2016/08/26 17:52:31
That was the first thing I tried. It doesn't work
| |
25 int32_t pixel_width = retina ? width * 2 : width; | |
26 int32_t pixel_height = retina ? height * 2 : height; | |
27 std::unique_ptr<uint8_t, base::FreeDeleter> buffer( | |
28 static_cast<uint8_t*>(malloc(pixel_width * pixel_height * 4))); | |
29 base::ScopedCFTypeRef<CGDataProviderRef> provider( | |
30 CGDataProviderCreateWithData( | |
31 nullptr, buffer.get(), (pixel_width * pixel_height * 4), nullptr)); | |
Avi (use Gerrit)
2016/08/25 18:53:45
You never initialized the malloc memory, so won't
erikchen
2016/08/26 17:52:30
It didn't...but calloc it is!
| |
32 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( | |
33 CGColorSpaceCreateWithName(kCGColorSpaceSRGB)); | |
34 base::ScopedCFTypeRef<CGImageRef> image_ref( | |
35 CGImageCreate(pixel_width, pixel_height, 8, 32, 4 * pixel_width, | |
36 color_space.get(), kCGBitmapByteOrderDefault, | |
37 provider.get(), nullptr, NO, kCGRenderingIntentDefault)); | |
38 return base::scoped_nsobject<NSImage>([[NSImage alloc] | |
39 initWithCGImage:image_ref.get() | |
40 size:NSMakeSize(width, height)]); | |
41 } | |
42 }; | |
43 | |
44 TEST_F(ClipboardMacTest, ReadImageRetina) { | |
45 int32_t width = 99; | |
46 int32_t height = 101; | |
47 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; | |
48 base::scoped_nsobject<NSImage> image = CreateImage(width, height, true); | |
49 [pasteboard->get() writeObjects:@[ image.get() ]]; | |
50 | |
51 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | |
52 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); | |
53 | |
54 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, | |
55 pasteboard->get()); | |
56 EXPECT_EQ(width, bitmap.width()); | |
57 EXPECT_EQ(height, bitmap.height()); | |
58 } | |
59 | |
60 TEST_F(ClipboardMacTest, ReadImageNonRetina) { | |
61 int32_t width = 99; | |
62 int32_t height = 101; | |
63 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; | |
64 base::scoped_nsobject<NSImage> image = CreateImage(width, height, false); | |
65 [pasteboard->get() writeObjects:@[ image.get() ]]; | |
66 | |
67 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | |
68 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); | |
69 | |
70 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, | |
71 pasteboard->get()); | |
72 EXPECT_EQ(width, bitmap.width()); | |
73 EXPECT_EQ(height, bitmap.height()); | |
74 } | |
75 | |
76 } // namespace ui | |
OLD | NEW |