| Index: ios/chrome/browser/ui/collection_view/collection_view_controller_unittest.mm
|
| diff --git a/ios/chrome/browser/ui/collection_view/collection_view_controller_unittest.mm b/ios/chrome/browser/ui/collection_view/collection_view_controller_unittest.mm
|
| index d2318fb8c83e5c3d466e448901c863b5bcd53aa3..aaaeeb9175a19f12ce0a38fccd0f69043e1e764b 100644
|
| --- a/ios/chrome/browser/ui/collection_view/collection_view_controller_unittest.mm
|
| +++ b/ios/chrome/browser/ui/collection_view/collection_view_controller_unittest.mm
|
| @@ -44,7 +44,73 @@ typedef NS_ENUM(NSInteger, ItemType) {
|
| ItemTypeFooBiz,
|
| };
|
|
|
| -class CollectionViewControllerTest : public BlockCleanupTest {};
|
| +typedef void (^ReconfigureBlock)(CollectionViewController*,
|
| + NSArray*,
|
| + SectionIdentifier);
|
| +
|
| +class CollectionViewControllerTest : public BlockCleanupTest {
|
| + public:
|
| + void TestReconfigureBlock(ReconfigureBlock block) {
|
| + // Setup.
|
| + CollectionViewController* controller = [[CollectionViewController alloc]
|
| + initWithStyle:CollectionViewControllerStyleDefault];
|
| + [controller loadModel];
|
| +
|
| + CollectionViewModel* model = [controller collectionViewModel];
|
| + [model addSectionWithIdentifier:SectionIdentifierFoo];
|
| +
|
| + MockCollectionViewItem* firstReconfiguredItem =
|
| + [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar];
|
| + [model addItem:firstReconfiguredItem
|
| + toSectionWithIdentifier:SectionIdentifierFoo];
|
| +
|
| + MockCollectionViewItem* secondReconfiguredItem =
|
| + [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| + [model addItem:secondReconfiguredItem
|
| + toSectionWithIdentifier:SectionIdentifierFoo];
|
| +
|
| + MockCollectionViewItem* firstNonReconfiguredItem =
|
| + [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| + [model addItem:firstNonReconfiguredItem
|
| + toSectionWithIdentifier:SectionIdentifierFoo];
|
| +
|
| + MockCollectionViewItem* thirdReconfiguredItem =
|
| + [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| + [model addItem:thirdReconfiguredItem
|
| + toSectionWithIdentifier:SectionIdentifierFoo];
|
| +
|
| + MockCollectionViewItem* secondNonReconfiguredItem =
|
| + [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| + [model addItem:secondNonReconfiguredItem
|
| + toSectionWithIdentifier:SectionIdentifierFoo];
|
| +
|
| + // The collection view is not visible on screen, so it has not created any
|
| + // of its cells. Swizzle |cellsForItemAtIndexPath:| and inject an
|
| + // implementation for testing that always returns a non-nil cell.
|
| + MDCCollectionViewCell* dummyCell = [[MDCCollectionViewCell alloc] init];
|
| + {
|
| + ScopedBlockSwizzler swizzler([UICollectionView class],
|
| + @selector(cellForItemAtIndexPath:),
|
| + ^(id self) {
|
| + return dummyCell;
|
| + });
|
| +
|
| + NSArray* itemsToReconfigure = @[
|
| + firstReconfiguredItem, secondReconfiguredItem, thirdReconfiguredItem
|
| + ];
|
| + // Action.
|
| + block(controller, itemsToReconfigure, SectionIdentifierFoo);
|
| + }
|
| +
|
| + // Tests.
|
| + EXPECT_TRUE([firstReconfiguredItem configureCellCalled]);
|
| + EXPECT_TRUE([secondReconfiguredItem configureCellCalled]);
|
| + EXPECT_TRUE([thirdReconfiguredItem configureCellCalled]);
|
| +
|
| + EXPECT_FALSE([firstNonReconfiguredItem configureCellCalled]);
|
| + EXPECT_FALSE([secondNonReconfiguredItem configureCellCalled]);
|
| + }
|
| +};
|
|
|
| } // namespace
|
|
|
| @@ -79,60 +145,30 @@ TEST_F(CollectionViewControllerTest, CellForItemAtIndexPath) {
|
| }
|
|
|
| TEST_F(CollectionViewControllerTest, ReconfigureCells) {
|
| - CollectionViewController* controller = [[CollectionViewController alloc]
|
| - initWithStyle:CollectionViewControllerStyleDefault];
|
| - [controller loadModel];
|
| -
|
| - CollectionViewModel* model = [controller collectionViewModel];
|
| - [model addSectionWithIdentifier:SectionIdentifierFoo];
|
| -
|
| - MockCollectionViewItem* firstReconfiguredItem =
|
| - [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar];
|
| - [model addItem:firstReconfiguredItem
|
| - toSectionWithIdentifier:SectionIdentifierFoo];
|
| -
|
| - MockCollectionViewItem* secondReconfiguredItem =
|
| - [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| - [model addItem:secondReconfiguredItem
|
| - toSectionWithIdentifier:SectionIdentifierFoo];
|
| -
|
| - MockCollectionViewItem* firstNonReconfiguredItem =
|
| - [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| - [model addItem:firstNonReconfiguredItem
|
| - toSectionWithIdentifier:SectionIdentifierFoo];
|
| -
|
| - MockCollectionViewItem* thirdReconfiguredItem =
|
| - [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| - [model addItem:thirdReconfiguredItem
|
| - toSectionWithIdentifier:SectionIdentifierFoo];
|
| -
|
| - MockCollectionViewItem* secondNonReconfiguredItem =
|
| - [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz];
|
| - [model addItem:secondNonReconfiguredItem
|
| - toSectionWithIdentifier:SectionIdentifierFoo];
|
| -
|
| - // The collection view is not visible on screen, so it has not created any of
|
| - // its cells. Swizzle |cellsForItemAtIndexPath:| and inject an implementation
|
| - // for testing that always returns a non-nil cell.
|
| - MDCCollectionViewCell* dummyCell = [[MDCCollectionViewCell alloc] init];
|
| - {
|
| - ScopedBlockSwizzler swizzler([UICollectionView class],
|
| - @selector(cellForItemAtIndexPath:),
|
| - ^(id self) {
|
| - return dummyCell;
|
| - });
|
| -
|
| - NSArray* itemsToReconfigure = @[
|
| - firstReconfiguredItem, secondReconfiguredItem, thirdReconfiguredItem
|
| - ];
|
| + TestReconfigureBlock(^void(CollectionViewController* controller,
|
| + NSArray* itemsToReconfigure,
|
| + SectionIdentifier sectionIdentifier) {
|
| [controller reconfigureCellsForItems:itemsToReconfigure
|
| - inSectionWithIdentifier:SectionIdentifierFoo];
|
| - }
|
| -
|
| - EXPECT_TRUE([firstReconfiguredItem configureCellCalled]);
|
| - EXPECT_TRUE([secondReconfiguredItem configureCellCalled]);
|
| - EXPECT_TRUE([thirdReconfiguredItem configureCellCalled]);
|
| + inSectionWithIdentifier:sectionIdentifier];
|
| + });
|
| +}
|
|
|
| - EXPECT_FALSE([firstNonReconfiguredItem configureCellCalled]);
|
| - EXPECT_FALSE([secondNonReconfiguredItem configureCellCalled]);
|
| +TEST_F(CollectionViewControllerTest, ReconfigureCellsWithIndexPath) {
|
| + TestReconfigureBlock(^void(CollectionViewController* controller,
|
| + NSArray* itemsToReconfigure,
|
| + SectionIdentifier sectionIdentifier) {
|
| + // More setup.
|
| + NSMutableArray* indexPaths = [NSMutableArray array];
|
| + for (CollectionViewItem* item : itemsToReconfigure) {
|
| + NSIndexPath* indexPath =
|
| + [controller.collectionViewModel indexPathForItem:item
|
| + inSectionWithIdentifier:sectionIdentifier];
|
| + if (indexPath) {
|
| + [indexPaths addObject:indexPath];
|
| + }
|
| + }
|
| +
|
| + // Action.
|
| + [controller reconfigureCellsAtIndexPaths:indexPaths];
|
| + });
|
| }
|
|
|