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