| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" | 5 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 6 | 6 |
| 7 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 7 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 8 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 8 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 9 #import "ios/chrome/test/base/scoped_block_swizzler.h" | 9 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 10 #include "ios/chrome/test/block_cleanup_test.h" | 10 #include "ios/chrome/test/block_cleanup_test.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 38 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 39 SectionIdentifierFoo = kSectionIdentifierEnumZero, | 39 SectionIdentifierFoo = kSectionIdentifierEnumZero, |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 typedef NS_ENUM(NSInteger, ItemType) { | 42 typedef NS_ENUM(NSInteger, ItemType) { |
| 43 ItemTypeFooBar = kItemTypeEnumZero, | 43 ItemTypeFooBar = kItemTypeEnumZero, |
| 44 ItemTypeFooBiz, | 44 ItemTypeFooBiz, |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 class CollectionViewControllerTest : public BlockCleanupTest {}; | 47 typedef void (^ReconfigureBlock)(CollectionViewController*, |
| 48 NSArray*, |
| 49 SectionIdentifier); |
| 50 |
| 51 class CollectionViewControllerTest : public BlockCleanupTest { |
| 52 public: |
| 53 void TestReconfigureBlock(ReconfigureBlock block) { |
| 54 // Setup. |
| 55 CollectionViewController* controller = [[CollectionViewController alloc] |
| 56 initWithStyle:CollectionViewControllerStyleDefault]; |
| 57 [controller loadModel]; |
| 58 |
| 59 CollectionViewModel* model = [controller collectionViewModel]; |
| 60 [model addSectionWithIdentifier:SectionIdentifierFoo]; |
| 61 |
| 62 MockCollectionViewItem* firstReconfiguredItem = |
| 63 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar]; |
| 64 [model addItem:firstReconfiguredItem |
| 65 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 66 |
| 67 MockCollectionViewItem* secondReconfiguredItem = |
| 68 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 69 [model addItem:secondReconfiguredItem |
| 70 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 71 |
| 72 MockCollectionViewItem* firstNonReconfiguredItem = |
| 73 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 74 [model addItem:firstNonReconfiguredItem |
| 75 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 76 |
| 77 MockCollectionViewItem* thirdReconfiguredItem = |
| 78 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 79 [model addItem:thirdReconfiguredItem |
| 80 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 81 |
| 82 MockCollectionViewItem* secondNonReconfiguredItem = |
| 83 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 84 [model addItem:secondNonReconfiguredItem |
| 85 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 86 |
| 87 // The collection view is not visible on screen, so it has not created any |
| 88 // of its cells. Swizzle |cellsForItemAtIndexPath:| and inject an |
| 89 // implementation for testing that always returns a non-nil cell. |
| 90 MDCCollectionViewCell* dummyCell = [[MDCCollectionViewCell alloc] init]; |
| 91 { |
| 92 ScopedBlockSwizzler swizzler([UICollectionView class], |
| 93 @selector(cellForItemAtIndexPath:), |
| 94 ^(id self) { |
| 95 return dummyCell; |
| 96 }); |
| 97 |
| 98 NSArray* itemsToReconfigure = @[ |
| 99 firstReconfiguredItem, secondReconfiguredItem, thirdReconfiguredItem |
| 100 ]; |
| 101 // Action. |
| 102 block(controller, itemsToReconfigure, SectionIdentifierFoo); |
| 103 } |
| 104 |
| 105 // Tests. |
| 106 EXPECT_TRUE([firstReconfiguredItem configureCellCalled]); |
| 107 EXPECT_TRUE([secondReconfiguredItem configureCellCalled]); |
| 108 EXPECT_TRUE([thirdReconfiguredItem configureCellCalled]); |
| 109 |
| 110 EXPECT_FALSE([firstNonReconfiguredItem configureCellCalled]); |
| 111 EXPECT_FALSE([secondNonReconfiguredItem configureCellCalled]); |
| 112 } |
| 113 }; |
| 48 | 114 |
| 49 } // namespace | 115 } // namespace |
| 50 | 116 |
| 51 TEST_F(CollectionViewControllerTest, InitDefaultStyle) { | 117 TEST_F(CollectionViewControllerTest, InitDefaultStyle) { |
| 52 CollectionViewController* controller = [[CollectionViewController alloc] | 118 CollectionViewController* controller = [[CollectionViewController alloc] |
| 53 initWithStyle:CollectionViewControllerStyleDefault]; | 119 initWithStyle:CollectionViewControllerStyleDefault]; |
| 54 EXPECT_EQ(nil, controller.appBar); | 120 EXPECT_EQ(nil, controller.appBar); |
| 55 } | 121 } |
| 56 | 122 |
| 57 TEST_F(CollectionViewControllerTest, InitAppBarStyle) { | 123 TEST_F(CollectionViewControllerTest, InitAppBarStyle) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 72 [[controller collectionViewModel] addItem:someItem | 138 [[controller collectionViewModel] addItem:someItem |
| 73 toSectionWithIdentifier:SectionIdentifierFoo]; | 139 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 74 | 140 |
| 75 ASSERT_EQ(NO, [someItem configureCellCalled]); | 141 ASSERT_EQ(NO, [someItem configureCellCalled]); |
| 76 [controller collectionView:[controller collectionView] | 142 [controller collectionView:[controller collectionView] |
| 77 cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; | 143 cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 78 EXPECT_EQ(YES, [someItem configureCellCalled]); | 144 EXPECT_EQ(YES, [someItem configureCellCalled]); |
| 79 } | 145 } |
| 80 | 146 |
| 81 TEST_F(CollectionViewControllerTest, ReconfigureCells) { | 147 TEST_F(CollectionViewControllerTest, ReconfigureCells) { |
| 82 CollectionViewController* controller = [[CollectionViewController alloc] | 148 TestReconfigureBlock(^void(CollectionViewController* controller, |
| 83 initWithStyle:CollectionViewControllerStyleDefault]; | 149 NSArray* itemsToReconfigure, |
| 84 [controller loadModel]; | 150 SectionIdentifier sectionIdentifier) { |
| 151 [controller reconfigureCellsForItems:itemsToReconfigure |
| 152 inSectionWithIdentifier:sectionIdentifier]; |
| 153 }); |
| 154 } |
| 85 | 155 |
| 86 CollectionViewModel* model = [controller collectionViewModel]; | 156 TEST_F(CollectionViewControllerTest, ReconfigureCellsWithIndexPath) { |
| 87 [model addSectionWithIdentifier:SectionIdentifierFoo]; | 157 TestReconfigureBlock(^void(CollectionViewController* controller, |
| 158 NSArray* itemsToReconfigure, |
| 159 SectionIdentifier sectionIdentifier) { |
| 160 // More setup. |
| 161 NSMutableArray* indexPaths = [NSMutableArray array]; |
| 162 for (CollectionViewItem* item : itemsToReconfigure) { |
| 163 NSIndexPath* indexPath = |
| 164 [controller.collectionViewModel indexPathForItem:item |
| 165 inSectionWithIdentifier:sectionIdentifier]; |
| 166 if (indexPath) { |
| 167 [indexPaths addObject:indexPath]; |
| 168 } |
| 169 } |
| 88 | 170 |
| 89 MockCollectionViewItem* firstReconfiguredItem = | 171 // Action. |
| 90 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar]; | 172 [controller reconfigureCellsAtIndexPaths:indexPaths]; |
| 91 [model addItem:firstReconfiguredItem | 173 }); |
| 92 toSectionWithIdentifier:SectionIdentifierFoo]; | |
| 93 | |
| 94 MockCollectionViewItem* secondReconfiguredItem = | |
| 95 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; | |
| 96 [model addItem:secondReconfiguredItem | |
| 97 toSectionWithIdentifier:SectionIdentifierFoo]; | |
| 98 | |
| 99 MockCollectionViewItem* firstNonReconfiguredItem = | |
| 100 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; | |
| 101 [model addItem:firstNonReconfiguredItem | |
| 102 toSectionWithIdentifier:SectionIdentifierFoo]; | |
| 103 | |
| 104 MockCollectionViewItem* thirdReconfiguredItem = | |
| 105 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; | |
| 106 [model addItem:thirdReconfiguredItem | |
| 107 toSectionWithIdentifier:SectionIdentifierFoo]; | |
| 108 | |
| 109 MockCollectionViewItem* secondNonReconfiguredItem = | |
| 110 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; | |
| 111 [model addItem:secondNonReconfiguredItem | |
| 112 toSectionWithIdentifier:SectionIdentifierFoo]; | |
| 113 | |
| 114 // The collection view is not visible on screen, so it has not created any of | |
| 115 // its cells. Swizzle |cellsForItemAtIndexPath:| and inject an implementation | |
| 116 // for testing that always returns a non-nil cell. | |
| 117 MDCCollectionViewCell* dummyCell = [[MDCCollectionViewCell alloc] init]; | |
| 118 { | |
| 119 ScopedBlockSwizzler swizzler([UICollectionView class], | |
| 120 @selector(cellForItemAtIndexPath:), | |
| 121 ^(id self) { | |
| 122 return dummyCell; | |
| 123 }); | |
| 124 | |
| 125 NSArray* itemsToReconfigure = @[ | |
| 126 firstReconfiguredItem, secondReconfiguredItem, thirdReconfiguredItem | |
| 127 ]; | |
| 128 [controller reconfigureCellsForItems:itemsToReconfigure | |
| 129 inSectionWithIdentifier:SectionIdentifierFoo]; | |
| 130 } | |
| 131 | |
| 132 EXPECT_TRUE([firstReconfiguredItem configureCellCalled]); | |
| 133 EXPECT_TRUE([secondReconfiguredItem configureCellCalled]); | |
| 134 EXPECT_TRUE([thirdReconfiguredItem configureCellCalled]); | |
| 135 | |
| 136 EXPECT_FALSE([firstNonReconfiguredItem configureCellCalled]); | |
| 137 EXPECT_FALSE([secondNonReconfiguredItem configureCellCalled]); | |
| 138 } | 174 } |
| OLD | NEW |