diff options
-rw-r--r-- | base/base.gyp | 8 | ||||
-rw-r--r-- | base/mac_util.h | 8 | ||||
-rw-r--r-- | base/mac_util.mm | 19 | ||||
-rw-r--r-- | base/mac_util_unittest.cc | 27 | ||||
-rw-r--r-- | base/mac_util_unittest.mm | 59 |
5 files changed, 86 insertions, 35 deletions
diff --git a/base/base.gyp b/base/base.gyp index d948e84..3688cc6 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -586,7 +586,7 @@ 'leak_tracker_unittest.cc', 'linked_list_unittest.cc', 'linked_ptr_unittest.cc', - 'mac_util_unittest.cc', + 'mac_util_unittest.mm', 'message_loop_unittest.cc', 'message_pump_glib_unittest.cc', 'object_watcher_unittest.cc', @@ -648,6 +648,7 @@ ], 'conditions': [ ['OS == "linux" or OS == "freebsd"', { + 'sources/': [ ['exclude', '\\.mm?$' ] ], 'sources!': [ 'file_version_info_unittest.cc', 'worker_pool_linux_unittest.cc', @@ -662,11 +663,6 @@ 'message_pump_glib_unittest.cc', ] }], - ['OS != "mac"', { - 'sources!': [ - 'mac_util_unittest.cc', - ], - }], # This is needed to trigger the dll copy step on windows. # TODO(mark): This should not be necessary. ['OS == "win"', { diff --git a/base/mac_util.h b/base/mac_util.h index fc886e54..3c99b2a 100644 --- a/base/mac_util.h +++ b/base/mac_util.h @@ -1,4 +1,4 @@ -// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2008-2009 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. @@ -7,13 +7,16 @@ #include <Carbon/Carbon.h> #include <string> +#include <vector> class FilePath; #ifdef __OBJC__ @class NSBundle; +@class NSWindow; #else class NSBundle; +class NSWindow; #endif namespace mac_util { @@ -72,6 +75,9 @@ void RequestFullScreen(); // this will show the menu bar. Must be called on main thread. void ReleaseFullScreen(); +// Pulls a snapshot of the entire browser into png_representation. +void GrabWindowSnapshot(NSWindow* window, + std::vector<unsigned char>* png_representation); } // namespace mac_util #endif // BASE_MAC_UTIL_H_ diff --git a/base/mac_util.mm b/base/mac_util.mm index 772fe16b..3ffab82 100644 --- a/base/mac_util.mm +++ b/base/mac_util.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2008-2009 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. @@ -161,4 +161,21 @@ void ReleaseFullScreen() { SetSystemUIMode(kUIModeNormal, 0); } +void GrabWindowSnapshot(NSWindow* window, + std::vector<unsigned char>* png_representation) { + // Make sure to grab the "window frame" view so we get current tab + + // tabstrip. + NSView* view = [[window contentView] superview]; + NSBitmapImageRep* rep = + [view bitmapImageRepForCachingDisplayInRect:[view bounds]]; + [view cacheDisplayInRect:[view bounds] toBitmapImageRep:rep]; + NSData* data = [rep representationUsingType:NSPNGFileType properties:nil]; + const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); + NSUInteger length = [data length]; + if (buf != NULL && length > 0){ + png_representation->assign(buf, buf + length); + DCHECK(png_representation->size() > 0); + } +} + } // namespace mac_util diff --git a/base/mac_util_unittest.cc b/base/mac_util_unittest.cc deleted file mode 100644 index 18b8b5e..0000000 --- a/base/mac_util_unittest.cc +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2008 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. - -#include "base/mac_util.h" - -#include <ApplicationServices/ApplicationServices.h> - -#include "base/file_path.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/platform_test.h" - -typedef PlatformTest MacUtilTest; - -TEST_F(MacUtilTest, TestFSRef) { - FSRef ref; - std::string path("/System/Library"); - - ASSERT_TRUE(mac_util::FSRefFromPath(path, &ref)); - EXPECT_EQ(path, mac_util::PathFromFSRef(ref)); -} - -TEST_F(MacUtilTest, TestLibraryPath) { - FilePath library_dir = mac_util::GetUserLibraryPath(); - // Make sure the string isn't empty. - EXPECT_FALSE(library_dir.value().empty()); -} diff --git a/base/mac_util_unittest.mm b/base/mac_util_unittest.mm new file mode 100644 index 0000000..162a746 --- /dev/null +++ b/base/mac_util_unittest.mm @@ -0,0 +1,59 @@ +// Copyright (c) 2008-2009 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 <Cocoa/Cocoa.h> +#include <vector> + +#include "base/mac_util.h" + +#include "base/file_path.h" +#include "base/scoped_nsobject.h" +#include "base/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +typedef PlatformTest MacUtilTest; + +TEST_F(MacUtilTest, TestFSRef) { + FSRef ref; + std::string path("/System/Library"); + + ASSERT_TRUE(mac_util::FSRefFromPath(path, &ref)); + EXPECT_EQ(path, mac_util::PathFromFSRef(ref)); +} + +TEST_F(MacUtilTest, TestLibraryPath) { + FilePath library_dir = mac_util::GetUserLibraryPath(); + // Make sure the string isn't empty. + EXPECT_FALSE(library_dir.value().empty()); +} + +TEST_F(MacUtilTest, TestGrabWindowSnapshot) { + // Launch a test window so we can take a snapshot. + [NSApplication sharedApplication]; + NSRect frame = NSMakeRect(0, 0, 400, 400); + scoped_nsobject<NSWindow> window( + [[NSWindow alloc] initWithContentRect:frame + styleMask:NSBorderlessWindowMask + backing:NSBackingStoreBuffered + defer:NO]); + [window setBackgroundColor:[NSColor whiteColor]]; + [window makeKeyAndOrderFront:NSApp]; + + scoped_ptr<std::vector<unsigned char> > png_representation( + new std::vector<unsigned char>); + mac_util::GrabWindowSnapshot(window, png_representation.get()); + + // Copy png back into NSData object so we can make sure we grabbed a png. + scoped_nsobject<NSData> image_data( + [[NSData alloc] initWithBytes:&(*png_representation)[0] + length:png_representation->size()]); + NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:image_data.get()]; + EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]); + EXPECT_TRUE(CGImageGetWidth([rep CGImage]) == 400); + NSColor* color = [rep colorAtX:200 y:200]; + CGFloat red = 0, green = 0, blue = 0, alpha = 0; + [color getRed:&red green:&green blue:&blue alpha:&alpha]; + EXPECT_GE(red + green + blue, 3.0); +} |