Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / base / mac / mac_util_unittest.mm
blob2984fee92fa7031988ea733595b45d08c38117f0
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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "base/mac/mac_util.h"
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/memory/scoped_nsobject.h"
14 #include "base/scoped_temp_dir.h"
15 #include "base/sys_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
19 namespace base {
20 namespace mac {
22 namespace {
24 typedef PlatformTest MacUtilTest;
26 TEST_F(MacUtilTest, TestFSRef) {
27   FSRef ref;
28   std::string path("/System/Library");
30   ASSERT_TRUE(FSRefFromPath(path, &ref));
31   EXPECT_EQ(path, PathFromFSRef(ref));
34 TEST_F(MacUtilTest, GetUserDirectoryTest) {
35   // Try a few keys, make sure they come back with non-empty paths.
36   FilePath caches_dir;
37   EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir));
38   EXPECT_FALSE(caches_dir.empty());
40   FilePath application_support_dir;
41   EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory,
42                                &application_support_dir));
43   EXPECT_FALSE(application_support_dir.empty());
45   FilePath library_dir;
46   EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir));
47   EXPECT_FALSE(library_dir.empty());
50 TEST_F(MacUtilTest, TestLibraryPath) {
51   FilePath library_dir = GetUserLibraryPath();
52   // Make sure the string isn't empty.
53   EXPECT_FALSE(library_dir.value().empty());
56 TEST_F(MacUtilTest, TestGetAppBundlePath) {
57   FilePath out;
59   // Make sure it doesn't crash.
60   out = GetAppBundlePath(FilePath());
61   EXPECT_TRUE(out.empty());
63   // Some more invalid inputs.
64   const char* invalid_inputs[] = {
65     "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
66     "foo/bar./bazquux", "foo/.app", "//foo",
67   };
68   for (size_t i = 0; i < arraysize(invalid_inputs); i++) {
69     out = GetAppBundlePath(FilePath(invalid_inputs[i]));
70     EXPECT_TRUE(out.empty()) << "loop: " << i;
71   }
73   // Some valid inputs; this and |expected_outputs| should be in sync.
74   struct {
75     const char *in;
76     const char *expected_out;
77   } valid_inputs[] = {
78     { "FooBar.app/", "FooBar.app" },
79     { "/FooBar.app", "/FooBar.app" },
80     { "/FooBar.app/", "/FooBar.app" },
81     { "//FooBar.app", "//FooBar.app" },
82     { "/Foo/Bar.app", "/Foo/Bar.app" },
83     { "/Foo/Bar.app/", "/Foo/Bar.app" },
84     { "/F/B.app", "/F/B.app" },
85     { "/F/B.app/", "/F/B.app" },
86     { "/Foo/Bar.app/baz", "/Foo/Bar.app" },
87     { "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
88     { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
89     { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
90         "/Applications/Google Foo.app" },
91   };
92   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) {
93     out = GetAppBundlePath(FilePath(valid_inputs[i].in));
94     EXPECT_FALSE(out.empty()) << "loop: " << i;
95     EXPECT_STREQ(valid_inputs[i].expected_out,
96         out.value().c_str()) << "loop: " << i;
97   }
100 TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
101   // The file must already exist in order to set its exclusion property.
102   ScopedTempDir temp_dir_;
103   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
104   FilePath dummy_file_path = temp_dir_.path().Append("DummyFile");
105   const char dummy_data[] = "All your base are belong to us!";
106   // Dump something real into the file.
107   ASSERT_EQ(static_cast<int>(arraysize(dummy_data)),
108       file_util::WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
109   NSString* fileURLString =
110       [NSString stringWithUTF8String:dummy_file_path.value().c_str()];
111   NSURL* fileURL = [NSURL URLWithString:fileURLString];
112   // Initial state should be non-excluded.
113   EXPECT_FALSE(CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), NULL));
114   // Exclude the file.
115   EXPECT_TRUE(SetFileBackupExclusion(dummy_file_path));
116   // SetFileBackupExclusion never excludes by path.
117   Boolean excluded_by_path = FALSE;
118   Boolean excluded =
119       CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), &excluded_by_path);
120   EXPECT_TRUE(excluded);
121   EXPECT_FALSE(excluded_by_path);
124 TEST_F(MacUtilTest, CopyNSImageToCGImage) {
125   scoped_nsobject<NSImage> nsImage(
126       [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]);
127   [nsImage lockFocus];
128   [[NSColor redColor] set];
129   NSRect rect = NSZeroRect;
130   rect.size = [nsImage size];
131   NSRectFill(rect);
132   [nsImage unlockFocus];
134   ScopedCFTypeRef<CGImageRef> cgImage(CopyNSImageToCGImage(nsImage.get()));
135   EXPECT_TRUE(cgImage.get());
138 TEST_F(MacUtilTest, NSObjectRetainRelease) {
139   scoped_nsobject<NSArray> array([[NSArray alloc] initWithObjects:@"foo", nil]);
140   EXPECT_EQ(1U, [array retainCount]);
142   NSObjectRetain(array);
143   EXPECT_EQ(2U, [array retainCount]);
145   NSObjectRelease(array);
146   EXPECT_EQ(1U, [array retainCount]);
149 TEST_F(MacUtilTest, IsOSEllipsis) {
150   int32 major, minor, bugfix;
151   base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
153   if (major == 10) {
154     if (minor == 6) {
155       EXPECT_TRUE(IsOSSnowLeopard());
156       EXPECT_FALSE(IsOSLion());
157       EXPECT_TRUE(IsOSLionOrEarlier());
158       EXPECT_FALSE(IsOSLionOrLater());
159       EXPECT_FALSE(IsOSMountainLion());
160       EXPECT_FALSE(IsOSMountainLionOrLater());
161       EXPECT_FALSE(IsOSLaterThanMountainLion_DontCallThis());
162     } else if (minor == 7) {
163       EXPECT_FALSE(IsOSSnowLeopard());
164       EXPECT_TRUE(IsOSLion());
165       EXPECT_TRUE(IsOSLionOrEarlier());
166       EXPECT_TRUE(IsOSLionOrLater());
167       EXPECT_FALSE(IsOSMountainLion());
168       EXPECT_FALSE(IsOSMountainLionOrLater());
169       EXPECT_FALSE(IsOSLaterThanMountainLion_DontCallThis());
170     } else if (minor == 8) {
171       EXPECT_FALSE(IsOSSnowLeopard());
172       EXPECT_FALSE(IsOSLion());
173       EXPECT_FALSE(IsOSLionOrEarlier());
174       EXPECT_TRUE(IsOSLionOrLater());
175       EXPECT_TRUE(IsOSMountainLion());
176       EXPECT_TRUE(IsOSMountainLionOrLater());
177       EXPECT_FALSE(IsOSLaterThanMountainLion_DontCallThis());
178     } else {
179       // Not five, six, seven, or eight. Ah, ah, ah.
180       EXPECT_TRUE(false);
181     }
182   } else {
183     // Not ten. What you gonna do?
184     EXPECT_FALSE(true);
185   }
188 TEST_F(MacUtilTest, ParseModelIdentifier) {
189   std::string model;
190   int32 major = 1, minor = 2;
192   EXPECT_FALSE(ParseModelIdentifier("", &model, &major, &minor));
193   EXPECT_EQ(0U, model.length());
194   EXPECT_EQ(1, major);
195   EXPECT_EQ(2, minor);
196   EXPECT_FALSE(ParseModelIdentifier("FooBar", &model, &major, &minor));
198   EXPECT_TRUE(ParseModelIdentifier("MacPro4,1", &model, &major, &minor));
199   EXPECT_EQ(model, "MacPro");
200   EXPECT_EQ(4, major);
201   EXPECT_EQ(1, minor);
203   EXPECT_TRUE(ParseModelIdentifier("MacBookPro6,2", &model, &major, &minor));
204   EXPECT_EQ(model, "MacBookPro");
205   EXPECT_EQ(6, major);
206   EXPECT_EQ(2, minor);
209 }  // namespace
211 }  // namespace mac
212 }  // namespace base