From 7d9ccfc9eade6d0168314cf5f20df32a7ea48ef5 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Mon, 17 Apr 2023 20:41:49 -0700 Subject: [PATCH] Added test coverage to collection view and collection view layout Summary: Adds additional test coverage to the collection view and collection view layout test suites Reviewed By: candance Differential Revision: D45004591 fbshipit-source-id: 21587df6f9595cb54481067322aae6443e089152 --- Tests/IGListCollectionViewLayoutTests.m | 57 +++++++++++++++++++++++++ Tests/IGListCollectionViewTests.m | 40 +++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/Tests/IGListCollectionViewLayoutTests.m b/Tests/IGListCollectionViewLayoutTests.m index aefd12cc..7a989613 100644 --- a/Tests/IGListCollectionViewLayoutTests.m +++ b/Tests/IGListCollectionViewLayoutTests.m @@ -12,9 +12,18 @@ #import "IGLayoutTestDataSource.h" #import "IGLayoutTestItem.h" #import "IGLayoutTestSection.h" +#import "IGListAdapter.h" +#import "IGListAdapterProxy.h" +#import "IGListAdapterUpdater.h" #import "IGListCollectionViewLayoutInternal.h" #import "IGListTestHelpers.h" +@interface IGListCollectionViewLayout (Tests) + +- (NSString *)_classNameForDelegate:(id)delegate sectionIndex:(NSInteger)section; + +@end + @interface IGListCollectionViewLayoutTests : XCTestCase @property (nonatomic, strong) IGListCollectionViewLayout *layout; @@ -84,6 +93,24 @@ static const CGRect kTestFrame = (CGRect){{0, 0}, {100, 100}}; [self.collectionView layoutIfNeeded]; } +- (void)test_whenCreatingViaCoder_thatObjectIsValid { + XCTAssertNotNil([[IGListCollectionViewLayout alloc] initWithCoder:[NSCoder new]]); +} + +- (void)test_whenApplyingSameBoundsValue_thatLayoutIsntInvalidated { + [self setUpWithStickyHeaders:YES topInset:0]; + [self prepareWithData:nil]; + XCTAssertFalse([self.layout shouldInvalidateLayoutForBoundsChange:self.collectionView.bounds]); +} + +- (void)test_whenApplyingInvalidatedSectionLogic_thatMinimumInvalidatedSectionIsCorrect { + [self setUpWithStickyHeaders:YES topInset:0]; + [self prepareWithData:nil]; + [self.layout didModifySection:NSNotFound]; + [self.layout didModifySection:0]; + [self.layout didModifySection:NSNotFound]; +} + - (void)test_whenEmptyData_thatContentSizeZero { [self setUpWithStickyHeaders:YES topInset:0]; @@ -1221,4 +1248,34 @@ static const CGRect kTestFrame = (CGRect){{0, 0}, {100, 100}}; IGAssertEqualFrame([self cellForSection:0 item:2].frame, 40, 0, 20, 20); } +#pragma mark - Internal debugging + +- (void)test_withDelegateNameDebugger_thatReturnedNamesAreValid { + [self setUpWithStickyHeaders:NO topInset:0]; + + // Test with the regular delegate + XCTAssertTrue([[self.layout _classNameForDelegate:(id)self.collectionView.delegate + sectionIndex:0] + isEqualToString:@"IGLayoutTestDataSource"]); + + // Test with a proxy providing a new adapter + IGListAdapter *adapter = [[IGListAdapter alloc] initWithUpdater:[IGListAdapterUpdater new] viewController:nil]; + IGListAdapterProxy *proxy = [[IGListAdapterProxy alloc] initWithCollectionViewTarget:self.collectionView.delegate + scrollViewTarget:nil + interceptor:adapter]; + XCTAssertNil([self.layout _classNameForDelegate:(id)proxy sectionIndex:0]); + + // Test with a proxy with an invalid adapter + IGListAdapterProxy *invalidProxy = [[IGListAdapterProxy alloc] initWithCollectionViewTarget:self.collectionView.delegate + scrollViewTarget:nil + interceptor:(id)[NSObject new]]; + XCTAssertNil([self.layout _classNameForDelegate:(id)invalidProxy sectionIndex:0]); +} + +- (void)test_withSupplementalViewAttributes_thatOOBErrorsAreHandled { + [self setUpWithStickyHeaders:NO topInset:0]; + XCTAssertNil([self.layout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader + atIndexPath:[NSIndexPath indexPathForItem:10 inSection:10]]); +} + @end diff --git a/Tests/IGListCollectionViewTests.m b/Tests/IGListCollectionViewTests.m index c72de26b..badebc05 100644 --- a/Tests/IGListCollectionViewTests.m +++ b/Tests/IGListCollectionViewTests.m @@ -134,6 +134,33 @@ IGAssertEqualFrame([self cellForSection:2 item:0].frame, 40, 0, 20, 20); } +- (void)test_whenMoveItem_thatLayoutPartiallyUpdates { + self.dataSource.sections = @[ + genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]), + genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(20, 20))]), + genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(30, 30))]) + ]; + [self.collectionView reloadData]; + [self.collectionView layoutIfNeeded]; + + NSArray *sections = @[genLayoutTestItem(CGSizeMake(10, 10)), genLayoutTestItem(CGSizeMake(20, 20))]; + self.dataSource.sections = @[ + genLayoutTestSection(sections), + genLayoutTestSection(@[]), + genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(20, 20))]), + ]; + + [self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1] + toIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]]; + + // check that section 0 wasn't updated + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 10, 10); + // check that section 1 was updated + IGAssertEqualFrame([self cellForSection:0 item:1].frame, 10, 0, 20, 20); + // check that section 2 was updated + IGAssertEqualFrame([self cellForSection:2 item:0].frame, 30, 0, 20, 20); +} + #pragma mark - Batch - (void)test_whenInsertDeleteMoveSection_thatLayoutPartiallyUpdates { @@ -178,6 +205,19 @@ }]; } +- (void)test_whenInsertingNilSection_thatExecutionCompletesCleanly { + self.dataSource.sections = @[ + genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]) + ]; + [self.collectionView reloadData]; + [self.collectionView layoutIfNeeded]; + + [self.collectionView insertSections:[NSIndexSet indexSet]]; + + // check that section 0 wasn't updated + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 10, 10); +} + #pragma mark - Helpers - (UICollectionViewCell *)cellForSection:(NSInteger)section item:(NSInteger)item {