From 8840ad2bc82ca6ebbdf1c145fa4514a4e17bff15 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Fri, 6 Oct 2023 03:08:34 -0700 Subject: [PATCH] Update IGListCollectionViewTests for new collection view behaviour Summary: It seems the under-the-hood changes made to `UICollectionView` from iOS 16.4 onwards has changed our expectations of how these tests work. When initially testing, it seemed that the new behaviour was that the existing cell in the view would get updated, but any newly inserted cells wouldn't be added to the collection view yet. Thinking this might simply be invalid behaviour when the collection view isn't added to a superview, I added the collection view to a new `UIWindow` instance. When I re-ran the tests again while the view has a superview, all of the views update correctly, and none of them were left in a partial state. I'm not too sure what the original intent of testing the collection view cells for being in a partially updated state was, but I think we need to reconsider that for this new `UICollectionView` behaviour. Differential Revision: D49906268 fbshipit-source-id: 7fdc7ba3a534bd49a8a0684888283d2d1eba5912 --- Tests/IGListCollectionViewTests.m | 48 +++++++++++++++++-------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/Tests/IGListCollectionViewTests.m b/Tests/IGListCollectionViewTests.m index badebc058..8f7ba1b90 100644 --- a/Tests/IGListCollectionViewTests.m +++ b/Tests/IGListCollectionViewTests.m @@ -16,6 +16,7 @@ @interface IGListCollectionViewTests : XCTestCase +@property (nonatomic, strong) UIWindow *window; @property (nonatomic, strong) IGListCollectionView *collectionView; @property (nonatomic, strong) IGLayoutTestDataSource *dataSource; @@ -25,10 +26,13 @@ @implementation IGListCollectionViewTests - (void)setUp { [super setUp]; + const CGRect frame = CGRectMake(0, 0, 100, 100); + self.window = [[UIWindow alloc] initWithFrame:frame]; self.dataSource = [IGLayoutTestDataSource new]; - self.collectionView = [[IGListCollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; + self.collectionView = [[IGListCollectionView alloc] initWithFrame:frame]; self.collectionView.dataSource = self.dataSource; self.collectionView.delegate = self.dataSource; + [self.window addSubview:self.collectionView]; [self.dataSource configCollectionView:self.collectionView]; } @@ -54,7 +58,7 @@ - (void)test_whenReloadData_thatEntireLayoutUpdates { #pragma mark - Insert/Delete/Reload/Move -- (void)test_whenInsertingSection_thatLayoutPartiallyUpdates { +- (void)test_whenInsertingSection_thatLayoutUpdates { self.dataSource.sections = @[ genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]) ]; @@ -67,13 +71,13 @@ - (void)test_whenInsertingSection_thatLayoutPartiallyUpdates { ]; [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:1]]; - // check that section 0 wasn't updated - IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 10, 10); + // check that section 0 was updated + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 20, 20); // check that section 1 was updated - IGAssertEqualFrame([self cellForSection:1 item:0].frame, 10, 0, 10, 10); + IGAssertEqualFrame([self cellForSection:1 item:0].frame, 20, 0, 10, 10); } -- (void)test_whenDeletingSection_thatLayoutPartiallyUpdates { +- (void)test_whenDeletingSection_thatLayoutUpdates { self.dataSource.sections = @[ genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]), genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]) @@ -87,10 +91,10 @@ - (void)test_whenDeletingSection_thatLayoutPartiallyUpdates { [self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:1]]; // check that section 0 wasn't updated - IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 10, 10); + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 20, 20); } -- (void)test_whenReloadingSection_thatLayoutPartiallyUpdates { +- (void)test_whenReloadingSection_thatLayoutUpdates { self.dataSource.sections = @[ genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]), genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]) @@ -104,13 +108,13 @@ - (void)test_whenReloadingSection_thatLayoutPartiallyUpdates { ]; [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]]; - // check that section 0 wasn't updated - IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 10, 10); + // check that section 0 was updated + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 20, 20); // check that section 1 was updated - IGAssertEqualFrame([self cellForSection:1 item:0].frame, 10, 0, 20, 20); + IGAssertEqualFrame([self cellForSection:1 item:0].frame, 20, 0, 20, 20); } -- (void)test_whenMoveSection_thatLayoutPartiallyUpdates { +- (void)test_whenMoveSection_thatLayoutUpdates { self.dataSource.sections = @[ genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(10, 10))]), genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(20, 20))]), @@ -126,12 +130,12 @@ - (void)test_whenMoveSection_thatLayoutPartiallyUpdates { ]; [self.collectionView moveSection:1 toSection:2]; - // check that section 0 wasn't updated - IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 10, 10); + // check that section 0 was updated + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 40, 40); // check that section 1 was updated - IGAssertEqualFrame([self cellForSection:1 item:0].frame, 10, 0, 30, 30); + IGAssertEqualFrame([self cellForSection:1 item:0].frame, 40, 0, 30, 30); // check that section 2 was updated - IGAssertEqualFrame([self cellForSection:2 item:0].frame, 40, 0, 20, 20); + IGAssertEqualFrame([self cellForSection:2 item:0].frame, 70, 0, 20, 20); } - (void)test_whenMoveItem_thatLayoutPartiallyUpdates { @@ -163,7 +167,7 @@ - (void)test_whenMoveItem_thatLayoutPartiallyUpdates { #pragma mark - Batch -- (void)test_whenInsertDeleteMoveSection_thatLayoutPartiallyUpdates { +- (void)test_whenInsertDeleteMoveSection_thatLayoutUpdates { self.dataSource.sections = @[ genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(1, 1))]), genLayoutTestSection(@[genLayoutTestItem(CGSizeMake(2, 2))]), @@ -190,14 +194,14 @@ - (void)test_whenInsertDeleteMoveSection_thatLayoutPartiallyUpdates { [self.collectionView layoutIfNeeded]; [expectation fulfill]; - // check that section 0 wasn't updated - IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 1, 1); + // check that section 0 was updated + IGAssertEqualFrame([self cellForSection:0 item:0].frame, 0, 0, 0, 0); // check that section 1 was updated - IGAssertEqualFrame([self cellForSection:1 item:0].frame, 1, 0, 4, 4); + IGAssertEqualFrame([self cellForSection:1 item:0].frame, 0, 0, 4, 4); // check that section 2 was updated - IGAssertEqualFrame([self cellForSection:2 item:0].frame, 5, 0, 3, 3); + IGAssertEqualFrame([self cellForSection:2 item:0].frame, 4, 0, 3, 3); // check that section 3 was updated - IGAssertEqualFrame([self cellForSection:3 item:0].frame, 8, 0, 5, 5); + IGAssertEqualFrame([self cellForSection:3 item:0].frame, 7, 0, 5, 5); }]; [self waitForExpectationsWithTimeout:30 handler:^(NSError * _Nullable error) {