forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASPagerNodeTests.m
More file actions
165 lines (130 loc) · 5.65 KB
/
Copy pathASPagerNodeTests.m
File metadata and controls
165 lines (130 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// ASPagerNodeTests.m
// AsyncDisplayKit
//
// Created by Luke Parham on 11/6/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/AsyncDisplayKit.h>
@interface ASPagerNodeTestDataSource : NSObject <ASPagerDataSource>
@end
@implementation ASPagerNodeTestDataSource
- (instancetype)init
{
if (!(self = [super init])) {
return nil;
}
return self;
}
- (NSInteger)numberOfPagesInPagerNode:(ASPagerNode *)pagerNode
{
return 2;
}
- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index
{
return [[ASCellNode alloc] init];
}
@end
@interface ASPagerNodeTestController: UIViewController
@property (nonatomic, strong) ASPagerNodeTestDataSource *testDataSource;
@property (nonatomic, strong) ASPagerNode *pagerNode;
@end
@implementation ASPagerNodeTestController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Populate these immediately so that they're not unexpectedly nil during tests.
self.testDataSource = [[ASPagerNodeTestDataSource alloc] init];
self.pagerNode = [[ASPagerNode alloc] init];
self.pagerNode.dataSource = self.testDataSource;
[self.view addSubnode:self.pagerNode];
}
return self;
}
@end
@interface ASPagerNodeTests : XCTestCase
@property (nonatomic, strong) ASPagerNode *pagerNode;
@property (nonatomic, strong) ASPagerNodeTestDataSource *testDataSource;
@end
@implementation ASPagerNodeTests
- (void)testPagerReturnsIndexOfPages {
ASPagerNodeTestController *testController = [self testController];
ASCellNode *cellNode = [testController.pagerNode nodeForPageAtIndex:0];
XCTAssertEqual([testController.pagerNode indexOfPageWithNode:cellNode], 0);
}
- (void)testPagerReturnsNotFoundForCellThatDontExistInPager {
ASPagerNodeTestController *testController = [self testController];
ASCellNode *badNode = [[ASCellNode alloc] init];
XCTAssertEqual([testController.pagerNode indexOfPageWithNode:badNode], NSNotFound);
}
- (ASPagerNodeTestController *)testController {
ASPagerNodeTestController *testController = [[ASPagerNodeTestController alloc] initWithNibName:nil bundle:nil];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window makeKeyAndVisible];
window.rootViewController = testController;
[testController.pagerNode reloadData];
[testController.pagerNode setNeedsLayout];
return testController;
}
// Disabled due to flakiness https://github.com/facebook/AsyncDisplayKit/issues/2818
- (void)DISABLED_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