Skip to content

Commit

Permalink
Merge pull request #1109 from harleyjcooper/harley/gaperlinski/collec…
Browse files Browse the repository at this point in the history
…tionViewScrollAtPosition

Add "atPosition:" variations to UICollectionView waitForCellAtIndexPath methods
  • Loading branch information
harleyjcooper authored Jun 18, 2019
2 parents ae07a65 + f5fb6b0 commit 84f1319
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
33 changes: 30 additions & 3 deletions Classes/KIFUITestActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,19 +752,33 @@ typedef NS_ENUM(NSUInteger, KIFPullToRefreshTiming) {
@abstract Scrolls a collection view while waiting for the cell at the given indexPath to appear.
@discussion This step will get the cell at the indexPath.
For cases where you may need to work from the end of a table view rather than the beginning, negative sections count back from the end of the table view (-1 is the last section) and negative rows count back from the end of the section (-1 is the last row for that section).
By default, scrolls to the vertical and horizontal middle of the cell. If you need to scroll to custom positions, use the @c atPosition: variation.
For cases where you may need to work from the end of a collection view rather than the beginning, negative sections count back from the end of the collection view (-1 is the last section) and negative rows count back from the end of the section (-1 is the last row for that section).
@param indexPath Index path of the cell.
@param collectionView UICollectionView containing the cell.
@result Collection view cell at index path
*/
- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView;

/*!
@abstract Scrolls a collection view while waiting for the cell at the given indexPath to appear.
@discussion This step will get the cell at the indexPath.
For cases where you may need to work from the end of a collection view rather than the beginning, negative sections count back from the end of the collection view (-1 is the last section) and negative rows count back from the end of the section (-1 is the last row for that section).
@param indexPath Index path of the cell.
@param collectionView UICollectionView containing the cell.
@param position Collection View scroll position to scroll to. Useful for custom-sized cells when the content needed is in a specific location.
@result Collection view cell at index path
*/
- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView atPosition:(UICollectionViewScrollPosition)position;

/*!
@abstract Scrolls a given collection view while waiting for the item at the given indexPath to appear.
@abstract Scrolls a collection view with the given identifier while waiting for the item at the given indexPath to appear.
@discussion This step will get the view with the specified accessibility identifier and then get the cell at indexPath.
By default, scrolls to the vertical and horizontal middle of the cell. If you need to scroll to custom positions, use the @c atPosition: variation.
For cases where you may need to work from the end of a collection view rather than the beginning, negative sections count back from the end of the collection view (-1 is the last section) and negative items count back from the end of the section (-1 is the last item for that section).
@param indexPath Index path of the item to tap.
Expand All @@ -773,6 +787,19 @@ typedef NS_ENUM(NSUInteger, KIFPullToRefreshTiming) {
*/
- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessibilityIdentifier:(NSString *)identifier;

/*!
@abstract Scrolls a collection view with the given identifier while waiting for the item at the given indexPath to appear.
@discussion This step will get the view with the specified accessibility identifier and then get the cell at indexPath.
For cases where you may need to work from the end of a collection view rather than the beginning, negative sections count back from the end of the collection view (-1 is the last section) and negative rows count back from the end of the section (-1 is the last row for that section).
@param indexPath Index path of the cell.
@param identifier Accessibility identifier of the table view.
@param position Collection View scroll position to scroll to. Useful for custom-sized cells when the content needed is in a specific location.
@result Table view cell at index path
*/
- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessibilityIdentifier:(NSString *)identifier atPosition:(UICollectionViewScrollPosition)position;

/*!
@abstract Moves the row at sourceIndexPath to destinationIndexPath in a table view with the given identifier.
@discussion This step will get the view with the specified accessibility identifier and move the row at sourceIndexPath to destinationIndexPath.
Expand Down
16 changes: 14 additions & 2 deletions Classes/KIFUITestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -1273,13 +1273,25 @@ - (UITableViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inTableView
}

- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessibilityIdentifier:(NSString *)identifier
{
return [self waitForCellAtIndexPath:indexPath inCollectionViewWithAccessibilityIdentifier:identifier atPosition:UICollectionViewScrollPositionCenteredHorizontally |
UICollectionViewScrollPositionCenteredVertically];
}

- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessibilityIdentifier:(NSString *)identifier atPosition:(UICollectionViewScrollPosition)position;
{
UICollectionView *collectionView;
[self waitForAccessibilityElement:NULL view:&collectionView withIdentifier:identifier tappable:NO];
return [self waitForCellAtIndexPath:indexPath inCollectionView:collectionView];
return [self waitForCellAtIndexPath:indexPath inCollectionView:collectionView atPosition:position];
}

- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
return [self waitForCellAtIndexPath:indexPath inCollectionView:collectionView atPosition:UICollectionViewScrollPositionCenteredHorizontally |
UICollectionViewScrollPositionCenteredVertically];
}

- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView atPosition:(UICollectionViewScrollPosition)position
{
if (![collectionView isKindOfClass:[UICollectionView class]]) {
[self failWithError:[NSError KIFErrorWithFormat:@"View is not a collection view"] stopTest:YES];
Expand Down Expand Up @@ -1310,7 +1322,7 @@ - (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inColl
}];

[collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically
atScrollPosition:position
animated:[[self class] testActorAnimationsEnabled]];

// waitForAnimationsToFinish doesn't allow collection view to settle when animations are sped up
Expand Down
25 changes: 25 additions & 0 deletions KIF Tests/CollectionViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ - (void)testTappingLastItemAndSectionWithoutAnimation
}


- (void)testScrollingLastAndFirstRowAtPositionWithAccessiblityIdentifier
{
UICollectionViewCell *lastCell = [tester waitForCellAtIndexPath:[NSIndexPath indexPathForRow:-1 inSection:-1] inCollectionViewWithAccessibilityIdentifier:@"CollectionView Tests CollectionView" atPosition:UICollectionViewScrollPositionBottom];
CGRect lastCellConverted = [lastCell.superview convertRect:lastCell.frame toView:nil];
CGRect superviewConverted = [lastCell convertRect:lastCell.superview.frame toView:nil];
XCTAssertEqual(lastCellConverted.origin.y, superviewConverted.origin.y);
UICollectionViewCell *firstCell = [tester waitForCellAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] inCollectionViewWithAccessibilityIdentifier:@"CollectionView Tests CollectionView" atPosition:UICollectionViewScrollPositionTop];
CGRect firstCellConverted = [firstCell.superview convertRect:firstCell.frame toView:nil];
superviewConverted = [firstCell convertRect:firstCell.superview.frame toView:nil];
XCTAssertEqual(firstCellConverted.origin.y, superviewConverted.origin.y);
}

- (void)testScrollingLastAndFirstRowAtPositionWithView
{
UICollectionView *collectionView = (UICollectionView *)[[viewTester usingIdentifier:@"CollectionView Tests CollectionView"] waitForView];
UICollectionViewCell *lastCell = [tester waitForCellAtIndexPath:[NSIndexPath indexPathForRow:-1 inSection:-1] inCollectionView:collectionView atPosition:UICollectionViewScrollPositionBottom];
CGRect lastCellConverted = [lastCell.superview convertRect:lastCell.frame toView:nil];
CGRect superviewConverted = [lastCell convertRect:lastCell.superview.frame toView:nil];
XCTAssertEqual(lastCellConverted.origin.y, superviewConverted.origin.y);
UICollectionViewCell *firstCell = [tester waitForCellAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] inCollectionView:collectionView atPosition:UICollectionViewScrollPositionTop];
CGRect firstCellConverted = [firstCell.superview convertRect:firstCell.frame toView:nil];
superviewConverted = [firstCell convertRect:firstCell.superview.frame toView:nil];
XCTAssertEqual(firstCellConverted.origin.y, superviewConverted.origin.y);
}

- (void)testOutOfBounds
{
KIFExpectFailure([[tester usingTimeout:1] tapItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:99] inCollectionViewWithAccessibilityIdentifier:@"CollectionView Tests CollectionView"]);
Expand Down

0 comments on commit 84f1319

Please sign in to comment.