|
| 1 | +// |
| 2 | +// ASCellVisibilityScrollEventTests.m |
| 3 | +// Texture |
| 4 | +// |
| 5 | +// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. |
| 6 | +// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved. |
| 7 | +// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | + |
| 10 | +#import <Foundation/Foundation.h> |
| 11 | +#import <XCTest/XCTest.h> |
| 12 | + |
| 13 | +#import <AsyncDisplayKit/AsyncDisplayKit.h> |
| 14 | + |
| 15 | +@interface ASCellVisibilityTestNode: ASTextCellNode |
| 16 | +@property (nonatomic) NSUInteger cellNodeVisibilityEventVisibleCount; |
| 17 | +@property (nonatomic) NSUInteger cellNodeVisibilityEventVisibleRectChangedCount; |
| 18 | +@property (nonatomic) NSUInteger cellNodeVisibilityEventInvisibleCount; |
| 19 | +@property (nonatomic) NSUInteger cellNodeVisibilityEventWillBeginDraggingCount; |
| 20 | +@property (nonatomic) NSUInteger cellNodeVisibilityEventDidEndDraggingCount; |
| 21 | +@property (nonatomic) NSUInteger cellNodeVisibilityEventDidStopScrollingCount; |
| 22 | +@end |
| 23 | + |
| 24 | +@implementation ASCellVisibilityTestNode |
| 25 | + |
| 26 | +- (void)cellNodeVisibilityEvent:(ASCellNodeVisibilityEvent)event inScrollView:(UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame |
| 27 | +{ |
| 28 | + switch (event) { |
| 29 | + case ASCellNodeVisibilityEventVisible: |
| 30 | + self.cellNodeVisibilityEventVisibleCount++; |
| 31 | + break; |
| 32 | + case ASCellNodeVisibilityEventVisibleRectChanged: |
| 33 | + self.cellNodeVisibilityEventVisibleRectChangedCount++; |
| 34 | + break; |
| 35 | + case ASCellNodeVisibilityEventInvisible: |
| 36 | + self.cellNodeVisibilityEventInvisibleCount++; |
| 37 | + break; |
| 38 | + case ASCellNodeVisibilityEventWillBeginDragging: |
| 39 | + self.cellNodeVisibilityEventWillBeginDraggingCount++; |
| 40 | + break; |
| 41 | + case ASCellNodeVisibilityEventDidEndDragging: |
| 42 | + self.cellNodeVisibilityEventDidEndDraggingCount++; |
| 43 | + break; |
| 44 | + case ASCellNodeVisibilityEventDidStopScrolling: |
| 45 | + self.cellNodeVisibilityEventDidStopScrollingCount++; |
| 46 | + break; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +@end |
| 51 | + |
| 52 | +@interface ASTableView (Private_Testing) |
| 53 | +- (void)scrollViewDidScroll:(UIScrollView *)scrollView; |
| 54 | +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; |
| 55 | +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; |
| 56 | +- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; |
| 57 | +- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; |
| 58 | +@end |
| 59 | + |
| 60 | +@interface ASCellVisibilityTableViewTestController: UIViewController<ASTableDataSource> |
| 61 | + |
| 62 | +@property (nonatomic) ASTableNode *tableNode; |
| 63 | +@property (nonatomic) ASTableView *tableView; |
| 64 | + |
| 65 | +@end |
| 66 | + |
| 67 | +@implementation ASCellVisibilityTableViewTestController |
| 68 | + |
| 69 | +- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { |
| 70 | + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 71 | + if (self) { |
| 72 | + self.tableNode = [[ASTableNode alloc] init]; |
| 73 | + self.tableView = self.tableNode.view; |
| 74 | + self.tableNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 75 | + self.tableNode.dataSource = self; |
| 76 | + |
| 77 | + [self.view addSubview:self.tableView]; |
| 78 | + } |
| 79 | + return self; |
| 80 | +} |
| 81 | + |
| 82 | +- (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section |
| 83 | +{ |
| 84 | + return 1; |
| 85 | +} |
| 86 | + |
| 87 | +- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath; |
| 88 | +{ |
| 89 | + return ^{ |
| 90 | + ASCellVisibilityTestNode *cell = [[ASCellVisibilityTestNode alloc] init]; |
| 91 | + return cell; |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +@end |
| 96 | + |
| 97 | +@interface ASCollectionView (Private_Testing) |
| 98 | +- (void)scrollViewDidScroll:(UIScrollView *)scrollView; |
| 99 | +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; |
| 100 | +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; |
| 101 | +- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; |
| 102 | +- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)rawCell forItemAtIndexPath:(NSIndexPath *)indexPath; |
| 103 | +@end |
| 104 | + |
| 105 | +@interface ASCellVisibilityCollectionViewTestController: UIViewController<ASCollectionDataSource> |
| 106 | + |
| 107 | +@property (nonatomic) ASCollectionNode *collectionNode; |
| 108 | +@property (nonatomic) ASCollectionView *collectionView; |
| 109 | + |
| 110 | +@end |
| 111 | + |
| 112 | +@implementation ASCellVisibilityCollectionViewTestController |
| 113 | + |
| 114 | +- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { |
| 115 | + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
| 116 | + if (self) { |
| 117 | + id realLayout = [UICollectionViewFlowLayout new]; |
| 118 | + self.collectionNode = [[ASCollectionNode alloc] initWithFrame:self.view.bounds collectionViewLayout:realLayout]; |
| 119 | + self.collectionView = self.collectionNode.view; |
| 120 | + self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 121 | + self.collectionNode.dataSource = self; |
| 122 | + |
| 123 | + [self.view addSubview:self.collectionView]; |
| 124 | + } |
| 125 | + return self; |
| 126 | +} |
| 127 | + |
| 128 | +- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section |
| 129 | +{ |
| 130 | + return 1; |
| 131 | +} |
| 132 | + |
| 133 | +- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath |
| 134 | +{ |
| 135 | + return ^{ |
| 136 | + ASCellVisibilityTestNode *cell = [[ASCellVisibilityTestNode alloc] init]; |
| 137 | + return cell; |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +@end |
| 142 | + |
| 143 | + |
| 144 | +@interface ASCellVisibilityScrollEventTests : XCTestCase |
| 145 | +@end |
| 146 | + |
| 147 | +@implementation ASCellVisibilityScrollEventTests |
| 148 | + |
| 149 | +- (void)testTableNodeEvents |
| 150 | +{ |
| 151 | + ASCellVisibilityTableViewTestController *testController = [[ASCellVisibilityTableViewTestController alloc] initWithNibName:nil bundle:nil]; |
| 152 | + |
| 153 | + UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 154 | + [window setRootViewController:testController]; |
| 155 | + [window makeKeyAndVisible]; |
| 156 | + |
| 157 | + [testController.tableNode reloadData]; |
| 158 | + [testController.tableNode waitUntilAllUpdatesAreProcessed]; |
| 159 | + [testController.tableNode layoutIfNeeded]; |
| 160 | + |
| 161 | + ASTableView *tableView = testController.tableView; |
| 162 | + |
| 163 | + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; |
| 164 | + ASCellVisibilityTestNode *cell = (ASCellVisibilityTestNode *)[testController.tableNode nodeForRowAtIndexPath:indexPath]; |
| 165 | + UITableViewCell *uicell = [testController.tableNode cellForRowAtIndexPath:indexPath]; |
| 166 | + |
| 167 | + // Pretend the cell is appearing so it is added to _cellsForVisibilityUpdates |
| 168 | + [tableView tableView:tableView willDisplayCell:uicell forRowAtIndexPath:indexPath]; |
| 169 | + |
| 170 | + // simulator scrollViewDidScroll so we can see if the cell got the event |
| 171 | + [tableView scrollViewDidScroll:tableView]; |
| 172 | + XCTAssertTrue(cell.cellNodeVisibilityEventVisibleRectChangedCount == 1); |
| 173 | + |
| 174 | + [tableView scrollViewDidEndDecelerating:tableView]; |
| 175 | + XCTAssertTrue(cell.cellNodeVisibilityEventDidStopScrollingCount == 1); |
| 176 | + |
| 177 | + [tableView scrollViewWillBeginDragging:tableView]; |
| 178 | + XCTAssertTrue(cell.cellNodeVisibilityEventWillBeginDraggingCount == 1); |
| 179 | + |
| 180 | + [tableView scrollViewDidEndDragging:tableView willDecelerate:YES]; |
| 181 | + XCTAssertTrue(cell.cellNodeVisibilityEventDidEndDraggingCount == 1); |
| 182 | + |
| 183 | +} |
| 184 | + |
| 185 | +- (void)testCollectionNodeEvents |
| 186 | +{ |
| 187 | + ASCellVisibilityCollectionViewTestController *testController = [[ASCellVisibilityCollectionViewTestController alloc] initWithNibName:nil bundle:nil]; |
| 188 | + |
| 189 | + UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 190 | + [window setRootViewController:testController]; |
| 191 | + [window makeKeyAndVisible]; |
| 192 | + |
| 193 | + [testController.collectionNode reloadData]; |
| 194 | + [testController.collectionNode waitUntilAllUpdatesAreProcessed]; |
| 195 | + [testController.collectionNode layoutIfNeeded]; |
| 196 | + |
| 197 | + ASCollectionView *collectionView = testController.collectionView; |
| 198 | + |
| 199 | + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; |
| 200 | + ASCellVisibilityTestNode *cell = (ASCellVisibilityTestNode *)[testController.collectionNode nodeForItemAtIndexPath:indexPath]; |
| 201 | + UICollectionViewCell *uicell = [testController.collectionNode cellForItemAtIndexPath:indexPath]; |
| 202 | + |
| 203 | + // Pretend the cell is appearing so it is added to _cellsForVisibilityUpdates |
| 204 | + [collectionView collectionView:collectionView willDisplayCell:uicell forItemAtIndexPath:indexPath]; |
| 205 | + |
| 206 | + // simulator scrollViewDidScroll so we can see if the cell got the event |
| 207 | + [collectionView scrollViewDidScroll:collectionView]; |
| 208 | + XCTAssertTrue(cell.cellNodeVisibilityEventVisibleRectChangedCount == 1); |
| 209 | + |
| 210 | + [collectionView scrollViewDidEndDecelerating:collectionView]; |
| 211 | + XCTAssertTrue(cell.cellNodeVisibilityEventDidStopScrollingCount == 1); |
| 212 | + |
| 213 | + [collectionView scrollViewWillBeginDragging:collectionView]; |
| 214 | + XCTAssertTrue(cell.cellNodeVisibilityEventWillBeginDraggingCount == 1); |
| 215 | + |
| 216 | + [collectionView scrollViewDidEndDragging:collectionView willDecelerate:YES]; |
| 217 | + XCTAssertTrue(cell.cellNodeVisibilityEventDidEndDraggingCount == 1); |
| 218 | +} |
| 219 | + |
| 220 | + |
| 221 | +@end |
| 222 | + |
0 commit comments