From b3ae15858a9d263379ebfd2fd6a496c9066ec091 Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Thu, 12 Feb 2015 16:09:50 -0800 Subject: [PATCH] Unit tests for batch fetching --- AsyncDisplayKit.xcodeproj/project.pbxproj | 14 +++ AsyncDisplayKit/ASCollectionView.h | 4 +- AsyncDisplayKit/ASCollectionView.mm | 31 ++---- AsyncDisplayKit/ASTableView.h | 4 +- AsyncDisplayKit/ASTableView.mm | 20 +--- AsyncDisplayKit/Details/ASBatchContext.h | 12 ++- AsyncDisplayKit/Details/ASBatchFetching.h | 36 +++++++ AsyncDisplayKit/Details/ASBatchFetching.m | 51 ++++++++++ AsyncDisplayKit/Details/ASLayoutController.h | 10 +- AsyncDisplayKit/Details/ASScrollDirection.h | 17 ++++ AsyncDisplayKitTests/ASBatchFetchingTests.m | 102 +++++++++++++++++++ 11 files changed, 248 insertions(+), 53 deletions(-) create mode 100644 AsyncDisplayKit/Details/ASBatchFetching.h create mode 100644 AsyncDisplayKit/Details/ASBatchFetching.m create mode 100644 AsyncDisplayKit/Details/ASScrollDirection.h create mode 100644 AsyncDisplayKitTests/ASBatchFetchingTests.m diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index 9f0afe7a2e..0f18515d2e 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -145,6 +145,9 @@ 292C59A31A956527007E5DD6 /* ASRangeHandlerRender.h in Headers */ = {isa = PBXBuildFile; fileRef = 292C599D1A956527007E5DD6 /* ASRangeHandlerRender.h */; settings = {ATTRIBUTES = (Public, ); }; }; 292C59A41A956527007E5DD6 /* ASRangeHandlerRender.mm in Sources */ = {isa = PBXBuildFile; fileRef = 292C599E1A956527007E5DD6 /* ASRangeHandlerRender.mm */; }; 299DA1A91A828D2900162D41 /* ASBatchContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 299DA1A71A828D2900162D41 /* ASBatchContext.h */; }; + 296A0A2E1A9516B2005ACEAA /* ASBatchFetching.h in Headers */ = {isa = PBXBuildFile; fileRef = 296A0A2C1A9516B2005ACEAA /* ASBatchFetching.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 296A0A2F1A9516B2005ACEAA /* ASBatchFetching.m in Sources */ = {isa = PBXBuildFile; fileRef = 296A0A2D1A9516B2005ACEAA /* ASBatchFetching.m */; }; + 296A0A351A951ABF005ACEAA /* ASBatchFetchingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 296A0A341A951ABF005ACEAA /* ASBatchFetchingTests.m */; }; 299DA1A91A828D2900162D41 /* ASBatchContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 299DA1A71A828D2900162D41 /* ASBatchContext.h */; settings = {ATTRIBUTES = (Public, ); }; }; 299DA1AA1A828D2900162D41 /* ASBatchContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 299DA1A81A828D2900162D41 /* ASBatchContext.m */; }; 3C9C128519E616EF00E942A0 /* ASTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C9C128419E616EF00E942A0 /* ASTableViewTests.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; @@ -299,6 +302,10 @@ 292C599C1A956527007E5DD6 /* ASRangeHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASRangeHandler.h; sourceTree = ""; }; 292C599D1A956527007E5DD6 /* ASRangeHandlerRender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASRangeHandlerRender.h; sourceTree = ""; }; 292C599E1A956527007E5DD6 /* ASRangeHandlerRender.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASRangeHandlerRender.mm; sourceTree = ""; }; + 296A0A2C1A9516B2005ACEAA /* ASBatchFetching.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASBatchFetching.h; sourceTree = ""; }; + 296A0A2D1A9516B2005ACEAA /* ASBatchFetching.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASBatchFetching.m; sourceTree = ""; }; + 296A0A311A951715005ACEAA /* ASScrollDirection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASScrollDirection.h; path = AsyncDisplayKit/Details/ASScrollDirection.h; sourceTree = SOURCE_ROOT; }; + 296A0A341A951ABF005ACEAA /* ASBatchFetchingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASBatchFetchingTests.m; sourceTree = ""; }; 299DA1A71A828D2900162D41 /* ASBatchContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASBatchContext.h; sourceTree = ""; }; 299DA1A81A828D2900162D41 /* ASBatchContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASBatchContext.m; sourceTree = ""; }; 3C9C128419E616EF00E942A0 /* ASTableViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASTableViewTests.m; sourceTree = ""; }; @@ -432,6 +439,7 @@ 058D09C5195D04C000B7D73C /* AsyncDisplayKitTests */ = { isa = PBXGroup; children = ( + 296A0A341A951ABF005ACEAA /* ASBatchFetchingTests.m */, 2911485B1A77147A005D0878 /* ASControlNodeTests.m */, 058D0A2D195D057000B7D73C /* ASDisplayLayerTests.m */, 058D0A2E195D057000B7D73C /* ASDisplayNodeAppearanceTests.m */, @@ -472,6 +480,8 @@ 054963481A1EA066000F8E56 /* ASBasicImageDownloader.mm */, 299DA1A71A828D2900162D41 /* ASBatchContext.h */, 299DA1A81A828D2900162D41 /* ASBatchContext.m */, + 296A0A2C1A9516B2005ACEAA /* ASBatchFetching.h */, + 296A0A2D1A9516B2005ACEAA /* ASBatchFetching.m */, 464052191A3F83C40061C0BA /* ASDataController.h */, 4640521A1A3F83C40061C0BA /* ASDataController.mm */, 05A6D05819D0EB64002DD95E /* ASDealloc2MainObject.h */, @@ -497,6 +507,7 @@ 292C599C1A956527007E5DD6 /* ASRangeHandler.h */, 292C599D1A956527007E5DD6 /* ASRangeHandlerRender.h */, 292C599E1A956527007E5DD6 /* ASRangeHandlerRender.mm */, + 296A0A311A951715005ACEAA /* ASScrollDirection.h */, 058D09EA195D050800B7D73C /* ASTextNodeCoreTextAdditions.h */, 058D09EB195D050800B7D73C /* ASTextNodeCoreTextAdditions.m */, 058D09EC195D050800B7D73C /* ASTextNodeRenderer.h */, @@ -600,6 +611,7 @@ 058D0A4F195D05CB00B7D73C /* ASImageNode.h in Headers */, 058D0A50195D05CB00B7D73C /* ASImageNode.mm in Headers */, 058D0A51195D05CB00B7D73C /* ASTextNode.h in Headers */, + 296A0A2E1A9516B2005ACEAA /* ASBatchFetching.h in Headers */, 058D0A52195D05CB00B7D73C /* ASTextNode.mm in Headers */, 055F1A3819ABD413004DAFF1 /* ASRangeController.h in Headers */, 292C59A31A956527007E5DD6 /* ASRangeHandlerRender.h in Headers */, @@ -832,6 +844,7 @@ 058D0A29195D050800B7D73C /* ASDisplayNode+DebugTiming.mm in Sources */, 058D0A22195D050800B7D73C /* _ASAsyncTransaction.m in Sources */, 055F1A3919ABD413004DAFF1 /* ASRangeController.mm in Sources */, + 296A0A2F1A9516B2005ACEAA /* ASBatchFetching.m in Sources */, D785F6631A74327E00291744 /* ASScrollNode.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -841,6 +854,7 @@ buildActionMask = 2147483647; files = ( 2911485C1A77147A005D0878 /* ASControlNodeTests.m in Sources */, + 296A0A351A951ABF005ACEAA /* ASBatchFetchingTests.m in Sources */, 058D0A3E195D057000B7D73C /* ASTextNodeRendererTests.m in Sources */, 058D0A3D195D057000B7D73C /* ASTextNodeCoreTextAdditionsTests.m in Sources */, 058D0A3C195D057000B7D73C /* ASMutableAttributedStringBuilderTests.m in Sources */, diff --git a/AsyncDisplayKit/ASCollectionView.h b/AsyncDisplayKit/ASCollectionView.h index 8ef63cfbc7..da640c61b9 100644 --- a/AsyncDisplayKit/ASCollectionView.h +++ b/AsyncDisplayKit/ASCollectionView.h @@ -185,7 +185,7 @@ * If not implemented, the collectionView assumes that it should notify its asyncDelegate when batch fetching * should occur. */ -- (BOOL)shouldBatchFetchForCollectionView:(UICollectionView *)collectionView; +- (BOOL)shouldBatchFetchForCollectionView:(ASCollectionView *)collectionView; /** * Receive a message that the collectionView is near the end of its data set and more data should be fetched if @@ -200,7 +200,7 @@ * UICollectionView currently only supports batch events for tail loads. If you require a head load, consider * implementing a UIRefreshControl. */ -- (void)collectionView:(UICollectionView *)collectionView beginBatchFetchingWithContext:(ASBatchContext *)context; +- (void)collectionView:(ASCollectionView *)collectionView beginBatchFetchingWithContext:(ASBatchContext *)context; @end diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index f5b673c989..60ac8b1a7b 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -13,6 +13,7 @@ #import "ASRangeController.h" #import "ASDataController.h" #import "ASDisplayNodeInternal.h" +#import "ASBatchFetching.h" const static NSUInteger kASCollectionViewAnimationNone = 0; @@ -144,6 +145,8 @@ static BOOL _isInterceptedSelector(SEL sel) _batchContext = [[ASBatchContext alloc] init]; + _leadingScreensForBatching = 1.0; + _proxyDelegate = [[_ASCollectionViewProxy alloc] initWithTarget:nil interceptor:self]; super.delegate = (id)_proxyDelegate; @@ -386,7 +389,8 @@ static BOOL _isInterceptedSelector(SEL sel) if ([self.asyncDelegate respondsToSelector:@selector(shouldBatchFetchForCollectionView:)]) { return [self.asyncDelegate shouldBatchFetchForCollectionView:self]; } else { - return YES; + // if the delegate does not respond to this method, there is no point in starting to fetch + return [self.asyncDelegate respondsToSelector:@selector(collectionView:beginBatchFetchingWithContext:)]; } } @@ -394,32 +398,11 @@ static BOOL _isInterceptedSelector(SEL sel) { ASDisplayNodeAssert(_batchContext != nil, @"Batch context should exist"); - // Bail if we are already fetching, the delegate doesn't care, or we're told not to fetch - if ([_batchContext isFetching] || - ![self.asyncDelegate respondsToSelector:@selector(collectionView:beginBatchFetchingWithContext:)] || - ![self shouldFetchBatch]) { + if (![self shouldFetchBatch]) { return; } - ASScrollDirection scrollDirection = [self scrollDirection]; - CGFloat viewSize, offset, contentSize; - - if (scrollDirection == ASScrollDirectionUp) { - viewSize = CGRectGetHeight(self.bounds); - offset = targetOffset.y; - contentSize = self.contentSize.height; - } else { // horizontal - viewSize = CGRectGetWidth(self.bounds); - offset = targetOffset.x; - contentSize = self.contentSize.width; - } - - CGFloat triggerDistance = viewSize * _leadingScreensForBatching; - - // Determine if the offset that we are headed to is within the number of screens we have defined - // ASCollectionView supports tail loading only currently, hence the check against Up and Left - BOOL supportedBatchScrollDirection = scrollDirection == ASScrollDirectionUp || ASScrollDirectionLeft; - if (supportedBatchScrollDirection && contentSize - (viewSize + offset) <= triggerDistance) { + if (ASDisplayShouldFetchBatchForContext(_batchContext, [self scrollDirection], self.bounds, self.contentSize, targetOffset, _leadingScreensForBatching)) { [_batchContext beginBatchFetching]; [self.asyncDelegate collectionView:self beginBatchFetchingWithContext:_batchContext]; } diff --git a/AsyncDisplayKit/ASTableView.h b/AsyncDisplayKit/ASTableView.h index eb4b7968e2..08ac6be7a9 100644 --- a/AsyncDisplayKit/ASTableView.h +++ b/AsyncDisplayKit/ASTableView.h @@ -190,7 +190,7 @@ * If not implemented, the tableView assumes that it should notify its asyncDelegate when batch fetching * should occur. */ -- (BOOL)shouldBatchFetchForTableView:(UITableView *)tableView; +- (BOOL)shouldBatchFetchForTableView:(ASTableView *)tableView; /** * Receive a message that the tableView is near the end of its data set and more data should be fetched if necessary. @@ -204,7 +204,7 @@ * ASTableView currently only supports batch events for tail loads. If you require a head load, consider implementing a * UIRefreshControl. */ -- (void)tableView:(UITableView *)tableView beginBatchFetchingWithContext:(ASBatchContext *)context; +- (void)tableView:(ASTableView *)tableView beginBatchFetchingWithContext:(ASBatchContext *)context; @end diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 81ab448535..a1fbfa7550 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -14,7 +14,7 @@ #import "ASLayoutController.h" #import "ASRangeController.h" #import "ASDisplayNodeInternal.h" - +#import "ASBatchFetching.h" #pragma mark - @@ -395,7 +395,8 @@ static BOOL _isInterceptedSelector(SEL sel) if ([self.asyncDelegate respondsToSelector:@selector(shouldBatchFetchForTableView:)]) { return [self.asyncDelegate shouldBatchFetchForTableView:self]; } else { - return YES; + // if the delegate does not respond to this method, there is no point in starting to fetch + return [self.asyncDelegate respondsToSelector:@selector(tableView:beginBatchFetchingWithContext:)]; } } @@ -403,22 +404,11 @@ static BOOL _isInterceptedSelector(SEL sel) { ASDisplayNodeAssert(_batchContext != nil, @"Batch context should exist"); - // Bail if we are already fetching, the delegate doesn't care, or we're told not to fetch - if ([_batchContext isFetching] || - ![self.asyncDelegate respondsToSelector:@selector(tableView:beginBatchFetchingWithContext:)] || - ![self shouldFetchBatch]) { + if (![self shouldFetchBatch]) { return; } - CGFloat viewHeight = CGRectGetHeight(self.bounds); - CGFloat triggerDistance = viewHeight * _leadingScreensForBatching; - CGFloat offset = targetOffset.y; - CGFloat contentHeight = self.contentSize.height; - - // Determine if the offset that we are headed to is within the number of screens we have defined - // ASTableView supports tail loading only currently, hence the check against ASScrollDirectionUp - if ([self scrollDirection] == ASScrollDirectionUp && - contentHeight - (viewHeight + offset) <= triggerDistance) { + if (ASDisplayShouldFetchBatchForContext(_batchContext, [self scrollDirection], self.bounds, self.contentSize, targetOffset, _leadingScreensForBatching)) { [_batchContext beginBatchFetching]; [self.asyncDelegate tableView:self beginBatchFetchingWithContext:_batchContext]; } diff --git a/AsyncDisplayKit/Details/ASBatchContext.h b/AsyncDisplayKit/Details/ASBatchContext.h index baceb5074a..2d8f2bb2e1 100644 --- a/AsyncDisplayKit/Details/ASBatchContext.h +++ b/AsyncDisplayKit/Details/ASBatchContext.h @@ -6,6 +6,8 @@ * of patent rights can be found in the PATENTS file in the same directory. */ +#import + /** * @abstract A context object to notify when batch fetches are finished or cancelled. */ @@ -29,8 +31,6 @@ */ - (void)completeBatchFetching:(BOOL)didComplete; -- (void)beginBatchFetching; - /** * Ask the context object if the batch fetching process was cancelled by the context owner. * @@ -49,4 +49,12 @@ */ - (void)cancelBatchFetching; +/** + * Notify the context object that fetching has started. + * + * @discussion Call this method only when you are beginning a fetch process. This should really only be called by the + * context object's owner. Calling this method should be complimented with -completeBatchFetching:. + */ +- (void)beginBatchFetching; + @end diff --git a/AsyncDisplayKit/Details/ASBatchFetching.h b/AsyncDisplayKit/Details/ASBatchFetching.h new file mode 100644 index 0000000000..9aeea5ad26 --- /dev/null +++ b/AsyncDisplayKit/Details/ASBatchFetching.h @@ -0,0 +1,36 @@ +/* Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import + +#import "ASBatchContext.h" +#import "ASScrollDirection.h" +#import "ASBaseDefines.h" + +ASDISPLAYNODE_EXTERN_C_BEGIN + +/** + @abstract Determine if batch fetching should begin based on the state of the parameters. + @param context The batch fetching context that contains knowledge about in-flight fetches. + @param scrollDirection The current scrolling direction of the scroll view. + @param bounds The bounds of the scrollview. + @param contentSize The content size of the scrollview. + @param targetOffset The offset that the scrollview will scroll to. + @param leadingScreens How many screens in the remaining distance will trigger batch fetching. + @return Whether or not the current state should proceed with batch fetching. + @discussion This method is broken into a category for unit testing purposes and should be used with the ASTableView and + * ASCollectionView batch fetching API. + */ +extern BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context, + ASScrollDirection scrollDirection, + CGRect bounds, + CGSize contentSize, + CGPoint targetOffset, + CGFloat leadingScreens); + +ASDISPLAYNODE_EXTERN_C_END diff --git a/AsyncDisplayKit/Details/ASBatchFetching.m b/AsyncDisplayKit/Details/ASBatchFetching.m new file mode 100644 index 0000000000..c0233cc626 --- /dev/null +++ b/AsyncDisplayKit/Details/ASBatchFetching.m @@ -0,0 +1,51 @@ +/* Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import "ASBatchFetching.h" + +BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context, + ASScrollDirection scrollDirection, + CGRect bounds, + CGSize contentSize, + CGPoint targetOffset, + CGFloat leadingScreens) { + // do not allow fetching if a batch is already in-flight and hasn't been completed or cancelled + if ([context isFetching]) { + return NO; + } + + // no fetching for null states + if (leadingScreens <= 0.0 || + CGPointEqualToPoint(targetOffset, CGPointZero) || + CGSizeEqualToSize(contentSize, CGSizeZero) || + CGRectEqualToRect(bounds, CGRectZero)) { + return NO; + } + + // only Up and Left scrolls are currently supported (tail loading) + if (scrollDirection != ASScrollDirectionUp && scrollDirection != ASScrollDirectionLeft) { + return NO; + } + + CGFloat viewLength, offset, contentLength; + + if (scrollDirection == ASScrollDirectionUp) { + viewLength = bounds.size.height; + offset = targetOffset.y; + contentLength = contentSize.height; + } else { // horizontal + viewLength = bounds.size.width; + offset = targetOffset.x; + contentLength = contentSize.width; + } + + CGFloat triggerDistance = viewLength * leadingScreens; + CGFloat remainingDistance = contentLength - viewLength - offset; + + return remainingDistance <= triggerDistance; +} diff --git a/AsyncDisplayKit/Details/ASLayoutController.h b/AsyncDisplayKit/Details/ASLayoutController.h index 60482922fc..51a3c62a9e 100644 --- a/AsyncDisplayKit/Details/ASLayoutController.h +++ b/AsyncDisplayKit/Details/ASLayoutController.h @@ -10,20 +10,14 @@ #import #import +#import "ASScrollDirection.h" + typedef struct { CGFloat leadingBufferScreenfuls; CGFloat trailingBufferScreenfuls; } ASRangeTuningParameters; -typedef NS_ENUM(NSInteger, ASScrollDirection) { - ASScrollDirectionNone, - ASScrollDirectionRight, - ASScrollDirectionLeft, - ASScrollDirectionUp, - ASScrollDirectionDown, -}; - @protocol ASLayoutController /** diff --git a/AsyncDisplayKit/Details/ASScrollDirection.h b/AsyncDisplayKit/Details/ASScrollDirection.h new file mode 100644 index 0000000000..5459feffe8 --- /dev/null +++ b/AsyncDisplayKit/Details/ASScrollDirection.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import + +typedef NS_ENUM(NSInteger, ASScrollDirection) { + ASScrollDirectionNone, + ASScrollDirectionRight, + ASScrollDirectionLeft, + ASScrollDirectionUp, + ASScrollDirectionDown, +}; diff --git a/AsyncDisplayKitTests/ASBatchFetchingTests.m b/AsyncDisplayKitTests/ASBatchFetchingTests.m new file mode 100644 index 0000000000..48f367e223 --- /dev/null +++ b/AsyncDisplayKitTests/ASBatchFetchingTests.m @@ -0,0 +1,102 @@ +/* Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#import + +#import "ASBatchFetching.h" + +@interface ASBatchFetchingTests : XCTestCase + +@end + +@implementation ASBatchFetchingTests + +#define PASSING_RECT (CGRect){0,0,1,1} +#define PASSING_SIZE (CGSize){1,1} +#define PASSING_POINT (CGPoint){1,1} +#define VERTICAL_RECT(h) (CGRect){0,0,0,h} +#define VERTICAL_SIZE(h) (CGSize){0,h} +#define VERTICAL_OFFSET(y) (CGPoint){0,y} +#define HORIZONTAL_RECT(w) (CGRect){0,0,w,0} +#define HORIZONTAL_SIZE(w) (CGSize){w,0} +#define HORIZONTAL_OFFSET(x) (CGPoint){x,0} + +- (void)testBatchNullState { + ASBatchContext *context = [[ASBatchContext alloc] init]; + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, CGRectZero, CGSizeZero, CGPointZero, 0.0); + XCTAssert(shouldFetch == NO, @"Should not fetch in the null state"); +} + +- (void)testBatchAlreadyFetching { + ASBatchContext *context = [[ASBatchContext alloc] init]; + [context beginBatchFetching]; + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, PASSING_RECT, PASSING_SIZE, PASSING_POINT, 1.0); + XCTAssert(shouldFetch == NO, @"Should not fetch when context is already fetching"); +} + +- (void)testUnsupportedScrollDirections { + ASBatchContext *context = [[ASBatchContext alloc] init]; + BOOL fetchRight = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionRight, PASSING_RECT, PASSING_SIZE, PASSING_POINT, 1.0); + XCTAssert(fetchRight == NO, @"Should not fetch for scrolling right"); + BOOL fetchDown = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionDown, PASSING_RECT, PASSING_SIZE, PASSING_POINT, 1.0); + XCTAssert(fetchDown == NO, @"Should not fetch for scrolling down"); + BOOL fetchUp = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, PASSING_RECT, PASSING_SIZE, PASSING_POINT, 1.0); + XCTAssert(fetchUp == YES, @"Should fetch for scrolling up"); + BOOL fetchLeft = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionLeft, PASSING_RECT, PASSING_SIZE, PASSING_POINT, 1.0); + XCTAssert(fetchLeft == YES, @"Should fetch for scrolling left"); +} + +- (void)testVerticalScrollToExactLeading { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // scroll to 1-screen top offset, height is 1 screen, so bottom is 1 screen away from end of content + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, VERTICAL_RECT(screen), VERTICAL_SIZE(screen * 3.0), VERTICAL_OFFSET(screen * 1.0), 1.0); + XCTAssert(shouldFetch == YES, @"Fetch should begin when vertically scrolling to exactly 1 leading screen away"); +} + +- (void)testVerticalScrollToLessThanLeading { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // 3 screens of content, scroll only 1/2 of one screen + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, VERTICAL_RECT(screen), VERTICAL_SIZE(screen * 3.0), VERTICAL_OFFSET(screen * 0.5), 1.0); + XCTAssert(shouldFetch == NO, @"Fetch should not begin when vertically scrolling less than the leading distance away"); +} + +- (void)testVerticalScrollingPastContentSize { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // 3 screens of content, top offset to 3-screens, height 1 screen, so its 1 screen past the leading + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, VERTICAL_RECT(screen), VERTICAL_SIZE(screen * 3.0), VERTICAL_OFFSET(screen * 3.0), 1.0); + XCTAssert(shouldFetch == YES, @"Fetch should begin when vertically scrolling past the content size"); +} + +- (void)testHorizontalScrollToExactLeading { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // scroll to 1-screen left offset, width is 1 screen, so right is 1 screen away from end of content + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionLeft, HORIZONTAL_RECT(screen), HORIZONTAL_SIZE(screen * 3.0), HORIZONTAL_OFFSET(screen * 1.0), 1.0); + XCTAssert(shouldFetch == YES, @"Fetch should begin when horizontally scrolling to exactly 1 leading screen away"); +} + +- (void)testHorizontalScrollToLessThanLeading { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // 3 screens of content, scroll only 1/2 of one screen + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionRight, HORIZONTAL_RECT(screen), HORIZONTAL_SIZE(screen * 3.0), HORIZONTAL_OFFSET(screen * 0.5), 1.0); + XCTAssert(shouldFetch == NO, @"Fetch should not begin when horizontally scrolling less than the leading distance away"); +} + +- (void)testHorizontalScrollingPastContentSize { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // 3 screens of content, left offset to 3-screens, width 1 screen, so its 1 screen past the leading + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, HORIZONTAL_RECT(screen), HORIZONTAL_SIZE(screen * 3.0), HORIZONTAL_OFFSET(screen * 3.0), 1.0); + XCTAssert(shouldFetch == YES, @"Fetch should begin when vertically scrolling past the content size"); +} + +@end