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/mac/scoped_cftyperef.h" | 7 #include "base/mac/scoped_cftyperef.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 TEST(FoundationUtilTest, CFCast) { | 10 TEST(FoundationUtilTest, CFCast) { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 EXPECT_FALSE(base::mac::CFCastStrict<CFBagRef>(NULL)); | 152 EXPECT_FALSE(base::mac::CFCastStrict<CFBagRef>(NULL)); |
153 EXPECT_FALSE(base::mac::CFCastStrict<CFBooleanRef>(NULL)); | 153 EXPECT_FALSE(base::mac::CFCastStrict<CFBooleanRef>(NULL)); |
154 EXPECT_FALSE(base::mac::CFCastStrict<CFDataRef>(NULL)); | 154 EXPECT_FALSE(base::mac::CFCastStrict<CFDataRef>(NULL)); |
155 EXPECT_FALSE(base::mac::CFCastStrict<CFDateRef>(NULL)); | 155 EXPECT_FALSE(base::mac::CFCastStrict<CFDateRef>(NULL)); |
156 EXPECT_FALSE(base::mac::CFCastStrict<CFDictionaryRef>(NULL)); | 156 EXPECT_FALSE(base::mac::CFCastStrict<CFDictionaryRef>(NULL)); |
157 EXPECT_FALSE(base::mac::CFCastStrict<CFNullRef>(NULL)); | 157 EXPECT_FALSE(base::mac::CFCastStrict<CFNullRef>(NULL)); |
158 EXPECT_FALSE(base::mac::CFCastStrict<CFNumberRef>(NULL)); | 158 EXPECT_FALSE(base::mac::CFCastStrict<CFNumberRef>(NULL)); |
159 EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL)); | 159 EXPECT_FALSE(base::mac::CFCastStrict<CFSetRef>(NULL)); |
160 EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL)); | 160 EXPECT_FALSE(base::mac::CFCastStrict<CFStringRef>(NULL)); |
161 } | 161 } |
162 | |
163 TEST(FoundationUtilTest, ObjCCast) { | |
164 id test_array = [NSArray arrayWithObjects: | |
Mark Mentovai
2011/10/19 22:59:32
Let’s get a ScopedNSAutoreleasePool in here to mak
| |
165 @"whoomp", @"there", @"it", @"is", nil]; | |
166 id test_array_mutable = [NSMutableArray arrayWithObjects: | |
167 @"a man", @"a plan", | |
168 @"a canal", @"panama", | |
169 nil]; | |
170 id test_data = [NSData data]; | |
171 id test_date = [NSDate date]; | |
172 id test_dict = [NSDictionary | |
173 dictionaryWithObject:[NSNumber numberWithInt:42] | |
Mark Mentovai
2011/10/19 22:59:32
Formatting nit: I find it clearer to do
id test_
| |
174 forKey:@"meaning"]; | |
175 id test_dict_mutable = [NSMutableDictionary dictionaryWithCapacity:10]; | |
176 id test_number = [NSNumber numberWithInt:42]; | |
177 id test_null = [NSNull null]; | |
178 id test_set = [NSSet setWithObject:@"string object"]; | |
179 id test_set_mutable = [NSMutableSet setWithCapacity:10]; | |
180 id test_str = @"bonjour"; | |
Mark Mentovai
2011/10/19 22:59:32
As in the CF test:
This one is test_str_const. Yo
| |
181 id test_str_mutable = [NSMutableString stringWithCapacity:10]; | |
182 | |
183 // Make sure the allocations of CF types are good. | |
Mark Mentovai
2011/10/19 22:59:32
Adjust the comments because this test isn’t about
| |
184 EXPECT_TRUE(test_array); | |
185 EXPECT_TRUE(test_array_mutable); | |
186 EXPECT_TRUE(test_data); | |
187 EXPECT_TRUE(test_date); | |
188 EXPECT_TRUE(test_dict); | |
189 EXPECT_TRUE(test_dict_mutable); | |
190 EXPECT_TRUE(test_number); | |
191 EXPECT_TRUE(test_null); | |
192 EXPECT_TRUE(test_set); | |
193 EXPECT_TRUE(test_set_mutable); | |
194 EXPECT_TRUE(test_str); | |
195 EXPECT_TRUE(test_str_mutable); | |
196 | |
197 // Casting the id correctly provides the same pointer. | |
198 EXPECT_EQ(test_array, base::mac::ObjCCast<NSArray>(test_array)); | |
199 EXPECT_EQ(test_array_mutable, | |
200 base::mac::ObjCCast<NSArray>(test_array_mutable)); | |
201 EXPECT_EQ(test_data, base::mac::ObjCCast<NSData>(test_data)); | |
202 EXPECT_EQ(test_date, base::mac::ObjCCast<NSDate>(test_date)); | |
203 EXPECT_EQ(test_dict, base::mac::ObjCCast<NSDictionary>(test_dict)); | |
204 EXPECT_EQ(test_dict_mutable, | |
205 base::mac::ObjCCast<NSDictionary>(test_dict_mutable)); | |
206 EXPECT_EQ(test_number, base::mac::ObjCCast<NSNumber>(test_number)); | |
207 EXPECT_EQ(test_null, base::mac::ObjCCast<NSNull>(test_null)); | |
208 EXPECT_EQ(test_set, base::mac::ObjCCast<NSSet>(test_set)); | |
209 EXPECT_EQ(test_set_mutable, base::mac::ObjCCast<NSSet>(test_set_mutable)); | |
210 EXPECT_EQ(test_str, base::mac::ObjCCast<NSString>(test_str)); | |
211 EXPECT_EQ(test_str_mutable, | |
212 base::mac::ObjCCast<NSString>(test_str_mutable)); | |
213 | |
214 // When given an incorrect ObjC cast, provide NULL. | |
Mark Mentovai
2011/10/19 22:59:32
nil.
| |
215 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array)); | |
216 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_array_mutable)); | |
217 EXPECT_FALSE(base::mac::ObjCCast<NSString>(test_data)); | |
218 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_date)); | |
219 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(test_dict)); | |
220 EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(test_dict_mutable)); | |
221 EXPECT_FALSE(base::mac::ObjCCast<NSNull>(test_number)); | |
222 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_null)); | |
223 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(test_set)); | |
224 EXPECT_FALSE(base::mac::ObjCCast<NSDate>(test_set_mutable)); | |
225 EXPECT_FALSE(base::mac::ObjCCast<NSData>(test_str)); | |
226 EXPECT_FALSE(base::mac::ObjCCast<NSArray>(test_str_mutable)); | |
227 | |
228 // Giving a NULL provides a NULL. | |
Mark Mentovai
2011/10/19 22:59:32
nil, and nil as the argument to each of these cast
| |
229 EXPECT_FALSE(base::mac::ObjCCast<NSArray>(NULL)); | |
230 EXPECT_FALSE(base::mac::ObjCCast<NSData>(NULL)); | |
231 EXPECT_FALSE(base::mac::ObjCCast<NSDate>(NULL)); | |
232 EXPECT_FALSE(base::mac::ObjCCast<NSDictionary>(NULL)); | |
233 EXPECT_FALSE(base::mac::ObjCCast<NSNull>(NULL)); | |
234 EXPECT_FALSE(base::mac::ObjCCast<NSNumber>(NULL)); | |
235 EXPECT_FALSE(base::mac::ObjCCast<NSSet>(NULL)); | |
236 EXPECT_FALSE(base::mac::ObjCCast<NSString>(NULL)); | |
237 | |
238 // ObjCCastStrict: correct cast results in correct pointer being returned. | |
239 EXPECT_EQ(test_array, base::mac::ObjCCastStrict<NSArray>(test_array)); | |
240 EXPECT_EQ(test_array_mutable, | |
241 base::mac::ObjCCastStrict<NSArray>(test_array_mutable)); | |
242 EXPECT_EQ(test_data, base::mac::ObjCCastStrict<NSData>(test_data)); | |
243 EXPECT_EQ(test_date, base::mac::ObjCCastStrict<NSDate>(test_date)); | |
244 EXPECT_EQ(test_dict, base::mac::ObjCCastStrict<NSDictionary>(test_dict)); | |
245 EXPECT_EQ(test_dict_mutable, | |
246 base::mac::ObjCCastStrict<NSDictionary>(test_dict_mutable)); | |
247 EXPECT_EQ(test_number, base::mac::ObjCCastStrict<NSNumber>(test_number)); | |
248 EXPECT_EQ(test_null, base::mac::ObjCCastStrict<NSNull>(test_null)); | |
249 EXPECT_EQ(test_set, base::mac::ObjCCastStrict<NSSet>(test_set)); | |
250 EXPECT_EQ(test_set_mutable, | |
251 base::mac::ObjCCastStrict<NSSet>(test_set_mutable)); | |
252 EXPECT_EQ(test_str, base::mac::ObjCCastStrict<NSString>(test_str)); | |
253 EXPECT_EQ(test_str_mutable, | |
254 base::mac::ObjCCastStrict<NSString>(test_str_mutable)); | |
255 | |
256 // ObjCCastStrict: Giving a NULL provides a NULL. | |
Mark Mentovai
2011/10/19 22:59:32
And again here.
| |
257 EXPECT_FALSE(base::mac::ObjCCastStrict<NSArray>(NULL)); | |
258 EXPECT_FALSE(base::mac::ObjCCastStrict<NSData>(NULL)); | |
259 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDate>(NULL)); | |
260 EXPECT_FALSE(base::mac::ObjCCastStrict<NSDictionary>(NULL)); | |
261 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNull>(NULL)); | |
262 EXPECT_FALSE(base::mac::ObjCCastStrict<NSNumber>(NULL)); | |
263 EXPECT_FALSE(base::mac::ObjCCastStrict<NSSet>(NULL)); | |
264 EXPECT_FALSE(base::mac::ObjCCastStrict<NSString>(NULL)); | |
265 } | |
OLD | NEW |