| 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/time_range_selector_collection_view_cont
roller.h" | 5 #import "ios/chrome/browser/ui/settings/time_range_selector_collection_view_cont
roller.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "components/browsing_data/core/pref_names.h" | 9 #include "components/browsing_data/core/pref_names.h" |
| 10 #include "components/prefs/pref_registry_simple.h" | 10 #include "components/prefs/pref_registry_simple.h" |
| 11 #include "components/prefs/pref_service.h" | 11 #include "components/prefs/pref_service.h" |
| 12 #include "components/sync_preferences/pref_service_mock_factory.h" | 12 #include "components/sync_preferences/pref_service_mock_factory.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 14 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" | 14 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 15 #include "ios/chrome/grit/ios_strings.h" | 15 #include "ios/chrome/grit/ios_strings.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/ocmock/OCMock/OCMock.h" | 17 #include "third_party/ocmock/OCMock/OCMock.h" |
| 18 #import "third_party/ocmock/gtest_support.h" | 18 #import "third_party/ocmock/gtest_support.h" |
| 19 | 19 |
| 20 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 21 #error "This file requires ARC support." |
| 22 #endif |
| 23 |
| 20 @interface TimeRangeSelectorCollectionViewController (ExposedForTesting) | 24 @interface TimeRangeSelectorCollectionViewController (ExposedForTesting) |
| 21 - (void)updatePrefValue:(int)prefValue; | 25 - (void)updatePrefValue:(int)prefValue; |
| 22 @end | 26 @end |
| 23 | 27 |
| 24 namespace { | 28 namespace { |
| 25 | 29 |
| 26 const NSInteger kNumberOfItems = 5; | 30 const NSInteger kNumberOfItems = 5; |
| 27 | 31 |
| 28 class TimeRangeSelectorCollectionViewControllerTest | 32 class TimeRangeSelectorCollectionViewControllerTest |
| 29 : public CollectionViewControllerTest { | 33 : public CollectionViewControllerTest { |
| 30 protected: | 34 protected: |
| 31 void SetUp() override { | 35 void SetUp() override { |
| 32 CollectionViewControllerTest::SetUp(); | 36 CollectionViewControllerTest::SetUp(); |
| 33 pref_service_ = CreateLocalState(); | 37 pref_service_ = CreateLocalState(); |
| 34 delegate_.reset([[OCMockObject | 38 delegate_ = [OCMockObject |
| 35 mockForProtocol:@protocol( | 39 mockForProtocol:@protocol( |
| 36 TimeRangeSelectorCollectionViewControllerDelegate)] | 40 TimeRangeSelectorCollectionViewControllerDelegate)]; |
| 37 retain]); | |
| 38 CreateController(); | 41 CreateController(); |
| 39 } | 42 } |
| 40 | 43 |
| 41 CollectionViewController* NewController() override NS_RETURNS_RETAINED { | 44 CollectionViewController* InstantiateController() override { |
| 42 time_range_selector_controller_.reset( | 45 time_range_selector_controller_ = |
| 43 [[TimeRangeSelectorCollectionViewController alloc] | 46 [[TimeRangeSelectorCollectionViewController alloc] |
| 44 initWithPrefs:pref_service_.get() | 47 initWithPrefs:pref_service_.get() |
| 45 delegate:delegate_.get()]); | 48 delegate:delegate_]; |
| 46 return [time_range_selector_controller_ retain]; | 49 return time_range_selector_controller_; |
| 47 } | 50 } |
| 48 | 51 |
| 49 std::unique_ptr<PrefService> CreateLocalState() { | 52 std::unique_ptr<PrefService> CreateLocalState() { |
| 50 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); | 53 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple()); |
| 51 registry->RegisterIntegerPref(browsing_data::prefs::kDeleteTimePeriod, 0); | 54 registry->RegisterIntegerPref(browsing_data::prefs::kDeleteTimePeriod, 0); |
| 52 | 55 |
| 53 sync_preferences::PrefServiceMockFactory factory; | 56 sync_preferences::PrefServiceMockFactory factory; |
| 54 return factory.Create(registry.get()); | 57 return factory.Create(registry.get()); |
| 55 } | 58 } |
| 56 | 59 |
| 57 // Verifies that the cell at |item| in |section| has the given |accessory| | 60 // Verifies that the cell at |item| in |section| has the given |accessory| |
| 58 // type. | 61 // type. |
| 59 void CheckTextItemAccessoryType(int accessory_type, int section, int item) { | 62 void CheckTextItemAccessoryType(int accessory_type, int section, int item) { |
| 60 CollectionViewTextItem* cell = GetCollectionViewItem(section, item); | 63 CollectionViewTextItem* cell = GetCollectionViewItem(section, item); |
| 61 EXPECT_EQ(accessory_type, cell.accessoryType); | 64 EXPECT_EQ(accessory_type, cell.accessoryType); |
| 62 } | 65 } |
| 63 | 66 |
| 64 base::MessageLoopForUI message_loop_; | 67 base::MessageLoopForUI message_loop_; |
| 65 std::unique_ptr<PrefService> pref_service_; | 68 std::unique_ptr<PrefService> pref_service_; |
| 66 base::scoped_nsobject<id> delegate_; | 69 id delegate_; |
| 67 base::scoped_nsobject<TimeRangeSelectorCollectionViewController> | 70 TimeRangeSelectorCollectionViewController* time_range_selector_controller_; |
| 68 time_range_selector_controller_; | |
| 69 }; | 71 }; |
| 70 | 72 |
| 71 TEST_F(TimeRangeSelectorCollectionViewControllerTest, TestModel) { | 73 TEST_F(TimeRangeSelectorCollectionViewControllerTest, TestModel) { |
| 72 CheckController(); | 74 CheckController(); |
| 73 EXPECT_EQ(1, NumberOfSections()); | 75 EXPECT_EQ(1, NumberOfSections()); |
| 74 | 76 |
| 75 // No section header + 5 rows | 77 // No section header + 5 rows |
| 76 EXPECT_EQ(kNumberOfItems, NumberOfItemsInSection(0)); | 78 EXPECT_EQ(kNumberOfItems, NumberOfItemsInSection(0)); |
| 77 | 79 |
| 78 CheckTextItemAccessoryType(MDCCollectionViewCellAccessoryCheckmark, 0, 0); | 80 CheckTextItemAccessoryType(MDCCollectionViewCellAccessoryCheckmark, 0, 0); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 107 } else { | 109 } else { |
| 108 CheckTextItemAccessoryType(MDCCollectionViewCellAccessoryNone, 0, item); | 110 CheckTextItemAccessoryType(MDCCollectionViewCellAccessoryNone, 0, item); |
| 109 } | 111 } |
| 110 } | 112 } |
| 111 } | 113 } |
| 112 } | 114 } |
| 113 | 115 |
| 114 TEST_F(TimeRangeSelectorCollectionViewControllerTest, TestUpdatePrefValue) { | 116 TEST_F(TimeRangeSelectorCollectionViewControllerTest, TestUpdatePrefValue) { |
| 115 CheckController(); | 117 CheckController(); |
| 116 UICollectionView* collectionView = | 118 UICollectionView* collectionView = |
| 117 time_range_selector_controller_.get().collectionView; | 119 time_range_selector_controller_.collectionView; |
| 118 for (NSInteger checkedItem = 0; checkedItem < kNumberOfItems; ++checkedItem) { | 120 for (NSInteger checkedItem = 0; checkedItem < kNumberOfItems; ++checkedItem) { |
| 119 NSIndexPath* indexPath = | 121 NSIndexPath* indexPath = |
| 120 [NSIndexPath indexPathForItem:checkedItem inSection:0]; | 122 [NSIndexPath indexPathForItem:checkedItem inSection:0]; |
| 121 [[delegate_.get() expect] | 123 [[delegate_ expect] |
| 122 timeRangeSelectorViewController:time_range_selector_controller_.get() | 124 timeRangeSelectorViewController:time_range_selector_controller_ |
| 123 didSelectTimePeriod:static_cast<browsing_data::TimePeriod>( | 125 didSelectTimePeriod:static_cast<browsing_data::TimePeriod>( |
| 124 checkedItem)]; | 126 checkedItem)]; |
| 125 [time_range_selector_controller_ collectionView:collectionView | 127 [time_range_selector_controller_ collectionView:collectionView |
| 126 didSelectItemAtIndexPath:indexPath]; | 128 didSelectItemAtIndexPath:indexPath]; |
| 127 EXPECT_EQ( | 129 EXPECT_EQ( |
| 128 pref_service_->GetInteger(browsing_data::prefs::kDeleteTimePeriod), | 130 pref_service_->GetInteger(browsing_data::prefs::kDeleteTimePeriod), |
| 129 checkedItem); | 131 checkedItem); |
| 130 EXPECT_OCMOCK_VERIFY(delegate_); | 132 EXPECT_OCMOCK_VERIFY(delegate_); |
| 131 } | 133 } |
| 132 } | 134 } |
| 133 | 135 |
| 134 } // namespace | 136 } // namespace |
| OLD | NEW |