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