// 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 #include #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 window( [[NSWindow alloc] initWithContentRect:frame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]); [window setBackgroundColor:[NSColor whiteColor]]; [window makeKeyAndOrderFront:NSApp]; scoped_ptr > png_representation( new std::vector); mac_util::GrabWindowSnapshot(window, png_representation.get()); // Copy png back into NSData object so we can make sure we grabbed a png. scoped_nsobject 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); } TEST_F(MacUtilTest, TestGetAppBundlePath) { FilePath out; // Make sure it doesn't crash. out = mac_util::GetAppBundlePath(FilePath()); EXPECT_TRUE(out.empty()); // Some more invalid inputs. const char* invalid_inputs[] = { "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux", "foo/bar./bazquux", "foo/.app", "//foo", }; for (size_t i = 0; i < arraysize(invalid_inputs); i++) { out = mac_util::GetAppBundlePath(FilePath(invalid_inputs[i])); EXPECT_TRUE(out.empty()) << "loop: " << i; } // Some valid inputs; this and |expected_outputs| should be in sync. struct { const char *in; const char *expected_out; } valid_inputs[] = { { "FooBar.app/", "FooBar.app" }, { "/FooBar.app", "/FooBar.app" }, { "/FooBar.app/", "/FooBar.app" }, { "//FooBar.app", "//FooBar.app" }, { "/Foo/Bar.app", "/Foo/Bar.app" }, { "/Foo/Bar.app/", "/Foo/Bar.app" }, { "/F/B.app", "/F/B.app" }, { "/F/B.app/", "/F/B.app" }, { "/Foo/Bar.app/baz", "/Foo/Bar.app" }, { "/Foo/Bar.app/baz/", "/Foo/Bar.app" }, { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" }, { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper", "/Applications/Google Foo.app" }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) { out = mac_util::GetAppBundlePath(FilePath(valid_inputs[i].in)); EXPECT_FALSE(out.empty()) << "loop: " << i; EXPECT_STREQ(valid_inputs[i].expected_out, out.value().c_str()) << "loop: " << i; } }