1// Copyright (c) 2008-2009 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#import <Cocoa/Cocoa.h> 6#include <vector> 7 8#include "base/mac_util.h" 9 10#include "base/file_path.h" 11#include "base/file_util.h" 12#include "base/scoped_cftyperef.h" 13#include "base/scoped_nsobject.h" 14#include "base/scoped_ptr.h" 15#include "testing/gtest/include/gtest/gtest.h" 16#include "testing/platform_test.h" 17 18namespace mac_util { 19 20namespace { 21 22typedef PlatformTest MacUtilTest; 23 24TEST_F(MacUtilTest, TestFSRef) { 25 FSRef ref; 26 std::string path("/System/Library"); 27 28 ASSERT_TRUE(FSRefFromPath(path, &ref)); 29 EXPECT_EQ(path, PathFromFSRef(ref)); 30} 31 32TEST_F(MacUtilTest, GetUserDirectoryTest) { 33 // Try a few keys, make sure they come back with non-empty paths. 34 FilePath caches_dir; 35 EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir)); 36 EXPECT_FALSE(caches_dir.empty()); 37 38 FilePath application_support_dir; 39 EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory, 40 &application_support_dir)); 41 EXPECT_FALSE(application_support_dir.empty()); 42 43 FilePath library_dir; 44 EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir)); 45 EXPECT_FALSE(library_dir.empty()); 46} 47 48TEST_F(MacUtilTest, TestLibraryPath) { 49 FilePath library_dir = GetUserLibraryPath(); 50 // Make sure the string isn't empty. 51 EXPECT_FALSE(library_dir.value().empty()); 52} 53 54TEST_F(MacUtilTest, TestGrabWindowSnapshot) { 55 // Launch a test window so we can take a snapshot. 56 [NSApplication sharedApplication]; 57 NSRect frame = NSMakeRect(0, 0, 400, 400); 58 scoped_nsobject<NSWindow> window( 59 [[NSWindow alloc] initWithContentRect:frame 60 styleMask:NSBorderlessWindowMask 61 backing:NSBackingStoreBuffered 62 defer:NO]); 63 [window setBackgroundColor:[NSColor whiteColor]]; 64 [window makeKeyAndOrderFront:NSApp]; 65 66 scoped_ptr<std::vector<unsigned char> > png_representation( 67 new std::vector<unsigned char>); 68 GrabWindowSnapshot(window, png_representation.get()); 69 70 // Copy png back into NSData object so we can make sure we grabbed a png. 71 scoped_nsobject<NSData> image_data( 72 [[NSData alloc] initWithBytes:&(*png_representation)[0] 73 length:png_representation->size()]); 74 NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:image_data.get()]; 75 EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]); 76 EXPECT_TRUE(CGImageGetWidth([rep CGImage]) == 400); 77 NSColor* color = [rep colorAtX:200 y:200]; 78 CGFloat red = 0, green = 0, blue = 0, alpha = 0; 79 [color getRed:&red green:&green blue:&blue alpha:&alpha]; 80 EXPECT_GE(red + green + blue, 3.0); 81} 82 83TEST_F(MacUtilTest, TestGetAppBundlePath) { 84 FilePath out; 85 86 // Make sure it doesn't crash. 87 out = GetAppBundlePath(FilePath()); 88 EXPECT_TRUE(out.empty()); 89 90 // Some more invalid inputs. 91 const char* invalid_inputs[] = { 92 "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux", 93 "foo/bar./bazquux", "foo/.app", "//foo", 94 }; 95 for (size_t i = 0; i < arraysize(invalid_inputs); i++) { 96 out = GetAppBundlePath(FilePath(invalid_inputs[i])); 97 EXPECT_TRUE(out.empty()) << "loop: " << i; 98 } 99 100 // Some valid inputs; this and |expected_outputs| should be in sync. 101 struct { 102 const char *in; 103 const char *expected_out; 104 } valid_inputs[] = { 105 { "FooBar.app/", "FooBar.app" }, 106 { "/FooBar.app", "/FooBar.app" }, 107 { "/FooBar.app/", "/FooBar.app" }, 108 { "//FooBar.app", "//FooBar.app" }, 109 { "/Foo/Bar.app", "/Foo/Bar.app" }, 110 { "/Foo/Bar.app/", "/Foo/Bar.app" }, 111 { "/F/B.app", "/F/B.app" }, 112 { "/F/B.app/", "/F/B.app" }, 113 { "/Foo/Bar.app/baz", "/Foo/Bar.app" }, 114 { "/Foo/Bar.app/baz/", "/Foo/Bar.app" }, 115 { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" }, 116 { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper", 117 "/Applications/Google Foo.app" }, 118 }; 119 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) { 120 out = GetAppBundlePath(FilePath(valid_inputs[i].in)); 121 EXPECT_FALSE(out.empty()) << "loop: " << i; 122 EXPECT_STREQ(valid_inputs[i].expected_out, 123 out.value().c_str()) << "loop: " << i; 124 } 125} 126 127TEST_F(MacUtilTest, TestExcludeFileFromBackups) { 128 NSString* homeDirectory = NSHomeDirectory(); 129 NSString* dummyFilePath = 130 [homeDirectory stringByAppendingPathComponent:@"DummyFile"]; 131 const char* dummy_file_path = [dummyFilePath fileSystemRepresentation]; 132 ASSERT_TRUE(dummy_file_path); 133 FilePath file_path(dummy_file_path); 134 // It is not actually necessary to have a physical file in order to 135 // set its exclusion property. 136 NSURL* fileURL = [NSURL URLWithString:dummyFilePath]; 137 // Reset the exclusion in case it was set previously. 138 SetFileBackupExclusion(file_path, false); 139 Boolean excludeByPath; 140 // Initial state should be non-excluded. 141 EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); 142 // Exclude the file. 143 EXPECT_TRUE(SetFileBackupExclusion(file_path, true)); 144 EXPECT_TRUE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); 145 // Un-exclude the file. 146 EXPECT_TRUE(SetFileBackupExclusion(file_path, false)); 147 EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); 148} 149 150TEST_F(MacUtilTest, TestGetValueFromDictionary) { 151 scoped_cftyperef<CFMutableDictionaryRef> dict( 152 CFDictionaryCreateMutable(0, 0, 153 &kCFTypeDictionaryKeyCallBacks, 154 &kCFTypeDictionaryValueCallBacks)); 155 CFDictionarySetValue(dict.get(), CFSTR("key"), CFSTR("value")); 156 157 EXPECT_TRUE(CFEqual(CFSTR("value"), 158 GetValueFromDictionary( 159 dict, CFSTR("key"), CFStringGetTypeID()))); 160 EXPECT_FALSE(GetValueFromDictionary(dict, CFSTR("key"), CFNumberGetTypeID())); 161 EXPECT_FALSE(GetValueFromDictionary( 162 dict, CFSTR("no-exist"), CFStringGetTypeID())); 163} 164 165} // namespace 166 167} // namespace mac_util 168