| 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 CollectionViewController* controller = [[CollectionViewController alloc] |
| 55 initWithStyle:CollectionViewControllerStyleDefault]; |
| 56 [controller loadModel]; |
| 57 |
| 58 CollectionViewModel* model = [controller collectionViewModel]; |
| 59 [model addSectionWithIdentifier:SectionIdentifierFoo]; |
| 60 |
| 61 MockCollectionViewItem* firstReconfiguredItem = |
| 62 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar]; |
| 63 [model addItem:firstReconfiguredItem |
| 64 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 65 |
| 66 MockCollectionViewItem* secondReconfiguredItem = |
| 67 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 68 [model addItem:secondReconfiguredItem |
| 69 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 70 |
| 71 MockCollectionViewItem* firstNonReconfiguredItem = |
| 72 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 73 [model addItem:firstNonReconfiguredItem |
| 74 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 75 |
| 76 MockCollectionViewItem* thirdReconfiguredItem = |
| 77 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 78 [model addItem:thirdReconfiguredItem |
| 79 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 80 |
| 81 MockCollectionViewItem* secondNonReconfiguredItem = |
| 82 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; |
| 83 [model addItem:secondNonReconfiguredItem |
| 84 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 85 |
| 86 // The collection view is not visible on screen, so it has not created any |
| 87 // of its cells. Swizzle |cellsForItemAtIndexPath:| and inject an |
| 88 // implementation for testing that always returns a non-nil cell. |
| 89 MDCCollectionViewCell* dummyCell = [[MDCCollectionViewCell alloc] init]; |
| 90 { |
| 91 ScopedBlockSwizzler swizzler([UICollectionView class], |
| 92 @selector(cellForItemAtIndexPath:), |
| 93 ^(id self) { |
| 94 return dummyCell; |
| 95 }); |
| 96 |
| 97 NSArray* itemsToReconfigure = @[ |
| 98 firstReconfiguredItem, secondReconfiguredItem, thirdReconfiguredItem |
| 99 ]; |
| 100 block(controller, itemsToReconfigure, SectionIdentifierFoo); |
| 101 } |
| 102 |
| 103 EXPECT_TRUE([firstReconfiguredItem configureCellCalled]); |
| 104 EXPECT_TRUE([secondReconfiguredItem configureCellCalled]); |
| 105 EXPECT_TRUE([thirdReconfiguredItem configureCellCalled]); |
| 106 |
| 107 EXPECT_FALSE([firstNonReconfiguredItem configureCellCalled]); |
| 108 EXPECT_FALSE([secondNonReconfiguredItem configureCellCalled]); |
| 109 } |
| 110 }; |
| 48 | 111 |
| 49 } // namespace | 112 } // namespace |
| 50 | 113 |
| 51 TEST_F(CollectionViewControllerTest, InitDefaultStyle) { | 114 TEST_F(CollectionViewControllerTest, InitDefaultStyle) { |
| 52 CollectionViewController* controller = [[CollectionViewController alloc] | 115 CollectionViewController* controller = [[CollectionViewController alloc] |
| 53 initWithStyle:CollectionViewControllerStyleDefault]; | 116 initWithStyle:CollectionViewControllerStyleDefault]; |
| 54 EXPECT_EQ(nil, controller.appBar); | 117 EXPECT_EQ(nil, controller.appBar); |
| 55 } | 118 } |
| 56 | 119 |
| 57 TEST_F(CollectionViewControllerTest, InitAppBarStyle) { | 120 TEST_F(CollectionViewControllerTest, InitAppBarStyle) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 72 [[controller collectionViewModel] addItem:someItem | 135 [[controller collectionViewModel] addItem:someItem |
| 73 toSectionWithIdentifier:SectionIdentifierFoo]; | 136 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 74 | 137 |
| 75 ASSERT_EQ(NO, [someItem configureCellCalled]); | 138 ASSERT_EQ(NO, [someItem configureCellCalled]); |
| 76 [controller collectionView:[controller collectionView] | 139 [controller collectionView:[controller collectionView] |
| 77 cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; | 140 cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 78 EXPECT_EQ(YES, [someItem configureCellCalled]); | 141 EXPECT_EQ(YES, [someItem configureCellCalled]); |
| 79 } | 142 } |
| 80 | 143 |
| 81 TEST_F(CollectionViewControllerTest, ReconfigureCells) { | 144 TEST_F(CollectionViewControllerTest, ReconfigureCells) { |
| 82 CollectionViewController* controller = [[CollectionViewController alloc] | 145 TestReconfigureBlock(^void(CollectionViewController* controller, |
| 83 initWithStyle:CollectionViewControllerStyleDefault]; | 146 NSArray* itemsToReconfigure, |
| 84 [controller loadModel]; | 147 SectionIdentifier sectionIdentifier) { |
| 148 [controller reconfigureCellsForItems:itemsToReconfigure |
| 149 inSectionWithIdentifier:sectionIdentifier]; |
| 150 }); |
| 151 } |
| 85 | 152 |
| 86 CollectionViewModel* model = [controller collectionViewModel]; | 153 TEST_F(CollectionViewControllerTest, ReconfigureCellsWithIndexPath) { |
| 87 [model addSectionWithIdentifier:SectionIdentifierFoo]; | 154 TestReconfigureBlock(^void(CollectionViewController* controller, |
| 88 | 155 NSArray* itemsToReconfigure, |
| 89 MockCollectionViewItem* firstReconfiguredItem = | 156 SectionIdentifier sectionIdentifier) { |
| 90 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar]; | 157 NSMutableArray* indexPaths = [NSMutableArray array]; |
| 91 [model addItem:firstReconfiguredItem | 158 for (CollectionViewItem* item : itemsToReconfigure) { |
| 92 toSectionWithIdentifier:SectionIdentifierFoo]; | 159 NSIndexPath* indexPath = |
| 93 | 160 [controller.collectionViewModel indexPathForItem:item |
| 94 MockCollectionViewItem* secondReconfiguredItem = | 161 inSectionWithIdentifier:sectionIdentifier]; |
| 95 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; | 162 if (indexPath) { |
| 96 [model addItem:secondReconfiguredItem | 163 [indexPaths addObject:indexPath]; |
| 97 toSectionWithIdentifier:SectionIdentifierFoo]; | 164 } |
| 98 | 165 } |
| 99 MockCollectionViewItem* firstNonReconfiguredItem = | 166 [controller reconfigureCellsAtIndexPaths:indexPaths]; |
| 100 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]; | 167 }); |
| 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 } | 168 } |
| OLD | NEW |