OLD | NEW |
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 Loading... |
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 |
| 283 base::mac::ScopedCFTypeRef<CFNumberRef> cf_one( |
| 284 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &one)); |
| 285 base::mac::ScopedCFTypeRef<CFNumberRef> cf_two( |
| 286 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &two)); |
| 287 base::mac::ScopedCFTypeRef<CFNumberRef> cf_three( |
| 288 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &three)); |
| 289 |
| 290 CFStringRef keys[] = { CFSTR("one"), CFSTR("two"), CFSTR("three") }; |
| 291 CFNumberRef values[] = { cf_one, cf_two, cf_three }; |
| 292 |
| 293 COMPILE_ASSERT(arraysize(keys) == arraysize(values), |
| 294 keys_and_values_arraysizes_are_different); |
| 295 |
| 296 base::mac::ScopedCFTypeRef<CFDictionaryRef> test_dict( |
| 297 CFDictionaryCreate(kCFAllocatorDefault, |
| 298 reinterpret_cast<const void**>(keys), |
| 299 reinterpret_cast<const void**>(values), |
| 300 arraysize(values), |
| 301 &kCFCopyStringDictionaryKeyCallBacks, |
| 302 &kCFTypeDictionaryValueCallBacks)); |
| 303 |
| 304 // base::mac::GetValueFromDictionary<>(_, _) should produce the correct |
| 305 // expected output. |
| 306 EXPECT_EQ(values[0], |
| 307 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, |
| 308 CFSTR("one"))); |
| 309 EXPECT_EQ(values[1], |
| 310 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, |
| 311 CFSTR("two"))); |
| 312 EXPECT_EQ(values[2], |
| 313 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, |
| 314 CFSTR("three"))); |
| 315 |
| 316 // Bad input should produce bad output. |
| 317 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, |
| 318 CFSTR("four"))); |
| 319 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFStringRef>(test_dict, |
| 320 CFSTR("one"))); |
| 321 } |
OLD | NEW |