OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 11 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 12 #include "ios/chrome/test/block_cleanup_test.h" |
| 13 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 // Checks that key methods are called. |
| 17 // CollectionViewItem can't easily be mocked via OCMock as one of the methods to |
| 18 // mock returns a Class type. |
| 19 @interface MockCollectionViewItem : CollectionViewItem |
| 20 @property(nonatomic, assign) BOOL configureCellCalled; |
| 21 @end |
| 22 |
| 23 @implementation MockCollectionViewItem |
| 24 |
| 25 @synthesize configureCellCalled = _configureCellCalled; |
| 26 |
| 27 - (void)configureCell:(MDCCollectionViewCell*)cell { |
| 28 self.configureCellCalled = YES; |
| 29 [super configureCell:cell]; |
| 30 } |
| 31 |
| 32 @end |
| 33 |
| 34 namespace { |
| 35 |
| 36 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 37 SectionIdentifierFoo = kSectionIdentifierEnumZero, |
| 38 }; |
| 39 |
| 40 typedef NS_ENUM(NSInteger, ItemType) { |
| 41 ItemTypeFooBar = kItemTypeEnumZero, |
| 42 ItemTypeFooBiz, |
| 43 }; |
| 44 |
| 45 class CollectionViewControllerTest : public BlockCleanupTest {}; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 TEST_F(CollectionViewControllerTest, InitDefaultStyle) { |
| 50 base::scoped_nsobject<CollectionViewController> controller( |
| 51 [[CollectionViewController alloc] |
| 52 initWithStyle:CollectionViewControllerStyleDefault]); |
| 53 EXPECT_EQ(nil, controller.get().appBar); |
| 54 } |
| 55 |
| 56 TEST_F(CollectionViewControllerTest, InitAppBarStyle) { |
| 57 base::scoped_nsobject<CollectionViewController> controller( |
| 58 [[CollectionViewController alloc] |
| 59 initWithStyle:CollectionViewControllerStyleAppBar]); |
| 60 EXPECT_NE(nil, controller.get().appBar); |
| 61 } |
| 62 |
| 63 TEST_F(CollectionViewControllerTest, CellForItemAtIndexPath) { |
| 64 base::scoped_nsobject<CollectionViewController> controller( |
| 65 [[CollectionViewController alloc] |
| 66 initWithStyle:CollectionViewControllerStyleDefault]); |
| 67 [controller loadModel]; |
| 68 |
| 69 [[controller collectionViewModel] |
| 70 addSectionWithIdentifier:SectionIdentifierFoo]; |
| 71 base::scoped_nsobject<MockCollectionViewItem> someItem( |
| 72 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar]); |
| 73 [[controller collectionViewModel] addItem:someItem |
| 74 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 75 |
| 76 ASSERT_EQ(NO, [someItem configureCellCalled]); |
| 77 [controller collectionView:[controller collectionView] |
| 78 cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 79 EXPECT_EQ(YES, [someItem configureCellCalled]); |
| 80 } |
| 81 |
| 82 TEST_F(CollectionViewControllerTest, ReconfigureCells) { |
| 83 base::scoped_nsobject<CollectionViewController> controller( |
| 84 [[CollectionViewController alloc] |
| 85 initWithStyle:CollectionViewControllerStyleDefault]); |
| 86 [controller loadModel]; |
| 87 |
| 88 CollectionViewModel* model = [controller collectionViewModel]; |
| 89 [model addSectionWithIdentifier:SectionIdentifierFoo]; |
| 90 |
| 91 base::scoped_nsobject<MockCollectionViewItem> firstReconfiguredItem( |
| 92 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBar]); |
| 93 [model addItem:firstReconfiguredItem |
| 94 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 95 |
| 96 base::scoped_nsobject<MockCollectionViewItem> secondReconfiguredItem( |
| 97 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]); |
| 98 [model addItem:secondReconfiguredItem |
| 99 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 100 |
| 101 base::scoped_nsobject<MockCollectionViewItem> firstNonReconfiguredItem( |
| 102 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]); |
| 103 [model addItem:firstNonReconfiguredItem |
| 104 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 105 |
| 106 base::scoped_nsobject<MockCollectionViewItem> thirdReconfiguredItem( |
| 107 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]); |
| 108 [model addItem:thirdReconfiguredItem |
| 109 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 110 |
| 111 base::scoped_nsobject<MockCollectionViewItem> secondNonReconfiguredItem( |
| 112 [[MockCollectionViewItem alloc] initWithType:ItemTypeFooBiz]); |
| 113 [model addItem:secondNonReconfiguredItem |
| 114 toSectionWithIdentifier:SectionIdentifierFoo]; |
| 115 |
| 116 // The collection view is not visible on screen, so it has not created any of |
| 117 // its cells. Swizzle |cellsForItemAtIndexPath:| and inject an implementation |
| 118 // for testing that always returns a non-nil cell. |
| 119 base::scoped_nsobject<MDCCollectionViewCell> dummyCell( |
| 120 [[MDCCollectionViewCell alloc] init]); |
| 121 { |
| 122 ScopedBlockSwizzler swizzler([UICollectionView class], |
| 123 @selector(cellForItemAtIndexPath:), |
| 124 ^(id self) { |
| 125 return dummyCell.get(); |
| 126 }); |
| 127 |
| 128 NSArray* itemsToReconfigure = @[ |
| 129 firstReconfiguredItem, secondReconfiguredItem, thirdReconfiguredItem |
| 130 ]; |
| 131 [controller reconfigureCellsForItems:itemsToReconfigure |
| 132 inSectionWithIdentifier:SectionIdentifierFoo]; |
| 133 } |
| 134 |
| 135 EXPECT_TRUE([firstReconfiguredItem configureCellCalled]); |
| 136 EXPECT_TRUE([secondReconfiguredItem configureCellCalled]); |
| 137 EXPECT_TRUE([thirdReconfiguredItem configureCellCalled]); |
| 138 |
| 139 EXPECT_FALSE([firstNonReconfiguredItem configureCellCalled]); |
| 140 EXPECT_FALSE([secondNonReconfiguredItem configureCellCalled]); |
| 141 } |
OLD | NEW |