| 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_model.h" | 5 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/test/logging_utils.h" |
| 9 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 11 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gtest_mac.h" | 13 #include "testing/gtest_mac.h" |
| 12 | 14 |
| 13 @interface CollectionViewModel (Testing) | 15 @interface CollectionViewModel (Testing) |
| 14 // Adds an item with the given type to the section with the given identifier. | 16 // Adds an item with the given type to the section with the given identifier. |
| 15 // It is possible to add multiple items with the same type to the same section. | 17 // It is possible to add multiple items with the same type to the same section. |
| 16 // Sharing types across sections is undefined behavior. | 18 // Sharing types across sections is undefined behavior. |
| 17 - (void)addItemWithType:(NSInteger)itemType | 19 - (void)addItemWithType:(NSInteger)itemType |
| 18 toSectionWithIdentifier:(NSInteger)sectionIdentifier; | 20 toSectionWithIdentifier:(NSInteger)sectionIdentifier; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 40 ItemTypeCheeseHeader = kItemTypeEnumZero, | 42 ItemTypeCheeseHeader = kItemTypeEnumZero, |
| 41 ItemTypeCheeseCheddar, | 43 ItemTypeCheeseCheddar, |
| 42 ItemTypeCheeseGouda, | 44 ItemTypeCheeseGouda, |
| 43 ItemTypeCheesePepperJack, | 45 ItemTypeCheesePepperJack, |
| 44 ItemTypeWeasleyRon, | 46 ItemTypeWeasleyRon, |
| 45 ItemTypeWeasleyGinny, | 47 ItemTypeWeasleyGinny, |
| 46 ItemTypeWeasleyArthur, | 48 ItemTypeWeasleyArthur, |
| 47 ItemTypeWeasleyFooter, | 49 ItemTypeWeasleyFooter, |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 void LogSink(const std::string& str) { | 52 void LogSink(const char* file, |
| 53 int line, |
| 54 size_t message_start, |
| 55 size_t stack_start, |
| 56 const std::string& str) { |
| 51 // No-op. | 57 // No-op. |
| 52 } | 58 } |
| 53 | 59 |
| 54 TEST(CollectionViewModelTest, EmptyModel) { | 60 TEST(CollectionViewModelTest, EmptyModel) { |
| 55 base::scoped_nsobject<CollectionViewModel> model( | 61 base::scoped_nsobject<CollectionViewModel> model( |
| 56 [[CollectionViewModel alloc] init]); | 62 [[CollectionViewModel alloc] init]); |
| 57 | 63 |
| 58 // Check there are no items. | 64 // Check there are no items. |
| 59 EXPECT_EQ(NO, [model hasItemAtIndexPath:[NSIndexPath indexPathForItem:0 | 65 EXPECT_EQ(NO, [model hasItemAtIndexPath:[NSIndexPath indexPathForItem:0 |
| 60 inSection:0]]); | 66 inSection:0]]); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 EXPECT_NSEQ(cheddar, cheeseItems[0]); | 301 EXPECT_NSEQ(cheddar, cheeseItems[0]); |
| 296 EXPECT_NSEQ(pepperJack, cheeseItems[1]); | 302 EXPECT_NSEQ(pepperJack, cheeseItems[1]); |
| 297 EXPECT_NSEQ(gouda, cheeseItems[2]); | 303 EXPECT_NSEQ(gouda, cheeseItems[2]); |
| 298 } | 304 } |
| 299 | 305 |
| 300 TEST(CollectionViewModelTest, InvalidIndexPath) { | 306 TEST(CollectionViewModelTest, InvalidIndexPath) { |
| 301 base::scoped_nsobject<CollectionViewModel> model( | 307 base::scoped_nsobject<CollectionViewModel> model( |
| 302 [[CollectionViewModel alloc] init]); | 308 [[CollectionViewModel alloc] init]); |
| 303 [model addSectionWithIdentifier:SectionIdentifierCheese]; | 309 [model addSectionWithIdentifier:SectionIdentifierCheese]; |
| 304 | 310 |
| 305 logging::SetLogAssertHandler(&LogSink); | 311 logging::ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
| 306 bool out_of_bounds_exception_thrown = false; | 312 bool out_of_bounds_exception_thrown = false; |
| 307 @try { | 313 @try { |
| 308 [model indexInItemTypeForIndexPath:[NSIndexPath indexPathForItem:0 | 314 [model indexInItemTypeForIndexPath:[NSIndexPath indexPathForItem:0 |
| 309 inSection:0]]; | 315 inSection:0]]; |
| 310 } @catch (NSException* exception) { | 316 } @catch (NSException* exception) { |
| 311 if ([[exception name] isEqualToString:NSRangeException]) { | 317 if ([[exception name] isEqualToString:NSRangeException]) { |
| 312 out_of_bounds_exception_thrown = true; | 318 out_of_bounds_exception_thrown = true; |
| 313 } | 319 } |
| 314 } | 320 } |
| 315 EXPECT_TRUE(out_of_bounds_exception_thrown); | 321 EXPECT_TRUE(out_of_bounds_exception_thrown); |
| 316 logging::SetLogAssertHandler(nullptr); | |
| 317 } | 322 } |
| 318 | 323 |
| 319 TEST(CollectionViewModelTest, RemoveItems) { | 324 TEST(CollectionViewModelTest, RemoveItems) { |
| 320 base::scoped_nsobject<CollectionViewModel> model( | 325 base::scoped_nsobject<CollectionViewModel> model( |
| 321 [[CollectionViewModel alloc] init]); | 326 [[CollectionViewModel alloc] init]); |
| 322 | 327 |
| 323 [model addSectionWithIdentifier:SectionIdentifierCheese]; | 328 [model addSectionWithIdentifier:SectionIdentifierCheese]; |
| 324 [model addItemWithType:ItemTypeCheesePepperJack | 329 [model addItemWithType:ItemTypeCheesePepperJack |
| 325 toSectionWithIdentifier:SectionIdentifierCheese]; | 330 toSectionWithIdentifier:SectionIdentifierCheese]; |
| 326 [model addItemWithType:ItemTypeCheeseGouda | 331 [model addItemWithType:ItemTypeCheeseGouda |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 | 588 |
| 584 EXPECT_FALSE([model footerForSectionWithIdentifier:SectionIdentifierCheese]); | 589 EXPECT_FALSE([model footerForSectionWithIdentifier:SectionIdentifierCheese]); |
| 585 EXPECT_FALSE([model footerForSection:cheeseSection]); | 590 EXPECT_FALSE([model footerForSection:cheeseSection]); |
| 586 | 591 |
| 587 EXPECT_EQ(weasleyFooter.get(), | 592 EXPECT_EQ(weasleyFooter.get(), |
| 588 [model footerForSectionWithIdentifier:SectionIdentifierWeasley]); | 593 [model footerForSectionWithIdentifier:SectionIdentifierWeasley]); |
| 589 EXPECT_EQ(weasleyFooter.get(), [model footerForSection:weasleySection]); | 594 EXPECT_EQ(weasleyFooter.get(), [model footerForSection:weasleySection]); |
| 590 } | 595 } |
| 591 | 596 |
| 592 } // namespace | 597 } // namespace |
| OLD | NEW |