Chromium Code Reviews| 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 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 | |
|
Mark Mentovai
2011/11/13 01:28:22
Extra blank line.
| |
| 292 COMPILE_ASSERT(arraysize(keys) == arraysize(values), | |
| 293 keys_and_values_arraysizes_are_different); | |
| 294 | |
| 295 base::mac::ScopedCFTypeRef<CFDictionaryRef> test_dict( | |
| 296 CFDictionaryCreate(kCFAllocatorDefault, | |
| 297 (const void**)keys, | |
| 298 (const void**)values, | |
| 299 arraysize(values), | |
| 300 &kCFCopyStringDictionaryKeyCallBacks, | |
| 301 &kCFTypeDictionaryValueCallBacks)); | |
| 302 | |
| 303 // base::mac::GetValueFromDictionary<>(_, _) should produce the correct | |
| 304 // expected output. | |
| 305 EXPECT_EQ(values[0], | |
| 306 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 307 CFSTR("one"))); | |
| 308 EXPECT_EQ(values[1], | |
| 309 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 310 CFSTR("two"))); | |
| 311 EXPECT_EQ(values[2], | |
| 312 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 313 CFSTR("three"))); | |
| 314 | |
| 315 // Bad input should produce bad output. | |
| 316 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 317 CFSTR("four"))); | |
| 318 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFStringRef>(test_dict, | |
| 319 CFSTR("one"))); | |
| 320 | |
| 321 // base::mac::GetValueFromDictionary<>(_, _) should have the same | |
| 322 // response as base::mac::GetValueFromDictionary(_, _, _) does. | |
| 323 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("one"), | |
| 324 CFNumberGetTypeID()), | |
| 325 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 326 CFSTR("one"))); | |
| 327 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("two"), | |
| 328 CFNumberGetTypeID()), | |
| 329 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 330 CFSTR("two"))); | |
| 331 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("three"), | |
| 332 CFNumberGetTypeID()), | |
| 333 base::mac::GetValueFromDictionary<CFNumberRef>(test_dict, | |
| 334 CFSTR("three"))); | |
| 335 EXPECT_EQ(base::mac::GetValueFromDictionary(test_dict, CFSTR("one"), | |
| 336 CFStringGetTypeID()), | |
| 337 base::mac::GetValueFromDictionary<CFStringRef>(test_dict, | |
| 338 CFSTR("one"))); | |
| 339 } | |
| OLD | NEW |