| OLD | NEW |
| 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 "ui/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/mac/mac_util.h" |
| 11 #include "base/mac/scoped_nsobject.h" | 12 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 13 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 15 #include "ui/gfx/geometry/rect.h" |
| 16 #include "ui/gfx/image/image.h" |
| 17 #import "ui/gfx/test/ui_cocoa_test_helper.h" |
| 15 | 18 |
| 16 namespace ui { | 19 namespace ui { |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 typedef PlatformTest GrabWindowSnapshotTest; | 22 typedef CocoaTest GrabWindowSnapshotTest; |
| 20 | 23 |
| 21 TEST_F(GrabWindowSnapshotTest, TestGrabWindowSnapshot) { | 24 TEST_F(GrabWindowSnapshotTest, TestGrabWindowSnapshot) { |
| 25 // TODO(https://crbug.com/685088): This test fails on MacOS 10.11 and above. |
| 26 if (base::mac::IsAtLeastOS10_11()) |
| 27 return; |
| 28 |
| 22 // Launch a test window so we can take a snapshot. | 29 // Launch a test window so we can take a snapshot. |
| 23 NSRect frame = NSMakeRect(0, 0, 400, 400); | 30 NSRect frame = NSMakeRect(0, 0, 400, 400); |
| 24 base::scoped_nsobject<NSWindow> window( | 31 NSWindow* window = test_window(); |
| 25 [[NSWindow alloc] initWithContentRect:frame | 32 [window setFrame:frame display:false]; |
| 26 styleMask:NSBorderlessWindowMask | |
| 27 backing:NSBackingStoreBuffered | |
| 28 defer:NO]); | |
| 29 [window setBackgroundColor:[NSColor whiteColor]]; | 33 [window setBackgroundColor:[NSColor whiteColor]]; |
| 30 [window makeKeyAndOrderFront:NSApp]; | 34 [window makeKeyAndOrderFront:NSApp]; |
| 35 [window display]; |
| 31 | 36 |
| 32 std::unique_ptr<std::vector<unsigned char>> png_representation( | 37 gfx::Image image; |
| 33 new std::vector<unsigned char>); | |
| 34 gfx::Rect bounds = gfx::Rect(0, 0, frame.size.width, frame.size.height); | 38 gfx::Rect bounds = gfx::Rect(0, 0, frame.size.width, frame.size.height); |
| 35 EXPECT_TRUE(ui::GrabWindowSnapshot(window, png_representation.get(), | 39 EXPECT_TRUE(ui::GrabWindowSnapshot(window, bounds, &image)); |
| 36 bounds)); | |
| 37 | 40 |
| 38 // Copy png back into NSData object so we can make sure we grabbed a png. | 41 NSImage* nsImage = image.ToNSImage(); |
| 39 base::scoped_nsobject<NSData> image_data( | 42 CGImageRef cgImage = |
| 40 [[NSData alloc] initWithBytes:&(*png_representation)[0] | 43 [nsImage CGImageForProposedRect:nil context:nil hints:nil]; |
| 41 length:png_representation->size()]); | 44 base::scoped_nsobject<NSBitmapImageRep> rep( |
| 42 NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:image_data.get()]; | 45 [[NSBitmapImageRep alloc] initWithCGImage:cgImage]); |
| 43 EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]); | 46 EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]); |
| 44 CGFloat scaleFactor = 1.0f; | 47 CGFloat scaleFactor = 1.0f; |
| 45 if ([window respondsToSelector:@selector(backingScaleFactor)]) | 48 if ([window respondsToSelector:@selector(backingScaleFactor)]) |
| 46 scaleFactor = [window backingScaleFactor]; | 49 scaleFactor = [window backingScaleFactor]; |
| 47 EXPECT_EQ(400 * scaleFactor, CGImageGetWidth([rep CGImage])); | 50 EXPECT_EQ(400 * scaleFactor, CGImageGetWidth([rep CGImage])); |
| 48 NSColor* color = [rep colorAtX:200 * scaleFactor y:200 * scaleFactor]; | 51 NSColor* color = [rep colorAtX:200 * scaleFactor y:200 * scaleFactor]; |
| 49 CGFloat red = 0, green = 0, blue = 0, alpha = 0; | 52 CGFloat red = 0, green = 0, blue = 0, alpha = 0; |
| 50 [color getRed:&red green:&green blue:&blue alpha:&alpha]; | 53 [color getRed:&red green:&green blue:&blue alpha:&alpha]; |
| 51 EXPECT_GE(red + green + blue, 3.0); | 54 EXPECT_GE(red + green + blue, 3.0); |
| 52 } | 55 } |
| 53 | 56 |
| 54 } // namespace | 57 } // namespace |
| 55 } // namespace ui | 58 } // namespace ui |
| OLD | NEW |