forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASPagerNodeTests.m
187 lines (150 loc) · 6.43 KB
/
ASPagerNodeTests.m
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// ASPagerNodeTests.m
// Texture
//
// 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 /ASDK-Licenses directory of this source tree. An additional
// grant of patent rights can be found in the PATENTS file in the same directory.
//
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#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) ASPagerNodeTestDataSource *testDataSource;
@property (nonatomic) 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) ASPagerNode *pagerNode;
@property (nonatomic) 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);
}
- (void)testScrollPageToIndex
{
ASPagerNodeTestController *testController = [self testController];
testController.pagerNode.frame = CGRectMake(0, 0, 500, 500);
[testController.pagerNode scrollToPageAtIndex:1 animated:false];
XCTAssertEqual(testController.pagerNode.currentPageIndex, 1);
}
- (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.contentOffset.y, 0);
XCTAssertEqual(pagerNode.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.contentOffset.y, 0);
XCTAssertEqual(pagerNode.contentInset.top, 0);
}
@end