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" |
| 8 #include "base/logging.h" |
7 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
8 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 10 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "testing/gtest_mac.h" | 12 #include "testing/gtest_mac.h" |
11 | 13 |
12 #if !defined(__has_feature) || !__has_feature(objc_arc) | 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
13 #error "This file requires ARC support." | 15 #error "This file requires ARC support." |
14 #endif | 16 #endif |
15 | 17 |
16 @interface CollectionViewModel (Testing) | 18 @interface CollectionViewModel (Testing) |
(...skipping 30 matching lines...) Expand all Loading... |
47 ItemTypeCheeseHeader = kItemTypeEnumZero, | 49 ItemTypeCheeseHeader = kItemTypeEnumZero, |
48 ItemTypeCheeseCheddar, | 50 ItemTypeCheeseCheddar, |
49 ItemTypeCheeseGouda, | 51 ItemTypeCheeseGouda, |
50 ItemTypeCheesePepperJack, | 52 ItemTypeCheesePepperJack, |
51 ItemTypeWeasleyRon, | 53 ItemTypeWeasleyRon, |
52 ItemTypeWeasleyGinny, | 54 ItemTypeWeasleyGinny, |
53 ItemTypeWeasleyArthur, | 55 ItemTypeWeasleyArthur, |
54 ItemTypeWeasleyFooter, | 56 ItemTypeWeasleyFooter, |
55 }; | 57 }; |
56 | 58 |
57 void LogSink(const std::string& str) { | 59 void LogSink(const char* file, |
| 60 int line, |
| 61 const base::StringPiece& message, |
| 62 const base::StringPiece& stack_trace) { |
58 // No-op. | 63 // No-op. |
59 } | 64 } |
60 | 65 |
61 // Test generic model boxing (check done at compilation time). | 66 // Test generic model boxing (check done at compilation time). |
62 TEST(CollectionViewModelTest, GenericModelBoxing) { | 67 TEST(CollectionViewModelTest, GenericModelBoxing) { |
63 CollectionViewModel<TestCollectionViewItemSubclass*>* specificModel = | 68 CollectionViewModel<TestCollectionViewItemSubclass*>* specificModel = |
64 [[CollectionViewModel alloc] init]; | 69 [[CollectionViewModel alloc] init]; |
65 | 70 |
66 // |generalModel| is a superclass of |specificModel|. So specificModel can be | 71 // |generalModel| is a superclass of |specificModel|. So specificModel can be |
67 // boxed into generalModel, but not the other way around. | 72 // boxed into generalModel, but not the other way around. |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 EXPECT_EQ(3U, [cheeseItems count]); | 310 EXPECT_EQ(3U, [cheeseItems count]); |
306 EXPECT_NSEQ(cheddar, cheeseItems[0]); | 311 EXPECT_NSEQ(cheddar, cheeseItems[0]); |
307 EXPECT_NSEQ(pepperJack, cheeseItems[1]); | 312 EXPECT_NSEQ(pepperJack, cheeseItems[1]); |
308 EXPECT_NSEQ(gouda, cheeseItems[2]); | 313 EXPECT_NSEQ(gouda, cheeseItems[2]); |
309 } | 314 } |
310 | 315 |
311 TEST(CollectionViewModelTest, InvalidIndexPath) { | 316 TEST(CollectionViewModelTest, InvalidIndexPath) { |
312 CollectionViewModel* model = [[CollectionViewModel alloc] init]; | 317 CollectionViewModel* model = [[CollectionViewModel alloc] init]; |
313 [model addSectionWithIdentifier:SectionIdentifierCheese]; | 318 [model addSectionWithIdentifier:SectionIdentifierCheese]; |
314 | 319 |
315 logging::SetLogAssertHandler(&LogSink); | 320 logging::ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); |
316 bool out_of_bounds_exception_thrown = false; | 321 bool out_of_bounds_exception_thrown = false; |
317 @try { | 322 @try { |
318 [model indexInItemTypeForIndexPath:[NSIndexPath indexPathForItem:0 | 323 [model indexInItemTypeForIndexPath:[NSIndexPath indexPathForItem:0 |
319 inSection:0]]; | 324 inSection:0]]; |
320 } @catch (NSException* exception) { | 325 } @catch (NSException* exception) { |
321 if ([[exception name] isEqualToString:NSRangeException]) { | 326 if ([[exception name] isEqualToString:NSRangeException]) { |
322 out_of_bounds_exception_thrown = true; | 327 out_of_bounds_exception_thrown = true; |
323 } | 328 } |
324 } | 329 } |
325 EXPECT_TRUE(out_of_bounds_exception_thrown); | 330 EXPECT_TRUE(out_of_bounds_exception_thrown); |
326 logging::SetLogAssertHandler(nullptr); | |
327 } | 331 } |
328 | 332 |
329 TEST(CollectionViewModelTest, RemoveItems) { | 333 TEST(CollectionViewModelTest, RemoveItems) { |
330 CollectionViewModel* model = [[CollectionViewModel alloc] init]; | 334 CollectionViewModel* model = [[CollectionViewModel alloc] init]; |
331 | 335 |
332 [model addSectionWithIdentifier:SectionIdentifierCheese]; | 336 [model addSectionWithIdentifier:SectionIdentifierCheese]; |
333 [model addItemWithType:ItemTypeCheesePepperJack | 337 [model addItemWithType:ItemTypeCheesePepperJack |
334 toSectionWithIdentifier:SectionIdentifierCheese]; | 338 toSectionWithIdentifier:SectionIdentifierCheese]; |
335 [model addItemWithType:ItemTypeCheeseGouda | 339 [model addItemWithType:ItemTypeCheeseGouda |
336 toSectionWithIdentifier:SectionIdentifierCheese]; | 340 toSectionWithIdentifier:SectionIdentifierCheese]; |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 | 589 |
586 EXPECT_FALSE([model footerForSectionWithIdentifier:SectionIdentifierCheese]); | 590 EXPECT_FALSE([model footerForSectionWithIdentifier:SectionIdentifierCheese]); |
587 EXPECT_FALSE([model footerForSection:cheeseSection]); | 591 EXPECT_FALSE([model footerForSection:cheeseSection]); |
588 | 592 |
589 EXPECT_EQ(weasleyFooter, | 593 EXPECT_EQ(weasleyFooter, |
590 [model footerForSectionWithIdentifier:SectionIdentifierWeasley]); | 594 [model footerForSectionWithIdentifier:SectionIdentifierWeasley]); |
591 EXPECT_EQ(weasleyFooter, [model footerForSection:weasleySection]); | 595 EXPECT_EQ(weasleyFooter, [model footerForSection:weasleySection]); |
592 } | 596 } |
593 | 597 |
594 } // namespace | 598 } // namespace |
OLD | NEW |