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

Unified Diff: ui/base/clipboard/clipboard_mac_unittest.mm

Issue 2271203005: mac: Add tests for reading images from NSPasteboard. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi. Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/base/clipboard/clipboard_mac.mm ('k') | ui/base/ui_base_tests.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/clipboard/clipboard_mac_unittest.mm
diff --git a/ui/base/clipboard/clipboard_mac_unittest.mm b/ui/base/clipboard/clipboard_mac_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..cd69e7433c8baeb993b91f4a20756c3b19b8a17c
--- /dev/null
+++ b/ui/base/clipboard/clipboard_mac_unittest.mm
@@ -0,0 +1,81 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ui/base/clipboard/clipboard_mac.h"
+
+#include "base/mac/scoped_cftyperef.h"
+#include "base/mac/scoped_nsobject.h"
+#include "base/memory/free_deleter.h"
+#include "base/memory/ref_counted.h"
+#include "testing/platform_test.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "ui/base/clipboard/clipboard_types.h"
+#include "ui/base/clipboard/clipboard_util_mac.h"
+
+namespace ui {
+
+class ClipboardMacTest : public PlatformTest {
+ public:
+ ClipboardMacTest() { }
+
+ base::scoped_nsobject<NSImage> CreateImage(int32_t width,
+ int32_t height,
+ bool retina) {
+ int32_t pixel_width = retina ? width * 2 : width;
+ int32_t pixel_height = retina ? height * 2 : height;
+
+ // It seems more natural to create an NSBitmapImageRep and set it as the
+ // representation for an NSImage. This doesn't work, because when the
+ // result is written, and then read from an NSPasteboard, the new NSImage
+ // loses its "retina-ness".
+ std::unique_ptr<uint8_t, base::FreeDeleter> buffer(
+ static_cast<uint8_t*>(calloc(pixel_width * pixel_height * 4)));
Avi (use Gerrit) 2016/08/26 18:00:32 calloc takes two parameters, not one.
erikchen 2016/08/26 18:05:45 Done.
+ base::ScopedCFTypeRef<CGDataProviderRef> provider(
+ CGDataProviderCreateWithData(
+ nullptr, buffer.get(), (pixel_width * pixel_height * 4), nullptr));
+ base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
+ CGColorSpaceCreateWithName(kCGColorSpaceSRGB));
+ base::ScopedCFTypeRef<CGImageRef> image_ref(
+ CGImageCreate(pixel_width, pixel_height, 8, 32, 4 * pixel_width,
+ color_space.get(), kCGBitmapByteOrderDefault,
+ provider.get(), nullptr, NO, kCGRenderingIntentDefault));
+ return base::scoped_nsobject<NSImage>([[NSImage alloc]
+ initWithCGImage:image_ref.get()
+ size:NSMakeSize(width, height)]);
+ }
+};
+
+TEST_F(ClipboardMacTest, ReadImageRetina) {
+ int32_t width = 99;
+ int32_t height = 101;
+ scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard;
+ base::scoped_nsobject<NSImage> image = CreateImage(width, height, true);
+ [pasteboard->get() writeObjects:@[ image.get() ]];
+
+ ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
+ ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard);
+
+ SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE,
+ pasteboard->get());
+ EXPECT_EQ(width, bitmap.width());
+ EXPECT_EQ(height, bitmap.height());
+}
+
+TEST_F(ClipboardMacTest, ReadImageNonRetina) {
+ int32_t width = 99;
+ int32_t height = 101;
+ scoped_refptr<ui::UniquePasteboard> pasteboard = new ui::UniquePasteboard;
+ base::scoped_nsobject<NSImage> image = CreateImage(width, height, false);
+ [pasteboard->get() writeObjects:@[ image.get() ]];
+
+ ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
+ ui::ClipboardMac* clipboard_mac = static_cast<ui::ClipboardMac*>(clipboard);
+
+ SkBitmap bitmap = clipboard_mac->ReadImage(ui::CLIPBOARD_TYPE_COPY_PASTE,
+ pasteboard->get());
+ EXPECT_EQ(width, bitmap.width());
+ EXPECT_EQ(height, bitmap.height());
+}
+
+} // namespace ui
« no previous file with comments | « ui/base/clipboard/clipboard_mac.mm ('k') | ui/base/ui_base_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698