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), | |
Mark Mentovai
2011/11/14 19:29:14
Looks like a leak to me.
KushalP
2011/11/14 20:37:13
How so? What did I do wrong here?
Mark Mentovai
2011/11/14 20:50:19
KushalP wrote:
KushalP
2011/11/14 21:04:12
It's owned by values because of the CF "create rul
Mark Mentovai
2011/11/14 21:09:01
KushalP wrote:
KushalP
2011/11/14 21:14:39
I'll wrap these in ScopedCFTypeRefs
| |
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"))); | |
Mark Mentovai
2011/11/14 19:29:14
Bad alignment. Line 318 too.
| |
317 EXPECT_FALSE(base::mac::GetValueFromDictionary<CFStringRef>(test_dict, | |
318 CFSTR("one"))); | |
319 | |
320 // base::mac::GetValueFromDictionary<>(_, _) should have the same | |
Mark Mentovai
2011/11/14 19:29:14
You will get rid of the three-argument form, so ge
| |
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 } | |
OLD | NEW |