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_test.h
" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/logging.h" |
| 9 #import "base/mac/foundation_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" |
| 12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 17 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 18 |
| 19 #include "testing/gtest_mac.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 |
| 22 CollectionViewControllerTest::CollectionViewControllerTest() {} |
| 23 |
| 24 CollectionViewControllerTest::~CollectionViewControllerTest() {} |
| 25 |
| 26 void CollectionViewControllerTest::TearDown() { |
| 27 // Delete the controller before deleting other test variables, such as a |
| 28 // profile, to ensure things are cleaned up in the same order as in Chrome. |
| 29 controller_.reset(); |
| 30 BlockCleanupTest::TearDown(); |
| 31 } |
| 32 |
| 33 void CollectionViewControllerTest::CreateController() { |
| 34 DCHECK(!controller_); |
| 35 controller_.reset(NewController()); |
| 36 // Force the model to be loaded. |
| 37 [controller_ loadModel]; |
| 38 // Force the collectionView to be built. |
| 39 EXPECT_TRUE([controller_ view]); |
| 40 } |
| 41 |
| 42 CollectionViewController* CollectionViewControllerTest::controller() { |
| 43 if (!controller_) |
| 44 CreateController(); |
| 45 return controller_.get(); |
| 46 } |
| 47 |
| 48 void CollectionViewControllerTest::ResetController() { |
| 49 controller_.reset(); |
| 50 } |
| 51 |
| 52 void CollectionViewControllerTest::CheckController() { |
| 53 EXPECT_TRUE([controller_ view]); |
| 54 EXPECT_TRUE([controller_ collectionView]); |
| 55 EXPECT_TRUE([controller_ collectionViewModel]); |
| 56 EXPECT_EQ(controller_.get(), [controller_ collectionView].dataSource); |
| 57 EXPECT_EQ(controller_.get(), [controller_ collectionView].delegate); |
| 58 } |
| 59 |
| 60 int CollectionViewControllerTest::NumberOfSections() { |
| 61 return [[controller_ collectionViewModel] numberOfSections]; |
| 62 } |
| 63 |
| 64 int CollectionViewControllerTest::NumberOfItemsInSection(int section) { |
| 65 return [[controller_ collectionViewModel] numberOfItemsInSection:section]; |
| 66 } |
| 67 |
| 68 id CollectionViewControllerTest::GetCollectionViewItem(int section, int item) { |
| 69 CollectionViewModel* model = [controller_ collectionViewModel]; |
| 70 NSIndexPath* index_path = |
| 71 [NSIndexPath indexPathForItem:item inSection:section]; |
| 72 CollectionViewItem* collection_view_item = |
| 73 [model hasItemAtIndexPath:index_path] ? [model itemAtIndexPath:index_path] |
| 74 : nil; |
| 75 EXPECT_TRUE(collection_view_item); |
| 76 return collection_view_item; |
| 77 } |
| 78 |
| 79 void CollectionViewControllerTest::CheckTitle(NSString* expected_title) { |
| 80 EXPECT_NSEQ(expected_title, [controller_ title]); |
| 81 } |
| 82 |
| 83 void CollectionViewControllerTest::CheckTitleWithId(int expected_title_id) { |
| 84 CheckTitle(l10n_util::GetNSString(expected_title_id)); |
| 85 } |
| 86 |
| 87 void CollectionViewControllerTest::CheckSectionHeader(NSString* expected_title, |
| 88 int section) { |
| 89 CollectionViewItem* header = |
| 90 [[controller_ collectionViewModel] headerForSection:section]; |
| 91 ASSERT_TRUE([header respondsToSelector:@selector(text)]); |
| 92 EXPECT_NSEQ(expected_title, [(id)header text]); |
| 93 } |
| 94 |
| 95 void CollectionViewControllerTest::CheckSectionHeaderWithId( |
| 96 int expected_title_id, |
| 97 int section) { |
| 98 CheckSectionHeader(l10n_util::GetNSString(expected_title_id), section); |
| 99 } |
| 100 |
| 101 void CollectionViewControllerTest::CheckSectionFooter(NSString* expected_text, |
| 102 int section) { |
| 103 ASSERT_EQ(1, NumberOfItemsInSection(section)); |
| 104 CollectionViewFooterItem* footer_item = |
| 105 base::mac::ObjCCastStrict<CollectionViewFooterItem>( |
| 106 GetCollectionViewItem(section, 0)); |
| 107 EXPECT_NSEQ(expected_text, footer_item.text); |
| 108 } |
| 109 |
| 110 void CollectionViewControllerTest::CheckSectionFooterWithId( |
| 111 int expected_text_id, |
| 112 int section) { |
| 113 return CheckSectionFooter(l10n_util::GetNSString(expected_text_id), section); |
| 114 } |
| 115 |
| 116 void CollectionViewControllerTest::CheckTextCellTitle(NSString* expected_title, |
| 117 int section, |
| 118 int item) { |
| 119 CollectionViewTextItem* cell = GetCollectionViewItem(section, item); |
| 120 EXPECT_NSEQ(expected_title, cell.text); |
| 121 EXPECT_FALSE(cell.detailText); |
| 122 } |
| 123 |
| 124 void CollectionViewControllerTest::CheckTextCellTitleWithId( |
| 125 int expected_title_id, |
| 126 int section, |
| 127 int item) { |
| 128 CheckTextCellTitle(l10n_util::GetNSString(expected_title_id), section, item); |
| 129 } |
| 130 |
| 131 void CollectionViewControllerTest::CheckTextCellTitleAndSubtitle( |
| 132 NSString* expected_title, |
| 133 NSString* expected_subtitle, |
| 134 int section, |
| 135 int item) { |
| 136 CollectionViewTextItem* cell = GetCollectionViewItem(section, item); |
| 137 EXPECT_NSEQ(expected_title, cell.text); |
| 138 EXPECT_NSEQ(expected_subtitle, cell.detailText); |
| 139 } |
| 140 |
| 141 void CollectionViewControllerTest::CheckDetailItemTextWithIds( |
| 142 int expected_text_id, |
| 143 int expected_detail_text_id, |
| 144 int section_id, |
| 145 int item_id) { |
| 146 CollectionViewDetailItem* item = GetCollectionViewItem(section_id, item_id); |
| 147 EXPECT_NSEQ(l10n_util::GetNSString(expected_text_id), item.text); |
| 148 EXPECT_NSEQ(l10n_util::GetNSString(expected_detail_text_id), item.detailText); |
| 149 } |
| 150 |
| 151 void CollectionViewControllerTest::CheckSwitchCellStateAndTitle( |
| 152 BOOL expected_state, |
| 153 NSString* expected_title, |
| 154 int section, |
| 155 int item) { |
| 156 CollectionViewSwitchItem* cell = GetCollectionViewItem(section, item); |
| 157 EXPECT_NSEQ(expected_title, cell.text); |
| 158 EXPECT_EQ(expected_state, cell.isOn); |
| 159 } |
| 160 |
| 161 void CollectionViewControllerTest::CheckSwitchCellStateAndTitleWithId( |
| 162 BOOL expected_state, |
| 163 int expected_title_id, |
| 164 int section, |
| 165 int item) { |
| 166 CheckSwitchCellStateAndTitle( |
| 167 expected_state, l10n_util::GetNSString(expected_title_id), section, item); |
| 168 } |
| 169 |
| 170 void CollectionViewControllerTest::CheckAccessoryType( |
| 171 MDCCollectionViewCellAccessoryType accessory_type, |
| 172 int section, |
| 173 int item) { |
| 174 id text_item = GetCollectionViewItem(section, item); |
| 175 EXPECT_TRUE([text_item respondsToSelector:@selector(accessoryType)]); |
| 176 EXPECT_EQ(accessory_type, |
| 177 (MDCCollectionViewCellAccessoryType)[text_item accessoryType]); |
| 178 } |
| 179 |
| 180 void CollectionViewControllerTest::DeleteItem( |
| 181 int section, |
| 182 int item, |
| 183 ProceduralBlock completion_block) { |
| 184 NSIndexPath* index_path = |
| 185 [NSIndexPath indexPathForItem:item inSection:section]; |
| 186 base::WeakNSObject<CollectionViewController> weak_controller(controller_); |
| 187 void (^batch_updates)() = ^{ |
| 188 base::scoped_nsobject<CollectionViewController> strong_controller( |
| 189 [weak_controller retain]); |
| 190 if (!strong_controller) |
| 191 return; |
| 192 // Notify delegate to delete data. |
| 193 [strong_controller collectionView:[strong_controller collectionView] |
| 194 willDeleteItemsAtIndexPaths:@[ index_path ]]; |
| 195 |
| 196 // Delete index paths. |
| 197 [[strong_controller collectionView] |
| 198 deleteItemsAtIndexPaths:@[ index_path ]]; |
| 199 }; |
| 200 |
| 201 void (^completion)(BOOL finished) = ^(BOOL finished) { |
| 202 // Notify delegate of deletion. |
| 203 base::scoped_nsobject<CollectionViewController> strong_controller( |
| 204 [weak_controller retain]); |
| 205 if (!strong_controller) |
| 206 return; |
| 207 [strong_controller collectionView:[strong_controller collectionView] |
| 208 didDeleteItemsAtIndexPaths:@[ index_path ]]; |
| 209 if (completion_block) { |
| 210 completion_block(); |
| 211 } |
| 212 }; |
| 213 |
| 214 [[controller_ collectionView] performBatchUpdates:batch_updates |
| 215 completion:completion]; |
| 216 } |
OLD | NEW |