// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/mac/foundation_util.h" #include "base/mac/scoped_cftyperef.h" #include "testing/gtest/include/gtest/gtest.h" TEST(FoundationUtilTest, CFCast) { // Build out the CF types to be tested as empty containers. base::mac::ScopedCFTypeRef test_array( CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); base::mac::ScopedCFTypeRef test_array_mutable( CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)); base::mac::ScopedCFTypeRef test_bag( CFBagCreate(NULL, NULL, 0, &kCFTypeBagCallBacks)); base::mac::ScopedCFTypeRef test_bag_mutable( CFBagCreateMutable(NULL, 0, &kCFTypeBagCallBacks)); CFTypeRef test_bool = kCFBooleanTrue; base::mac::ScopedCFTypeRef test_data( CFDataCreate(NULL, NULL, 0)); base::mac::ScopedCFTypeRef test_data_mutable( CFDataCreateMutable(NULL, 0)); base::mac::ScopedCFTypeRef test_date( CFDateCreate(NULL, 0)); base::mac::ScopedCFTypeRef test_dict( CFDictionaryCreate(NULL, NULL, NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); base::mac::ScopedCFTypeRef test_dict_mutable( CFDictionaryCreateMutable(NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); int int_val = 256; base::mac::ScopedCFTypeRef test_number( CFNumberCreate(NULL, kCFNumberIntType, &int_val)); CFTypeRef test_null = kCFNull; base::mac::ScopedCFTypeRef test_set( CFSetCreate(NULL, NULL, 0, &kCFTypeSetCallBacks)); base::mac::ScopedCFTypeRef test_set_mutable( CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks)); base::mac::ScopedCFTypeRef test_str( CFStringCreateWithBytes(NULL, NULL, 0, kCFStringEncodingASCII, false)); CFTypeRef test_str_const = CFSTR("hello"); base::mac::ScopedCFTypeRef test_str_mutable( CFStringCreateMutable(NULL, 0)); // Make sure the allocations of CF types are good. EXPECT_TRUE(test_array); EXPECT_TRUE(test_array_mutable); EXPECT_TRUE(test_bag); EXPECT_TRUE(test_bag_mutable); EXPECT_TRUE(test_bool); EXPECT_TRUE(test_data); EXPECT_TRUE(test_data_mutable); EXPECT_TRUE(test_date); EXPECT_TRUE(test_dict); EXPECT_TRUE(test_dict_mutable); EXPECT_TRUE(test_number); EXPECT_TRUE(test_null); EXPECT_TRUE(test_set); EXPECT_TRUE(test_set_mutable); EXPECT_TRUE(test_str); EXPECT_TRUE(test_str_const); EXPECT_TRUE(test_str_mutable); // Casting the CFTypeRef objects correctly provides the same pointer. EXPECT_EQ(test_array, base::mac::CFCast(test_array)); EXPECT_EQ(test_array_mutable, base::mac::CFCast(test_array_mutable)); EXPECT_EQ(test_bag, base::mac::CFCast(test_bag)); EXPECT_EQ(test_bag_mutable, base::mac::CFCast(test_bag_mutable)); EXPECT_EQ(test_bool, base::mac::CFCast(test_bool)); EXPECT_EQ(test_data, base::mac::CFCast(test_data)); EXPECT_EQ(test_data_mutable, base::mac::CFCast(test_data_mutable)); EXPECT_EQ(test_date, base::mac::CFCast(test_date)); EXPECT_EQ(test_dict, base::mac::CFCast(test_dict)); EXPECT_EQ(test_dict_mutable, base::mac::CFCast(test_dict_mutable)); EXPECT_EQ(test_number, base::mac::CFCast(test_number)); EXPECT_EQ(test_null, base::mac::CFCast(test_null)); EXPECT_EQ(test_set, base::mac::CFCast(test_set)); EXPECT_EQ(test_set_mutable, base::mac::CFCast(test_set_mutable)); EXPECT_EQ(test_str, base::mac::CFCast(test_str)); EXPECT_EQ(test_str_const, base::mac::CFCast(test_str_const)); EXPECT_EQ(test_str_mutable, base::mac::CFCast(test_str_mutable)); // When given an incorrect CF cast, provide NULL. EXPECT_FALSE(base::mac::CFCast(test_array)); EXPECT_FALSE(base::mac::CFCast(test_array_mutable)); EXPECT_FALSE(base::mac::CFCast(test_bag)); EXPECT_FALSE(base::mac::CFCast(test_bag_mutable)); EXPECT_FALSE(base::mac::CFCast(test_bool)); EXPECT_FALSE(base::mac::CFCast(test_data)); EXPECT_FALSE(base::mac::CFCast(test_data_mutable)); EXPECT_FALSE(base::mac::CFCast(test_date)); EXPECT_FALSE(base::mac::CFCast(test_dict)); EXPECT_FALSE(base::mac::CFCast(test_dict_mutable)); EXPECT_FALSE(base::mac::CFCast(test_number)); EXPECT_FALSE(base::mac::CFCast(test_null)); EXPECT_FALSE(base::mac::CFCast(test_set)); EXPECT_FALSE(base::mac::CFCast(test_set_mutable)); EXPECT_FALSE(base::mac::CFCast(test_str)); EXPECT_FALSE(base::mac::CFCast(test_str_const)); EXPECT_FALSE(base::mac::CFCast(test_str_mutable)); // Giving a NULL provides a NULL. EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); EXPECT_FALSE(base::mac::CFCast(NULL)); // CFCastStrict: correct cast results in correct pointer being returned. EXPECT_EQ(test_array, base::mac::CFCastStrict(test_array)); EXPECT_EQ(test_array_mutable, base::mac::CFCastStrict(test_array_mutable)); EXPECT_EQ(test_bag, base::mac::CFCastStrict(test_bag)); EXPECT_EQ(test_bag_mutable, base::mac::CFCastStrict(test_bag_mutable)); EXPECT_EQ(test_bool, base::mac::CFCastStrict(test_bool)); EXPECT_EQ(test_data, base::mac::CFCastStrict(test_data)); EXPECT_EQ(test_data_mutable, base::mac::CFCastStrict(test_data_mutable)); EXPECT_EQ(test_date, base::mac::CFCastStrict(test_date)); EXPECT_EQ(test_dict, base::mac::CFCastStrict(test_dict)); EXPECT_EQ(test_dict_mutable, base::mac::CFCastStrict(test_dict_mutable)); EXPECT_EQ(test_number, base::mac::CFCastStrict(test_number)); EXPECT_EQ(test_null, base::mac::CFCastStrict(test_null)); EXPECT_EQ(test_set, base::mac::CFCastStrict(test_set)); EXPECT_EQ(test_set_mutable, base::mac::CFCastStrict(test_set_mutable)); EXPECT_EQ(test_str, base::mac::CFCastStrict(test_str)); EXPECT_EQ(test_str_const, base::mac::CFCastStrict(test_str_const)); EXPECT_EQ(test_str_mutable, base::mac::CFCastStrict(test_str_mutable)); // CFCastStrict: Giving a NULL provides a NULL. EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); EXPECT_FALSE(base::mac::CFCastStrict(NULL)); }