Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: base/mac_util_unittest.mm

Issue 6046009: Move base/mac_util.h to base/mac and use the base::mac namespace.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/mac_util.mm ('k') | chrome/app/breakpad_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/mac/scoped_cftyperef.h"
13 #include "base/test/mock_chrome_application_mac.h"
14 #include "base/scoped_nsobject.h"
15 #include "base/scoped_ptr.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
18
19 namespace mac_util {
20
21 namespace {
22
23 typedef PlatformTest MacUtilTest;
24
25 TEST_F(MacUtilTest, TestFSRef) {
26 FSRef ref;
27 std::string path("/System/Library");
28
29 ASSERT_TRUE(FSRefFromPath(path, &ref));
30 EXPECT_EQ(path, PathFromFSRef(ref));
31 }
32
33 TEST_F(MacUtilTest, GetUserDirectoryTest) {
34 // Try a few keys, make sure they come back with non-empty paths.
35 FilePath caches_dir;
36 EXPECT_TRUE(GetUserDirectory(NSCachesDirectory, &caches_dir));
37 EXPECT_FALSE(caches_dir.empty());
38
39 FilePath application_support_dir;
40 EXPECT_TRUE(GetUserDirectory(NSApplicationSupportDirectory,
41 &application_support_dir));
42 EXPECT_FALSE(application_support_dir.empty());
43
44 FilePath library_dir;
45 EXPECT_TRUE(GetUserDirectory(NSLibraryDirectory, &library_dir));
46 EXPECT_FALSE(library_dir.empty());
47 }
48
49 TEST_F(MacUtilTest, TestLibraryPath) {
50 FilePath library_dir = GetUserLibraryPath();
51 // Make sure the string isn't empty.
52 EXPECT_FALSE(library_dir.value().empty());
53 }
54
55 TEST_F(MacUtilTest, TestGrabWindowSnapshot) {
56 // Launch a test window so we can take a snapshot.
57 [MockCrApp sharedApplication];
58 NSRect frame = NSMakeRect(0, 0, 400, 400);
59 scoped_nsobject<NSWindow> window(
60 [[NSWindow alloc] initWithContentRect:frame
61 styleMask:NSBorderlessWindowMask
62 backing:NSBackingStoreBuffered
63 defer:NO]);
64 [window setBackgroundColor:[NSColor whiteColor]];
65 [window makeKeyAndOrderFront:NSApp];
66
67 scoped_ptr<std::vector<unsigned char> > png_representation(
68 new std::vector<unsigned char>);
69 int width, height;
70 GrabWindowSnapshot(window, png_representation.get(),
71 &width, &height);
72
73 // Copy png back into NSData object so we can make sure we grabbed a png.
74 scoped_nsobject<NSData> image_data(
75 [[NSData alloc] initWithBytes:&(*png_representation)[0]
76 length:png_representation->size()]);
77 NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:image_data.get()];
78 EXPECT_TRUE([rep isKindOfClass:[NSBitmapImageRep class]]);
79 EXPECT_TRUE(CGImageGetWidth([rep CGImage]) == 400);
80 NSColor* color = [rep colorAtX:200 y:200];
81 CGFloat red = 0, green = 0, blue = 0, alpha = 0;
82 [color getRed:&red green:&green blue:&blue alpha:&alpha];
83 EXPECT_GE(red + green + blue, 3.0);
84 }
85
86 TEST_F(MacUtilTest, TestGetAppBundlePath) {
87 FilePath out;
88
89 // Make sure it doesn't crash.
90 out = GetAppBundlePath(FilePath());
91 EXPECT_TRUE(out.empty());
92
93 // Some more invalid inputs.
94 const char* invalid_inputs[] = {
95 "/", "/foo", "foo", "/foo/bar.", "foo/bar.", "/foo/bar./bazquux",
96 "foo/bar./bazquux", "foo/.app", "//foo",
97 };
98 for (size_t i = 0; i < arraysize(invalid_inputs); i++) {
99 out = GetAppBundlePath(FilePath(invalid_inputs[i]));
100 EXPECT_TRUE(out.empty()) << "loop: " << i;
101 }
102
103 // Some valid inputs; this and |expected_outputs| should be in sync.
104 struct {
105 const char *in;
106 const char *expected_out;
107 } valid_inputs[] = {
108 { "FooBar.app/", "FooBar.app" },
109 { "/FooBar.app", "/FooBar.app" },
110 { "/FooBar.app/", "/FooBar.app" },
111 { "//FooBar.app", "//FooBar.app" },
112 { "/Foo/Bar.app", "/Foo/Bar.app" },
113 { "/Foo/Bar.app/", "/Foo/Bar.app" },
114 { "/F/B.app", "/F/B.app" },
115 { "/F/B.app/", "/F/B.app" },
116 { "/Foo/Bar.app/baz", "/Foo/Bar.app" },
117 { "/Foo/Bar.app/baz/", "/Foo/Bar.app" },
118 { "/Foo/Bar.app/baz/quux.app/quuux", "/Foo/Bar.app" },
119 { "/Applications/Google Foo.app/bar/Foo Helper.app/quux/Foo Helper",
120 "/Applications/Google Foo.app" },
121 };
122 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid_inputs); i++) {
123 out = GetAppBundlePath(FilePath(valid_inputs[i].in));
124 EXPECT_FALSE(out.empty()) << "loop: " << i;
125 EXPECT_STREQ(valid_inputs[i].expected_out,
126 out.value().c_str()) << "loop: " << i;
127 }
128 }
129
130 TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
131 NSString* homeDirectory = NSHomeDirectory();
132 NSString* dummyFilePath =
133 [homeDirectory stringByAppendingPathComponent:@"DummyFile"];
134 const char* dummy_file_path = [dummyFilePath fileSystemRepresentation];
135 ASSERT_TRUE(dummy_file_path);
136 FilePath file_path(dummy_file_path);
137 // It is not actually necessary to have a physical file in order to
138 // set its exclusion property.
139 NSURL* fileURL = [NSURL URLWithString:dummyFilePath];
140 // Reset the exclusion in case it was set previously.
141 SetFileBackupExclusion(file_path, false);
142 Boolean excludeByPath;
143 // Initial state should be non-excluded.
144 EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
145 // Exclude the file.
146 EXPECT_TRUE(SetFileBackupExclusion(file_path, true));
147 EXPECT_TRUE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
148 // Un-exclude the file.
149 EXPECT_TRUE(SetFileBackupExclusion(file_path, false));
150 EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
151 }
152
153 TEST_F(MacUtilTest, TestGetValueFromDictionary) {
154 base::mac::ScopedCFTypeRef<CFMutableDictionaryRef> dict(
155 CFDictionaryCreateMutable(0, 0,
156 &kCFTypeDictionaryKeyCallBacks,
157 &kCFTypeDictionaryValueCallBacks));
158 CFDictionarySetValue(dict.get(), CFSTR("key"), CFSTR("value"));
159
160 EXPECT_TRUE(CFEqual(CFSTR("value"),
161 GetValueFromDictionary(
162 dict, CFSTR("key"), CFStringGetTypeID())));
163 EXPECT_FALSE(GetValueFromDictionary(dict, CFSTR("key"), CFNumberGetTypeID()));
164 EXPECT_FALSE(GetValueFromDictionary(
165 dict, CFSTR("no-exist"), CFStringGetTypeID()));
166 }
167
168 TEST_F(MacUtilTest, CopyNSImageToCGImage) {
169 scoped_nsobject<NSImage> nsImage(
170 [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]);
171 [nsImage lockFocus];
172 [[NSColor redColor] set];
173 NSRect rect = NSZeroRect;
174 rect.size = [nsImage size];
175 NSRectFill(rect);
176 [nsImage unlockFocus];
177
178 base::mac::ScopedCFTypeRef<CGImageRef> cgImage(
179 mac_util::CopyNSImageToCGImage(nsImage.get()));
180 EXPECT_TRUE(cgImage.get());
181 }
182
183 TEST_F(MacUtilTest, NSObjectRetainRelease) {
184 scoped_nsobject<NSArray> array([[NSArray alloc] initWithObjects:@"foo", nil]);
185 EXPECT_EQ(1U, [array retainCount]);
186
187 mac_util::NSObjectRetain(array);
188 EXPECT_EQ(2U, [array retainCount]);
189
190 mac_util::NSObjectRelease(array);
191 EXPECT_EQ(1U, [array retainCount]);
192 }
193
194 } // namespace
195
196 } // namespace mac_util
OLDNEW
« no previous file with comments | « base/mac_util.mm ('k') | chrome/app/breakpad_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698