@@ -34,6 +34,22 @@ + (NSInteger)maximumPoolSize:(id)context
34
34
35
35
@end
36
36
37
+ @interface CKStatefulViewComponentInvalidatableContext : NSObject
38
+ @property (assign , nonatomic ) BOOL isValid;
39
+ @end
40
+ @implementation CKStatefulViewComponentInvalidatableContext
41
+ @end
42
+
43
+ @interface CKStatefulViewComponentWithInvalidContextController : CKStatefulViewComponentController
44
+ @end
45
+ @implementation CKStatefulViewComponentWithInvalidContextController
46
+
47
+ + (BOOL )isContextValid : (CKStatefulViewComponentInvalidatableContext *)context {
48
+ return context.isValid ;
49
+ }
50
+
51
+ @end
52
+
37
53
@interface EnqueueOnDealloc : NSObject
38
54
+ (instancetype )newWithPool : (CKStatefulViewReusePool *)pool ;
39
55
@end
@@ -272,6 +288,95 @@ - (void)testMaximumPoolSizeOfOneByEnqueueingTwoViewsThenDequeueingTwoViewsReturn
272
288
XCTAssertTrue (dequeuedView2 != view2, @" Didn't expect view in container2 to be returned" );
273
289
}
274
290
291
+ - (void )testInvalidContextEnqueue
292
+ {
293
+ CKStatefulViewReusePool *pool = [[CKStatefulViewReusePool alloc ] init ];
294
+ CKStatefulViewComponentInvalidatableContext *context = [CKStatefulViewComponentInvalidatableContext new ];
295
+
296
+ __block int calledBlockCount = 0 ;
297
+
298
+ UIView *container1 = [[UIView alloc ] init ];
299
+ CKTestStatefulView *view1 = [[CKTestStatefulView alloc ] init ];
300
+ [container1 addSubview: view1];
301
+ [pool enqueueStatefulView: view1
302
+ forControllerClass: [CKStatefulViewComponentWithInvalidContextController class ]
303
+ context: context
304
+ mayRelinquishBlock: ^BOOL {
305
+ return YES ;
306
+ }];
307
+
308
+ UIView *container2 = [[UIView alloc ] init ];
309
+ CKTestStatefulView *view2 = [[CKTestStatefulView alloc ] init ];
310
+ [container2 addSubview: view2];
311
+ [pool enqueueStatefulView: view2
312
+ forControllerClass: [CKOtherStatefulViewComponentController class ]
313
+ context: nil
314
+ mayRelinquishBlock: ^BOOL {
315
+ calledBlockCount++;
316
+ return YES ;
317
+ }];
318
+
319
+ CKRunRunLoopUntilBlockIsTrue (^BOOL {
320
+ return calledBlockCount == 1 ;
321
+ });
322
+
323
+ UIView *dequeuedView1 = [pool dequeueStatefulViewForControllerClass: [CKStatefulViewComponentWithInvalidContextController class ]
324
+ preferredSuperview: container1
325
+ context: nil ];
326
+ XCTAssertTrue (dequeuedView1 == nil , @" Expected nil to be returned, since context is invalid" );
327
+
328
+ UIView *dequeuedView2 = [pool dequeueStatefulViewForControllerClass: [CKOtherStatefulViewComponentController class ]
329
+ preferredSuperview: container2
330
+ context: nil ];
331
+ XCTAssertTrue (dequeuedView2 == view2, @" View with valid context should be returned" );
332
+ }
333
+
334
+
335
+ - (void )testInvalidContextAfterEnqueue
336
+ {
337
+ CKStatefulViewReusePool *pool = [[CKStatefulViewReusePool alloc ] init ];
338
+ CKStatefulViewComponentInvalidatableContext *context = [CKStatefulViewComponentInvalidatableContext new ];
339
+ context.isValid = YES ;
340
+
341
+ __block int calledBlockCount = 0 ;
342
+
343
+ UIView *container1 = [[UIView alloc ] init ];
344
+ CKTestStatefulView *view1 = [[CKTestStatefulView alloc ] init ];
345
+ [container1 addSubview: view1];
346
+ [pool enqueueStatefulView: view1
347
+ forControllerClass: [CKStatefulViewComponentWithInvalidContextController class ]
348
+ context: context
349
+ mayRelinquishBlock: ^BOOL {
350
+ return YES ;
351
+ }];
352
+ context.isValid = NO ;
353
+
354
+ UIView *container2 = [[UIView alloc ] init ];
355
+ CKTestStatefulView *view2 = [[CKTestStatefulView alloc ] init ];
356
+ [container2 addSubview: view2];
357
+ [pool enqueueStatefulView: view2
358
+ forControllerClass: [CKOtherStatefulViewComponentController class ]
359
+ context: nil
360
+ mayRelinquishBlock: ^BOOL {
361
+ calledBlockCount++;
362
+ return YES ;
363
+ }];
364
+
365
+ CKRunRunLoopUntilBlockIsTrue (^BOOL {
366
+ return calledBlockCount == 1 ;
367
+ });
368
+
369
+ UIView *dequeuedView1 = [pool dequeueStatefulViewForControllerClass: [CKStatefulViewComponentWithInvalidContextController class ]
370
+ preferredSuperview: container1
371
+ context: nil ];
372
+ XCTAssertTrue (dequeuedView1 == nil , @" Expected nil to be returned, since context is invalid" );
373
+
374
+ UIView *dequeuedView2 = [pool dequeueStatefulViewForControllerClass: [CKOtherStatefulViewComponentController class ]
375
+ preferredSuperview: container2
376
+ context: nil ];
377
+ XCTAssertTrue (dequeuedView2 == view2, @" View with valid context should be returned" );
378
+ }
379
+
275
380
#pragma mark - Pending pool tests
276
381
277
382
- (void )testEnqueueingViewThenDequeueingWithPendingEnabledReturnsSameViewImmediately
0 commit comments