[ASPagerNode] Fix content insets are wrong in pager node if root node of ASViewController and transition back (#2736)

* Fix content insets are wrong in pager node if root node of ASViewController and transition back

* Add unit test

* Improve test

* Fix test

* Move tests to ASPagerNodeTests
This commit is contained in:
Michael Schneider 2016-12-12 16:05:09 -08:00 committed by appleguy
parent d874eed78a
commit 57cd6a3ba8
2 changed files with 73 additions and 6 deletions

View File

@ -1232,10 +1232,6 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (void)layoutSubviews
{
if (_zeroContentInsets) {
self.contentInset = UIEdgeInsetsZero;
}
// Flush any pending invalidation action if needed.
ASCollectionViewInvalidationStyle invalidationStyle = _nextLayoutInvalidationStyle;
_nextLayoutInvalidationStyle = ASCollectionViewInvalidationStyleNone;
@ -1254,6 +1250,10 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
// To ensure _maxSizeForNodesConstrainedSize is up-to-date for every usage, this call to super must be done last
[super layoutSubviews];
if (_zeroContentInsets) {
self.contentInset = UIEdgeInsetsZero;
}
// Update range controller immediately if possible & needed.
// Calling -updateIfNeeded in here with self.window == nil (early in the collection view's life)

View File

@ -7,8 +7,7 @@
//
#import <XCTest/XCTest.h>
#import "ASPagerNode.h"
#import "ASCellNode.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
@interface ASPagerNodeTestDataSource : NSObject <ASPagerDataSource>
@end
@ -94,4 +93,72 @@
return testController;
}
- (void)testThatRootPagerNodeDoesGetTheRightInsetWhilePoppingBack
{
UICollectionViewCell *cell = nil;
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;
ASPagerNodeTestDataSource *dataSource = [[ASPagerNodeTestDataSource alloc] init];
ASPagerNode *pagerNode = [[ASPagerNode alloc] init];
pagerNode.dataSource = dataSource;
node.layoutSpecBlock = ^(ASDisplayNode *node, ASSizeRange constrainedSize){
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsZero child:pagerNode];
};
ASViewController *vc = [[ASViewController alloc] initWithNode:node];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
window.rootViewController = nav;
[window makeKeyAndVisible];
[window layoutIfNeeded];
// Wait until view controller is visible
XCTestExpectation *e = [self expectationWithDescription:@"Transition completed"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[e fulfill];
});
[self waitForExpectationsWithTimeout:2 handler:nil];
// Test initial values
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
cell = [pagerNode.view cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
#pragma clang diagnostic pop
XCTAssertEqualObjects(NSStringFromCGRect(window.bounds), NSStringFromCGRect(node.frame));
XCTAssertEqualObjects(NSStringFromCGRect(window.bounds), NSStringFromCGRect(cell.frame));
XCTAssertEqual(pagerNode.view.contentOffset.y, 0);
XCTAssertEqual(pagerNode.view.contentInset.top, 0);
e = [self expectationWithDescription:@"Transition completed"];
// Push another view controller
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.frame = nav.view.bounds;
vc2.view.backgroundColor = [UIColor blueColor];
[nav pushViewController:vc2 animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.505 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[e fulfill];
});
[self waitForExpectationsWithTimeout:2 handler:nil];
// Pop view controller
e = [self expectationWithDescription:@"Transition completed"];
[vc2.navigationController popViewControllerAnimated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.505 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[e fulfill];
});
[self waitForExpectationsWithTimeout:2 handler:nil];
// Test values again after popping the view controller
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
cell = [pagerNode.view cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
#pragma clang diagnostic pop
XCTAssertEqualObjects(NSStringFromCGRect(window.bounds), NSStringFromCGRect(node.frame));
XCTAssertEqualObjects(NSStringFromCGRect(window.bounds), NSStringFromCGRect(cell.frame));
XCTAssertEqual(pagerNode.view.contentOffset.y, 0);
XCTAssertEqual(pagerNode.view.contentInset.top, 0);
}
@end