Inject an ASDataController class to ASTableView, for testing purposes

- Injection can be done via a new internal initializer. The class will be used by ASTableView to create (and configure) a new data controller.
- ASTableViewTests now injects its own type of ASDataController. This facilitates new ways for testing ASTableView-specific behaviours. The first application is counting the number of times relayoutAllNodes is called on the data controller.
This commit is contained in:
Huy Nguyen
2015-10-26 13:33:58 +02:00
parent 048566aed5
commit 72fca5395f
4 changed files with 93 additions and 15 deletions

View File

@@ -7,6 +7,7 @@
*/
#import "ASTableView.h"
#import "ASTableViewInternal.h"
#import "ASAssert.h"
#import "ASChangeSetDataController.h"
@@ -155,7 +156,6 @@ static BOOL _isInterceptedSelector(SEL sel)
_ASTableViewProxy *_proxyDataSource;
_ASTableViewProxy *_proxyDelegate;
ASDataController *_dataController;
ASFlowLayoutController *_layoutController;
ASRangeController *_rangeController;
@@ -174,6 +174,7 @@ static BOOL _isInterceptedSelector(SEL sel)
}
@property (atomic, assign) BOOL asyncDataSourceLocked;
@property (nonatomic, retain, readwrite) ASDataController *dataController;
@end
@@ -199,24 +200,29 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) {
}
}
+ (Class)dataControllerClass
{
return [ASChangeSetDataController class];
}
#pragma mark -
#pragma mark Lifecycle
- (void)configureWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled
- (void)configureWithDataControllerClass:(Class)dataControllerClass asyncDataFetching:(BOOL)asyncDataFetching
{
_layoutController = [[ASFlowLayoutController alloc] initWithScrollOption:ASFlowLayoutDirectionVertical];
_rangeController = [[ASRangeController alloc] init];
_rangeController.layoutController = _layoutController;
_rangeController.delegate = self;
_dataController = [[ASChangeSetDataController alloc] initWithAsyncDataFetching:asyncDataFetchingEnabled];
_dataController = [[dataControllerClass alloc] initWithAsyncDataFetching:asyncDataFetching];
_dataController.dataSource = self;
_dataController.delegate = _rangeController;
_layoutController.dataSource = _dataController;
_asyncDataFetchingEnabled = asyncDataFetchingEnabled;
_asyncDataFetchingEnabled = asyncDataFetching;
_asyncDataSourceLocked = NO;
_leadingScreensForBatching = 1.0;
@@ -236,6 +242,11 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) {
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style asyncDataFetching:(BOOL)asyncDataFetchingEnabled
{
return [self initWithFrame:frame style:style dataControllerClass:[self.class dataControllerClass] asyncDataFetching:asyncDataFetchingEnabled];
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass asyncDataFetching:(BOOL)asyncDataFetchingEnabled
{
if (!(self = [super initWithFrame:frame style:style]))
return nil;
@@ -244,8 +255,8 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) {
// https://github.com/facebook/AsyncDisplayKit/issues/385
asyncDataFetchingEnabled = NO;
[self configureWithAsyncDataFetching:asyncDataFetchingEnabled];
[self configureWithDataControllerClass:dataControllerClass asyncDataFetching:asyncDataFetchingEnabled];
return self;
}
@@ -254,7 +265,7 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) {
if (!(self = [super initWithCoder:aDecoder]))
return nil;
[self configureWithAsyncDataFetching:NO];
[self configureWithDataControllerClass:[self.class dataControllerClass] asyncDataFetching:NO];
return self;
}