Add assert for ASCollectionDataSource implementing collectionView:constrainedSizeForNodeAtIndexPath: instead of ASCollectionDelegate (#2165)

This commit is contained in:
Michael Schneider
2016-08-29 16:44:11 -07:00
committed by Adlai Holler
parent 8d86bb6db3
commit 1849c41b03
2 changed files with 74 additions and 0 deletions

View File

@@ -63,6 +63,9 @@ static inline ASSizeRange NodeConstrainedSizeForScrollDirection(ASCollectionView
{ {
if (_delegateFlags.implementsConstrainedSizeForNodeAtIndexPath) { if (_delegateFlags.implementsConstrainedSizeForNodeAtIndexPath) {
return [collectionView.asyncDelegate collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]; return [collectionView.asyncDelegate collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
} else {
// With 2.0 `collectionView:constrainedSizeForNodeAtIndexPath:` was moved to the delegate. Assert if not implemented on the delegate but on the data source
ASDisplayNodeAssert([collectionView.asyncDataSource respondsToSelector:@selector(collectionView:constrainedSizeForNodeAtIndexPath:)] == NO, @"collectionView:constrainedSizeForNodeAtIndexPath: was moved from the ASCollectionDataSource to the ASCollectionDataSource.");
} }
return NodeConstrainedSizeForScrollDirection(collectionView); return NodeConstrainedSizeForScrollDirection(collectionView);
@@ -143,6 +146,9 @@ static inline ASSizeRange NodeConstrainedSizeForScrollDirection(ASCollectionView
{ {
if (_delegateFlags.implementsConstrainedSizeForNodeAtIndexPath) { if (_delegateFlags.implementsConstrainedSizeForNodeAtIndexPath) {
return [collectionView.asyncDelegate collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]; return [collectionView.asyncDelegate collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
} else {
// With 2.0 `collectionView:constrainedSizeForNodeAtIndexPath:` was moved to the delegate. Assert if not implemented on the delegate but on the data source
ASDisplayNodeAssert([collectionView.asyncDataSource respondsToSelector:@selector(collectionView:constrainedSizeForNodeAtIndexPath:)] == NO, @"collectionView:constrainedSizeForNodeAtIndexPath: was moved from the ASCollectionDataSource to the ASCollectionDataSource.");
} }
CGSize itemSize = _layout.itemSize; CGSize itemSize = _layout.itemSize;

View File

@@ -10,6 +10,7 @@
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <OCMock/OCMock.h>
#import "ASCollectionView.h" #import "ASCollectionView.h"
#import "ASCollectionViewFlowLayoutInspector.h" #import "ASCollectionViewFlowLayoutInspector.h"
@@ -45,6 +46,27 @@
@end @end
@protocol InspectorTestDataSourceDelegateProtocol <ASCollectionDataSource, ASCollectionDelegate>
@end
@interface InspectorTestDataSourceDelegateWithoutNodeConstrainedSize : NSObject <InspectorTestDataSourceDelegateProtocol>
@end
@implementation InspectorTestDataSourceDelegateWithoutNodeConstrainedSize
- (ASCellNodeBlock)collectionView:(ASCollectionView *)collectionView nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath
{
return ^{ return [[ASCellNode alloc] init]; };
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 0;
}
@end
@interface ASCollectionViewFlowLayoutInspectorTests : XCTestCase @interface ASCollectionViewFlowLayoutInspectorTests : XCTestCase
@end @end
@@ -345,4 +367,50 @@
collectionView.asyncDelegate = nil; collectionView.asyncDelegate = nil;
} }
- (void)testThatItThrowsIfNodeConstrainedSizeIsImplementedOnDataSourceButNotOnDelegateLayoutInspector
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
id dataSourceAndDelegate = [OCMockObject mockForProtocol:@protocol(InspectorTestDataSourceDelegateProtocol)];
ASSizeRange constrainedSize = ASSizeRangeMake(CGSizeZero, CGSizeZero);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
NSValue *value = [NSValue value:&constrainedSize withObjCType:@encode(ASSizeRange)];
[[[dataSourceAndDelegate stub] andReturnValue:value] collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
collectionView.asyncDataSource = dataSourceAndDelegate;
id delegate = [InspectorTestDataSourceDelegateWithoutNodeConstrainedSize new];
collectionView.asyncDelegate = delegate;
ASCollectionViewLayoutInspector *inspector = [[ASCollectionViewLayoutInspector alloc] initWithCollectionView:collectionView];
collectionView.layoutInspector = inspector;
XCTAssertThrows([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
collectionView.asyncDelegate = dataSourceAndDelegate;
XCTAssertNoThrow([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
}
- (void)testThatItThrowsIfNodeConstrainedSizeIsImplementedOnDataSourceButNotOnDelegateFlowLayoutInspector
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
ASCollectionView *collectionView = [[ASCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
id dataSourceAndDelegate = [OCMockObject mockForProtocol:@protocol(InspectorTestDataSourceDelegateProtocol)];
ASSizeRange constrainedSize = ASSizeRangeMake(CGSizeZero, CGSizeZero);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
NSValue *value = [NSValue value:&constrainedSize withObjCType:@encode(ASSizeRange)];
[[[dataSourceAndDelegate stub] andReturnValue:value] collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath];
collectionView.asyncDataSource = dataSourceAndDelegate;
id delegate = [InspectorTestDataSourceDelegateWithoutNodeConstrainedSize new];
collectionView.asyncDelegate = delegate;
ASCollectionViewFlowLayoutInspector *inspector = collectionView.layoutInspector;
XCTAssertThrows([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
collectionView.asyncDelegate = dataSourceAndDelegate;
XCTAssertNoThrow([inspector collectionView:collectionView constrainedSizeForNodeAtIndexPath:indexPath]);
}
@end @end