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