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