Home | History | Annotate | Download | only in mac
      1 // Copyright (c) 2011 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 
      7 #include "base/mac/mac_util.h"
      8 
      9 #include "base/file_path.h"
     10 #include "base/file_util.h"
     11 #include "base/mac/scoped_cftyperef.h"
     12 #include "base/memory/scoped_nsobject.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "testing/platform_test.h"
     15 
     16 namespace base {
     17 namespace mac {
     18 
     19 namespace {
     20 
     21 typedef PlatformTest MacUtilTest;
     22 
     23 TEST_F(MacUtilTest, TestFSRef) {
     24   FSRef ref;
     25   std::string path("/System/Library");
     26 
     27   ASSERT_TRUE(FSRefFromPath(path, &ref));
     28   EXPECT_EQ(path, PathFromFSRef(ref));
     29 }
     30 
     31 TEST_F(MacUtilTest, GetUserDirectoryTest) {
     32   // Try a few keys, make sure they come back with non-empty paths.
     33   FilePath caches_dir;
     34   EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir));
     35   EXPECT_FALSE(caches_dir.empty());
     36 
     37   FilePath application_support_dir;
     38   EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory,
     39                                &application_support_dir));
     40   EXPECT_FALSE(application_support_dir.empty());
     41 
     42   FilePath library_dir;
     43   EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir));
     44   EXPECT_FALSE(library_dir.empty());
     45 }
     46 
     47 TEST_F(MacUtilTest, TestLibraryPath) {
     48   FilePath library_dir = GetUserLibraryPath();
     49   // Make sure the string isn't empty.
     50   EXPECT_FALSE(library_dir.value().empty());
     51 }
     52 
     53 TEST_F(MacUtilTest, TestGetAppBundlePath) {
     54   FilePath out;
     55 
     56   // Make sure it doesn't crash.
     57   out = GetAppBundlePath(FilePath());
     58   EXPECT_TRUE(out.empty());
     59 
     60   // Some more invalid inputs.
     61   const char* invalid_inputs[] = {
     62     "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
     63     "foo/bar./bazquux", "foo/.app", "//foo",
     64   };
     65   for (size_t i = 0; i < arraysize(invalid_inputs); i++) {
     66     out = GetAppBundlePath(FilePath(invalid_inputs[i]));
     67     EXPECT_TRUE(out.empty()) << "loop: " << i;
     68   }
     69 
     70   // Some valid inputs; this and |expected_outputs| should be in sync.
     71   struct {
     72     const char *in;
     73     const char *expected_out;
     74   } valid_inputs[] = {
     75     { "FooBar.app/", "FooBar.app" },
     76     { "/FooBar.app", "/FooBar.app" },
     77     { "/FooBar.app/", "/FooBar.app" },
     78     { "//FooBar.app", "//FooBar.app" },
     79     { "/Foo/Bar.app", "/Foo/Bar.app" },
     80     { "/Foo/Bar.app/", "/Foo/Bar.app" },
     81     { "/F/B.app", "/F/B.app" },
     82     { "/F/B.app/", "/F/B.app" },
     83     { "/Foo/Bar.app/baz", "/Foo/Bar.app" },
     84     { "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
     85     { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
     86     { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
     87         "/Applications/Google Foo.app" },
     88   };
     89   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) {
     90     out = GetAppBundlePath(FilePath(valid_inputs[i].in));
     91     EXPECT_FALSE(out.empty()) << "loop: " << i;
     92     EXPECT_STREQ(valid_inputs[i].expected_out,
     93         out.value().c_str()) << "loop: " << i;
     94   }
     95 }
     96 
     97 TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
     98   NSString* homeDirectory = NSHomeDirectory();
     99   NSString* dummyFilePath =
    100       [homeDirectory stringByAppendingPathComponent:@"DummyFile"];
    101   const char* dummy_file_path = [dummyFilePath fileSystemRepresentation];
    102   ASSERT_TRUE(dummy_file_path);
    103   FilePath file_path(dummy_file_path);
    104   // It is not actually necessary to have a physical file in order to
    105   // set its exclusion property.
    106   NSURL* fileURL = [NSURL URLWithString:dummyFilePath];
    107   // Reset the exclusion in case it was set previously.
    108   SetFileBackupExclusion(file_path, false);
    109   Boolean excludeByPath;
    110   // Initial state should be non-excluded.
    111   EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
    112   // Exclude the file.
    113   EXPECT_TRUE(SetFileBackupExclusion(file_path, true));
    114   EXPECT_TRUE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
    115   // Un-exclude the file.
    116   EXPECT_TRUE(SetFileBackupExclusion(file_path, false));
    117   EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
    118 }
    119 
    120 TEST_F(MacUtilTest, TestGetValueFromDictionary) {
    121   ScopedCFTypeRef<CFMutableDictionaryRef> dict(
    122       CFDictionaryCreateMutable(0, 0,
    123                                 &kCFTypeDictionaryKeyCallBacks,
    124                                 &kCFTypeDictionaryValueCallBacks));
    125   CFDictionarySetValue(dict.get(), CFSTR("key"), CFSTR("value"));
    126 
    127   EXPECT_TRUE(CFEqual(CFSTR("value"),
    128                       GetValueFromDictionary(
    129                           dict, CFSTR("key"), CFStringGetTypeID())));
    130   EXPECT_FALSE(GetValueFromDictionary(dict, CFSTR("key"), CFNumberGetTypeID()));
    131   EXPECT_FALSE(GetValueFromDictionary(
    132                    dict, CFSTR("no-exist"), CFStringGetTypeID()));
    133 }
    134 
    135 TEST_F(MacUtilTest, CopyNSImageToCGImage) {
    136   scoped_nsobject<NSImage> nsImage(
    137       [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]);
    138   [nsImage lockFocus];
    139   [[NSColor redColor] set];
    140   NSRect rect = NSZeroRect;
    141   rect.size = [nsImage size];
    142   NSRectFill(rect);
    143   [nsImage unlockFocus];
    144 
    145   ScopedCFTypeRef<CGImageRef> cgImage(CopyNSImageToCGImage(nsImage.get()));
    146   EXPECT_TRUE(cgImage.get());
    147 }
    148 
    149 TEST_F(MacUtilTest, NSObjectRetainRelease) {
    150   scoped_nsobject<NSArray> array([[NSArray alloc] initWithObjects:@"foo", nil]);
    151   EXPECT_EQ(1U, [array retainCount]);
    152 
    153   NSObjectRetain(array);
    154   EXPECT_EQ(2U, [array retainCount]);
    155 
    156   NSObjectRelease(array);
    157   EXPECT_EQ(1U, [array retainCount]);
    158 }
    159 
    160 }  // namespace
    161 
    162 }  // namespace mac
    163 }  // namespace base
    164