OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #import "ui/base/clipboard/clipboard_mac.h" | 5 #import "ui/base/clipboard/clipboard_mac.h" |
6 | 6 |
| 7 #import <AppKit/AppKit.h> |
| 8 |
7 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
8 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
9 #include "base/memory/free_deleter.h" | 11 #include "base/memory/free_deleter.h" |
10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
11 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/base/clipboard/clipboard_types.h" | 15 #include "ui/base/clipboard/clipboard_types.h" |
14 #include "ui/base/clipboard/clipboard_util_mac.h" | 16 #include "ui/base/clipboard/clipboard_util_mac.h" |
15 | 17 |
| 18 @interface RedView : NSView |
| 19 @end |
| 20 @implementation RedView |
| 21 - (void)drawRect:(NSRect)dirtyRect { |
| 22 [[NSColor redColor] setFill]; |
| 23 NSRectFill(dirtyRect); |
| 24 [super drawRect:dirtyRect]; |
| 25 } |
| 26 @end |
| 27 |
16 namespace ui { | 28 namespace ui { |
17 | 29 |
18 class ClipboardMacTest : public PlatformTest { | 30 class ClipboardMacTest : public PlatformTest { |
19 public: | 31 public: |
20 ClipboardMacTest() { } | 32 ClipboardMacTest() { } |
21 | 33 |
22 base::scoped_nsobject<NSImage> CreateImage(int32_t width, | 34 base::scoped_nsobject<NSImage> CreateImage(int32_t width, |
23 int32_t height, | 35 int32_t height, |
24 bool retina) { | 36 bool retina) { |
25 int32_t pixel_width = retina ? width * 2 : width; | 37 int32_t pixel_width = retina ? width * 2 : width; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 83 |
72 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | 84 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
73 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); | 85 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); |
74 | 86 |
75 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, | 87 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, |
76 pasteboard->get()); | 88 pasteboard->get()); |
77 EXPECT_EQ(width, bitmap.width()); | 89 EXPECT_EQ(width, bitmap.width()); |
78 EXPECT_EQ(height, bitmap.height()); | 90 EXPECT_EQ(height, bitmap.height()); |
79 } | 91 } |
80 | 92 |
| 93 TEST_F(ClipboardMacTest, EmptyImage) { |
| 94 base::scoped_nsobject<NSImage> image([[NSImage alloc] init]); |
| 95 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; |
| 96 [pasteboard->get() writeObjects:@[ image.get() ]]; |
| 97 |
| 98 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 99 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); |
| 100 |
| 101 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 102 pasteboard->get()); |
| 103 EXPECT_EQ(0, bitmap.width()); |
| 104 EXPECT_EQ(0, bitmap.height()); |
| 105 } |
| 106 |
| 107 TEST_F(ClipboardMacTest, PDFImage) { |
| 108 int32_t width = 99; |
| 109 int32_t height = 101; |
| 110 NSRect frame = NSMakeRect(0, 0, width, height); |
| 111 |
| 112 // This seems like a round-about way of getting a NSPDFImageRep to shove into |
| 113 // an NSPasteboard. However, I haven't found any other way of generating a |
| 114 // "PDF" image that makes NSPasteboard happy. |
| 115 base::scoped_nsobject<NSView> v([[RedView alloc] initWithFrame:frame]); |
| 116 NSData* data = [v dataWithPDFInsideRect:frame]; |
| 117 |
| 118 scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard; |
| 119 [pasteboard->get() setData:data forType:NSPasteboardTypePDF]; |
| 120 |
| 121 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 122 ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard); |
| 123 |
| 124 SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE, |
| 125 pasteboard->get()); |
| 126 EXPECT_EQ(width, bitmap.width()); |
| 127 EXPECT_EQ(height, bitmap.height()); |
| 128 } |
| 129 |
81 } // namespace ui | 130 } // namespace ui |
OLD | NEW |