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/settings/settings_root_collection_view_controller
.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/gtest_mac.h" |
| 14 #include "testing/platform_test.h" |
| 15 |
| 16 @interface TestSettingsRootCollectionViewController |
| 17 : SettingsRootCollectionViewController |
| 18 @property(nonatomic, readonly) NSMutableArray* items; |
| 19 - (void)reset; |
| 20 @end |
| 21 |
| 22 @implementation TestSettingsRootCollectionViewController { |
| 23 base::scoped_nsobject<NSMutableArray> _items; |
| 24 } |
| 25 |
| 26 - (NSMutableArray*)items { |
| 27 if (!_items) { |
| 28 _items.reset([[NSMutableArray alloc] init]); |
| 29 } |
| 30 return _items.get(); |
| 31 } |
| 32 |
| 33 - (void)reset { |
| 34 _items.reset(); |
| 35 } |
| 36 |
| 37 - (void)loadModel { |
| 38 [super loadModel]; |
| 39 if ([self.items count] > 0) { |
| 40 [self.collectionViewModel |
| 41 addSectionWithIdentifier:kSectionIdentifierEnumZero]; |
| 42 } |
| 43 for (CollectionViewItem* item in self.items) { |
| 44 [self.collectionViewModel addItem:item |
| 45 toSectionWithIdentifier:kSectionIdentifierEnumZero]; |
| 46 } |
| 47 } |
| 48 |
| 49 @end |
| 50 |
| 51 namespace { |
| 52 |
| 53 class SettingsRootCollectionViewControllerTest : public PlatformTest { |
| 54 public: |
| 55 SettingsRootCollectionViewControllerTest() |
| 56 : controller_([[TestSettingsRootCollectionViewController alloc] |
| 57 initWithStyle:CollectionViewControllerStyleDefault]) { |
| 58 item1_.reset([[CollectionViewItem alloc] initWithType:kItemTypeEnumZero]); |
| 59 item2_.reset( |
| 60 [[CollectionViewItem alloc] initWithType:kItemTypeEnumZero + 10]); |
| 61 } |
| 62 |
| 63 protected: |
| 64 void SetUp() override { |
| 65 PlatformTest::SetUp(); |
| 66 [controller() reset]; |
| 67 } |
| 68 |
| 69 TestSettingsRootCollectionViewController* controller() { return controller_; } |
| 70 CollectionViewItem* item1() { return item1_; } |
| 71 CollectionViewItem* item2() { return item2_; } |
| 72 |
| 73 id getCollectionViewItem(int section, int item) { |
| 74 return [controller().collectionViewModel |
| 75 itemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:section]]; |
| 76 } |
| 77 |
| 78 int numberOfSections() { |
| 79 return [controller().collectionViewModel numberOfSections]; |
| 80 } |
| 81 |
| 82 int numberOfItems(int section) { |
| 83 return [controller().collectionViewModel numberOfItemsInSection:section]; |
| 84 } |
| 85 |
| 86 base::scoped_nsobject<TestSettingsRootCollectionViewController> controller_; |
| 87 base::scoped_nsobject<CollectionViewItem> item1_; |
| 88 base::scoped_nsobject<CollectionViewItem> item2_; |
| 89 }; |
| 90 |
| 91 TEST_F(SettingsRootCollectionViewControllerTest, Empty) { |
| 92 [controller() loadModel]; |
| 93 EXPECT_EQ(0, numberOfSections()); |
| 94 } |
| 95 |
| 96 TEST_F(SettingsRootCollectionViewControllerTest, ModelContents) { |
| 97 [controller().items addObjectsFromArray:@[ item1(), item2() ]]; |
| 98 [controller() loadModel]; |
| 99 EXPECT_EQ(1, numberOfSections()); |
| 100 EXPECT_EQ(2, numberOfItems(0)); |
| 101 EXPECT_EQ(item1(), getCollectionViewItem(0, 0)); |
| 102 EXPECT_EQ(item2(), getCollectionViewItem(0, 1)); |
| 103 } |
| 104 |
| 105 } // namespace |
OLD | NEW |