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

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

Issue 8540021: Create a nicer interface for base::mac::GetValueFromDictionary (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add a TypeNameForCFType function Created 9 years, 1 month 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
« base/mac/foundation_util.mm ('K') | « base/mac/foundation_util.mm ('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/basictypes.h"
7 #include "base/mac/scoped_cftyperef.h" 8 #include "base/mac/scoped_cftyperef.h"
8 #include "base/mac/scoped_nsautorelease_pool.h" 9 #include "base/mac/scoped_nsautorelease_pool.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 TEST(FoundationUtilTest, CFCast) { 12 TEST(FoundationUtilTest, CFCast) {
12 // Build out the CF types to be tested as empty containers. 13 // Build out the CF types to be tested as empty containers.
13 base::mac::ScopedCFTypeRef<CFTypeRef> test_array( 14 base::mac::ScopedCFTypeRef<CFTypeRef> test_array(
14 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); 15 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks));
15 base::mac::ScopedCFTypeRef<CFTypeRef> test_array_mutable( 16 base::mac::ScopedCFTypeRef<CFTypeRef> test_array_mutable(
16 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); 17 CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks));
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 // ObjCCastStrict: Giving a nil provides a nil. 269 // ObjCCastStrict: Giving a nil provides a nil.
269 EXPECT_FALSE(base::mac::ObjCCastStrict<NSArray>(nil)); 270 EXPECT_FALSE(base::mac::ObjCCastStrict<NSArray>(nil));
270 EXPECT_FALSE(base::mac::ObjCCastStrict<NSData>(nil)); 271 EXPECT_FALSE(base::mac::ObjCCastStrict<NSData>(nil));
271 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDate>(nil)); 272 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDate>(nil));
272 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDictionary>(nil)); 273 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDictionary>(nil));
273 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNull>(nil)); 274 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNull>(nil));
274 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNumber>(nil)); 275 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNumber>(nil));
275 EXPECT_FALSE(base::mac::ObjCCastStrict<NSSet>(nil)); 276 EXPECT_FALSE(base::mac::ObjCCastStrict<NSSet>(nil));
276 EXPECT_FALSE(base::mac::ObjCCastStrict<NSString>(nil)); 277 EXPECT_FALSE(base::mac::ObjCCastStrict<NSString>(nil));
277 } 278 }
279
280 TEST(FoundationUtilTest, GetValueFromDictionary) {
281 int one = 1, two = 2, three = 3;
282 CFStringRef keys[] = {
283 CFSTR("one"), CFSTR("two"), CFSTR("three")
284 };
285 CFNumberRef values[] = {
286 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &one),
287 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &two),
288 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &three)
289 };
290
291 COMPILE_ASSERT(arraysize(keys) == arraysize(values),
292 keys_and_values_arraysizes_are_different);
293
294 base::mac::ScopedCFTypeRef<CFDictionaryRef> test_dict(
295 CFDictionaryCreate(kCFAllocatorDefault,
296 (const void**)keys,
297 (const void**)values,
298 arraysize(values),
299 &kCFCopyStringDictionaryKeyCallBacks,
300 &kCFTypeDictionaryValueCallBacks));
301
302 // base::mac::GetValueFromDictionary<>(_, _) should produce the correct
303 // expected output.
304 EXPECT_EQ(values[0],
305 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
306 CFSTR("one")));
307 EXPECT_EQ(values[1],
308 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
309 CFSTR("two")));
310 EXPECT_EQ(values[2],
311 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
312 CFSTR("three")));
313
314 // Bad input should produce bad output.
315 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
316 CFSTR("four")));
317 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFStringRef>(test_dict,
318 CFSTR("one")));
319
320 // base::mac::GetValueFromDictionary<>(_, _) should have the same
321 // response as base::mac::GetValueFromDictionary(_, _, _) does.
322 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("one"),
323 CFNumberGetTypeID()),
324 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
325 CFSTR("one")));
326 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("two"),
327 CFNumberGetTypeID()),
328 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
329 CFSTR("two")));
330 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("three"),
331 CFNumberGetTypeID()),
332 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict,
333 CFSTR("three")));
334 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("one"),
335 CFStringGetTypeID()),
336 base::mac::GetValueFromDictionary<CFStringRef>(test_dict,
337 CFSTR("one")));
338 }
OLDNEW
« base/mac/foundation_util.mm ('K') | « base/mac/foundation_util.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698