-
Notifications
You must be signed in to change notification settings - Fork 84
/
Tests.m
575 lines (497 loc) · 25.7 KB
/
Tests.m
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
//
// MAKVONotificationCenter_Tests.m
// MAKVONotificationCenter-Tests
//
// Created by Gwynne on 12/1/11.
//
#import "Tests.h"
#import "MAKVONotificationCenter.h"
// IMPORTANT NOTE:
// Several of these tests do a lot of very odd dancing around with autoreleases
// and seemingly phantom retains. All of this is unfortunately necessary in
// order to test the automatic deregistration semantics correctly; doing it
// with ARC turned on would have been even uglier. Autorelease pools have to be
// drained at the right times and observation objects properly retained in
// order to control the object lifetimes without losing them prematurely. It's
// evil.
/******************************************************************************/
@interface TestObserver : NSObject
{
@public
BOOL _triggered, _triggered2;
}
@end
@implementation TestObserver
- (void)observePath:(NSString *)keyPath object:(id)object change:(NSDictionary *)change info:(id)info
{
// STAssertEqualObjects(info, @"test", @"User info was wrong: expected \"test\", got %@", info);
_triggered = YES;
}
- (void)observePath2:(NSString *)keyPath object:(id)object change:(NSDictionary *)change info:(id)info
{
// STAssertEqualObjects(info, @"test", @"User info was wrong: expected \"test\", got %@", info);
_triggered2 = YES;
}
@end
/******************************************************************************/
@interface TestObject : NSObject
@property(nonatomic,assign) BOOL toggle, toggle2;
@end
@implementation TestObject
@synthesize toggle, toggle2;
@end
/******************************************************************************/
@interface TestKeyPaths : NSObject <MAKVOKeyPathSet>
@end
@implementation TestKeyPaths
- (id<NSFastEnumeration>)ma_keyPathsAsSetOfStrings { return [NSSet setWithObjects:@"toggle", @"toggle2", nil]; }
@end
/******************************************************************************/
@interface DeallocTesterSuperclass : NSObject
@end
@implementation DeallocTesterSuperclass
- (void)dealloc { [super dealloc]; }
@end
@interface DeallocTesterSubclass : DeallocTesterSuperclass
@end
@implementation DeallocTesterSubclass
- (void)dealloc { [super dealloc]; }
@end
/******************************************************************************/
@interface MAKVONotificationCenter_Tests ()
@end
/******************************************************************************/
@implementation MAKVONotificationCenter_Tests
- (void)testBasicObserving
{
@autoreleasepool
{
TestObject *target = [[[TestObject alloc] init] autorelease];
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
id<MAKVOObservation> observation = nil;
observer->_triggered = NO;
observation = [target addObserver:observer keyPath:@"toggle" selector:@selector(observePath:object:change:info:) userInfo:@"test" options:0];
target.toggle = YES;
STAssertTrue(observer->_triggered, @"Basic observation was not fired");
observer->_triggered = NO;
[target removeObserver:observer keyPath:@"toggle" selector:@selector(observePath:object:change:info:)];
target.toggle = NO;
STAssertFalse(observer->_triggered, @"Basic observation was not removed");
STAssertFalse(observation.isValid, @"Basic observation was not invalidated");
observer->_triggered = NO;
observation = [target addObserver:observer keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { observer->_triggered = YES; }];
STAssertNotNil(observation, @"nil observation returned");
target.toggle = YES;
STAssertTrue(observer->_triggered, @"Basic block observation was not fired");
observer->_triggered = NO;
[observation remove];
target.toggle = NO;
STAssertFalse(observer->_triggered, @"Basic block observation was not removed");
STAssertFalse(observation.isValid, @"Basic block observation was not invalidated");
}
}
- (void)testReversedObserving
{
@autoreleasepool
{
TestObject *target = [[[TestObject alloc] init] autorelease];
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
id<MAKVOObservation> observation = nil;
observer->_triggered = NO;
observation = [observer observeTarget:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:) userInfo:@"test" options:0];
target.toggle = YES;
STAssertTrue(observer->_triggered, @"Reversed observation was not fired");
observer->_triggered = NO;
[observer stopObserving:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:)];
target.toggle = NO;
STAssertFalse(observer->_triggered, @"Reversed observation was not removed");
STAssertFalse(observation.isValid, @"Reversed observation was not invalidated");
observer->_triggered = NO;
observation = [observer observeTarget:target keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { observer->_triggered = YES; }];
STAssertNotNil(observation, @"nil observation returned");
target.toggle = YES;
STAssertTrue(observer->_triggered, @"Reversed block observation was not fired");
observer->_triggered = NO;
[observation remove];
target.toggle = NO;
STAssertFalse(observer->_triggered, @"Reversed block observation was not removed");
STAssertFalse(observation.isValid, @"Reversed block observation was not invalidated");
}
}
- (void)testAutoDeregistrationObserver
{
@autoreleasepool
{
TestObject *target = [[[TestObject alloc] init] autorelease];
TestObserver __block *observer = [[TestObserver alloc] init];
id<MAKVOObservation> observation = nil;
@autoreleasepool
{
observation = [[target addObserver:observer keyPath:@"toggle" options:0
block:^ (MAKVONotification *notification) { observer->_triggered = YES; }] retain];
}
[observation autorelease]; // balance artificial retain, but do NOT release yet!
[observer release];
STAssertFalse(observation.isValid, @"Observation was not automatically removed when observer was deallocated.");
}
}
- (void)testAutoDeregistrationTarget
{
@autoreleasepool
{
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
TestObject *target = [[TestObject alloc] init];
id<MAKVOObservation> observation = nil;
@autoreleasepool
{
observation = [[target addObserver:observer keyPath:@"toggle" options:0
block:^(MAKVONotification *notification) { observer->_triggered = YES; }] retain];
}
[observation autorelease];
[target release];
STAssertFalse(observation.isValid, @"Observation was not automatically removed when target was deallocated.");
}
}
- (void)testManualDeregistrationObserver
{
@autoreleasepool
{
TestObject *target = [[[TestObject alloc] init] autorelease];
TestObserver __block *observer = [[TestObserver alloc] init];
id<MAKVOObservation> observation = nil;
@autoreleasepool
{
observation = [[target addObserver:observer keyPath:@"toggle" options:MAKeyValueObservingOptionUnregisterManually
block:^ (MAKVONotification *notification) { observer->_triggered = YES; }] retain];
}
[observation autorelease];
[observer release];
STAssertTrue(observation.isValid, @"Observation was automatically removed, but shouldn't have been.");
[observation remove];
STAssertFalse(observation.isValid, @"Observation was not manually removed.");
}
}
/*
// THIS TEST CORRUPTS MEMORY AND CAN NOT BE IMPLEMENTED CORRECTLY.
- (void)testManualDeregistrationTarget
{
@autoreleasepool
{
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
TestObject *target = [[TestObject alloc] init];
id<MAKVOObservation> observation = nil;
@autoreleasepool
{
observation = [[target addObserver:observer keyPath:@"toggle" options:MAKeyValueObservingOptionUnregisterManually
block:^ (MAKVONotification *notification) { observer->_triggered = YES; }] retain];
}
[observation autorelease];
[target release];
STAssertTrue(observation.isValid, @"Observation was automatically removed, but shouldn't have been.");
[observation remove];
STAssertFalse(observation.isValid, @"Observation was not manually removed.");
}
}
*/
- (void)testOptions
{
@autoreleasepool
{
TestObserver __block *observer = [[[TestObserver alloc] init] autorelease];
TestObject __block *target = [[[TestObject alloc] init] autorelease];
id<MAKVOObservation> observation = nil;
observation = [observer observeTarget:target keyPath:@"toggle"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
block:
^ (MAKVONotification *notification) {
STAssertEqualObjects(notification.keyPath, @"toggle", @"Expected keyPath of \"toggle\", got \"%@\"", notification.keyPath);
STAssertEqualObjects(notification.observer, observer, @"Expected observer to be %@, got %@", observer, notification.observer);
STAssertEqualObjects(notification.target, target, @"Expected target to be %@, got %@", target, notification.target);
STAssertEquals(notification.kind, (NSKeyValueChange)NSKeyValueChangeSetting, @"Expected kind to be \"setting\", got %lu", notification.kind);
STAssertEqualObjects(notification.oldValue, [NSNumber numberWithBool:NO], @"Expected old value to be NO, got %@", notification.oldValue);
STAssertEqualObjects(notification.newValue, [NSNumber numberWithBool:YES], @"Expected new value to be YES, got %@", notification.newValue);
STAssertFalse(notification.isPrior, @"Expected prior flag to be NO, it wasn't.");
}];
target.toggle = YES;
[observation remove];
observation = [observer observeTarget:target keyPath:@"toggle"
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial
block:
^ (MAKVONotification *notification) {
STAssertEqualObjects(notification.keyPath, @"toggle", @"Expected keyPath of \"toggle\", got \"%@\"", notification.keyPath);
STAssertEqualObjects(notification.observer, observer, @"Expected observer to be %@, got %@", observer, notification.observer);
STAssertEqualObjects(notification.target, target, @"Expected target to be %@, got %@", target, notification.target);
STAssertEquals(notification.kind, (NSKeyValueChange)NSKeyValueChangeSetting, @"Expected kind to be \"setting\", got %lu", notification.kind);
STAssertNil(notification.oldValue, @"Expected old value to be nil, got %@", notification.oldValue);
STAssertEqualObjects(notification.newValue, [NSNumber numberWithBool:YES], @"Expected new value to be YES, got %@", notification.newValue);
STAssertFalse(notification.isPrior, @"Expected prior flag to be NO, it wasn't.");
}];
// [observation remove]; // unnecessary!
}
}
- (void)testSingleObserverMultipleTargets
{
#define STAssertTriggers(t1, t2, t3, t4, t5) \
STAssert ## t1(trigger1, @"First trigger misfire"); \
STAssert ## t2(trigger2, @"Second trigger misfire"); \
STAssert ## t3(trigger3, @"Third trigger misfire"); \
STAssert ## t4(observer->_triggered, @"Fourth trigger misfire"); \
STAssert ## t5(observer->_triggered2, @"Fifth trigger misfire"); \
do { trigger1 = NO; trigger2 = NO; trigger3 = NO; observer->_triggered = NO; observer->_triggered2 = NO; } while (0)
@autoreleasepool
{
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
TestObject *target1 = [[TestObject alloc] init], *target2 = [[TestObject alloc] init],
*target3 = [[TestObject alloc] init], *target4 = [[TestObject alloc] init];
id<MAKVOObservation> observation1 = nil, observation2 = nil, observation3 = nil, observation4 = nil, observation5 = nil;
BOOL __block trigger1 = NO, __block trigger2 = NO, __block trigger3 = NO;
@autoreleasepool
{
observation1 = [[observer observeTarget:target1 keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger1 = YES; }] retain];
observation2 = [[observer observeTarget:target2 keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger2 = YES; }] retain];
observation3 = [[observer observeTarget:target3 keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger3 = YES; }] retain];
observation4 = [[observer observeTarget:target4 keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
observation5 = [[observer observeTarget:target4 keyPath:@"toggle" selector:@selector(observePath2:object:change:info:)
userInfo:@"test" options:0] retain];
}
[observation1 autorelease];
[observation2 autorelease];
[observation3 autorelease];
[observation4 autorelease];
[observation5 autorelease];
@autoreleasepool
{
target1.toggle = YES;
target2.toggle = YES;
target3.toggle = YES;
target4.toggle = YES;
}
STAssertTriggers(True, True, True, True, True);
[observation1 remove];
@autoreleasepool { target1.toggle = NO; }
STAssertTriggers(False, False, False, False, False);
[target2 release];
STAssertFalse(observation2.isValid, @"Second observation didn't automatically deregister");
@autoreleasepool { target3.toggle = NO; }
STAssertTriggers(False, False, True, False, False);
[observer stopObserving:target4 keyPath:@"toggle" selector:@selector(observePath:object:change:info:)];
@autoreleasepool { target4.toggle = NO; }
STAssertFalse(observation4.isValid, @"Fourth observation wasn't deregistered");
STAssertTriggers(False, False, False, False, True);
@autoreleasepool
{
observation4 = [[observer observeTarget:target4 keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
}
[observation4 autorelease];
[observer stopObserving:target4 keyPath:@"toggle"];
STAssertFalse(observation4.isValid, @"Fourth observation wasn't deregistered again");
STAssertFalse(observation5.isValid, @"Fifth observation wasn't deregistered");
@autoreleasepool
{
observation4 = [[observer observeTarget:target4 keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
observation5 = [[observer observeTarget:target4 keyPath:@"toggle" selector:@selector(observePath2:object:change:info:)
userInfo:@"test" options:0] retain];
}
[observation4 autorelease];
[observation5 autorelease];
[observer stopObservingAllTargets];
STAssertFalse(observation3.isValid, @"Third observation wasn't deregistered");
STAssertFalse(observation4.isValid, @"Fourth observation wasn't deregistered");
STAssertFalse(observation5.isValid, @"Fifth observation wasn't deregistered");
[target1 release];
[target3 release];
[target4 release];
}
#undef STAssertTriggers
}
- (void)testSingleTargetMultipleObservers
{
#define STAssertTriggers(t1, t2, t3, t4, t5) \
STAssert ## t1(trigger1, @"First trigger misfire"); \
STAssert ## t2(trigger2, @"Second trigger misfire"); \
STAssert ## t3(trigger3, @"Third trigger misfire"); \
STAssert ## t4(observer4->_triggered, @"Fourth trigger misfire"); \
STAssert ## t5(observer5->_triggered, @"Fifth trigger misfire"); \
do { trigger1 = NO; trigger2 = NO; trigger3 = NO; observer4->_triggered = NO; observer5->_triggered = NO; } while (0)
@autoreleasepool
{
TestObserver *observer1 = [[TestObserver alloc] init], *observer2 = [[TestObserver alloc] init],
*observer3 = [[TestObserver alloc] init], *observer4 = [[TestObserver alloc] init],
*observer5 = [[TestObserver alloc] init];
TestObject *target = [[[TestObject alloc] init] autorelease];
id<MAKVOObservation> observation1 = nil, observation2 = nil, observation3 = nil, observation4 = nil, observation5 = nil;
BOOL __block trigger1 = NO, __block trigger2 = NO, __block trigger3 = NO;
@autoreleasepool
{
observation1 = [[observer1 observeTarget:target keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger1 = YES; }] retain];
observation2 = [[observer2 observeTarget:target keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger2 = YES; }] retain];
observation3 = [[observer3 observeTarget:target keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger3 = YES; }] retain];
observation4 = [[observer4 observeTarget:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
observation5 = [[observer5 observeTarget:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
}
[observation1 autorelease];
[observation2 autorelease];
[observation3 autorelease];
[observation4 autorelease];
[observation5 autorelease];
@autoreleasepool { target.toggle = YES; }
STAssertTriggers(True, True, True, True, True);
[observation1 remove];
@autoreleasepool { target.toggle = NO; }
STAssertTriggers(False, True, True, True, True);
[observer2 release];
STAssertFalse(observation2.isValid, @"Second observation didn't automatically deregister");
[observer4 stopObserving:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:)];
@autoreleasepool { target.toggle = NO; }
STAssertFalse(observation4.isValid, @"Fourth observation wasn't deregistered");
STAssertTriggers(False, False, True, False, True);
[observer5 stopObserving:target keyPath:@"toggle"];
STAssertFalse(observation5.isValid, @"Fifth observation wasn't deregistered");
@autoreleasepool
{
observation4 = [[observer4 observeTarget:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
observation5 = [[observer5 observeTarget:target keyPath:@"toggle" selector:@selector(observePath:object:change:info:)
userInfo:@"test" options:0] retain];
}
[observation4 autorelease];
[observation5 autorelease];
[target removeAllObservers];
STAssertFalse(observation3.isValid, @"Third observation wasn't deregistered");
STAssertFalse(observation4.isValid, @"Fourth observation wasn't deregistered");
STAssertFalse(observation5.isValid, @"Fifth observation wasn't deregistered");
[observer1 release];
[observer3 release];
[observer4 release];
[observer5 release];
}
#undef STAssertTriggers
}
- (void)testKeyPathProtocol
{
#define STAssertTriggers(t1, t2) \
STAssert ## t1(trigger1, @"First trigger misfire"); \
STAssert ## t2(trigger2, @"Second trigger misfire"); \
do { trigger1 = NO; trigger2 = NO; } while (0)
#define TestKeyPath(paths) do { \
observation = [observer observeTarget:target keyPath:(paths) options:0 block:notificationBlock]; \
target.toggle = YES; \
STAssertTriggers(True, False); \
target.toggle2 = YES; \
STAssertTriggers(False, True); \
[observation remove]; \
} while (0)
@autoreleasepool
{
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
TestObject *target = [[[TestObject alloc] init] autorelease];
TestKeyPaths *keyPaths = [[[TestKeyPaths alloc] init] autorelease];
id<MAKVOObservation> observation = nil;
BOOL __block trigger1 = NO, __block trigger2 = NO;
void (^notificationBlock)(MAKVONotification *) = ^ (MAKVONotification *notification) {
if ([notification.keyPath isEqualToString:@"toggle"])
trigger1 = YES;
else if ([notification.keyPath isEqualToString:@"toggle2"])
trigger2 = YES;
};
TestKeyPath(([NSArray arrayWithObjects:@"toggle", @"toggle2", nil]));
TestKeyPath(([NSSet setWithObjects:@"toggle", @"toggle2", nil]));
TestKeyPath(([NSOrderedSet orderedSetWithObjects:@"toggle", @"toggle2", nil]));
TestKeyPath(keyPaths);
}
#undef TestKeyPath
#undef STAssertTriggers
}
- (void)testArrayTarget
{
#define STAssertTriggers(t1, t2) \
STAssert ## t1(trigger1, @"First trigger misfire"); \
STAssert ## t2(trigger2, @"Second trigger misfire"); \
do { trigger1 = NO; trigger2 = NO; } while (0)
@autoreleasepool
{
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
TestObject *target1 = [[[TestObject alloc] init] autorelease], *target2 = [[[TestObject alloc] init] autorelease];
NSArray *targetArray = [NSArray arrayWithObjects:target1, target2, nil];
id<MAKVOObservation> observation = nil;
BOOL __block trigger1 = NO, __block trigger2 = NO;
void (^notificationBlock)(MAKVONotification *) = ^ (MAKVONotification *notification) {
if (notification.target == target1)
trigger1 = YES;
else if (notification.target == target2)
trigger2 = YES;
};
observation = [observer observeTarget:targetArray keyPath:@"toggle" options:0 block:notificationBlock];
target1.toggle = YES;
STAssertTriggers(True, False);
target2.toggle = YES;
STAssertTriggers(False, True);
target1.toggle = YES;
target2.toggle = YES;
STAssertTriggers(True, True);
[observation remove];
target1.toggle = YES;
target2.toggle = YES;
STAssertTriggers(False, False);
}
}
- (void)testArrayTargetAutoDeregistration
{
@autoreleasepool
{
TestObserver *observer = [[[TestObserver alloc] init] autorelease];
TestObject *target1 = [[[TestObject alloc] init] autorelease], *target2 = [[[TestObject alloc] init] autorelease];
NSArray *targetArray = [[NSArray alloc] initWithObjects:target1, target2, nil];
id<MAKVOObservation> observation = nil;
@autoreleasepool
{
observation = [[observer observeTarget:targetArray keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { }] retain];
}
[observation autorelease];
[targetArray release];
STAssertFalse(observation.isValid, @"Array target wasn't auto-deregistered on deallocation");
}
}
- (void)testSelfObservation
{
@autoreleasepool
{
TestObject *object1 = [[[TestObject alloc] init] autorelease];
id<MAKVOObservation> observation = nil;
BOOL __block trigger = NO;
observation = [object1 observeTarget:object1 keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { trigger = !trigger; }];
object1.toggle = YES;
STAssertTrue(trigger, @"Trigger didn't fire or fired too many times.");
[observation remove];
trigger = NO;
object1.toggle = NO;
STAssertFalse(observation.isValid, @"Observation didn't go invalid.");
STAssertFalse(trigger, @"Trigger fired but shouldn't have.");
@autoreleasepool
{
TestObject *object2 = [[[TestObject alloc] init] autorelease];
observation = [[object2 addObserver:object2 keyPath:@"toggle" options:0 block:^ (MAKVONotification *notification) { }] retain];
}
[observation autorelease];
STAssertFalse(observation.isValid, @"Observation didn't automatically deregister.");
}
}
- (void)testMultipleDeallocSwizzleInHierarchy
{
NSMutableArray *observations = [NSMutableArray array];
@autoreleasepool {
TestObject *object = [[[TestObject alloc] init] autorelease];
DeallocTesterSuperclass *observer1 = [[[DeallocTesterSuperclass alloc] init] autorelease];
DeallocTesterSubclass *observer2 = [[[DeallocTesterSubclass alloc] init] autorelease];
[observations addObject: [object addObserver:observer1 keyPath:@"self" options:0
block:^(MAKVONotification *notification) {}]];
[observations addObject: [object addObserver:observer2 keyPath:@"self" options:0
block:^(MAKVONotification *notification) {}]];
}
}
@end