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

Side by Side Diff: base/mac/foundation_util_unittest.mm

Issue 8356024: Create ObjCCast<>() and ObjCCastStrict<>() methods (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Reword. Remove non-ASCII. Created 9 years, 2 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/foundation_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/mac/foundation_util.h" 5 #include "base/mac/foundation_util.h"
6 6
7 #include "base/mac/scoped_cftyperef.h" 7 #include "base/mac/scoped_cftyperef.h"
8 #include "base/mac/scoped_nsautorelease_pool.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
10 TEST(FoundationUtilTest, CFCast) { 11 TEST(FoundationUtilTest, CFCast) {
11 // Build out the CF types to be tested as empty containers. 12 // Build out the CF types to be tested as empty containers.
12 base::mac::ScopedCFTypeRef<CFTypeRef> test_array( 13 base::mac::ScopedCFTypeRef<CFTypeRef> test_array(
13 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); 14 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks));
14 base::mac::ScopedCFTypeRef<CFTypeRef> test_array_mutable( 15 base::mac::ScopedCFTypeRef<CFTypeRef> test_array_mutable(
15 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); 16 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks));
16 base::mac::ScopedCFTypeRef<CFTypeRef> test_bag( 17 base::mac::ScopedCFTypeRef<CFTypeRef> test_bag(
17 CFBagCreate(NULL, NULL, 0, &kCFTypeBagCallBacks)); 18 CFBagCreate(NULL, NULL, 0, &kCFTypeBagCallBacks));
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 EXPECT_FALSE(base::mac::CFCastStrict<CFBagRef>(NULL)); 153 EXPECT_FALSE(base::mac::CFCastStrict<CFBagRef>(NULL));
153 EXPECT_FALSE(base::mac::CFCastStrict<CFBooleanRef>(NULL)); 154 EXPECT_FALSE(base::mac::CFCastStrict<CFBooleanRef>(NULL));
154 EXPECT_FALSE(base::mac::CFCastStrict<CFDataRef>(NULL)); 155 EXPECT_FALSE(base::mac::CFCastStrict<CFDataRef>(NULL));
155 EXPECT_FALSE(base::mac::CFCastStrict<CFDateRef>(NULL)); 156 EXPECT_FALSE(base::mac::CFCastStrict<CFDateRef>(NULL));
156 EXPECT_FALSE(base::mac::CFCastStrict<CFDictionaryRef>(NULL)); 157 EXPECT_FALSE(base::mac::CFCastStrict<CFDictionaryRef>(NULL));
157 EXPECT_FALSE(base::mac::CFCastStrict<CFNullRef>(NULL)); 158 EXPECT_FALSE(base::mac::CFCastStrict<CFNullRef>(NULL));
158 EXPECT_FALSE(base::mac::CFCastStrict<CFNumberRef>(NULL)); 159 EXPECT_FALSE(base::mac::CFCastStrict<CFNumberRef>(NULL));
159 EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL)); 160 EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL));
160 EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL)); 161 EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL));
161 } 162 }
163
164 TEST(FoundationUtilTest, ObjCCast) {
165 base::mac::ScopedNSAutoreleasePool pool;
166
167 id test_array = [NSArray array];
168 id test_array_mutable = [NSMutableArray array];
169 id test_data = [NSData data];
170 id test_data_mutable = [NSMutableData dataWithCapacity:10];
171 id test_date = [NSDate date];
172 id test_dict =
173 [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:42]
174 forKey:@"meaning"];
175 id test_dict_mutable = [NSMutableDictionary dictionaryWithCapacity:10];
176 id test_number = [NSNumber numberWithInt:42];
177 id test_null = [NSNull null];
178 id test_set = [NSSet setWithObject:@"string object"];
179 id test_set_mutable = [NSMutableSet setWithCapacity:10];
180 id test_str = [NSString string];
181 id test_str_const = @"bonjour";
182 id test_str_mutable = [NSMutableString stringWithCapacity:10];
183
184 // Make sure the allocations of NS types are good.
185 EXPECT_TRUE(test_array);
186 EXPECT_TRUE(test_array_mutable);
187 EXPECT_TRUE(test_data);
188 EXPECT_TRUE(test_data_mutable);
189 EXPECT_TRUE(test_date);
190 EXPECT_TRUE(test_dict);
191 EXPECT_TRUE(test_dict_mutable);
192 EXPECT_TRUE(test_number);
193 EXPECT_TRUE(test_null);
194 EXPECT_TRUE(test_set);
195 EXPECT_TRUE(test_set_mutable);
196 EXPECT_TRUE(test_str);
197 EXPECT_TRUE(test_str_const);
198 EXPECT_TRUE(test_str_mutable);
199
200 // Casting the id correctly provides the same pointer.
201 EXPECT_EQ(test_array, base::mac::ObjCCast<NSArray>(test_array));
202 EXPECT_EQ(test_array_mutable,
203 base::mac::ObjCCast<NSArray>(test_array_mutable));
204 EXPECT_EQ(test_data, base::mac::ObjCCast<NSData>(test_data));
205 EXPECT_EQ(test_data_mutable,
206 base::mac::ObjCCast<NSData>(test_data_mutable));
207 EXPECT_EQ(test_date, base::mac::ObjCCast<NSDate>(test_date));
208 EXPECT_EQ(test_dict, base::mac::ObjCCast<NSDictionary>(test_dict));
209 EXPECT_EQ(test_dict_mutable,
210 base::mac::ObjCCast<NSDictionary>(test_dict_mutable));
211 EXPECT_EQ(test_number, base::mac::ObjCCast<NSNumber>(test_number));
212 EXPECT_EQ(test_null, base::mac::ObjCCast<NSNull>(test_null));
213 EXPECT_EQ(test_set, base::mac::ObjCCast<NSSet>(test_set));
214 EXPECT_EQ(test_set_mutable, base::mac::ObjCCast<NSSet>(test_set_mutable));
215 EXPECT_EQ(test_str, base::mac::ObjCCast<NSString>(test_str));
216 EXPECT_EQ(test_str_const, base::mac::ObjCCast<NSString>(test_str_const));
217 EXPECT_EQ(test_str_mutable,
218 base::mac::ObjCCast<NSString>(test_str_mutable));
219
220 // When given an incorrect ObjC cast, provide nil.
221 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array));
222 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array_mutable));
223 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_data));
224 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_data_mutable));
225 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_date));
226 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_dict));
227 EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(test_dict_mutable));
228 EXPECT_FALSE(base::mac::ObjCCast<NSNull>(test_number));
229 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_null));
230 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_set));
231 EXPECT_FALSE(base::mac::ObjCCast<NSDate>(test_set_mutable));
232 EXPECT_FALSE(base::mac::ObjCCast<NSData>(test_str));
233 EXPECT_FALSE(base::mac::ObjCCast<NSData>(test_str_const));
234 EXPECT_FALSE(base::mac::ObjCCast<NSArray>(test_str_mutable));
235
236 // Giving a nil provides a nil.
237 EXPECT_FALSE(base::mac::ObjCCast<NSArray>(nil));
238 EXPECT_FALSE(base::mac::ObjCCast<NSData>(nil));
239 EXPECT_FALSE(base::mac::ObjCCast<NSDate>(nil));
240 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(nil));
241 EXPECT_FALSE(base::mac::ObjCCast<NSNull>(nil));
242 EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(nil));
243 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(nil));
244 EXPECT_FALSE(base::mac::ObjCCast<NSString>(nil));
245
246 // ObjCCastStrict: correct cast results in correct pointer being returned.
247 EXPECT_EQ(test_array, base::mac::ObjCCastStrict<NSArray>(test_array));
248 EXPECT_EQ(test_array_mutable,
249 base::mac::ObjCCastStrict<NSArray>(test_array_mutable));
250 EXPECT_EQ(test_data, base::mac::ObjCCastStrict<NSData>(test_data));
251 EXPECT_EQ(test_data_mutable,
252 base::mac::ObjCCastStrict<NSData>(test_data_mutable));
253 EXPECT_EQ(test_date, base::mac::ObjCCastStrict<NSDate>(test_date));
254 EXPECT_EQ(test_dict, base::mac::ObjCCastStrict<NSDictionary>(test_dict));
255 EXPECT_EQ(test_dict_mutable,
256 base::mac::ObjCCastStrict<NSDictionary>(test_dict_mutable));
257 EXPECT_EQ(test_number, base::mac::ObjCCastStrict<NSNumber>(test_number));
258 EXPECT_EQ(test_null, base::mac::ObjCCastStrict<NSNull>(test_null));
259 EXPECT_EQ(test_set, base::mac::ObjCCastStrict<NSSet>(test_set));
260 EXPECT_EQ(test_set_mutable,
261 base::mac::ObjCCastStrict<NSSet>(test_set_mutable));
262 EXPECT_EQ(test_str, base::mac::ObjCCastStrict<NSString>(test_str));
263 EXPECT_EQ(test_str_const,
264 base::mac::ObjCCastStrict<NSString>(test_str_const));
265 EXPECT_EQ(test_str_mutable,
266 base::mac::ObjCCastStrict<NSString>(test_str_mutable));
267
268 // ObjCCastStrict: Giving a nil provides a nil.
269 EXPECT_FALSE(base::mac::ObjCCastStrict<NSArray>(nil));
270 EXPECT_FALSE(base::mac::ObjCCastStrict<NSData>(nil));
271 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDate>(nil));
272 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDictionary>(nil));
273 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNull>(nil));
274 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNumber>(nil));
275 EXPECT_FALSE(base::mac::ObjCCastStrict<NSSet>(nil));
276 EXPECT_FALSE(base::mac::ObjCCastStrict<NSString>(nil));
277 }
OLDNEW
« no previous file with comments | « base/mac/foundation_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698