aboutsummaryrefslogtreecommitdiff
path: root/AddressBook/GTMABAddressBookTest.m
blob: 0d1a3cd6835858526f973ac8d005b52314197338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//
//  GTMAddressBookTest.m
//
//  Copyright 2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import "GTMABAddressBook.h"

#if GTM_IPHONE_SDK
#import "UIKit/UIKit.h"
#else
#import <AppKit/AppKit.h>
#endif  // GTM_IPHONE_SDK

static NSString *const kGTMABTestFirstName = @"GTMABAddressBookTestFirstName";
static NSString *const kGTMABTestLastName = @"GTMABAddressBookTestLastName";
static NSString *const kGTMABTestGroupName = @"GTMABAddressBookTestGroupName";

@interface GTMABAddressBookTest : GTMTestCase {
 @private
  GTMABAddressBook *book_;
}
@end


@implementation GTMABAddressBookTest
- (void)setUp {
  // Create a book forcing it out of it's autorelease pool.
  // I force it out of the release pool, so that we will see any errors
  // for it immediately at teardown, and it will be clear which release
  // caused us problems.
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  book_ = [[GTMABAddressBook addressBook] retain];
  [pool release];
  STAssertNotNil(book_, nil);
  NSArray *people
    = [book_ peopleWithCompositeNameWithPrefix:kGTMABTestFirstName];
  GTMABPerson *person;
  GTM_FOREACH_OBJECT(person, people) {
    [book_ removeRecord:person];
  }
  NSArray *groups
    = [book_ groupsWithCompositeNameWithPrefix:kGTMABTestGroupName];
  GTMABGroup *group;
  GTM_FOREACH_OBJECT(group, groups) {
    [book_ removeRecord:group];
  }
  [book_ save];
}

- (void)tearDown {
  [book_ release];
}

- (void)testGenericAddressBook {
  STAssertEqualObjects([GTMABAddressBook localizedLabel:(NSString *)kABHomeLabel],
                       @"home",
                       nil);
  STAssertThrows([GTMABRecord recordWithRecord:nil], nil);
}

- (void)testAddingAndRemovingPerson {
  // Create a person
  GTMABPerson *person = [GTMABPerson personWithFirstName:kGTMABTestFirstName
                                                lastName:kGTMABTestLastName];
  STAssertNotNil(person, nil);

  // Add person
  NSArray *people = [book_ people];
  STAssertFalse([people containsObject:person], nil);
  STAssertTrue([book_ addRecord:person], nil);
#if GTM_IPHONE_SDK && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_3_2)
  // Normally this next line would be STAssertTrue, however due to
  // Radar 6200638: ABAddressBookHasUnsavedChanges doesn't work
  // We will check to make sure it stays broken ;-)
  STAssertFalse([book_ hasUnsavedChanges], nil);
#else  // GTM_IPHONE_SDK
  STAssertTrue([book_ hasUnsavedChanges], nil);
#endif  // GTM_IPHONE_SDK

  people = [book_ people];
  STAssertNotNil(people, nil);
#if GTM_IPHONE_SDK
  // Normally this next line would be STAssertTrue, however due to
  // Radar 6200703: ABAddressBookAddRecord doesn't add an item to the people
  //                array until it's saved
  // We will check to make sure it stays broken ;-)
  STAssertFalse([people containsObject:person], nil);
#else  // GTM_IPHONE_SDK
  STAssertTrue([people containsObject:person], nil);
#endif  // GTM_IPHONE_SDK

  // Save book_
  STAssertTrue([book_ save], nil);
  people = [book_ people];
  STAssertNotNil(people, nil);
  STAssertTrue([people containsObject:person], nil);
  people = [book_ peopleWithCompositeNameWithPrefix:kGTMABTestFirstName];
  STAssertEqualObjects([people objectAtIndex:0], person, nil);

  GTMABRecordID recordID = [person recordID];
  STAssertNotEquals(recordID, kGTMABRecordInvalidID, nil);

  GTMABRecord *record = [book_ personForId:recordID];
  STAssertEqualObjects(record, person, nil);

  // Remove person
  STAssertTrue([book_ removeRecord:person], nil);
  people = [book_ peopleWithCompositeNameWithPrefix:kGTMABTestFirstName];
  STAssertEquals([people count], (NSUInteger)0, nil);

#if GTM_IPHONE_SDK && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_3_2)
  // Normally this next line would be STAssertTrue, however due to
  // Radar 6200638: ABAddressBookHasUnsavedChanges doesn't work
  // We will check to make sure it stays broken ;-)
  STAssertFalse([book_ hasUnsavedChanges], nil);
#else  // GTM_IPHONE_SDK
  STAssertTrue([book_ hasUnsavedChanges], nil);
#endif  // GTM_IPHONE_SDK
  people = [book_ people];
  STAssertFalse([people containsObject:person], nil);

  // Save Book
  STAssertTrue([book_ save], nil);
  people = [book_ people];
  STAssertFalse([book_ hasUnsavedChanges], nil);
  STAssertFalse([people containsObject:person], nil);
  record = [book_ personForId:recordID];
  STAssertNil(record, nil);

  // Bogus data
  STAssertFalse([book_ addRecord:nil], nil);
  STAssertFalse([book_ removeRecord:nil], nil);

  STAssertNotNULL([book_ addressBookRef], nil);

}

- (void)testAddingAndRemovingGroup {
  // Create a group
  GTMABGroup *group = [GTMABGroup groupNamed:kGTMABTestGroupName];
  STAssertNotNil(group, nil);

  // Add group
  NSArray *groups = [book_ groups];
  STAssertFalse([groups containsObject:group], nil);
  STAssertTrue([book_ addRecord:group], nil);
#if GTM_IPHONE_SDK && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_3_2)
  // Normally this next line would be STAssertTrue, however due to
  // Radar 6200638: ABAddressBookHasUnsavedChanges doesn't work
  // We will check to make sure it stays broken ;-)
  STAssertFalse([book_ hasUnsavedChanges], nil);
#else  // GTM_IPHONE_SDK
  STAssertTrue([book_ hasUnsavedChanges], nil);
#endif  // GTM_IPHONE_SDK

  groups = [book_ groups];
  STAssertNotNil(groups, nil);
#if GTM_IPHONE_SDK
  // Normally this next line would be STAssertTrue, however due to
  // Radar 6200703: ABAddressBookAddRecord doesn't add an item to the groups
  //                array until it's saved
  // We will check to make sure it stays broken ;-)
  STAssertFalse([groups containsObject:group], nil);
#else  // GTM_IPHONE_SDK
  STAssertTrue([groups containsObject:group], nil);
#endif  // GTM_IPHONE_SDK

  // Save book_
  STAssertTrue([book_ save], nil);
  groups = [book_ groups];
  STAssertNotNil(groups, nil);
  STAssertTrue([groups containsObject:group], nil);
  groups = [book_ groupsWithCompositeNameWithPrefix:kGTMABTestGroupName];
  STAssertEqualObjects([groups objectAtIndex:0], group, nil);

  GTMABRecordID recordID = [group recordID];
  STAssertNotEquals(recordID, kGTMABRecordInvalidID, nil);

  GTMABRecord *record = [book_ groupForId:recordID];
  STAssertEqualObjects(record, group, nil);

  // Remove group
  STAssertTrue([book_ removeRecord:group], nil);

#if GTM_IPHONE_SDK && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_3_2)
  // Normally this next line would be STAssertTrue, however due to
  // Radar 6200638: ABAddressBookHasUnsavedChanges doesn't work
  // We will check to make sure it stays broken ;-)
  STAssertFalse([book_ hasUnsavedChanges], nil);
#else  // GTM_IPHONE_SDK
  STAssertTrue([book_ hasUnsavedChanges], nil);
#endif  // GTM_IPHONE_SDK
  groups = [book_ groups];
  STAssertFalse([groups containsObject:group], nil);

  // Save Book
  STAssertTrue([book_ save], nil);
  groups = [book_ groups];
  STAssertFalse([book_ hasUnsavedChanges], nil);
  STAssertFalse([groups containsObject:group], nil);
  groups = [book_ groupsWithCompositeNameWithPrefix:kGTMABTestGroupName];
  STAssertEquals([groups count], (NSUInteger)0, nil);
  record = [book_ groupForId:recordID];
  STAssertNil(record, nil);
}

- (void)testPerson {
  GTMABPerson *person = [[[GTMABPerson alloc] initWithRecord:nil] autorelease];
  STAssertNil(person, nil);
  person = [GTMABPerson personWithFirstName:kGTMABTestFirstName
                                   lastName:nil];
  STAssertNotNil(person, nil);
  STAssertEqualObjects([person compositeName], kGTMABTestFirstName, nil);
  NSString *firstName = [person valueForProperty:kGTMABPersonFirstNameProperty];
  STAssertEqualObjects(firstName, kGTMABTestFirstName, nil);
  NSString *lastName = [person valueForProperty:kGTMABPersonLastNameProperty];
  STAssertNil(lastName, nil);
  STAssertTrue([person removeValueForProperty:kGTMABPersonFirstNameProperty], nil);
  STAssertFalse([person removeValueForProperty:kGTMABPersonFirstNameProperty], nil);
  STAssertFalse([person removeValueForProperty:kGTMABPersonLastNameProperty], nil);
  STAssertFalse([person setValue:nil forProperty:kGTMABPersonFirstNameProperty], nil);
  STAssertFalse([person setValue:[NSNumber numberWithInt:1]
                     forProperty:kGTMABPersonFirstNameProperty], nil);
  STAssertFalse([person setValue:@"Bart"
                     forProperty:kGTMABPersonBirthdayProperty], nil);

  GTMABPropertyType property
    = [GTMABPerson typeOfProperty:kGTMABPersonLastNameProperty];
  STAssertEquals(property, (GTMABPropertyType)kGTMABStringPropertyType, nil);

  NSString *string
    = [GTMABPerson localizedPropertyName:kGTMABPersonLastNameProperty];
  STAssertEqualObjects(string, @"Last", nil);

  string = [GTMABPerson localizedPropertyName:kGTMABRecordInvalidID];
#ifdef GTM_IPHONE_SDK
  STAssertEqualObjects(string, kGTMABUnknownPropertyName, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(string, kGTMABRecordInvalidID, nil);
#endif  // GTM_IPHONE_SDK
  string = [person description];
  STAssertNotNil(string, nil);

  GTMABPersonCompositeNameFormat format = [GTMABPerson compositeNameFormat];
  STAssertTrue(format == kABPersonCompositeNameFormatFirstNameFirst ||
               format == kABPersonCompositeNameFormatLastNameFirst, nil);

  NSData *data = [person imageData];
  STAssertNil(data, nil);
  STAssertTrue([person setImageData:nil], nil);
  data = [person imageData];
  STAssertNil(data, nil);
  NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  NSString *phonePath = [bundle pathForResource:@"phone" ofType:@"png"];
  STAssertNotNil(phonePath, nil);
  GTMABImage *image
    = [[[GTMABImage alloc] initWithContentsOfFile:phonePath] autorelease];
  STAssertNotNil(image, nil);
#ifdef GTM_IPHONE_SDK
  data = UIImagePNGRepresentation(image);
#else  // GTM_IPHONE_SDK
  data = [image TIFFRepresentation];
#endif  // GTM_IPHONE_SDK
  STAssertTrue([person setImageData:data], nil);
  NSData *data2 = [person imageData];
  STAssertEqualObjects(data, data2, nil);
  STAssertTrue([person setImageData:nil], nil);
  data = [person imageData];
  STAssertNil(data, nil);

  STAssertTrue([person setImage:image], nil);
  GTMABImage *image2 = [person image];
  STAssertNotNil(image2, nil);
#ifdef GTM_IPHONE_SDK
  STAssertEqualObjects(UIImagePNGRepresentation(image),
                       UIImagePNGRepresentation(image2), nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects([image TIFFRepresentation],
                       [image2 TIFFRepresentation], nil);
#endif  // GTM_IPHONE_SDK

  person = [GTMABPerson personWithFirstName:kGTMABTestFirstName
                                   lastName:kGTMABTestLastName];

  data = [NSData dataWithBytes:"a" length:1];
  STAssertFalse([person setImageData:data], nil);

  GTMABMutableMultiValue *value
    = [GTMABMutableMultiValue valueWithPropertyType:kGTMABStringPropertyType];
  STAssertNotNil(value, nil);
  STAssertNotEquals([value addValue:@"222-222-2222"
                          withLabel:(CFStringRef)kABHomeLabel],
                    kGTMABMultiValueInvalidIdentifier, nil);
  STAssertNotEquals([value addValue:@"333-333-3333"
                          withLabel:(CFStringRef)kABWorkLabel],
                    kGTMABMultiValueInvalidIdentifier, nil);
  STAssertTrue([person setValue:value
                    forProperty:kGTMABPersonPhoneProperty], nil);
  id value2 = [person valueForProperty:kGTMABPersonPhoneProperty];
  STAssertNotNil(value2, nil);
  STAssertEqualObjects(value, value2, nil);
  STAssertEquals([value hash], [value2 hash], nil);
  STAssertNotEquals([person hash], (NSUInteger)0, nil);
}

- (void)testGroup {
  GTMABGroup *group = [[[GTMABGroup alloc] initWithRecord:nil] autorelease];
  STAssertNil(group, nil);
  group = [GTMABGroup groupNamed:kGTMABTestGroupName];
  STAssertNotNil(group, nil);
  STAssertEqualObjects([group compositeName], kGTMABTestGroupName, nil);
  NSString *name = [group valueForProperty:kABGroupNameProperty];
  STAssertEqualObjects(name, kGTMABTestGroupName, nil);
  NSString *lastName = [group valueForProperty:kGTMABPersonLastNameProperty];
  STAssertNil(lastName, nil);
  STAssertTrue([group removeValueForProperty:kABGroupNameProperty], nil);
  STAssertFalse([group removeValueForProperty:kABGroupNameProperty], nil);
  STAssertFalse([group removeValueForProperty:kGTMABPersonLastNameProperty], nil);
  STAssertFalse([group setValue:nil forProperty:kABGroupNameProperty], nil);
  STAssertFalse([group setValue:[NSNumber numberWithInt:1]
                    forProperty:kABGroupNameProperty], nil);
  STAssertFalse([group setValue:@"Bart"
                    forProperty:kGTMABPersonBirthdayProperty], nil);

  ABPropertyType property = [GTMABGroup typeOfProperty:kABGroupNameProperty];
  STAssertEquals(property, (ABPropertyType)kGTMABStringPropertyType, nil);

  property = [GTMABGroup typeOfProperty:kGTMABPersonLastNameProperty];
  STAssertEquals(property, (ABPropertyType)kGTMABInvalidPropertyType, nil);

  NSString *string = [GTMABGroup localizedPropertyName:kABGroupNameProperty];
  STAssertEqualObjects(string, @"Name", nil);

  string = [GTMABGroup localizedPropertyName:kGTMABPersonLastNameProperty];
  STAssertEqualObjects(string, kGTMABUnknownPropertyName, nil);

  string = [GTMABGroup localizedPropertyName:kGTMABRecordInvalidID];
  STAssertEqualObjects(string, kGTMABUnknownPropertyName, nil);

  string = [group description];
  STAssertNotNil(string, nil);

  // Adding and removing members
  group = [GTMABGroup groupNamed:kGTMABTestGroupName];
  NSArray *members = [group members];
  STAssertEquals([members count], (NSUInteger)0, @"Members: %@", members);

  STAssertFalse([group addMember:nil], nil);

  members = [group members];
  STAssertEquals([members count], (NSUInteger)0, @"Members: %@", members);

  GTMABPerson *person = [GTMABPerson personWithFirstName:kGTMABTestFirstName
                                                lastName:kGTMABTestLastName];
  STAssertNotNil(person, nil);
  STAssertTrue([book_ addRecord:person], nil);
  STAssertTrue([book_ save], nil);
  STAssertTrue([book_ addRecord:group], nil);
  STAssertTrue([book_ save], nil);
  STAssertTrue([group addMember:person], nil);
  STAssertTrue([book_ save], nil);
  members = [group members];
  STAssertEquals([members count], (NSUInteger)1, @"Members: %@", members);
  STAssertTrue([group removeMember:person], nil);
  STAssertFalse([group removeMember:person], nil);
  STAssertFalse([group removeMember:nil], nil);
  STAssertTrue([book_ removeRecord:group], nil);
  STAssertTrue([book_ removeRecord:person], nil);
  STAssertTrue([book_ save], nil);
}


- (void)testMultiValues {
  STAssertThrows([[GTMABMultiValue alloc] init], nil);
  STAssertThrows([[GTMABMutableMultiValue alloc] init], nil);
  GTMABMultiValue *value = [[GTMABMultiValue alloc] initWithMultiValue:nil];
  STAssertNil(value, nil);
  GTMABMutableMultiValue *mutValue
    = [GTMABMutableMultiValue valueWithPropertyType:kGTMABInvalidPropertyType];
  STAssertNil(mutValue, nil);
  mutValue
    = [[[GTMABMutableMultiValue alloc]
        initWithMutableMultiValue:nil] autorelease];
  STAssertNil(mutValue, nil);
  mutValue
    = [[[GTMABMutableMultiValue alloc]
        initWithMultiValue:nil] autorelease];
  STAssertNil(mutValue, nil);
#if GTM_IPHONE_SDK
  // Only the IPhone version actually allows you to check types of a multivalue
  // before you stick anything in it
  const GTMABPropertyType types[] = {
    kGTMABStringPropertyType,
    kGTMABIntegerPropertyType,
    kGTMABRealPropertyType,
    kGTMABDateTimePropertyType,
    kGTMABDictionaryPropertyType,
    kGTMABMultiStringPropertyType,
    kGTMABMultiIntegerPropertyType,
    kGTMABMultiRealPropertyType,
    kGTMABMultiDateTimePropertyType,
    kGTMABMultiDictionaryPropertyType
  };
  for (size_t i = 0; i < sizeof(types) / sizeof(GTMABPropertyType); ++i) {
    mutValue = [GTMABMutableMultiValue valueWithPropertyType:types[i]];
    STAssertNotNil(mutValue, nil);
    // Oddly the Apple APIs allow you to create a mutable multi value with
    // either a property type of kABFooPropertyType or kABMultiFooPropertyType
    // and apparently you get back basically the same thing. However if you
    // ask a type that you created with kABMultiFooPropertyType for it's type
    // it returns just kABFooPropertyType.
    STAssertEquals([mutValue propertyType],
                   (GTMABPropertyType)(types[i] & ~kABMultiValueMask), nil);
  }
#endif  // GTM_IPHONE_SDK
  mutValue
    = [GTMABMutableMultiValue valueWithPropertyType:kGTMABStringPropertyType];
  STAssertNotNil(mutValue, nil);
  value = [[mutValue copy] autorelease];
  STAssertEqualObjects([value class], [GTMABMultiValue class], nil);
  mutValue = [[value mutableCopy] autorelease];
  STAssertEqualObjects([mutValue class], [GTMABMutableMultiValue class], nil);
  STAssertEquals([mutValue count], (NSUInteger)0, nil);
  STAssertNil([mutValue valueAtIndex:0], nil);
  STAssertNil([mutValue labelAtIndex:0], nil);
#if GTM_IPHONE_SDK
  STAssertEquals([mutValue identifierAtIndex:0],
                 kGTMABMultiValueInvalidIdentifier, nil);
  STAssertEquals([mutValue propertyType],
                 (GTMABPropertyType)kGTMABStringPropertyType, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects([mutValue identifierAtIndex:0],
                       kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK
  GTMABMultiValueIdentifier ident
    = [mutValue addValue:nil withLabel:(CFStringRef)kABHomeLabel];
#if GTM_IPHONE_SDK
  STAssertEquals(ident, kGTMABMultiValueInvalidIdentifier, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(ident, kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK

  ident = [mutValue addValue:@"val1"
                   withLabel:nil];
#if GTM_IPHONE_SDK
  STAssertEquals(ident, kGTMABMultiValueInvalidIdentifier, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(ident, kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK
  ident = [mutValue insertValue:@"val1"
                      withLabel:nil
                        atIndex:0];
#if GTM_IPHONE_SDK
  STAssertEquals(ident, kGTMABMultiValueInvalidIdentifier, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(ident, kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK
  ident = [mutValue insertValue:nil
                      withLabel:(CFStringRef)kABHomeLabel
                        atIndex:0];
#if GTM_IPHONE_SDK
  STAssertEquals(ident, kGTMABMultiValueInvalidIdentifier, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(ident, kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK
  ident = [mutValue addValue:@"val1"
                   withLabel:(CFStringRef)kABHomeLabel];
#if GTM_IPHONE_SDK
  STAssertNotEquals(ident, kGTMABMultiValueInvalidIdentifier, nil);
#else  // GTM_IPHONE_SDK
  STAssertNotEqualObjects(ident, kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK
  GTMABMultiValueIdentifier identCheck = [mutValue identifierAtIndex:0];
#if GTM_IPHONE_SDK
  STAssertEquals(ident, identCheck, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(ident, identCheck, nil);
#endif  // GTM_IPHONE_SDK
  NSUInteger idx = [mutValue indexForIdentifier:ident];
  STAssertEquals(idx, (NSUInteger)0, nil);
  STAssertTrue([mutValue replaceLabelAtIndex:0
                                   withLabel:(CFStringRef)kABWorkLabel], nil);
  STAssertFalse([mutValue replaceLabelAtIndex:10
                                    withLabel:(CFStringRef)kABWorkLabel], nil);
  STAssertTrue([mutValue replaceValueAtIndex:0
                                   withValue:@"newVal1"], nil);
  STAssertFalse([mutValue replaceValueAtIndex:10
                                    withValue:@"newVal1"], nil);

  STAssertEqualObjects([mutValue valueForIdentifier:ident], @"newVal1", nil);
  STAssertEqualObjects([mutValue labelForIdentifier:ident],
                       (NSString *)kABWorkLabel, nil);

  GTMABMultiValueIdentifier ident2
    = [mutValue insertValue:@"val2"
                  withLabel:(CFStringRef)kABOtherLabel
                    atIndex:0];
  STAssertNotEquals(ident2, kGTMABMultiValueInvalidIdentifier, nil);
  STAssertNotEquals(ident2, ident, nil);
  GTMABMultiValueIdentifier ident3
    = [mutValue insertValue:@"val3"
                  withLabel:(CFStringRef)kGTMABPersonPhoneMainLabel
                    atIndex:10];
#if GTM_IPHONE_SDK
  STAssertEquals(ident3, kGTMABMultiValueInvalidIdentifier, nil);
#else  // GTM_IPHONE_SDK
  STAssertEqualObjects(ident3, kGTMABMultiValueInvalidIdentifier, nil);
#endif  // GTM_IPHONE_SDK
  NSUInteger idx3 = [mutValue indexForIdentifier:ident3];
  STAssertEquals(idx3, (NSUInteger)NSNotFound, nil);
  STAssertTrue([mutValue removeValueAndLabelAtIndex:1], nil);
  STAssertFalse([mutValue removeValueAndLabelAtIndex:1], nil);

  NSUInteger idx4
    = [mutValue indexForIdentifier:kGTMABMultiValueInvalidIdentifier];
  STAssertEquals(idx4, (NSUInteger)NSNotFound, nil);

  STAssertNotNULL([mutValue multiValueRef], nil);

  // Enumerator test
  mutValue
    = [GTMABMutableMultiValue valueWithPropertyType:kGTMABIntegerPropertyType];
  STAssertNotNil(mutValue, nil);
  for (int i = 0; i < 100; i++) {
    NSString *label = [NSString stringWithFormat:@"label %d", i];
    NSNumber *val = [NSNumber numberWithInt:i];
    STAssertNotEquals([mutValue addValue:val
                               withLabel:(CFStringRef)label],
                      kGTMABMultiValueInvalidIdentifier, nil);
  }
  int count = 0;
  NSString *label;
  GTM_FOREACH_ENUMEREE(label, [mutValue labelEnumerator]) {
    NSString *testLabel = [NSString stringWithFormat:@"label %d", count++];
    STAssertEqualObjects(label, testLabel, nil);
  }
  count = 0;
  value = [[mutValue copy] autorelease];
  NSNumber *val;
  GTM_FOREACH_ENUMEREE(val, [value valueEnumerator]) {
    STAssertEqualObjects(val, [NSNumber numberWithInt:count++], nil);
  }

  // Test messing with the values while we're enumerating them
  NSEnumerator *labelEnum = [mutValue labelEnumerator];
  NSEnumerator *valueEnum = [mutValue valueEnumerator];
  STAssertNotNil(labelEnum, nil);
  STAssertNotNil(valueEnum, nil);
  STAssertNotNil([labelEnum nextObject], nil);
  STAssertNotNil([valueEnum nextObject], nil);
  STAssertTrue([mutValue removeValueAndLabelAtIndex:0], nil);
  STAssertThrows([labelEnum nextObject], nil);
  STAssertThrows([valueEnum nextObject], nil);

  // Test messing with the values while we're fast enumerating them
  // Should throw an exception on the second access.
   BOOL exceptionThrown = NO;
  // Start at one because we removed index 0 above.
  count = 1;
  @try {
    GTM_FOREACH_ENUMEREE(label, [mutValue labelEnumerator]) {
      NSString *testLabel = [NSString stringWithFormat:@"label %d", count++];
      STAssertEqualObjects(label, testLabel, nil);
      STAssertTrue([mutValue removeValueAndLabelAtIndex:50], nil);
    }
  } @catch(NSException *e) {
    STAssertEqualObjects([e name], NSGenericException, @"Got %@ instead", e);
    STAssertEquals(count, 2,
                   @"Should have caught it on the second access");
    exceptionThrown = YES;
  }  // COV_NF_LINE - because we always catch, this brace doesn't get exec'd
  STAssertTrue(exceptionThrown, @"We should have thrown an exception"
               @" because the values under the enumerator were modified");

}

#if GTM_IPHONE_SDK
- (void)testRadar6208390 {
  GTMABPropertyType types[] = {
    kGTMABStringPropertyType,
    kGTMABIntegerPropertyType,
    kGTMABRealPropertyType,
    kGTMABDateTimePropertyType,
    kGTMABDictionaryPropertyType
  };
  for (size_t j = 0; j < sizeof(types) / sizeof(ABPropertyType); ++j) {
    ABPropertyType type = types[j];
#ifdef GTM_IPHONE_SDK
    ABMultiValueRef ref = ABMultiValueCreateMutable(type);
#else  // GTM_IPHONE_SDK
    ABMutableMultiValueRef ref = ABMultiValueCreateMutable();
#endif  // GTM_IPHONE_SDK
    STAssertNotNULL(ref, nil);
    NSString *label = [[NSString alloc] initWithString:@"label"];
    STAssertNotNil(label, nil);
    id val = nil;
    if (type == kGTMABDictionaryPropertyType) {
      val = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"1", nil];
    } else if (type == kGTMABStringPropertyType) {
      val = [[NSString alloc] initWithFormat:@"value %zu", j];
    } else if (type == kGTMABIntegerPropertyType
               || type == kGTMABRealPropertyType ) {
      val = [[NSNumber alloc] initWithInt:143];
    } else if (type == kGTMABDateTimePropertyType) {
      val = [[NSDate alloc] init];
    }
    STAssertNotNil(val,
                   @"Testing type %d, %@", type, val);
    NSUInteger firstRetainCount = [val retainCount];
    STAssertNotEquals(firstRetainCount,
                      (NSUInteger)0,
                      @"Testing type %d, %@", type, val);

    GTMABMultiValueIdentifier identifier;
    STAssertTrue(ABMultiValueAddValueAndLabel(ref,
                                              val,
                                              (CFStringRef)label,
                                              &identifier),
                 @"Testing type %d, %@", type, val);
    NSUInteger secondRetainCount = [val retainCount];
    STAssertEquals(firstRetainCount + 1,
                   secondRetainCount,
                   @"Testing type %d, %@", type, val);
    [label release];
    [val release];
    NSUInteger thirdRetainCount = [val retainCount];
    STAssertEquals(firstRetainCount,
                   thirdRetainCount,
                   @"Testing type %d, %@", type, val);

    id oldVal = val;
    val = (id)ABMultiValueCopyValueAtIndex(ref, 0);
    NSUInteger fourthRetainCount = [val retainCount];

    // kABDictionaryPropertyTypes appear to do an actual copy, so the retain
    // count checking trick won't work. We only check the retain count if
    // we didn't get a new version.
    if (val == oldVal) {
      if (type == kGTMABIntegerPropertyType
          || type == kGTMABRealPropertyType) {
        // We are verifying that yes indeed 6208390 is still broken
        STAssertEquals(fourthRetainCount,
                       thirdRetainCount,
                       @"Testing type %d, %@. If you see this error it may "
                       @"be time to update the code to change retain behaviors"
                       @"with this os version", type, val);
      } else {
        STAssertEquals(fourthRetainCount,
                       thirdRetainCount + 1,
                       @"Testing type %d, %@", type, val);
        [val release];
      }
    } else {
      [val release];
    }
    CFRelease(ref);
  }
}

// Globals used by testRadar6240394.
static GTMABPropertyID gGTMTestID;
static const GTMABPropertyID *gGTMTestIDPtr;

void __attribute__((constructor))SetUpIDForTestRadar6240394(void) {
  // These must be set up BEFORE ABAddressBookCreate is called.
  gGTMTestID = kGTMABPersonLastNameProperty;
  gGTMTestIDPtr = &kGTMABPersonLastNameProperty;
}

- (void)testRadar6240394 {
  // As of iPhone SDK 2.1, the property IDs aren't initialized until
  // ABAddressBookCreate is actually called. They will return zero until
  // then. Logged as radar 6240394.
  STAssertEquals(gGTMTestID, 0, @"If this isn't zero, Apple has fixed 6240394");
  (void)ABAddressBookCreate();
  STAssertEquals(*gGTMTestIDPtr, kGTMABPersonLastNameProperty,
                 @"If this doesn't work, something else has broken");
}

#endif  // GTM_IPHONE_SDK
@end