| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "base/memory/scoped_nsobject.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "testing/platform_test.h" | |
| 12 #include "ui/gfx/rect.h" | |
| 13 | |
| 14 #if !defined(MAC_OS_X_VERSION_10_7) || \ | |
| 15 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | |
| 16 | |
| 17 @interface NSWindow (LionAPI) | |
| 18 - (CGFloat)backingScaleFactor; | |
| 19 @end | |
| 20 | |
| 21 #endif // 10.7 | |
| 22 | |
| 23 namespace chrome { | |
| 24 namespace { | |
| 25 | |
| 26 typedef PlatformTest GrabWindowSnapshotTest; | |
| 27 | |
| 28 TEST_F(GrabWindowSnapshotTest, TestGrabWindowSnapshot) { | |
| 29 // Launch a test window so we can take a snapshot. | |
| 30 NSRect frame = NSMakeRect(0, 0, 400, 400); | |
| 31 scoped_nsobject<NSWindow> window( | |
| 32 [[NSWindow alloc] initWithContentRect:frame | |
| 33 styleMask:NSBorderlessWindowMask | |
| 34 backing:NSBackingStoreBuffered | |
| 35 defer:NO]); | |
| 36 [window setBackgroundColor:[NSColor whiteColor]]; | |
| 37 [window makeKeyAndOrderFront:NSApp]; | |
| 38 | |
| 39 scoped_ptr<std::vector<unsigned char> > png_representation( | |
| 40 new std::vector<unsigned char>); | |
| 41 gfx::Rect bounds = gfx::Rect(0, 0, frame.size.width, frame.size.height); | |
| 42 EXPECT_TRUE(internal::GrabWindowSnapshot(window, png_representation.get(), | |
| 43 bounds)); | |
| 44 | |
| 45 // Copy png back into NSData object so we can make sure we grabbed a png. | |
| 46 scoped_nsobject<NSData> image_data( | |
| 47 [[NSData alloc] initWithBytes:&(*png_representation)[0] | |
| 48 length:png_representation->size()]); | |
| 49 NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:image_data.get()]; | |
| 50 EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]); | |
| 51 CGFloat scaleFactor = 1.0f; | |
| 52 if ([window respondsToSelector:@selector(backingScaleFactor)]) | |
| 53 scaleFactor = [window backingScaleFactor]; | |
| 54 EXPECT_EQ(400 * scaleFactor, CGImageGetWidth([rep CGImage])); | |
| 55 NSColor* color = [rep colorAtX:200 * scaleFactor y:200 * scaleFactor]; | |
| 56 CGFloat red = 0, green = 0, blue = 0, alpha = 0; | |
| 57 [color getRed:&red green:&green blue:&blue alpha:&alpha]; | |
| 58 EXPECT_GE(red + green + blue, 3.0); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 } // namespace chrome | |
| OLD | NEW |